/* Aptis B2 study app */
:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-2: #f0f2f8;
  --border: #e1e4ee;
  --text: #1b2033;
  --muted: #5d6478;
  --primary: #1f3a8a;
  --primary-2: #2f54c9;
  --primary-soft: #e8edfc;
  --on-primary: #ffffff;
  --accent: #d97706;
  --ok: #15803d;
  --ok-soft: #e3f6e9;
  --bad: #c62828;
  --bad-soft: #fde8e8;
  --warn-soft: #fff5df;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 4px 16px rgba(20, 30, 60, .06);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --fs: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1320; --surface: #171c2c; --surface-2: #1f2538; --border: #2c3450;
    --text: #e8ebf5; --muted: #a0a8c0; --primary: #8fa8ff; --primary-2: #a9bcff;
    --primary-soft: #23305a; --on-primary: #0f1320; --accent: #f5a524; --ok: #4ade80; --ok-soft: #133022;
    --bad: #f87171; --bad-soft: #3a1717; --warn-soft: #3a2f12;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1320; --surface: #171c2c; --surface-2: #1f2538; --border: #2c3450;
  --text: #e8ebf5; --muted: #a0a8c0; --primary: #8fa8ff; --primary-2: #a9bcff;
  --primary-soft: #23305a; --on-primary: #0f1320; --accent: #f5a524; --ok: #4ade80; --ok-soft: #133022;
  --bad: #f87171; --bad-soft: #3a1717; --warn-soft: #3a2f12;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4); color-scheme: dark;
}
:root[data-size="lg"] { --fs: 18px; }
:root[data-size="xl"] { --fs: 20px; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text); font: 400 var(--fs)/1.55 var(--font);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
img { max-width: 100%; height: auto; border-radius: 10px; }
a { color: var(--primary-2); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.6em; } h2 { font-size: 1.3em; } h3 { font-size: 1.08em; }
p { margin: .4em 0; }
.muted { color: var(--muted); }
.small { font-size: .875em; }
.nowrap { white-space: nowrap; }
.noscript { padding: 24px; }

/* boot + spinner */
.boot { min-height: 100vh; display: grid; place-items: center; }
.boot-card { text-align: center; color: var(--muted); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--border);
  border-top-color: var(--primary); animation: spin 0.9s linear infinite; margin: 0 auto 12px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* layout */
.shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.side {
  position: sticky; top: 0; height: 100vh; background: var(--surface); border-right: 1px solid var(--border);
  padding: 20px 14px; display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; font-weight: 700; font-size: 1.05em; }
.brand img { width: 32px; height: 32px; border-radius: 8px; }
.brand small { display: block; font-weight: 500; color: var(--muted); font-size: .78em; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px;
  color: var(--text); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--primary-soft); color: var(--primary); }
.nav .ico { width: 22px; text-align: center; font-size: 1.05em; }
.nav .badge { margin-left: auto; }
.side .foot { margin-top: auto; font-size: .82em; color: var(--muted); padding: 8px; }
.main { padding: 28px clamp(16px, 4vw, 40px) 64px; max-width: 1100px; width: 100%; }
.topbar { display: none; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { display: none; }
  .topbar {
    display: flex; position: sticky; top: 0; z-index: 20; align-items: center; gap: 10px;
    padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px; background: var(--surface);
    border-bottom: 1px solid var(--border);
  }
  .topbar .brand { padding: 0; }
  .main { padding: 18px 16px 96px; }
  .tabbar {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; display: grid;
    grid-template-columns: repeat(5, 1fr); background: var(--surface); border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .tabbar a { text-align: center; padding: 8px 2px 6px; font-size: .72em; color: var(--muted); text-decoration: none; }
  .tabbar a .ico { display: block; font-size: 1.35em; line-height: 1.2; }
  .tabbar a.active { color: var(--primary); font-weight: 600; }
}
@media (min-width: 861px) { .tabbar { display: none; } }

/* floating back-to-top button (shown once the page is scrolled) */
.to-home {
  position: fixed; right: 16px; bottom: calc(24px + env(safe-area-inset-bottom, 0px)); z-index: 35;
  display: inline-flex; align-items: center; gap: 6px; padding: 10px 16px; border-radius: 99px;
  background: var(--primary); color: var(--on-primary); font: inherit; font-weight: 600; text-decoration: none;
  border: 0; cursor: pointer; box-shadow: 0 6px 18px rgba(0, 0, 0, .2);
}
.to-home:hover { background: var(--primary-2); color: var(--on-primary); }
.to-home[hidden] { display: none; }
@media (max-width: 860px) { .to-home { bottom: calc(72px + env(safe-area-inset-bottom, 0px)); } }
@media print { .to-home { display: none; } }

/* generic components */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; }
.card + .card { margin-top: 14px; }
.card.flat { box-shadow: none; }
.grid { display: grid; gap: 14px; }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 860px) { .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .grid.cols-3 { grid-template-columns: 1fr; } }
@media (max-width: 560px) { .grid.cols-2 { grid-template-columns: 1fr; } }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head p { margin: 0; color: var(--muted); }
.section-title { margin: 26px 0 12px; font-size: 1.05em; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: 9px 16px; border-radius: 10px; cursor: pointer;
  font-weight: 600; text-decoration: none; min-height: 42px; transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-2); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--bad); }
.btn.ok { color: var(--ok); }
.btn.sm { padding: 5px 10px; min-height: 34px; font-size: .88em; }
.btn.block { width: 100%; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-weight: 500; font-size: .9em;
}
.chip.on { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .75em; font-weight: 700;
  background: var(--surface-2); color: var(--muted); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.warn { background: var(--warn-soft); color: var(--accent); }
.badge.pri { background: var(--primary-soft); color: var(--primary); }

input[type=text], input[type=password], input[type=search], select, textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); min-height: 44px;
}
textarea { min-height: 140px; resize: vertical; line-height: 1.5; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, .chip:focus-visible, .opt:focus-within {
  outline: 2px solid var(--primary-2); outline-offset: 1px;
}
label.field { display: block; margin-bottom: 12px; }
label.field span { display: block; font-weight: 600; margin-bottom: 4px; font-size: .92em; }
.check { display: flex; gap: 8px; align-items: center; cursor: pointer; }
.check input { width: 18px; height: 18px; }

.alert { padding: 10px 14px; border-radius: 10px; background: var(--warn-soft); border: 1px solid transparent; }
.alert.bad { background: var(--bad-soft); color: var(--bad); }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.alert.info { background: var(--primary-soft); }

/* auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth .card { width: min(420px, 100%); padding: 28px; }
.auth .brand { justify-content: center; padding-bottom: 8px; }

/* stats */
.stat { padding: 16px; }
.stat .v { font-size: 1.8em; font-weight: 800; line-height: 1.1; }
.stat .l { color: var(--muted); font-size: .88em; }
.bar { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--primary-2); border-radius: 99px; width: 0; }
.bar.ok > i { background: var(--ok); }
.skill-card { display: flex; flex-direction: column; gap: 10px; }
.skill-card .head { display: flex; align-items: center; gap: 10px; }
.skill-ico { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; font-size: 1.25em;
  background: var(--primary-soft); color: var(--primary); flex: none; }
.part-line { display: grid; grid-template-columns: 70px 1fr 64px; gap: 10px; align-items: center; font-size: .88em; }

/* lists */
.list { display: flex; flex-direction: column; }
.list .item { display: flex; gap: 12px; align-items: center; padding: 12px 4px; border-bottom: 1px solid var(--border); }
.list .item:last-child { border-bottom: 0; }
.list .item .grow { flex: 1; min-width: 0; }
.list a.item { color: inherit; text-decoration: none; }
.list a.item:hover { background: var(--surface-2); }
.score-pill { min-width: 64px; text-align: center; font-weight: 800; padding: 6px 8px; border-radius: 10px; background: var(--surface-2); }
.score-pill.good { background: var(--ok-soft); color: var(--ok); }
.score-pill.mid { background: var(--warn-soft); color: var(--accent); }
.score-pill.low { background: var(--bad-soft); color: var(--bad); }
.empty { text-align: center; color: var(--muted); padding: 32px 12px; }

/* exam */
.exam { max-width: 860px; margin: 0 auto; }
.exam-bar {
  position: sticky; top: 0; z-index: 25; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  background: var(--bg); padding: 10px 0 12px; border-bottom: 1px solid var(--border); margin-bottom: 16px;
}
@media (max-width: 860px) { .exam-bar { top: calc(57px + env(safe-area-inset-top, 0px)); } }
.exam-bar .title { font-weight: 700; flex: 1; min-width: 160px; }
.timer { font-family: var(--mono); font-weight: 700; padding: 6px 10px; border-radius: 8px; background: var(--surface); border: 1px solid var(--border); }
.timer.low { color: var(--bad); border-color: var(--bad); }
.palette { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 16px; }
.palette button {
  min-width: 38px; height: 34px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface);
  cursor: pointer; font-weight: 600; font-size: .85em;
}
.palette button.done { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }
.palette button.cur { outline: 2px solid var(--accent); outline-offset: 1px; }
.palette button.flag::after { content: "⚑"; color: var(--accent); margin-left: 2px; }
.palette button.ok { background: var(--ok-soft); border-color: var(--ok); color: var(--ok); }
.palette button.bad { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }
.palette .sep { width: 100%; font-size: .75em; color: var(--muted); margin-top: 4px; }

.task-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.prompt { font-size: 1.05em; }
.prompt p:first-child { margin-top: 0; }
.vi { color: var(--muted); font-style: italic; font-size: .92em; }
.vi-toggle-off .vi, .vi-toggle-off [lang="vi"] { display: none; }
.context { background: var(--surface-2); border-radius: 12px; padding: 12px 14px; margin: 10px 0; }
.qblock { padding: 14px 0; border-top: 1px dashed var(--border); }
.qblock:first-of-type { border-top: 0; }
.qnum { display: inline-grid; place-items: center; min-width: 28px; height: 28px; border-radius: 8px;
  background: var(--primary-soft); color: var(--primary); font-weight: 800; font-size: .85em; margin-right: 8px; padding: 0 6px; }
.opts { display: grid; gap: 8px; margin-top: 10px; }
.opt {
  display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer;
}
.opt:hover { border-color: var(--primary-2); }
.opt input { margin-top: 4px; width: 18px; height: 18px; flex: none; }
.opt .k { font-weight: 800; color: var(--primary); min-width: 18px; }
.opt.sel { border-color: var(--primary); background: var(--primary-soft); }
.opt.right { border-color: var(--ok); background: var(--ok-soft); }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.rowq { display: grid; grid-template-columns: minmax(0, 1fr) minmax(150px, 280px); gap: 10px; align-items: center; padding: 10px 0; border-top: 1px solid var(--border); }
.rowq:first-child { border-top: 0; }
@media (max-width: 560px) { .rowq { grid-template-columns: 1fr; } }
.gap-sentence { line-height: 2.2; }
.gap-sentence select { width: auto; min-width: 120px; display: inline-block; min-height: 36px; padding: 4px 8px; }
.audio { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 8px 0; }
.audio audio { width: min(100%, 420px); }
.note { font-size: .86em; color: var(--muted); }
.hint { background: var(--warn-soft); border-radius: 10px; padding: 8px 12px; margin-top: 8px; font-size: .92em; }
.answer-box { margin-top: 10px; padding: 10px 14px; border-radius: 10px; background: var(--ok-soft); }
.answer-box.bad { background: var(--bad-soft); }
.answer-box .lbl { font-weight: 700; margin-right: 6px; }
.explain { margin-top: 10px; padding: 12px 14px; border-radius: 12px; background: var(--surface-2); font-size: .94em; }
.explain summary { cursor: pointer; font-weight: 700; }
.explain img { max-height: 520px; }
.sample { margin-top: 10px; padding: 12px 14px; border-radius: 12px; background: var(--primary-soft); }
.sample p { margin: .3em 0; }
.ok, span.ok { color: var(--ok); font-weight: 700; }
.was-wrong { text-decoration: line-through; color: var(--bad); }
.result-mark { font-weight: 800; }
.result-mark.r { color: var(--ok); } .result-mark.w { color: var(--bad); } .result-mark.n { color: var(--muted); }
.wc { font-size: .85em; color: var(--muted); text-align: right; }
.wc.ok { color: var(--ok); }
.speak-tools { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 10px; }
.big-timer { font: 800 1.6em/1 var(--mono); min-width: 80px; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--bad); animation: pulse 1s infinite; display: inline-block; }
@keyframes pulse { 50% { opacity: .2; } }
.order-list { display: flex; flex-direction: column; gap: 8px; }
.order-item { display: grid; grid-template-columns: 90px 1fr; gap: 10px; align-items: center; padding: 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.order-item select { min-height: 38px; padding: 4px 8px; }
.exam-nav { display: flex; justify-content: space-between; gap: 10px; margin-top: 18px; flex-wrap: wrap; }

/* answers bank */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filters input[type=search] { flex: 1; min-width: 200px; }
.set-card h3 { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; }
.reveal { filter: blur(6px); cursor: pointer; transition: filter .15s; user-select: none; }
.reveal.open { filter: none; user-select: auto; }
.src { font-size: .78em; color: var(--muted); }
.more { display: flex; justify-content: center; margin-top: 16px; }

/* modal + toast */
.modal-back { position: fixed; inset: 0; background: rgba(10, 14, 30, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: 16px; padding: 22px; width: min(480px, 100%); box-shadow: var(--shadow); }
.modal .actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.toast { position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; font-weight: 600; max-width: 90vw; }

/* table */
.table-wrap { overflow-x: auto; }
table.t { border-collapse: collapse; width: 100%; font-size: .92em; }
table.t th, table.t td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.t th { color: var(--muted); font-weight: 600; }

@media print {
  .side, .tabbar, .topbar, .filters, .btn, .exam-bar { display: none !important; }
  .main { padding: 0; max-width: none; }
  .card { box-shadow: none; break-inside: avoid; }
  .reveal { filter: none; }
}

/* spacing helpers (no inline styles: CSP style-src 'self') */
.gap { height: 14px; }
.mt { margin-top: 14px; }
.mt-s { margin-top: 8px; }
.grow { flex: 1; }
.review-head { margin: 22px 0 10px; }
.review-head h2 { margin: 0; }
.hist { display: flex; align-items: center; gap: 6px; border-bottom: 1px solid var(--border); }
.hist:last-child { border-bottom: 0; }
.hist > .item { flex: 1; min-width: 0; border-bottom: 0; }
.grid > .card + .card { margin-top: 0; }
a.chip { text-decoration: none; color: inherit; }
a.chip.on { color: var(--primary); }
@media (max-width: 560px) {
  .exam-bar { gap: 8px; }
  .exam-bar .title { flex-basis: 100%; font-size: .95em; }
  .exam-bar .btn { min-height: 36px; padding: 5px 10px; }
  .exam-bar #submit { margin-left: auto; }
  .page-head h1 { font-size: 1.35em; }
}

/* Ôn tập: một đoạn trả lời, câu đang đọc được tô sáng */
.ot-a { font-size: 1.1rem; line-height: 1.9; margin: 10px 0 0; }
.ot-s { cursor: pointer; border-radius: 4px; padding: 1px 2px; }
.ot-s.sel { background: var(--primary-soft); color: var(--primary); }
.ot-vi { color: var(--muted); font-size: 0.95rem; line-height: 1.6; margin: 6px 0 0; padding-top: 6px; border-top: 1px dashed var(--border); }
.ot-grp { margin: 22px 0 0; font-size: 1.05rem; color: var(--primary); }
.ot-also { color: var(--muted); font-size: 0.85rem; margin: 6px 0 0; }

/* chấm điểm theo Part */
.rub-btns { margin: .5rem 0; }
.rub-desc { list-style: none; margin: 0; padding: 0; font-size: .9rem; }
.rub-desc li { padding: .2rem .5rem; border-radius: 6px; }
.rub-desc li.sel { background: rgba(37, 99, 235, .14); font-weight: 600; }
.score-card .v { font-size: 1.4rem; }

/* kiểm ngữ pháp */
.gm { background: transparent; color: inherit; border-bottom: 2px solid var(--accent); cursor: help; }
.gm-sp { border-bottom-color: var(--bad); background: var(--bad-soft); }
.gm-gr { border-bottom-color: var(--accent); background: var(--warn-soft); }
.gm-st { border-bottom-color: var(--primary-2); background: var(--primary-soft); }
.gm-fix { background: #d7f5dd; border-radius: 3px; padding: 0 2px; }
.gram-fix { margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--line, #ccc); }
.fixed-text { margin: 4px 0; }
.gram { margin-top: 8px; }
.gram-list { margin: 6px 0 0; padding-left: 1.2rem; }
.gram-list li { margin: .35rem 0; }
.gram-list .gw { color: var(--bad); }
.gram-sum { margin: .4rem 0; }
