:root {
  color-scheme: light;
  --bg: #f4efe6;
  --panel: #fffcf5;
  --ink: #241f1a;
  --muted: #746b60;
  --line: #ddd1c0;
  --accent: #b4532a;
  --accent-dark: #8f3f1f;
  --soft: #f7e5d7;
  --warn: #945a1b;
  --shadow: 0 20px 60px rgba(55, 40, 25, 0.12);
  font-family: "Segoe UI", "Aptos", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100dvh;
  background:
    linear-gradient(135deg, rgba(180, 83, 42, 0.08), transparent 42%),
    var(--bg);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0 16px;
  cursor: pointer;
  font-weight: 750;
}

button {
  transition: transform 150ms ease, box-shadow 200ms ease, background-color 150ms ease, border-color 150ms ease;
}

button[disabled] {
  cursor: wait;
  opacity: 0.82;
}

button:active {
  transform: translateY(1px);
}

.primary:hover,
.secondary:hover,
.nav-actions button:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 20px rgba(55, 40, 25, 0.18);
}

input,
select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 12px;
  outline: none;
}

input:focus,
select:focus,
button:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(180, 83, 42, 0.2);
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 18px;
  max-width: 860px;
  font-size: clamp(2.4rem, 6vw, 5.2rem);
  line-height: 0.98;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 18px;
  font-size: clamp(1.5rem, 3vw, 2.35rem);
  line-height: 1.12;
  letter-spacing: 0;
}

h3 {
  margin-bottom: 0;
  font-size: 1.15rem;
}

code {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f7f8f4;
  padding: 1px 5px;
}

.shell {
  width: min(1400px, calc(100% - 32px));
  margin: 0 auto;
  padding: 18px 0 28px;
}

.nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 20px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
}

.brand {
  display: grid;
  flex: 0 0 auto;
  min-height: 46px;
  background: transparent;
  color: var(--ink);
  text-align: left;
}

.brand span {
  font-size: 1.05rem;
  font-weight: 850;
}

.brand small {
  color: var(--muted);
  font-weight: 600;
}

.nav-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: flex-end;
  min-width: 0;
}

.nav-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.nav-actions button,
.secondary {
  background: var(--ink);
  color: #fff;
}

.nav-actions button.nav-active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 0 0 2px var(--accent);
}

.nav-actions .update-app-btn {
  background: #eef8f0;
  color: #2d7d46;
  box-shadow: inset 0 0 0 1px rgba(45, 125, 70, 0.32);
}

.nav-actions .update-app-btn:disabled {
  opacity: 0.75;
}

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

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

.nav-active {
  background: var(--soft) !important;
  color: var(--accent) !important;
  box-shadow: inset 0 0 0 1px var(--accent);
}

.view {
  animation: enter 180ms ease both;
}

@keyframes enter {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
}

.hero,
.grid-2,
.app-grid,
.settings-layout {
  display: grid;
  gap: 22px;
}

.hero {
  grid-template-columns: minmax(0, 980px);
  justify-content: center;
  align-items: stretch;
  min-height: calc(100dvh - 110px);
}

.hero > div,
.panel,
.topbar,
.filters,
.question-panel,
.coach-panel,
.side-panel section {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 252, 245, 0.94);
  box-shadow: var(--shadow);
}

.hero > div {
  display: grid;
  align-content: center;
  padding: clamp(24px, 6vw, 72px);
}

.panel,
.question-panel,
.coach-panel,
.side-panel section {
  padding: 22px;
}

/* Hero 3D backdrop sits behind the hero copy inside the same card. */
.hero > div {
  position: relative;
  overflow: hidden;
}

.hero-scene {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

.hero > div > :not(.hero-scene) {
  position: relative;
  z-index: 1;
}

.hero-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.lead {
  max-width: 680px;
  color: var(--muted);
  font-size: 1.12rem;
  line-height: 1.65;
}

.grid-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.settings-layout,
.history-layout {
  grid-template-columns: minmax(0, 680px);
  justify-content: center;
  align-content: start;
  padding-top: 8px;
}

.history-layout {
  grid-template-columns: minmax(0, 920px);
}

.upload-layout {
  grid-template-columns: minmax(0, 720px);
  justify-content: center;
}

.app-grid {
  grid-template-columns: minmax(0, 1fr) 330px;
}

.practice,
.side-panel,
.panel,
.key-form {
  display: grid;
  gap: 16px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 22px;
}

.eyebrow {
  margin: 0 0 7px;
  color: var(--accent);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.muted,
.status,
#progressText {
  color: var(--muted);
  line-height: 1.5;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  color: var(--muted);
}

.stats span,
.question-meta span {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #f9faf7;
}

.filters {
  display: grid;
  grid-template-columns: 1.2fr 120px 130px minmax(180px, 1fr) auto;
  gap: 12px;
  padding: 14px;
}

.question-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--muted);
  font-size: 0.82rem;
}

.context-panel {
  margin-top: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcf8;
  overflow: hidden;
}

.question-panel.split-reading {
  display: grid;
  grid-template-columns: minmax(280px, 0.94fr) minmax(320px, 1.06fr);
  grid-template-rows: auto minmax(0, 1fr);
  gap: 14px;
  min-height: min(72vh, 760px);
}

.question-panel.split-reading .question-meta {
  grid-column: 1 / -1;
}

.question-panel.split-reading .context-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  margin-top: 0;
  min-height: 0;
}

.question-workspace {
  min-width: 0;
}

.question-workspace.scroll-pane,
.context-panel.scroll-pane .context-text {
  max-height: min(64vh, 680px);
  overflow: auto;
  scrollbar-width: thin;
}

.question-workspace.scroll-pane {
  padding-right: 4px;
}

.question-workspace.scroll-pane #questionText {
  margin-top: 0;
}

.context-title {
  margin: 0;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  color: var(--accent);
  font-weight: 800;
}

.context-text {
  max-height: 460px;
  overflow: auto;
  padding: 14px;
  color: #2d3128;
  line-height: 1.68;
  white-space: pre-wrap;
  font-size: 1.02rem;
}

#questionText {
  margin: 18px 0 20px;
  max-width: 980px;
}

.options {
  display: grid;
  gap: 10px;
}

.question-card {
  display: grid;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffaf1;
}

.question-card h3 {
  margin: 0;
  font-size: 1.08rem;
  line-height: 1.45;
}

.compact-options {
  gap: 8px;
}

.option {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  align-items: stretch;
  width: 100%;
  min-height: 58px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  text-align: left;
}

.option {
  transition: transform 140ms ease, border-color 140ms ease, background-color 140ms ease, box-shadow 200ms ease;
}

.option:hover,
.option.selected {
  border-color: var(--accent);
  background: var(--soft);
}

.option.correct {
  border-color: #2d7d46;
  background: #e8f5e9;
}

.option.correct .option-letter {
  color: #2d7d46;
}

.option.incorrect {
  border-color: #c0392b;
  background: #fdecea;
}

.option.incorrect .option-letter {
  color: #c0392b;
}

.option.revealed-correct {
  border-color: #2d7d46;
  background: #eafaf1;
}

.option.revealed-correct .option-letter {
  color: #2d7d46;
}

.option.muted-option {
  opacity: 0.5;
}

.option-letter::after {
  display: inline-block;
  margin-left: 4px;
  font-size: 0.85em;
}

.option.correct .option-letter::after {
  content: " ✓";
  color: #2d7d46;
}

.option.incorrect .option-letter::after {
  content: " ✗";
  color: #c0392b;
}

.option:hover {
  transform: translateX(3px);
  box-shadow: 0 4px 14px rgba(55, 40, 25, 0.1);
}

.option:disabled {
  cursor: default;
}

.option:disabled:hover {
  transform: none;
  box-shadow: none;
}

.option.correct .option-letter {
  color: #2d7d46;
  border-right-color: #2d7d46;
}

.option.incorrect {
  border-color: #c0392b;
  background: #fdecea;
}

.option.incorrect .option-letter {
  color: #c0392b;
  border-right-color: #c0392b;
}

.option.revealed-correct {
  border-color: #2d7d46;
  background: #e8f5e9;
}

.option.revealed-correct .option-letter {
  color: #2d7d46;
  border-right-color: #2d7d46;
}

.option:hover {
  transform: translateX(3px);
  box-shadow: 0 4px 14px rgba(55, 40, 25, 0.1);
}

.option:disabled {
  cursor: default;
}

.option:disabled:hover {
  transform: none;
  box-shadow: none;
}

.question-card {
  transition: box-shadow 200ms ease;
}

.question-card:hover {
  box-shadow: 0 10px 28px rgba(55, 40, 25, 0.1);
}

.option-letter {
  display: grid;
  place-items: center;
  height: 100%;
  border-right: 1px solid var(--line);
  color: var(--accent);
  font-weight: 850;
}

.option-text {
  padding: 14px;
  line-height: 1.35;
}

.coach-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.coach-text {
  margin-top: 18px;
  color: #2d3128;
  line-height: 1.62;
}

.blank {
  display: inline-block;
  min-width: 6.5ch;
  border-bottom: 2px solid var(--accent);
  color: transparent;
  transform: translateY(-0.12em);
}

.markdown-image {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 12px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.meter {
  width: 100%;
  height: 10px;
  overflow: hidden;
  border-radius: 999px;
  background: #e7eadf;
}

.meter div {
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 180ms ease;
}

.prebox {
  min-height: 260px;
  max-height: 520px;
  overflow: auto;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f9faf7;
  white-space: pre-wrap;
  line-height: 1.5;
}

button.busy {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

button.busy::before {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.55);
  border-top-color: #fff;
  border-radius: 999px;
  content: "";
  animation: spin 800ms linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.status.ready {
  color: var(--accent);
}

.status.warn {
  color: var(--warn);
}

.status-list,
.users-table {
  display: grid;
  gap: 10px;
}

.user-search {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 0.95rem;
  background: #fff;
  color: var(--ink);
  outline: none;
  transition: border-color 0.2s;
}

.user-search:focus {
  border-color: var(--accent);
}

.status-list div {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

.db-status {
  margin-bottom: 4px;
}

.db-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.82rem;
  font-weight: 600;
}

.db-ok { background: #e8f5e9; color: #2e7d32; }
.db-err { background: #fce4ec; color: #c62828; }
.db-off { background: #fff3e0; color: #e65100; }

.user-card {
  display: grid;
  gap: 14px;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.04);
}

.user-card-header {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-name {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--ink);
}

.role-badge {
  padding: 3px 12px;
  border-radius: 14px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  line-height: 1.4;
}

.role-badge.admin { background: #1a1a2e; color: #e8d5b7; }
.role-badge.user { background: #e8edf2; color: #445; }

.user-card-perms {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}

.user-card-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.role-select {
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  font-size: 0.9rem;
  color: var(--ink);
  cursor: pointer;
}

.history-container {
  margin-top: 4px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcf8;
  overflow-x: auto;
}

.history-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.history-btn {
  background: #e8edf2 !important;
  color: #445 !important;
  border-color: #d0d7de !important;
}

.history-btn:hover {
  background: #d0d7de !important;
}

.medal-history {
  margin-bottom: 12px;
  padding: 10px 12px;
  border: 1px solid rgba(45, 125, 70, 0.28);
  border-radius: 8px;
  background: #eef8f0;
}

.medal-history h4 {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0 0 8px;
  color: #2d7d46;
}

.medal-history ul {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.medal-history li {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: center;
}

.medal-icon {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 1px solid rgba(45, 125, 70, 0.22);
  border-radius: 999px;
  background: #fff8dd;
  box-shadow: 0 2px 8px rgba(87, 67, 33, 0.08);
}

.medal-history time {
  color: var(--muted);
  font-size: 0.88rem;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}

.toggle input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  width: 38px;
  height: 22px;
  border-radius: 12px;
  background: #ccc;
  position: relative;
  transition: background 0.2s;
}

.toggle input:checked + .toggle-track {
  background: var(--accent);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  transition: transform 0.2s;
}

.toggle input:checked + .toggle-track .toggle-thumb {
  transform: translateX(16px);
}

.toggle-label {
  font-size: 0.9rem;
  color: var(--ink);
}

.learning-stats {
  display: grid;
  gap: 16px;
}

.user-stats-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  background: #fbfcf8;
}

.user-stats-card h3 {
  margin: 0 0 12px;
  color: var(--accent);
}

.stats-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin-bottom: 12px;
}

.stats-summary span {
  padding: 4px 10px;
  border-radius: 6px;
  background: #f0efe8;
  font-size: 0.92rem;
}

.stat-correct { color: #2e7d32; }
.stat-incorrect { color: #c62828; }
.stat-keyless { color: #6d4c41; }

.user-stats-card details {
  margin-top: 8px;
}

.user-stats-card summary {
  cursor: pointer;
  padding: 4px 0;
  color: var(--accent);
  font-weight: 600;
}

.stats-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-size: 0.9rem;
}

.stats-table th,
.stats-table td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
}

.stats-table th {
  background: #f5f4ec;
  font-weight: 600;
}

.upload-progress {
  margin-top: 12px;
  display: grid;
  gap: 8px;
}

.upload-progress .meter {
  height: 6px;
}

.upload-progress .meter div {
  background: var(--accent);
  transition: width 300ms ease;
}

.upload-progress p {
  margin: 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.score-board {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.score {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 10px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 700;
}

.score.correct {
  background: #e8f5e9;
  color: #2d7d46;
}

.score.incorrect {
  background: #fdecea;
  color: #c0392b;
}

.score.keyless {
  background: #fff8e1;
  color: #945a1b;
}

.next-set {
  margin-top: 12px;
  width: 100%;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, calc(100% - 36px));
  padding: 14px 16px;
  border: 1px solid rgba(37, 109, 74, 0.34);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
  color: var(--ink);
  font-weight: 750;
  z-index: 20;
}

@media (max-width: 1050px) {
  .hero,
  .grid-2,
  .app-grid,
  .settings-layout,
  .history-layout {
    grid-template-columns: 1fr;
  }

  .filters {
    grid-template-columns: 1fr 1fr;
  }

  .search-label,
  .filters .secondary {
    grid-column: 1 / -1;
  }

  .question-panel.split-reading {
    grid-template-columns: 1fr;
    min-height: 0;
  }

  .question-workspace.scroll-pane,
  .context-panel.scroll-pane .context-text {
    max-height: 52vh;
  }
}

@media (max-width: 700px) {
  .shell {
    width: min(100% - 20px, 1400px);
    padding-top: 10px;
  }

  .nav,
  .topbar,
  .history-head,
  .coach-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .nav-actions,
  .nav-group,
  .nav-actions button,
  .hero-actions button {
    width: 100%;
  }

  .filters,
  .status-list div {
    grid-template-columns: 1fr;
  }

  .hero > div,
  .panel,
  .question-panel,
  .coach-panel,
  .side-panel section,
  .topbar {
    padding: 16px;
  }
}

@media (max-width: 550px) {
  .filters {
    grid-template-columns: 1fr;
  }

  .search-label,
  .filters .secondary {
    grid-column: auto;
  }

  .score-board {
    flex-direction: column;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}

/* Inline answer result below each question */
.answer-result {
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: var(--radius, 8px);
  font-size: 0.92rem;
  animation: resultIn 0.25s ease;
}

.result-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 6px;
  font-weight: 600;
}

.result-correct {
  background: #e6f9ed;
  color: #0d6b2e;
  border: 1px solid #b7e6c6;
}

.result-incorrect {
  background: #fde8e8;
  color: #9b1c1c;
  border: 1px solid #f5c6c6;
}

.result-unknown {
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fcd34d;
}

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

/* Auth tabs */
.auth-tabs {
  max-width: 420px;
  margin: 0 auto;
  padding-top: 6vh;
}

.tab-bar {
  display: flex;
  gap: 0;
  border-radius: 12px;
  overflow: hidden;
  margin-bottom: 22px;
  border: 1px solid var(--line);
  background: #fbfcf8;
}

.tab-nav {
  display: flex;
  gap: 0;
  border-radius: var(--radius, 10px);
  overflow: hidden;
  margin-bottom: 20px;
  border: 1px solid var(--border, #e2e0dc);
}

.tab-btn {
  flex: 1;
  padding: 10px 0;
  border: none;
  background: var(--bg-card, #fff);
  color: var(--muted, #78746d);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.tab-btn.active {
  background: var(--accent, #b4532a);
  color: #fff;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Explanation inside answer-result */
.explanation-text {
  margin-top: 8px;
  padding: 10px 12px;
  background: #f8f6f2;
  border-radius: 6px;
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text, #333);
}

.explanation-text p {
  margin: 4px 0;
}

.ai-explanation {
  background: #f0f4ff;
  border: 1px solid #c4d4f5;
}

.deep-explain-btn {
  margin-top: 8px;
  font-size: 0.85rem;
  padding: 5px 14px;
}
