/* ==== Base ==== */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: #0f172a;
  color: #e2e8f0;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

:root {
  --bg: #0f172a;
  --bg-elev: #1e293b;
  --bg-card: #1e293b;
  --border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-strong: #0ea5e9;
  --success: #22c55e;
  --success-bg: #052e16;
  --error: #ef4444;
  --error-bg: #450a0a;
  --warn: #f59e0b;
  --radius: 14px;
}

/* ==== Header ==== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: env(safe-area-inset-top) 16px 12px;
  padding-top: max(env(safe-area-inset-top), 12px);
  background: rgba(15, 23, 42, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.app-header h1 {
  font-size: 1rem;
  font-weight: 600;
  margin: 0;
  letter-spacing: 0.2px;
}
.icon-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text);
  width: 38px; height: 38px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background .15s;
}
.icon-btn:hover { background: var(--bg-elev); }
.icon-btn.hidden { visibility: hidden; }
.header-right { display: flex; gap: 4px; }

/* ==== Main / Screens ==== */
#app { flex: 1; display: flex; flex-direction: column; }
.screen {
  display: none;
  padding: 16px;
  padding-bottom: calc(env(safe-area-inset-bottom) + 16px);
  flex: 1;
  flex-direction: column;
  gap: 16px;
}
.screen.active { display: flex; }

.hero h2 {
  margin: 0 0 4px;
  font-size: 1.4rem;
}
.muted { color: var(--muted); }
p { line-height: 1.5; }

/* ==== Buttons ==== */
.primary-btn, .ghost-btn {
  font: inherit;
  font-weight: 600;
  border-radius: 12px;
  padding: 14px 18px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform .05s ease, background .15s, opacity .15s;
}
.primary-btn {
  background: var(--accent-strong);
  color: #001018;
}
.primary-btn:active { transform: scale(0.98); }
.primary-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.ghost-btn {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
.ghost-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.full { width: 100%; }

/* ==== User banner / Login CTA ==== */
.user-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: linear-gradient(135deg, rgba(56, 189, 248, 0.12), rgba(129, 140, 248, 0.12));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.user-info { min-width: 0; }
.user-name {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-email {
  font-size: 0.8rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ghost-btn.small { padding: 8px 12px; font-size: 0.85rem; }

.login-cta {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login-cta p { margin: 0; color: var(--muted); }

.section-title {
  margin: 8px 0 4px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

/* ==== Resume card ==== */
.resume-wrap { display: flex; flex-direction: column; gap: 6px; }
.resume-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: linear-gradient(135deg, rgba(56, 189, 248, 0.18), rgba(14, 165, 233, 0.18));
  cursor: pointer;
  text-align: left;
  font: inherit;
  color: var(--text);
  width: 100%;
}
.resume-card:active { transform: scale(0.99); }
.resume-card .meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.resume-card .title { font-weight: 600; }
.resume-card .subtitle { font-size: 0.85rem; color: var(--muted); }
.resume-card .count {
  flex-shrink: 0;
  background: var(--accent-strong);
  color: #001018;
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ==== Tomo sections ==== */
.tomo-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.tomo-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 4px;
}
.tomo-header .meta { min-width: 0; }
.tomo-header .title {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.2px;
}
.tomo-header .subtitle {
  font-size: 0.8rem;
  color: var(--muted);
}
.tomo-practicar-btn {
  flex-shrink: 0;
  padding: 8px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  border-radius: 999px;
  background: rgba(56, 189, 248, 0.12);
  color: var(--accent);
  border: 1px solid rgba(56, 189, 248, 0.3);
  cursor: pointer;
  font: inherit;
  white-space: nowrap;
}
.tomo-practicar-btn:hover { background: rgba(56, 189, 248, 0.2); }
.tomo-practicar-btn:active { transform: scale(0.98); }

/* ==== Block list ==== */
.block-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.block-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  /* <button> defaults to serif font in most user-agent stylesheets;
     force the page font + color so block titles match the rest of the UI. */
  font: inherit;
  color: inherit;
  text-align: left;
  transition: background .15s, transform .05s;
}
.block-card:active { transform: scale(0.99); }
.block-card .meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.block-card .title { font-weight: 600; font-size: 1rem; }
.block-card .subtitle { font-size: 0.85rem; color: var(--muted); }
.block-card .count {
  font-variant-numeric: tabular-nums;
  background: rgba(56, 189, 248, 0.12);
  color: var(--accent);
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  flex-shrink: 0;
}

/* ==== Practice ==== */
.progress-row { display: flex; flex-direction: column; gap: 6px; }
.progress-bar {
  height: 6px;
  background: var(--bg-elev);
  border-radius: 999px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), #818cf8);
  transition: width .3s ease;
}
.progress-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex: 1;
  outline: none;
}
.card:focus-visible { border-color: var(--accent); }
.card-id {
  font-size: 0.75rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.4px;
  text-transform: uppercase;
}
.card-question {
  font-size: 1.08rem;
  font-weight: 600;
  line-height: 1.4;
}

.options {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.option {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.5);
  cursor: pointer;
  text-align: left;
  font: inherit;
  color: var(--text);
  width: 100%;
  transition: background .15s, border-color .15s, transform .05s;
}
.option:hover { background: rgba(56, 189, 248, 0.08); }
.option:active { transform: scale(0.99); }
.option:disabled { cursor: default; }
.option .letter {
  flex-shrink: 0;
  width: 26px; height: 26px;
  border-radius: 50%;
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}
.option .text { flex: 1; line-height: 1.4; }

.option.correct {
  border-color: var(--success);
  background: var(--success-bg);
}
.option.correct .letter {
  background: var(--success);
  border-color: var(--success);
  color: #052e16;
}
.option.wrong {
  border-color: var(--error);
  background: var(--error-bg);
}
.option.wrong .letter {
  background: var(--error);
  border-color: var(--error);
  color: #450a0a;
}
.option.dim { opacity: 0.55; }

.feedback {
  padding: 10px 12px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 0.95rem;
  border: 1px solid var(--border);
}
.feedback.ok { background: var(--success-bg); border-color: var(--success); color: #bbf7d0; }
.feedback.ko { background: var(--error-bg); border-color: var(--error); color: #fecaca; }
.hidden { display: none !important; }

.rationale {
  background: rgba(56, 189, 248, 0.06);
  border: 1px solid rgba(56, 189, 248, 0.25);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 0.9rem;
}
.rationale summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--accent);
  list-style: none;
}
.rationale summary::-webkit-details-marker { display: none; }
.rationale summary::after { content: " ▾"; }
.rationale[open] summary::after { content: " ▴"; }
.rationale p { margin: 8px 0 0; color: var(--text); }

.controls {
  display: flex;
  gap: 10px;
}
.controls > * { flex: 1; }

/* ==== Results ==== */
.results {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 24px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.big-score {
  font-size: 3rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  background: linear-gradient(90deg, var(--accent), #818cf8);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

/* ==== Stats screen ==== */
.stats-summary {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.stat-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  text-align: center;
}
.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.stat-label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-top: 2px;
}

.per-block-list, .history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.per-block-row {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.per-block-row .label { font-size: 0.9rem; min-width: 0; }
.per-block-row .ratio {
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
}
.per-block-row .pct {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 0.9rem;
  min-width: 48px;
  text-align: right;
}
.per-block-row .bar {
  height: 6px;
  background: rgba(255,255,255,0.06);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 6px;
  grid-column: 1 / -1;
}
.per-block-row .bar > div {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #818cf8);
}
.per-block-row { display: grid; grid-template-columns: 1fr auto auto; }

.history-item {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.9rem;
}
.history-item .meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.history-item .meta .t { font-weight: 600; }
.history-item .meta .s { color: var(--muted); font-size: 0.8rem; }
.history-item .score {
  font-variant-numeric: tabular-nums;
  background: rgba(56, 189, 248, 0.12);
  color: var(--accent);
  padding: 4px 10px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.85rem;
  white-space: nowrap;
}

/* ==== Small screens ==== */
@media (max-width: 380px) {
  .card { padding: 16px; }
  .card-question { font-size: 1rem; }
  .option { padding: 10px 12px; }
}

/* ==== Tablet/Desktop: constrain width ==== */
@media (min-width: 720px) {
  main { max-width: 640px; margin: 0 auto; width: 100%; }
  .hero h2 { font-size: 1.6rem; }
}