:root {
  --ink: #1a2332;
  --muted: #5c6b7a;
  --paper: #f3efe6;
  --panel: rgba(255, 252, 246, 0.92);
  --line: #c9bba3;
  --accent: #0f6e56;
  --accent-deep: #0a4f3d;
  --warn: #9a3412;
  --good: #166534;
  --bad: #991b1b;
  --good-bg: #dcfce7;
  --bad-bg: #fee2e2;
  --shadow: 0 18px 40px rgba(26, 35, 50, 0.08);
  --radius: 14px;
  --font: "IBM Plex Sans", system-ui, sans-serif;
  --display: "Fraunces", Georgia, serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  color: var(--ink);
  background: var(--paper);
}

.bg-grid {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(ellipse 80% 50% at 10% -10%, rgba(15, 110, 86, 0.12), transparent 55%),
    radial-gradient(ellipse 60% 40% at 100% 0%, rgba(154, 52, 18, 0.08), transparent 50%),
    linear-gradient(rgba(201, 187, 163, 0.35) 1px, transparent 1px),
    linear-gradient(90deg, rgba(201, 187, 163, 0.35) 1px, transparent 1px);
  background-size: auto, auto, 28px 28px, 28px 28px;
}

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem 1.75rem;
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
  background: rgba(243, 239, 230, 0.75);
}

.top-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1.25rem;
}

.lang-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.2rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.lang-select {
  font-family: var(--font), "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", sans-serif;
  font-size: 0.9rem;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.4rem 0.65rem;
  min-width: 13rem;
  cursor: pointer;
}

html[dir="rtl"] .lang-wrap {
  align-items: flex-start;
}

.brand-mark {
  font-family: var(--display);
  font-weight: 700;
  font-size: 1.75rem;
  letter-spacing: -0.02em;
  color: var(--accent-deep);
  display: block;
}

.brand-sub {
  color: var(--muted);
  font-size: 0.95rem;
}

.stats-bar {
  display: flex;
  gap: 1.5rem;
}

.stat {
  text-align: right;
}

.stat-val {
  display: block;
  font-family: var(--mono);
  font-weight: 500;
  font-size: 1.35rem;
}

.stat-lbl {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.layout {
  display: grid;
  grid-template-columns: minmax(200px, 240px) minmax(0, 1fr) minmax(200px, 260px);
  gap: 1.25rem;
  padding: 1.25rem 1.75rem 2.5rem;
  max-width: 1280px;
  margin: 0 auto;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.15rem 1.2rem;
}

.panel h2 {
  font-family: var(--display);
  font-size: 1.15rem;
  margin: 0 0 0.35rem;
}

.muted {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 0 1rem;
  line-height: 1.4;
}

.topic, .topic-list .topic {
  display: block;
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  font: inherit;
  color: var(--ink);
  cursor: pointer;
  margin-bottom: 0.25rem;
}

.topic:hover { background: rgba(15, 110, 86, 0.08); }
.topic.active {
  background: rgba(15, 110, 86, 0.14);
  border-color: rgba(15, 110, 86, 0.35);
  font-weight: 600;
}

.topic .m {
  float: right;
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--muted);
}

.linkish {
  background: none;
  border: none;
  color: var(--accent);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  padding: 0;
  margin-top: 1rem;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.linkish.danger { color: var(--warn); }

.progress-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.55rem;
  margin-top: 1.25rem;
}

.progress-actions .linkish {
  margin-top: 0;
}

.quiz {
  min-height: 420px;
  display: flex;
  flex-direction: column;
}

.quiz-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.pill {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: rgba(15, 110, 86, 0.12);
  color: var(--accent-deep);
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
}

.prompt {
  font-family: var(--display);
  font-weight: 500;
  font-size: clamp(1.25rem, 2.2vw, 1.65rem);
  line-height: 1.35;
  margin: 0 0 1rem;
  white-space: pre-wrap;
}

.figure {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0.25rem 0 1.15rem;
  padding: 0.5rem 0;
}

.q-svg {
  display: block;
  width: min(100%, 380px);
  max-width: 100%;
  height: auto;
  margin: 0 auto;
  overflow: visible;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-sizing: content-box;
}

.hint-stack {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  margin-bottom: 1rem;
}

.hint {
  background: rgba(154, 52, 18, 0.08);
  border-left: 3px solid var(--warn);
  padding: 0.65rem 0.85rem;
  font-size: 0.95rem;
  margin: 0;
  white-space: pre-wrap;
}

.setup {
  font-family: var(--mono);
  font-size: 0.95rem;
  line-height: 1.45;
  background: rgba(15, 110, 86, 0.08);
  border-left: 3px solid var(--accent);
  padding: 0.75rem 0.9rem;
  margin: 0;
  white-space: pre-wrap;
  overflow-x: auto;
}

.calc {
  font-family: var(--mono);
  font-size: 0.88rem;
  line-height: 1.45;
  background: rgba(30, 58, 95, 0.08);
  border-left: 3px solid #1e3a5f;
  padding: 0.75rem 0.9rem;
  margin: 0;
  white-space: pre-wrap;
  overflow-x: auto;
}

.hint-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.85rem;
  justify-content: flex-end;
}

.hint-btns .linkish {
  margin-top: 0;
}

.hint-btns .linkish:disabled {
  color: var(--muted);
  text-decoration: none;
  cursor: default;
}

.choices {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.choice {
  text-align: left;
  font: inherit;
  padding: 0.85rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.choice:hover:not(:disabled) {
  border-color: var(--accent);
  background: rgba(15, 110, 86, 0.06);
}

.choice:disabled { cursor: default; opacity: 0.95; }
.choice.right { border-color: var(--good); background: var(--good-bg); }
.choice.wrong { border-color: var(--bad); background: var(--bad-bg); }

.answer-form label {
  display: block;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.input-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

#q-input {
  flex: 1;
  font-family: var(--mono);
  font-size: 1.1rem;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

#q-input:focus {
  outline: 2px solid rgba(15, 110, 86, 0.35);
  border-color: var(--accent);
}

.unit {
  font-family: var(--mono);
  color: var(--muted);
  white-space: nowrap;
}

.math-insert {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.15rem 0 0.55rem;
}

.math-btn {
  font-family: var(--mono);
  font-size: 0.95rem;
  font-weight: 500;
  min-width: 2.4rem;
  padding: 0.35rem 0.55rem;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  line-height: 1.2;
}

.math-btn:hover {
  border-color: var(--accent);
  color: var(--accent-deep);
  background: rgba(15, 110, 86, 0.06);
}

.math-btn:focus-visible {
  outline: 2px solid rgba(15, 110, 86, 0.35);
  outline-offset: 1px;
}

.primary, .ghost {
  font: inherit;
  font-weight: 600;
  border-radius: 10px;
  padding: 0.7rem 1.15rem;
  cursor: pointer;
  border: 1px solid transparent;
}

.primary {
  background: var(--accent);
  color: #fff;
}

.primary:hover { background: var(--accent-deep); }

.ghost {
  background: transparent;
  border-color: var(--line);
  color: var(--muted);
}

.answer-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.35rem;
}

.answer-actions #btn-next {
  margin-inline-start: auto;
}

.feedback {
  margin-top: 0.75rem;
  padding: 0.9rem 1rem;
  border-radius: 10px;
  font-size: 0.98rem;
  line-height: 1.45;
  white-space: pre-wrap;
}

.feedback.ok { background: var(--good-bg); color: var(--good); }
.feedback.no { background: var(--bad-bg); color: var(--bad); }

.mastery-row {
  margin-bottom: 0.85rem;
}

.mastery-row header {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  margin-bottom: 0.25rem;
}

.mastery-row header span:last-child {
  font-family: var(--mono);
  color: var(--muted);
}

.bar {
  height: 8px;
  background: rgba(26, 35, 50, 0.08);
  border-radius: 99px;
  overflow: hidden;
}

.bar > i {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 99px;
  transition: width 0.35s ease;
}

.mastery-row.mastered header span:first-child {
  color: var(--accent-deep);
  font-weight: 600;
}

.mastery-row.mastered .bar > i {
  background: var(--accent);
}

@media (max-width: 960px) {
  .layout {
    grid-template-columns: 1fr;
  }
  .topics { order: 2; }
  .quiz { order: 1; }
  .mastery { order: 3; }
}
