/* A working tool: Create is controls beside the sheet as it will print, Scan is the camera beside the
   reassembly state, Format explains the tile. Paper, ink, one green. */
:root {
  --paper: #f3f4f6; --surface: #ffffff; --well: #e9ebef; --ink: #101216; --ink-2: #505762; --line: #d5d9df;
  --accent: #1BB24B; --on-accent: #04210f; --danger: #b3261e; --ok: #17764a; --warn: #9a5b00;
  --sheet: #ffffff; --raised: #ffffff; --cell: #d5d9df;
  --shadow: 0 1px 2px rgba(16, 18, 22, .06), 0 8px 24px rgba(16, 18, 22, .06);
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
  --r: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0e1013; --surface: #171a1f; --well: #20242b; --ink: #eceef2; --ink-2: #9ca4b0; --line: #2c313a;
    --accent: #1BB24B; --on-accent: #04210f; --danger: #ff7468; --ok: #4fc790; --warn: #e3a341; --raised: #353b46; --cell: #2c313a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .3); color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--paper); color: var(--ink); font: 15px/1.5 var(--sans); -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
button, select, textarea, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app { max-width: 1180px; margin: 0 auto; padding-inline: 16px; padding-block: 0 48px; }
.top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px 24px; padding-block: 16px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; color: inherit; text-decoration: none; }
.brand svg { width: 32px; height: 32px; flex: none; }
.brand-name { font-weight: 800; font-size: 20px; letter-spacing: -.02em; line-height: 1.1; }
.brand-sub { font: 11px/1.3 var(--mono); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-2); }
.tabs { display: flex; gap: 2px; padding: 3px; background: var(--well); border-radius: 999px; }
.tabs button { border: 0; background: transparent; padding: 8px 18px; border-radius: 999px; font-weight: 600; color: var(--ink-2); cursor: pointer; }
.tabs button[aria-selected="true"] { background: var(--raised); color: var(--ink); box-shadow: var(--shadow); }

.label { display: block; font: 600 11px/1.3 var(--mono); letter-spacing: .09em; text-transform: uppercase; color: var(--ink-2); margin-bottom: 6px; }
.hint { margin: 6px 0 0; font-size: 13px; color: var(--ink-2); }
/* a short explanation for every setting: hover the name (or tap it) */
[data-tip] { cursor: help; text-decoration: underline dotted; text-decoration-color: var(--line); text-underline-offset: 3px; }
#tip { position: fixed; z-index: 40; max-width: min(300px, calc(100vw - 24px)); background: var(--ink); color: var(--paper); padding: 8px 11px; border-radius: 8px;
  font: 12.5px/1.45 var(--sans); box-shadow: var(--shadow); pointer-events: none; }
.hint.warn { color: var(--warn); }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 42px; padding: 0 16px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600; cursor: pointer; text-decoration: none; }
.btn:hover { border-color: var(--ink-2); }
.btn.go { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 13px; }
select, textarea, input[type=number], input[type=text] { width: 100%; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); padding: 9px 10px; }
select, input[type=number], input[type=text] { min-height: 42px; }
textarea { resize: vertical; min-height: 96px; font: 13px/1.5 var(--mono); }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px; padding: 3px; background: var(--well); border-radius: 9px; }
.seg button { border: 0; background: transparent; border-radius: 7px; padding: 7px 4px; cursor: pointer; color: var(--ink-2); font-weight: 600; font-size: 13px; line-height: 1.25; }
.seg button small { display: block; font: 400 11px/1.2 var(--mono); }
.seg button[aria-pressed="true"] { background: var(--raised); color: var(--ink); box-shadow: var(--shadow); }
details.more { border-top: 1px solid var(--line); padding-top: 12px; }
details.more > summary { cursor: pointer; font: 600 11px/1.3 var(--mono); letter-spacing: .09em; text-transform: uppercase; color: var(--ink-2); }
details.more[open] > summary { margin-bottom: 12px; }
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 12px; }

/* ---- create ---- */
.create-grid { display: grid; grid-template-columns: minmax(280px, 360px) minmax(0, 1fr); gap: 20px; align-items: start; }
/* grid children may shrink below their content, so a long file name is cut short instead of widening the panel */
.create-grid > *, .scan-grid > * { min-width: 0; }
.controls { padding: 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; }
.controls > * { min-width: 0; }
.drop { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 12px; border: 1.5px dashed var(--line); border-radius: 8px; color: var(--ink-2); font-size: 13px; }
.drop.over { border-color: var(--accent); background: var(--well); }
.source-chip { display: flex; align-items: center; gap: 8px; margin-top: 8px; font: 12px/1.4 var(--mono); color: var(--ink-2); min-width: 0; }
.source-chip b { color: var(--ink); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.source-chip .tag, .source-chip .size, .source-chip .btn { flex: none; }
.source-chip .tag { padding: 1px 6px; border-radius: 4px; background: var(--well); }
.output { display: grid; gap: 14px; min-width: 0; }
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; }
.stat { background: var(--surface); padding: 12px 14px; min-width: 0; }
.stat .v { font: 700 22px/1.15 var(--sans); letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat .v small { font: 500 12px var(--mono); color: var(--ink-2); letter-spacing: 0; }
.stat .k { font: 11px/1.3 var(--mono); color: var(--ink-2); margin-top: 3px; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.actions .note { font-size: 13px; color: var(--ink-2); flex: 1 1 200px; min-width: 0; }
.sheet-wrap { background: var(--well); border-radius: var(--r); padding: 16px; display: grid; justify-items: center; gap: 10px; }
#sheet { width: 100%; max-width: 620px; height: auto; background: var(--sheet); box-shadow: var(--shadow); display: block; }
.pager { display: flex; align-items: center; gap: 12px; font: 12px var(--mono); color: var(--ink-2); }
.msg { padding: 10px 12px; border-radius: 8px; background: var(--well); font-size: 13px; }
.msg.err { color: var(--danger); }
.extras { padding: 14px 16px; }
.extras .row { align-items: center; }
.extras input[type=number] { width: 84px; }

/* ---- scan ---- */
.scan-grid { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(280px, 1fr); gap: 20px; align-items: start; }
/* The viewfinder is always a square, so the counter under it stays in sight on a phone. The live
   picture is painted on the canvas by the page itself (the middle square of the camera frame; the
   whole frame is read), together with the markers, so the two always share one coordinate system.
   The <video> only delivers the frames; it lies under the canvas. A still picture is shown whole. */
.viewport { position: relative; background: #0b0c0e; border-radius: var(--r); overflow: hidden; width: 100%; height: 0; padding-top: 100%; }
.vp-in { position: absolute; left: 0; top: 0; width: 100%; height: 100%; display: grid; place-items: center; }
.vp-in > video, .vp-in > #overlay { position: absolute; left: 0; top: 0; width: 100%; height: 100%; }
.vp-in > video { object-fit: cover; opacity: .01; }
.vp-in > #overlay { pointer-events: none; }
.pic { position: relative; line-height: 0; max-width: 100%; }
.pic #still { display: block; width: 100%; height: auto; }
.pic #still-overlay { position: absolute; left: 0; top: 0; width: 100%; height: 100%; pointer-events: none; }
.finder { position: absolute; left: 14px; top: 14px; width: calc(100% - 28px); height: calc(100% - 28px); pointer-events: none; fill: none; stroke: rgba(255, 255, 255, .8); stroke-width: 3; stroke-linecap: round; }
.vf-bar { position: absolute; left: 0; top: 0; width: 100%; height: 5px; background: rgba(255, 255, 255, .16); pointer-events: none; }
.vf-bar i { display: block; height: 100%; width: 0; background: #1BB24B; transition: width .2s; }
.vf-badge { position: absolute; left: 10px; top: 14px; background: rgba(11, 12, 14, .74); color: #fff; font: 700 17px/1.3 var(--sans); font-variant-numeric: tabular-nums; border-radius: 8px; padding: 3px 10px; pointer-events: none; }
.vf-badge small { font: 500 12px var(--mono); opacity: .8; }
.vf-foot { position: absolute; left: 10px; right: 10px; bottom: 10px; display: grid; gap: 6px; justify-items: center; pointer-events: none; }
.vf-tip { background: rgba(11, 12, 14, .74); color: #fff; font-weight: 600; font-size: 14px; line-height: 1.35; border-radius: 8px; padding: 6px 12px; }
.vf-note { justify-self: stretch; background: rgba(11, 12, 14, .74); color: #fff; font-size: 13px; line-height: 1.35; border-radius: 8px; padding: 6px 10px; pointer-events: none; text-align: center; }
.vf-note.warn { color: #ffd27a; }
.vf-map { position: absolute; right: 10px; top: 14px; display: grid; gap: 4px; justify-items: center; background: rgba(11, 12, 14, .74); border-radius: 8px; padding: 6px; pointer-events: none; }
.vf-map canvas { display: block; }
.vf-map span { font: 500 11px/1.2 var(--mono); color: #fff; opacity: .85; }
.vf-map span:empty { display: none; }
.viewport-empty { position: relative; z-index: 1; color: #d9dde3; text-align: center; padding: 20px; max-width: 34ch; font-size: 14px; }
.viewport-empty svg { width: 56px; height: 56px; margin-bottom: 12px; }
.viewport:has(> .vp-in > #viewport-empty:not([hidden])) { cursor: pointer; }
.cam-controls { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.cam-controls .btn { flex: 1 1 130px; }
.cam-pick { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.cam-pick label { font: 600 11px/1.3 var(--mono); letter-spacing: .09em; text-transform: uppercase; color: var(--ink-2); flex: none; }
.cam-pick select { flex: 1 1 auto; min-width: 0; }
.cam-controls .btn.icon { flex: 0 0 46px; padding: 0; }
.cam-controls .btn.icon svg { width: 20px; height: 20px; fill: currentColor; }
.cam-controls .btn.icon[aria-pressed="true"] { background: var(--ink); color: var(--surface); border-color: var(--ink); }
@media (max-width: 360px) { .cam-controls { gap: 8px; } .cam-controls .btn { flex-basis: 100px; padding: 0 8px; } .cam-controls .btn.icon { flex-basis: 42px; } }
.progress { padding: 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.count { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.count .n { font: 800 44px/1 var(--sans); letter-spacing: -.03em; font-variant-numeric: tabular-nums; }
.count .of { font: 14px var(--mono); color: var(--ink-2); }
.bar { height: 8px; border-radius: 4px; background: var(--well); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.bar.done i { background: var(--ok); }
.status { font-size: 13px; color: var(--ink-2); min-height: 1.5em; }
.status b { color: var(--ink); }
.need { font-size: 14px; }
.need.warn { color: var(--warn); font-weight: 600; }
.maps { display: flex; flex-wrap: wrap; gap: 12px; }
.maps figure { margin: 0; }
.maps canvas { display: block; border-radius: 3px; }
.maps figcaption { font: 11px/1.4 var(--mono); color: var(--ink-2); margin-top: 4px; }
.result { border-top: 1px solid var(--line); padding-top: 14px; display: grid; gap: 10px; scroll-margin-top: 12px; }
.result h3 { margin: 0; font-size: 16px; overflow-wrap: anywhere; }
.result .meta { font: 12px/1.5 var(--mono); color: var(--ink-2); }
.result .ok { color: var(--ok); font-weight: 600; }
.preview { max-height: 220px; overflow: auto; margin: 0; padding: 10px; background: var(--well); border-radius: 8px; font: 12px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; }
.preview-img { max-width: 100%; max-height: 240px; border-radius: 8px; background: var(--sheet); }
.row { display: flex; flex-wrap: wrap; gap: 10px; }
.counters { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.counters div { background: var(--surface); padding: 8px 10px; }
.counters b { display: block; font: 700 16px/1.2 var(--sans); font-variant-numeric: tabular-nums; }
.counters span { font: 11px/1.3 var(--mono); color: var(--ink-2); }
.log { max-height: 180px; overflow: auto; margin: 10px 0 0; padding: 10px; background: var(--well); border-radius: 8px; font: 12px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; }
.others { font-size: 13px; color: var(--ink-2); display: grid; gap: 6px; }


/* ---- format ---- */
.spec { display: grid; grid-template-columns: minmax(0, 1fr); gap: 22px; max-width: 860px; }
.spec > * { min-width: 0; }
.spec h2 { margin: 0 0 4px; font-size: 22px; letter-spacing: -.02em; text-wrap: balance; }
.spec h3 { margin: 0 0 8px; font-size: 15px; }
.spec p, .spec li { max-width: 68ch; color: var(--ink-2); }
.spec p { margin: 0 0 10px; }
.spec ul.plain { margin: 0; padding-left: 18px; }
.anatomy { display: grid; grid-template-columns: minmax(0, 260px) minmax(0, 1fr); gap: 20px; align-items: center; }
.anatomy > * { min-width: 0; }
.anatomy canvas { width: 100%; max-width: 320px; height: auto; justify-self: center; background: var(--sheet); image-rendering: pixelated; border-radius: 4px; box-shadow: var(--shadow); }
.legend { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; font-size: 14px; }
.legend li { display: grid; grid-template-columns: 14px minmax(0, 1fr); gap: 10px; align-items: baseline; color: var(--ink); max-width: none; }
.legend i { width: 12px; height: 12px; border-radius: 2px; border: 1px solid var(--line); transform: translateY(1px); }
.legend.row { display: flex; flex-wrap: wrap; gap: 6px 18px; }
.pair { display: grid; gap: 12px; margin-bottom: 12px; }
.pair canvas { width: 100%; max-width: 560px; height: auto; background: var(--sheet); image-rendering: pixelated; border-radius: 4px; box-shadow: var(--shadow); }
.steps { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; counter-reset: step; }
.steps li { counter-increment: step; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 12px; display: grid; grid-template-columns: 22px minmax(0, 1fr); gap: 6px 8px; align-items: center; align-content: start; font-size: 14px; max-width: none; }
.steps li span { grid-column: 1 / -1; }      /* number and name on one line, the explanation under both */
.steps li::before { content: counter(step); width: 22px; height: 22px; border-radius: 6px; background: var(--accent); color: var(--on-accent); font: 700 12px/22px var(--mono); text-align: center; }
.steps b { color: var(--ink); }
.guards { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.guards li { max-width: 68ch; padding-left: 12px; border-left: 3px solid var(--accent); }
.guards b { color: var(--ink); }
.toy { display: grid; grid-template-columns: minmax(0, 300px) minmax(0, 1fr); gap: 20px; align-items: start; }
.toy-grid { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 4px; }
.toy-grid button { aspect-ratio: 1; border: 0; border-radius: 4px; padding: 0; background: var(--accent); cursor: pointer; }
.toy-grid button.spare { opacity: .5; }
.toy-grid button[aria-pressed="true"] { background: transparent; opacity: 1; box-shadow: inset 0 0 0 1px var(--line); }
.toy-status { font-weight: 600; color: var(--ok); margin: 0 0 10px; }
.toy-status.warn { color: var(--warn); }
.toy-side .hint { margin-top: 10px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--r); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font: 600 11px var(--mono); letter-spacing: .06em; text-transform: uppercase; color: var(--ink-2); }
td.num, th.num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
tr:last-child td { border-bottom: 0; }
td.wrap { white-space: normal; min-width: 220px; color: var(--ink-2); }

/* ---- presenter ---- */
#presenter { position: fixed; left: 0; top: 0; right: 0; bottom: 0; z-index: 20; background: #ffffff; color: #101216; display: grid; grid-template-rows: auto minmax(0, 1fr); padding-top: env(safe-area-inset-top, 0px); }
.present-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; padding: 10px 16px; border-bottom: 1px solid #d5d9df; font: 12px var(--mono); }
.present-bar .grow { flex: 1; min-width: 120px; }
.present-bar b { max-width: min(40ch, 50vw); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.present-bar .btn, .present-bar select { background: #fff; color: #101216; border-color: #c9ced6; }
.present-bar select { width: auto; min-height: 32px; padding: 4px 8px; }
.present-stage { display: grid; place-items: center; min-height: 0; padding: 8px; }
.present-stage canvas { image-rendering: pixelated; max-width: 100%; max-height: 100%; }
#toast { position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); z-index: 30; max-width: min(92vw, 520px);
  background: var(--ink); color: var(--paper); padding: 10px 14px; border-radius: 8px; font-size: 13px; box-shadow: var(--shadow); }

@media (max-width: 820px) {
  .create-grid, .scan-grid, .anatomy, .toy { grid-template-columns: minmax(0, 1fr); }
  .steps { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .toy-grid { max-width: 340px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .top { justify-content: center; }
  .tabs { width: 100%; }
  .tabs button { flex: 1; padding-inline: 8px; }
  .count .n { font-size: 38px; }
  table { font-size: 12px; }
  th, td { white-space: normal; padding: 7px 6px; }
  td.num, th.num { white-space: nowrap; }
  td.wrap { min-width: 0; }
  .opt { display: none; }               /* less important table columns */
}
@media (prefers-reduced-motion: reduce) { .bar i { transition: none; } }
