:root {
  --bg: #ffffff;
  --bg-2: #f6f6f7;
  --text: #1d1d1f;
  --text-2: #5f5f66;
  --text-3: #8e8e95;
  --line: #e6e6e9;
  --accent: #0a66d6;
  --accent-bg: rgba(10, 102, 214, 0.08);
  --danger: #c4321b;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.08);
  --r-sm: 8px;
  --r-lg: 14px;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161618;
    --bg-2: #202023;
    --text: #f2f2f4;
    --text-2: #a6a6ad;
    --text-3: #75757c;
    --line: #2c2c30;
    --accent: #5ea2ff;
    --accent-bg: rgba(94, 162, 255, 0.12);
    --danger: #ff7a66;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5), 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
::selection { background: var(--accent-bg); }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* Top bar */
.bar {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid transparent;
  transition: border-color 200ms var(--ease);
}
.bar.scrolled { border-bottom-color: var(--line); }
.bar-inner {
  max-width: 720px; margin: 0 auto; height: 52px;
  padding: 0 max(16px, env(safe-area-inset-left));
  display: flex; align-items: center; justify-content: space-between;
}
.wordmark { font-size: 15px; font-weight: 600; letter-spacing: -0.01em; }
.bar-actions { display: flex; gap: 4px; }
.icon-btn {
  width: 36px; height: 36px; border: 0; border-radius: var(--r-sm);
  background: transparent; color: var(--text-2);
  display: inline-grid; place-items: center; cursor: pointer;
  transition: background 150ms var(--ease), color 150ms var(--ease);
}
.icon-btn:hover { background: var(--bg-2); color: var(--text); }
.icon-btn:disabled { opacity: 0.35; cursor: default; background: transparent; }
@media (pointer: coarse) { .icon-btn { width: 44px; height: 44px; } }

/* Page */
.page { max-width: 720px; margin: 0 auto; padding: 32px 16px 120px; }
@media (min-width: 640px) { .page { padding: 48px 24px 160px; } .bar-inner { padding: 0 24px; } }

.masthead { margin-bottom: 32px; }
.masthead h1 { font-size: 34px; line-height: 1.15; font-weight: 700; letter-spacing: -0.02em; margin: 0; }
.meta { color: var(--text-3); font-size: 15px; margin-top: 6px; font-variant-numeric: tabular-nums; }

.section-label { font-size: 15px; font-weight: 600; color: var(--text-2); margin: 0 0 12px; }

.tldr { list-style: none; margin: 0 0 48px; padding: 0; }
.tldr li { border-top: 1px solid var(--line); }
.tldr a {
  display: flex; gap: 12px; align-items: baseline;
  padding: 12px 4px; color: var(--text); border-radius: 6px;
  transition: background 150ms var(--ease);
}
.tldr a:hover { text-decoration: none; background: var(--bg-2); }
.tldr .topic { width: 72px; flex: none; }

.topic { color: var(--text-3); font-size: 13px; font-weight: 500; }

.story { padding: 32px 0; border-top: 1px solid var(--line); scroll-margin-top: 68px; }
.story h2 { font-size: 24px; line-height: 1.25; font-weight: 650; letter-spacing: -0.015em; margin: 6px 0 12px; }
.story p { margin: 0 0 12px; }
.why { color: var(--text-2); }
.why strong { color: var(--text); font-weight: 600; }

.cite {
  font-size: 12px; font-weight: 600; vertical-align: super; line-height: 0;
  padding: 0 1px; font-variant-numeric: tabular-nums;
}

.story-actions { display: flex; gap: 20px; margin-top: 16px; }
.text-btn {
  border: 0; background: none; padding: 6px 0; cursor: pointer;
  color: var(--accent); font-size: 15px; font-weight: 500;
  display: inline-flex; align-items: center; gap: 4px;
}
.text-btn svg { width: 16px; height: 16px; transition: transform 200ms var(--ease); }
.text-btn[aria-expanded="true"] svg { transform: rotate(180deg); }
.text-btn:hover { text-decoration: underline; }

.reveal { display: grid; grid-template-rows: 0fr; transition: grid-template-rows 220ms var(--ease); }
.reveal > div { overflow: hidden; }
.reveal.open { grid-template-rows: 1fr; }
.reveal-inner { padding-top: 16px; }

.deep p { color: var(--text); }

.sources { list-style: none; margin: 0; padding: 0; }
.sources li { display: flex; gap: 12px; padding: 10px 0; }
.sources li + li { border-top: 1px solid var(--line); }
.src-num { color: var(--text-3); font-size: 13px; width: 16px; text-align: right; padding-top: 2px; font-variant-numeric: tabular-nums; }
.src-icon { width: 20px; height: 20px; border-radius: 5px; background: var(--bg-2); flex: none; margin-top: 2px; }
.src-body { min-width: 0; }
.src-title { display: block; color: var(--text); font-size: 15px; line-height: 1.4; }
.src-meta { color: var(--text-3); font-size: 13px; }
.sources li.flash { background: var(--accent-bg); border-radius: 6px; transition: background 600ms; }

/* States */
.state { padding: 96px 0; text-align: center; color: var(--text-2); }
.state p { margin: 0 0 16px; }
.skeleton { display: grid; gap: 12px; }
.skeleton i { display: block; height: 14px; border-radius: 4px; background: var(--bg-2); animation: pulse 1.4s ease-in-out infinite; }
.skeleton i.h { height: 34px; width: 60%; margin-bottom: 20px; }
@keyframes pulse { 50% { opacity: 0.5; } }

.btn {
  height: 36px; padding: 0 16px; border-radius: var(--r-sm); cursor: pointer;
  border: 1px solid var(--line); background: var(--bg); color: var(--text); font-size: 15px; font-weight: 500;
  transition: background 150ms var(--ease);
}
.btn:hover { background: var(--bg-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
@media (pointer: coarse) { .btn { height: 44px; } }

/* Context menu */
.menu {
  position: fixed; z-index: 30; min-width: 180px; padding: 4px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
  animation: pop 150ms var(--ease);
}
.menu button {
  width: 100%; display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border: 0; border-radius: 6px; background: none; cursor: pointer; font-size: 14px; text-align: left;
}
.menu button svg { width: 16px; height: 16px; color: var(--text-2); }
.menu button:hover, .menu button:focus-visible { background: var(--accent); color: #fff; outline: none; }
.menu button:hover svg, .menu button:focus-visible svg { color: #fff; }
@keyframes pop { from { opacity: 0; transform: scale(0.97); } }

.explain-chip {
  position: fixed; z-index: 25; height: 36px; padding: 0 14px;
  border: 0; border-radius: 18px; background: var(--text); color: var(--bg);
  font-size: 14px; font-weight: 600; box-shadow: var(--shadow); cursor: pointer;
  animation: pop 150ms var(--ease);
}

/* Explain panel: floating card on desktop, bottom sheet on phones */
.explain {
  position: fixed; z-index: 20; right: 24px; bottom: 24px; width: 380px; max-height: min(70vh, 560px);
  display: flex; flex-direction: column;
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--shadow);
  animation: rise 220ms var(--ease);
}
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }
@media (max-width: 640px) {
  .explain { left: 0; right: 0; bottom: 0; width: auto; border-radius: var(--r-lg) var(--r-lg) 0 0; padding-bottom: env(safe-area-inset-bottom); }
}
.explain-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; padding: 16px 12px 4px 20px; }
.explain-term { font-size: 17px; font-weight: 650; margin: 6px 0 0; line-height: 1.3; }
.explain-body { overflow-y: auto; padding: 4px 20px 20px; font-size: 15px; line-height: 1.5; }
.explain-body h3 { font-size: 13px; font-weight: 600; color: var(--text-3); margin: 14px 0 4px; }
.explain-body p { margin: 0; }
.explain-body .error { color: var(--danger); }
.explain-body .actions { margin-top: 12px; }

/* Settings sheet */
.sheet {
  width: min(440px, calc(100vw - 32px)); padding: 24px; border: 0; border-radius: var(--r-lg);
  background: var(--bg); color: var(--text); box-shadow: var(--shadow);
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.3); }
.sheet[open] { animation: pop 200ms var(--ease); }
.sheet h2 { font-size: 20px; margin: 0 0 20px; }
.field { display: grid; gap: 6px; margin-bottom: 12px; }
.field span { font-size: 14px; font-weight: 500; color: var(--text-2); }
.field input {
  height: 40px; padding: 0 12px; border-radius: var(--r-sm); border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font: inherit; font-size: 15px;
}
.field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }
.field-note { margin: -4px 0 16px; font-size: 13px; color: var(--text-3); }
.sheet-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 24px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Ideas */
.ideas { margin-top: 16px; padding-top: 40px; border-top: 1px solid var(--line); }
.idea { padding: 20px 0; }
.idea + .idea { border-top: 1px solid var(--line); }
.idea h3 { font-size: 19px; font-weight: 650; margin: 4px 0 8px; letter-spacing: -0.01em; }
.idea p { margin: 0 0 8px; }
