/* Layout: three columns — inputs | the paper on a desk | results. Stacks on phones. */
:root {
  --desk: #e7e9ee;      /* the desk behind the paper */
  --panel: #ffffff;
  --ink: #1d2433;       /* blue-black ink */
  --muted: #5d6578;
  --line: #d5d9e2;
  --red: #c8102e;       /* the red pen */
  --red-soft: #fbe7ea;
  --ok: #1d7a46;
  --ok-soft: #e3f3ea;
  --warn: #9a6200;
  --warn-soft: #fff2d6;
  --focus: #2f6fde;
  --f-ui: 'IBM Plex Sans', 'IBM Plex Sans Arabic', system-ui, sans-serif;
  --f-head: 'Space Grotesk', 'IBM Plex Sans Arabic', system-ui, sans-serif;
  --f-hand: 'Caveat', 'Aref Ruqaa', 'Segoe Print', cursive;
  --f-mono: 'IBM Plex Mono', ui-monospace, monospace;
  --r: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root { --desk:#151922; --panel:#1e2430; --ink:#e8ebf2; --muted:#9aa3b5; --line:#323a4a; --red:#ff5d6e; --red-soft:#3a1d23; --ok:#5fd394; --ok-soft:#173226; --warn:#f2b84b; --warn-soft:#3a2e14; --focus:#7aa7ff; color-scheme: dark; }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--desk); color: var(--ink); font: 14px/1.45 var(--f-ui); min-height: 100vh; }
[hidden] { display: none !important; }
[dir="rtl"] body { font-family: 'IBM Plex Sans Arabic', var(--f-ui); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* Header */
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px max(16px, env(safe-area-inset-right, 0px)) 10px max(16px, env(safe-area-inset-left, 0px));
  padding-top: max(10px, env(safe-area-inset-top, 0px));
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 38px; height: 38px; border-radius: 50%; border: 2.5px solid var(--red); color: var(--red);
  display: grid; place-items: center; font: 700 26px/1 var(--f-hand); transform: rotate(-8deg); }
.brand h1 { margin: 0; font: 700 20px/1 var(--f-head); letter-spacing: -.01em; }
.brand h1 span { color: var(--red); font-family: var(--f-hand); font-size: 24px; font-weight: 700; }
.byline { margin: 2px 0 0; font-size: 11px; color: var(--muted); letter-spacing: .04em; }
.top-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

/* Layout */
.layout { display: grid; grid-template-columns: minmax(280px, 340px) minmax(0, 1fr) minmax(280px, 340px); gap: 14px;
  padding: 14px max(16px, env(safe-area-inset-right, 0px)) 24px max(16px, env(safe-area-inset-left, 0px)); align-items: start; }
.col { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.col-left, .col-right { position: sticky; top: 76px; max-height: calc(100vh - 90px); overflow: auto; padding-bottom: 4px; }
@media (max-width: 1180px) {
  .layout { grid-template-columns: minmax(270px, 320px) minmax(0, 1fr); }
  .col-right { grid-column: 1 / -1; position: static; max-height: none; display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .col-left { position: static; max-height: none; }
}

/* Panels */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.panel-head { display: flex; align-items: center; gap: 8px; }
.panel-head h2 { margin: 0; font: 600 15px/1.2 var(--f-head); flex: 1; }
.step { width: 22px; height: 22px; border-radius: 50%; background: var(--ink); color: var(--panel); display: grid; place-items: center; font: 600 12px/1 var(--f-mono); }
.chip { font: 500 12px/1 var(--f-mono); padding: 4px 8px; border-radius: 99px; background: var(--desk); color: var(--muted); }
.chip:empty { display: none; }

.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.end { justify-content: flex-end; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Buttons */
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 8px 12px; font-weight: 500; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--ink); }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 5px 9px; font-size: 12px; }
.btn.danger { background: var(--red); color: #fff; border-color: var(--red); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.link { background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; padding: 0; align-self: flex-start; font-size: 12px; }

.seg { display: flex; background: var(--desk); border-radius: 8px; padding: 3px; gap: 3px; }
.seg-btn { flex: 1; border: 0; background: transparent; border-radius: 6px; padding: 6px; cursor: pointer; font-size: 13px; color: var(--muted); }
.seg-btn.on { background: var(--panel); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

.drop { border: 1.5px dashed var(--line); border-radius: 8px; padding: 14px; text-align: center; display: flex; flex-direction: column; gap: 2px; cursor: pointer; transition: border-color .15s, background .15s; }
.drop:hover, .drop.over { border-color: var(--red); background: var(--red-soft); }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop span { font-size: 12px; color: var(--muted); }

.check { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; cursor: pointer; }
textarea, input[type=text], input[type=password], input[type=number], select { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; background: var(--panel); }
textarea { resize: vertical; }

.thumbs { display: flex; flex-wrap: wrap; gap: 6px; }
.thumbs img { width: 52px; height: 70px; object-fit: cover; border-radius: 4px; border: 1px solid var(--line); }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: start; font: 600 11px/1.2 var(--f-ui); color: var(--muted); text-transform: uppercase; letter-spacing: .05em; padding: 6px 4px; border-bottom: 1px solid var(--line); }
td { padding: 5px 4px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.ktable input { padding: 4px 6px; border-radius: 6px; }
.ktable td:nth-child(1) input { width: 46px; }
.ktable td:nth-child(4) input { width: 52px; }
.ktable .x { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; }
.qtable td:first-child { font: 500 12px var(--f-mono); }
.qtable .wrote { font-family: var(--f-hand); font-size: 18px; line-height: 1.1; }
.qtable .key { display: block; font-size: 11px; color: var(--muted); }
.qtable select { padding: 3px 4px; font-size: 12px; width: auto; }
.qtable input { width: 52px; padding: 3px 5px; font-variant-numeric: tabular-nums; }
.qtable tr.flag td { background: var(--warn-soft); }
.qtable tr.sel td { outline: 1px solid var(--focus); }
.v-correct { color: var(--ok); }
.v-incorrect, .v-blank { color: var(--red); }
.v-partial, .v-unclear { color: var(--warn); }

/* Students list */
.stu-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.stu { display: grid; grid-template-columns: auto 1fr auto auto; gap: 8px; align-items: center; padding: 7px 8px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.stu.cur { border-color: var(--ink); box-shadow: inset 3px 0 0 var(--red); }
[dir="rtl"] .stu.cur { box-shadow: inset -3px 0 0 var(--red); }
.stu img { width: 30px; height: 40px; object-fit: cover; border-radius: 3px; border: 1px solid var(--line); }
.stu .nm { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.stu .pg { font-size: 11px; color: var(--muted); }
.stu .x { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; }
.pill { font: 500 11px/1 var(--f-ui); padding: 4px 8px; border-radius: 99px; white-space: nowrap; }
.pill-wait { background: var(--desk); color: var(--muted); }
.pill-ok { background: var(--ok-soft); color: var(--ok); }
.pill-bad { background: var(--red-soft); color: var(--red); }
.pill-busy { background: var(--warn-soft); color: var(--warn); }
.status { margin: 0; font-size: 12.5px; min-height: 1.2em; }
.status.err { color: var(--red); }
.status.busy { color: var(--warn); }
.status.ok { color: var(--ok); }

/* Toolbar + paper */
.toolbar { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 6px; position: sticky; top: 66px; z-index: 5; }
.tools { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.tool { width: 36px; height: 36px; border: 1px solid transparent; background: transparent; border-radius: 8px; display: grid; place-items: center; cursor: pointer; }
.tool svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tool:hover { background: var(--desk); }
.tool.on { background: var(--red-soft); color: var(--red); border-color: var(--red); }
.inspector { display: flex; gap: 12px; flex-wrap: wrap; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 8px 10px; }
.inspector label { display: flex; gap: 6px; align-items: center; font-size: 12px; color: var(--muted); flex: 1; min-width: 160px; }
.inspector input[type=text] { font-family: var(--f-hand); font-size: 18px; color: var(--red); }

.paper { display: flex; flex-direction: column; gap: 18px; align-items: center; }
.page { position: relative; width: 100%; max-width: 900px; }
.page canvas { display: block; width: 100%; height: auto; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.08), 0 8px 24px rgba(20,25,40,.12); border-radius: 2px; touch-action: none; }
.page .pmeta { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); padding: 0 2px 4px; }
.page.tool-add canvas { cursor: crosshair; }
.page.tool-move canvas { cursor: default; }
.empty { width: 100%; max-width: 900px; aspect-ratio: 1 / 1.3; max-height: 70vh; background: var(--panel); border: 1px dashed var(--line); border-radius: 4px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 24px; gap: 4px; }
.hand { font: 700 30px/1.1 var(--f-hand); color: var(--red); margin: 0; }
.busy-veil { position: absolute; inset: 22px 0 0 0; background: rgba(255,255,255,.55); display: grid; place-items: center; font: 700 26px var(--f-hand); color: var(--red); }

/* Score */
.score { display: flex; align-items: center; gap: 14px; }
.score-big { font: 700 46px/1 var(--f-hand); color: var(--red); min-width: 92px; height: 74px; padding: 0 10px; border: 2.5px solid var(--red); border-radius: 50% 46% 52% 48% / 54% 50% 50% 46%; display: grid; place-items: center; transform: rotate(-4deg); font-variant-numeric: tabular-nums; }
.score-side { display: flex; flex-direction: column; gap: 2px; }
.score-pct { font: 600 22px/1 var(--f-head); }
.name-field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.note { margin: 0; font-size: 12px; background: var(--warn-soft); color: var(--warn); padding: 7px 9px; border-radius: 8px; }

/* Dialogs */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--panel); color: var(--ink); width: min(460px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(15, 18, 28, .45); }
.dlg { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
#classDlg { width: min(760px, calc(100vw - 32px)); }
.dlg h2 { margin: 0; font: 600 18px var(--f-head); }
.dlg h3 { margin: 6px 0 0; font: 600 14px var(--f-head); }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.field small { font-size: 11.5px; }
.items { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 6px; }
.item { border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; font-size: 12px; display: flex; flex-direction: column; gap: 4px; }
.bar { height: 6px; background: var(--desk); border-radius: 3px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--ok); }
.item.low .bar i { background: var(--red); }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
