:root {
  color-scheme: dark;
  --bg: #0b1220;
  --bg-accent: #152238;
  --ink: #e8eef8;
  --muted: #9aabbf;
  --line: #243552;
  --brand: #5b9dff;
  --brand-dark: #3d7fe0;
  --brand-ink: #061018;
  --good: #4ade80;
  --good-bg: rgba(34, 197, 94, 0.14);
  --bad: #f87171;
  --bad-bg: rgba(248, 113, 113, 0.14);
  --warn: #fbbf24;
  --card: #111b2e;
  --option: #0e1728;
  --option-hover: #18263d;
  --option-selected: rgba(91, 157, 255, 0.16);
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  --radius: 14px;
  --font: "IBM Plex Sans", "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --touch: 44px;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  min-height: 100dvh;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(900px 420px at 8% -8%, rgba(91, 157, 255, 0.16) 0%, transparent 55%),
    radial-gradient(700px 360px at 100% 0%, rgba(37, 99, 180, 0.18) 0%, transparent 50%),
    var(--bg);
}

body {
  padding-bottom: calc(16px + var(--safe-bottom));
}

a {
  color: var(--brand);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.shell {
  width: 100%;
  max-width: 880px;
  margin: 0 auto;
  padding: calc(16px + var(--safe-top)) 16px 72px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.topbar-meta {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

#user-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 10rem;
}

.explain-preload-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin: 0;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1.3;
  cursor: pointer;
  white-space: nowrap;
  min-height: 28px;
}

.explain-preload-chip::after {
  content: "▾";
  font-size: 0.65rem;
  opacity: 0.7;
}

.explain-preload-chip.is-open::after {
  content: "▴";
}

.explain-preload-chip.is-busy {
  color: #f0c674;
  border-color: rgba(240, 198, 116, 0.45);
}

.explain-preload-chip.is-ok {
  color: #7dcea0;
  border-color: rgba(125, 206, 160, 0.45);
}

.explain-preload-chip.is-error,
.explain-preload-chip.is-warn {
  color: #f1948a;
  border-color: rgba(241, 148, 138, 0.45);
}

.explain-preload-chip:hover {
  border-color: var(--brand);
  color: var(--ink);
}

.brand {
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
  color: var(--ink);
  white-space: nowrap;
}

.nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
  color: var(--muted);
  font-size: 0.9rem;
  margin-left: auto;
}

.nav a,
.nav button {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
}

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

h1 {
  margin: 0 0 8px;
  font-size: clamp(1.6rem, 6vw, 2.4rem);
  letter-spacing: -0.03em;
  line-height: 1.15;
}

.lead {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.55;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

button,
.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 12px;
  min-height: var(--touch);
  padding: 12px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--brand);
  color: var(--brand-ink);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button:hover,
.btn:hover {
  background: var(--brand-dark);
  color: #fff;
  text-decoration: none;
}

button.secondary,
.btn.secondary {
  background: transparent;
  color: var(--ink);
  border-color: var(--line);
}

button.secondary:hover,
.btn.secondary:hover {
  background: var(--bg-accent);
  color: var(--ink);
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.stats {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 16px;
}

.stat {
  background: var(--bg-accent);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  min-width: 0;
}

.stat .label {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stat .value {
  font-family: var(--mono);
  font-size: 1.05rem;
  font-weight: 600;
}

.explain-preload {
  margin: 0 0 16px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(11, 18, 32, 0.55);
}

.explain-preload[hidden] {
  display: none !important;
}

.explain-preload-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.explain-preload-title {
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--muted);
  text-transform: uppercase;
}

.explain-preload-state {
  font-family: var(--mono);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
}

.explain-preload-state.is-busy {
  color: #f0c674;
  border-color: rgba(240, 198, 116, 0.45);
}

.explain-preload-state.is-ok {
  color: #7dcea0;
  border-color: rgba(125, 206, 160, 0.45);
}

.explain-preload-state.is-error,
.explain-preload-state.is-warn {
  color: #f1948a;
  border-color: rgba(241, 148, 138, 0.45);
}

.explain-preload-bar {
  height: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  overflow: hidden;
  margin-bottom: 8px;
}

.explain-preload-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #3d7eff, #7db0ff);
  transition: width 0.35s ease;
}

.explain-preload-summary {
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
  margin-bottom: 8px;
  word-break: break-word;
}

.explain-preload-log {
  margin: 0;
  max-height: 9.5rem;
  overflow: auto;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(255, 255, 255, 0.05);
  font-family: var(--mono);
  font-size: 0.68rem;
  line-height: 1.45;
  color: #c5d0e0;
  white-space: pre-wrap;
  word-break: break-word;
}

.meta {
  color: var(--muted);
  font-size: 0.88rem;
  margin-bottom: 10px;
  line-height: 1.4;
}

#question-stem {
  font-size: clamp(1.05rem, 3.6vw, 1.2rem);
  line-height: 1.55;
  margin: 0 0 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.options {
  display: grid;
  gap: 10px;
  margin-bottom: 16px;
}

.option {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  min-height: var(--touch);
  cursor: pointer;
  background: var(--option);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.option:hover {
  border-color: #3a557a;
  background: var(--option-hover);
}

.option.selected {
  border-color: var(--brand);
  background: var(--option-selected);
}

.option.mark-correct,
.option.mark-correct.selected {
  border-color: var(--good);
  background: var(--good-bg);
}

.option.mark-correct .key {
  color: var(--good);
}

.option.mark-wrong,
.option.mark-wrong.selected {
  border-color: var(--bad);
  background: var(--bad-bg);
}

.option.mark-wrong .key {
  color: var(--bad);
}

.options.revealed .option {
  cursor: default;
  pointer-events: none;
}

.options.revealed .option:hover {
  border-color: var(--line);
  background: var(--option);
}

.options.revealed .option.selected:hover {
  border-color: var(--brand);
  background: var(--option-selected);
}

.options.revealed .option.mark-correct:hover,
.options.revealed .option.mark-correct.selected:hover {
  border-color: var(--good);
  background: var(--good-bg);
}

.options.revealed .option.mark-wrong:hover,
.options.revealed .option.mark-wrong.selected:hover {
  border-color: var(--bad);
  background: var(--bad-bg);
}

.option input {
  margin-top: 4px;
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  accent-color: var(--brand);
}

.option .key {
  font-family: var(--mono);
  font-weight: 700;
  min-width: 1.2rem;
  color: var(--brand);
}

.option .text {
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.result {
  margin: 12px 0;
  padding: 12px 14px;
  border-radius: 12px;
  display: none;
  line-height: 1.45;
}

.result.show {
  display: block;
}

.result.correct {
  background: var(--good-bg);
  color: var(--good);
  border: 1px solid rgba(74, 222, 128, 0.25);
}

.result.incorrect {
  background: var(--bad-bg);
  color: var(--bad);
  border: 1px solid rgba(248, 113, 113, 0.25);
}

.explain-box {
  margin-top: 12px;
  padding: 14px;
  border-radius: 12px;
  background: var(--bg-accent);
  border: 1px dashed var(--line);
  color: var(--ink);
  white-space: pre-wrap;
  line-height: 1.5;
  display: none;
  overflow-wrap: anywhere;
}

.explain-box.show {
  display: block;
}

.explain-box.markdown-body {
  white-space: normal;
}

.explain-box.markdown-body > :first-child {
  margin-top: 0;
}

.explain-box.markdown-body > :last-child {
  margin-bottom: 0;
}

.explain-box.markdown-body h1,
.explain-box.markdown-body h2,
.explain-box.markdown-body h3,
.explain-box.markdown-body h4 {
  margin: 1em 0 0.45em;
  line-height: 1.3;
  font-weight: 700;
  color: var(--ink);
}

.explain-box.markdown-body h1 {
  font-size: 1.2rem;
}

.explain-box.markdown-body h2 {
  font-size: 1.08rem;
}

.explain-box.markdown-body h3,
.explain-box.markdown-body h4 {
  font-size: 1rem;
}

.explain-box.markdown-body p {
  margin: 0.55em 0;
}

.explain-box.markdown-body ul,
.explain-box.markdown-body ol {
  margin: 0.45em 0 0.7em;
  padding-left: 1.3em;
}

.explain-box.markdown-body li {
  margin: 0.25em 0;
}

.explain-box.markdown-body hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 1.1em 0;
}

.explain-box.markdown-body code {
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-size: 0.9em;
  padding: 0.1em 0.35em;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.06);
}

.explain-box.markdown-body strong {
  font-weight: 700;
}

.explain-box .explain-meta {
  margin-top: 1em;
  color: var(--muted);
  font-size: 0.85rem;
}

.form {
  display: grid;
  gap: 14px;
  max-width: 420px;
  width: 100%;
}

label {
  display: grid;
  gap: 6px;
  font-weight: 600;
  font-size: 0.92rem;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 500;
  color: var(--muted);
}

.checkbox-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--brand);
}

input[type="text"],
input[type="password"] {
  border: 1px solid var(--line);
  border-radius: 12px;
  min-height: var(--touch);
  padding: 12px 14px;
  font: inherit;
  color: var(--ink);
  background: var(--option);
  width: 100%;
}

input[type="text"]:focus,
input[type="password"]:focus {
  outline: 2px solid rgba(91, 157, 255, 0.45);
  outline-offset: 1px;
  border-color: var(--brand);
}

.message {
  min-height: 1.4em;
  color: var(--bad);
  font-size: 0.92rem;
}

.mode-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 16px;
}

.mode-row button {
  width: 100%;
  min-height: 40px;
  padding: 10px 8px;
  font-size: 0.88rem;
}

.mode-row button.active {
  background: var(--brand);
  color: var(--brand-ink);
  border-color: transparent;
}

.review-list {
  display: grid;
  gap: 12px;
}

.review-item {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  background: var(--option);
}

.review-item h3 {
  margin: 0 0 8px;
  font-size: 1rem;
}

.review-item p {
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.empty {
  color: var(--muted);
  padding: 20px 0;
}

.toolbar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

.toolbar #submit-answer {
  grid-column: 1 / -1;
}

.toolbar #prev-btn,
.toolbar #next-btn {
  grid-column: auto;
}

.progress-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 12px;
}

.progress-filters button {
  min-height: 36px;
}

.progress-summary {
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 12px;
}

.progress-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.progress-row {
  display: grid;
  grid-template-columns: 3.2rem minmax(0, 1fr) auto minmax(7rem, 9rem);
  gap: 10px 12px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(11, 18, 32, 0.35);
  color: inherit;
  text-decoration: none;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.progress-row:hover {
  border-color: var(--brand);
  background: rgba(91, 157, 255, 0.08);
  text-decoration: none;
}

.progress-num {
  font-family: var(--mono);
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--muted);
}

.progress-stem {
  font-size: 0.9rem;
  line-height: 1.35;
  min-width: 0;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.progress-icons {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.status-icon,
.flag-icon {
  display: inline-flex;
  width: 1.35rem;
  height: 1.35rem;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}

.status-icon svg,
.flag-icon svg {
  width: 1rem;
  height: 1rem;
  display: block;
  fill: currentColor;
}

.status-icon.is-correct {
  color: var(--good);
}

.status-icon.is-wrong {
  color: var(--bad);
}

.status-icon.is-open {
  color: var(--muted);
}

.flag-icon.is-on {
  color: var(--warn);
}

.flag-icon.is-off {
  opacity: 0.35;
}

.progress-time {
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--muted);
  text-align: right;
  line-height: 1.3;
}

.mobile-nav {
  display: none;
}

@media (max-width: 720px) {
  .shell {
    padding: calc(12px + var(--safe-top)) 12px calc(88px + var(--safe-bottom));
  }

  .topbar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
    margin-bottom: 14px;
  }

  .topbar-meta {
    order: 2;
    flex: 1 1 auto;
    justify-content: flex-end;
  }

  .brand {
    order: 1;
    font-size: 1rem;
  }

  .nav {
    display: none;
  }

  .mobile-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 0;
    background: rgba(11, 18, 32, 0.94);
    border-top: 1px solid var(--line);
    backdrop-filter: blur(12px);
    padding: 6px 8px calc(6px + var(--safe-bottom));
  }

  .mobile-nav a,
  .mobile-nav button {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: 52px;
    border: 0;
    background: transparent;
    color: var(--muted);
    font: inherit;
    font-size: 0.72rem;
    font-weight: 600;
    text-decoration: none;
    border-radius: 10px;
  }

  .mobile-nav a.active,
  .mobile-nav a:hover,
  .mobile-nav button:hover {
    color: var(--brand);
    background: rgba(91, 157, 255, 0.1);
    text-decoration: none;
  }

  .panel {
    padding: 16px;
    border-radius: 16px;
  }

  .stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .stat:nth-child(4),
  .stat:nth-child(5) {
    grid-column: span 1;
  }

  .mode-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  .actions .btn,
  .actions button {
    width: 100%;
    justify-content: center;
    text-align: center;
  }

  .form {
    max-width: none;
  }

  .toolbar {
    position: sticky;
    bottom: calc(64px + var(--safe-bottom));
    z-index: 20;
    margin: 16px -4px 0;
    padding: 10px 4px 4px;
    background: linear-gradient(to top, var(--card) 70%, transparent);
  }

  .toolbar #prev-btn,
  .toolbar #next-btn {
    grid-column: auto;
  }

  .progress-row {
    grid-template-columns: 2.6rem minmax(0, 1fr) auto;
    grid-template-areas:
      "num stem icons"
      "time time time";
  }

  .progress-num {
    grid-area: num;
  }

  .progress-stem {
    grid-area: stem;
  }

  .progress-icons {
    grid-area: icons;
  }

  .progress-time {
    grid-area: time;
    text-align: left;
  }
}

@media (max-width: 420px) {
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .mode-row button {
    font-size: 0.82rem;
  }

  #question-stem {
    font-size: 1.02rem;
  }
}
