:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f2f7;
  --text: #1c1f2a;
  --muted: #5d6475;
  --border: #e2e5ee;
  --accent: #4f46e5;
  --accent-soft: #eceafe;
  --accent-text: #ffffff;
  --ok: #15803d;
  --ok-soft: #e7f6ec;
  --bad: #c62828;
  --bad-soft: #fdecec;
  --warn: #b45309;
  --warn-soft: #fdf3e2;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 6%);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141b;
    --surface: #1b1e28;
    --surface-2: #242836;
    --text: #e8eaf1;
    --muted: #9aa1b4;
    --border: #2e3344;
    --accent: #8b85ff;
    --accent-soft: #2a2852;
    --accent-text: #12141b;
    --ok: #4ade80;
    --ok-soft: #173224;
    --bad: #f87171;
    --bad-soft: #3a1d20;
    --warn: #fbbf24;
    --warn-soft: #3a2e14;
    --shadow: none;
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
}

a {
  color: var(--accent);
}

h1 {
  font-size: 1.6rem;
  margin: 0.5rem 0 1.25rem;
}

h2 {
  font-size: 1.1rem;
  margin: 1.75rem 0 0.75rem;
}

kbd {
  font: inherit;
  font-size: 0.72em;
  padding: 0.05em 0.4em;
  border-radius: 5px;
  border: 1px solid currentColor;
  opacity: 0.55;
  margin-left: 0.35em;
}

@media (hover: none) {
  kbd {
    display: none;
  }
}

.container {
  max-width: 820px;
  margin: 0 auto;
  padding: 1.25rem 1rem 4rem;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.875rem;
}

.error {
  color: var(--bad);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  box-shadow: var(--shadow);
}

.empty {
  text-align: center;
  padding: 2rem 1.25rem;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 1rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  font: inherit;
  font-weight: 600;
  padding: 0.6rem 1.1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}

.btn:hover {
  border-color: var(--accent);
}

.btn:active {
  transform: translateY(1px);
}

.btn:focus-visible,
.option:focus-visible,
.quiz-row:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid var(--accent-soft);
  outline-offset: 2px;
  border-color: var(--accent);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.btn.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}

.btn.ghost:hover {
  color: var(--text);
  border-color: var(--border);
}

.btn.small {
  padding: 0.35rem 0.75rem;
  font-size: 0.875rem;
}

.btn.disabled,
.btn:disabled {
  opacity: 0.45;
  cursor: default;
  pointer-events: none;
}

.btn.success-soft {
  background: var(--ok-soft);
  border-color: var(--ok-soft);
  color: var(--ok);
}

.btn.danger-soft {
  background: var(--bad-soft);
  border-color: var(--bad-soft);
  color: var(--bad);
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 2rem;
  height: 2rem;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
}

.icon-btn:hover {
  background: var(--surface-2);
  color: var(--text);
}

.badge {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}

.badge.new {
  background: var(--accent-soft);
  color: var(--accent);
}

.badge.due,
.badge.retry {
  background: var(--warn-soft);
  color: var(--warn);
}

.badge.done {
  background: var(--ok-soft);
  color: var(--ok);
}

.progress {
  height: 6px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.progress.big {
  height: 10px;
  margin: 1rem 0;
}

.progress-bar {
  height: 100%;
  background: var(--accent);
  border-radius: inherit;
  transition: width 0.3s ease;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 820px;
  margin: 0 auto;
  padding: 0.6rem 1rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}

.brand {
  font-weight: 800;
  color: var(--text);
  text-decoration: none;
  letter-spacing: -0.01em;
}

.nav {
  display: flex;
  gap: 0.25rem;
}

.nav-link {
  padding: 0.35rem 0.7rem;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
}

.nav-link:hover {
  background: var(--surface-2);
  color: var(--text);
}

.nav-link.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.count {
  display: inline-block;
  min-width: 1.4em;
  margin-left: 0.35em;
  padding: 0 0.4em;
  border-radius: 999px;
  background: var(--warn);
  color: var(--surface);
  font-size: 0.75rem;
  font-weight: 700;
  text-align: center;
}

.user {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.user-name {
  font-weight: 600;
}

.sync {
  font-size: 0.75rem;
  color: var(--warn);
}

@media (max-width: 600px) {
  .topbar-inner {
    flex-wrap: wrap;
    gap: 0.4rem 0.75rem;
  }
  .nav {
    order: 3;
    width: 100%;
  }
  .user-name {
    display: none;
  }
}

.login-wrap {
  max-width: 460px;
  margin: 3rem auto 0;
}

.login label {
  display: block;
  font-weight: 600;
  margin: 1rem 0 0.4rem;
}

.input-row {
  display: flex;
  gap: 0.5rem;
}

input,
select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.55rem 0.75rem;
  min-width: 0;
}

.input-row input {
  flex: 1;
}

.form-message:not(:empty) {
  margin-top: 1rem;
}

.recent {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin-top: 1rem;
}

.chip {
  font: inherit;
  font-size: 0.875rem;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.chip:hover {
  border-color: var(--accent);
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.9rem;
}

.action-card {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.action-card .btn {
  margin-top: auto;
  align-self: flex-start;
}

.action-card.primary {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.action-title {
  font-weight: 600;
  color: var(--muted);
}

.action-value {
  font-size: 2.2rem;
  font-weight: 800;
  line-height: 1.1;
}

.action-card p {
  margin: 0.2rem 0 0.9rem;
}

.topic-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.6rem;
  padding: 0.9rem 1.1rem;
}

.topic-main {
  flex: 1;
  min-width: 0;
}

.topic-title {
  font-weight: 600;
  color: var(--text);
  text-decoration: none;
}

.topic-title:hover {
  color: var(--accent);
}

.stacked-progress {
  display: flex;
  height: 8px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
  margin: 0.45rem 0 0.3rem;
}

.seg.mastered {
  background: var(--ok);
}

.seg.seen {
  background: color-mix(in srgb, var(--accent) 45%, transparent);
}

.due-text {
  color: var(--warn);
  font-weight: 600;
}

.howto {
  margin-top: 2rem;
}

.howto summary {
  cursor: pointer;
  font-weight: 600;
}

.controls {
  display: grid;
  grid-template-columns: 2fr repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
  align-items: end;
}

@media (max-width: 700px) {
  .controls {
    grid-template-columns: 1fr 1fr;
  }
  .controls .control.grow {
    grid-column: 1 / -1;
  }
}

.control {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}

.control select,
.control input {
  width: 100%;
  font-size: 0.95rem;
  font-weight: 400;
}

.topic-group h2 {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
}

.course-tag {
  font-weight: 400;
}

.quiz-row {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.6rem;
  color: inherit;
  text-decoration: none;
  transition: border-color 0.15s, transform 0.1s;
}

.quiz-row:hover {
  border-color: var(--accent);
}

.quiz-main {
  flex: 1;
  min-width: 0;
}

.quiz-title {
  font-weight: 600;
}

.quiz-side {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.4rem;
  min-width: 110px;
}

.error-rate {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--muted);
}

.error-bar {
  width: 56px;
  height: 6px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.error-bar div {
  height: 100%;
  background: var(--bad);
}

.runner-head {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.5rem 1rem;
  margin-bottom: 1rem;
}

.runner-title {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-weight: 600;
  min-width: 0;
}

.runner-title span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.runner-count {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 0.9rem;
}

.runner-head .progress {
  grid-column: 1 / -1;
}

.question-panel {
  padding: 1.5rem;
}

.question-panel .badge {
  margin-bottom: 0.6rem;
}

.prompt {
  font-size: 1.08rem;
}

.rich > :first-child {
  margin-top: 0;
}

.rich > :last-child {
  margin-bottom: 0;
}

.rich .katex {
  font-size: 1.05em;
}

.rich .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.2rem 0;
}

.options {
  display: grid;
  gap: 0.55rem;
  margin: 1.25rem 0 0;
}

.option {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  width: 100%;
  text-align: left;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 0.75rem 0.9rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.option:hover:not(:disabled) {
  border-color: var(--accent);
}

.option:disabled {
  cursor: default;
}

.option-key {
  flex: none;
  display: grid;
  place-items: center;
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}

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

.option.selected .option-key {
  background: var(--accent);
  color: var(--accent-text);
}

.option.correct {
  border-color: var(--ok);
  background: var(--ok-soft);
}

.option.correct .option-key {
  background: var(--ok);
  color: var(--surface);
}

.option.wrong {
  border-color: var(--bad);
  background: var(--bad-soft);
}

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

.option:disabled:not(.correct):not(.wrong) {
  opacity: 0.55;
}

.question .btn.primary {
  margin-top: 1rem;
}

.hint-row {
  margin-top: 1rem;
}

.hint {
  margin-top: 1rem;
  padding: 0.75rem 1rem;
  border-radius: 10px;
  background: var(--warn-soft);
  border-left: 3px solid var(--warn);
}

.feedback {
  margin-top: 1rem;
  padding: 1.1rem 1.25rem;
  border-radius: var(--radius);
  background: var(--ok-soft);
  border: 1px solid color-mix(in srgb, var(--ok) 35%, transparent);
  animation: rise 0.18s ease-out;
}

.feedback.bad {
  background: var(--bad-soft);
  border-color: color-mix(in srgb, var(--bad) 35%, transparent);
}

.verdict {
  font-weight: 800;
  margin-bottom: 0.4rem;
}

.verdict.ok {
  color: var(--ok);
}

.verdict.bad {
  color: var(--bad);
}

.ref {
  margin: 0.6rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.flashcard .card-label {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  margin-bottom: 0.5rem;
}

.card-back {
  margin-top: 1.1rem;
  padding-top: 1.1rem;
  border-top: 1px dashed var(--border);
  animation: rise 0.18s ease-out;
}

.grade-buttons {
  display: flex;
  gap: 0.6rem;
}

.summary {
  text-align: center;
  padding: 2rem 1.25rem;
}

.summary-score {
  font-size: 3rem;
  font-weight: 800;
  line-height: 1;
}

.summary .actions {
  justify-content: center;
}

.missed-item {
  margin-bottom: 0.6rem;
}

.missed-item summary {
  cursor: pointer;
  font-weight: 500;
}

.missed-item summary > * {
  display: inline;
}

.missed-item[open] summary {
  margin-bottom: 0.75rem;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translate(-50%, 1rem);
  max-width: min(90vw, 480px);
  padding: 0.7rem 1rem;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}

.toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast.error {
  background: var(--bad);
  color: #fff;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
}

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

@media (max-width: 560px) {
  .quiz-row {
    flex-wrap: wrap;
    gap: 0.6rem;
  }
  .quiz-side {
    width: 100%;
    min-width: 0;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}
