:root {
  --bg: #f5f5f5;
  --card-bg: #ffffff;
  --border: #a8aaad;
  --text: #121316;
  --muted: #3b3d42;
  --primary: #ffd100;
  --on-accent: #121316;
  --correct: #16a34a;
  --incorrect: #dc2626;
  --tint-bg: #fff3c4;
  --success-bg: #ecfdf3;
  --danger-bg: #fef2f2;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
  --panel-bg: #f5f5f5;
  --overlay: rgba(15, 18, 25, 0.5);
  --radius: 4px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121316;
    --card-bg: #3b3d42;
    --border: #a8aaad;
    --text: #f5f5f5;
    --muted: #a8aaad;
    --primary: #ffd100;
    --correct: #22c55e;
    --incorrect: #f87171;
    --tint-bg: rgba(255, 209, 0, 0.15);
    --success-bg: rgba(34, 197, 94, 0.15);
    --danger-bg: rgba(248, 113, 113, 0.15);
    --warn-bg: rgba(251, 191, 36, 0.15);
    --warn-text: #fbbf24;
    --panel-bg: #121316;
    --overlay: rgba(0, 0, 0, 0.65);
  }
}

:root[data-theme="dark"] {
  --bg: #121316;
  --card-bg: #3b3d42;
  --border: #a8aaad;
  --text: #f5f5f5;
  --muted: #a8aaad;
  --primary: #ffd100;
  --correct: #22c55e;
  --incorrect: #f87171;
  --tint-bg: rgba(255, 209, 0, 0.15);
  --success-bg: rgba(34, 197, 94, 0.15);
  --danger-bg: rgba(248, 113, 113, 0.15);
  --warn-bg: rgba(251, 191, 36, 0.15);
  --warn-text: #fbbf24;
  --panel-bg: #121316;
  --overlay: rgba(0, 0, 0, 0.65);
}

* { box-sizing: border-box; }

/* Native <button>/<input>/<select>/<textarea> don't inherit color/font from the
   body by default (browser UA stylesheet) — without this, any control that only
   sets `background` (most .answer-btn/.topic-btn/etc do) keeps hardcoded black
   text and stays unreadable once the theme goes dark. */
button, input, select, textarea {
  font: inherit;
  color: inherit;
}

input, select, textarea {
  background: var(--panel-bg);
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: 3rem;
}

/* Flat UI: no borders on interface elements. Elevation/separation comes from
   background contrast (--card-bg vs --bg/--panel-bg) and, where a control needs
   a visible focus affordance, an outline (never a border) on :focus-visible. */
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.topbar {
  background: var(--card-bg);
  padding: 1rem 1.25rem;
  position: sticky;
  top: 0;
  z-index: 10;
}

.tabs {
  display: flex;
  flex-direction: column;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.06);
  max-height: 400px;
  opacity: 1;
  margin-top: 0.5rem;
  transition: max-height 0.25s ease, opacity 0.2s ease, margin-top 0.25s ease, visibility 0s linear 0s;
}

.tab {
  border: none;
  background: var(--card-bg);
  color: var(--text);
  padding: 0.75rem 1rem;
  min-height: 44px;
  display: flex;
  align-items: center;
  text-align: left;
  cursor: pointer;
  font-size: 0.9rem;
}

.tab.active {
  background: var(--tint-bg);
  color: var(--text);
  font-weight: 600;
}

.menu-toggle {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.8rem;
  min-height: 44px;
  border: none;
  border-radius: var(--radius);
  background: var(--panel-bg);
  cursor: pointer;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.menu-toggle-icon {
  display: inline-block;
  transition: transform 0.2s ease;
}

.menu-toggle[aria-expanded="true"] .menu-toggle-icon {
  transform: rotate(180deg);
}

.tabs.collapsed {
  max-height: 0;
  opacity: 0;
  margin-top: 0;
  visibility: hidden;
  transition: max-height 0.25s ease, opacity 0.2s ease, margin-top 0.25s ease, visibility 0s linear 0.25s;
}

.topbar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.topbar h1 {
  margin: 0;
  font-size: 1.15rem;
}

.lang-toggle-group--topbar {
  display: inline-flex;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--panel-bg);
  flex-shrink: 0;
}

.lang-toggle-group--topbar .lang-opt {
  border: none;
  border-radius: 0;
  background: transparent;
  padding: 0.3rem 0.7rem;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  color: var(--muted);
}

.lang-toggle-group--topbar .lang-opt.active {
  background: var(--primary);
  color: var(--on-accent);
}

.topbar-controls {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  flex: 1 1 auto;
  min-width: 0;
}

/* Keeps the theme selector pinned to the topbar's right edge at any viewport
   width, including when this row wraps onto its own line below the title. */
#theme-toggle-topbar {
  margin-left: auto;
}

#app {
  max-width: 900px;
  margin: 1.25rem auto;
  padding: 0 1rem;
}

.card, .question-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  padding: 1rem 1.1rem;
  margin-bottom: 1rem;
}

.q-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.4rem;
}

.topic-tag {
  font-size: 0.75rem;
  color: var(--muted);
}

.badge {
  font-size: 0.7rem;
  background: var(--tint-bg);
  color: var(--text);
  padding: 0.1rem 0.5rem;
  border-radius: var(--radius);
}

.badge.new { background: var(--warn-bg); color: var(--warn-text); }

.q-image {
  max-width: 100%;
  border-radius: var(--radius);
  margin: 0.5rem auto;
  display: block;
}

.q-image-fallback {
  margin: 0.5rem auto;
  padding: 0.75rem;
  background: var(--panel-bg);
  border-radius: var(--radius);
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}

.lang-ru { font-weight: 500; }
.lang-es { color: var(--muted); font-style: italic; }
.lang-es-secondary { display: block; margin-top: 0.2rem; font-size: 0.85em; }

.answers {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.6rem;
}

.answer-btn {
  text-align: left;
  padding: 0.55rem 0.75rem;
  border: none;
  border-radius: var(--radius);
  background: var(--panel-bg);
  cursor: pointer;
  font-size: 0.95rem;
  transition: opacity 0.2s ease, background-color 0.2s ease;
}

.answer-btn:disabled { cursor: default; }
.answer-btn.correct { background: var(--success-bg); }
.answer-btn.incorrect { background: var(--danger-bg); }
.answer-btn.muted { opacity: 0.55; }
.answer-btn.static { cursor: default; }
.answer-btn.selected { background: var(--tint-bg); }

.answer-btn.reveal.correct { animation: pulse-correct 0.5s ease-out; }
.answer-btn.reveal.incorrect { animation: shake 0.3s ease-in-out; }

@keyframes pulse-correct {
  0% { box-shadow: 0 0 0 0 rgba(22, 163, 74, 0.35); }
  100% { box-shadow: 0 0 0 8px rgba(22, 163, 74, 0); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.explanation {
  margin-top: 0.75rem;
  padding: 0.6rem 0.75rem;
  background: var(--panel-bg);
  border-left: 3px solid var(--primary);
  border-radius: var(--radius);
  font-size: 0.9rem;
  animation: fade-in 0.25s ease-out;
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.hidden { display: none !important; }

.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.stat-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  padding: 0.75rem;
  text-align: center;
}

.stat-card strong { display: block; font-size: 1.4rem; }
.stat-card span { font-size: 0.75rem; color: var(--muted); }

.callout {
  background: var(--tint-bg);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  font-size: 0.95rem;
}

.callout.error { background: var(--danger-bg); color: var(--incorrect); }

.practice {
  display: flex;
  gap: 1.25rem;
  align-items: flex-start;
}

.topic-sidebar {
  width: 260px;
  flex-shrink: 0;
}

.topic-sidebar ul { list-style: none; margin: 0; padding: 0; }
.topic-sidebar li { margin-bottom: 0.3rem; }

.topic-btn {
  width: 100%;
  text-align: left;
  padding: 0.5rem 0.7rem;
  border: none;
  background: var(--card-bg);
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
}

.topic-btn.active { background: var(--tint-bg); color: var(--text); }
.topic-btn .count { color: var(--muted); font-size: 0.75rem; }

.topic-content { flex: 1; min-width: 0; }

.topic-picker-mobile { display: none; }

.primary-btn {
  background: var(--primary);
  color: var(--on-accent);
  border: none;
  padding: 0.6rem 1.2rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.95rem;
  margin: 0.75rem 0;
}

.danger-btn {
  background: var(--incorrect);
  color: white;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  cursor: pointer;
  margin-top: 1.5rem;
}

.field { display: flex; flex-direction: column; gap: 0.4rem; margin-bottom: 1rem; max-width: 320px; }
.field input[type="number"] { padding: 0.4rem; border: none; border-radius: var(--radius); }
.hint { font-size: 0.8rem; color: var(--muted); margin-top: -0.5rem; }

.lang-toggle-group { display: flex; gap: 0.4rem; }
.lang-opt {
  border: none;
  background: var(--panel-bg);
  padding: 0.35rem 0.8rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.85rem;
}
.lang-opt.active { background: var(--primary); color: var(--on-accent); }

.field-row { display: flex; gap: 0.6rem; margin-bottom: 1rem; }
.field-row button {
  padding: 0.5rem 0.9rem;
  border: none;
  border-radius: var(--radius);
  background: var(--panel-bg);
  cursor: pointer;
}

.dict-form { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.dict-form input {
  flex: 1;
  min-width: 160px;
  padding: 0.5rem;
  border: none;
  border-radius: var(--radius);
}
.dict-form button {
  background: var(--primary);
  color: var(--on-accent);
  border: none;
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  cursor: pointer;
}

.dict-list { list-style: none; margin: 0; padding: 0; }
.dict-list li {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 0.6rem 0.8rem;
  margin-bottom: 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.dict-topic { font-size: 0.7rem; color: var(--muted); margin-left: auto; }
.dict-remove {
  border: none;
  background: transparent;
  color: var(--incorrect);
  cursor: pointer;
  font-size: 1rem;
}

.selection-toolbar {
  position: absolute;
  z-index: 50;
}
.selection-toolbar button {
  background: #121316;
  color: #f5f5f5;
  border: none;
  padding: 0.4rem 0.7rem;
  border-radius: var(--radius);
  font-size: 0.8rem;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}

@media (max-width: 640px) {
  .practice { flex-direction: column; }
  .topic-sidebar { display: none; }
  .topic-picker-mobile {
    display: block;
    margin-bottom: 1rem;
  }
  .topic-picker-mobile summary {
    display: flex;
    justify-content: space-between;
    align-items: center;
    min-height: 44px;
    padding: 0.5rem 0.7rem;
    border-radius: var(--radius);
    background: var(--card-bg);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
    font-size: 0.95rem;
    cursor: pointer;
    list-style: none;
  }
  .topic-picker-mobile summary::-webkit-details-marker { display: none; }
  .topic-picker-mobile summary::after {
    content: "▾";
    color: var(--muted);
    margin-left: 0.5rem;
    transition: transform 0.15s ease;
  }
  .topic-picker-mobile[open] summary {
    border-radius: var(--radius) var(--radius) 0 0;
    box-shadow: none;
  }
  .topic-picker-mobile[open] summary::after { transform: rotate(180deg); }
  .topic-picker-mobile ul {
    list-style: none;
    margin: 0;
    padding: 0.4rem;
    max-height: 50vh;
    overflow-y: auto;
    background: var(--card-bg);
    border-radius: 0 0 var(--radius) var(--radius);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  }
  .topic-picker-mobile li { margin-bottom: 0.3rem; }

  .dict-form { flex-direction: column; }
  .dict-form input, .dict-form button { width: 100%; }

  .topbar { padding: 0.6rem 0.9rem; }
  .topbar-head { margin-bottom: 0.4rem; }
  .topbar h1 { font-size: 0.88rem; }
  .lang-toggle-group--topbar .lang-opt { padding: 0.3rem 0.55rem; }
}

.view-transition-slide {
  animation: view-enter 0.45s ease-out;
}

.view-transition-fade {
  animation: fade-only 0.4s ease-out;
}

@keyframes view-enter {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fade-only {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Native View Transitions (language/theme/view switches): each .question-card and the
   active lang/theme pill keep a stable view-transition-name (see app.js), so the browser
   morphs their box smoothly to the new size/position instead of blanking the screen and
   popping in a new size. The old/new image-pair duration is kept equal to the group's so
   the text cross-fade finishes exactly when the box resize does — a mismatch here is what
   made the boxes look like they were lagging behind the rest of the page. */
::view-transition-group(*) {
  animation-duration: 0.35s;
  animation-timing-function: ease-out;
}

::view-transition-old(*),
::view-transition-new(*) {
  animation-duration: 0.35s;
  animation-timing-function: ease-out;
}

/* The active lang/theme segment only *looks* clipped to the container's shape because its
   parent (.lang-toggle-group--topbar) clips it with overflow:hidden — the segment itself has
   border-radius:0. View Transition snapshots render in a top-layer overlay outside that
   parent, so without this the sliding highlight shows its real square corners mid-animation. */
::view-transition-group(lang-active-pill),
::view-transition-group(theme-active-pill) {
  border-radius: var(--radius);
  overflow: hidden;
}

@media (prefers-reduced-motion: reduce) {
  .view-transition-slide, .view-transition-fade,
  .explanation, .answer-btn.reveal.correct, .answer-btn.reveal.incorrect, .tabs, .menu-toggle-icon,
  .modal {
    animation: none !important;
    transition: none !important;
  }
}

.stats-block { margin-bottom: 1.75rem; }
.stats-block h3 { margin-bottom: 0.6rem; }

.box-bars { display: flex; flex-direction: column; gap: 0.5rem; }

.box-bar-row {
  display: grid;
  grid-template-columns: 140px 1fr 2.5rem;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.85rem;
}

.box-bar-track {
  background: var(--panel-bg);
  border-radius: var(--radius);
  overflow: hidden;
  height: 0.75rem;
}

.box-bar-fill {
  height: 100%;
  background: var(--muted);
  border-radius: var(--radius);
}

.box-bar-fill.box-0 { background: var(--muted); opacity: 0.6; }
.box-bar-fill.box-1, .box-bar-fill.box-2 { background: var(--muted); }
.box-bar-fill.box-3, .box-bar-fill.box-4 { background: var(--primary); }
.box-bar-fill.box-5 { background: var(--correct); }

.box-bar-count { text-align: right; color: var(--muted); }

.stats-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.stats-table th, .stats-table td {
  text-align: left;
  padding: 0.45rem 0.6rem;
}
.stats-table th { color: var(--muted); font-weight: 600; }
.stats-table tbody tr:nth-child(even) { background: var(--panel-bg); }

.exam-history { list-style: none; margin: 1rem 0 0; padding: 0; }
.exam-history-item {
  width: 100%;
  text-align: left;
  display: flex;
  justify-content: space-between;
  padding: 0.6rem 0.8rem;
  border: none;
  background: var(--card-bg);
  border-radius: var(--radius);
  cursor: pointer;
  margin-bottom: 0.4rem;
  font-size: 0.85rem;
}
.exam-pass { color: var(--correct); font-weight: 600; }
.exam-fail { color: var(--incorrect); font-weight: 600; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}

.modal-overlay.hidden { display: none !important; }

.modal {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 1.25rem;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
  animation: modal-in 0.2s ease-out;
}

.modal h3 { margin-top: 0; }

.modal-input {
  width: 100%;
  padding: 0.5rem;
  border: none;
  border-radius: var(--radius);
  margin: 0.5rem 0 1rem;
  background: var(--panel-bg);
  color: var(--text);
  font-size: 0.95rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.modal-btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--radius);
  background: var(--panel-bg);
  color: var(--text);
  cursor: pointer;
  font-size: 0.9rem;
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
