/* ---------- tokens ---------- */
:root {
  --bg: #faf8f4;
  --panel: #ffffff;
  --ink: #1d1c1a;
  --ink-soft: #3b3935;
  --muted: #6d695f;
  --line: #e7e2d8;
  --accent: #1f5c4a;
  --accent-soft: #e3efe9;
  --accent-ink: #ffffff;
  --user-bubble: #f1ece3;
  --hl: rgba(96, 150, 230, 0.26);
  --hl-edge: #6f9be0;
  --pers: rgba(255, 208, 70, 0.42);
  --pers-strong: rgba(255, 196, 40, 0.65);
  --flash: rgba(31, 92, 74, 0.22);
  --shadow: 0 8px 28px rgba(30, 25, 15, 0.14), 0 1px 3px rgba(30, 25, 15, 0.08);
  --serif: "Newsreader", "Iowan Old Style", Georgia, "Times New Roman", serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --chat-w: clamp(360px, 32vw, 460px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151513;
    --panel: #1c1b19;
    --ink: #ecebe7;
    --ink-soft: #d4d1ca;
    --muted: #a09c93;
    --line: #2f2d29;
    --accent: #72c6a8;
    --accent-soft: #1f302a;
    --accent-ink: #0c1d17;
    --user-bubble: #272521;
    --hl: rgba(110, 160, 240, 0.3);
    --hl-edge: #7fa7ea;
    --pers: rgba(255, 196, 60, 0.26);
    --pers-strong: rgba(255, 196, 60, 0.45);
    --flash: rgba(114, 198, 168, 0.28);
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.5), 0 1px 3px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
}
a { color: var(--accent); }
button { font: inherit; color: inherit; cursor: pointer; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--chat-w);
  min-height: 100vh;
}

/* ---------- reader ---------- */
.reader { padding-inline: 24px; padding-block: 0 96px; }
.site-bar {
  max-width: 44rem;
  margin: 0 auto;
  padding-block: 22px;
  display: flex; gap: 12px; align-items: baseline; justify-content: space-between;
  font-size: 13px; color: var(--muted);
  border-bottom: 1px solid var(--line);
}
.site-bar a { color: var(--ink); text-decoration: none; font-weight: 600; letter-spacing: 0.01em; }
.site-bar .hint { display: flex; align-items: center; gap: 6px; }
.site-bar .hint kbd {
  font: inherit; font-size: 12px; padding: 1px 6px; border-radius: 4px;
  background: var(--hl); color: var(--ink);
}

.prose {
  max-width: 44rem;
  margin: 0 auto;
  font-family: var(--serif);
  font-size: 1.24rem;
  line-height: 1.62;
  color: var(--ink-soft);
}
.prose h1 {
  font-size: clamp(2rem, 4.2vw, 2.9rem);
  line-height: 1.12;
  letter-spacing: -0.015em;
  font-weight: 600;
  color: var(--ink);
  margin: 1.4em 0 0.35em;
  text-wrap: balance;
}
.prose .subtitle {
  font-size: 1.35rem; font-style: italic; color: var(--muted);
  margin: 0 0 2.2em;
}
.prose h2 {
  font-size: 1.6rem; line-height: 1.25; font-weight: 600; color: var(--ink);
  margin: 2.4em 0 0.7em; text-wrap: balance; scroll-margin-top: 24px;
}
.prose h3 { font-size: 1.25rem; color: var(--ink); margin: 2em 0 0.5em; }
.prose p { margin: 0 0 1.1em; }
.prose strong { color: var(--ink); font-weight: 600; }
.prose ul, .prose ol { padding-left: 1.3em; margin: 0 0 1.2em; }
.prose li { margin: 0.35em 0; }
.prose li::marker { color: var(--muted); }
.prose blockquote {
  margin: 1.6em 0; padding: 0.2em 0 0.2em 1.1em;
  border-left: 3px solid var(--accent); color: var(--muted); font-style: italic;
}
.prose a[href^="#ask"], .prose a[href="#chat"] {
  font-family: var(--sans); font-size: 0.82em; font-weight: 600;
  text-decoration: none; padding: 2px 9px; border-radius: 99px;
  background: var(--accent-soft); color: var(--accent); white-space: nowrap;
}
.prose a[href^="#ask"]::before, .prose a[href="#chat"]::before { content: "💬 "; }
.prose ::selection { background: var(--hl); }
.loading { color: var(--muted); font-family: var(--sans); font-size: 15px; padding-top: 3rem; }

::highlight(ask-quote) { background-color: var(--hl); }
::highlight(ask-flash) { background-color: var(--flash); }

/* ---------- selection popover ---------- */
.sel-pop {
  position: absolute; z-index: 40;
  display: flex; gap: 2px; padding: 4px;
  background: var(--ink); color: var(--bg);
  border-radius: 10px; box-shadow: var(--shadow);
  transform: translateX(-50%);
  font-size: 13px; font-weight: 500;
  animation: pop 0.12s ease-out;
}
.sel-pop button {
  background: none; border: 0; color: inherit;
  padding: 7px 11px; border-radius: 7px; white-space: nowrap;
}
.sel-pop button:hover, .sel-pop button:focus-visible { background: rgba(127, 127, 127, 0.28); outline: none; }
.sel-pop button.primary { color: #9fc2ff; font-weight: 600; }
@media (prefers-color-scheme: dark) { .sel-pop button.primary { color: #1f4f9a; } }
@keyframes pop { from { opacity: 0; transform: translate(-50%, 4px); } }

/* ---------- chat panel ---------- */
.chat {
  position: sticky; top: 0;
  height: 100vh; height: 100dvh;
  display: flex; flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--line);
}
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 18px 14px;
  border-bottom: 1px solid var(--line);
}
.chat-head .titles { flex: 1; min-width: 0; }
.chat-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.chat-head p { margin: 1px 0 0; font-size: 12.5px; color: var(--muted); }
.icon-btn {
  background: none; border: 1px solid var(--line); border-radius: 8px;
  padding: 5px 10px; font-size: 12.5px; color: var(--muted);
}
.icon-btn:hover { color: var(--ink); border-color: var(--muted); }
.chat-close { display: none; }

.chat-log {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  padding: 18px 18px 8px;
  display: flex; flex-direction: column; gap: 16px;
}
.intro { color: var(--ink-soft); font-size: 14.5px; }
.intro p { margin: 0 0 14px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line); background: var(--bg);
  border-radius: 99px; padding: 6px 12px; font-size: 13px; text-align: left;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.intro .tip { margin: 18px 0 0; font-size: 12.5px; color: var(--muted); }
.tip mark { background: var(--hl); color: inherit; padding: 0 3px; border-radius: 3px; }

.msg { max-width: 100%; }
.msg.user { align-self: flex-end; max-width: 88%; }
.msg.user .bubble {
  background: var(--user-bubble); border-radius: 14px 14px 4px 14px;
  padding: 9px 13px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg .quote {
  display: block; width: 100%; text-align: left;
  border: 0; border-left: 3px solid var(--hl-edge);
  background: var(--hl); color: var(--ink-soft);
  font-family: var(--serif); font-size: 14.5px; font-style: italic; line-height: 1.45;
  padding: 6px 10px; margin: 0 0 6px; border-radius: 0 8px 8px 0;
}
.msg .quote:hover { filter: brightness(0.97); }
.msg .quote .jump { display: block; font-family: var(--sans); font-style: normal; font-size: 11.5px; color: var(--muted); margin-top: 3px; }
.msg.assistant .md { font-size: 14.5px; line-height: 1.6; color: var(--ink); overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 0.75em; }
.md ul, .md ol { margin: 0 0 0.75em; padding-left: 1.25em; }
.md li { margin: 0.2em 0; }
.md blockquote { margin: 0 0 0.75em; padding-left: 0.8em; border-left: 2px solid var(--line); color: var(--muted); }
.md code { font-size: 0.9em; background: var(--user-bubble); padding: 1px 4px; border-radius: 4px; }
.md pre { overflow-x: auto; background: var(--user-bubble); padding: 10px; border-radius: 8px; }
.md h1, .md h2, .md h3, .md h4 { font-size: 1em; margin: 0.8em 0 0.4em; }
.msg.error .md { color: #b3261e; }
@media (prefers-color-scheme: dark) { .msg.error .md { color: #f2b8b5; } }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: 0.2s; }
.typing i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.composer { padding: 10px 14px 12px; border-top: 1px solid var(--line); }
.pending-quote {
  display: flex; gap: 8px; align-items: flex-start;
  background: var(--hl); border-left: 3px solid var(--hl-edge); border-radius: 0 8px 8px 0;
  padding: 6px 8px 6px 10px; margin-bottom: 8px;
  font-family: var(--serif); font-style: italic; font-size: 14px; line-height: 1.4; color: var(--ink-soft);
}
.pending-quote span { flex: 1; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.pending-quote button { background: none; border: 0; font-style: normal; color: var(--muted); padding: 0 4px; font-size: 16px; line-height: 1; }
.input-row {
  display: flex; align-items: flex-end; gap: 8px;
  border: 1px solid var(--line); border-radius: 14px; background: var(--bg);
  padding: 6px 6px 6px 12px;
}
.input-row:focus-within { border-color: var(--accent); }
.input-row textarea {
  flex: 1; border: 0; background: none; resize: none; outline: none;
  font: inherit; font-size: 15px; color: var(--ink); line-height: 1.45;
  max-height: 160px; padding: 5px 0;
}
.send {
  flex: none; width: 34px; height: 34px; border-radius: 10px; border: 0;
  background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center;
}
.send:disabled { opacity: 0.35; cursor: default; }
.send svg { width: 16px; height: 16px; }
.fineprint { margin: 8px 2px 0; font-size: 11.5px; color: var(--muted); display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.fineprint a { color: var(--muted); }
.counter.warn { color: #b3261e; }

/* ---------- floating button (narrow screens) ---------- */
.chat-fab {
  display: none;
  position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 30; border: 0; border-radius: 99px;
  background: var(--accent); color: var(--accent-ink);
  padding: 12px 18px; font-weight: 600; font-size: 14px; box-shadow: var(--shadow);
}

@media (max-width: 980px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .reader { padding-inline: 18px; }
  .prose { font-size: 1.15rem; }
  .site-bar .hint { display: none; }
  .chat-fab { display: block; }
  .chat {
    position: fixed; inset: 0; z-index: 50; height: 100dvh;
    border-left: 0;
    transform: translateY(100%); visibility: hidden;
    transition: transform 0.25s ease, visibility 0s linear 0.25s;
  }
  .chat.open { transform: none; visibility: visible; transition: transform 0.25s ease; }
  .chat-close { display: inline-block; }
  .composer { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  body.chat-open { overflow: hidden; }
}
@media (prefers-reduced-motion: reduce) {
  .chat, .chat.open { transition: none; }
  .sel-pop { animation: none; }
}


/* ---------- site bar extras ---------- */
.bar-right { display: flex; align-items: center; gap: 14px; }
.clean-toggle {
  background: none; border: 1px solid var(--line); border-radius: 99px;
  padding: 3px 10px; font-size: 12px; color: var(--muted);
}
.clean-toggle:hover { color: var(--ink); border-color: var(--muted); }

/* ---------- "Read this as you" card ---------- */
.me-card {
  font-family: var(--sans); font-size: 14.5px; line-height: 1.5; color: var(--ink);
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 18px 20px; margin: -0.8em 0 2.4em;
  box-shadow: 0 1px 2px rgba(30, 25, 15, 0.04);
}
.me-kicker { font-size: 11.5px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--accent); margin-bottom: 6px; }
.me-intro { margin: 0 0 12px; color: var(--ink-soft); }
.me-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.me-row label { flex: 1 1 200px; display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.me-card input, .me-card textarea {
  font: inherit; font-size: 15px; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; width: 100%;
}
.me-card textarea { resize: vertical; min-height: 44px; }
.me-card input:focus, .me-card textarea:focus { outline: none; border-color: var(--accent); }
.me-actions { display: flex; align-items: center; gap: 14px; margin-top: 12px; flex-wrap: wrap; }
.me-go {
  border: 0; border-radius: 10px; padding: 9px 16px; font-weight: 600;
  background: var(--accent); color: var(--accent-ink);
  display: inline-flex; align-items: center; gap: 8px;
}
.me-go:disabled { opacity: 0.75; cursor: progress; }
.me-fine { margin: 10px 0 0; font-size: 12px; color: var(--muted); }
.me-err { margin: 10px 0 0; font-size: 13px; color: #b3261e; }
.me-label { margin: 0 0 6px; font-size: 16px; }
.me-links { margin-left: 10px; font-size: 13px; color: var(--muted); }
.me-tip { margin: 0; color: var(--ink-soft); }
.linkish { background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; text-underline-offset: 2px; font-size: 13px; }
.spin { width: 14px; height: 14px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- personal highlights ---------- */
mark.pers {
  background: var(--pers); color: inherit; border-radius: 2px;
  padding: 1px 0; cursor: pointer; -webkit-box-decoration-break: clone; box-decoration-break: clone;
  transition: background 0.15s;
}
mark.pers:hover { background: var(--pers-strong); }
mark.pers.last::after {
  content: "💡"; font-size: 0.72em; margin-left: 3px; vertical-align: 0.25em;
  font-style: normal;
}
.why-pop {
  position: absolute; z-index: 45;
  background: var(--panel); color: var(--ink); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: var(--shadow); padding: 12px 14px 14px;
  font-family: var(--sans); font-size: 14px; line-height: 1.5;
  animation: rise 0.14s ease-out;
}
.why-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; font-size: 13px; }
.why-x { background: none; border: 0; font-size: 18px; line-height: 1; color: var(--muted); padding: 0 2px; }
.why-pop p { margin: 6px 0 10px; color: var(--ink-soft); }
.why-go { background: var(--accent-soft); color: var(--accent); border: 0; border-radius: 8px; padding: 7px 11px; font-weight: 600; font-size: 13px; }

/* ---------- markers ---------- */
.yt {
  display: inline-flex; align-items: baseline; gap: 6px; flex-wrap: wrap;
  font-family: var(--sans); font-size: 0.72em; font-weight: 500; line-height: 1.35;
  text-align: left; vertical-align: 0.1em;
  margin: 4px 0 2px; padding: 4px 11px 5px; border-radius: 99px;
  background: var(--accent-soft); color: var(--accent); border: 1px solid transparent;
}
.yt:hover { border-color: var(--accent); }
.yt-kicker { font-weight: 700; letter-spacing: 0.02em; }
.yt-kicker::before { content: "💬 "; }
li > .yt { display: inline-flex; }

.rh {
  display: inline-grid; place-items: center; vertical-align: 0.05em;
  width: 1.25em; height: 1.25em; margin-left: 2px; padding: 0;
  border: 0; border-radius: 50%; background: transparent;
  color: var(--accent); opacity: 0.75; position: relative;
}
.rh svg { width: 1em; height: 1em; }
.rh:hover, .rh:focus-visible { opacity: 1; color: var(--accent); background: var(--accent-soft); outline: none; }
.rh::after {
  content: attr(data-label); position: absolute; left: 50%; bottom: calc(100% + 6px); transform: translateX(-50%);
  white-space: nowrap; font-family: var(--sans); font-size: 12px; font-weight: 500; line-height: 1.2;
  background: var(--ink); color: var(--bg); padding: 5px 8px; border-radius: 6px;
  opacity: 0; pointer-events: none; transition: opacity 0.12s;
}
.rh:hover::after, .rh:focus-visible::after { opacity: 1; }
::highlight(rh-hover) { background-color: var(--accent-soft); color: var(--ink); }

.clean .yt, .clean .rh { display: none !important; }
.clean mark.pers { background: none; cursor: text; }
.clean mark.pers.last::after { content: none; }

/* ---------- chat additions ---------- */
.chip-me { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.tip-yt { font-weight: 600; color: var(--accent); }
.tip-rh svg, .rh-mini svg { width: 13px; height: 13px; vertical-align: -2px; }
.rh-mini { color: var(--accent); }
.asks-at {
  display: block; border: 0; background: none; padding: 0; margin: 0 0 4px;
  font-size: 11.5px; font-weight: 600; color: var(--accent); text-align: left;
}
.msg.asks .md { border-left: 3px solid var(--accent); padding-left: 10px; }
@media (max-width: 980px) {
  .me-card { padding: 16px; }
  .rh::after { display: none; }
}

@keyframes rise { from { opacity: 0; transform: translateY(4px); } }
@media (prefers-reduced-motion: reduce) { .why-pop { animation: none; } }

/* ---------- article drawings ---------- */
.prose p:has(> img:only-child) { margin: 1.8em 0; }
.prose img {
  display: block; width: 100%; height: auto; max-width: 100%;
  aspect-ratio: 4 / 3; object-fit: contain;
  border-radius: 12px; border: 1px solid var(--line); background: #fbfbfa;
}
@media (prefers-color-scheme: dark) {
  /* line drawings: flip white paper to dark, keep the ink colours recognisable */
  .prose img { filter: invert(0.9) hue-rotate(180deg) saturate(1.3); background: #fbfbfa; border-color: transparent; }
}
