:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --border: #e2e4e9;
  --text: #1a1d23;
  --text-dim: #63666f;
  --accent: #3457d5;
  --accent-dim: #e8ecfb;
  --good: #1a8a4a;
  --good-bg: #e6f6ec;
  --mid: #b8790a;
  --mid-bg: #fdf1de;
  --bad: #c8372f;
  --bad-bg: #fbe9e8;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(20, 20, 30, 0.08), 0 1px 2px rgba(20, 20, 30, 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --surface: #1d2027;
    --surface-2: #262a33;
    --border: #333844;
    --text: #e8e9ed;
    --text-dim: #9a9ea8;
    --accent: #7f9bff;
    --accent-dim: #232a4a;
    --good: #4ad187;
    --good-bg: #123322;
    --mid: #e0a848;
    --mid-bg: #3a2c11;
    --bad: #f0685f;
    --bad-bg: #3a1a18;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.5;
  padding-bottom: 40px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px max(16px, env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar h1 {
  font-size: 1.05rem;
  margin: 0;
  font-weight: 600;
}

.modes {
  display: flex;
  gap: 6px;
  padding: 10px 16px;
  overflow-x: auto;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.mode-btn {
  flex: none;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}

.mode-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.views {
  display: flex;
  gap: 6px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.view-btn {
  padding: 7px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
}

.view-btn.active {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--text-dim);
}

#app {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px max(16px, env(safe-area-inset-left));
}

.search-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 14px;
}

.library-list details.topic-group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
  overflow: hidden;
}

.library-list summary.topic-summary {
  padding: 12px 14px;
  font-weight: 600;
  font-size: 0.92rem;
  cursor: pointer;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.library-list summary.topic-summary::-webkit-details-marker { display: none; }

.topic-summary .count {
  font-weight: 400;
  color: var(--text-dim);
  font-size: 0.8rem;
}

.library-list details.q-item {
  border-top: 1px solid var(--border);
}

.library-list summary.q-summary {
  padding: 10px 14px;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.88rem;
}

.library-list summary.q-summary::-webkit-details-marker { display: none; }

.library-list summary.q-summary .q-title { flex: 1; }

.q-body {
  padding: 4px 14px 14px 14px;
  background: var(--surface-2);
}

.q-body .question-text, .q-body .answer-text {
  font-size: 0.9rem;
}

.q-body .answer-label {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin: 10px 0 4px 0;
}

.no-results {
  text-align: center;
  color: var(--text-dim);
  padding: 30px 0;
}

.controls {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

select {
  flex: 1;
  min-width: 140px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  min-height: 180px;
}

.card-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.pill {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--accent-dim);
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 600;
}

.pill.level-easy { background: var(--good-bg); color: var(--good); }
.pill.level-medium { background: var(--mid-bg); color: var(--mid); }
.pill.level-hard { background: var(--bad-bg); color: var(--bad); }

.id-tag {
  font-size: 0.72rem;
  color: var(--text-dim);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  margin-left: auto;
}

#cardTitle {
  font-size: 1.15rem;
  margin: 0 0 10px 0;
}

.question-text, .statement {
  font-size: 0.98rem;
  white-space: pre-wrap;
}

.examples {
  margin-top: 10px;
  padding: 10px 12px;
  background: var(--surface-2);
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.82rem;
  white-space: pre-wrap;
  overflow-x: auto;
}

.primary-btn {
  display: block;
  width: 100%;
  margin-top: 16px;
  padding: 13px 16px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.ghost-btn {
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  font-size: 0.9rem;
  cursor: pointer;
}

.danger-btn {
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--bad);
  background: transparent;
  color: var(--bad);
  font-size: 0.85rem;
  cursor: pointer;
  margin-top: 16px;
}

.answer-box {
  margin-top: 14px;
  padding: 14px;
  background: var(--surface-2);
  border-radius: 10px;
  border-left: 3px solid var(--accent);
}

.answer-text {
  white-space: pre-wrap;
  font-size: 0.95rem;
}

.answer-text code, .question-text code, .statement code {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.87em;
}

.md-list {
  margin: 6px 0;
  padding-left: 20px;
}

.md-list li { margin-bottom: 4px; }

.code-block {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  margin: 8px 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.82rem;
  white-space: pre-wrap;
  overflow-x: auto;
}

.notes-text {
  margin-top: 8px;
  font-size: 0.85rem;
  color: var(--text-dim);
  white-space: pre-wrap;
}

.grade-box {
  margin-top: 16px;
  text-align: center;
}

.grade-label {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin: 0 0 8px 0;
}

.grade-btns {
  display: flex;
  gap: 8px;
}

.grade-btn {
  flex: 1;
  padding: 12px 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.grade-btn.grade-good:hover, .grade-btn.grade-good.picked { background: var(--good-bg); border-color: var(--good); color: var(--good); }
.grade-btn.grade-mid:hover, .grade-btn.grade-mid.picked { background: var(--mid-bg); border-color: var(--mid); color: var(--mid); }
.grade-btn.grade-bad:hover, .grade-btn.grade-bad.picked { background: var(--bad-bg); border-color: var(--bad); color: var(--bad); }

.nav-row {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.nav-row .primary-btn { margin-top: 0; }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.modal-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 480px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
}

.close-x {
  position: absolute;
  top: 12px;
  right: 12px;
  padding: 6px 10px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}

.stat-row .counts { color: var(--text-dim); font-size: 0.82rem; }

@media (max-width: 480px) {
  .topbar h1 { font-size: 0.95rem; }
  .grade-btns { flex-direction: row; }
  .grade-btn { font-size: 0.78rem; padding: 10px 4px; }
}
