:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-soft: #f1f3f6;
  --border: #dde1e7;
  --text: #1c2330;
  --muted: #667085;
  --primary: #2f5bea;
  --primary-text: #ffffff;
  --pass: #157f3b;
  --pass-bg: #e3f6ea;
  --fail: #c0362c;
  --fail-bg: #fdecea;
  --warn: #9a6200;
  --warn-bg: #fff4dc;
  --python: #2b6cb0;
  --javascript: #a07800;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --radius: 10px;
  --mono: ui-monospace, "Cascadia Code", "Fira Code", Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1b1f27;
    --surface-soft: #232833;
    --border: #2f3542;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --primary: #5b82ff;
    --pass: #4ad07a;
    --pass-bg: #163222;
    --fail: #ff7b6e;
    --fail-bg: #3a1c1a;
    --warn: #f0b84a;
    --warn-bg: #3a2d12;
    --python: #6aa8ec;
    --javascript: #e5c04a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--primary); }
h1 { font-size: 1.6rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 0; }
h3 { font-size: 1rem; margin: 1.2rem 0 0.5rem; }
pre, code, textarea[spellcheck="false"] { font-family: var(--mono); font-size: 13.5px; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { text-align: center; padding: 4rem 1rem; }
.right { text-align: right; }
.spacer { flex: 1; }

/* ---------- layout ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.7rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); text-decoration: none; margin-right: auto; }
.topbar nav { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; justify-content: flex-end; }

.page { max-width: 1100px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.2rem;
}
.card.soft { background: var(--surface-soft); box-shadow: none; }

.row-between { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: 1rem; }

/* ---------- buttons & forms ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem 0.95rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-primary:hover { filter: brightness(1.08); border-color: var(--primary); }
.btn-danger { color: var(--fail); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-soft); border-color: transparent; }
.btn-small { padding: 0.3rem 0.65rem; font-size: 0.875rem; }

label { display: block; font-weight: 500; margin-bottom: 0.8rem; }
input, select, textarea {
  display: block;
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 400;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
label.checkbox { display: flex; align-items: center; gap: 0.5rem; }
label.checkbox input, .switch input { width: auto; margin: 0; }

.form-grid { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 0 1rem; }
.form-grid .span-2 { grid-column: span 1; }
.form-grid label.checkbox { grid-column: 1 / -1; }
.field { margin-bottom: 1.2rem; }
.field-label { font-weight: 500; margin-bottom: 0.35rem; }
.form-message { padding: 0.6rem 0.8rem; border-radius: 8px; margin: 0.8rem 0 0; }
.form-message.error { background: var(--fail-bg); color: var(--fail); }
.form-message.success { background: var(--pass-bg); color: var(--pass); }

.switch { display: inline-flex; align-items: center; gap: 0.45rem; margin: 0; font-weight: 400; cursor: pointer; }

/* ---------- auth ---------- */

.auth-wrap { display: grid; place-items: center; padding: 4rem 1rem; }
.auth-card { width: 100%; max-width: 400px; }
.auth-card .btn-primary { width: 100%; justify-content: center; padding: 0.65rem; }
.auth-links { display: flex; flex-direction: column; gap: 0.4rem; margin-top: 1rem; font-size: 0.9rem; }

/* ---------- badges ---------- */

.badge {
  display: inline-block;
  padding: 0.12rem 0.55rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  background: var(--surface-soft);
  border: 1px solid var(--border);
}
.badge-python { color: var(--python); }
.badge-javascript { color: var(--javascript); }

.status { font-size: 0.8rem; font-weight: 600; padding: 0.12rem 0.55rem; border-radius: 999px; }
.status-new { color: var(--muted); background: var(--surface-soft); }
.status-done { color: var(--pass); background: var(--pass-bg); }
.status-partial { color: var(--warn); background: var(--warn-bg); }

/* ---------- task list ---------- */

.task-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 1rem; }
.task-card { color: var(--text); text-decoration: none; display: flex; flex-direction: column; gap: 0.7rem; transition: border-color 0.15s; }
.task-card:hover { border-color: var(--primary); }
.task-card-top { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; }

/* ---------- task page ---------- */

.task-page {
  display: grid;
  grid-template-columns: minmax(280px, 2fr) 3fr;
  gap: 1.25rem;
  padding: 1.25rem;
  max-width: 1500px;
  margin: 0 auto;
}
.task-info { min-width: 0; }
.task-info h1 { margin: 0.6rem 0 0.4rem; }
.back { text-decoration: none; font-size: 0.9rem; }
.task-work { min-width: 0; display: flex; flex-direction: column; gap: 0.75rem; }

.prose { margin-top: 1rem; }
.prose pre { background: var(--surface-soft); padding: 0.75rem; border-radius: 8px; overflow-x: auto; }
.prose code { background: var(--surface-soft); padding: 0.1rem 0.3rem; border-radius: 4px; }
.prose pre code { padding: 0; background: none; }

.test-preview { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.6rem; }
.test-preview li { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 0.6rem 0.75rem; }
.test-preview pre, .kv pre { margin: 0.3rem 0 0; white-space: pre-wrap; word-break: break-word; }
.kv { margin-top: 0.35rem; }
.kv span { font-size: 0.78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.kv pre { background: var(--surface-soft); padding: 0.4rem 0.55rem; border-radius: 6px; }

.toolbar { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

.editor { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.editor .CodeMirror { height: 420px; font-family: var(--mono); font-size: 14px; }
.editor-short .CodeMirror { height: 200px; }
.cm-s-default.CodeMirror { background: var(--surface); color: var(--text); }
.cm-s-default .CodeMirror-gutters { background: var(--surface-soft); border-right-color: var(--border); }

.stdin-box summary { cursor: pointer; color: var(--muted); font-size: 0.9rem; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.8rem 1rem; }
.panel-title { font-weight: 600; margin-bottom: 0.4rem; }
.panel pre, pre.error { margin: 0; white-space: pre-wrap; word-break: break-word; max-height: 320px; overflow-y: auto; }
.error { color: var(--fail); }

.result-summary { font-weight: 600; padding: 0.55rem 0.75rem; border-radius: 8px; margin-bottom: 0.6rem; }
.result-summary.all-pass { background: var(--pass-bg); color: var(--pass); }
.result-summary.some-fail { background: var(--fail-bg); color: var(--fail); }
.result-summary.running { background: var(--surface-soft); color: var(--muted); }
.test-results { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.45rem; }
.test-results li { border-left: 3px solid var(--border); padding: 0.3rem 0 0.3rem 0.7rem; }
.test-results li.pass { border-left-color: var(--pass); }
.test-results li.fail { border-left-color: var(--fail); }
.test-results .test-name { font-weight: 500; }
.test-results li.pass .test-name { color: var(--pass); }
.test-results li.fail .test-name { color: var(--fail); }
.test-results pre.error { background: var(--fail-bg); padding: 0.5rem 0.6rem; border-radius: 6px; margin-top: 0.35rem; }

/* ---------- teacher ---------- */

.tabs { display: flex; gap: 0.25rem; border-bottom: 1px solid var(--border); margin-bottom: 1.5rem; }
.tab {
  background: none; border: none; border-bottom: 2px solid transparent;
  padding: 0.6rem 1rem; font: inherit; font-weight: 500; color: var(--muted); cursor: pointer; margin-bottom: -1px;
}
.tab.active { color: var(--text); border-bottom-color: var(--primary); }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 0.6rem 0.8rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table thead th { font-size: 0.8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; background: var(--surface-soft); }
.table tbody tr:last-child > * { border-bottom: none; }

.test-row { margin-bottom: 0.75rem; }
.test-row label { margin-bottom: 0.5rem; }
.test-row .two-col { margin-bottom: 0; }

.results-table th, .results-table td { text-align: center; white-space: nowrap; }
.results-table thead th { max-width: 140px; overflow: hidden; text-overflow: ellipsis; }
.results-table .student-name { text-align: left; font-weight: 500; position: sticky; left: 0; background: var(--surface); }
.cell { font-weight: 600; }
.cell[data-task] { cursor: pointer; }
.cell-pass { color: var(--pass); background: var(--pass-bg); }
.cell-partial { color: var(--warn); background: var(--warn-bg); }
.cell-none { color: var(--muted); }
.total { font-weight: 700; }

dialog {
  width: min(800px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 1rem 1.2rem;
}
dialog::backdrop { background: rgb(0 0 0 / 45%); }
dialog h3 { margin: 0; }
.code-view { background: var(--surface-soft); padding: 0.8rem; border-radius: 8px; max-height: 60vh; overflow: auto; margin: 0; }

.notice { max-width: 560px; margin: 3rem auto; }

/* ---------- toast ---------- */

.toast {
  position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 0.7rem 1.1rem; border-radius: 8px; z-index: 100;
  background: var(--text); color: var(--bg); box-shadow: 0 6px 20px rgb(0 0 0 / 20%);
  animation: toast-in 0.2s ease-out;
}
.toast-error { background: var(--fail); color: #fff; }
.toast-success { background: var(--pass); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------- small screens ---------- */

@media (max-width: 860px) {
  .task-page { grid-template-columns: 1fr; padding: 1rem; }
  .editor .CodeMirror { height: 320px; }
  .two-col, .form-grid { grid-template-columns: 1fr; }
  #user-email { display: none; }
}
@media (prefers-color-scheme: dark) {
  .toast-error, .toast-success { color: #10131a; }
}
