:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eef2f8;
  --text: #17202e;
  --muted: #5b6678;
  --border: #d6dde8;
  --primary: #153c6e;
  --primary-contrast: #ffffff;
  --accent: #ff9f1c;
  --ok: #1f8a4c;
  --ok-bg: #e3f5ea;
  --bad: #c0392b;
  --bad-bg: #fbe6e3;
  --warn-bg: #fff4dd;
  --focus: #2f7de1;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141c;
    --surface: #18202c;
    --surface-2: #212b39;
    --text: #e8edf4;
    --muted: #9aa6b8;
    --border: #2d394a;
    --primary: #5b9be6;
    --primary-contrast: #0b1320;
    --ok: #4cc27f;
    --ok-bg: #173526;
    --bad: #ff7a6b;
    --bad-bg: #3a1d1a;
    --warn-bg: #3a3018;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 17px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  padding-bottom: env(safe-area-inset-bottom);
}
img { max-width: 100%; height: auto; }
a { color: var(--primary); }
h1 { font-size: 1.5rem; line-height: 1.25; margin: .2em 0 .6em; }
h2 { font-size: 1.2rem; margin: 1.2em 0 .5em; }
h3 { font-size: 1.05rem; margin: 1em 0 .4em; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  background: #153c6e; color: #fff;
}
.brand { display: flex; align-items: center; gap: 8px; color: #fff; text-decoration: none; font-weight: 700; }
.brand img { border-radius: 6px; }
.mainnav { display: flex; gap: 4px; flex-wrap: wrap; margin-left: auto; }
.mainnav a, .mainnav button {
  color: #fff; text-decoration: none; padding: 6px 10px; border-radius: 8px;
  background: transparent; border: 0; font: inherit; font-size: .95rem; cursor: pointer;
}
.mainnav a[aria-current="page"], .mainnav a:hover, .mainnav button:hover { background: rgba(255, 255, 255, .16); }
main { max-width: 860px; margin: 0 auto; padding: 16px; outline: none; }
.offline-banner { background: var(--warn-bg); color: var(--text); padding: 8px 16px; text-align: center; font-size: .9rem; }
.loading { color: var(--muted); }

/* ---------- components ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin: 0 0 16px;
}
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.tile {
  display: flex; flex-direction: column; gap: 4px; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; color: var(--text); text-decoration: none; box-shadow: var(--shadow);
  font: inherit; cursor: pointer;
}
.tile:hover { border-color: var(--primary); }
.tile strong { font-size: 1.05rem; }
.tile span { color: var(--muted); font-size: .9rem; }
.tile .icon { font-size: 1.6rem; line-height: 1; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 18px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:hover { border-color: var(--primary); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); color: var(--primary-contrast); border-color: var(--primary); }
.btn.ok { background: var(--ok); color: #fff; border-color: var(--ok); }
.btn.bad { background: var(--bad); color: #fff; border-color: var(--bad); }
.btn.small { min-height: 34px; padding: 4px 10px; font-size: .9rem; }
.btn.link { border: 0; background: none; color: var(--primary); padding: 4px; min-height: 0; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
.actions.spread { justify-content: space-between; }

form .field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
label { font-weight: 600; font-size: .95rem; }
input[type=text], input[type=password], select, textarea {
  font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); width: 100%;
}
textarea { min-height: 160px; resize: vertical; }
.checkbox { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.checkbox input { width: 20px; height: 20px; }
fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px 12px; margin: 0 0 12px; }
legend { font-weight: 600; padding: 0 4px; }
.radio-row { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.radio-row label { font-weight: 500; display: flex; align-items: center; gap: 6px; }

.alert { padding: 10px 14px; border-radius: 10px; margin: 0 0 12px; }
.alert.error { background: var(--bad-bg); color: var(--bad); }
.alert.ok { background: var(--ok-bg); color: var(--ok); }
.alert.info { background: var(--warn-bg); }
.muted { color: var(--muted); }
.small { font-size: .88rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

.badge {
  display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: .78rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.new { background: var(--surface-2); color: var(--muted); }
.badge.learning { background: var(--warn-bg); color: #8a5a00; }
.badge.weak { background: var(--bad-bg); color: var(--bad); }
.badge.mastered { background: var(--ok-bg); color: var(--ok); }
.badge.learned { background: #e6eefb; color: var(--primary); }
@media (prefers-color-scheme: dark) {
  .badge.learning { color: #ffcf70; }
  .badge.learned { background: #1c2c44; }
}

.progress { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; display: flex; }
.progress > span { display: block; height: 100%; }
.progress .p-mastered { background: var(--ok); }
.progress .p-learning { background: var(--accent); }
.progress .p-weak { background: var(--bad); }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: .85rem; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: middle; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.stat b { display: block; font-size: 1.4rem; }
.stat span { color: var(--muted); font-size: .85rem; }

.list { list-style: none; padding: 0; margin: 0; }
.list li { border-bottom: 1px solid var(--border); }
.list li:last-child { border-bottom: 0; }
.list a, .list .row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 4px; color: var(--text); text-decoration: none;
}
.list a:hover { background: var(--surface-2); }

/* ---------- question display ---------- */
.q-meta { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; color: var(--muted); font-size: .88rem; margin-bottom: 6px; }
.q-text { font-size: 1.2rem; font-weight: 700; margin: 0 0 12px; }
.q-images { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); margin: 12px 0; }
.q-images.single { grid-template-columns: 1fr; }
.q-images figure { margin: 0; background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 6px; }
.q-images img { display: block; margin: 0 auto; max-height: 420px; object-fit: contain; cursor: zoom-in; image-rendering: auto; }
.q-images img.pixel { image-rendering: pixelated; max-height: 120px; }
.answer { background: var(--surface-2); border-left: 4px solid var(--ok); border-radius: 8px; padding: 12px 14px; margin-top: 12px; }
.answer h3 { margin-top: 0; }
.answer ul { margin: 0; padding-left: 1.2em; }
.answer li { margin: 4px 0; }
.short { font-weight: 600; }
.note { font-size: .88rem; color: var(--muted); font-style: italic; }

.choices { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }
.choice {
  text-align: left; padding: 12px 14px; border-radius: 10px; border: 2px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; cursor: pointer; min-height: 48px;
}
.choice:hover:enabled { border-color: var(--primary); }
.choice.correct { border-color: var(--ok); background: var(--ok-bg); }
.choice.wrong { border-color: var(--bad); background: var(--bad-bg); }
.choice:disabled { cursor: default; }

.kw { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.kw span { padding: 2px 8px; border-radius: 6px; font-size: .85rem; }
.kw .hit { background: var(--ok-bg); color: var(--ok); }
.kw .miss { background: var(--bad-bg); color: var(--bad); }

.result-banner { padding: 12px 14px; border-radius: 10px; font-weight: 700; margin-top: 12px; }
.result-banner.ok { background: var(--ok-bg); color: var(--ok); }
.result-banner.bad { background: var(--bad-bg); color: var(--bad); }

.exam-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.exam-progress { flex: 1; height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.exam-progress span { display: block; height: 100%; background: var(--primary); }

table { width: 100%; border-collapse: collapse; font-size: .95rem; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .85rem; color: var(--muted); }
.table-wrap { overflow-x: auto; }

.lightbox {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .88); z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 16px; cursor: zoom-out;
}
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; background: #fff; }

.password-box { font-family: ui-monospace, Menlo, Consolas, monospace; background: var(--surface-2); padding: 8px 12px; border-radius: 8px; user-select: all; word-break: break-all; }

.login-wrap { max-width: 420px; margin: 8vh auto 0; }
.login-wrap .logo { display: block; margin: 0 auto 12px; width: 88px; height: 88px; }
.login-wrap h1 { text-align: center; }

@media (max-width: 600px) {
  body { font-size: 16px; }
  main { padding: 12px; }
  .topbar { padding: 6px 10px; }
  .brand span { display: none; }
  .mainnav { margin-left: 0; flex: 1; justify-content: flex-end; }
  .mainnav a, .mainnav button { padding: 6px 7px; font-size: .88rem; }
  .q-text { font-size: 1.1rem; }
}
.grow { flex: 1; min-width: 0; }
.progress { width: 100%; }
.actions > .progress { flex: 1 1 100%; }
.legend .p-mastered { background: var(--ok); }
.legend .p-learning { background: var(--accent); }
.legend .p-weak { background: var(--bad); }
.card > h2:first-child, .card > form > h2:first-child { margin-top: 0; }
.list .row .progress { margin-top: 6px; }
.card-question { border-left: 4px solid var(--primary); }
.card-question.replacement { border-left-color: var(--bad); }
.card-text { font-size: 1.1rem; font-weight: 600; margin: 4px 0 0; }
.cards-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.card-tile { align-items: flex-start; }
