:root {
  --bg: #111318;
  --panel: #1a1d24;
  --panel-2: #232733;
  --text: #f2f4f8;
  --muted: #aeb6c7;
  --line: #343a4b;
  --accent: #6aa3ff;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.32);
  --radius: 12px;
  --sidebar-width: clamp(230px, 16vw, 310px);
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; }
body {
  font-family: "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  color: var(--text);
  background: var(--bg);
}

button, input, select, textarea { font: inherit; }
button, .file-btn {
  min-height: 38px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 9px;
  padding: 8px 10px;
  cursor: pointer;
}
button:hover, .file-btn:hover { filter: brightness(1.08); }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #0b1526;
  font-weight: 700;
}
button.ghost { background: transparent; }

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

.work-sidebar {
  min-width: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #15181f;
  border-right: 1px solid var(--line);
  overflow: hidden;
}
.tool-brand {
  flex: 0 0 auto;
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--line);
}
.tool-brand h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.3;
}
.tool-brand p {
  margin: 5px 0 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}
.version {
  font-size: 12px;
  color: #9ec1ff;
}
.sidebar-scroll {
  min-height: 0;
  overflow-y: auto;
  padding: 10px;
  scrollbar-width: thin;
}

.control-card {
  padding: 11px;
  margin-bottom: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.control-card:last-child { margin-bottom: 0; }
.control-card h2 {
  margin: 0 0 10px;
  font-size: 14px;
}
.field-stack, .button-stack {
  display: grid;
  gap: 8px;
}
.button-stack { margin-bottom: 10px; }
.field-grid, .button-grid {
  display: grid;
  gap: 8px;
}
.two-cols { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.compact-actions { margin-top: 9px; }
.mode-top-gap { margin-top: 9px; }
.full-button { width: 100%; margin-top: 8px; }

label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
  color: var(--muted);
  font-size: 12px;
}
.file-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
}
.file-btn.primary-file {
  background: #2d3b58;
  border-color: #465b83;
  font-weight: 700;
}
.file-btn.alt { background: #293042; }
input[type="number"], input[type="text"], select, textarea {
  width: 100%;
  min-width: 0;
  min-height: 38px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #11151c;
  color: var(--text);
}
textarea { resize: vertical; line-height: 1.45; }
.background-row {
  display: grid;
  grid-template-columns: 62px minmax(0, 1fr);
  gap: 8px;
  align-items: end;
}
input[type="color"] {
  width: 100%;
  height: 38px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: transparent;
}
.check-field {
  justify-content: end;
  gap: 8px;
}
.check-field input {
  width: 22px;
  height: 22px;
  accent-color: var(--accent);
}
.size-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 9px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #121720;
  font-size: 12px;
  color: var(--muted);
}
.size-box strong { color: var(--text); font-size: 13px; }
.status-block {
  display: grid;
  gap: 5px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #121720;
  color: var(--muted);
  font-size: 12px;
}
.status-block strong { color: var(--text); }
.object-status { margin-bottom: 9px; }
.sidebar-help {
  margin: 8px 1px 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.45;
}

.scale-control-block {
  margin-bottom: 10px;
}
.subsection-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
}
input:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.export-button { width: 100%; min-height: 42px; margin-top: 10px; }
.danger-button { border-color: #6f3a43; background: #3a2026; }

.workspace {
  min-width: 0;
  height: 100vh;
  padding: 10px;
  overflow: hidden;
}
.preview-panel {
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.preview-toolbar {
  flex: 0 0 auto;
  min-height: 38px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  padding: 0 2px 8px;
}
.preview-toolbar strong { font-size: 15px; }
.hint, .preview-meta {
  color: var(--muted);
  font-size: 12px;
}
.hint { margin-left: 7px; }
.preview-wrap {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 1px solid #2d3342;
  border-radius: 10px;
  background: #0e1117;
}
#previewCanvas {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  background: #666;
}
.cell-overlay,
.free-object-overlay {
  position: absolute;
  inset: 0;
}
.cell-overlay { pointer-events: auto; }
.free-object-overlay { pointer-events: none; }
.overlay-cell {
  position: absolute;
  padding: 0;
  min-height: 0;
  border: 1px dashed rgba(255,255,255,0.34);
  border-radius: 0;
  background: rgba(255,255,255,0.018);
  cursor: pointer;
}
.overlay-cell:hover { border-color: rgba(255,255,255,0.8); }
.overlay-cell.active {
  z-index: 2;
  border: 3px solid var(--accent);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.18);
}
.overlay-cell .cell-badge,
.overlay-cell .cell-state {
  position: absolute;
  padding: 3px 7px;
  border-radius: 999px;
  background: rgba(0,0,0,0.58);
  font-size: 11px;
  line-height: 1.3;
}
.overlay-cell .cell-badge { top: 6px; left: 6px; }
.overlay-cell .cell-state { right: 6px; bottom: 6px; color: var(--muted); }

.free-object-box {
  position: absolute;
  min-width: 24px;
  min-height: 24px;
  border: 1px dashed transparent;
  transform-origin: center center;
  cursor: move;
  pointer-events: auto;
  touch-action: none;
}
.free-object-box:hover { border-color: rgba(255,255,255,0.62); }
.free-object-box.active {
  border: 2px solid var(--accent);
  box-shadow: 0 0 0 1px rgba(0,0,0,0.45);
}
.free-handle {
  position: absolute;
  width: 15px;
  height: 15px;
  min-height: 0;
  padding: 0;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 1px 5px rgba(0,0,0,.5);
  touch-action: none;
}
.resize-handle {
  right: -9px;
  bottom: -9px;
  cursor: nwse-resize;
}
.rotate-handle {
  left: 50%;
  top: -29px;
  transform: translateX(-50%);
  cursor: grab;
}
.rotate-handle::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 13px;
  width: 1px;
  height: 14px;
  background: var(--accent);
}

.asset-panel {
  position: fixed;
  top: 14px;
  right: 14px;
  width: min(330px, calc(100vw - 28px));
  max-height: calc(100vh - 28px);
  z-index: 30;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(26, 29, 36, 0.985);
  box-shadow: var(--shadow);
}
.asset-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: #222735;
  cursor: move;
  user-select: none;
}
.asset-panel-header span {
  margin-left: 6px;
  color: var(--muted);
  font-size: 11px;
}
.asset-panel-actions button { min-height: 32px; padding: 5px 9px; }
.asset-panel-body {
  max-height: calc(100vh - 82px);
  overflow-y: auto;
  padding: 9px;
}
.asset-panel-note {
  margin-bottom: 9px;
  padding: 8px 9px;
  border: 1px solid #2c3343;
  border-radius: 9px;
  background: #121720;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.4;
}
.asset-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.asset-item {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 5px;
  width: 100%;
  padding: 7px;
  text-align: left;
}
.asset-item img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: contain;
  border: 1px solid #2c3343;
  border-radius: 7px;
  background: #0f131a;
}
.asset-name {
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  color: var(--text);
  font-size: 11px;
  line-height: 1.3;
}
.asset-meta { color: var(--muted); font-size: 10px; }
.hidden { display: none !important; }

@media (max-width: 1100px) {
  :root { --sidebar-width: clamp(220px, 20vw, 260px); }
  .preview-meta { display: none; }
  .hint { display: block; margin: 2px 0 0; }
}

@media (max-width: 760px) {
  html, body { overflow: auto; }
  .app-shell {
    display: block;
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }
  .work-sidebar {
    width: 100%;
    height: auto;
    max-height: none;
  }
  .sidebar-scroll { overflow: visible; }
  .workspace { height: 72vh; min-height: 520px; }
}
