:root {
  --bg: #0b0b1a;
  --surface: #12122a;
  --surface-2: #1a1a38;
  --line: #262650;
  --ink: #eef0ff;
  --muted: #9a9cc4;
  --accent: #6a5cff;
  --accent-2: #00a6ff;
  --pink: #ff3b6b;
  --ok: #2ecc71;
  --warn: #ffb020;
  --grad: linear-gradient(135deg, #00a6ff, #9657ff 60%, #ff3b6b);
  --check-a: #1b1b36;
  --check-b: #22224a;
  --radius: 12px;
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme='dark']) {
    --bg: #f4f5fb; --surface: #ffffff; --surface-2: #eef0fa; --line: #dfe2f2; --ink: #14142e; --muted: #5d6088;
    --check-a: #e9ebf5; --check-b: #f8f9fe; color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); overflow: hidden; -webkit-tap-highlight-color: transparent; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
h1, h2, h3, h4, p { margin: 0; }

#app {
  height: 100dvh; display: grid;
  grid-template-columns: 224px minmax(0, 1fr) 330px;
  grid-template-rows: 56px minmax(0, 1fr);
  grid-template-areas: 'top top top' 'nav stage panel';
}

/* ---------- top bar ---------- */
.topbar { grid-area: top; display: flex; align-items: center; gap: 10px; padding: 0 14px; background: var(--surface); border-bottom: 1px solid var(--line); min-width: 0; }
.brand { display: flex; align-items: center; gap: 9px; color: var(--ink); text-decoration: none; font-weight: 800; font-size: 19px; letter-spacing: -.04em; }
.brand-mark { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 9px; background: var(--grad); color: #fff; font-size: 16px; }
.file-name { margin-left: 8px; padding-left: 14px; border-left: 1px solid var(--line); color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.spacer { flex: 1; }
.group { display: flex; align-items: center; gap: 2px; }

.icon-btn, .text-btn { display: inline-grid; place-items: center; min-width: 34px; height: 34px; padding: 0 8px; border: 0; border-radius: 9px; background: transparent; color: var(--ink); }
.text-btn { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.icon-btn:hover:not(:disabled), .text-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--ink); }
.icon-btn:disabled, .text-btn:disabled { opacity: .35; cursor: default; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 36px; padding: 0 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); color: var(--ink); font-size: 13.5px; font-weight: 600; transition: transform .12s, background .15s, border-color .15s; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:active:not(:disabled) { transform: scale(.98); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--grad); border-color: transparent; color: #fff; box-shadow: 0 6px 20px #6a5cff40; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn.big { min-height: 46px; padding: 0 20px; font-size: 15px; border-radius: 12px; }
.btn.wide { width: 100%; }
.ic { display: inline-grid; place-items: center; }

/* ---------- nav ---------- */
.toolnav { grid-area: nav; display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--line); }
.search { display: flex; align-items: center; gap: 8px; margin: 12px; padding: 0 10px; height: 36px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); }
.search input[type='search'] { flex: 1; min-width: 0; width: auto; padding: 0; border: 0; border-radius: 0; background: transparent; font-size: 13px; }
.search:focus-within { border-color: var(--accent); }
.nav-list { flex: 1; overflow-y: auto; padding: 0 8px 16px; }
.nav-title { margin: 16px 8px 6px; font-size: 10.5px; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); }
.nav-item { display: flex; align-items: center; gap: 11px; width: 100%; padding: 9px 10px; border: 0; border-radius: 10px; background: transparent; color: var(--ink); font-size: 13.5px; font-weight: 550; text-align: left; }
.nav-item:hover { background: var(--surface-2); }
.nav-item.on { background: color-mix(in srgb, var(--accent) 22%, transparent); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 60%, transparent); }
.nav-ic { display: grid; place-items: center; width: 30px; height: 30px; border-radius: 9px; background: var(--surface-2); color: var(--accent-2); }
.nav-item[data-cat='annotate'] .nav-ic { color: var(--pink); }
.nav-item[data-cat='effects'] .nav-ic { color: #ffb020; }
.nav-item[data-cat='export'] .nav-ic { color: var(--ok); }
.nav-item[data-cat='inspect'] .nav-ic { color: #c471ed; }
.nav-item.on .nav-ic { background: var(--grad); color: #fff; }

/* ---------- stage ---------- */
.stage { grid-area: stage; position: relative; display: flex; overflow: auto; background: radial-gradient(1200px 600px at 30% 0%, #1a1a4020, transparent), var(--bg); padding: 20px; min-width: 0; min-height: 0; }
.holder { position: relative; margin: auto; flex: none; display: none; background: repeating-conic-gradient(var(--check-a) 0 25%, var(--check-b) 0 50%) 0 0 / 20px 20px; box-shadow: 0 20px 60px #00000060, 0 0 0 1px var(--line); border-radius: 3px; }
body.has-image .holder { display: block; }
.holder canvas { display: block; position: absolute; inset: 0; image-rendering: auto; transform-origin: center; }
#overlay { pointer-events: none; touch-action: none; }
#overlay.active { touch-action: none; }
.has-img { visibility: hidden; }
body.has-image .has-img { visibility: visible; }

.empty { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; overflow: auto; }
body.has-image .empty { display: none; }
.empty-card { max-width: 560px; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 18px; }
.empty-glyph { display: grid; place-items: center; width: 64px; height: 64px; border-radius: 20px; background: var(--grad); color: #fff; font-size: 30px; box-shadow: 0 14px 40px #6a5cff55; }
.empty h1 { font-size: clamp(30px, 4.5vw, 46px); line-height: 1.05; letter-spacing: -.05em; }
.empty h1 span { background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
.empty p { color: var(--muted); line-height: 1.6; max-width: 460px; }
.empty small { color: var(--muted); font-size: 12px; }
.empty-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

.zoombar { position: absolute; right: 16px; bottom: 16px; z-index: 4; display: flex; align-items: center; gap: 2px; padding: 4px; background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: blur(10px); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 8px 30px #0006; }
.drop-veil { position: fixed; inset: 0; z-index: 50; display: none; place-items: center; background: color-mix(in srgb, var(--accent) 35%, #000a); backdrop-filter: blur(4px); font-size: 24px; font-weight: 800; border: 3px dashed #fff8; margin: 10px; border-radius: 20px; pointer-events: none; }
body.dragging .drop-veil { display: grid; }

/* ---------- panel ---------- */
.panel { grid-area: panel; display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-left: 1px solid var(--line); }
.panel-head { padding: 16px 18px 12px; border-bottom: 1px solid var(--line); }
.panel-head h2 { font-size: 17px; letter-spacing: -.02em; }
.panel-head p { margin-top: 4px; color: var(--muted); font-size: 12.5px; line-height: 1.45; }
.panel-body { flex: 1; overflow-y: auto; padding: 6px 18px 24px; }
.panel-empty { display: flex; flex-direction: column; gap: 10px; padding: 24px 0; color: var(--muted); }

.section { padding: 14px 0 4px; display: flex; flex-direction: column; gap: 10px; border-bottom: 1px solid color-mix(in srgb, var(--line) 60%, transparent); padding-bottom: 14px; }
.section:last-of-type { border-bottom: 0; }
.section h4 { font-size: 11px; letter-spacing: .1em; text-transform: uppercase; color: var(--muted); font-weight: 800; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; min-width: 0; }
.row.wrap { flex-wrap: wrap; row-gap: 10px; }
.row.wrap > * { flex: 0 0 calc(50% - 4px); }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.field small { color: var(--muted); font-size: 11.5px; }
.field output { font-size: 12px; color: var(--ink); font-variant-numeric: tabular-nums; font-weight: 700; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }

input[type='text'], input[type='number'], input[type='search'], select, textarea { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2); font-size: 13.5px; outline: 0; }
input[type='text']:focus, input[type='number']:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 56px; }
input[type='range'] { width: 100%; accent-color: var(--accent); height: 22px; }
input[type='color'] { width: 32px; height: 28px; padding: 0; border: 1px solid var(--line); border-radius: 8px; background: none; cursor: pointer; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 6px 11px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface-2); font-size: 12.5px; font-weight: 600; }
.chip:hover { border-color: var(--accent); }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.segmented { display: flex; padding: 3px; gap: 2px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; }
.segmented button { flex: 1; padding: 7px 8px; border: 0; border-radius: 7px; background: transparent; font-size: 12.5px; font-weight: 650; color: var(--muted); }
.segmented button.on { background: var(--accent); color: #fff; }
.swatches { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.swatch { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--line); padding: 0; }
.swatch.on { outline: 2px solid var(--accent); outline-offset: 2px; }

.actions { display: flex; gap: 8px; padding-top: 14px; }
.actions .btn { flex: 1; }
.actions.secondary { padding-top: 8px; flex-wrap: wrap; }
.actions.secondary > * { min-width: 90px; }
.note { font-size: 12px; line-height: 1.5; color: var(--muted); padding-top: 10px; }
.note.warn { color: var(--warn); }
.readout { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }

.stats { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; padding: 12px; border-radius: var(--radius); background: var(--surface-2); border: 1px solid var(--line); transition: opacity .15s; }
.stats:empty { display: none; }
.stats.busy { opacity: .55; }
.stat { flex: 1; min-width: 110px; display: flex; flex-direction: column; gap: 2px; }
.stat span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; font-weight: 700; }
.stat b { font-size: 20px; letter-spacing: -.02em; }
.stat small { font-size: 12px; color: var(--muted); }
.good { color: var(--ok) !important; font-weight: 700; }
.bad { color: var(--warn) !important; font-weight: 700; }
.bar { width: 100%; height: 6px; border-radius: 6px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--grad); border-radius: 6px; }

.icon-previews { display: flex; align-items: flex-end; gap: 14px; flex-wrap: wrap; padding: 12px; border-radius: var(--radius); background: repeating-conic-gradient(var(--check-a) 0 25%, var(--check-b) 0 50%) 0 0 / 12px 12px; border: 1px solid var(--line); }
.icon-previews figure { margin: 0; display: grid; justify-items: center; gap: 4px; }
.icon-previews figcaption { font-size: 10.5px; color: var(--muted); }
.icon-previews canvas { image-rendering: pixelated; }
.code { margin: 0; padding: 10px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); font: 11.5px/1.5 ui-monospace, Menlo, Consolas, monospace; overflow-x: auto; white-space: pre; }

.file-list { max-height: 160px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.file-list:empty { display: none; }
.file-row { display: flex; justify-content: space-between; gap: 10px; padding: 7px 10px; font-size: 12.5px; border-bottom: 1px solid var(--line); }
.file-row:last-child { border-bottom: 0; }
.file-row span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-row small { color: var(--muted); flex: none; }
.progress { height: 6px; background: var(--line); border-radius: 6px; overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: var(--grad); transition: width .2s; }

.palette { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.pal { height: 54px; border: 1px solid #ffffff22; border-radius: 10px; padding: 8px 10px; text-align: left; font-size: 12px; font-weight: 800; display: flex; align-items: flex-end; }
.pal span { display: flex; justify-content: space-between; width: 100%; }
.pal small { font-weight: 600; opacity: .8; }
.picked { display: flex; gap: 12px; align-items: center; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); }
.picked i { width: 48px; height: 48px; border-radius: 10px; border: 1px solid #ffffff30; flex: none; }
.picked > div { display: flex; flex-direction: column; align-items: flex-start; gap: 1px; min-width: 0; }
.picked b { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.link { padding: 0; border: 0; background: none; color: var(--ink); font: 12.5px ui-monospace, Menlo, Consolas, monospace; text-align: left; }
.link:hover { color: var(--accent-2); text-decoration: underline; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 0; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-weight: 600; word-break: break-all; }

.toast { position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 20px); z-index: 60; padding: 10px 16px; border-radius: 12px; background: var(--ink); color: var(--bg); font-size: 13.5px; font-weight: 650; opacity: 0; pointer-events: none; transition: .22s; max-width: 90vw; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-kind='err'] { background: var(--pink); color: #fff; }
.toast[data-kind='ok'] { background: var(--ok); color: #06210f; }

:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 10px; border: 2px solid transparent; background-clip: padding-box; }

/* ---------- tablet / mobile ---------- */
@media (max-width: 1100px) {
  #app { grid-template-columns: 196px minmax(0, 1fr) 290px; }
}
@media (max-width: 860px) {
  #app { grid-template-columns: minmax(0, 1fr); grid-template-rows: 52px minmax(0, 1fr) 38dvh auto; grid-template-areas: 'top' 'stage' 'panel' 'nav'; }
  .panel { min-height: 0; border-left: 0; border-top: 1px solid var(--line); }
  .panel-head { padding: 10px 14px 8px; }
  .panel-head p { display: none; }
  .panel-body { padding: 0 14px 16px; }
  .toolnav { flex-direction: row; border-right: 0; border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
  .search, .nav-title { display: none; }
  .nav-list { display: flex; overflow-x: auto; overflow-y: hidden; padding: 6px 8px; gap: 2px; }
  .nav-group { display: flex; gap: 2px; }
  .nav-group + .nav-group { margin-left: 6px; padding-left: 6px; border-left: 1px solid var(--line); }
  .nav-item { flex-direction: column; gap: 3px; width: auto; min-width: 64px; padding: 6px 8px; font-size: 11px; text-align: center; }
  .nav-name { white-space: nowrap; }
  .file-name, .brand-name, .btn .lbl, .text-btn#revertBtn { display: none; }
  .stage { padding: 10px; }
  .zoombar { right: 8px; bottom: 8px; }
  .topbar { gap: 4px; padding: 0 10px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
