:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --code-bg: #fbfbfc;
  --text: #1c2230;
  --muted: #5d6677;
  --border: #dde1e7;
  --border-strong: #c3c9d3;
  --accent: #2b59c3;
  --accent-hover: #234aa4;
  --accent-ink: #ffffff;
  --accent-soft: #e9effc;
  --danger: #b42318;
  --danger-soft: #fdeceb;
  --warn: #7a5300;
  --warn-soft: #fff5dc;
  --ok: #1b7a46;
  --ok-soft: #e5f5ec;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "JetBrains Mono", "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --code-size: 13.5px;
  --topbar-h: 48px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1a1e26;
    --surface-2: #222733;
    --code-bg: #151920;
    --text: #e4e7ee;
    --muted: #9aa3b2;
    --border: #2d3340;
    --border-strong: #3c4454;
    --accent: #7ea2f7;
    --accent-hover: #9bb8fa;
    --accent-ink: #0d1424;
    --accent-soft: #1e2a45;
    --danger: #f2877d;
    --danger-soft: #3a1d1b;
    --warn: #f0c96a;
    --warn-soft: #33290f;
    --ok: #6fd39b;
    --ok-soft: #15301f;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1 { font-size: 1.45rem; line-height: 1.25; margin: 0 0 0.75rem; }
h2 { font-size: 1.1rem; margin: 1.5rem 0 0.5rem; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 1rem;
  height: var(--topbar-h); padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.progress { display: flex; align-items: center; gap: 0.6rem; margin-left: auto; font-size: 0.85rem; color: var(--muted); }
.progress .bar { width: 120px; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.progress .bar span { display: block; height: 100%; background: var(--accent); }
.timer { font-variant-numeric: tabular-nums; font-size: 0.85rem; color: var(--muted); white-space: nowrap; }
.preview-banner { background: var(--warn-soft); color: var(--warn); text-align: center; font-size: 0.85rem; padding: 4px; }

/* ---------- cards / generic ---------- */
.page { padding: 24px 16px 48px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 28px 32px; margin: 0 auto;
}
.card.narrow { max-width: 760px; }
.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }
.prose p, .prose ul, .prose ol { margin: 0.5em 0; }
.prose code { font-family: var(--mono); font-size: 0.9em; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.prose pre { background: var(--surface-2); padding: 10px 12px; border-radius: 6px; overflow-x: auto; }
.callout {
  border: 1px solid var(--border); background: var(--accent-soft); border-radius: 8px;
  padding: 12px 16px; margin: 1rem 0;
}
.callout.warn { background: var(--warn-soft); border-color: transparent; color: var(--text); }
.code-badge { font-family: var(--mono); font-size: 1.4rem; letter-spacing: 0.15em; font-weight: 600; margin: 0.25rem 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  font: inherit; font-weight: 600; padding: 9px 18px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); text-decoration: none;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-hover); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.icon-btn {
  font: 600 12px var(--font); min-width: 30px; height: 26px; padding: 0 6px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
}
.icon-btn:hover, .icon-btn[aria-pressed="true"] { color: var(--text); border-color: var(--border-strong); }

/* ---------- forms ---------- */
.field { display: block; margin: 1.25rem 0; }
.field-label { display: block; font-weight: 600; margin-bottom: 0.35rem; }
.field-help { display: block; font-size: 0.85rem; color: var(--muted); margin-top: 0.35rem; }
input[type="text"], input[type="email"], input[type="password"], textarea, select {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 8px; padding: 8px 10px;
}
textarea { resize: vertical; min-height: 4.5em; }
input:focus-visible, textarea:focus-visible, select:focus-visible, .btn:focus-visible, .icon-btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.error { color: var(--danger); font-size: 0.875rem; margin: 0.35rem 0 0; }
.error.banner { background: var(--danger-soft); padding: 8px 12px; border-radius: 8px; }

.q { border: 0; margin: 0 0 1.5rem; padding: 0; min-width: 0; }
.q-prompt { display: flex; gap: 0.6rem; align-items: baseline; font-weight: 500; margin-bottom: 0.6rem; }
.q-prompt .prose { flex: 1; }
.q-num { flex: none; font-size: 0.8rem; font-weight: 700; color: var(--accent); background: var(--accent-soft); border-radius: 5px; padding: 1px 6px; }
.optional { flex: none; font-size: 0.75rem; color: var(--muted); border: 1px solid var(--border); border-radius: 999px; padding: 0 8px; }
.q-help { font-size: 0.875rem; color: var(--muted); margin: -0.3rem 0 0.6rem; }
.q.has-error .q-prompt { color: var(--danger); }
.q-info { margin-bottom: 1.25rem; }

.options { display: grid; gap: 4px; }
.opt { display: flex; align-items: flex-start; gap: 0.55rem; padding: 6px 10px; border-radius: 8px; cursor: pointer; }
.opt:hover { background: var(--surface-2); }
.opt input[type="radio"], .opt input[type="checkbox"] { margin-top: 0.3em; accent-color: var(--accent); flex: none; }
.opt-other { align-items: center; flex-wrap: wrap; }
.opt-other input[type="text"] { flex: 1; min-width: 160px; width: auto; padding: 4px 8px; }

.likert { display: grid; grid-template-columns: repeat(var(--points), minmax(0, 1fr)); gap: 4px; max-width: 560px; }
.lk {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px; text-align: center; cursor: pointer;
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 2px; font-size: 0.8rem; color: var(--muted);
}
.lk:hover { border-color: var(--border-strong); }
.lk input { position: absolute; opacity: 0; pointer-events: none; }
.lk-num { font-weight: 700; font-size: 0.95rem; color: var(--text); }
.lk-label { line-height: 1.2; }
.lk:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }
.lk:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 1px; }

.submit-row { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; padding-top: 0.5rem; border-top: 1px solid var(--border); margin-top: 1rem; }
.submit-row .submit-note { flex-basis: 100%; margin: 0.5rem 0 0; font-size: 0.85rem; color: var(--muted); }

.consent-text {
  max-height: 260px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px;
  padding: 12px 16px; background: var(--surface-2); font-size: 0.92rem; margin-bottom: 0.75rem;
}
.consent.has-error .opt { color: var(--danger); }

/* ---------- code ---------- */
.code-pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--code-bg); }
.pane-head {
  display: flex; align-items: center; gap: 0.5rem; justify-content: space-between;
  padding: 6px 10px; border-bottom: 1px solid var(--border); background: var(--surface);
  font-size: 0.85rem; font-weight: 600;
}
.pane-tools { display: flex; gap: 4px; }
.codeview { flex: 1; overflow: auto; min-height: 0; }
.codeview .hl { font-family: var(--mono); font-size: var(--code-size); line-height: 1.5; }
.codeview table.hltable { border-collapse: collapse; width: 100%; }
.codeview td { padding: 0; vertical-align: top; }
.codeview td.linenos {
  width: 1%; text-align: right; color: var(--muted); opacity: 0.7; padding: 10px 10px 10px 12px;
  border-right: 1px solid var(--border); user-select: none; -webkit-user-select: none;
}
.codeview td.code { padding: 10px 14px; }
.codeview pre { margin: 0; font: inherit; }
.codeview.wrap td.code pre { white-space: pre-wrap; overflow-wrap: anywhere; }
.codeview.wrap td.linenos { display: none; }
.protected, .protected * { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }

/* task page: code left, questions right */
body.workspace { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
body.workspace .page { flex: 1; min-height: 0; padding: 0; }
.split { height: 100%; display: grid; grid-template-columns: var(--left, 58%) 6px minmax(0, 1fr); }
.split .q-pane { overflow-y: auto; padding: 24px 28px 40px; background: var(--surface); }
.divider { cursor: col-resize; background: var(--border); transition: background 0.15s; }
.divider:hover, .divider.dragging { background: var(--accent); }

/* perception page */
body.compare .page { max-width: 1600px; margin: 0 auto; }
.compare-head { margin-bottom: 12px; }
.columns { display: grid; grid-template-columns: repeat(var(--cols), minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.columns .code-pane { height: 62vh; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.compare-form { max-width: 1000px; }
.q-grid .grid-row { display: grid; grid-template-columns: 110px minmax(0, 1fr); gap: 12px; align-items: center; padding: 4px 0; }
.grid-label { font-weight: 600; font-size: 0.9rem; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: 0.9rem;
  transition: opacity 0.2s, transform 0.2s; z-index: 100; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media print { .codeview { display: none !important; } }

@media (max-width: 900px) {
  body.workspace { height: auto; overflow: visible; }
  .split { display: block; }
  .split .code-pane { height: 60vh; }
  .divider { display: none; }
  .columns { grid-template-columns: minmax(0, 1fr); }
  .columns .code-pane { height: 50vh; }
  .card { padding: 20px 16px; }
  .q-grid .grid-row { grid-template-columns: minmax(0, 1fr); gap: 4px; }
  .progress .bar { width: 60px; }
  .brand { max-width: 40vw; }
}

/* ---------- admin ---------- */
body.admin .page { max-width: 1400px; margin: 0 auto; }
.admin-nav { display: flex; gap: 2px; margin-left: 1rem; flex-wrap: wrap; }
.admin-nav a { text-decoration: none; color: var(--muted); padding: 6px 10px; border-radius: 6px; font-size: 0.9rem; }
.admin-nav a:hover, .admin-nav a.active { color: var(--text); background: var(--surface-2); }
.admin-user { margin-left: auto; display: flex; gap: 0.75rem; align-items: center; font-size: 0.85rem; color: var(--muted); }
.admin-user form { margin: 0; }
.linkish { background: none; border: 0; color: var(--accent); cursor: pointer; font: inherit; padding: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat .v { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .k { font-size: 0.8rem; color: var(--muted); }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 20px; }
.panel h2 { margin-top: 0; }
.table-wrap { overflow-x: auto; }
table.data { border-collapse: collapse; width: 100%; font-size: 0.875rem; }
table.data th, table.data td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.data th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); font-weight: 600; white-space: nowrap; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tr:hover td { background: var(--surface-2); }
.pill { display: inline-block; font-size: 0.75rem; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill.completed { background: var(--ok-soft); color: var(--ok); }
.pill.active { background: var(--accent-soft); color: var(--accent); }
.pill.excluded { background: var(--danger-soft); color: var(--danger); }
.flash { background: var(--ok-soft); color: var(--ok); border-radius: 8px; padding: 8px 12px; margin-bottom: 16px; }
.errors-box { background: var(--danger-soft); color: var(--danger); border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; }
.errors-box ul { margin: 0.25rem 0 0; padding-left: 1.25rem; }
.answer-text { white-space: pre-wrap; background: var(--surface-2); border-radius: 8px; padding: 10px 12px; margin: 0.25rem 0; }
.grade-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; background: var(--surface); }
.grade-card.done { border-left: 4px solid var(--ok); }
.grade-card.mixed { border-left: 4px solid var(--warn); }
.grade-meta { display: flex; gap: 0.75rem; flex-wrap: wrap; font-size: 0.8rem; color: var(--muted); }
.score-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.score-row label { position: relative; display: inline-flex; align-items: center; justify-content: center; min-width: 38px; height: 32px; border: 1px solid var(--border-strong); border-radius: 6px; cursor: pointer; font-weight: 600; }
.score-row label:has(input:checked) { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.score-row input[type="radio"] { position: absolute; opacity: 0; pointer-events: none; }
.score-row input[type="text"] { flex: 1; min-width: 200px; padding: 5px 8px; }
.sticky-save { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0; display: flex; gap: 1rem; align-items: center; }
.two-col { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.reference { font-size: 0.9rem; }
details > summary { cursor: pointer; font-weight: 600; }
dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 0; font-size: 0.9rem; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; overflow-wrap: anywhere; }
.inline-form { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.inline-form input[type="text"] { width: auto; }
@media (max-width: 900px) { .two-col { grid-template-columns: minmax(0, 1fr); } }
