/* ===== Theme tokens ===== */
:root {
  --bg: #ffffff;
  --bg-elev: #f5f6f8;
  --card: #ffffff;
  --border: #e3e5ea;
  --text: #14161a;
  --text-dim: #5b6270;
  --primary: #4f46e5;
  --primary-text: #ffffff;
  --correct: #16a34a;
  --correct-bg: #e7f6ec;
  --wrong: #dc2626;
  --wrong-bg: #fdeaea;
  --hint: #2563eb;
  --neutral: #9ca3af;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08);
  --radius: 14px;
  --tap: 44px;
}

html[data-theme='dark'] {
  --bg: #0f1115;
  --bg-elev: #171a21;
  --card: #1b1f27;
  --border: #2a2f3a;
  --text: #e6e8ec;
  --text-dim: #9aa1ad;
  --primary: #818cf8;
  --primary-text: #0f1115;
  --correct: #4ade80;
  --correct-bg: #143020;
  --wrong: #f87171;
  --wrong-bg: #3a1718;
  --hint: #60a5fa;
  --neutral: #6b7280;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

@media (prefers-color-scheme: dark) {
  html[data-theme='auto'] {
    --bg: #0f1115;
    --bg-elev: #171a21;
    --card: #1b1f27;
    --border: #2a2f3a;
    --text: #e6e8ec;
    --text-dim: #9aa1ad;
    --primary: #818cf8;
    --primary-text: #0f1115;
    --correct: #4ade80;
    --correct-bg: #143020;
    --wrong: #f87171;
    --wrong-bg: #3a1718;
    --hint: #60a5fa;
    --neutral: #6b7280;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

/* ===== Base ===== */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  min-height: 100vh;
}

/* ===== Header ===== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) 12px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.app-title {
  flex: 1;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  text-align: center;
  line-height: 1.2;
  /* long unit titles wrap to two lines (then ellipsis) instead of being cut
     to one line — keeps orientation context on small screens */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
@media (max-width: 380px) { .app-title { font-size: 16px; } }
.hdr-right { display: flex; align-items: center; gap: 4px; }
.hdr-btn {
  min-width: var(--tap);
  height: var(--tap);
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 24px;
  cursor: pointer;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.hdr-btn:active { background: var(--bg-elev); }

/* ===== Layout ===== */
.view {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 16px calc(96px + env(safe-area-inset-bottom));
}
.screen-title { font-size: 22px; font-weight: 700; margin: 4px 0 16px; }
.screen-sub { color: var(--text-dim); margin: -8px 0 18px; font-size: 14px; }

/* ===== Cards / list rows ===== */
.list { display: flex; flex-direction: column; gap: 10px; }
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: inherit;
  font: inherit;
}
.row:active { transform: scale(.995); }
.row.disabled { opacity: .5; pointer-events: none; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; }
.row-meta { font-size: 13px; color: var(--text-dim); margin-top: 2px; }
/* interaction-type chip — tells the learner what they'll do at a glance */
.type-chip {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  vertical-align: middle;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--bg-elev);
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.row-icon { font-size: 20px; }
.chev { color: var(--neutral); font-size: 22px; }

.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .03em;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--bg-elev);
  color: var(--text-dim);
}

/* ===== Filter (large unit lists) ===== */
.filter { margin: 0 0 12px; }
.filter-input {
  width: 100%;
  height: var(--tap);
  padding: 0 14px;
  font-size: 16px; /* ≥16px avoids iOS zoom-on-focus */
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-elev);
  color: var(--text);
}
.filter-input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.filter-empty { color: var(--text-dim); text-align: center; padding: 24px 0; }

/* ===== Progress bar ===== */
.pbar {
  height: 6px;
  border-radius: 999px;
  background: var(--bg-elev);
  overflow: hidden;
  margin-top: 8px;
}
.pbar > span {
  display: block;
  height: 100%;
  background: var(--primary);
  transition: width .3s ease;
}

/* status dots */
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.dot.clean { background: var(--correct); }
.dot.mistakes { background: #f59e0b; }
.dot.not_done { background: var(--neutral); }

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 12px 18px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
}
.btn:active { transform: scale(.99); }
.btn.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }

/* fixed bottom action bar */
.actionbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
  z-index: 9;
}
.actionbar .inner { max-width: 720px; margin: 0 auto; display: flex; gap: 10px; }
.actionbar .inner > * { flex: 1; }

/* ===== Theory cards ===== */
.flip-wrap { perspective: 1200px; margin: 8px 0 16px; }
.flip {
  position: relative;
  width: 100%;
  min-height: 320px;
  transform-style: preserve-3d;
  transition: transform .45s;
}
.flip.flipped { transform: rotateY(180deg); }
.flip-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow: auto;
}
.flip-face.back { transform: rotateY(180deg); }
.flip-face .label { font-size: 12px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 10px; }
.flip-face .q { font-size: 20px; font-weight: 700; }
.flip-face .a { white-space: pre-wrap; font-size: 16px; }
.flip-hint { text-align: center; color: var(--text-dim); font-size: 13px; margin-top: 4px; }
.card-nav { display: flex; align-items: center; justify-content: space-between; margin-top: 14px; }
.card-nav .count { color: var(--text-dim); font-size: 14px; }
.card-nav button {
  min-width: var(--tap); height: var(--tap); border-radius: 10px;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
  font-size: 22px; cursor: pointer;
}
.card-nav button:disabled { opacity: .35; }

/* ===== Theory card (rich / new schema) ===== */
.theory-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin: 8px 0 16px;
}
.tc-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 6px; }
.tc-title { font-size: 20px; font-weight: 700; flex: 1; }
.tc-level {
  font-size: 12px; font-weight: 600; color: var(--text-dim);
  border: 1px solid var(--border); border-radius: 999px; padding: 2px 8px; white-space: nowrap;
}
.tc-section { margin-top: 18px; }
.tc-heading { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--primary); margin-bottom: 8px; }
.tc-text { font-size: 16px; line-height: 1.5; white-space: pre-wrap; }
.tc-example { margin-top: 10px; padding-left: 12px; border-left: 3px solid var(--border); }
.tc-en { font-size: 16px; font-weight: 600; }
.tc-note { font-size: 14px; color: var(--text-dim); margin-top: 2px; }
.tc-mistake { margin-top: 10px; }
.tc-wrong { font-size: 15px; color: var(--wrong); }
.tc-right { font-size: 15px; color: var(--correct); }
.tc-why { font-size: 14px; color: var(--text-dim); margin-top: 2px; }
.tc-qt { margin-top: 10px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.tc-q { font-size: 16px; flex: 1 1 100%; }
.tc-reveal {
  font-size: 14px; border: 1px solid var(--border); background: var(--card); color: var(--text);
  border-radius: 8px; padding: 4px 10px; cursor: pointer;
}
.tc-a { font-size: 16px; font-weight: 700; color: var(--correct); }
.theory-btn { margin-bottom: 16px; }

/* ===== Matching ===== */
.match { display: flex; flex-direction: column; gap: 12px; }
.match-row { display: flex; flex-direction: column; gap: 6px; }
.match-left { font-size: 16px; }
.match-select {
  width: 100%; min-height: var(--tap); font-size: 16px;
  padding: 8px 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
}
.match-select.ok { border-color: var(--correct); }
.match-select.no { border-color: var(--wrong); }
.match-correct { font-size: 14px; color: var(--correct); margin-top: 2px; }

/* ===== Exercise ===== */
.instruction { font-size: 16px; font-weight: 600; margin: 4px 0 16px; }
.ex-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.ex-item.correct { border-color: var(--correct); background: var(--correct-bg); }
.ex-item.wrong { border-color: var(--wrong); background: var(--wrong-bg); }
.ex-prompt { font-size: 17px; margin-bottom: 10px; white-space: pre-wrap; }
.ex-prompt .gap-inline {
  display: inline-block;
  min-width: 90px;
  border: none;
  border-bottom: 2px solid var(--primary);
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: center;
  padding: 2px 4px;
  margin: 0 2px;
}
.gap-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 4px; }
input.gap {
  flex: 1;
  min-width: 120px;
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  padding: 10px 12px;
}
input.gap:focus { outline: 2px solid var(--primary); border-color: var(--primary); }
input.gap.ok { border-color: var(--correct); }
input.gap.no { border-color: var(--wrong); }

/* option buttons (choice / multichoice) */
.opts { display: flex; flex-direction: column; gap: 8px; }
.opt {
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  padding: 10px 14px;
  text-align: left;
  cursor: pointer;
}
.opt.selected { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, transparent); }
.opt.ok { border-color: var(--correct); background: var(--correct-bg); }
.opt.no { border-color: var(--wrong); background: var(--wrong-bg); }

.verdict { font-size: 14px; margin-top: 8px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.verdict .mark { font-weight: 700; }
.verdict .mark.ok { color: var(--correct); }
.verdict .mark.no { color: var(--wrong); }
.answer-reveal { color: var(--text-dim); }
.answer-reveal b { color: var(--text); }
.hint-btn {
  border: 1px solid var(--hint);
  color: var(--hint);
  background: transparent;
  border-radius: 999px;
  height: 30px; min-width: 30px;
  font-weight: 700;
  cursor: pointer;
}
.hint-box {
  margin-top: 8px;
  font-size: 14px;
  background: var(--bg-elev);
  border-left: 3px solid var(--hint);
  padding: 8px 12px;
  border-radius: 6px;
}

/* results */
.score-big { font-size: 46px; font-weight: 800; text-align: center; }
.score-sub { text-align: center; color: var(--text-dim); margin-bottom: 20px; }
.callout {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin: 16px 0;
  text-align: center;
}

/* toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 50;
  box-shadow: var(--shadow);
}

.empty { text-align: center; color: var(--text-dim); padding: 48px 16px; }
.spinner { text-align: center; padding: 48px; color: var(--text-dim); }
