/* ===== Tokens ===== */
:root {
  color-scheme: light;
  --bg: #F7F4EB;
  --surface: #FFFDF7;
  --surface-2: #F0EDE3;
  --line: #E6DFD0;
  --text: #263328;
  --mut: #6E796C;
  --accent: #35744B;
  --accent-ink: #FFFFFF;
  --accent-soft: #E8F1E4;
  --ok: #287746;
  --ok-bg: #E5F1E6;
  --bad: #B3261E;
  --bad-bg: #FBEAEA;
  --figure: #FFFFFF;
  --radius: 14px;
  --tabbar-h: 64px;
  font-family: "Segoe UI", Roboto, "SF Pro Text", -apple-system, system-ui, "Helvetica Neue", Arial, sans-serif;
}

:root[data-theme="energy"] {
  color-scheme: dark;
  --bg: #101B2C;
  --surface: #18263A;
  --surface-2: #203149;
  --line: #293B53;
  --text: #F2F6FC;
  --mut: #A7B6CA;
  --accent: #B5F34A;
  --accent-ink: #14200B;
  --accent-soft: #253B36;
  --ok: #8BE09C;
  --ok-bg: #1C382E;
  --bad: #FF938D;
  --bad-bg: #48292D;
  --figure: #F1F2EE;
}

/* ===== Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; background: var(--bg); scroll-padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); }
html:has(body.in-quiz) { scroll-padding-bottom: 24px; }
body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}
body.in-quiz { padding-bottom: calc(env(safe-area-inset-bottom) + 24px); }
button, select { font: inherit; color: inherit; touch-action: manipulation; }
button { cursor: pointer; background: none; border: 0; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
img { display: block; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; scroll-behavior: auto !important; } }

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px 0;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

h1 { font-size: 24px; line-height: 1.25; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 17px; font-weight: 650; }
.muted { color: var(--mut); }
.small { font-size: 14px; }

/* ===== Top bar ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; flex: 1; min-width: 0; }
.brand span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.quiz-progress { flex: 1; display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--mut); font-variant-numeric: tabular-nums; }
.bar { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .25s ease; }

.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border-radius: 10px;
  color: var(--text);
  flex-shrink: 0;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg, .tabbar svg, .row-link svg { width: 22px; height: 22px; }

.lang-select {
  appearance: none;
  -webkit-appearance: none;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 14px;
  font-weight: 600;
  min-height: 44px;
  min-width: 52px;
}

/* ===== Tab bar ===== */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 20;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
body.in-quiz .tabbar { display: none; }
.tabbar button {
  height: var(--tabbar-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  font-size: 12px; font-weight: 600;
  color: var(--mut);
  position: relative;
}
.tabbar button[aria-current="page"] { color: var(--accent); }
.tabbar .count {
  position: absolute; top: 8px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--bad); color: #fff;
  font-size: 11px; line-height: 18px; text-align: center;
}

/* ===== Blocks ===== */
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}
.section { display: flex; flex-direction: column; gap: 10px; }
.section > h2 { padding: 0 2px; }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 50px;
  padding: 12px 18px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 16px;
  font-weight: 650;
  width: 100%;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn:disabled { opacity: .5; cursor: default; }
.btn:active:not(:disabled) { transform: translateY(1px); }

/* Home: continue */
.continue { display: grid; grid-template-columns: 56px 1fr; gap: 14px; align-items: center; }
.continue .sign { width: 56px; height: 56px; object-fit: contain; }
.continue .btn { grid-column: 1 / -1; }
.meter { height: 6px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 4px; overflow: hidden; margin-top: 8px; }
.meter i { display: block; height: 100%; background: var(--accent); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.stats div { background: var(--surface); padding: 12px; }
.stats b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.stats span { font-size: 13px; color: var(--mut); }

/* Lists of rows (topics, actions, settings) */
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.row-link {
  width: 100%;
  display: grid;
  grid-template-columns: 40px 1fr auto;
  gap: 14px;
  align-items: center;
  padding: 12px 14px;
  text-align: left;
  border-top: 1px solid var(--line);
}
.list > :first-child { border-top: 0; }
.row-link:active { background: var(--surface-2); }
.row-link .sign { width: 40px; height: 40px; object-fit: contain; }
.row-link .glyph { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 10px; background: var(--accent-soft); color: var(--accent); }
.row-title { font-weight: 600; line-height: 1.3; }
.row-sub { font-size: 13px; color: var(--mut); margin-top: 2px; font-variant-numeric: tabular-nums; }
.row-end { color: var(--mut); font-size: 14px; font-variant-numeric: tabular-nums; }
.row-link .meter { margin-top: 6px; }
.row-link[aria-disabled="true"] .row-title { color: var(--mut); }

/* ===== Question ===== */
.figure {
  background: var(--figure);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  display: grid;
  place-items: center;
}
.figure.sign { padding: 24px; }
.figure.sign img { width: 168px; height: 168px; object-fit: contain; }
.figure.scene img { width: 100%; height: auto; }

.question { font-size: 19px; line-height: 1.38; font-weight: 650; }
.tags { display: flex; gap: 6px; flex-wrap: wrap; }
.tag { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); }

.opts { display: flex; flex-direction: column; gap: 8px; }
.opt {
  display: grid;
  grid-template-columns: 32px 1fr;
  gap: 12px;
  align-items: center;
  width: 100%;
  min-height: 56px;
  padding: 10px 14px;
  text-align: left;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: 10px;
  font-size: 16px;
  line-height: 1.35;
}
.opt .key {
  width: 32px; height: 32px;
  display: grid; place-items: center;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  font-weight: 700; font-size: 14px;
}
.opt:active:not(:disabled) { background: var(--surface-2); }
.opt:disabled { cursor: default; color: var(--text); }
.opt.correct { border-color: var(--ok); background: var(--ok-bg); }
.opt.correct .key { background: var(--ok); border-color: var(--ok); color: #fff; }
.opt.incorrect { border-color: var(--bad); background: var(--bad-bg); }
.opt.incorrect .key { background: var(--bad); border-color: var(--bad); color: #fff; }

.explain { border-left: 4px solid var(--bad); }
.explain.ok { border-left-color: var(--ok); }
.explain h2 { margin-bottom: 6px; }
.explain.ok h2 { color: var(--ok); }
.explain:not(.ok) h2 { color: var(--bad); }
.explain .source { margin-top: 10px; font-size: 13px; color: var(--mut); }
.explain .source a { color: var(--accent); }
.next-btn { scroll-margin-bottom: 24px; }

/* Result */
.result { text-align: center; display: flex; flex-direction: column; gap: 14px; align-items: center; padding: 28px 16px; }
.result .score { font-size: 44px; font-weight: 750; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.result .verdict { font-weight: 650; }
.result .verdict.ok { color: var(--ok); }
.result .verdict.bad { color: var(--bad); }
.actions { display: grid; gap: 8px; width: 100%; }

/* Exam rules */
.rules { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.rules div { background: var(--surface); padding: 14px; }
.rules b { display: block; font-size: 24px; font-variant-numeric: tabular-nums; }
.rules span { font-size: 13px; color: var(--mut); }

.notice { background: var(--accent-soft); color: var(--text); border-radius: 10px; padding: 12px 14px; font-size: 15px; }
.empty { text-align: center; padding: 32px 16px; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.empty img { width: 64px; height: 64px; margin-bottom: 6px; }

/* Settings */
.field { display: flex; flex-direction: column; gap: 8px; }
.field label { font-weight: 600; }
.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 3px; }
.segmented button { min-height: 44px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--mut); padding: 0 6px; }
.segmented.two-rows { grid-auto-flow: row; grid-template-columns: 1fr 1fr; }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.danger { color: var(--bad); }

/* ===== Друга мова ===== */
.second { display: block; color: var(--mut); font-size: .9em; font-weight: 500; margin-top: 3px; line-height: 1.35; }
.second.block { margin-top: 6px; }
.question-second { font-size: 16px; margin-top: -4px; }
.opt-text { display: block; }
.opt.with-img { grid-template-columns: 32px 56px 1fr; }
.opt-sign { width: 56px; height: 56px; object-fit: contain; }
.opt.chosen { border-color: var(--accent); background: var(--accent-soft); }
.opt.chosen .key { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ===== Теорія ===== */
.section-title { font-size: 15px; font-weight: 700; color: var(--mut); margin: 6px 2px -2px; }
.lesson-head { display: grid; grid-template-columns: 52px 1fr; gap: 12px; align-items: center; }
.lesson-head .sign { width: 52px; height: 52px; object-fit: contain; }
.lesson-card { display: flex; flex-direction: column; gap: 10px; }
.lesson-title { display: grid; grid-template-columns: auto 1fr; gap: 12px; align-items: center; }
.lesson-title .sign { width: 44px; height: 44px; object-fit: contain; }
.lesson-title:not(:has(.sign)) { grid-template-columns: 1fr; }
.lesson-card h2 { font-size: 17px; }
.lesson-card > p { line-height: 1.5; }
.terms { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.terms-label { font-size: 12px; font-weight: 700; color: var(--mut); text-transform: uppercase; letter-spacing: .04em; width: 100%; }
.term { font-size: 13px; padding: 4px 9px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--line); }
.term span { font-weight: 650; }
.lesson-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--mut); border-top: 1px solid var(--line); padding-top: 10px; }
.badge-serious, .badge-light { font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 6px; white-space: nowrap; }
.badge-serious { background: var(--bad-bg); color: var(--bad); }
.badge-light { background: var(--surface-2); color: var(--mut); border: 1px solid var(--line); }
.serious-note { margin-top: 8px; font-size: 14px; font-weight: 600; color: var(--bad); }

/* ===== Каталог знаків ===== */
.sign-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.sign-tile { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.sign-tile summary { list-style: none; display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 14px 10px 12px; cursor: pointer; text-align: center; min-height: 44px; }
.sign-tile summary::-webkit-details-marker { display: none; }
.sign-tile summary .sign { width: 72px; height: 72px; object-fit: contain; flex-shrink: 0; }
.sign-name { display: flex; flex-direction: column; gap: 2px; font-size: 14px; line-height: 1.3; }
.sign-name b { font-size: 12px; color: var(--mut); letter-spacing: .04em; }
.sign-tile[open] { grid-column: 1 / -1; }
.sign-tile[open] summary { flex-direction: row; text-align: left; gap: 14px; }
.sign-tile > p, .sign-tile > .second { padding: 0 14px 14px; font-size: 15px; line-height: 1.45; }
.sign-tile > .second { margin-top: -8px; }

/* ===== Іспит ===== */
.timer-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.timer { height: 8px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 5px; overflow: hidden; }
.timer i { display: block; height: 100%; width: 100%; background: var(--accent); transition: width .1s linear; }
.timer i.low { background: var(--bad); }
.timer-label { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; min-width: 36px; text-align: right; }
.exam-counts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; width: 100%; }
.exam-counts div { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px; }
.exam-counts b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.exam-counts span { font-size: 13px; color: var(--mut); }
.row-static { display: flex; justify-content: space-between; gap: 10px; padding: 12px 14px; border-top: 1px solid var(--line); font-size: 15px; }
.row-static b.ok { color: var(--ok); }
.row-static b.bad { color: var(--bad); }
.review-card { display: flex; flex-direction: column; gap: 6px; }
.review-head { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: start; }
.review-head:not(:has(.sign)) { grid-template-columns: 1fr auto; }
.review-head .sign { width: 40px; height: 40px; object-fit: contain; }
.review-head h3 { font-size: 15px; line-height: 1.35; }
.review-bad { color: var(--bad); font-size: 14px; font-weight: 600; }
.review-ok { color: var(--ok); font-size: 14px; font-weight: 600; }

@media (min-width: 700px) {
  .figure.sign img { width: 200px; height: 200px; }
}

/* Desktop: retain the compact bottom navigation on phones, use a side rail on wide screens. */
@media (min-width: 900px) {
  body { padding-bottom: 24px; }
  main { max-width: 1080px; margin: 0 auto 0 244px; padding: 28px 32px 32px; }
  .topbar { padding-left: 260px; padding-right: 28px; }
  .tabbar {
    top: 0; right: auto; bottom: 0; left: 0;
    width: 220px;
    display: flex; flex-direction: column; align-items: stretch; justify-content: flex-start;
    gap: 6px; padding: 22px 14px;
    border-top: 0; border-right: 1px solid var(--line);
  }
  .tabbar button {
    height: 48px; min-height: 48px; flex-direction: row; justify-content: flex-start;
    gap: 12px; padding: 0 14px; border-radius: 10px; font-size: 14px;
  }
  .tabbar button[aria-current="page"] { background: var(--accent-soft); }
  .tabbar .count { top: 14px; left: auto; right: 12px; }
  .tabbar svg { width: 20px; height: 20px; }
  body.in-quiz main { margin-left: auto; margin-right: auto; }
  body.in-quiz .topbar { padding-left: 20px; }
}
