:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f2f3f5;
  --border: #e3e5e8;
  --text: #1c1f23;
  --muted: #6b7280;
  --accent: #1f7a4d;
  --accent-hover: #186640;
  --good: #1f8a50;
  --bad: #d23b3b;
  --easy: #1f8a50;
  --medium: #c27c0e;
  --hard: #d23b3b;
  --code-bg: #f6f7f9;
  --cm-keyword: #8b3fc6;
  --cm-string: #1d7c3c;
  --cm-number: #1a5fb4;
  --cm-comment: #8a8f98;
  --cm-def: #1a5fb4;
  --cm-builtin: #b45309;
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1a;
    --surface: #262626;
    --surface-2: #2f2f2f;
    --border: #3a3a3a;
    --text: #e8e8e8;
    --muted: #9ca3af;
    --accent: #2c9c64;
    --accent-hover: #34b373;
    --good: #3cc47a;
    --bad: #f06464;
    --easy: #3cc47a;
    --medium: #f0b232;
    --hard: #f06464;
    --code-bg: #1f1f1f;
    --cm-keyword: #c792ea;
    --cm-string: #a5d6a7;
    --cm-number: #82aaff;
    --cm-comment: #7f8690;
    --cm-def: #82aaff;
    --cm-builtin: #ffcb6b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }
.good { color: var(--good); }
.bad { color: var(--bad); }

/* ---------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  height: 48px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex: none;
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); font-weight: 600; }
.brand:hover { text-decoration: none; }
.toolbar { flex: 1; display: flex; align-items: center; gap: 12px; min-width: 0; }
.back { color: var(--muted); }
.engine { font-size: 12px; white-space: nowrap; }

.actions { display: flex; gap: 8px; }
button.run, button.submit {
  border: 0;
  border-radius: 6px;
  padding: 6px 18px;
  font-weight: 600;
}
button.run { background: var(--surface-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--border); }
button.run:hover { background: var(--border); }
button.submit { background: var(--accent); color: #fff; }
button.submit:hover { background: var(--accent-hover); }
button.run:disabled, button.submit:disabled { opacity: 0.6; cursor: progress; }
button.link { background: none; border: 0; padding: 0; color: var(--muted); font-size: 12px; }
button.link:hover { color: var(--text); text-decoration: underline; }
.icon { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-button {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  padding: 0;
}
.icon-button:hover { background: var(--border); color: var(--text); }
button.ghost {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  padding: 2px 10px;
  font-size: 12px;
}
button.ghost:hover { color: var(--text); }

main { flex: 1; min-height: 0; }

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

.list-view { width: 100%; max-width: 960px; margin: 0 auto; padding: 32px 16px 64px; overflow: auto; }
.list-head { display: flex; align-items: baseline; gap: 12px; }
.list-head h1 { margin: 0 0 16px; font-size: 22px; }
.list-view input[type="search"] {
  width: 100%;
  padding: 8px 12px;
  margin-bottom: 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
}
table.problems {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.problems th, .problems td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.problems th { font-weight: 500; color: var(--muted); font-size: 12px; background: var(--surface-2); }
.problems tbody tr { cursor: pointer; }
.problems tbody tr:hover { background: var(--surface-2); }
.problems tbody tr:last-child td { border-bottom: 0; }
.problems .status { width: 32px; text-align: center; }
.problems a { color: var(--text); font-weight: 500; }
.check { color: var(--good); font-weight: 700; }
.empty { text-align: center; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; }
.tag { background: var(--surface-2); border-radius: 999px; padding: 1px 8px; font-size: 12px; color: var(--muted); }
.difficulty { font-size: 12px; font-weight: 600; }
.difficulty.easy { color: var(--easy); }
.difficulty.medium { color: var(--medium); }
.difficulty.hard { color: var(--hard); }

.error-box {
  border: 1px solid var(--bad);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin: 16px;
  color: var(--bad);
  background: var(--surface);
}
.pad { padding: 16px; }

/* ---------------------------------------------------------------- problem */

.problem-view { display: flex; padding: 8px; }
.workspace {
  --split: 45%;
  --vsplit: 60%;
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, var(--split)) 8px minmax(0, 1fr);
}
.workspace.desc-collapsed { grid-template-columns: 40px 8px minmax(0, 1fr); }
.right { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.pane-code { flex: 0 1 var(--vsplit); }
.pane-bottom { flex: 1 1 0; }
.pane-head {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 38px;
  padding: 0 6px 0 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  flex: none;
  user-select: none;
}
.pane-title { font-weight: 600; font-size: 13px; display: flex; gap: 8px; align-items: baseline; }
.lang { font-size: 12px; font-weight: 500; color: var(--muted); }
.pane-body { flex: 1; min-height: 0; overflow: auto; }
.pane-strip { display: none; }

/* collapsed panes keep only their header (or a vertical strip on the left) */
.pane.collapsed > :not(.pane-head):not(.pane-strip) { display: none; }
.pane.collapsed { flex: none; }
.pane-desc.collapsed .pane-head { display: none; }
.pane-desc.collapsed .pane-strip {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  height: 100%;
  padding: 10px 0;
  border: 0;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 600;
  font-size: 13px;
}
.pane-desc.collapsed .pane-strip span { writing-mode: vertical-rl; }
.pane-desc.collapsed .pane-strip:hover { color: var(--text); }
.pane-desc.collapsed .pane-head .icon-button { display: none; }
.pane.collapsed .pane-head { border-bottom: 0; }
.workspace.code-collapsed .pane-bottom,
.workspace.bottom-collapsed .pane-code { flex: 1 1 0; }
.workspace.code-collapsed .divider-y,
.workspace.bottom-collapsed .divider-y,
.workspace.desc-collapsed .divider-x { pointer-events: none; }
.workspace.code-collapsed .divider-y::after,
.workspace.bottom-collapsed .divider-y::after,
.workspace.desc-collapsed .divider-x::after { display: none; }

/* maximized pane fills the workspace */
.workspace[data-max="desc"], .workspace[data-max="code"], .workspace[data-max="bottom"] { grid-template-columns: minmax(0, 1fr); }
.workspace[data-max="desc"] > :not(.pane-desc),
.workspace[data-max="code"] > :not(.right),
.workspace[data-max="bottom"] > :not(.right),
.workspace[data-max="code"] .right > :not(.pane-code),
.workspace[data-max="bottom"] .right > :not(.pane-bottom) { display: none; }
.workspace[data-max] .right > .pane { flex: 1 1 0; }

.divider { position: relative; flex: none; }
.divider-x { cursor: col-resize; }
.divider-y { cursor: row-resize; height: 8px; }
.divider::after { content: ""; position: absolute; border-radius: 2px; background: transparent; }
.divider-x::after { inset: 40% 3px; }
.divider-y::after { inset: 3px 40%; }
.divider:hover::after, .divider:focus-visible::after, .divider.dragging::after { background: var(--accent); }

/* description */
.description { padding: 16px 20px 32px; }
.problem-title { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.problem-title h1 { margin: 0; font-size: 20px; }
.solved-badge { color: var(--good); font-size: 12px; font-weight: 600; }
.level-tabs {
  display: flex;
  gap: 4px;
  margin: 14px 0 16px;
  padding: 3px;
  background: var(--surface-2);
  border-radius: 8px;
}
.level-tab {
  flex: 1 1 0;
  min-width: 0;
  border: 0;
  border-radius: 6px;
  padding: 5px 8px;
  background: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.level-tab:hover { color: var(--text); }
.level-tab.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / 0.12); }
.level-check { display: none; color: var(--good); margin-right: 4px; }
.level-tab.passed .level-check { display: inline; }
/* In a narrow description pane the tabs show just the level number. */
.pane-desc .pane-body { container-type: inline-size; }
@container (max-width: 300px) {
  .level-word, .level-tab.passed .level-check { display: none; }
  .level-tab { padding: 5px 2px; }
  .level-tab.passed { color: var(--good); }
}

.markdown h1, .markdown h2, .markdown h3 { line-height: 1.3; margin: 1.4em 0 0.5em; }
.markdown h2 { font-size: 16px; }
.markdown h3 { font-size: 14px; }
.markdown > :first-child { margin-top: 0; }
.markdown code, .example code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--surface-2);
  border-radius: 4px;
  padding: 1px 5px;
}
.markdown pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  overflow: auto;
}
.markdown pre code { background: none; padding: 0; }
.markdown table { border-collapse: collapse; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 4px 10px; }
.markdown blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }

.example h3 { font-size: 14px; margin: 20px 0 8px; }
.example-box { border-left: 2px solid var(--border); padding: 2px 0 2px 14px; }
.example-box .label { font-weight: 600; font-size: 13px; margin-top: 6px; }
.example-box pre {
  font-family: var(--mono);
  font-size: 12.5px;
  margin: 2px 0 4px;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--muted);
}
.example-box p { margin: 2px 0; color: var(--muted); }

/* editor */
.code-body { position: relative; overflow: hidden; }
.code-body .editor { position: absolute; inset: 0; }
.code-body .CodeMirror {
  height: 100%;
  font-family: var(--mono);
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
}
.CodeMirror-gutters { background: var(--surface); border-right: 1px solid var(--border); }
.CodeMirror-linenumber { color: var(--muted); }
.CodeMirror-cursor { border-left-color: var(--text); }
.CodeMirror-selected { background: color-mix(in srgb, var(--accent) 18%, transparent) !important; }
.CodeMirror-matchingbracket { color: var(--accent) !important; text-decoration: underline; }
.cm-s-default .cm-keyword { color: var(--cm-keyword); }
.cm-s-default .cm-string { color: var(--cm-string); }
.cm-s-default .cm-number { color: var(--cm-number); }
.cm-s-default .cm-comment { color: var(--cm-comment); font-style: italic; }
.cm-s-default .cm-def { color: var(--cm-def); }
.cm-s-default .cm-builtin { color: var(--cm-builtin); }
.cm-s-default .cm-variable, .cm-s-default .cm-property, .cm-s-default .cm-operator, .cm-s-default .cm-variable-2 { color: var(--text); }
.cm-s-default .cm-meta { color: var(--cm-builtin); }

/* testcase / result */
.tabs { display: flex; gap: 2px; height: 100%; }
.tab {
  border: 0;
  background: none;
  color: var(--muted);
  font-weight: 600;
  font-size: 13px;
  padding: 0 10px;
  border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--text); }
.tab-panel { padding: 12px 14px 16px; }
.hint { margin: 4px 0; }
.small { font-size: 12px; }

.case-chips { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 12px; }
.chip {
  display: inline-flex;
  align-items: center;
  border-radius: 6px;
  background: none;
}
.chip > button {
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  padding: 4px 10px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.chip:hover, .chip.active { background: var(--surface-2); }
.chip.active > button { color: var(--text); }
.chip .chip-remove { padding: 4px 6px 4px 0; visibility: hidden; }
.chip:hover .chip-remove { visibility: visible; }
.chip-remove .icon { width: 12px; height: 12px; }
.chip .dot { width: 6px; height: 6px; border-radius: 50%; }
.chip.pass .dot { background: var(--good); }
.chip.fail .dot { background: var(--bad); }

.field { display: block; margin-bottom: 12px; }
.field .label, .io .label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field textarea {
  display: block;
  width: 100%;
  resize: vertical;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font: 12.5px/1.5 var(--mono);
}
.field textarea:focus { border-color: var(--accent); outline: none; }

.verdict-line { display: flex; align-items: baseline; gap: 12px; margin-bottom: 12px; }
.verdict { font-size: 18px; font-weight: 700; }
.verdict.good { color: var(--good); }
.verdict.bad { color: var(--bad); }
.verdict-line .muted { font-size: 12px; }

.io { margin-bottom: 10px; }
.io pre {
  margin: 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface-2);
  font: 12.5px/1.5 var(--mono);
  white-space: pre-wrap;
  word-break: break-word;
}
.io.wrong pre { color: var(--bad); }
.io.error pre { color: var(--bad); border-left: 3px solid var(--bad); }
.io mark { background: color-mix(in srgb, var(--bad) 20%, transparent); color: inherit; border-radius: 3px; padding: 0 2px; }
.io.wrong mark { font-weight: 700; }
pre.trace {
  font: 12px/1.5 var(--mono);
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-left: 3px solid var(--bad);
  border-radius: 6px;
  padding: 8px 10px;
  margin: 0 0 10px;
  white-space: pre-wrap;
  word-break: break-word;
}
.level-rows { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 12px; }
.level-row { display: flex; align-items: center; gap: 8px; padding: 7px 12px; border-bottom: 1px solid var(--border); font-size: 13px; }
.level-row:last-child { border-bottom: 0; }
.level-row.pass .mark { color: var(--good); }
.level-row.fail .mark { color: var(--bad); }
.detail-title { font-size: 13px; margin: 0 0 8px; }
button.next { margin-bottom: 12px; }

@media (max-width: 800px) {
  .brand span { display: none; }
  .engine { display: none; }
  .problem-view { display: block; padding: 6px; overflow: auto; }
  .workspace, .workspace.desc-collapsed { display: flex; flex-direction: column; gap: 6px; }
  .right { gap: 6px; }
  .divider { display: none; }
  .pane, .workspace[data-max] .right > .pane { flex: none; }
  .pane-desc .pane-body { max-height: none; }
  .pane-desc.collapsed .pane-head { display: flex; }
  .pane-desc.collapsed .pane-strip { display: none; }
  .code-body { height: 55vh; flex: none; }
  .pane-bottom .pane-body { min-height: 200px; }
}
.toolbar > .spacer:last-child { text-align: right; }
.level-heading { font-size: 15px; margin: 0 0 10px; }

/* ---------------------------------------------------------------- nav */

.nav { display: flex; gap: 2px; }
.nav a {
  color: var(--muted);
  padding: 5px 10px;
  border-radius: 6px;
  font-weight: 500;
  white-space: nowrap;
}
.nav a:hover { color: var(--text); background: var(--surface-2); text-decoration: none; }
.nav a[aria-current] { color: var(--text); background: var(--surface-2); }

/* ---------------------------------------------------------------- system design list */

.lede { margin: -6px 0 16px; }
.question-section { margin-top: 24px; }
.question-section h2 { font-size: 15px; margin: 0 0 10px; }
.question-section .problems { table-layout: fixed; }
.question-section .problems td.status { width: 32px; }
.problems td.score { width: 48px; text-align: right; }
.problems td.diff { width: 80px; }
.question-section .problems td.tags { width: 38%; }
/* A flex <td> stops being a table cell; keep tag cells as cells. */
.problems td.tags { display: table-cell; }
.problems td.tags .tag { display: inline-block; margin: 2px 4px 2px 0; }
.notes {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 16px;
  margin: 12px 0 20px;
}
.notes > summary { cursor: pointer; font-weight: 600; }
.notes[open] > summary { margin-bottom: 8px; }

/* ---------------------------------------------------------------- question workspace */

.answer-body { display: flex; flex-direction: column; padding: 10px 12px 12px; gap: 6px; }
.step-tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.step-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: none;
  color: var(--muted);
  padding: 3px 10px 3px 4px;
  font-size: 12px;
  font-weight: 500;
}
.step-tab:hover { color: var(--text); }
.step-tab.active { color: var(--text); border-color: var(--text); }
.step-n {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--surface-2);
  font-size: 11px;
}
.step-tab.filled .step-n { background: var(--good); color: #fff; }
.step-hint { margin: 2px 2px 0; }
textarea.answer {
  flex: 1;
  min-height: 120px;
  resize: none;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: 13px/1.55 var(--mono);
}
.clock { font: 600 15px var(--mono); padding: 0 6px; min-width: 56px; text-align: right; align-self: center; }
.clock.over { color: var(--bad); }
.ref-step { border-top: 1px solid var(--border); padding: 8px 0; }
.ref-step > summary { cursor: pointer; font-weight: 600; }
.ref-step[open] > summary { margin-bottom: 6px; }
.rubric { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-direction: column; gap: 6px; }
.rubric label { display: flex; gap: 8px; align-items: baseline; cursor: pointer; }
.rubric input { flex: none; transform: translateY(2px); }
.mentioned {
  flex: none;
  font-size: 11px;
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
  border-radius: 999px;
  padding: 0 6px;
}

@media (max-width: 800px) {
  .topbar:has(.toolbar:not(:empty)) .nav { display: none; }
  .topbar { gap: 10px; padding: 0 10px; }
  .toolbar { gap: 6px; }
  .toolbar .actions { gap: 4px; }
  .toolbar button.run, .toolbar button.submit { padding: 6px 10px; }
  .back { white-space: nowrap; font-size: 13px; }
  .clock { min-width: 0; padding: 0 2px; }
  .wide-only { display: none; }
  .nav a { padding: 5px 6px; }
  .answer-body { min-height: 60vh; }
  .problems td.tags, .question-section .problems td.tags { display: none; }
}
