/* Layout: a desk with three trays — pages on the left, the paper in the middle,
   the spreadsheet on the right. Paperwork vernacular: ruled lines, column letters,
   a highlighter for anything the reader should double-check. */
@font-face { font-family: "Plex Sans"; src: url("fonts/ibm-plex-sans-latin-400-normal.woff2") format("woff2"); font-weight: 400; font-display: swap; }
@font-face { font-family: "Plex Sans"; src: url("fonts/ibm-plex-sans-latin-500-normal.woff2") format("woff2"); font-weight: 500; font-display: swap; }
@font-face { font-family: "Plex Sans"; src: url("fonts/ibm-plex-sans-latin-600-normal.woff2") format("woff2"); font-weight: 600; font-display: swap; }
@font-face { font-family: "Plex Mono"; src: url("fonts/ibm-plex-mono-latin-400-normal.woff2") format("woff2"); font-weight: 400; font-display: swap; }
@font-face { font-family: "Plex Mono"; src: url("fonts/ibm-plex-mono-latin-500-normal.woff2") format("woff2"); font-weight: 500; font-display: swap; }
@font-face { font-family: "Plex Cond"; src: url("fonts/ibm-plex-sans-condensed-latin-600-normal.woff2") format("woff2"); font-weight: 600; font-display: swap; }
@font-face { font-family: "Plex Cond"; src: url("fonts/ibm-plex-sans-condensed-latin-700-normal.woff2") format("woff2"); font-weight: 700; font-display: swap; }

:root {
  --desk: #E9EEF1;      /* app background, cool grey-blue */
  --sheet: #FFFFFF;     /* panels and paper */
  --ink: #17232D;       /* text */
  --muted: #5A6A77;     /* secondary text */
  --rule: #D3DCE2;      /* hairlines */
  --rule-strong: #B4C2CC;
  --accent: #1D6A86;    /* ledger blue: actions, column lines */
  --accent-ink: #FFFFFF;
  --accent-soft: #DCEBF1;
  --mark: #FFE680;      /* highlighter: check this cell */
  --mark-ink: #3A2E00;
  --ok: #2B7A4B;
  --warn: #A3551A;
  --danger: #A8322D;
  --shadow: 0 1px 2px rgba(23, 35, 45, .08), 0 4px 14px rgba(23, 35, 45, .06);
  --f-ui: "Plex Sans", "Segoe UI", system-ui, -apple-system, sans-serif;
  --f-head: "Plex Cond", "Arial Narrow", "Segoe UI", system-ui, sans-serif;
  --f-data: "Plex Mono", Consolas, "SF Mono", ui-monospace, monospace;
  --r: 6px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --desk: #0E151A; --sheet: #162028; --ink: #E3EAEE; --muted: #93A4B0; --rule: #26343E; --rule-strong: #384955;
    --accent: #5DB3D3; --accent-ink: #062330; --accent-soft: #17323F; --mark: #5E4E0E; --mark-ink: #FFF4C2;
    --ok: #5FC28B; --warn: #E09A5E; --danger: #EE7B74;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 14px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --desk: #0E151A; --sheet: #162028; --ink: #E3EAEE; --muted: #93A4B0; --rule: #26343E; --rule-strong: #384955;
  --accent: #5DB3D3; --accent-ink: #062330; --accent-soft: #17323F; --mark: #5E4E0E; --mark-ink: #FFF4C2;
  --ok: #5FC28B; --warn: #E09A5E; --danger: #EE7B74;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 14px rgba(0,0,0,.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--desk); color: var(--ink);
  font: 14px/1.45 var(--f-ui);
  display: flex; flex-direction: column;
}
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */
.bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding-block: 10px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--sheet); border-bottom: 1px solid var(--rule);
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: 6px; }
.brand svg { width: 28px; height: 28px; flex: none; }
.brand b { font: 700 20px/1 var(--f-head); letter-spacing: .01em; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--ok); border: 1px solid currentColor; border-radius: 99px; padding: 2px 9px;
}
.chip i { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.group { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

.btn {
  display: inline-flex; align-items: center; gap: 7px;
  height: 34px; padding: 0 12px; border-radius: var(--r);
  border: 1px solid var(--rule-strong); background: var(--sheet); cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn svg { width: 16px; height: 16px; flex: none; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 500; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.sm { height: 28px; padding: 0 9px; font-size: 13px; }
.btn[disabled] { opacity: .45; cursor: not-allowed; }

.seg { display: inline-flex; border: 1px solid var(--rule-strong); border-radius: var(--r); overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 0 12px; height: 32px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--rule); }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 30; min-width: 240px;
  background: var(--sheet); border: 1px solid var(--rule-strong); border-radius: var(--r); box-shadow: var(--shadow); padding: 6px;
}
.menu.left { right: auto; left: 0; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 4px; cursor: pointer; }
.menu button:hover { background: var(--accent-soft); }
.menu small { display: block; color: var(--muted); font-size: 12px; }
.menu hr { border: 0; border-top: 1px solid var(--rule); margin: 6px 0; }
.menu label { display: flex; gap: 8px; align-items: center; padding: 6px 10px; font-size: 13px; }

/* ---------- desk ---------- */
.desk {
  flex: 1; min-height: 0; display: grid; gap: 12px;
  grid-template-columns: 172px minmax(0, 1fr) minmax(0, 1.15fr);
  padding-block: 12px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.tray { background: var(--sheet); border: 1px solid var(--rule); border-radius: 8px; min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.tray-head {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 12px; border-bottom: 1px solid var(--rule); min-height: 46px;
}
.tray-head h2 { margin: 0; font: 600 13px/1 var(--f-head); text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.tray-body { flex: 1; min-height: 0; overflow: auto; }

/* pages */
.pages { list-style: none; margin: 0; padding: 10px; display: flex; flex-direction: column; gap: 10px; }
.page-card {
  display: grid; gap: 6px; padding: 6px; border-radius: 6px; border: 1px solid transparent; cursor: pointer; background: none; text-align: left; width: 100%;
}
.page-card:hover { border-color: var(--rule-strong); }
.page-card[aria-current="true"] { border-color: var(--accent); background: var(--accent-soft); }
.page-card .thumb { aspect-ratio: 8.5 / 11; max-width: 100%; background: var(--desk) center/contain no-repeat; border: 1px solid var(--rule); border-radius: 3px; position: relative; overflow: hidden; }
.page-card .meta { display: flex; justify-content: space-between; gap: 6px; font-size: 12px; min-width: 0; }
.page-card .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status { font: 500 11px/1.6 var(--f-data); padding: 0 6px; border-radius: 3px; white-space: nowrap; }
.status.done { color: var(--ok); }
.status.work { color: var(--accent); }
.status.err { color: var(--danger); }
.status.wait { color: var(--muted); }
.progress { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--accent); transition: width .2s; }
.add-page {
  border: 1px dashed var(--rule-strong); border-radius: 6px; background: none; padding: 14px 8px; cursor: pointer; color: var(--muted); font-size: 12px;
}
.add-page:hover { color: var(--accent); border-color: var(--accent); }

/* preview */
.stage { position: relative; flex: 1; min-height: 0; overflow: auto; background: var(--desk); padding: 14px; }
.paper { position: relative; margin: 0 auto; box-shadow: var(--shadow); background: #fff; line-height: 0; }
.paper canvas { display: block; width: 100%; height: auto; }
.paper svg.overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
.overlay .word { fill: transparent; stroke: none; }
.overlay .word.low { fill: rgba(255, 214, 0, .38); }
.overlay .band { fill: rgba(29, 106, 134, .06); stroke: rgba(29, 106, 134, .55); stroke-dasharray: 6 5; stroke-width: 2; }
.overlay .col { stroke: #1D6A86; stroke-width: 3; }
.overlay .col-hit { stroke: transparent; stroke-width: 22; cursor: ew-resize; }
.overlay .col-tab { fill: #1D6A86; cursor: ew-resize; }
.overlay .col-tab-t { fill: #fff; font: 600 22px var(--f-data); pointer-events: none; }
.overlay .col.dragging { stroke: #A3551A; }
.stage-foot { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: 8px 12px; border-top: 1px solid var(--rule); font-size: 13px; color: var(--muted); }
.stage-foot input[type="range"] { width: 120px; accent-color: var(--accent); }
.hint { font-size: 12px; color: var(--muted); }

/* table */
.grid-wrap { flex: 1; min-height: 0; overflow: auto; }
table.grid { border-collapse: separate; border-spacing: 0; font: 13px/1.35 var(--f-data); font-variant-numeric: tabular-nums; min-width: 100%; }
.grid th, .grid td { border-right: 1px solid var(--rule); border-bottom: 1px solid var(--rule); padding: 6px 8px; vertical-align: top; min-width: 70px; max-width: 320px; }
.grid thead th {
  position: sticky; top: 0; z-index: 2; background: var(--sheet); font: 600 12px/1 var(--f-data); color: var(--muted); text-align: left; padding: 0;
  border-bottom: 1px solid var(--rule-strong);
}
.grid thead th .colh { display: flex; align-items: center; justify-content: space-between; gap: 4px; padding: 6px 4px 6px 8px; }
.grid th.rowh, .grid td.rowh { position: sticky; left: 0; z-index: 1; background: var(--sheet); min-width: 0; width: 1%; text-align: right; color: var(--muted); font-size: 11px; padding: 6px 6px; white-space: nowrap; }
.grid thead th.rowh { z-index: 3; }
.grid td[contenteditable] { outline: none; white-space: pre-wrap; word-break: break-word; }
.grid td[contenteditable]:focus { box-shadow: inset 0 0 0 2px var(--accent); background: var(--accent-soft); }
.grid td.low { background: var(--mark); color: var(--mark-ink); }
.grid td.num { text-align: right; }
.grid tr.header td { font-weight: 500; background: var(--accent-soft); }
.grid tr.header td.low { background: var(--mark); }
.icon-btn { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 2px 4px; border-radius: 3px; line-height: 1; font-size: 13px; }
.icon-btn:hover { color: var(--danger); background: var(--accent-soft); }
.row-tools { display: inline-flex; gap: 0; visibility: hidden; }
.grid tr:hover .row-tools, .grid tr:focus-within .row-tools { visibility: visible; }
.row-tools .icon-btn:hover { color: var(--accent); }

.extras { border-top: 1px solid var(--rule); padding: 10px 12px; display: grid; gap: 10px; max-height: 38%; overflow: auto; }
.extras h3 { margin: 0 0 4px; font: 600 12px/1 var(--f-head); text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.fields { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 13px; }
.fields dt { color: var(--muted); }
.fields dd { margin: 0; font-family: var(--f-data); min-width: 0; overflow-wrap: anywhere; }
.fields dd[contenteditable]:focus { outline: 2px solid var(--accent); }
.notes { margin: 0; padding-left: 18px; font-size: 12px; color: var(--muted); }
.notes li { overflow-wrap: anywhere; }

/* empty state */
.empty { display: grid; place-content: center; gap: 14px; text-align: center; padding: 30px 16px; height: 100%; color: var(--muted); }
.empty h3 { margin: 0; color: var(--ink); font: 600 22px/1.2 var(--f-head); text-wrap: balance; }
.empty p { margin: 0; max-width: 46ch; }
.empty .group { justify-content: center; }
.drop-active .stage { outline: 3px dashed var(--accent); outline-offset: -10px; }

/* toast, modal */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(18px + env(safe-area-inset-bottom, 0px)); z-index: 50;
  background: var(--ink); color: var(--sheet); padding: 9px 14px; border-radius: var(--r); box-shadow: var(--shadow); font-size: 13px; max-width: calc(100% - 32px);
}
dialog { border: 1px solid var(--rule-strong); border-radius: 10px; background: var(--sheet); color: var(--ink); padding: 0; max-width: min(560px, calc(100% - 32px)); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(10, 18, 24, .45); }
dialog .d-head { padding: 14px 18px; border-bottom: 1px solid var(--rule); font: 600 17px var(--f-head); }
dialog .d-body { padding: 14px 18px; display: grid; gap: 10px; font-size: 13.5px; }
dialog .d-body p { margin: 0; }
dialog .d-foot { padding: 12px 18px; border-top: 1px solid var(--rule); display: flex; justify-content: flex-end; gap: 8px; }
dialog video { width: 100%; border-radius: 6px; background: #000; }
.credits { font-size: 12px; color: var(--muted); }
.pastebox { width: 100%; height: 160px; font: 12px var(--f-data); background: var(--desk); color: var(--ink); border: 1px solid var(--rule); border-radius: 6px; padding: 8px; }

/* ---------- narrow screens: stack the trays ---------- */
@media (max-width: 1050px) {
  .desk { grid-template-columns: minmax(0, 1fr); grid-auto-rows: auto; overflow: auto; }
  .tray.pages-tray .tray-body { overflow-x: auto; }
  .pages { flex-direction: row; }
  .page-card { width: 110px; flex: none; }
  .add-page { width: 110px; flex: none; }
  .tray.preview-tray { height: 70vh; }
  .tray.table-tray { min-height: 60vh; }
  .bar .spacer { display: none; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
.grid td.num { white-space: nowrap; word-break: normal; }
@media (max-width: 1050px) { .pages-tray .tray-body { flex: none; } }
