:root {
  --bg: #eef1f4;
  --panel: #ffffff;
  --line: #d9dfe6;
  --ink: #1d2733;
  --ink-soft: #5b6877;
  --accent: #1f7a78;
  --accent-ink: #ffffff;
  --danger: #b3382f;
  --tool: #eef3f6;
  --tool-active: #1f6fb2;
  --view-bg: linear-gradient(180deg, #f9fafb, #e6ebf0);
  --cell: #fae7c8;
  --cell-stroke: #6a5b45;
  --editor-bg: #f7f9fb;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12171d; --panel: #1b222b; --line: #2e3844; --ink: #e6ebf0; --ink-soft: #9aa7b6;
    --accent: #3ab0ac; --accent-ink: #06201f; --danger: #ef7b72; --tool: #25303b; --tool-active: #3d8fd6;
    --view-bg: linear-gradient(180deg, #1f2832, #151b22); --editor-bg: #171e26;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; display: grid; grid-template-rows: auto minmax(0, 1fr);
  font-family: "Space Grotesk", system-ui, sans-serif; font-size: 14px; color: var(--ink); background: var(--bg);
}
h1, h2, h3 { margin: 0; font-family: "Sora", sans-serif; }
h1 { font-size: 1.1rem; }
h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-soft); margin-bottom: 0.6rem; }
input, select, button { font: inherit; }
input, select {
  width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 0.45rem 0.55rem;
  background: var(--panel); color: var(--ink);
}
:focus-visible { outline: 2px solid var(--tool-active); outline-offset: 1px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  padding: 0.55rem 1rem; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 0.75rem; flex-wrap: wrap; }
.tagline { color: var(--ink-soft); font-size: 0.85rem; }
.topbar-actions { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
.topbar-actions .status { margin: 0 0.5rem 0 0; max-width: 24ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.divider { width: 1px; height: 1.4rem; background: var(--line); margin: 0 0.25rem; }

button { cursor: pointer; border-radius: 8px; font-weight: 600; padding: 0.42rem 0.8rem; border: 1px solid transparent; }
button:disabled { opacity: 0.4; cursor: default; }
.primary-btn { background: var(--accent); color: var(--accent-ink); }
.ghost-btn { background: var(--tool); color: var(--ink); border-color: var(--line); }
.danger-btn { background: transparent; color: var(--danger); border-color: var(--danger); }
.full { width: 100%; margin-top: 0.7rem; }
.primary-btn:hover:not(:disabled), .ghost-btn:hover:not(:disabled) { filter: brightness(1.07); }

.workspace { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 0.75rem; padding: 0.75rem; min-height: 0; }
.sidebar { display: flex; flex-direction: column; gap: 0.75rem; overflow-y: auto; min-height: 0; padding-right: 2px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 0.85rem; }
details.card > summary { cursor: pointer; font-weight: 600; }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.55rem; }
.field-grid .wide { grid-column: 1 / -1; }
label { display: grid; gap: 0.25rem; font-size: 0.78rem; color: var(--ink-soft); }
.hint, .status { margin: 0.5rem 0 0; font-size: 0.8rem; color: var(--ink-soft); }
.summary-list { margin: 0.4rem 0 0; padding-left: 1.1rem; display: grid; gap: 0.2rem; font-size: 0.85rem; }
.clear-row { display: grid; grid-template-columns: 1fr auto auto; gap: 0.4rem; }

.stage { display: grid; grid-template-rows: auto minmax(0, 1.35fr) minmax(0, 1fr) auto; gap: 0.6rem; min-height: 0; min-width: 0; }
.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem; padding: 0.5rem;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
}
.tool-btn {
  display: inline-flex; align-items: center; gap: 0.4rem; background: var(--tool); color: var(--ink);
  border-color: var(--line); font-family: "Sora", sans-serif; font-size: 0.8rem;
}
.tool-btn .ico { font-size: 1rem; line-height: 1; }
.tool-btn kbd { font: 0.68rem monospace; opacity: 0.6; border: 1px solid currentColor; border-radius: 4px; padding: 0 0.25rem; }
.tool-btn.active { background: var(--tool-active); border-color: var(--tool-active); color: #fff; }
.tool-hint { color: var(--ink-soft); font-size: 0.82rem; margin-left: 0.4rem; flex: 1 1 14rem; }

.sheet-actions { display: none; align-items: center; gap: 0.4rem; margin-left: auto; }
body[data-tool="sheet"] .sheet-actions { display: flex; }
.sheet-actions .status { margin: 0; }
.sheet-btn { width: 2.1rem; height: 2.1rem; padding: 0; border-radius: 999px; font-size: 1rem; border: 1px solid var(--line); }
.sheet-btn.confirm { background: #d7e8d2; color: #2b6b33; }
.sheet-btn.cancel { background: #f2d4d0; color: #a63d3d; }

.viewport3d {
  position: relative; min-height: 0; overflow: hidden; border: 1px solid var(--line);
  border-radius: 12px; background: var(--view-bg);
}
.viewport3d canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.scale-overlay {
  position: absolute; left: 0.7rem; bottom: 0.7rem; pointer-events: none; padding: 0.3rem 0.45rem;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; opacity: 0.92;
}
.scale-line { height: 0; width: 90px; border-top: 3px solid var(--ink); border-left: 1px solid var(--ink); border-right: 1px solid var(--ink); margin-bottom: 0.2rem; }
.scale-label { font-size: 0.74rem; text-align: center; }

.viewport-layer-controls {
  position: absolute; top: 0.6rem; right: 0.6rem; width: min(270px, calc(100% - 1.2rem)); z-index: 3;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 0.4rem 0.55rem;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.12); opacity: 0.96;
}
.viewport-layer-controls > summary { cursor: pointer; font-family: "Sora", sans-serif; font-size: 0.8rem; font-weight: 600; }
.viewport-layer-controls .full { margin-top: 0.4rem; padding: 0.3rem; font-size: 0.78rem; }
.viewport-layer-row { display: grid; grid-template-columns: 78px 1fr 38px; align-items: center; gap: 0.4rem; padding: 0.15rem 0; }
.viewport-layer-toggle { display: inline-flex; flex-direction: row; align-items: center; gap: 0.3rem; font-size: 0.76rem; color: var(--ink); }
.viewport-layer-toggle input { width: auto; margin: 0; }
.viewport-layer-row input[type="range"] { padding: 0; accent-color: var(--tool-active); }
.viewport-opacity-value { font-size: 0.7rem; color: var(--ink-soft); text-align: right; }

.editor2d-wrap { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; min-height: 0; }
.editor2d-card {
  display: flex; flex-direction: column; min-height: 0; min-width: 0; padding: 0.4rem;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
}
.editor2d-card h3 { font-size: 0.85rem; padding: 0.1rem 0.2rem 0.3rem; }
.editor2d-card h3 span { font-family: "Space Grotesk", sans-serif; font-weight: 400; color: var(--ink-soft); font-size: 0.75rem; }
#frontEditorCanvas, #backEditorCanvas {
  display: block; flex: 1 1 0; min-height: 0; width: 100%; touch-action: none;
  border-radius: 8px; border: 1px solid var(--line); background: var(--editor-bg); cursor: crosshair;
}
body[data-tool="move"] #frontEditorCanvas, body[data-tool="move"] #backEditorCanvas { cursor: ew-resize; }
.statusbar { padding: 0 0.25rem; }
.statusbar .status { margin: 0; }

.editor-cell { fill: var(--cell); stroke: var(--cell-stroke); stroke-width: 2; }
.editor-pad-front { fill: #d8ebff; }
.editor-pad-back { fill: #ffe4cb; }
.editor-strip-front { stroke: #4f86c5; stroke-width: 4; }
.editor-strip-back { stroke: #b78654; stroke-width: 4; }
.editor-main-pos { fill: #ffdfdf; stroke: #cf2b2b; stroke-width: 2; }
.editor-main-neg { fill: #dfebff; stroke: #2a57a8; stroke-width: 2; }
.cell-group.dragging { opacity: 0.72; }
.cell-group.dragging .cell-circle { stroke-dasharray: 4 3; }
.editor-cell.sheet-draft-cell { fill: rgba(192, 192, 192, 0.78); stroke: #7d8087; }
.editor-sheet-outline, .editor-sheet-preview { fill: rgba(193, 195, 199, 0.24); stroke: rgba(122, 126, 134, 0.95); stroke-width: 2.5; }

@media (max-width: 980px) {
  html, body { height: auto; }
  body { display: block; }
  .workspace { display: block; }
  .sidebar { overflow: visible; margin-bottom: 0.75rem; }
  .stage { display: flex; flex-direction: column; }
  .viewport3d { height: 60vh; flex: none; }
  .editor2d-wrap { grid-template-columns: 1fr; }
  #frontEditorCanvas, #backEditorCanvas { flex: none; height: 260px; }
  .tagline { display: none; }
}

/* ---- Modern layer ---- */
:root { color-scheme: light dark; }
html, body { height: 100dvh; }
.topbar { position: sticky; top: 0; z-index: 10; backdrop-filter: blur(10px); background: color-mix(in srgb, var(--panel) 85%, transparent); }
.card, .toolbar, .editor2d-card, .viewport3d { border-radius: 16px; box-shadow: 0 1px 2px rgba(20, 30, 45, 0.06); }
button { transition: background 0.15s, filter 0.15s, transform 0.1s; }
button:active:not(:disabled) { transform: scale(0.97); }
@media (pointer: coarse) {
  button, input, select { min-height: 44px; }
  .viewport-layer-row input[type="range"] { min-height: 32px; }
}
@media (max-width: 980px) {
  html, body { height: auto; }
  .workspace { display: flex; flex-direction: column; padding: 0.5rem; }
  .stage { order: -1; }
  .viewport3d { height: 55dvh; min-height: 320px; }
  .toolbar { position: sticky; top: 0; z-index: 5; overflow-x: auto; flex-wrap: nowrap; }
  .tool-hint { display: none; }
  .topbar-actions .status { display: none; }
}
@media (max-width: 720px) {
  .tool-btn { font-size: 0; gap: 0; padding: 0.5rem 0.7rem; }
  .tool-btn .ico { font-size: 1.15rem; }
  .tool-btn kbd { display: none; }
  .brand h1 { font-size: 1rem; }
  .viewport-layer-controls { width: 220px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ---- Toolbar polish ---- */
.toolbar { gap: 0.6rem; padding: 0.45rem 0.6rem; }
.tool-group { display: flex; gap: 2px; padding: 3px; background: var(--tool); border: 1px solid var(--line); border-radius: 12px; flex: none; }
.tool-group .tool-btn { background: transparent; border-color: transparent; border-radius: 9px; padding: 0.4rem 0.65rem; font-family: inherit; font-weight: 500; color: var(--ink-soft); }
.tool-group .tool-btn:hover { background: color-mix(in srgb, var(--panel) 65%, transparent); color: var(--ink); }
.tool-group .tool-btn.active { background: var(--panel); color: var(--tool-active); box-shadow: 0 1px 3px rgba(20, 30, 45, 0.2), inset 0 0 0 1.5px var(--tool-active); }
.tool-btn svg, .sheet-btn svg { width: 1.15rem; height: 1.15rem; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.tool-btn kbd { border: 0; background: var(--line); color: var(--ink-soft); opacity: 1; }
#toolMainPlusBtn svg { stroke: #cf2b2b; }
#toolMainMinusBtn svg { stroke: #2f6fd0; }
.clear-sel { display: inline-flex; align-items: center; gap: 0.4rem; flex: none; }
.clear-sel svg { width: 1.1rem; height: 1.1rem; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.clear-sel .count { min-width: 1.3rem; padding: 0 0.35rem; border-radius: 999px; background: var(--tool-active); color: #fff; font-size: 0.72rem; text-align: center; }
.clear-sel:disabled .count { display: none; }
.sheet-btn { width: 2.2rem; height: 2.2rem; display: inline-grid; place-items: center; }
.sheet-btn.confirm { background: #2e8b57; color: #fff; border-color: #2e8b57; }
.sheet-btn.cancel { background: transparent; color: var(--danger); border-color: var(--danger); }
@media (max-width: 720px) {
  .tool-btn span, .clear-sel .label { display: none; }
  .tool-group .tool-btn { padding: 0.5rem 0.6rem; }
}

/* ---- Version badge ---- */
.statusbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.version-badge { font: 0.75rem ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--ink-soft); text-decoration: none; padding: 0.15rem 0.5rem; border: 1px solid var(--line); border-radius: 999px; background: var(--panel); }
a.version-badge:hover { color: var(--tool-active); border-color: var(--tool-active); }

/* ---- Flip rows button (floats over the Back view, so it never changes layout) ---- */
.editor2d-card { position: relative; }
.flip-fab { position: absolute; top: 0.3rem; right: 0.4rem; z-index: 2; }
.ghost-btn.small { display: inline-flex; align-items: center; gap: 0.35rem; padding: 0.2rem 0.6rem; font-size: 0.78rem; }
.ghost-btn.small svg { width: 1rem; height: 1rem; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.flip-fab[aria-pressed="true"] { background: var(--tool-active); color: #fff; border-color: var(--tool-active); }
@media (max-width: 720px) { .flip-fab .label { display: none; } }
