/* ============================================================================
   Bot-ler — stylesheet
   Terminal aesthetic inherited from the original single-file build. Mobile-first:
   every layout rule below assumes a phone and then widens at 860px.

   iPhone specifics that are load-bearing, not decoration:
     - env(safe-area-inset-*) on the header, nav and composer, so content does
       not sit under the notch or the home-indicator bar.
     - 100dvh (not 100vh) for app height, so the layout does not get shoved up
       when Safari's URL bar collapses.
     - 16px minimum font-size on the textarea — anything smaller makes iOS
       Safari auto-zoom the whole page on focus.
     - 44px minimum tap targets on nav and buttons (Apple HIG).
   ========================================================================== */

:root {
  --bg: #0a0e12;
  --panel: #0f151b;
  --panel-2: #131b23;
  --fg: #cfe8d8;
  --dim: #5f7a6c;
  --accent: #3ad07a;
  --user: #7fd1ff;
  --border: #1c2730;
  --danger: #e0574f;
  --gold: #c9a84c;
  --nav-h: 56px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.6 "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  /* Never let the page itself scroll sideways; wide children scroll internally. */
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

/* dvh keeps the app the height of the *visible* viewport on iOS. */
body { height: 100dvh; display: flex; flex-direction: column; }

/* SHELL VISIBILITY.
   #shell is display:contents so header / nav / main / footer are direct flex
   children of body — a real wrapper box would break the column layout.
   Because display:contents beats the `hidden` attribute, visibility is gated by
   a class on <body> instead: js/app.js adds .granted once access resolves.
   Do not swap this back to [hidden] — the shell will paint before the gate. */
#shell { display: contents; }
body:not(.granted) #shell { display: none; }

a { color: var(--accent); }
button { font: inherit; cursor: pointer; }
button:disabled { opacity: .4; cursor: default; }

/* ── Header + hamburger ─────────────────────────────────────────────────── */
header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(12px + env(safe-area-inset-top)) 14px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
header .dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent); box-shadow: 0 0 10px var(--accent);
  flex: 0 0 auto;
}
header b { letter-spacing: .5px; font-size: .95rem; }
header .tag {
  color: var(--dim); font-size: 11px;
  /* Hidden on phones — there is no room for it next to the nav button. */
  display: none;
}
header .spacer { margin-left: auto; }

#navToggle {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  background: transparent; border: 1px solid var(--border);
  border-radius: 8px; color: var(--fg);
}
#navToggle svg { width: 20px; height: 20px; }

/* ── Nav ────────────────────────────────────────────────────────────────
   Phone: a drawer that slides down under the header, toggled by #navToggle.
   Desktop: a permanent horizontal tab row. One markup, two behaviours. */
nav {
  flex: 0 0 auto;
  background: var(--panel-2);
  border-bottom: 1px solid var(--border);
  display: none;
  flex-direction: column;
}
nav.open { display: flex; }
nav button {
  min-height: 48px;
  display: flex; align-items: center; gap: 10px;
  padding: 0 18px;
  background: transparent; border: 0; border-bottom: 1px solid var(--border);
  color: var(--dim); text-align: left;
  letter-spacing: .06em; text-transform: uppercase; font-size: 12px;
}
nav button:last-child { border-bottom: 0; }
nav button.active { color: var(--accent); background: rgba(58, 208, 122, .07); }
nav button .n {
  width: 22px; height: 22px; flex: 0 0 auto;
  display: grid; place-items: center;
  border: 1px solid currentColor; border-radius: 50%;
  font-size: 10px;
}

/* ── Page container ─────────────────────────────────────────────────────── */
main { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.page { display: none; flex: 1 1 auto; min-height: 0; flex-direction: column; }
.page.active { display: flex; }

.scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 18px 14px calc(18px + env(safe-area-inset-bottom));
}

.page-head {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.page-head h2 {
  margin: 0; font-size: 12px; letter-spacing: .12em;
  text-transform: uppercase; color: var(--dim); font-weight: 600;
}
.page-head .spacer { margin-left: auto; }

.btn {
  min-height: 36px;
  padding: 0 14px;
  background: var(--accent); color: #04130a;
  border: 0; border-radius: 8px;
  font-weight: 600; font-size: 13px;
}
.btn.ghost { background: transparent; border: 1px solid var(--border); color: var(--fg); }
.btn.ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn.danger { background: transparent; border: 1px solid var(--border); color: var(--danger); }
.btn.sm { min-height: 30px; padding: 0 10px; font-size: 11px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--border); background: rgba(255, 255, 255, .04);
  font-size: 10px; letter-spacing: .08em; text-transform: uppercase; color: var(--dim);
}
.pill.on { color: var(--accent); border-color: rgba(58, 208, 122, .4); }
.pill.saved { color: var(--gold); border-color: rgba(201, 168, 76, .4); }

.empty { color: var(--dim); text-align: center; padding: 40px 20px; line-height: 1.8; }

/* ── Chat ───────────────────────────────────────────────────────────────── */
.msg { margin: 0 0 18px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }

/* SELECT-ALL FIX.
   The speaker label used to be real text ("you" / "bot-ler"), so Cmd-A + copy
   dragged every label into the clipboard and the transcript was unusable.
   CSS generated content is not part of the document text, so the browser
   cannot select or copy it — the label still renders, but Cmd-A now yields
   only the actual conversation. Do not turn these back into text nodes. */
.msg .who {
  display: block;
  margin-bottom: 3px;
  font-size: 11px; text-transform: uppercase; letter-spacing: 1px;
  color: var(--dim);
  user-select: none; -webkit-user-select: none;
}
.msg.user .who::before { content: "you"; }
.msg.bot .who::before { content: "bot-ler"; }
.msg.user .who { color: var(--user); }
.msg.user .body { color: #dff1ff; }
.msg.bot .who { color: var(--accent); }

.msg .body a { word-break: break-all; }

/* Truncation warning — surfaced rather than hidden, so a cut-off answer is
   obvious instead of looking like the model trailed off. */
.msg .trunc {
  display: block; margin-top: 8px;
  color: var(--gold); font-size: 11px;
  user-select: none; -webkit-user-select: none;
}

/* THINKING DOTS.
   Requirement was a 1-2-3 wave, not three dots blinking in unison, so each dot
   carries its own animation-delay one third of the cycle apart. */
.thinking { display: flex; align-items: center; gap: 6px; height: 20px; }
.thinking span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
  animation: dotWave 1.2s infinite ease-in-out;
}
.thinking span:nth-child(2) { animation-delay: .2s; }
.thinking span:nth-child(3) { animation-delay: .4s; }
@keyframes dotWave {
  0%, 70%, 100% { opacity: .18; transform: translateY(0); }
  35%           { opacity: 1;   transform: translateY(-3px); }
}
@media (prefers-reduced-motion: reduce) {
  .thinking span { animation-duration: 2s; }
  @keyframes dotWave { 0%, 100% { opacity: .3; transform: none; } 50% { opacity: 1; transform: none; } }
}

/* Composer */
form#chatForm {
  flex: 0 0 auto;
  display: flex; gap: 10px;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--panel);
}
#input {
  flex: 1 1 auto;
  min-width: 0;
  max-height: 40vh;
  background: #0a0f14; border: 1px solid var(--border);
  color: var(--fg); border-radius: 8px;
  padding: 11px 13px;
  /* MUST stay >= 16px: iOS Safari auto-zooms the whole page when a focused
     input's font-size is smaller, and the zoom is not undone on blur.
     Written as longhands on purpose — `font: 16px/1.5 inherit` is INVALID
     (`inherit` is not a legal family inside the shorthand), so the browser
     drops the whole declaration and the field silently falls back to ~13px. */
  font-family: inherit;
  font-size: 16px;
  line-height: 1.5;
  resize: none;
}
#input:focus { outline: none; border-color: var(--accent); }
#send { flex: 0 0 auto; min-width: 64px; }

/* Debug footer — token + cache accounting. Hidden unless ?debug=1. */
#debug {
  display: none;
  flex: 0 0 auto;
  padding: 6px 14px; border-top: 1px solid var(--border);
  background: #080c10; color: var(--dim); font-size: 10px;
}
body.debug #debug { display: block; }

/* ── Sessions list ──────────────────────────────────────────────────────── */
.card {
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--panel);
  padding: 12px 14px; margin-bottom: 10px;
}
.card h3 { margin: 0 0 4px; font-size: 14px; color: var(--fg); font-weight: 600; }
.card .meta { color: var(--dim); font-size: 11px; }
.card .row { display: flex; align-items: center; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

/* ── Downloads ──────────────────────────────────────────────────────────── */
/* Horizontal tab strip. Scrolls inside itself so the page body never does. */
.dl-tabs {
  flex: 0 0 auto;
  display: flex; gap: 6px;
  padding: 10px 14px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
}
.dl-tabs button {
  flex: 0 0 auto;
  min-width: 40px; min-height: 36px;
  border: 1px solid var(--border); border-radius: 8px;
  background: transparent; color: var(--dim);
  font-size: 12px;
}
.dl-tabs button.active { color: #04130a; background: var(--accent); border-color: var(--accent); }
.dl-tabs button.done { border-color: rgba(201, 168, 76, .55); color: var(--gold); }
.dl-tabs button.done.active { background: var(--gold); color: #1a1405; }
.dl-tabs button:disabled { opacity: .35; }

.dl-head { margin-bottom: 14px; }
.dl-head .wk { color: var(--dim); font-size: 11px; letter-spacing: .1em; text-transform: uppercase; }
.dl-head h1 { margin: 4px 0 10px; font-size: 1.2rem; line-height: 1.35; color: var(--fg); }
.dl-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 18px; }

/* Rendered lesson markdown */
.lesson { line-height: 1.75; }
.lesson h1, .lesson h2, .lesson h3 { line-height: 1.35; color: var(--fg); }
.lesson h1 { font-size: 1.15rem; margin: 26px 0 10px; }
.lesson h2 { font-size: 1rem; margin: 24px 0 8px; color: var(--accent); }
.lesson h3 { font-size: .9rem; margin: 20px 0 6px; color: var(--gold); }
.lesson p { margin: 0 0 14px; }
.lesson ul, .lesson ol { margin: 0 0 14px; padding-left: 22px; }
.lesson li { margin-bottom: 6px; }
.lesson strong { color: #eaf7ef; }
.lesson em { color: var(--gold); font-style: italic; }
.lesson blockquote {
  margin: 0 0 14px; padding: 10px 14px;
  border-left: 2px solid var(--accent); background: rgba(58, 208, 122, .06);
  color: var(--fg);
}
.lesson hr { border: 0; border-top: 1px solid var(--border); margin: 22px 0; }
/* Wide content scrolls in its own box — the page never scrolls sideways. */
.lesson pre { overflow-x: auto; background: #080c10; padding: 12px; border-radius: 8px; }
.lesson code { background: rgba(255, 255, 255, .06); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.lesson pre code { background: none; padding: 0; }

/* Six-week calendar */
.cal { margin-bottom: 20px; }
.cal h2 { font-size: 11px; letter-spacing: .12em; text-transform: uppercase; color: var(--dim); margin: 0 0 10px; }
.cal-grid { display: grid; grid-template-columns: 1fr; gap: 8px; }
.cal-week {
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--panel); padding: 10px 12px;
}
.cal-week .wk-label { font-size: 10px; letter-spacing: .1em; text-transform: uppercase; color: var(--dim); margin-bottom: 8px; }
.cal-slot {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 0; font-size: 12px;
}
.cal-slot .box {
  width: 16px; height: 16px; flex: 0 0 auto;
  border: 1px solid var(--border); border-radius: 4px;
  display: grid; place-items: center; font-size: 10px; color: var(--gold);
}
.cal-slot.done .box { border-color: var(--gold); }
.cal-slot .when { margin-left: auto; color: var(--dim); font-size: 10px; }

.daypick { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0 4px; }
.daypick button {
  min-width: 42px; min-height: 34px;
  border: 1px solid var(--border); border-radius: 8px;
  background: transparent; color: var(--dim); font-size: 11px;
}
.daypick button.on { color: #04130a; background: var(--accent); border-color: var(--accent); }
.hint { color: var(--dim); font-size: 11px; line-height: 1.6; }

/* ── Login / gate overlay ───────────────────────────────────────────────── */
#gate {
  position: fixed; inset: 0; z-index: 999;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  padding: 24px calc(24px + env(safe-area-inset-right)) 24px calc(24px + env(safe-area-inset-left));
  overflow-y: auto;
}
#gate .box { width: 100%; max-width: 380px; text-align: center; }
#gate .g-dot {
  width: 11px; height: 11px; border-radius: 50%; margin: 0 auto 18px;
  background: var(--accent); box-shadow: 0 0 14px var(--accent);
}
#gate h1 { letter-spacing: 3px; margin: 0 0 6px; font-size: 1.5rem; }
#gate .sub { color: var(--dim); margin: 0 0 22px; font-size: 12px; }
#gate .msg-box { color: var(--fg); opacity: .88; line-height: 1.7; margin: 0 0 22px; font-size: 13px; }
#gate form { display: flex; flex-direction: column; gap: 10px; text-align: left; }
#gate input {
  min-height: 44px;
  background: #0a0f14; border: 1px solid var(--border); color: var(--fg);
  border-radius: 8px; padding: 0 13px;
  /* Longhands, not the `font` shorthand — see the note on #input above. */
  font-family: inherit;
  font-size: 16px;
  line-height: 1.2;
}
#gate input:focus { outline: none; border-color: var(--accent); }
#gate .btn { min-height: 44px; }
#gate .or { color: var(--dim); font-size: 11px; text-align: center; margin: 2px 0; }
#gate .err { color: var(--danger); font-size: 12px; min-height: 16px; line-height: 1.5; }
#gate .back { display: inline-block; margin-top: 18px; color: var(--accent); text-decoration: none; font-size: 12px; }
#gate .foot { color: var(--dim); font-size: 11px; margin-top: 14px; line-height: 1.6; }

/* ── Desktop ────────────────────────────────────────────────────────────── */
@media (min-width: 860px) {
  header .tag { display: inline; }
  #navToggle { display: none; }

  /* Drawer becomes a permanent tab row. */
  nav {
    display: flex !important;
    flex-direction: row;
    padding: 0 14px;
  }
  nav button {
    min-height: 44px;
    border-bottom: 2px solid transparent;
    padding: 0 16px;
  }
  nav button:last-child { border-bottom: 2px solid transparent; }
  nav button.active { border-bottom-color: var(--accent); background: transparent; }
  nav button .n { display: none; }

  .scroll { padding: 26px 32px; }
  .scroll > * { max-width: 820px; margin-left: auto; margin-right: auto; }
  .cal-grid { grid-template-columns: repeat(3, 1fr); }
  form#chatForm { padding: 14px 32px; }
  form#chatForm > * { }
}
