:root {
  --bg: #0f1216;
  --panel: #161a20;
  --panel-2: #1d222a;
  --line: #2a313b;
  --text: #e7e4dc;
  --muted: #8d95a1;
  --accent: #3b6fd1;       /* lifted Prussian blue */
  --accent-deep: #173a7a;
  --paper: #f4f0e5;
  --danger: #e07a6b;
  --radius: 8px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  display: flex;
  flex-direction: column;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 15px; font-weight: 600; letter-spacing: .02em; margin: 0; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 5px;
  background: linear-gradient(135deg, var(--accent-deep) 0 50%, var(--paper) 50% 100%);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.15);
}
.modes { display: flex; gap: 4px; background: var(--bg); padding: 3px; border-radius: 999px; border: 1px solid var(--line); }
.mode {
  border: 0; background: none; color: var(--muted); padding: 6px 16px; border-radius: 999px;
  font: inherit; cursor: pointer;
}
.mode.is-active { background: var(--accent-deep); color: #fff; }

/* ---------- Layout ---------- */
.workspace { flex: 1; display: grid; grid-template-columns: 340px 1fr; min-height: 0; }
.workspace[hidden] { display: none; }
.panel { background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; padding-bottom: 24px; }
.stage { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.group { padding: 16px 18px; border-bottom: 1px solid var(--line); }
.group h2 {
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .09em;
  color: var(--muted); margin: 0 0 12px; display: flex; align-items: center; gap: 8px;
}
.step {
  display: inline-grid; place-items: center; width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent-deep); color: #fff; font-size: 10px; letter-spacing: 0;
}

/* ---------- Controls ---------- */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; flex: 1; min-width: 0; }
.field > span:first-child { color: var(--muted); font-size: 12px; }
.inline { display: flex; gap: 6px; align-items: center; }
.inline select { flex: 1; }
.row { display: flex; gap: 10px; align-items: flex-end; }

input[type=number], input[type=text], select, textarea {
  width: 100%; background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 7px 8px; font: inherit;
}
textarea { font-family: var(--mono); font-size: 12px; resize: vertical; }
input[type=color] { width: 36px; height: 32px; padding: 2px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel-2); }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

.slider { display: grid; grid-template-columns: 82px 1fr 42px; align-items: center; gap: 8px; margin: 4px 0; }
.slider > span { color: var(--muted); font-size: 12px; }
.slider output { font: 12px var(--mono); text-align: right; color: var(--text); }
.mix .slider { grid-template-columns: 18px 1fr 42px; }
input[type=range] { width: 100%; accent-color: var(--accent); }

.check { display: flex; align-items: center; gap: 8px; margin: 8px 0; cursor: pointer; }
.check small { color: var(--muted); }
.check input { accent-color: var(--accent); }

button, .button {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 8px 12px; font: inherit; cursor: pointer; display: inline-block; text-align: center;
}
button:hover:not(:disabled), .button:hover { border-color: #3a4350; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; width: 100%; font-weight: 600; }
button.primary:hover:not(:disabled) { background: #4a7ddd; }
button:disabled { opacity: .45; cursor: not-allowed; }
button.link { background: none; border: 0; padding: 4px 0; color: var(--accent); font-size: 12px; }
.row > button, .row > .button { flex: 1; }

.seg { display: flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; margin-bottom: 12px; }
.segbtn { flex: 1; border: 0; border-radius: 0; background: none; color: var(--muted); }
.segbtn.is-active { background: var(--panel-2); color: var(--text); }

.dropzone {
  display: flex; flex-direction: column; gap: 4px; padding: 18px; text-align: center;
  border: 1.5px dashed #3a4350; border-radius: var(--radius); cursor: pointer; transition: .15s;
}
.dropzone:hover, .dropzone.is-over { border-color: var(--accent); background: rgba(59,111,209,.08); }
.dropzone.small { padding: 12px; }
.dz-title { font-weight: 600; }
.dz-sub { color: var(--muted); font-size: 12px; }

.meta { color: var(--muted); font-size: 12px; margin: 8px 0 0; font-variant-numeric: tabular-nums; }
.meta.warn { color: #e6b45c; }
.help { color: var(--muted); font-size: 12px; margin: 0 0 10px; }
.error { color: var(--danger); font-size: 12px; margin: 8px 0 0; min-height: 0; }
.error:empty { display: none; }

.histogram { width: 100%; height: 56px; display: block; background: var(--bg); border-radius: 4px; margin-bottom: 8px; }
.curveplot { width: 140px; height: 140px; display: block; background: var(--bg); border-radius: 4px; }

.savedlist { margin: 12px 0; display: flex; flex-direction: column; gap: 6px; }
.saved {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 6px 8px; background: var(--panel-2); border-radius: 6px; font-size: 12px;
}
.saved button { padding: 2px 8px; font-size: 11px; }

/* ---------- Stage ---------- */
.viewtabs { display: flex; align-items: center; gap: 4px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.viewtab { background: none; border: 0; color: var(--muted); padding: 6px 12px; border-radius: 6px; }
.viewtab.is-active { background: var(--panel-2); color: var(--text); }
.viewnote { margin-left: auto; color: var(--muted); font-size: 12px; }

.canvaswrap {
  position: relative; flex: 1; min-height: 0; display: grid; place-items: center; padding: 28px; overflow: hidden;
  background: radial-gradient(circle at 50% 40%, #1a1f26, var(--bg) 70%);
}
.canvaswrap > canvas {
  position: absolute; inset: 0; margin: auto; display: block;
  max-width: calc(100% - 56px); max-height: calc(100% - 56px);
  box-shadow: 0 10px 40px rgba(0,0,0,.5);
}
.canvaswrap.lightbox { background: #dcdcd8; }

.empty { max-width: 400px; text-align: center; color: var(--muted); }
.empty-title { color: var(--text); font-size: 18px; margin: 0 0 6px; }
body.is-dragging .canvaswrap { outline: 2px dashed var(--accent); outline-offset: -14px; }

/* ---------- Filmstrip & batch ---------- */
.filmstrip { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-top: 1px solid var(--line); background: var(--panel); }
.filmstrip[hidden] { display: none; }
.strip { display: flex; gap: 8px; overflow-x: auto; flex: 1; padding: 2px; }
.thumb {
  position: relative; flex: 0 0 auto; width: 76px; height: 76px; padding: 0; border-radius: 6px; overflow: hidden;
  background: var(--panel-2); border: 2px solid transparent; cursor: pointer;
}
.thumb.is-active { border-color: var(--accent); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .name {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 2px 4px; font-size: 10px; text-align: left;
  background: rgba(0,0,0,.6); color: #fff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.thumb .x {
  position: absolute; top: 2px; right: 2px; width: 18px; height: 18px; border-radius: 50%; display: none;
  place-items: center; background: rgba(0,0,0,.7); color: #fff; font-size: 12px; line-height: 1;
}
.thumb:hover .x, .thumb:focus-visible .x { display: grid; }
.thumb .dot { position: absolute; top: 4px; left: 4px; width: 9px; height: 9px; border-radius: 50%; box-shadow: 0 0 0 1.5px rgba(0,0,0,.6); }
.thumb[data-status=working] .dot { background: #e6b45c; }
.thumb[data-status=done] .dot { background: #5cc48a; }
.thumb[data-status=error] .dot { background: var(--danger); }
.progress { height: 6px; background: var(--bg); border-radius: 3px; overflow: hidden; margin-top: 10px; }
.progress .bar { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.slider.is-disabled { opacity: .45; }
.field.grow2 { flex: 2; }
.thumb .sheetno {
  position: absolute; right: 3px; bottom: 17px; padding: 0 4px; border-radius: 3px; font-size: 9px; line-height: 13px;
  background: rgba(23,58,122,.9); color: #fff;
}
#preview.is-clickable { cursor: pointer; }
.scope .help { margin: 4px 0 0 24px; }
.scope.is-own { background: rgba(230,180,92,.10); box-shadow: inset 3px 0 0 #e6b45c; }
.thumb .own {
  position: absolute; top: 2px; left: 16px; padding: 0 5px; border-radius: 8px; font-size: 10px; line-height: 15px;
  background: #e6b45c; color: #1a1300; font-weight: 700;
}

/* ---------- Recipes ---------- */
.recipes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 6px; }
.recipe {
  position: relative; text-align: left; padding: 7px 9px; border-radius: 6px; font-size: 12px; line-height: 1.3;
  background: var(--panel-2); border: 1px solid var(--line);
}
.recipe strong { display: block; font-size: 12.5px; margin-bottom: 2px; }
.recipe span { color: var(--muted); font-size: 11px; }
.recipe.is-active { border-color: var(--accent); background: rgba(59,111,209,.14); }
.recipe.is-mine strong::after { content: " \2605"; color: #e6b45c; font-size: 10px; }
.recipe .del { position: absolute; top: 3px; right: 5px; color: var(--muted); font-size: 13px; display: none; }
.recipe:hover .del { display: block; }
.sharpen { margin-top: 8px; }

/* ---------- Compare & toning ---------- */
.compare { position: absolute; inset: 20px; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.compare figure { margin: 0; display: flex; flex-direction: column; justify-content: center; min-height: 0; gap: 8px; }
.compare canvas { flex: 0 1 auto; min-height: 0; width: 100%; height: auto; max-height: calc(100% - 28px); object-fit: contain; box-shadow: none; }
.compare figcaption { text-align: center; color: var(--muted); font-size: 12px; }
.toning { display: none; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; margin-left: 12px; }
.toning select { width: auto; padding: 4px 6px; font-size: 12px; }
.toning.is-shown { display: inline-flex; }

/* ---------- Guide ---------- */
.workspace.guide { display: block; overflow-y: auto; }
.guide-doc { max-width: 820px; margin: 0 auto; padding: 32px 28px 64px; line-height: 1.6; }
.guide-head { display: flex; gap: 24px; align-items: flex-start; justify-content: space-between; border-bottom: 1px solid var(--line); padding-bottom: 20px; }
.guide-head .primary { width: auto; white-space: nowrap; }
.eyebrow { margin: 0; color: var(--accent); text-transform: uppercase; letter-spacing: .1em; font-size: 11px; font-weight: 600; }
.guide-doc h1 { font-size: 28px; margin: 4px 0 6px; letter-spacing: -.01em; }
.lede { color: var(--muted); margin: 0; }
.guide-toc { display: flex; flex-wrap: wrap; gap: 6px 14px; padding: 14px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.guide-toc a { color: var(--accent); text-decoration: none; }
.guide-doc section { padding: 18px 0 6px; border-bottom: 1px solid var(--line); }
.guide-doc h2 { font-size: 19px; margin: 0 0 8px; }
.guide-doc h3 { font-size: 14px; margin: 14px 0 4px; color: var(--text); }
.guide-doc p, .guide-doc li { color: #cfd3d9; }
.guide-doc ul, .guide-doc ol { padding-left: 22px; margin: 6px 0; }
.guide-doc table { border-collapse: collapse; margin: 6px 0 10px; }
.guide-doc th, .guide-doc td { text-align: left; padding: 6px 12px 6px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
.steps li { margin-bottom: 4px; }
.supplies { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.supplies ul { padding-left: 18px; font-size: 13px; }
.guide-foot { color: var(--muted); font-size: 12px; margin-top: 24px; }
.guide-foot a, .faq a { color: var(--accent); }
.faq dt { font-weight: 600; margin-top: 14px; }
.faq dd { margin: 4px 0 0; }
.family { display: flex; gap: 16px; }
.family a { color: var(--muted); font-size: 13px; text-decoration: none; white-space: nowrap; }
.family a:hover { color: var(--accent); }
.family { align-items: center; }
.procmenu { position: relative; }
.procmenu summary { list-style: none; cursor: pointer; color: var(--muted); font-size: 13px; white-space: nowrap; }
.procmenu summary::-webkit-details-marker { display: none; }
.procmenu summary::after { content: " ▾"; }
.procmenu summary:hover, .procmenu[open] summary { color: var(--accent); }
.procmenu-list {
  position: absolute; right: 0; top: calc(100% + 10px); z-index: 20; min-width: 200px;
  display: flex; flex-direction: column; padding: 6px; border-radius: var(--radius);
  background: var(--panel-2); border: 1px solid var(--line); box-shadow: 0 8px 24px rgba(0,0,0,.4);
}
.procmenu-list a { color: var(--text); padding: 7px 10px; border-radius: 6px; font-size: 13px; }
.procmenu-list a:hover { background: var(--line); color: var(--text); }
@media (max-width: 760px) { .family > a { display: none; } }

@media print {
  @page { size: A4; margin: 14mm; }
  body { background: #fff; color: #111; display: block; font-size: 10.5pt; }
  body > *:not(#guide) { display: none !important; }
  #guide { display: block !important; overflow: visible; }
  .guide-doc { max-width: none; padding: 0; }
  .guide-head .primary, .guide-toc, .faq { display: none; }
  .guide-foot a { color: #222; text-decoration: none; }
  .guide-doc p, .guide-doc li, .lede, .guide-foot { color: #222; }
  .guide-doc section { border-color: #ccc; padding-top: 10px; }
  .guide-doc h1, .guide-doc h2, .guide-doc h3, .guide-doc th, .guide-doc strong { color: #111; }
  .guide-doc h2, .guide-doc h3 { break-after: avoid; }
  .guide-doc li, .guide-doc table, .supplies > div { break-inside: avoid; }
  .supplies { grid-template-columns: repeat(3, 1fr) !important; }
  .guide-doc h1 { font-size: 20pt; }
  .eyebrow { color: #173a7a; }
  .guide-doc th, .guide-doc td { border-color: #ccc; }
}

/* ---------- Calibration ---------- */
.calibview { flex: 1; overflow: auto; padding: 24px; display: flex; flex-direction: column; gap: 24px; }
.scanbox { position: relative; display: grid; place-items: center; min-height: 220px; background: var(--panel); border-radius: var(--radius); padding: 16px; }
.scanbox canvas { max-width: 100%; max-height: 50vh; cursor: crosshair; touch-action: none; display: none; }
.scanbox.has-scan canvas { display: block; }
.scanbox.has-scan .empty { display: none; }
.charts { display: flex; flex-wrap: wrap; gap: 24px; align-items: flex-start; }
.charts[hidden] { display: none; }
.charts figure { margin: 0; background: var(--panel); border-radius: var(--radius); padding: 14px; }
.charts canvas { display: block; max-width: 100%; }
.charts figcaption { color: var(--muted); font-size: 12px; margin-top: 8px; max-width: 460px; }

@media (max-width: 860px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .panel { border-right: 0; max-height: 50vh; }
  .canvaswrap { min-height: 50vh; }
  .compare { grid-template-columns: 1fr; grid-auto-rows: 60vh; overflow-y: auto; }
  .supplies { grid-template-columns: 1fr; }
}
