:root {
  --bg: #0d0f16;
  --bg-soft: #141824;
  --card-bg: #1a2030;
  --card-bg-2: #1e2436;
  --border: #2a3145;
  --text: #eceef5;
  --dim: #9aa3b8;
  --accent: #7c9cff;
  --accent-2: #5ee6c0;
  --warn: #ffb454;
  --danger: #ff6b7a;
  --again: #ff8a6b;
  --know: #6bb8ff;
  --mastered: #5ee6a9;
  --radius: 16px;
  --radius-sm: 12px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
  --bar-bg: rgba(20, 24, 36, 0.92);
  --header-h: 64px;
  --tabs-h: 46px;
}

* { box-sizing: border-box; }
html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--header-h) + var(--tabs-h) + 14px);
  scroll-padding-bottom: 110px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  overflow-x: clip;              /* hard safety net: never allow horizontal page scroll */
}
button, input, select { font: inherit; }
button { cursor: pointer; }

/* Mobile interaction hardening: no 300ms tap delay, no double-tap zoom, no selection jank
   on tap targets. */
button, select, .flashcard, .grid-tile, .cheat-row, input[type="search"], .check {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button, .flashcard, .grid-tile, .cheat-row, .btn-icon, .rate {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px;
}
::selection { background: rgba(124, 156, 255, 0.35); }

/* ── top bar ─────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; justify-content: space-between; gap: 18px;
  height: var(--header-h);
  padding: 8px 18px calc(8px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #171b2a 0%, #10141f 100%);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand .logo { font-size: 26px; line-height: 1; }
.brand h1 { font-size: 17px; margin: 0; letter-spacing: 0.2px; white-space: nowrap; }
.brand h1 .sub { color: var(--dim); font-weight: 500; font-size: 13px; }
.brand .meta { margin: 1px 0 0; color: var(--dim); font-size: 11.5px; max-width: 46vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.progress-wrap { min-width: 180px; max-width: 320px; flex: 1; }
.progress-bar {
  height: 8px; border-radius: 5px; background: var(--bg-soft);
  border: 1px solid var(--border); overflow: hidden;
}
.progress-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--mastered)); transition: width 0.3s ease; }
.progress-labels { display: flex; justify-content: space-between; gap: 6px; margin-top: 4px; font-size: 10.5px; color: var(--dim); white-space: nowrap; }

/* ── controls (tabs + filters) ───────────────────────── */
.controls {
  position: sticky; top: var(--header-h); z-index: 25;
  background: var(--bar-bg);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 6px 14px;
}
.row-top { display: flex; align-items: center; gap: 10px; }
.view-toggle { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--bg-soft); }
.view-btn {
  background: transparent; border: none; color: var(--dim);
  padding: 8px 15px; font-size: 13.5px; font-weight: 600; white-space: nowrap;
  transition: background 0.15s ease, color 0.15s ease;
}
.view-btn:hover { color: var(--text); }
.view-btn.active { background: var(--accent); color: #0b0e17; }
.filter-toggle {
  margin-left: auto; display: inline-flex; align-items: center; gap: 6px;
  background: transparent; color: var(--dim); border: 1px solid var(--border);
  padding: 7px 12px; border-radius: 9px; font-size: 13px;
}
.filter-toggle:hover { color: var(--text); border-color: var(--accent); }
.filter-toggle .ft-icon { transition: transform 0.2s ease; }
.filter-toggle.open .ft-icon { transform: rotate(90deg); }

.filters {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding-top: 10px;
}
select, input[type="search"] {
  background: var(--card-bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 9px; padding: 8px 11px; font-size: 14px;
}
select { max-width: 210px; }
input[type="search"] { flex: 1; min-width: 180px; max-width: 320px; font-size: 14px; }
.check { color: var(--dim); font-size: 13px; display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; cursor: pointer; }
.ghost {
  background: transparent; color: var(--dim); border: 1px solid var(--border);
  padding: 8px 12px; border-radius: 9px; font-size: 13px;
}
.ghost:hover { color: var(--text); border-color: var(--accent); }
.ghost.active { color: var(--accent); border-color: var(--accent); }
.primary { background: var(--accent); color: #0b0e17; font-weight: 600; border: none; padding: 10px 16px; border-radius: 10px; }

/* ── layout ──────────────────────────────────────────── */
main { flex: 1; padding: 14px 16px 130px; max-width: 900px; margin: 0 auto; width: 100%; }
.view { animation: fadein 0.2s ease; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.session-info {
  color: var(--dim); font-size: 13px; margin: 4px 0 12px; text-align: center;
  display: flex; align-items: center; justify-content: center; gap: 6px; flex-wrap: wrap;
}
.session-info .count { color: var(--text); font-weight: 600; }

/* ── flashcard ───────────────────────────────────────── */
.card-stage { max-width: 820px; margin: 0 auto; perspective: 1600px; }
.flashcard {
  display: grid;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(0.4, 0.2, 0.2, 1);
  cursor: pointer;
}
.flashcard > * { grid-area: 1 / 1; min-width: 0; }
.flashcard.flipped { transform: rotateY(180deg); }
.card-face {
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: linear-gradient(180deg, var(--card-bg-2) 0%, var(--card-bg) 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px 30px;
  min-height: 300px;
  min-width: 0;
  max-width: 100%;
  width: 100%;
  overflow: hidden;
}
.card-back { transform: rotateY(180deg); }
.card-top { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; min-width: 0; }
.badge {
  display: inline-block; background: var(--bg-soft); border: 1px solid var(--border);
  color: var(--dim); border-radius: 8px; padding: 4px 10px; font-size: 12px; font-weight: 600;
}
.source-badge { color: var(--accent); border-color: rgba(124, 156, 255, 0.5); }
.tag-badge { max-width: 62%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--accent-2); }
.btn-icon {
  margin-left: auto; background: none; border: none; font-size: 18px;
  opacity: 0.55; padding: 6px; border-radius: 8px; transition: opacity 0.15s ease, transform 0.1s ease;
}
.btn-icon:hover { opacity: 1; background: var(--bg-soft); }
.btn-icon:active { transform: scale(0.92); }
.btn-icon.flagged { opacity: 1; filter: drop-shadow(0 0 5px var(--danger)); background: rgba(255, 107, 122, 0.12); }
.card-title { margin: 2px 0 14px; font-size: 21px; line-height: 1.32; letter-spacing: 0.1px; }
.flip-hint { margin-top: 20px; color: var(--dim); font-size: 12px; text-align: center; }

/* ── card content ────────────────────────────────────── */
.card-sections {
  font-size: 14.5px;
  /* keep any long token/URL/diagram line inside the card — never widen the card */
  overflow-wrap: anywhere;
  word-break: break-word;
  min-width: 0;
}
.card-sections img { max-width: 100%; height: auto; }
.card-sections h5, .card-sections h6 {
  margin: 16px 0 7px; font-size: 11.5px; letter-spacing: 0.7px; text-transform: uppercase; color: var(--accent);
}
.front-sections .q { font-size: 17px; color: #fff; font-weight: 600; margin: 6px 0; }
.card-sections p { margin: 8px 0; }
.card-sections ul, .card-sections ol { margin: 8px 0; padding-left: 22px; }
.card-sections li { margin: 4px 0; }
.card-sections code { background: #0d1020; border: 1px solid var(--border); border-radius: 5px; padding: 1px 6px; font-size: 12.5px; color: var(--accent-2); }
.card-sections pre {
  background: #0d1020; border: 1px solid var(--border); border-radius: 10px;
  padding: 13px; overflow-x: auto; font-size: 12.5px; line-height: 1.55;
}
.card-sections pre.diagram { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: #c7d2e8; }
.card-sections blockquote {
  margin: 12px 0; padding: 10px 14px; border-left: 3px solid var(--accent);
  background: rgba(124, 156, 255, 0.08); border-radius: 0 10px 10px 0;
}
.card-sections blockquote p { margin: 4px 0; }
.card-sections a { color: var(--accent); }
.card-sections table { border-collapse: collapse; margin: 12px 0; width: 100%; font-size: 13px; display: block; overflow-x: auto; }
.card-sections th, .card-sections td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.card-sections th { background: var(--bg-soft); color: var(--accent); }

/* ── rating bar (sticky bottom) ──────────────────────── */
.rating-bar {
  position: sticky; bottom: 0; z-index: 20;
  display: flex; gap: 10px; justify-content: center;
  margin-top: 22px;
  padding: 12px 4px calc(12px + env(safe-area-inset-bottom));
  background: var(--bar-bg);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
}
.rate {
  flex: 1; max-width: 220px;
  border: 1px solid var(--border); background: var(--card-bg); color: var(--text);
  border-radius: 12px; padding: 13px 8px; font-size: 15px; font-weight: 600;
  min-height: 50px;
  transition: transform 0.08s ease, border-color 0.15s ease, background 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}
.rate:active { transform: scale(0.96); }
.rate.again { color: var(--again); }
.rate.again:hover { border-color: var(--again); background: rgba(255, 138, 107, 0.1); }
.rate.know { color: var(--know); }
.rate.know:hover { border-color: var(--know); background: rgba(107, 184, 255, 0.1); }
.rate.mastered { color: var(--mastered); }
.rate.mastered:hover { border-color: var(--mastered); background: rgba(94, 230, 169, 0.1); }

/* ── grid view ───────────────────────────────────────── */
.grid-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(172px, 1fr)); gap: 10px; }
.grid-group-title {
  grid-column: 1 / -1;
  margin: 6px 0 2px; padding: 10px 14px; border-radius: 10px;
  background: var(--bg-soft); border: 1px solid var(--border);
  font-size: 12px; font-weight: 700; letter-spacing: 0.8px; text-transform: uppercase; color: var(--accent);
  position: sticky; top: calc(var(--header-h) + var(--tabs-h)); z-index: 5;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.grid-tile {
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 12px 14px; cursor: pointer; text-align: left;
  display: flex; flex-direction: column; gap: 6px; min-height: 82px;
  transition: transform 0.08s ease, border-color 0.15s ease, background 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}
.grid-tile:hover { border-color: var(--accent); background: var(--card-bg-2); }
.grid-tile:active { transform: scale(0.97); }
.grid-tile .gt-row1 { display: flex; align-items: center; gap: 6px; }
.grid-tile .gt-num { font-weight: 700; color: var(--accent); font-size: 13.5px; }
.grid-tile .gt-src { width: 8px; height: 8px; border-radius: 50%; }
.grid-tile .gt-src.chatgpt { background: var(--accent); }
.grid-tile .gt-src.certlibrary { background: var(--accent-2); }
.grid-tile .gt-status { margin-left: auto; font-size: 13px; }
.grid-tile .gt-title {
  font-size: 13px; color: var(--dim); line-height: 1.35; margin: 0;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.grid-tile .gt-verify { font-size: 10.5px; color: var(--warn); text-transform: uppercase; letter-spacing: 0.4px; }

/* ── cheat sheet ─────────────────────────────────────── */
.cheat-list { max-width: 860px; margin: 0 auto; }
.cheat-grid { display: grid; grid-template-columns: 150px 1fr; }
.cheat-head {
  position: sticky; top: calc(var(--header-h) + var(--tabs-h)); z-index: 6;
  font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--accent);
  padding: 9px 12px; border-bottom: 1px solid var(--border);
  background: var(--bar-bg); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.cheat-cell { padding: 11px 12px; border-bottom: 1px solid var(--border); font-size: 13.5px; }
.cheat-cell.cardnum { font-weight: 600; color: var(--accent); }
.cheat-row { cursor: pointer; display: contents; }
.cheat-row:hover .cheat-cell { background: rgba(124, 156, 255, 0.07); }
.cheat-cell strong { color: var(--accent-2); }
.cheat-cell .answer-preview { color: var(--dim); }
.cheat-cell .tag-inline { color: var(--dim); font-size: 11.5px; display: block; margin-top: 4px; }
.cheat-empty { text-align: center; padding: 50px 20px; color: var(--dim); }

/* ── empty / done states ─────────────────────────────── */
.no-results, .session-done { text-align: center; padding: 70px 20px; }
.no-results .empty-emoji, .done-emoji { font-size: 54px; margin: 0 0 10px; }
.dim { color: var(--dim); }
.session-done .dim { margin-top: 8px; }
.done-inner { max-width: 380px; margin: 0 auto; }
.done-actions { display: flex; gap: 10px; justify-content: center; margin-top: 20px; flex-wrap: wrap; }

/* ── footer ──────────────────────────────────────────── */
.shortcuts {
  border-top: 1px solid var(--border); padding: 10px 18px calc(10px + env(safe-area-inset-bottom));
  color: var(--dim); font-size: 12px; display: flex; gap: 14px; flex-wrap: wrap; justify-content: center;
  background: var(--bg-soft);
}
kbd {
  background: #0d1020; border: 1px solid var(--border); border-bottom-width: 2px;
  border-radius: 5px; padding: 1px 6px; font-family: inherit; font-size: 11px; color: var(--text);
}
.noscript { text-align: center; padding: 40px; color: var(--warn); }

/* ── mobile (≤820px) ─────────────────────────────────── */
@media (max-width: 820px) {
  :root { --tabs-h: 44px; --header-h: 60px; }
  .topbar { padding: 6px 12px calc(6px + env(safe-area-inset-top)); gap: 10px; }
  .brand .logo { font-size: 22px; }
  .brand h1 { font-size: 15.5px; }
  .brand .meta { display: none; }
  .progress-labels { font-size: 9.5px; }
  .controls { padding: 4px 10px; }
  .view-btn { padding: 0 10px; min-height: 40px; font-size: 13px; }
  .filter-toggle { min-height: 40px; }
  .filters { display: none; flex-direction: column; align-items: stretch; }
  .filters.open { display: flex; animation: fadein 0.18s ease; }
  .filters select, .filters input[type="search"] { max-width: none; width: 100%; font-size: 16px; min-height: 46px; }
  .filters .check { align-self: flex-start; font-size: 14px; min-height: 40px; }
  .filters .ghost { font-size: 14px; min-height: 44px; }
  main { padding: 10px 10px 132px; }
  .card-stage { max-width: none; }
  .card-face { padding: 18px 18px; min-height: 240px; }
  .card-title { font-size: 18px; }
  .card-sections { font-size: 15.5px; }
  .front-sections .q { font-size: 16.5px; }
  .btn-icon { min-width: 40px; min-height: 40px; }
  .rating-bar { gap: 8px; padding: 10px 0 calc(10px + env(safe-area-inset-bottom)); }
  .rate { min-height: 56px; font-size: 15.5px; padding: 12px 6px; }
  .grid-list { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
  .grid-tile { min-height: 92px; }
  .cheat-grid { grid-template-columns: 84px 1fr; }
  .cheat-row, .cheat-cell { min-height: 46px; }
  .shortcuts { font-size: 11px; gap: 10px; }
}

/* Reduced motion: honour OS setting — drop flip/scroll/fade animations. */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .flashcard, .flashcard.flipped { transition: none; }
  .view, .filters.open { animation: none; }
  .btn-icon .ft-icon, .filter-toggle .ft-icon { transition: none; }
}

@media (max-width: 420px) {
  .view-toggle { width: 100%; }
  .view-btn { flex: 1; }
  .filter-toggle { margin-left: 0; width: 100%; justify-content: center; }
  .grid-list { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
  .brand .sub { display: none; }
}