/* ============================================================
   STATE LINES AI: launcher and panel (AI.md section 10).
   Built only from the tokens in styles.css. Dark only, one
   typeface, no gradients, no uppercase, no italics.
   Layers: nav 8000 < launcher 8700 < panel 8900 < grain 9000
   < lightbox 9300. Everything here is position: fixed, so no
   existing page moves.
   ============================================================ */

.ai-root { font-family: var(--sans); font-style: normal; }
.ai-root :focus-visible { outline: 2px solid var(--white); outline-offset: 2px; }
.ai-root strong { color: var(--white); font-weight: 700; }
[data-ai-focus]:focus { outline: none; }
.ai-offscreen { position: fixed; left: -9999px; top: 0; width: 1px; height: 1px; opacity: 0; }

/* ---------- launcher: the site's ghost pill, on dark glass ---------- */
.ai-launch {
  position: fixed; z-index: 8700;
  right: max(24px, env(safe-area-inset-right)); bottom: max(24px, env(safe-area-inset-bottom));
  --bg: rgba(15, 15, 17, 0.84); --fg: var(--white);
  -webkit-backdrop-filter: saturate(140%) blur(14px); backdrop-filter: saturate(140%) blur(14px);
  box-shadow: 0 12px 34px -14px rgba(0, 0, 0, 0.7);
  font-weight: 600; white-space: nowrap;
  transition: transform 0.5s var(--ease), border-color 0.4s var(--ease), background 0.4s var(--ease), opacity 0.3s var(--ease), visibility 0s 0s;
}
.ai-launch:hover { background: rgba(29, 29, 32, 0.92); border-color: var(--white); }
.ai-root.is-open .ai-launch,
.ai-root.is-suppressed .ai-launch,
html.intro-playing .ai-launch {
  opacity: 0; visibility: hidden; pointer-events: none;
  transition: opacity 0.2s var(--ease), visibility 0s 0.2s;
}
/* the phone menu is open (the script also sets .is-suppressed) */
html:has(#nav.is-open) .ai-launch { opacity: 0; visibility: hidden; pointer-events: none; }

/* ---------- panel ---------- */
.ai-panel {
  position: fixed; z-index: 8900;
  right: 24px; bottom: 24px;
  width: min(420px, calc(100vw - 32px));
  height: min(max(75vh, 480px), calc(100vh - 48px));
  height: min(max(75dvh, 480px), calc(100dvh - 48px));
  background: var(--near-black);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-lg);
  box-shadow: 0 32px 90px -24px rgba(0, 0, 0, 0.85), 0 0 0 1px rgba(0, 0, 0, 0.4);
  color: var(--white);
  overflow: hidden;
  opacity: 0; transform: translateY(12px);
  transition: opacity 0.26s var(--ease), transform 0.42s var(--ease-out);
}
.ai-panel[hidden] { display: none; }
.ai-panel.is-in { opacity: 1; transform: none; }
.ai-panel__inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }

.ai-panel.is-expanded {
  inset: 0; width: auto; height: auto;
  border-radius: 0; border: 0; box-shadow: none;
}
.ai-panel.is-expanded .ai-head,
.ai-panel.is-expanded .ai-log,
.ai-panel.is-expanded .ai-form { padding-inline: max(24px, calc((100% - 760px) / 2)); }

/* ---------- header ---------- */
.ai-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 16px 14px 14px 20px;
  border-bottom: 1px solid var(--line);
  flex: none;
}
.ai-head__text { min-width: 0; }
.ai-title-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.ai-title { font-size: 1rem; font-weight: 500; letter-spacing: 0; line-height: 1.25; color: var(--white); }
.ai-sub { font-size: 0.8rem; font-weight: 500; color: var(--muted); line-height: 1.4; margin-top: 3px; }
.ai-chip { font-size: 0.66rem; font-weight: 600; letter-spacing: var(--ls-label); color: var(--soft); border: 1px solid var(--line-strong); border-radius: 99px; padding: 0.15em 0.6em; }
.ai-chip[hidden] { display: none; }
.ai-head__ctl { display: flex; align-items: center; gap: 4px; flex: none; }
.ai-ctl { color: var(--soft); transition: color 0.3s var(--ease), border-color 0.3s var(--ease), background 0.3s var(--ease); }
.ai-ctl:hover { color: var(--white); }
.ai-ctl--text {
  font-size: 0.76rem; font-weight: 600; letter-spacing: -0.005em;
  border: 1px solid var(--line-strong); border-radius: 99px;
  padding: 0 0.85em; min-height: 34px; margin-right: 4px;
}
.ai-ctl--text:hover { border-color: var(--white); }
.ai-ctl--icon { width: 36px; height: 36px; border-radius: 99px; display: inline-flex; align-items: center; justify-content: center; }
.ai-ctl--icon:hover { background: rgba(255, 255, 255, 0.06); }

/* ---------- the conversation ---------- */
.ai-log {
  position: relative; flex: 1 1 auto; min-height: 0;
  overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 18px 20px 22px;
  display: flex; flex-direction: column; gap: 20px;
  scrollbar-width: thin; scrollbar-color: #26272b transparent;
}
.ai-log:focus { outline: none; }
.ai-log::-webkit-scrollbar { width: 8px; }
.ai-log::-webkit-scrollbar-track { background: transparent; }
.ai-log::-webkit-scrollbar-thumb { background: #26272b; border-radius: 99px; border: 2px solid var(--near-black); }

/* welcome */
.ai-welcome { padding-top: 6px; }
.ai-welcome__title { font-size: 1.4rem; font-weight: 500; letter-spacing: 0; line-height: 1.15; color: var(--white); }
.ai-welcome__text { font-size: 0.92rem; font-weight: 500; line-height: 1.55; color: var(--soft); margin-top: 8px; max-width: 36ch; }
.ai-suggest { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; }
.ai-suggest__btn {
  text-align: left; font-size: 0.84rem; font-weight: 500; line-height: 1.3; color: var(--soft);
  border: 1px solid var(--line-strong); border-radius: 99px;
  padding: 0.55em 1em; min-height: 40px;
  transition: color 0.3s var(--ease), border-color 0.3s var(--ease), background 0.3s var(--ease);
}
.ai-suggest__btn:hover { color: var(--white); border-color: var(--white); background: rgba(255, 255, 255, 0.03); }

/* messages */
.ai-msg { min-width: 0; }
.ai-msg--user { display: flex; justify-content: flex-end; }
.ai-bubble {
  max-width: 85%;
  background: var(--charcoal-2); border: 1px solid var(--line);
  border-radius: 16px 16px 4px 16px;
  padding: 0.55rem 0.9rem;
  font-size: 0.92rem; font-weight: 500; line-height: 1.45; color: var(--white);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.ai-text { font-size: 0.95rem; font-weight: 500; line-height: 1.62; color: var(--soft); overflow-wrap: anywhere; }
.ai-text[hidden] { display: none; }
/* detailed answers (AI.md 13.2): paragraphs, bullets and steps with an even rhythm */
.ai-text > * + * { margin-top: 0.75em; }
.ai-text p { text-wrap: pretty; }
.ai-text > .ai-text__head { color: var(--white); font-weight: 600; letter-spacing: -0.01em; }
.ai-text > * + .ai-text__head { margin-top: 1.1em; }
.ai-text > .ai-text__head + * { margin-top: 0.35em; }
.ai-ul { display: grid; gap: 0.35em; }
.ai-ul li { position: relative; padding-left: 1.1em; }
.ai-ul li::before { content: ""; position: absolute; left: 0; top: 0.8em; width: 8px; height: 1px; background: var(--muted); }
.ai-ol { display: grid; gap: 0.35em; list-style: decimal; padding-left: 1.45em; }
.ai-ol li { padding-left: 0.2em; }
.ai-ol li::marker { color: var(--muted); font-weight: 600; font-variant-numeric: tabular-nums; }
/* while the text streams in, it is never the element the browser anchors the scroll to */
.ai-msg.is-streaming .ai-text { overflow-anchor: none; }
.ai-label { font-size: var(--fs-label); font-weight: 500; letter-spacing: var(--ls-label); color: var(--muted); line-height: 1.4; }
.ai-label--general { margin-bottom: 6px; }
.ai-note { font-size: 0.8rem; font-weight: 500; color: var(--muted); margin-top: 10px; line-height: 1.45; }
.ai-note--lead { margin-top: 0; margin-bottom: 8px; }

/* thinking (AI.md 13.3): a quiet status line, three dots and a small Stop */
.ai-think { min-height: 28px; }
.ai-text:not([hidden]) + .ai-think { margin-top: 10px; }
.ai-think__line { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; min-height: 28px; }
.ai-think__label { font-size: 0.86rem; font-weight: 500; color: var(--muted); line-height: 1.4; font-variant-numeric: tabular-nums; }
.ai-think__label[hidden], .ai-think__slow[hidden] { display: none; }
.ai-think__slow { font-size: 0.8rem; font-weight: 500; color: var(--faint); line-height: 1.45; margin-top: 4px; }
.ai-dots { display: inline-flex; align-items: center; gap: 4px; height: 1em; }
.ai-dots i { display: block; width: 5px; height: 5px; border-radius: 50%; background: var(--soft); opacity: 0.25; animation: aiDot 1.4s var(--ease-in-out) infinite; }
.ai-dots i:nth-child(2) { animation-delay: 0.2s; }
.ai-dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes aiDot { 0%, 70%, 100% { opacity: 0.25; } 35% { opacity: 0.85; } }
.ai-think__stop {
  position: relative;
  font-size: 0.74rem; font-weight: 600; color: var(--soft);
  border: 1px solid var(--line-strong); border-radius: 99px;
  padding: 0 0.85em; min-height: 26px; line-height: 1;
  transition: color 0.3s var(--ease), border-color 0.3s var(--ease);
}
.ai-think__stop:hover { color: var(--white); border-color: var(--white); }

/* race summary */
.ai-race { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.ai-race__name { font-size: 1.3rem; font-weight: 500; letter-spacing: 0; line-height: 1.15; color: var(--white); margin-top: 4px; }
.ai-race__meta { font-size: 0.9rem; font-weight: 500; color: var(--soft); margin-top: 4px; line-height: 1.4; }
.ai-race__meta--dim { color: var(--muted); font-size: 0.84rem; }

/* inline links (race block, list items): the site's text link */
.ai-link, .ai-inline {
  display: inline-flex; align-items: center; gap: 0.4em; text-align: left;
  font-weight: 500; color: var(--white);
  border-bottom: 1px solid var(--line-strong); padding-bottom: 1px;
  transition: border-color 0.3s var(--ease), color 0.3s var(--ease);
}
.ai-link { font-size: 0.9rem; margin-top: 12px; }
.ai-inline { font-size: inherit; line-height: inherit; }
.ai-link:hover, .ai-inline:hover { border-color: var(--white); }

/* list */
.ai-list { margin-top: 14px; }
.ai-list .ai-label { margin-bottom: 8px; }
.ai-list .ai-suggest { margin-top: 0; }
.ai-list .ai-ul { font-size: 0.92rem; font-weight: 500; line-height: 1.5; color: var(--soft); }

/* results */
.ai-results { margin-top: 12px; list-style: none; border-bottom: 1px solid var(--line); }
.ai-result {
  width: 100%; display: grid; grid-template-columns: 1fr auto; column-gap: 12px; align-items: center;
  text-align: left; padding: 11px 2px; border-top: 1px solid var(--line);
  transition: background 0.3s var(--ease);
}
.ai-result:hover { background: rgba(255, 255, 255, 0.03); }
.ai-result__title { font-size: 0.92rem; font-weight: 600; color: var(--white); line-height: 1.35; grid-column: 1; }
.ai-result__sub { font-size: 0.8rem; font-weight: 500; color: var(--muted); line-height: 1.4; grid-column: 1; margin-top: 2px; }
.ai-result__arw { grid-column: 2; grid-row: 1 / span 2; color: var(--muted); transition: transform 0.4s var(--ease), color 0.3s var(--ease); }
.ai-result:hover .ai-result__arw { color: var(--white); transform: translateX(3px); }

/* project guide */
.ai-guide { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.ai-guide__title { font-size: 1rem; font-weight: 500; letter-spacing: 0; color: var(--white); margin-top: 4px; line-height: 1.3; }
.ai-guide__sec { margin-top: 12px; }
.ai-guide__head { font-size: 0.86rem; font-weight: 600; color: var(--white); margin-bottom: 6px; line-height: 1.35; }
.ai-guide .ai-ul { font-size: 0.9rem; font-weight: 500; line-height: 1.5; color: var(--soft); }

/* quote from a published chapter */
.ai-quote { margin-top: 14px; }
.ai-quote__text {
  margin-top: 6px; padding-left: 14px; border-left: 1px solid var(--line-strong);
  font-size: 0.92rem; font-weight: 500; line-height: 1.6; color: var(--soft); font-style: normal;
}
.ai-quote__text p + p { margin-top: 0.6em; }

/* sources and actions */
.ai-sources { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 14px; }
.ai-sources__k { font-size: 0.78rem; font-weight: 500; color: var(--muted); margin-right: 2px; }
.ai-source {
  font-size: 0.78rem; font-weight: 500; color: var(--soft);
  border: 1px solid var(--line); border-radius: 99px;
  padding: 0 0.75em; min-height: 30px;
  transition: color 0.3s var(--ease), border-color 0.3s var(--ease);
}
.ai-source:hover { color: var(--white); border-color: var(--line-strong); }
.ai-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.ai-act {
  display: inline-flex; align-items: center; gap: 0.5em;
  font-size: 0.84rem; font-weight: 600; letter-spacing: -0.01em; color: var(--white);
  border: 1px solid var(--line-strong); border-radius: 99px;
  padding: 0 1.05em; min-height: 38px;
  transition: transform 0.5s var(--ease), border-color 0.4s var(--ease), background 0.4s var(--ease);
}
.ai-act:hover { border-color: var(--white); background: rgba(255, 255, 255, 0.03); transform: translateY(-1px); }
.ai-act--sm { min-height: 34px; font-size: 0.8rem; }

/* copy and feedback: quiet */
.ai-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 14px; margin-top: 12px; }
.ai-tool {
  font-size: 0.76rem; font-weight: 500; color: var(--faint);
  min-height: 28px; padding: 0;
  transition: color 0.3s var(--ease);
}
button.ai-tool:hover { color: var(--soft); }
.ai-tool--done { display: inline-flex; align-items: center; }
.ai-feedback { margin-top: 12px; display: grid; gap: 8px; }
.ai-noteinput {
  width: 100%; resize: vertical; min-height: 64px;
  background: var(--charcoal); border: 1px solid var(--line-strong); border-radius: var(--r-sm);
  padding: 0.6rem 0.8rem; font-size: 0.9rem; font-weight: 500; line-height: 1.45; color: var(--white);
  outline: none; transition: border-color 0.3s var(--ease);
}
.ai-noteinput:focus { border-color: var(--white); }
.ai-noteinput::placeholder { color: var(--faint); }
.ai-feedback__row { display: flex; align-items: center; gap: 14px; }

/* the periodic feedback card (AI.md 13.4): inline, quiet, after an answer */
.ai-ask {
  margin-top: 16px; padding: 14px 16px 12px;
  background: var(--charcoal); border: 1px solid var(--line); border-radius: var(--r-md);
}
.ai-ask__title { font-size: 0.9rem; font-weight: 600; letter-spacing: -0.01em; color: var(--white); line-height: 1.4; }
.ai-ask__row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 10px; }
.ai-ask__later { margin-left: 6px; }
.ai-ask__form { display: grid; gap: 8px; margin-top: 10px; }
.ai-ask__label { font-size: 0.84rem; font-weight: 500; color: var(--soft); line-height: 1.4; }
.ai-ask__form .ai-ask__row { margin-top: 0; }
.ai-ask--done { padding: 0; background: none; border: 0; border-radius: 0; margin-top: 10px; }
.ai-ask__done { font-size: 0.8rem; font-weight: 500; color: var(--muted); line-height: 1.45; }

/* ---------- the question field ---------- */
.ai-form { flex: none; padding: 12px 16px 12px; border-top: 1px solid var(--line); background: var(--near-black); }
.ai-field {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--charcoal); border: 1px solid var(--line-strong); border-radius: var(--r-lg);
  padding: 5px 5px 5px 16px;
  transition: border-color 0.3s var(--ease);
}
.ai-field:focus-within { border-color: var(--white); }
.ai-input {
  flex: 1; min-width: 0; resize: none; overflow-y: hidden;
  background: none; border: 0; outline: none;
  font-size: 1rem; /* 16px: no zoom on iOS focus */
  font-weight: 500; line-height: 1.45; color: var(--white);
  padding: 8px 0; max-height: 132px;
}
.ai-input:focus-visible { outline: none; }
.ai-input::placeholder { color: var(--faint); }
.ai-send {
  flex: none; width: 40px; height: 40px; border-radius: 99px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--lime); color: #07090a; font-size: 1.05rem; font-weight: 600;
  transition: transform 0.4s var(--ease), box-shadow 0.4s var(--ease);
}
.ai-send:hover { transform: translateY(-1px); box-shadow: 0 10px 28px -12px var(--lime-glow); }
.ai-foot { font-size: 0.72rem; font-weight: 500; color: var(--faint); text-align: center; margin-top: 8px; line-height: 1.4; }

/* ---------- phones: a full-height sheet above the keyboard ---------- */
@media (max-width: 639.98px) {
  .ai-launch {
    right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom));
    min-height: 44px;
  }
  .ai-panel {
    left: 0; right: 0; bottom: auto;
    top: var(--ai-vvt, 0px);
    width: 100%;
    height: 100vh; height: var(--ai-vvh, 100dvh);
    border: 0; border-radius: 0; box-shadow: none;
    transform: translateY(16px);
  }
  .ai-panel.is-in { transform: none; }
  /* full screen carried over from a wider window still follows the keyboard */
  .ai-panel.is-expanded { top: var(--ai-vvt, 0px); bottom: auto; height: 100vh; height: var(--ai-vvh, 100dvh); }
  .ai-panel__inner {
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right);
  }
  .ai-head { padding: 10px 8px 10px 16px; align-items: center; }
  .ai-sub { font-size: 0.78rem; }
  .ai-ctl--expand { display: none; }
  .ai-ctl--icon { width: 44px; height: 44px; }
  .ai-ctl--text { min-height: 44px; padding: 0 1em; }
  .ai-log { padding: 16px 16px 20px; }
  .ai-suggest__btn { min-height: 44px; font-size: 0.9rem; }
  /* every control on the sheet is a 44px touch target */
  .ai-act, .ai-source, .ai-tool { min-height: 44px; }
  .ai-tool--done { min-height: 0; }
  /* Stop stays small to the eye, with a 44px hit area */
  .ai-think__stop::after { content: ""; position: absolute; left: -6px; right: -6px; top: 50%; height: 44px; transform: translateY(-50%); }
  .ai-ask { padding: 14px 14px 10px; }
  /* the race summary's text link keeps its underline tight and gets a taller hit area */
  .ai-link { position: relative; }
  .ai-link::after { content: ""; position: absolute; left: -4px; right: -4px; top: 50%; height: 44px; transform: translateY(-50%); }
  .ai-result { padding-block: 13px; }
  .ai-form { padding: 10px 12px max(10px, env(safe-area-inset-bottom)); }
  .ai-send { width: 44px; height: 44px; }
  .ai-foot { font-size: 0.7rem; }
  html.ai-open, html.ai-open body { overflow: hidden; }
}

/* ---------- reduced motion: no glide, no pulse ---------- */
@media (prefers-reduced-motion: reduce) {
  .ai-panel, .ai-panel.is-in { transition: none; transform: none; }
  .ai-launch, .ai-act, .ai-send, .ai-result__arw { transition: none; }
  .ai-launch:hover, .ai-act:hover, .ai-send:hover, .ai-result:hover .ai-result__arw { transform: none; }
  .ai-dots i { animation: none; opacity: 0.55; }
  .ai-think__stop { transition: none; }
}

@media print { .ai-root { display: none !important; } }
