:root {
  --bg: #f6f7f8;
  --surface: #ffffff;
  --ink: #16211f;
  --ink-soft: #5b6b68;
  --border: #e3e7e6;
  --accent: #0d9488;
  --accent-ink: #ffffff;
  --again: #e05252;
  --hard: #e08a3c;
  --good: #2f9e5c;
  --easy: #2b7de0;
  --shadow: 0 1px 2px rgba(16, 24, 22, 0.06), 0 8px 24px rgba(16, 24, 22, 0.08);
  --radius-lg: 22px;
  --radius-md: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1513;
    --surface: #182320;
    --ink: #eaf1ee;
    --ink-soft: #93a29e;
    --border: #263330;
    --accent: #14b8a6;
    --accent-ink: #06201c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

/* The hidden attribute must always win over component display rules
   (e.g. .rate-row{display:grid} and .flashcard-content{display:flex}
   otherwise have equal specificity and, being later in the cascade,
   would override [hidden] and show elements that should be hidden). */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior-y: contain;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 18px 14px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
}

.topbar-title {
  font-size: 1.15rem;
  font-weight: 700;
}

.icon-btn {
  border: none;
  background: transparent;
  font-size: 1.3rem;
  line-height: 1;
  padding: 8px;
  cursor: pointer;
  color: var(--ink-soft);
}

#app {
  flex: 1;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: 0 18px 100px;
  display: flex;
  flex-direction: column;
}

.screen { display: flex; flex-direction: column; gap: 20px; flex: 1; }
.screen[hidden] { display: none; }

/* Dashboard */
.today-done-line {
  font-size: 0.92rem;
  color: var(--ink-soft);
  text-align: center;
  margin: 0;
}

.today-done-line strong { color: var(--ink); font-weight: 700; }

.deck-hint {
  font-size: 0.78rem;
  color: var(--ink-soft);
  text-align: center;
  margin: -8px 0 0;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  flex: 0 0 auto;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 0.85rem;
  cursor: pointer;
  max-width: 100%;
  overflow-wrap: break-word;
}

.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.count-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.count-btn {
  padding: 12px 4px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}

.count-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: var(--radius-md);
  padding: 14px 18px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.btn-big { padding: 18px; font-size: 1.05rem; border-radius: var(--radius-lg); }

.btn-secondary {
  background: transparent;
  color: var(--again);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  font-size: 0.95rem;
  cursor: pointer;
}

.btn-outline {
  background: transparent;
  color: var(--accent);
  border: 1.5px solid var(--accent);
  border-radius: var(--radius-md);
  padding: 14px 18px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.hint { color: var(--ink-soft); font-size: 0.9rem; text-align: center; }

.progress-overview { margin-top: auto; }
.progress-bar-track {
  height: 8px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.3s ease;
}
.progress-overview-label {
  margin-top: 6px;
  font-size: 0.8rem;
  color: var(--ink-soft);
  text-align: center;
}

/* Review / Exam — full-height screens: fixed topbar, scrollable card
   area, action row pinned at the bottom so it's never pushed off-screen
   by long card content. */
.screen-immersive {
  height: calc(100vh - 68px - var(--safe-top));
  height: calc(100dvh - 68px - var(--safe-top));
  gap: 14px;
}

.review-topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}

.session-progress-track {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}
.session-progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.25s ease;
}
.session-count { font-size: 0.8rem; color: var(--ink-soft); min-width: 40px; text-align: right; }

.card-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  padding: 4px 0;
}

.flashcard {
  width: 100%;
  min-height: 220px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 26px 22px;
  display: flex;
  flex-direction: column;
  cursor: pointer;
}

.flashcard:active { border-color: var(--accent); }

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

.tag {
  font-size: 0.72rem;
  background: var(--bg);
  color: var(--ink-soft);
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  max-width: 100%;
  overflow-wrap: break-word;
}

.tag-diff { text-transform: capitalize; }

.flashcard-content {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 1.2rem;
  font-weight: 600;
  line-height: 1.45;
  padding: 8px 0;
}

.flashcard-back {
  font-size: 1.02rem;
  font-weight: 400;
  color: var(--ink-soft);
  border-top: 1px dashed var(--border);
  margin-top: 4px;
  padding-top: 14px;
  animation: reveal 0.22s ease;
}

@keyframes reveal {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.review-action {
  flex: 0 0 auto;
}

#showAnswerBtn {
  width: 100%;
}

.rate-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  animation: reveal 0.18s ease;
}

.rate-btn {
  border: none;
  border-radius: var(--radius-md);
  padding: 12px 4px;
  font-size: 0.85rem;
  font-weight: 700;
  color: #fff;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

.rate-sub {
  font-size: 0.68rem;
  font-weight: 500;
  opacity: 0.9;
}

.rate-again { background: var(--again); }
.rate-hard { background: var(--hard); }
.rate-good { background: var(--good); }
.rate-easy { background: var(--easy); }

.exam-grade-row {
  grid-template-columns: repeat(2, 1fr);
}

.exam-grade-row .rate-btn {
  padding: 16px 4px;
  font-size: 0.95rem;
}

.exam-score-headline { font-size: 1.6rem; }

.missed-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
}

.missed-item {
  background: var(--surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  font-size: 0.85rem;
}

.missed-item .topic-title { font-size: 0.78rem; color: var(--ink-soft); font-weight: 600; }
.missed-item .missed-front { margin-top: 2px; font-weight: 600; }
.missed-item .missed-back { margin-top: 4px; color: var(--ink-soft); }

/* Done screen */
.done-box {
  margin: auto;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
}
.done-emoji { font-size: 3rem; }
.done-box h2 { margin: 0; }

/* Topics */
.search-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font-size: 0.95rem;
}
.search-input::placeholder { color: var(--ink-soft); }

.topic-list { display: flex; flex-direction: column; gap: 10px; }

.topic-item {
  background: var(--surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.topic-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  cursor: pointer;
}

.topic-title { font-weight: 700; font-size: 0.95rem; }
.topic-sub { font-size: 0.78rem; color: var(--ink-soft); margin-top: 2px; }
.topic-chevron { color: var(--ink-soft); transition: transform 0.2s ease; flex: 0 0 auto; }
.topic-item.open .topic-chevron { transform: rotate(180deg); }

.topic-body {
  padding: 0 16px 16px;
  display: none;
  font-size: 0.9rem;
  color: var(--ink-soft);
  line-height: 1.5;
}
.topic-item.open .topic-body { display: block; }

.topic-body h4 { margin: 10px 0 4px; color: var(--ink); font-size: 0.85rem; }
.topic-body ul { margin: 0; padding-left: 18px; }
.topic-body li { margin-bottom: 4px; }
.topic-source { margin-top: 10px; font-size: 0.75rem; opacity: 0.7; }

/* Tab bar */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  z-index: 10;
}

.tab-btn {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px 0 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 0.75rem;
  color: var(--ink-soft);
  cursor: pointer;
}

.tab-btn.active { color: var(--accent); font-weight: 700; }
.tab-icon { font-size: 1.25rem; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  z-index: 20;
}
.modal-backdrop[hidden] { display: none; }

.modal {
  background: var(--surface);
  width: 100%;
  border-radius: 20px 20px 0 0;
  padding: 20px 20px calc(20px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal h3 { margin: 0; }

.modal-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
  gap: 10px;
}

.modal-row input {
  width: 80px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--ink);
  font-size: 0.95rem;
  text-align: center;
}

@media (min-width: 540px) {
  #app { padding-bottom: 40px; }
  .tabbar { max-width: 480px; left: 50%; transform: translateX(-50%); border-radius: 16px 16px 0 0; }
}
