:root,
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #3b4560;
  --page-2: #2a3350;
  --shell: #131b31;
  --card: #030a22;
  --raised: #1a2238;
  --raised-hover: #242d47;
  --bar: #030a22;
  --on-bar: #ffffff;
  --ink: #eef1f8;
  --muted: #a0aac4;
  --line: #2b3552;
  --line-strong: #5c6785;
  --primary: #12d6bd;
  --primary-hover: #40e3cd;
  --on-primary: #03201c;
  --primary-tint: rgba(18, 214, 189, 0.12);
  --submit: #e11d48;
  --submit-hover: #f43f5e;
  --done: #2fd3a7;
  --review: #f5a623;
  --skipped: #f4714a;
  --current-bg: #ffffff;
  --current-ink: #0b1228;
  --ok: #4ade9f;
  --ok-tint: rgba(74, 222, 159, 0.14);
  --warn: #ff8a80;
  --warn-tint: rgba(255, 90, 90, 0.14);
  --focus: #7cc4ff;
  --shadow: 0 24px 60px rgba(4, 8, 22, 0.45);
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

:root[data-theme="light"] {
  color-scheme: light;
  --page: #dde2ec;
  --page-2: #cfd6e4;
  --shell: #f4f6fa;
  --card: #ffffff;
  --raised: #eef1f6;
  --raised-hover: #e2e7f0;
  --bar: #ffffff;
  --on-bar: #131b31;
  --ink: #131b31;
  --muted: #525d75;
  --line: #d5dae5;
  --line-strong: #8791a6;
  --primary: #0a7d6e;
  --primary-hover: #086557;
  --on-primary: #ffffff;
  --primary-tint: rgba(10, 125, 110, 0.1);
  --submit: #d11a44;
  --submit-hover: #b01539;
  --done: #0a7d6e;
  --review: #a66200;
  --skipped: #c2421d;
  --current-bg: #131b31;
  --current-ink: #ffffff;
  --ok: #17603f;
  --ok-tint: #e2f1e9;
  --warn: #b3261e;
  --warn-tint: #fbe9e7;
  --focus: #1f6fd1;
  --shadow: 0 20px 50px rgba(40, 52, 84, 0.18);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; background: var(--page-2); }

body {
  margin: 0;
  min-height: 100vh;
  background: linear-gradient(160deg, var(--page), var(--page-2));
  color: var(--ink);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.5;
}

h1, h2, p { margin: 0; }
h1 { font-size: 1.5rem; line-height: 1.3; }
h2 { font-size: 1.1rem; }
a { color: var(--primary); }

[hidden] { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.muted { color: var(--muted); font-size: 0.94rem; }
.noscript { padding: 24px; }

.eyebrow {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

/* Logo và nút đổi giao diện */
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-logo { flex: none; width: 44px; height: 45px; object-fit: contain; }
.brand-name {
  max-width: 17em;
  color: var(--on-bar);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1.25;
  text-wrap: balance;
}
@media (min-width: 560px) {
  .brand-name { font-size: 0.86rem; letter-spacing: 0.06em; }
}

/* Menu tài khoản ở góc trên bên phải */
.topbar-actions { display: flex; flex: none; align-items: center; gap: 10px; }
.account { position: relative; }
.account-button {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  border-radius: 999px;
  color: var(--on-bar);
  cursor: pointer;
  list-style: none;
}
.account-button::-webkit-details-marker { display: none; }
/* Mũi tên báo đây là menu */
.account-button::after {
  content: "";
  flex: none;
  width: 7px;
  height: 7px;
  margin: -3px 2px 0 0;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg);
}
.account-avatar {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--on-primary);
  font-size: 0.95rem;
  font-weight: 700;
}
.account-name { display: none; }
.account-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 20;
  width: min(280px, calc(100vw - 32px));
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  color: var(--ink);
  box-shadow: var(--shadow);
}
.account-panel > * + * { margin-top: 6px; }
.account-panel .btn { margin-top: 14px; min-height: 44px; }
.account-panel-name { font-weight: 700; }
@media (min-width: 560px) {
  .account-name {
    display: block;
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.theme-toggle {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  background: transparent;
  color: var(--on-bar);
  cursor: pointer;
}
.theme-toggle:hover { background: var(--raised); }
.theme-toggle svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.corner-toggle { position: fixed; top: 14px; right: 16px; --on-bar: var(--ink); }

/* Bố cục chung */
.page { max-width: 620px; margin: 0 auto; padding: 20px 16px 40px; }
.center-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 64px 16px 24px;
}
.center-page > .card { width: 100%; max-width: 420px; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 24px 20px;
  box-shadow: var(--shadow);
}
.stack > * + * { margin-top: 16px; }
.stack-sm > * + * { margin-top: 10px; }
.login-card > * + * { margin-top: 10px; }
.login-card form { margin-top: 20px; }
.login-card .brand { margin-bottom: 18px; }
.login-card .brand-logo { width: 60px; height: 61px; }
.login-card .brand-name { font-size: 0.88rem; }
.home-title { font-size: 1.3rem; }

.topbar { position: relative; z-index: 5; background: var(--bar); border-bottom: 1px solid var(--line); padding: 10px 16px; }
.topbar-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

/* Nút và ô nhập */
.btn {
  min-height: 48px;
  padding: 10px 18px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover:not(:disabled) { background: var(--raised); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-submit { background: var(--submit); border-color: var(--submit); color: #fff; }
.btn-submit:hover:not(:disabled) { background: var(--submit-hover); border-color: var(--submit-hover); }
.btn-block { display: block; width: 100%; }
.btn-link {
  padding: 8px 0;
  border: 0;
  background: none;
  color: var(--primary);
  font: inherit;
  font-size: 0.94rem;
  text-decoration: underline;
  cursor: pointer;
}

.field label { display: block; margin-bottom: 6px; font-weight: 600; }
.field input {
  display: block;
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--raised);
  color: var(--ink);
  font: inherit;
}
.field input:disabled { opacity: 0.5; }
.form-error {
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--warn-tint);
  color: var(--warn);
  font-size: 0.94rem;
}
.notice {
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--primary-tint);
}

/* Trang chủ */
.identity-compact { display: block; }
@media (min-width: 560px) { .identity-compact { display: none; } }
.rules { margin: 0; padding-left: 20px; }
.rules li + li { margin-top: 8px; }

.finished { border-top: 1px solid var(--line); padding-top: 16px; text-align: center; }
.finished-title { font-size: 1.25rem; font-weight: 700; }
.finished-actions { display: grid; grid-template-columns: 1fr 1.6fr; gap: 10px; }

.result { border-top: 1px solid var(--line); padding-top: 16px; }
.result > * + * { margin-top: 12px; }
.result-score { text-align: center; }
.result-number { font-size: 1.5rem; color: var(--muted); }
.result-number strong { font-size: 3.5rem; line-height: 1.1; color: var(--ink); }
.result-caption { color: var(--muted); }
.result-recorded { font-weight: 600; }
.badge {
  display: inline-block;
  margin-top: 8px;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 0.94rem;
  font-weight: 600;
}
.badge-ok { background: var(--ok-tint); color: var(--ok); }
.facts { margin: 0; }
.facts > div {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}
.facts dt { color: var(--muted); }
.facts dd { margin: 0; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Màn hình làm bài ---------- */
.exam-page { position: relative; min-height: 100vh; min-height: 100dvh; }
.deco { display: none; }

.exam-shell { background: var(--shell); min-height: 100vh; min-height: 100dvh; padding-bottom: 96px; }

/* Điện thoại: hàng logo cuộn đi, chỉ đồng hồ và tiến độ bám trên đầu. */
.exam-top {
  position: sticky;
  top: -56px;
  z-index: 5;
  background: var(--bar);
}
.exam-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: 46px auto;
  grid-template-areas: "brand user" "clock clock";
  align-items: center;
  gap: 2px 8px;
  padding: 8px 12px 4px;
}
.exam-head .brand { grid-area: brand; gap: 8px; }
.exam-head .brand-logo { width: 38px; height: 39px; }
.exam-user { grid-area: user; display: flex; align-items: center; justify-content: flex-end; gap: 10px; }
.exam-head .clock { grid-area: clock; text-align: center; }

.clock {
  color: var(--primary);
  font-family: "SF Mono", "Cascadia Mono", Consolas, "Roboto Mono", ui-monospace, monospace;
  font-size: 1.6rem;
  font-weight: 500;
  letter-spacing: 0.06em;
  font-variant-numeric: tabular-nums;
}
.clock.is-warning { color: var(--warn); font-weight: 700; }

.progress { height: 3px; background: var(--line); }
.progress-fill { height: 100%; width: 0; background: var(--primary); transition: width 0.2s; }

.exam-sub { padding: 10px 14px; }
.exam-org, .exam-finish { display: none; }
.stats-row {
  display: grid;
  grid-template-columns: repeat(4, auto);
  justify-content: space-between;
  gap: 4px 10px;
}
.stat-item { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.stat-item strong { color: var(--ink); font-size: 1rem; font-weight: 500; font-variant-numeric: tabular-nums; }
.stat-item strong.tone-done { color: var(--done); }
.stat-item strong.tone-review { color: var(--review); }
.stat-item strong.tone-skipped { color: var(--skipped); }

.exam-body { padding: 0 14px; }
.exam-side { display: none; }
.exam-main {
  padding: 18px 16px;
  background: var(--card);
  border-radius: 16px;
}

/* Câu hỏi */
.question { margin: 0; padding: 0; border: 0; min-width: 0; }
.question legend,
.question-title {
  display: block;
  width: 100%;
  padding: 0;
  margin-bottom: 16px;
  font-size: 1.18rem;
  font-weight: 600;
  line-height: 1.4;
}
.options { display: grid; gap: 10px; }
.option {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 60px;
  padding: 10px 14px;
  border: 2px solid transparent;
  border-radius: 10px;
  background: var(--raised);
  cursor: pointer;
}
.option:hover { background: var(--raised-hover); }
.option input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.option:has(input:focus-visible) { outline: 3px solid var(--focus); outline-offset: 2px; }
.option-letter {
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--card);
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 600;
  line-height: 36px;
  text-align: center;
}
.option.is-selected { border-color: var(--primary); background: var(--primary-tint); }
.option.is-selected .option-letter { background: var(--primary); color: var(--on-primary); }
.option.is-selected .option-text { color: var(--primary); font-weight: 600; }

.prediction > * + * { margin-top: 12px; }
.prediction-row { display: flex; align-items: center; gap: 12px; }
.prediction-input {
  width: 180px;
  min-height: 64px;
  padding: 8px 14px;
  border: 2px solid var(--line-strong);
  border-radius: 10px;
  background: var(--raised);
  color: var(--ink);
  font: inherit;
  font-size: 2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.prediction-input:focus { border-color: var(--primary); }
.prediction-unit { font-size: 1.15rem; font-weight: 600; }

.question-foot { margin-top: 16px; }
.mark-row {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  color: var(--muted);
  cursor: pointer;
}
.mark-row input {
  flex: none;
  width: 22px;
  height: 22px;
  margin: 0;
  accent-color: var(--review);
  cursor: pointer;
}
.save-state { min-height: 1.4em; color: var(--muted); font-size: 0.85rem; }
.save-state.is-offline { color: var(--warn); font-weight: 600; }

/* Thanh chuyển câu cố định phía dưới trên điện thoại */
.exam-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--bar);
  border-top: 1px solid var(--line);
}
.btn-nav { padding: 10px 8px; }
.btn-next { border-color: var(--primary); color: var(--primary); }
.btn-next:hover:not(:disabled) { background: var(--primary-tint); }
.nav-overview { font-size: 0.88rem; font-weight: 500; white-space: nowrap; }

/* Bảng câu hỏi */
.grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 12px;
  justify-items: center;
}
.grid-cell {
  position: relative;
  width: 100%;
  max-width: 46px;
  aspect-ratio: 1;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 50%;
  background: var(--raised);
  color: var(--muted);
  font: inherit;
  font-size: 0.92rem;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.grid-cell:hover { background: var(--raised-hover); }
.grid-cell.is-done { border-color: var(--done); background: transparent; color: var(--ink); }
.grid-cell.is-review { border-color: var(--review); background: transparent; color: var(--ink); }
.grid-cell.is-skipped { border-color: var(--skipped); border-style: dashed; background: transparent; color: var(--ink); }
/* Dấu chấm để phân biệt "xem lại" không chỉ bằng màu */
.grid-cell.is-review::after {
  content: "";
  position: absolute;
  top: -3px;
  right: -3px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--review);
}
.grid-cell.is-current { border-color: var(--current-bg); background: var(--current-bg); color: var(--current-ink); font-weight: 700; }

.grid-legend {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 10px;
  margin: 14px 0;
  color: var(--muted);
  font-size: 0.82rem;
}
.legend-item { display: inline-flex; align-items: center; gap: 8px; }
.legend-dot {
  flex: none;
  width: 14px;
  height: 14px;
  border: 2px solid transparent;
  border-radius: 50%;
  background: var(--raised);
}
.legend-dot.is-done { border-color: var(--done); background: transparent; }
.legend-dot.is-review { background: var(--review); }
.legend-dot.is-skipped { border-color: var(--skipped); border-style: dashed; background: transparent; }

/* Hộp thoại */
.sheet {
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--card);
  color: var(--ink);
}
.sheet::backdrop { background: rgba(6, 12, 28, 0.6); }
.sheet > * + * { margin-top: 12px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }

/* Màn hình rộng: theo bố cục mẫu */
@media (min-width: 960px) {
  .exam-page { padding: 44px 32px; overflow-x: clip; }
  .exam-shell {
    position: relative;
    max-width: 1180px;
    min-height: 0;
    margin: 0 auto;
    padding-bottom: 44px;
    border-radius: 24px;
    box-shadow: var(--shadow);
  }
  .exam-top { top: 0; border-radius: 24px 24px 0 0; }
  .exam-head {
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    grid-template-rows: auto;
    grid-template-areas: "brand clock user";
    padding: 18px 36px;
  }
  .exam-head .brand { gap: 12px; }
  .exam-head .brand-logo { width: 52px; height: 53px; }
  .clock { font-size: 2.2rem; }
  .exam-sub {
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 22px 44px;
  }
  .exam-org {
    display: block;
    flex: 1;
    min-width: 0;
    color: var(--muted);
    font-size: 0.95rem;
  }
  .stats-row { display: flex; gap: 22px; }
  .stat-item { font-size: 0.92rem; }
  .stat-item strong { font-size: 1.2rem; }
  .exam-finish { display: block; flex: none; min-height: 44px; }
  .exam-body {
    display: grid;
    grid-template-columns: 320px minmax(0, 1fr);
    gap: 28px;
    align-items: start;
    padding: 0 44px;
  }
  .exam-side {
    display: block;
    padding: 24px 26px;
    background: var(--card);
    border-radius: 18px;
    box-shadow: var(--shadow);
  }
  .exam-side h2 { margin-bottom: 18px; font-size: 1.15rem; font-weight: 500; }
  .grid-legend { margin-bottom: 0; }
  .exam-main { padding: 30px 36px; border-radius: 18px; }
  .question legend, .question-title { margin-bottom: 22px; font-size: 1.3rem; font-weight: 500; }
  .options { grid-template-columns: 1fr 1fr; gap: 16px 20px; }
  .option { min-height: 68px; }
  .question-foot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-top: 24px;
  }
  .exam-nav {
    position: static;
    display: flex;
    flex: none;
    padding: 0;
    background: none;
    border: 0;
  }
  .btn-nav { min-width: 124px; min-height: 46px; padding: 8px 18px; }
  .nav-overview { display: none; }
  .deco { display: block; position: fixed; border-radius: 50%; pointer-events: none; }
  .deco-one { top: 6%; right: 7%; width: 76px; height: 76px; background: var(--primary); opacity: 0.55; filter: blur(2px); }
  .deco-two { top: 21%; left: 4%; width: 20px; height: 20px; background: var(--submit); opacity: 0.7; }
  .deco-three { bottom: 8%; left: 15%; width: 32px; height: 32px; background: var(--primary); opacity: 0.8; }
  .deco-four { bottom: 5%; right: 11%; width: 24px; height: 24px; background: var(--review); opacity: 0.8; }
}

/* Đủ rộng thì bảng câu hỏi nhô ra ngoài khung như mẫu */
@media (min-width: 1360px) {
  .exam-body { grid-template-columns: 228px minmax(0, 1fr); }
  .exam-side { width: 320px; margin-left: -92px; }
}

@media (prefers-reduced-motion: reduce) {
  .progress-fill { transition: none; }
}
