:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --stage: #d9dce1;
  --text: #1c2128;
  --muted: #667080;
  --line: #d0d5dc;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --warn: #c2410c;
  --skip: #8b93a1;
  color-scheme: light;
}
/* Dark palette: by system preference unless the user chose light, or when chosen explicitly. */
:root[data-theme="dark"] {
  --bg: #16191e;
  --panel: #1f242b;
  --stage: #0f1114;
  --text: #e3e7ec;
  --muted: #98a2b0;
  --line: #343b45;
  --accent: #4c8dff;
  --accent-text: #0b0e12;
  --warn: #fb923c;
  --skip: #6b7482;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #16191e;
    --panel: #1f242b;
    --stage: #0f1114;
    --text: #e3e7ec;
    --muted: #98a2b0;
    --line: #343b45;
    --accent: #4c8dff;
    --accent-text: #0b0e12;
    --warn: #fb923c;
    --skip: #6b7482;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: grid;
  grid-template-rows: auto 1fr;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, "Segoe UI", sans-serif;
  overflow: hidden;
}
[hidden] { display: none !important; }

button, select, input[type="text"], input[type="number"] {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 4px 9px;
}
button { cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 12px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
header h1 { font-size: 15px; margin: 0; }
#docName { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 30vw; }
#status { margin-inline-start: auto; color: var(--muted); }
#version { color: var(--muted); font-size: 12px; white-space: nowrap; }
#updateBtn { padding: 2px 8px; font-size: 12px; }
label.inline { display: flex; align-items: center; gap: 6px; color: var(--muted); }

main {
  display: grid;
  grid-template-columns: 170px minmax(0, 1fr) 300px;
  min-height: 0;
}

#thumbs {
  overflow-y: auto;
  padding: 8px;
  border-inline-end: 1px solid var(--line);
  background: var(--panel);
}
.thumb {
  position: relative;
  margin-bottom: 8px;
  padding: 3px;
  border: 2px solid transparent;
  border-radius: 5px;
  cursor: pointer;
}
.thumb.current { border-color: var(--accent); }
.thumb canvas { display: block; width: 100%; background: #fff; }
.thumb .num { display: block; text-align: center; color: var(--muted); font-size: 11px; }
.thumb .dot {
  position: absolute; top: 6px; right: 6px;
  width: 10px; height: 10px; border-radius: 50%;
  border: 1px solid #fff;
}
.thumb .dot.review { background: var(--warn); }
.thumb .dot.blank { background: var(--skip); }
.thumb .dot.manual { background: var(--accent); }

#center { display: grid; grid-template-rows: auto 1fr; min-width: 0; min-height: 0; }
#toolbar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 6px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
#sheetLabel { min-width: 90px; text-align: center; }
.sep { width: 1px; height: 18px; background: var(--line); }
.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-inline-start: -1px; }
.seg button:first-child { border-start-start-radius: 5px; border-end-start-radius: 5px; margin-inline-start: 0; }
.seg button:last-child { border-start-end-radius: 5px; border-end-end-radius: 5px; }
#zoomBtn { min-width: 52px; }
#flagNote { color: var(--warn); }

#stage {
  position: relative;
  display: flex;
  min-height: 0; overflow: auto;
  background: var(--stage);
}
#stage.drop { outline: 3px dashed var(--accent); outline-offset: -10px; }
#empty { margin: auto; text-align: center; color: var(--muted); max-width: 420px; padding: 20px; }
#empty strong { color: var(--text); font-size: 15px; }
#wrap { position: relative; flex: none; margin: auto; box-shadow: 0 2px 12px rgba(0, 0, 0, .3); }
#view { display: block; width: 100%; height: 100%; background: #fff; }
#overlay { position: absolute; inset: 0; touch-action: none; user-select: none; }

.box {
  position: absolute;
  border: 2px solid var(--accent);
  background: rgba(31, 111, 235, .10);
  cursor: move;
}
.box.active { background: rgba(31, 111, 235, .18); box-shadow: 0 0 0 1px #fff; }
.box.review { border-color: var(--warn); background: rgba(234, 88, 12, .12); }
.box.excluded { border: 2px dashed var(--skip); background: rgba(120, 128, 140, .25); }
.box .tag {
  position: absolute; top: 0; left: 0;
  display: flex; align-items: center; gap: 6px;
  padding: 1px 6px;
  background: var(--accent); color: #fff;
  font-size: 12px; white-space: nowrap; cursor: default;
}
.box.review .tag { background: var(--warn); }
.box.excluded .tag { background: var(--skip); }
.box .tag input { margin: 0; cursor: pointer; }
.handle {
  --size: 12px;
  --out: calc(var(--size) / -2 - 1px);
  position: absolute; width: var(--size); height: var(--size);
  background: #fff; border: 2px solid var(--accent); border-radius: 2px;
}
.box.excluded .handle { border-color: var(--skip); }
.handle.n, .handle.s { left: calc(50% - var(--size) / 2); cursor: ns-resize; }
.handle.e, .handle.w { top: calc(50% - var(--size) / 2); cursor: ew-resize; }
.handle.n, .handle.nw, .handle.ne { top: var(--out); }
.handle.s, .handle.sw, .handle.se { bottom: var(--out); }
.handle.w, .handle.nw, .handle.sw { left: var(--out); }
.handle.e, .handle.ne, .handle.se { right: var(--out); }
.handle.nw, .handle.se { cursor: nwse-resize; }
.handle.ne, .handle.sw { cursor: nesw-resize; }

#settings {
  overflow-y: auto;
  padding: 10px 14px 20px;
  border-inline-start: 1px solid var(--line);
  background: var(--panel);
}
#settings h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 6px 0 10px; }
#settings label { display: block; margin-bottom: 10px; color: var(--muted); }
#settings label > select, #settings label > input:not([type="checkbox"]) { display: block; width: 100%; margin-top: 3px; }
#settings label.check { display: flex; align-items: center; gap: 6px; color: var(--text); }
#settings input[type="range"] { padding: 0; }
#settings output { float: inline-end; color: var(--text); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
progress { width: 100%; margin: 4px 0 10px; }
#preview { display: block; width: 100%; border: 1px solid var(--line); background: #fff; }
#previewInfo { color: var(--muted); margin: 6px 0 0; }

#angle { width: 64px; }
#rotateSel { max-width: 150px; }
button.wide { display: block; width: 100%; margin-bottom: 10px; }
#presets { display: flex; margin-bottom: 12px; }
#presets button { flex: 1 1 auto; padding: 5px 2px; font-size: 12px; white-space: nowrap; }

/* Page organiser */
dialog {
  width: min(900px, 94vw); max-height: 88vh;
  padding: 14px 16px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialogHead { display: flex; align-items: center; gap: 8px; }
.dialogHead h2 { font-size: 15px; margin: 0; margin-inline-end: auto; }
.hint { color: var(--muted); margin: 8px 0 12px; }
#orgGrid { display: flex; flex-wrap: wrap; gap: 12px; }
.tile {
  width: 124px; padding: 4px;
  border: 2px solid transparent; border-radius: 6px;
  cursor: grab; text-align: center;
}
.tile.over { border-color: var(--accent); }
.tile canvas { display: block; width: 100%; background: #fff; border: 1px solid var(--line); }
.tile .num { display: block; margin: 3px 0; color: var(--muted); font-size: 11px; }
.tile .tools { display: flex; gap: 2px; }
.tile .tools button { flex: 1; padding: 2px 0; font-size: 12px; }
#applySel { max-width: 190px; }
#lang, #theme { color: var(--muted); }
#angle:disabled { opacity: .5; }

/* Fingers need bigger targets than a mouse pointer. */
@media (pointer: coarse) {
  .handle { --size: 22px; }
  button, select, input[type="text"], input[type="number"] { padding: 8px 11px; }
  .box .tag { padding: 4px 8px; font-size: 13px; }
  .box .tag input { width: 18px; height: 18px; }
}

/* Small laptops and tablets: same three columns, narrower side panels. */
@media (max-width: 1100px) {
  main { grid-template-columns: 120px minmax(0, 1fr) 250px; }
  #docName { max-width: 18vw; }
}

/* Phones and narrow windows: one column, the page scrolls. */
@media (max-width: 760px) {
  html, body { height: auto; }
  body { display: block; overflow: auto; }
  header { gap: 8px; }
  header h1 { display: none; }
  #docName { max-width: 45vw; }
  #status { margin-inline-start: 0; flex-basis: 100%; }
  main, #center { display: block; }
  #thumbs {
    display: flex; gap: 6px;
    overflow-x: auto; overflow-y: hidden;
    padding: 6px;
    border-inline-end: 0; border-bottom: 1px solid var(--line);
  }
  .thumb { flex: 0 0 92px; margin: 0; }
  #stage { padding: 20px 0; min-height: 200px; max-height: 85vh; }
  #settings { border-inline-start: 0; border-top: 1px solid var(--line); }
  #preview { max-width: 340px; margin: 0 auto; }
  #previewInfo { text-align: center; }
}
