/* ============================================================
   IFR Flight Deck — Jeppesen Instrument/Commercial Companion
   Aesthetic: flight-deck placard. Chart paper by day, panel by night.
   Type: Barlow Condensed (placard) · Barlow (body) · IBM Plex Mono (values)
   ============================================================ */
:root {
  --font-placard: "Barlow Condensed", "Arial Narrow", sans-serif;
  --font-body: "Barlow", "Helvetica Neue", sans-serif;
  --font-mono: "IBM Plex Mono", "Menlo", monospace;

  --bg: #f4f1ea; --bg-raised: #fdfcf8; --bg-sunken: #eae6db;
  --ink: #191d24; --ink-soft: #4c5361; --ink-faint: #8a8f9b;
  --line: #d5d0c3; --line-strong: #b9b2a0;
  --red: #c22a2a; --red-bg: #faeceb;
  --amber: #a06b0a; --amber-bg: #f9f0dc;
  --green: #2f7a3d; --green-bg: #e9f2e9;
  --blue: #1f5f96; --blue-bg: #e8f0f7;
  --magenta: #a3238e; --magenta-bg: #f6e9f3;
  --accent: #1f5f96;
  --shadow: 0 1px 2px rgba(25,29,36,.07), 0 4px 14px rgba(25,29,36,.06);
  --radius: 10px;
  /* instrument colors */
  --hsi-face: #0d1013; --hsi-card: #f3f4f2; --cyan: #22d3ee; --amberd: #ffb52e; --grn: #34d399; --white: #f5f7f8;
}
[data-theme="night"] {
  --bg: #0c0f14; --bg-raised: #151a22; --bg-sunken: #080a0e;
  --ink: #e8e6df; --ink-soft: #a8adb8; --ink-faint: #6b7280;
  --line: #262d38; --line-strong: #38414f;
  --red: #ff5449; --red-bg: #2a1412;
  --amber: #ffb52e; --amber-bg: #271d08;
  --green: #4cd964; --green-bg: #10240f;
  --blue: #58a6e0; --blue-bg: #0e2233;
  --magenta: #e879c7; --magenta-bg: #2a1226;
  --accent: #58a6e0;
  --shadow: 0 1px 2px rgba(0,0,0,.5), 0 6px 18px rgba(0,0,0,.35);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body { margin: 0; font-family: var(--font-body); font-size: 16px; line-height: 1.5; background: var(--bg); color: var(--ink); }
body::before {
  content: ""; position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)' opacity='.05'/%3E%3C/svg%3E");
  mix-blend-mode: multiply;
}
[data-theme="night"] body::before { mix-blend-mode: overlay; opacity: .5; }

#app { position: relative; z-index: 1; max-width: 1060px; margin: 0 auto; padding: 0 0 84px; }
@media (min-width: 720px) { #app { padding: 0 20px 92px; } }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
h1,h2,h3,h4 { font-family: var(--font-placard); letter-spacing: .02em; margin: 0; }
::selection { background: var(--accent); color: var(--bg); }
.loading { padding: 60px 16px; text-align: center; color: var(--ink-faint); font-family: var(--font-mono); }

/* ---------- masthead ---------- */
#masthead {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px 10px; border-bottom: 2px solid var(--ink);
  position: sticky; top: 0; z-index: 30;
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px);
}
.mast-ident { display: flex; align-items: baseline; gap: 10px; text-decoration: none; color: var(--ink); }
.mast-ident:hover { text-decoration: none; }
.mast-type { font-family: var(--font-placard); font-weight: 700; font-size: 27px; letter-spacing: .08em; line-height: 1; }
.mast-sub { font-family: var(--font-mono); font-size: 9.5px; letter-spacing: .1em; color: var(--ink-soft); }
@media (max-width: 520px) { .mast-sub { display: none; } }
.mast-tools { display: flex; gap: 8px; }
.mast-tools button { display: grid; place-items: center; width: 40px; height: 34px; border: 1.5px solid var(--line-strong); border-radius: 7px; background: var(--bg-raised); transition: border-color .15s, transform .1s; }
.mast-tools button:active { transform: translateY(1px); }
.mast-tools svg { width: 17px; height: 17px; }
#theme-btn { width: auto; padding: 0 10px; font-family: var(--font-mono); font-size: 11px; letter-spacing: .1em; }
#theme-btn .theme-night, [data-theme="night"] #theme-btn .theme-day { display: none; }
[data-theme="night"] #theme-btn .theme-night { display: inline; color: var(--green); }

/* ---------- tab bar ---------- */
#tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  display: flex; justify-content: space-around;
  background: color-mix(in srgb, var(--bg-raised) 94%, transparent); backdrop-filter: blur(12px);
  border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
#tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 0; color: var(--ink-faint); font-family: var(--font-placard); font-size: 11px; letter-spacing: .04em; text-transform: uppercase; }
#tabbar a:hover { text-decoration: none; }
#tabbar svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
#tabbar a.active { color: var(--accent); }
@media (min-width: 720px) { #tabbar { max-width: 1060px; margin: 0 auto; border-radius: 14px 14px 0 0; border: 1px solid var(--line); border-bottom: 0; } }

/* ---------- generic layout ---------- */
.wrap { padding: 16px; }
.eyebrow { font-family: var(--font-mono); font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--ink-faint); }
.page-h { font-size: 30px; font-weight: 700; margin: 2px 0 4px; }
.lead { color: var(--ink-soft); margin: 0 0 16px; max-width: 62ch; }
.grid { display: grid; gap: 12px; }
@media (min-width: 640px) { .grid.cols2 { grid-template-columns: 1fr 1fr; } .grid.cols3 { grid-template-columns: repeat(3,1fr); } }

.card { background: var(--bg-raised); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card h3 { font-size: 19px; }
.muted { color: var(--ink-soft); }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border: 1.5px solid var(--accent); border-radius: 8px; color: var(--accent); font-family: var(--font-placard); font-weight: 600; letter-spacing: .03em; background: var(--bg-raised); }
.btn:hover { text-decoration: none; background: var(--blue-bg); }
.btn.solid { background: var(--accent); color: var(--bg); }
.btn.ghost { border-color: var(--line-strong); color: var(--ink-soft); }
.chip { display: inline-flex; align-items: center; gap: 5px; padding: 4px 10px; border: 1px solid var(--line-strong); border-radius: 999px; font-family: var(--font-mono); font-size: 12px; color: var(--ink-soft); background: var(--bg-raised); }
.chip.on { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.pill { font-family: var(--font-mono); font-size: 10.5px; letter-spacing: .08em; padding: 2px 8px; border-radius: 999px; text-transform: uppercase; }
.pill.p1 { background: var(--blue-bg); color: var(--blue); }
.pill.p2 { background: var(--amber-bg); color: var(--amber); }
.pill.p3 { background: var(--magenta-bg); color: var(--magenta); }

/* source chip */
.src { display: inline-flex; align-items: center; gap: 5px; font-family: var(--font-mono); font-size: 11px; color: var(--ink-soft); background: var(--bg-sunken); border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; }
.src::before { content: "▤"; font-size: 11px; opacity: .7; }
.src:hover { text-decoration: none; border-color: var(--accent); color: var(--accent); }

/* ---------- home ---------- */
.hero { border: 2px solid var(--ink); border-radius: 14px; padding: 20px; background: linear-gradient(135deg, var(--bg-raised), var(--bg-sunken)); margin-bottom: 16px; }
.hero .big { font-size: 34px; font-weight: 700; line-height: 1.05; }
.stat-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.stat { background: var(--bg-raised); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; min-width: 78px; }
.stat b { font-family: var(--font-mono); font-size: 22px; display: block; }
.stat span { font-size: 11px; color: var(--ink-faint); text-transform: uppercase; letter-spacing: .06em; }
.progress { height: 8px; background: var(--bg-sunken); border-radius: 999px; overflow: hidden; margin-top: 8px; }
.progress > i { display: block; height: 100%; background: var(--green); border-radius: 999px; }
.tile { display: flex; flex-direction: column; gap: 4px; }
.tile .k { font-family: var(--font-placard); font-weight: 600; font-size: 18px; }
.quick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (min-width: 640px) { .quick { grid-template-columns: repeat(4,1fr); } }
.quick a { display: flex; flex-direction: column; gap: 6px; padding: 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg-raised); color: var(--ink); }
.quick a:hover { text-decoration: none; border-color: var(--accent); }
.quick svg { width: 24px; height: 24px; stroke: var(--accent); fill: none; stroke-width: 1.8; }

/* ---------- study: chapter index ---------- */
.part-h { margin: 22px 0 8px; display: flex; align-items: baseline; gap: 10px; }
.part-h .n { font-family: var(--font-mono); color: var(--magenta); font-size: 13px; letter-spacing: .1em; }
.ch-card { display: block; border: 1px solid var(--line); border-radius: 10px; background: var(--bg-raised); padding: 4px 0; margin-bottom: 10px; overflow: hidden; }
.ch-head { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.ch-num { font-family: var(--font-placard); font-weight: 700; font-size: 26px; color: var(--accent); min-width: 34px; }
.ch-title { font-family: var(--font-placard); font-weight: 600; font-size: 19px; }
.sec-row { display: flex; align-items: center; gap: 10px; padding: 9px 14px; color: var(--ink); border-top: 1px dashed var(--line); }
.sec-row:first-of-type { border-top: 0; }
.sec-row:hover { text-decoration: none; background: var(--bg-sunken); }
.sec-letter { font-family: var(--font-mono); font-weight: 600; color: var(--magenta); width: 20px; }
.sec-name { flex: 1; }
.sec-row .done { color: var(--green); }
.sec-meta { font-family: var(--font-mono); font-size: 11px; color: var(--ink-faint); }

/* ---------- section reader ---------- */
.reader { max-width: 70ch; }
.reader .crumb { font-family: var(--font-mono); font-size: 12px; color: var(--ink-faint); margin-bottom: 4px; }
.reader h1 { font-size: 28px; margin-bottom: 6px; }
.reader .toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 10px 0 18px; }
.reader p { margin: 0 0 14px; }
.reader .prose p { text-align: left; }
.block { border-left: 3px solid var(--line-strong); padding: 2px 0 2px 14px; margin: 18px 0; }
.block.sum { border-color: var(--green); }
.block.terms { border-color: var(--magenta); }
.block h4 { font-size: 13px; letter-spacing: .12em; text-transform: uppercase; color: var(--ink-faint); margin-bottom: 8px; }
.sum ul { margin: 0; padding: 0; list-style: none; }
.sum li { position: relative; padding-left: 22px; margin-bottom: 8px; }
.sum li::before { content: "✓"; position: absolute; left: 0; color: var(--green); font-weight: 700; }
.termgrid { display: grid; grid-template-columns: 1fr; gap: 4px 16px; }
@media (min-width: 560px) { .termgrid { grid-template-columns: 1fr 1fr; } }
.term { padding: 3px 0; border-bottom: 1px dotted var(--line); font-size: 15px; }
.figs { font-family: var(--font-mono); font-size: 12px; color: var(--ink-faint); }
.reader-nav { display: flex; justify-content: space-between; gap: 10px; margin: 24px 0 10px; }

/* ---------- quiz ---------- */
.quiz-setup .grid { margin-top: 12px; }
.q-card { border: 1px solid var(--line); border-radius: 10px; background: var(--bg-raised); padding: 16px; margin-bottom: 14px; }
.q-num { font-family: var(--font-mono); font-size: 12px; color: var(--ink-faint); }
.q-text { font-size: 17px; font-weight: 500; margin: 4px 0 12px; }
.choices { display: grid; gap: 8px; }
.choice { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 8px; text-align: left; }
.choice:hover { border-color: var(--accent); }
.choice .l { font-family: var(--font-mono); font-weight: 600; color: var(--accent); }
.choice.correct { border-color: var(--green); background: var(--green-bg); }
.choice.wrong { border-color: var(--red); background: var(--red-bg); }
.choice.chosen { border-width: 2px; }
.reveal { margin-top: 12px; padding: 12px; border-radius: 8px; background: var(--bg-sunken); border: 1px dashed var(--line-strong); }
.reveal .lbl { font-family: var(--font-mono); font-size: 11px; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-faint); }
.q-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.score-big { font-family: var(--font-mono); font-size: 40px; font-weight: 600; }
.tag { font-family: var(--font-mono); font-size: 10px; padding: 2px 7px; border-radius: 5px; background: var(--bg-sunken); color: var(--ink-soft); text-transform: uppercase; letter-spacing: .06em; }
.tag.open { background: var(--amber-bg); color: var(--amber); }
.tag.mc { background: var(--blue-bg); color: var(--blue); }

/* ---------- flashcards ---------- */
.flash-wrap { max-width: 520px; margin: 0 auto; }
.flash { perspective: 1400px; height: 300px; margin: 14px 0; }
.flash-inner { position: relative; width: 100%; height: 100%; transition: transform .5s; transform-style: preserve-3d; cursor: pointer; }
.flash.flipped .flash-inner { transform: rotateY(180deg); }
.flash-face { position: absolute; inset: 0; backface-visibility: hidden; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 24px; border: 1.5px solid var(--line-strong); border-radius: 14px; background: var(--bg-raised); box-shadow: var(--shadow); }
.flash-face.back { transform: rotateY(180deg); background: var(--bg-sunken); }
.flash-term { font-family: var(--font-placard); font-weight: 700; font-size: 26px; }
.flash-def { font-size: 16px; color: var(--ink); overflow-y: auto; }
.flash-hint { position: absolute; bottom: 10px; font-family: var(--font-mono); font-size: 10px; color: var(--ink-faint); }
.flash-bar { display: flex; gap: 8px; justify-content: center; margin-top: 10px; }

/* ---------- trainers ---------- */
.trainer-list { display: grid; gap: 12px; }
@media (min-width: 640px) { .trainer-list { grid-template-columns: 1fr 1fr; } }
.tr-card { display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg-raised); padding: 16px; color: var(--ink); }
.tr-card:hover { text-decoration: none; border-color: var(--accent); }
.tr-card .tr-title { font-family: var(--font-placard); font-weight: 700; font-size: 20px; }
.tr-card .tr-blurb { color: var(--ink-soft); font-size: 14px; }
.subtabs { display: flex; gap: 6px; margin: 12px 0 16px; border-bottom: 1px solid var(--line); }
.subtabs button { padding: 8px 14px; font-family: var(--font-placard); font-weight: 600; letter-spacing: .03em; color: var(--ink-faint); border-bottom: 2px solid transparent; }
.subtabs button.active { color: var(--accent); border-color: var(--accent); }
.trainer-stage { display: grid; gap: 18px; }
@media (min-width: 780px) { .trainer-stage.split { grid-template-columns: minmax(0,1fr) 300px; align-items: start; } }
.instrument { background: var(--hsi-face); border-radius: 16px; padding: 10px; box-shadow: inset 0 0 40px rgba(0,0,0,.6), var(--shadow); }
.instrument svg { display: block; width: 100%; height: auto; }
.controls { display: grid; gap: 16px; }
.ctrl { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.ctrl-label { font-family: var(--font-mono); font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-faint); }
.readout { font-family: var(--font-mono); font-size: 15px; background: var(--bg-sunken); border: 1px solid var(--line); border-radius: 6px; padding: 8px 12px; }
.readout b { color: var(--accent); }
.answer-box { border: 1.5px solid var(--green); background: var(--green-bg); border-radius: 10px; padding: 14px; }
.answer-box .big { font-family: var(--font-mono); font-size: 24px; font-weight: 600; }
.steps { list-style: none; padding: 0; margin: 8px 0 0; }
.steps li { padding: 6px 0 6px 26px; position: relative; border-top: 1px dashed var(--line); }
.steps li .s-n { position: absolute; left: 0; font-family: var(--font-mono); color: var(--accent); font-weight: 600; }

/* ---------- rotary knob ---------- */
.knob { display: inline-grid; place-items: center; gap: 6px; touch-action: none; user-select: none; -webkit-user-select: none; }
.knob-svg { width: 84px; height: 84px; cursor: grab; }
.knob:active .knob-svg { cursor: grabbing; }
.knob-ring { fill: #1a1f27; stroke: #3a424e; stroke-width: 2; }
.knob-body { fill: #232a34; stroke: #11151b; stroke-width: 1; }
.knob-tick { stroke: #5b6472; stroke-width: 1.4; }
.knob-dot { fill: var(--cyan); }
.knob-read { display: flex; align-items: baseline; gap: 3px; font-family: var(--font-mono); font-size: 18px; font-weight: 600; }
.knob-unit { font-size: 11px; color: var(--ink-faint); }
.knob-input { width: 68px; font: inherit; text-align: center; border: 1px solid var(--accent); border-radius: 5px; background: var(--bg); color: var(--ink); }

/* ---------- search overlay ---------- */
#search-overlay { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.4); backdrop-filter: blur(3px); display: flex; justify-content: center; padding: 8vh 12px 12px; }
#search-overlay[hidden] { display: none; }
.search-panel { width: 100%; max-width: 640px; background: var(--bg-raised); border: 1px solid var(--line-strong); border-radius: 14px; overflow: hidden; box-shadow: 0 20px 60px rgba(0,0,0,.4); max-height: 82vh; display: flex; flex-direction: column; }
.search-bar { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
#search-input { flex: 1; font: inherit; font-size: 17px; border: 0; background: none; color: var(--ink); outline: none; }
#search-close { font-family: var(--font-mono); font-size: 11px; color: var(--ink-faint); border: 1px solid var(--line); border-radius: 6px; padding: 4px 8px; }
.search-results { overflow-y: auto; padding: 6px; }
.sr { display: block; padding: 10px 12px; border-radius: 8px; color: var(--ink); }
.sr:hover { text-decoration: none; background: var(--bg-sunken); }
.sr .where { font-family: var(--font-mono); font-size: 10.5px; letter-spacing: .08em; color: var(--magenta); text-transform: uppercase; }
.sr .t { font-weight: 600; margin: 2px 0; }
.sr .x { font-size: 13px; color: var(--ink-soft); }
.sr mark, .reveal mark { background: var(--amber-bg); color: var(--amber); border-radius: 2px; }
.search-empty { padding: 24px; text-align: center; color: var(--ink-faint); }

.error-card { margin: 30px 16px; padding: 20px; border: 1px solid var(--red); background: var(--red-bg); border-radius: 12px; color: var(--red); }
.empty { padding: 40px 16px; text-align: center; color: var(--ink-faint); }
