:root {
  --bg: #f6f7f9;
  --panel: #fff;
  --line: #dcdfe4;
  --ink: #1b1f24;
  --muted: #6b727c;
  --accent: #2f6feb;
  --flag: #b54708;
  --flag-bg: #fff5e9;
  --ok: #1a7f4b;
  --danger: #b42318;
  --tap: 38px;            /* minimum comfortable touch target */
}

* { box-sizing: border-box; }

/* Any `display` set by a class outranks the user-agent rule for [hidden], so
   an element given both stays visible. Everything here toggles visibility
   through the attribute, so make it win. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

/* ------------------------------------------------------------------ header */

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
  flex-wrap: wrap;
}
header h1 { font-size: 16px; margin: 0; letter-spacing: -0.01em; }
header nav { display: flex; gap: 4px; }
header .who {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}
header .who input { width: 130px; }

/* ------------------------------------------------------------------ atoms */

button {
  font: inherit;
  padding: 6px 12px;
  min-height: var(--tap);
  border: 1px solid transparent;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button:hover { filter: brightness(1.07); }
button.ghost, nav button {
  background: var(--panel);
  color: var(--ink);
  border-color: var(--line);
}
button.ghost.active, nav button.active {
  background: #e8eefc;
  border-color: #b9cdf7;
  color: var(--accent);
}
button.danger { color: var(--danger); }
button:disabled { opacity: .5; cursor: default; }

input, select, textarea {
  font: inherit;
  padding: 6px 8px;
  min-height: var(--tap);
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: inherit;
  width: 100%;
  /* 16px stops iOS Safari zooming on focus. */
  font-size: max(16px, 1em);
}

label { display: block; font-size: 12px; color: var(--muted); }
label input, label select, label textarea { margin-top: 3px; color: var(--ink); }

.view { padding: 16px; }

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.bar .spacer { flex: 1; }
.bar select { width: auto; }
.check { display: flex; align-items: center; gap: 5px; white-space: nowrap; }
.check input { width: auto; min-height: auto; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  max-width: 640px;
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.card h2 { font-size: 14px; margin: 0; }
.card button[type=submit] { align-self: flex-start; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }

.note { font-size: 12px; color: var(--muted); margin: 0; }
.status { font-size: 13px; margin: 0; }
.empty { color: var(--muted); padding: 20px 0; }

#up-preview {
  max-width: 100%; max-height: 240px; border-radius: 6px;
  border: 1px solid var(--line);
}

/* -------------------------------------------------------------- sheet list */

table.list, table.results {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
table.list th, table.results th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  font-weight: 600;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
}
table.list td, table.results td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
table.list tbody tr:hover { background: #f0f4fb; cursor: pointer; }
table.list .sub { display: block; color: var(--muted); font-size: 12px; }

.pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  background: #eef0f3;
  color: var(--muted);
  white-space: nowrap;
}
.pill.needs_review { background: var(--flag-bg); color: var(--flag); }
.pill.approved { background: #e8f6ee; color: var(--ok); }
.pill.failed, .pill.rejected { background: #fdecea; color: var(--danger); }
.pill.processing { background: #e8eefc; color: var(--accent); }

.banner {
  background: #fdecea;
  border: 1px solid #f5c6c2;
  color: var(--danger);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 0 0 12px;
}

.pages { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.pages .muted { color: var(--muted); font-size: 12px; margin-right: 2px; }
.pages button.page {
  min-width: 32px;
  min-height: 32px;
  padding: 3px 6px;
  font-size: 12px;
  background: var(--panel);
  color: var(--ink);
  border-color: var(--line);
}
.pages button.page.needs_review { background: var(--flag-bg); color: var(--flag); }
.pages button.page.approved { background: #e8f6ee; color: var(--ok); }
.pages button.page.failed, .pages button.page.rejected {
  background: #fdecea; color: var(--danger);
}
.pages button.page.current { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ------------------------------------------------------------------ review */

.split { display: grid; grid-template-columns: minmax(260px, 38%) 1fr; gap: 16px; }

.pane-image { position: sticky; top: 64px; align-self: start; }
.canvas { position: relative; line-height: 0; }
.canvas img {
  width: 100%; border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel);
}
#boxes { position: absolute; inset: 0; pointer-events: none; }
#boxes div {
  position: absolute;
  border: 2px solid var(--accent);
  border-radius: 2px;
  background: rgba(47, 111, 235, .12);
}
#boxes div[data-cell="reg"] {
  border-color: var(--flag); background: rgba(181, 71, 8, .12);
}

/* The rows table is the one thing that genuinely needs width. Let it scroll
   inside its own pane rather than pushing the page sideways. */
.pane-rows { min-width: 0; overflow-x: auto; }

table.results td { font-variant-numeric: tabular-nums; }
table.results input { width: 68px; }
table.results input.grade { width: 66px; text-transform: uppercase; }
table.results input.reg {
  width: 100%; min-width: 130px;
  font-family: ui-monospace, SFMono-Regular, monospace;
}
table.results tr.flagged { background: var(--flag-bg); }
table.results tr.selected td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
table.results tr.selected td { background: rgba(47, 111, 235, .06); }
table.results tr.saving { opacity: .55; }
.idx { color: var(--muted); cursor: pointer; }

.flags { margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px; }
.flags span {
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--flag-bg);
  color: var(--flag);
  border: 1px solid #f3d2ae;
}
.suggest { margin-top: 4px; font-size: 12px; color: var(--muted); }
.suggest button { padding: 1px 7px; font-size: 12px; margin-left: 4px;
                  min-height: auto; }

#toast {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: #1b1f24;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  z-index: 20;
  max-width: calc(100vw - 32px);
}
#toast.bad { background: var(--danger); }

/* -------------------------------------------------------------- responsive */

/* Tablet: the scan and the rows stop competing for width. The image moves
   above the table and stops being sticky — on a short viewport a sticky pane
   would eat the screen the rows need. */
@media (max-width: 900px) {
  .split { grid-template-columns: 1fr; }
  .pane-image { position: static; }
  .canvas img { max-height: 45vh; width: auto; max-width: 100%; }
  .canvas { text-align: center; }
  /* An image that is no longer full-width breaks percentage-positioned
     overlays, so hide them rather than draw them in the wrong place. */
  #boxes { display: none; }
}

/* Phone. */
@media (max-width: 620px) {
  .view { padding: 12px; }
  header { gap: 8px; padding: 8px 12px; }
  header h1 { font-size: 15px; }
  header .who { width: 100%; margin-left: 0; order: 3; }
  header .who input { width: 100%; }
  header nav { margin-left: auto; }

  .row2, .row3 { grid-template-columns: 1fr; }
  .card { padding: 12px; }

  /* Every bar button reachable with a thumb, wrapping instead of overflowing. */
  .bar { gap: 6px; }
  .bar button { flex: 1 1 auto; min-width: 84px; }
  .bar #back { flex: 0 0 auto; }

  .hide-sm { display: none; }

  /* The rows table becomes a stack of cards: a 7-column grid at 360px is
     unusable, and marks are what people check on a phone. */
  table.results, table.results tbody, table.results tr, table.results td {
    display: block;
    width: 100%;
  }
  table.results thead { display: none; }
  table.results { border: 0; background: none; }
  table.results tr {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    margin-bottom: 8px;
    padding: 8px;
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 6px 8px;
    align-items: center;
  }
  table.results tr { grid-template-columns: 1fr 1fr; }
  table.results td { border: 0; padding: 0; }
  /* The row number becomes the reg cell's label, so the reg input gets the
     full card width — these numbers run to 16 characters. */
  table.results td:nth-child(1) { display: none; }
  table.results td:nth-child(2) { grid-column: 1 / -1; }
  /* CA / exam / total / grade pair up, each labelled. */
  table.results td:nth-child(n+2):nth-child(-n+6) {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }
  table.results td:nth-child(n+2):nth-child(-n+6)::before {
    content: attr(data-label);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--muted);
  }
  table.results input, table.results input.grade, table.results input.reg {
    width: 100%;
  }
  table.results td:last-child { grid-column: 1 / -1; }
  table.results td:last-child button { width: 100%; }
  table.results tr.selected td:first-child { box-shadow: none; }
  table.results tr.selected { outline: 2px solid var(--accent); }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b; --panel: #1c2026; --line: #2e343c; --ink: #e6e9ee;
    --muted: #98a1ad; --flag-bg: #33240f; --flag: #f0a35e;
  }
  table.list tbody tr:hover { background: #232a33; }
  .banner { background: #3a1c1a; border-color: #6b2a25; color: #f5a19a; }
  .pill { background: #262c34; }
  .pill.approved { background: #16301f; }
  .pill.processing { background: #1a2740; }
  .pill.failed, .pill.rejected { background: #3a1c1a; color: #f5a19a; }
  .pages button.page.approved { background: #16301f; }
  .pages button.page.failed, .pages button.page.rejected { background: #3a1c1a; }
}
