/* ==========================================================================
   Inside the Database Machine — Interactive Journey
   Notion-style system (see DESIGN-notion.md): warm paper canvas, near-black
   Inter type, one structural blue, hairlines + barely-there shadows.
   Tokens live in tokens.css (light default, data-theme="dark" override).
   No frameworks.
   ========================================================================== */

/* Theme toggle button (in the top bar next to progress) */
.theme-toggle {
  background: var(--bg-2); border: 1px solid var(--line);
  color: var(--text-dim); font-size: 15px;
  width: 36px; height: 36px; border-radius: var(--r-full);
  cursor: pointer; transition: border-color .2s, box-shadow .2s;
  flex: none;
}
.theme-toggle:hover { border-color: var(--text-faint); box-shadow: var(--shadow-1); }

* { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.6;
  overflow-x: hidden;
}

::selection { background: rgba(0, 117, 222, .15); }

/* ---------- Top bar ---------- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0;
  height: 56px;
  display: flex; align-items: center; gap: 16px;
  padding: 0 18px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  z-index: 50;
}

#brand {
  font-weight: 700; font-size: 15px;
  letter-spacing: -0.125px;
  color: var(--text);
  text-decoration: none;
}
#brand span { color: var(--primary); }

#home-link {
  font-size: 13px; font-weight: 500;
  color: var(--text-dim); text-decoration: none;
  border: 1px solid var(--line); border-radius: var(--r-md);
  padding: 4px 12px; transition: all .2s;
  white-space: nowrap;
  background: var(--bg-2);
}
#home-link:hover { color: var(--text); box-shadow: var(--shadow-1); }

#toc-toggle {
  background: var(--bg-2); border: 1px solid var(--line);
  color: var(--text-dim); font-size: 17px;
  width: 36px; height: 36px; border-radius: var(--r-md);
  cursor: pointer; transition: all .2s;
}
#toc-toggle:hover { color: var(--text); box-shadow: var(--shadow-1); }

#progress-wrap {
  margin-left: auto;
  display: flex; align-items: center; gap: 10px;
}
#progress-bar {
  width: 140px; height: 6px;
  background: var(--bg-3);
  border-radius: 3px; overflow: hidden;
}
#progress-fill {
  height: 100%; width: 0%;
  background: var(--primary);
  transition: width .6s ease;
}
#progress-label { font-size: 12px; font-weight: 600; color: var(--text-dim); }

/* ---------- Table of contents (sidebar) ---------- */
#toc {
  position: fixed; top: 56px; bottom: 0; left: 0;
  width: 300px;
  background: var(--bg);
  border-right: 1px solid var(--line);
  transform: translateX(-100%);
  transition: transform .3s ease;
  z-index: 40;
  overflow-y: auto;
}
/* Visibility is driven by body.sidebar-toggled, which always means "the
   opposite of this breakpoint's default": narrow screens hide the sidebar by
   default (toggled = reveal the overlay); wide screens (media query below)
   dock it and show it by default (toggled = hide it). The narrow "reveal" and
   wide "hide" rules are scoped to their own breakpoints so they never collide
   at equal specificity — every state is decided by specificity, not order. */
@media (max-width: 1023.98px) {
  body.sidebar-toggled #toc { transform: translateX(0); }
}
#toc-inner { padding: 20px 14px 60px; }

#toc .toc-part {
  font-size: 11px; font-weight: 600;
  letter-spacing: 1px; text-transform: uppercase;
  color: var(--text-faint);
  margin: 20px 8px 8px;
}
#toc a {
  display: flex; align-items: baseline; gap: 8px;
  padding: 7px 10px; border-radius: var(--r-sm);
  color: var(--text-dim); text-decoration: none;
  font-size: 14px;
  transition: all .15s;
}
#toc a:hover { color: var(--text); background: var(--bg-3); }
#toc a.active { color: var(--primary); background: var(--tint-primary); font-weight: 500; }
#toc a .n { font-size: 12px; font-weight: 600; color: var(--text-faint); min-width: 20px; }
#toc a.active .n { color: var(--primary); }
#toc a.done .n::after { content: " ✓"; color: var(--green); }

#toc-backdrop {
  position: fixed; inset: 0; z-index: 35;
  background: rgba(0,0,0,.25);
  opacity: 0; pointer-events: none;
  transition: opacity .3s;
}
body.sidebar-toggled #toc-backdrop { opacity: 1; pointer-events: auto; }

/* Wide screens: the sidebar is docked into its own column and present by
   default; the content shifts right to make room. The hamburger hides it and
   reclaims the space. No dimming backdrop is ever needed in this mode. */
@media (min-width: 1024px) {
  #toc-backdrop { display: none; }
  /* Docked and present by default; hidden only when the hamburger adds
     .sidebar-toggled. The two states are made mutually exclusive with :not()
     so each is chosen by *matching* rather than a specificity tie-break — and
     both outrank the plain `#toc`/`#app` base rules (1,1,1 > 1,0,0). When the
     class is present, #toc falls back to its base translateX(-100%) (hidden)
     and #app to its base margin:0 auto (recentred). */
  body:not(.sidebar-toggled) #toc { transform: translateX(0); }
  body:not(.sidebar-toggled) #app { margin-left: max(300px, calc((100vw - 880px) / 2)); }
  #app { transition: margin-left .3s ease; }
}

/* ---------- Main layout ---------- */
#app {
  position: relative; z-index: 1;
  max-width: 880px;
  margin: 0 auto;
  padding: 96px 22px 120px;
}

/* ---------- Home / journey map ---------- */
.hero { text-align: center; padding: 40px 0 30px; }
.hero h1 {
  font-size: clamp(34px, 6vw, 56px);
  line-height: 1.04;
  font-weight: 700;
  letter-spacing: -1.875px;
  color: var(--text);
}
.hero p.sub {
  margin: 18px auto 0; max-width: 620px;
  color: var(--text-dim); font-size: 17px;
}
.hero .cta {
  display: inline-block; margin-top: 30px;
  padding: 12px 32px; border-radius: var(--r-full);
  font-weight: 500; font-size: 16px; text-decoration: none;
  color: var(--on-primary);
  background: var(--primary);
  transition: background .15s, transform .15s;
}
.hero .cta:hover { background: var(--primary-active); }
.hero .cta:active { transform: scale(.97); }

.part-head {
  margin: 54px 0 6px;
  font-size: 12px; font-weight: 600;
  letter-spacing: 1px; text-transform: uppercase;
  color: var(--text-faint);
}
.part-desc { color: var(--text-dim); margin-bottom: 18px; font-size: 15px; }

/* Journey map: chapter cards connected by a quiet spine */
.journey { position: relative; padding-left: 26px; }
.journey::before {
  content: "";
  position: absolute; left: 7px; top: 8px; bottom: 8px;
  width: 2px;
  background: var(--line);
}
.ch-card {
  position: relative;
  display: block;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 14px;
  text-decoration: none; color: var(--text);
  transition: transform .2s, box-shadow .2s;
}
.ch-card::before { /* node on the spine */
  content: "";
  position: absolute; left: -26px; top: 26px;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--bg); border: 2px solid var(--text-faint);
  transform: translateX(2px);
}
.ch-card.visited::before { background: var(--primary); border-color: var(--primary); }
.ch-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-1);
}
.ch-card .num { font-size: 11px; font-weight: 600; letter-spacing: 1px; color: var(--primary); }
.ch-card h3 { font-size: 19px; font-weight: 600; letter-spacing: -0.25px; margin: 2px 0 4px; color: var(--text); }
.ch-card p { color: var(--text-dim); font-size: 14.5px; }
.ch-card .badges { margin-top: 8px; display: flex; gap: 8px; flex-wrap: wrap; }
.badge {
  font-size: 11px; font-weight: 600; letter-spacing: .125px;
  padding: 3px 9px; border-radius: var(--r-full);
  border: 1px solid var(--line); color: var(--text-dim);
  background: var(--bg-2);
}
.badge.viz { color: var(--primary); }
.badge.quiz-done { color: var(--green); }

/* ---------- Chapter page ---------- */
.ch-hero { padding: 30px 0 10px; }
.ch-hero .kicker {
  font-size: 12px; font-weight: 600; letter-spacing: 1px;
  text-transform: uppercase; color: var(--text-faint);
}
.ch-hero h1 {
  font-size: clamp(30px, 5vw, 40px); font-weight: 700;
  letter-spacing: -1px; color: var(--text); margin: 6px 0 14px; line-height: 1.1;
}
.ch-hero .intro { color: var(--text-dim); font-size: 17px; }

section.block {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 26px 28px;
  margin: 26px 0;
  /* scroll-reveal: starts hidden, .revealed animates in */
  opacity: 0; transform: translateY(24px);
  transition: opacity .6s ease, transform .6s ease;
}
section.block.revealed { opacity: 1; transform: none; }

section.block h2 {
  font-size: 24px; font-weight: 700; letter-spacing: -0.625px;
  color: var(--text);
  margin-bottom: 14px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}
section.block h3 { font-size: 18px; font-weight: 600; letter-spacing: -0.25px; color: var(--text); margin: 18px 0 8px; }
section.block p { margin: 12px 0; }
section.block ul, section.block ol { margin: 12px 0 12px 24px; }
section.block li { margin: 6px 0; }
section.block strong { color: var(--text); font-weight: 600; }
section.block em { color: var(--text); font-style: italic; }

/* Inline code & code blocks */
code {
  font-family: var(--mono); font-size: .85em;
  background: var(--bg-3);
  padding: 2px 6px; border-radius: var(--r-xs);
  color: var(--orange);
}
pre {
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 16px 18px;
  overflow-x: auto;
  margin: 14px 0;
}
pre code { background: none; border: none; padding: 0; color: var(--text-secondary); font-size: 13.5px; line-height: 1.6; }

/* Language tabs on Part V code examples (Java ⇄ Go) */
.code-tab-bar { display: flex; gap: 6px; margin: 14px 0 0; }
.code-tab {
  background: var(--bg-2); border: 1px solid var(--line); color: var(--text-dim);
  font-family: var(--sans); font-size: 12px; font-weight: 500; padding: 4px 14px;
  border-radius: var(--r-md); cursor: pointer; transition: all .2s;
}
.code-tab:hover { color: var(--text); box-shadow: var(--shadow-1); }
.code-tab.active { color: var(--primary); border-color: var(--primary); background: var(--tint-primary); }
.code-tabs pre { margin-top: 8px; }
.code-tabs pre[hidden] { display: none; }

/* Call-out boxes */
.callout {
  border: 1px solid rgba(221, 91, 0, .3);
  background: var(--tint-orange);
  border-radius: var(--r-md);
  padding: 14px 18px;
  margin: 14px 0;
  font-size: 15px;
}
.callout::before { content: "◆ "; color: var(--orange); }

/* ---------- Visualization frames ---------- */
.viz {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin: 18px 0 6px;
  overflow: hidden;
}
.viz-head {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  font-size: 11.5px; font-weight: 600;
  color: var(--text-dim); letter-spacing: 1px;
  background: var(--bg);
}
.viz-head::before { content: "●"; color: var(--green); font-size: 9px; }
.viz-body { padding: 16px; position: relative; }
.viz-body svg { display: block; width: 100%; height: auto; }
.viz-body canvas { display: block; width: 100%; }
.viz-controls {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding: 0 16px 14px;
}
.viz button, .quiz button {
  font-family: var(--sans); font-size: 13px; font-weight: 500;
  background: var(--bg-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 6px 14px;
  cursor: pointer;
  transition: all .15s;
}
.viz button:hover, .quiz button:hover { box-shadow: var(--shadow-1); border-color: var(--text-faint); }
.viz button:disabled { opacity: .4; cursor: default; box-shadow: none; }
.viz input[type="text"], .viz input[type="number"] {
  font-family: var(--mono); font-size: 13px;
  background: var(--bg-2); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--r-xs);
  padding: 6px 10px; width: 110px;
}
.viz input[type="text"]:focus, .viz input[type="number"]:focus {
  outline: none; border-color: var(--primary); box-shadow: var(--shadow-1);
}
.viz input[type="range"] { accent-color: var(--primary); }
.viz .viz-note {
  font-size: 13px; color: var(--text-dim);
  padding: 0 16px 14px;
  min-height: 18px;
}
.viz label { font-size: 12px; font-weight: 500; color: var(--text-dim); }

/* Tooltip used by visualizations (hover/tap reveals) */
#viz-tip {
  position: fixed; z-index: 100;
  max-width: 280px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-2);
  border-radius: var(--r-md);
  padding: 10px 13px;
  font-size: 13.5px; line-height: 1.5;
  pointer-events: none;
  opacity: 0; transition: opacity .15s;
}
#viz-tip.show { opacity: 1; }
#viz-tip b { color: var(--primary); display: block; margin-bottom: 3px; }

/* ---------- Key terms ---------- */
.terms { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px; margin-top: 10px; }
.term {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 12px 14px;
  cursor: pointer;
  transition: border-color .2s, box-shadow .2s;
}
.term:hover { box-shadow: var(--shadow-1); }
.term b { display: block; color: var(--primary); font-size: 14px; font-weight: 600; }
.term span { display: none; font-size: 13.5px; color: var(--text-dim); margin-top: 6px; }
.term.open span { display: block; }
.term::after { content: "tap to reveal"; font-size: 10px; color: var(--text-faint); }
.term.open::after { content: ""; }

/* ---------- Quiz ---------- */
.quiz { margin-top: 8px; }
.quiz .q-card {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin: 14px 0;
}
.quiz .q-num { font-size: 11px; font-weight: 600; letter-spacing: 1px; color: var(--text-faint); }
.quiz .q-text { font-size: 16px; font-weight: 500; color: var(--text); margin: 6px 0 12px; }
.quiz .opts { display: grid; gap: 8px; }
.quiz .opt {
  text-align: left;
  background: var(--bg-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: var(--r-md);
  padding: 10px 14px;
  font-size: 15px; font-family: var(--sans);
  cursor: pointer;
  transition: all .15s;
}
.quiz .opt:hover { box-shadow: var(--shadow-1); }
.quiz .opt.sel { border-color: var(--primary); background: var(--tint-primary); }
.quiz .opt.correct { border-color: var(--green); background: var(--tint-green); }
.quiz .opt.wrong { border-color: var(--pink); background: var(--tint-red); }
.quiz .opt:disabled { cursor: default; }
.quiz .why {
  display: none;
  margin-top: 10px; padding: 10px 14px;
  border-left: 3px solid var(--orange);
  background: var(--tint-orange);
  border-radius: 0 var(--r-md) var(--r-md) 0;
  font-size: 14px; color: var(--text-secondary);
}
.quiz .why.show { display: block; }
.quiz .blank-input {
  font-family: var(--mono); font-size: 15px;
  background: var(--bg-2); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--r-xs);
  padding: 9px 12px; width: 220px; max-width: 100%;
}
.quiz .blank-input:focus { outline: none; border-color: var(--primary); box-shadow: var(--shadow-1); }
.quiz .order-item {
  display: block; width: 100%; text-align: left;
  background: var(--bg-2); border: 1px solid var(--line);
  color: var(--text); border-radius: var(--r-md);
  padding: 10px 14px; margin: 6px 0;
  font-size: 15px; cursor: pointer;
}
.quiz .order-item.picked { border-color: var(--primary); color: var(--text-dim); }
.quiz .order-item .seq { font-weight: 600; color: var(--primary); margin-right: 8px; }
.quiz .result {
  margin-top: 16px; padding: 16px 20px;
  border-radius: var(--radius); text-align: center;
  font-size: 16px;
  border: 1px solid var(--line);
  background: var(--bg);
}
.quiz .result b { color: var(--primary); font-size: 22px; }

/* ---------- Chapter footer nav ---------- */
.ch-nav { display: flex; justify-content: space-between; gap: 12px; margin-top: 40px; }
.ch-nav a {
  flex: 1;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 18px;
  text-decoration: none; color: var(--text);
  transition: box-shadow .2s;
}
.ch-nav a:hover { box-shadow: var(--shadow-1); }
.ch-nav a.next { text-align: right; }
.ch-nav .dir { font-size: 11px; font-weight: 600; letter-spacing: 1px; color: var(--text-faint); display: block; }
.ch-nav .t { color: var(--primary); font-size: 15px; font-weight: 500; }

/* Confetti canvas overlays everything briefly on quiz success */
#confetti {
  position: fixed; inset: 0; z-index: 90; pointer-events: none;
}

/* ---------- Paywall gate overlay ---------- */
body.gated { overflow: hidden; }
#auth-gate {
  position: fixed; inset: 0; z-index: 200;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background: var(--bg);
}
.gate-card {
  width: 100%; max-width: 460px; text-align: center;
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: var(--r-xl); padding: 38px 34px;
  box-shadow: var(--shadow-2);
}
.gate-brand {
  font-weight: 700; font-size: 15px;
  letter-spacing: -0.125px; color: var(--text); margin-bottom: 18px;
}
.gate-brand b { color: var(--primary); }
.gate-tag {
  display: inline-block; font-size: 11px; font-weight: 600;
  letter-spacing: 1px; padding: 4px 11px; border-radius: var(--r-full);
  border: 1px solid var(--line); color: var(--primary); margin-bottom: 16px;
  background: var(--bg-2);
}
.gate-card h1 { font-size: 26px; font-weight: 700; letter-spacing: -0.625px; color: var(--text); margin-bottom: 10px; border: none; padding: 0; }
.gate-card h1::before { content: none; }
.gate-card p { color: var(--text-dim); font-size: 15px; margin: 10px 0; }
.gate-card strong { color: var(--text); }
.gate-card code {
  font-family: var(--mono); font-size: 12.5px; color: var(--orange);
  background: var(--bg-3); padding: 1px 6px; border-radius: var(--r-xs);
}
.gate-fine { font-size: 13px; color: var(--text-dim); }
.gate-btn {
  display: inline-block; margin-top: 22px; padding: 12px 30px;
  border: none; border-radius: var(--r-full); cursor: pointer; text-decoration: none;
  font-family: var(--sans); font-weight: 500; font-size: 15px; color: var(--on-primary);
  background: var(--primary);
  transition: background .15s, transform .15s;
}
.gate-btn:hover { background: var(--primary-active); }
.gate-btn:active { transform: scale(.97); }
.gate-btn:disabled { opacity: .7; cursor: default; transform: none; }
.gate-links { margin-top: 22px; display: flex; justify-content: center; gap: 20px; }
.gate-links a { color: var(--text-dim); text-decoration: none; font-size: 14px; }
.gate-links a:hover { color: var(--primary); }
.gate-spinner {
  width: 34px; height: 34px; margin: 6px auto 16px;
  border: 3px solid var(--line); border-top-color: var(--primary);
  border-radius: 50%; animation: gate-spin .8s linear infinite;
}
@keyframes gate-spin { to { transform: rotate(360deg); } }

/* ---------- Account chip in the top bar ---------- */
#account {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: var(--text-dim);
}
#account button {
  background: var(--bg-2); border: 1px solid var(--line); color: var(--text-dim);
  font-family: var(--sans); font-size: 12px; font-weight: 500; padding: 4px 10px;
  border-radius: var(--r-md); cursor: pointer; transition: all .2s;
}
#account button:hover { color: var(--text); box-shadow: var(--shadow-1); }

/* ---------- Static figure diagrams ---------- */
/* Framed like .viz but for inline SVG illustrations with a caption.
   Sits as a soft "image well" on the white card. */
.fig {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin: 18px 0 8px;
  padding: 16px 16px 12px;
}
.fig svg { display: block; width: 100%; height: auto; }
.fig .fig-cap {
  margin-top: 10px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--text-dim);
}
.fig .fig-cap::before { content: "▣ "; color: var(--primary); }

/* ---------- Responsive ---------- */
@media (max-width: 720px) {
  body { font-size: 15px; }
  #app { padding: 84px 14px 90px; }
  section.block { padding: 20px 16px; }
  #progress-bar { width: 80px; }
  .ch-nav { flex-direction: column; }
  .ch-nav a.next { text-align: left; }
}

/* Respect users who prefer less motion */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  section.block { transition: none; opacity: 1; transform: none; }
}
