/* ============ design tokens ============ */
:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --text-soft: #475569;
  --text-faint: #94a3b8;
  --border: #e5e7eb;
  --primary: #4f46e5;
  --primary-soft: #eef2ff;
  --accent: #0ea5e9;
  --good: #16a34a;
  --good-soft: #dcfce7;
  --warn: #ea580c;
  --warn-soft: #ffedd5;
  --radius: 20px;
  --radius-sm: 12px;
  --shadow: 0 10px 30px -12px rgba(15, 23, 42, .25);
  --shadow-sm: 0 4px 14px -6px rgba(15, 23, 42, .2);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #0f172a;
    --text: #f1f5f9;
    --text-soft: #cbd5e1;
    --text-faint: #64748b;
    --border: #1f2937;
    --primary: #818cf8;
    --primary-soft: #1e1b4b;
    --good-soft: #052e16;
    --warn-soft: #431407;
    --shadow: 0 10px 30px -12px rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  overscroll-behavior-y: none;
}

/* ============ app shell (mobile-first, phone-width) ============ */
.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  position: relative;
}
@media (min-width: 520px) {
  .app { box-shadow: 0 0 60px -20px rgba(0, 0, 0, .35); }
}

.app__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 16px) 18px 16px;
  background: linear-gradient(135deg, #4f46e5, #7c3aed 55%, #0ea5e9);
  color: #fff;
}
.app__brand { display: flex; align-items: center; gap: 12px; }
.app__logo { font-size: 28px; }
.app__header h1 { font-size: 20px; font-weight: 800; letter-spacing: -.02em; }
.app__header p { font-size: 12px; opacity: .85; }
.app__score {
  text-align: center;
  background: rgba(255, 255, 255, .18);
  border-radius: 14px;
  padding: 8px 12px;
  backdrop-filter: blur(4px);
  min-width: 64px;
}
.app__score-num { display: block; font-size: 20px; font-weight: 800; }
.app__score-label { font-size: 10px; opacity: .9; text-transform: uppercase; letter-spacing: .04em; }

/* ---- level scope switcher (under the header) ---- */
.levelbar {
  position: sticky;
  top: 0;
  z-index: 15;
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.levelbar__btn {
  flex: 1 0 auto;
  min-width: 84px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-soft);
  font: inherit;
  border-radius: 12px;
  padding: 7px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  transition: background .15s, color .15s, border-color .15s;
}
.levelbar__btn.is-active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.levelbar__code { font-weight: 800; font-size: 14px; letter-spacing: .02em; }
.levelbar__meta { font-size: 11px; opacity: .8; font-variant-numeric: tabular-nums; }

.leveljump { display: flex; flex-wrap: wrap; gap: 8px; }
.leveljump .chip { border: 0; cursor: pointer; font: inherit; }

.app__main {
  flex: 1;
  padding: 18px 16px 120px;
}

/* ============ bottom tab bar ============ */
.tabbar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: 480px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  padding: 8px 8px calc(env(safe-area-inset-bottom) + 8px);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  z-index: 30;
}
.tabbar__btn {
  position: relative;
  border: 0;
  background: transparent;
  color: var(--text-faint);
  font: inherit;
  font-size: 10.5px;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 6px 2px;
  border-radius: 12px;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.tabbar__btn .tabbar__icon { font-size: 19px; filter: grayscale(.4); transition: filter .15s; }
.tabbar__btn.is-active { color: var(--primary); background: var(--primary-soft); }
.tabbar__btn.is-active .tabbar__icon { filter: none; }
.tabbar__badge {
  position: absolute;
  top: 2px;
  right: calc(50% - 22px);
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: #ef4444;
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  line-height: 16px;
  text-align: center;
}

/* ---- account chip in header ---- */
.acct {
  display: flex;
  align-items: center;
  gap: 7px;
  border: 0;
  background: rgba(255, 255, 255, .18);
  color: #fff;
  font: inherit;
  font-weight: 700;
  font-size: 12px;
  padding: 6px 10px 6px 6px;
  border-radius: 999px;
  cursor: pointer;
  max-width: 46%;
}
.acct__avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: rgba(255, 255, 255, .25);
  display: grid;
  place-items: center;
  font-size: 15px;
  background-size: cover;
  background-position: center;
  flex: none;
}
.acct__avatar--img { font-size: 0; }
.acct__name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- login gate ---- */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: linear-gradient(160deg, #4f46e5, #7c3aed 55%, #0ea5e9);
}
.login__card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 30px 24px;
  text-align: center;
}
.login__logo { font-size: 44px; }
.login__card h1 { font-size: 26px; font-weight: 800; margin-top: 4px; }
.login__sub { color: var(--text-soft); font-size: 13px; margin: 8px 0 22px; }
.login__gbtn { display: flex; justify-content: center; min-height: 40px; margin-bottom: 12px; }
.login__guest { width: 100%; }
.login__err { color: #ef4444; font-size: 12.5px; margin-top: 12px; }
.login__hint { font-size: 11.5px; color: var(--text-faint); margin: 10px 0 2px; line-height: 1.5; }
.login__hint code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px; font-size: 11px; word-break: break-all; }

/* ---- review empty state ---- */
.review-empty { text-align: center; padding: 48px 12px; }
.review-empty__icon { font-size: 44px; }
.review-empty__title { font-size: 17px; font-weight: 800; margin: 10px 0 4px; }

/* due badge inside the level chip */
.levelbar__due {
  display: inline-block;
  min-width: 15px;
  padding: 0 4px;
  border-radius: 999px;
  background: #ef4444;
  color: #fff;
  font-size: 10px;
  line-height: 15px;
  vertical-align: middle;
}
.levelbar__btn.is-active .levelbar__due { background: rgba(255,255,255,.9); color: var(--primary); }

/* ---- Học tab: mode toggle (Ngẫu nhiên | Tất cả) ---- */
.modeseg {
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 4px;
  margin-bottom: 14px;
}
.modeseg button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text-soft);
  font: inherit;
  font-weight: 700;
  font-size: 12.5px;
  padding: 8px 6px;
  border-radius: 10px;
  cursor: pointer;
}
.modeseg button.is-active { background: var(--primary); color: #fff; }

/* ---- step through the full list ---- */
.stepnav { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.stepnav .btn { flex: 1; padding: 10px; }
.stepnav__pos { font-weight: 800; font-size: 13px; color: var(--text-soft); min-width: 64px; text-align: center; }

.word-row__idx {
  font-size: 11px;
  font-weight: 800;
  color: var(--text-faint);
  min-width: 22px;
  font-variant-numeric: tabular-nums;
}

/* ============ generic bits ============ */
.section-title { font-size: 13px; font-weight: 700; color: var(--text-faint); text-transform: uppercase; letter-spacing: .06em; margin: 22px 2px 10px; }
.muted { color: var(--text-soft); }
.center { text-align: center; }
.spinner {
  width: 34px; height: 34px; margin: 60px auto;
  border: 3px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.chip {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 700;
  padding: 4px 9px; border-radius: 999px;
  background: var(--primary-soft); color: var(--primary);
}
.chip--gray { background: var(--surface-2); color: var(--text-soft); }
.chip--good { background: var(--good-soft); color: var(--good); }
.chip--warn { background: var(--warn-soft); color: var(--warn); }

/* ============ flashcard (Learn) ============ */
.learn { display: flex; flex-direction: column; gap: 16px; }
.learn__hint { text-align: center; font-size: 12px; color: var(--text-faint); }

.flip { perspective: 1400px; }
.flip__inner {
  position: relative;
  width: 100%;
  min-height: 440px;
  transition: transform .5s cubic-bezier(.4, .2, .2, 1);
  transform-style: preserve-3d;
}
.flip.is-flipped .flip__inner { transform: rotateY(180deg); }
.card-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.card-face--back { transform: rotateY(180deg); overflow-y: auto; }

.card-front__top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.card-front__center { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 10px; }
.headword { font-size: 40px; font-weight: 800; letter-spacing: -.02em; }
.ipa { font-size: 16px; color: var(--text-soft); }
.pos { font-size: 13px; color: var(--text-faint); font-style: italic; }
.tap-note { font-size: 12px; color: var(--text-faint); display: flex; align-items: center; gap: 6px; }
.tap-note::before { content: "👆"; }

.card-back h2 { font-size: 24px; font-weight: 800; }
.card-back .ipa { margin-bottom: 12px; }
.def { margin: 6px 0; }
.def__en { font-size: 15px; font-weight: 600; }
.def__vi { font-size: 14px; color: var(--accent); }
.block { margin-top: 14px; }
.block__label { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint); margin-bottom: 4px; }
.block__body { font-size: 13.5px; color: var(--text-soft); }
.pattern { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; color: var(--text); }

.usage-grid { display: grid; gap: 8px; }
.usage-item { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; }
.usage-item b { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--primary); margin-bottom: 2px; }
.usage-item span { font-size: 13px; color: var(--text-soft); }

.ex { border-left: 3px solid var(--primary); padding: 4px 0 4px 10px; margin: 8px 0; }
.ex--bad { border-left-color: var(--warn); }
.ex__en { font-size: 14px; }
.ex__vi { font-size: 12.5px; color: var(--text-faint); }
.ex--bad .ex__en { text-decoration: line-through; text-decoration-color: var(--warn); }

.colloc { display: flex; flex-wrap: wrap; gap: 6px; }
.colloc__item { font-size: 12.5px; background: var(--primary-soft); color: var(--primary); border-radius: 8px; padding: 5px 9px; font-weight: 600; }

.dialogue { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; }
.dialogue__title { font-size: 13px; font-weight: 700; margin-bottom: 2px; }
.dialogue__scenario { font-size: 12px; color: var(--text-faint); margin-bottom: 10px; }
.line { display: flex; gap: 8px; margin: 7px 0; }
.line__who { font-weight: 800; font-size: 12px; color: var(--primary); min-width: 46px; }
.line__txt { font-size: 13px; }
.line__vi { font-size: 12px; color: var(--text-faint); }
.line.is-target .line__txt { background: #fef9c3; border-radius: 6px; padding: 0 4px; color: #713f12; }
@media (prefers-color-scheme: dark) { .line.is-target .line__txt { background: #422006; color: #fde68a; } }

/* action row */
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.actions--three { grid-template-columns: 1fr 1.4fr 1fr; }
.btn {
  border: 0; border-radius: 16px; padding: 15px 12px;
  font: inherit; font-weight: 800; font-size: 14px;
  cursor: pointer; transition: transform .08s, filter .15s, box-shadow .15s;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.btn small { font-weight: 600; font-size: 11px; opacity: .8; }
.btn:active { transform: scale(.96); }
.btn--primary { background: var(--primary); color: #fff; box-shadow: var(--shadow-sm); }
.btn--good { background: var(--good); color: #fff; box-shadow: var(--shadow-sm); }
.btn--warn { background: var(--warn-soft); color: var(--warn); }
.btn--ghost { background: var(--surface); color: var(--text-soft); border: 1px solid var(--border); }
.btn--danger { background: #ef4444; color: #fff; box-shadow: var(--shadow-sm); }
.btn--block { width: 100%; grid-column: 1 / -1; }

/* ---- boot splash (no login flash on reload) ---- */
.splash {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center; align-content: center; gap: 14px;
  background: linear-gradient(160deg, #4f46e5, #7c3aed 55%, #0ea5e9);
  animation: fadeIn .2s ease;
}
.splash[hidden] { display: none; }
.splash__logo { font-size: 52px; filter: drop-shadow(0 6px 16px rgba(0,0,0,.25)); }
.splash .spinner { border-color: rgba(255,255,255,.35); border-top-color: #fff; margin: 0; }

/* ---- view transition on tab change ---- */
.app__main.anim { animation: viewIn .22s cubic-bezier(.22,.61,.36,1); }
@keyframes viewIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ---- account sheet (logout) ---- */
.sheet { position: fixed; inset: 0; z-index: 60; }
.sheet[hidden] { display: none; }
.sheet__backdrop {
  position: absolute; inset: 0; background: rgba(15,23,42,.5);
  opacity: 0; transition: opacity .2s ease;
}
.sheet.is-open .sheet__backdrop { opacity: 1; }
.sheet__panel {
  position: absolute; left: 50%; bottom: 0; width: 100%; max-width: 480px;
  transform: translateX(-50%) translateY(100%);
  transition: transform .24s cubic-bezier(.22,.61,.36,1);
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 10px 18px calc(env(safe-area-inset-bottom) + 20px);
  box-shadow: 0 -12px 40px -12px rgba(0,0,0,.35);
}
.sheet.is-open .sheet__panel { transform: translateX(-50%) translateY(0); }
.sheet__grip { width: 40px; height: 4px; border-radius: 999px; background: var(--border); margin: 4px auto 16px; }
.sheet__head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.sheet__avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none;
  background: var(--primary-soft); color: var(--primary);
  display: grid; place-items: center; font-size: 22px;
  background-size: cover; background-position: center;
}
.sheet__avatar.acct__avatar--img { font-size: 0; }
.sheet__name { font-weight: 800; font-size: 16px; }
.sheet__sub { font-size: 12px; color: var(--text-faint); }
.sheet__stat {
  font-size: 12.5px; color: var(--text-soft);
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px 12px; margin-bottom: 14px;
}

/* ---- review-due badge pulse ---- */
.tabbar__badge { animation: badgePulse 1.8s ease-in-out infinite; }
@keyframes badgePulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,.55); }
  50%      { box-shadow: 0 0 0 5px rgba(239,68,68,0); }
}

/* ---- swipe-to-grade feedback on the flashcard ---- */
.flip { transition: transform .15s ease; touch-action: pan-y; }
.flip.swipe-good  .flip__inner { box-shadow: 0 0 0 3px var(--good) inset, var(--shadow); border-radius: var(--radius); }
.flip.swipe-again .flip__inner { box-shadow: 0 0 0 3px var(--warn) inset, var(--shadow); border-radius: var(--radius); }

@media (prefers-reduced-motion: reduce) {
  .app__main.anim, .splash, .tabbar__badge { animation: none; }
  .sheet__panel, .sheet__backdrop, .flip { transition: none; }
}

/* level selector */
.levels { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.levels button {
  border: 1px solid var(--border); background: var(--surface); color: var(--text-soft);
  font: inherit; font-weight: 700; font-size: 12px; padding: 6px 12px; border-radius: 999px; cursor: pointer;
}
.levels button.is-active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ============ Browse ============ */
.grouplist { display: grid; gap: 10px; }
.group-card {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 14px; cursor: pointer;
  transition: transform .08s;
}
.group-card:active { transform: scale(.98); }
.group-card__icon { font-size: 26px; }
.group-card__body { flex: 1; }
.group-card__name { font-weight: 700; font-size: 15px; }
.group-card__desc { font-size: 12px; color: var(--text-faint); }
.group-card__count { font-size: 12px; font-weight: 800; color: var(--primary); }

.wordlist { display: grid; gap: 8px; }
.word-row {
  display: flex; align-items: baseline; gap: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 12px 14px; cursor: pointer;
}
.word-row:active { transform: scale(.99); }
.word-row__head { font-weight: 700; font-size: 15px; }
.word-row__pos { font-size: 11px; color: var(--text-faint); font-style: italic; }
.word-row__def { font-size: 12px; color: var(--text-soft); margin-left: auto; text-align: right; max-width: 55%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.backlink { background: none; border: 0; color: var(--primary); font: inherit; font-weight: 700; font-size: 13px; cursor: pointer; margin-bottom: 12px; padding: 4px 0; }
.backlink::before { content: "‹ "; }

.search {
  width: 100%; border: 1px solid var(--border); background: var(--surface);
  border-radius: 12px; padding: 11px 14px; font: inherit; font-size: 14px; color: var(--text);
  margin-bottom: 12px;
}
.search::placeholder { color: var(--text-faint); }

/* detail reuses card-back styles inside a plain panel */
.detail-panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-sm); padding: 20px;
}

/* ============ Stats ============ */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 16px; text-align: center; }
.stat-card__num { font-size: 26px; font-weight: 800; color: var(--primary); }
.stat-card__label { font-size: 12px; color: var(--text-faint); }
.bar { height: 10px; border-radius: 999px; background: var(--surface-2); overflow: hidden; display: flex; margin-top: 6px; }
.bar > i { display: block; height: 100%; }
.bar > .b-known { background: var(--good); }
.bar > .b-learning { background: var(--accent); }
.bar > .b-new { background: var(--border); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-soft); margin-top: 8px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: middle; }

.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); font-size: 13px; font-weight: 700;
  padding: 10px 16px; border-radius: 999px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 50;
}
.toast.is-show { opacity: 1; transform: translateX(-50%) translateY(0); }
