:root {
  --bg: #000; --ink: #f4f1ea; --mute: #a7a39b; --dim: #8a867e; --line: #1f1f1f; --line2: #353535;
  --accent: #7c84ff;
  --paper: #f4f1ea; --paper-ink: #141414; --paper-mute: #5d5a54; --paper-line: #d9d3c6; --paper-accent: #4148c9;
  --serif: 'Playfair Display', Georgia, 'Times New Roman', serif;
  --text: 'Source Serif 4', Georgia, serif;
  --sans: Inter, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font-family: var(--sans); -webkit-font-smoothing: antialiased; overscroll-behavior: none; }
body { overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

#app { height: 100dvh; display: grid; grid-template-columns: 1fr; }
.stage { position: relative; min-width: 0; height: 100dvh; overflow: hidden; }
.side { display: none; }
.screen { position: absolute; inset: 0; }

/* ---------- sign in ---------- */
#login { display: flex; align-items: center; justify-content: center; padding: calc(24px + var(--safe-t)) 24px calc(24px + var(--safe-b)); overflow-y: auto; }
.login-in { width: 100%; max-width: 380px; display: flex; flex-direction: column; align-items: center; text-align: center; gap: 6px; }
.login-mark svg { width: 84px; height: 84px; stroke: none; margin-bottom: 10px; }
.login-h { font-family: var(--serif); font-weight: 700; font-size: 34px; margin: 18px 0 4px; }
.login-p { margin: 0 0 18px; font-size: 13px; line-height: 1.6; color: var(--mute); }
.login-form { width: 100%; display: flex; flex-direction: column; gap: 14px; text-align: left; }
.login-form label { display: flex; flex-direction: column; gap: 6px; font-size: 11px; letter-spacing: .18em; text-transform: uppercase; color: var(--mute); }
.login-form input { font: 16px var(--sans); color: var(--ink); background: #0d0d0d; border: 1px solid var(--line2); border-radius: 12px; height: 50px; padding: 0 16px; letter-spacing: normal; text-transform: none; }
.login-form input:focus { outline: none; border-color: var(--accent); }
.login-err { min-height: 18px; font-size: 13px; color: #ff8a7a; }
.login-form .pill { margin-top: 2px; }
#app.bare .side { display: none; }
.acct { margin-top: 8px; font-size: 12px; color: var(--dim); display: flex; flex-direction: column; align-items: center; gap: 2px; text-align: center; }
.acct b { color: var(--mute); font-weight: 500; }
.textlink.small { min-height: 32px; font-size: 12px; }
.invite { display: flex; align-items: center; gap: 8px; }
.invite .code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 14px; letter-spacing: .12em; color: var(--ink); border: 1px dashed var(--line2); border-radius: 8px; padding: 4px 10px; min-height: 32px; }

/* ---------- opening screen ---------- */
#intro { display: flex; flex-direction: column; align-items: center; padding: calc(28px + var(--safe-t)) 24px calc(32px + var(--safe-b)); text-align: center; }
.wordmark { font-family: var(--serif); font-weight: 600; font-size: 20px; letter-spacing: .32em; padding-left: .32em; }
.intro-mid { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 0; }
.big { font-family: var(--serif); font-weight: 700; font-size: clamp(40px, 12vw, 76px); line-height: 1.02; text-transform: uppercase; margin: 0; }
.big .hl { color: var(--accent); display: block; }
.intro-sub { margin-top: 28px; font-size: 13px; line-height: 1.7; color: var(--mute); max-width: 340px; }
.intro-actions { width: 100%; max-width: 380px; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.pill { display: flex; align-items: center; justify-content: center; width: 100%; min-height: 52px; border-radius: 26px; border: 1px solid var(--line2); font-size: 14px; letter-spacing: .14em; text-transform: uppercase; color: var(--ink); text-decoration: none; }
.pill.solid { background: var(--ink); color: #000; border-color: var(--ink); font-weight: 600; }
.pill:disabled { opacity: .4; cursor: default; }
.textlink { min-height: 44px; display: inline-flex; align-items: center; font-size: 13px; color: var(--mute); }
.textlink:hover { color: var(--ink); }
.loading { font-size: 11px; letter-spacing: .3em; color: var(--dim); margin-top: 24px; }
.loading::after { content: ''; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }

/* ---------- topics ---------- */
#topics { display: flex; flex-direction: column; background: var(--bg); }
.topics-head { padding: calc(28px + var(--safe-t)) 24px 10px; display: flex; flex-direction: column; gap: 8px; max-width: 680px; width: 100%; margin: 0 auto; }
.topics-head .wordmark { font-size: 14px; color: var(--mute); }
.topics-head h1 { margin: 0; font-family: var(--serif); font-weight: 700; font-size: 34px; line-height: 1.08; }
.topics-head p { margin: 0; font-size: 13px; color: var(--mute); }
.topic-list { flex: 1; overflow-y: auto; padding: 6px 16px 16px; max-width: 680px; width: 100%; margin: 0 auto; }
.topic { display: flex; align-items: center; gap: 8px; border-bottom: 1px solid var(--line); padding: 4px 8px; }
.topic-tog { flex: 1; display: flex; align-items: center; gap: 14px; min-height: 52px; text-align: left; color: var(--dim); }
.topic-tog .dot { width: 18px; height: 18px; border-radius: 50%; border: 1.5px solid #4a4a4a; flex: none; }
.topic-tog .nm { font-size: 15px; font-weight: 500; display: block; }
.topic-tog .bl { font-size: 12px; color: var(--dim); display: none; margin-top: 2px; }
.topic.on .topic-tog { color: var(--ink); }
.topic.on .dot { border: 0; background: var(--accent); }
.step { display: flex; align-items: center; gap: 2px; }
.step button { width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--line2); font-size: 18px; line-height: 1; }
.step button:disabled { opacity: .3; cursor: default; }
.step .n { width: 28px; text-align: center; font-family: var(--serif); font-weight: 700; font-size: 18px; color: var(--accent); }
.topics-foot { border-top: 1px solid var(--line); padding: 14px 24px calc(18px + var(--safe-b)); display: flex; flex-direction: column; align-items: center; gap: 4px; }
.topics-foot .pill { max-width: 420px; }
.topics-foot .row { display: flex; gap: 18px; }

/* ---------- swipe deck ---------- */
#deck { background: #000; touch-action: none; user-select: none; -webkit-user-select: none; }
.cards { position: absolute; inset: 0; overflow: hidden; }
.card { position: absolute; inset: 0; overflow: hidden; background: #15161a; will-change: transform; }
.card.anim { transition: transform .32s cubic-bezier(.2, .8, .2, 1), opacity .32s; }
.card-img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .4s; pointer-events: none; -webkit-user-drag: none; }
.card-img.ok { opacity: 1; }
.card-ghost { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-family: var(--serif); font-weight: 700; font-size: 26vmin; color: #1f2026; letter-spacing: -.02em; text-transform: uppercase; overflow: hidden; white-space: nowrap; }
.card-scrim { position: absolute; left: 0; right: 0; bottom: 0; height: 72%; background: linear-gradient(to bottom, rgba(0,0,0,0) 0%, rgba(0,0,0,.72) 42%, #000 100%); pointer-events: none; }
.card-body { position: absolute; left: 0; right: 0; bottom: 0; padding: 0 24px calc(28px + var(--safe-b)); display: flex; flex-direction: column; gap: 14px; max-width: 900px; }
.kicker { font-size: 11px; font-weight: 600; letter-spacing: .2em; text-transform: uppercase; color: var(--accent); }
.card-title { margin: 0; font-family: var(--serif); font-weight: 700; font-size: clamp(30px, 8.6vw, 56px); line-height: 1.08; text-wrap: balance; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 7; -webkit-box-orient: vertical; overflow: hidden; }
.meta { font-size: 12px; letter-spacing: .12em; color: var(--mute); text-transform: uppercase; }
.up-hint { align-self: center; margin-top: 8px; display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--mute); font-size: 11px; letter-spacing: .24em; min-height: 44px; padding: 0 16px; }
.up-hint svg { animation: nudge 2.2s ease-in-out infinite; }
@keyframes nudge { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-5px); } }
.card.end { background: #000; display: flex; align-items: center; justify-content: center; text-align: center; padding: 24px; }
.card.end .end-in { display: flex; flex-direction: column; align-items: center; gap: 10px; max-width: 380px; width: 100%; }
.card.end .pill { margin-top: 18px; }

.deck-head { position: absolute; left: 0; right: 0; top: 0; z-index: 5; padding: calc(12px + var(--safe-t)) 16px 0; background: linear-gradient(to bottom, rgba(0,0,0,.55), rgba(0,0,0,0)); pointer-events: none; }
.deck-head > * { pointer-events: auto; }
.bars { display: flex; gap: 4px; height: 3px; }
.bars i { flex: 1; border-radius: 2px; background: rgba(244,241,234,.28); }
.bars i.done { background: var(--ink); }
.bars i.cur { background: var(--accent); }
.deck-bar { display: flex; align-items: center; justify-content: space-between; margin-top: 4px; }
.brand-btn { font-family: var(--serif); font-weight: 600; font-size: 14px; letter-spacing: .32em; min-height: 44px; padding-right: 8px; }
.pos { font-size: 11px; letter-spacing: .18em; color: var(--mute); }
.icon-btn { width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; border-radius: 50%; }
.nav { display: none; }

/* ---------- reader ---------- */
.reader { position: fixed; inset: 0; z-index: 20; background: var(--paper); color: var(--paper-ink); transform: translateY(100%); transition: transform .34s cubic-bezier(.2, .8, .2, 1); display: flex; flex-direction: column; }
.reader.open { transform: translateY(0); }
.reader.drag { transition: none; }
.r-scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.r-hero { position: relative; height: 40vh; min-height: 220px; background: #2b2d33; }
.r-hero img { width: 100%; height: 100%; object-fit: cover; display: block; }
.r-hero.none { height: calc(72px + var(--safe-t)); background: var(--paper); }
.r-close { position: absolute; top: calc(14px + var(--safe-t)); left: 12px; width: 44px; height: 44px; border-radius: 50%; background: rgba(0,0,0,.55); color: var(--ink); display: flex; align-items: center; justify-content: center; }
.r-hero.none .r-close { background: rgba(0,0,0,.08); color: var(--paper-ink); }
.r-body { max-width: 680px; margin: 0 auto; padding: 24px 26px 130px; }
.r-kicker { font-size: 11px; font-weight: 600; letter-spacing: .2em; text-transform: uppercase; color: var(--paper-accent); }
.r-title { margin: 12px 0; font-family: var(--serif); font-weight: 700; font-size: clamp(28px, 7vw, 40px); line-height: 1.12; text-wrap: balance; }
.r-meta { font-size: 12px; letter-spacing: .12em; text-transform: uppercase; color: var(--paper-mute); }
.r-text { margin-top: 22px; font-family: var(--text); font-size: var(--rsize, 19px); line-height: 1.65; color: #1f1f1f; overflow-wrap: break-word; }
.r-text p { margin: 0 0 1.05em; }
.r-text h2, .r-text h3, .r-text h4 { font-family: var(--serif); line-height: 1.2; margin: 1.6em 0 .5em; }
.r-text img { max-width: 100%; height: auto; display: block; margin: 1.2em auto; border-radius: 4px; }
.r-text figure { margin: 1.4em 0; }
.r-text figcaption { font-family: var(--sans); font-size: 13px; color: var(--paper-mute); line-height: 1.45; }
.r-text blockquote { margin: 1.2em 0; padding-left: 18px; border-left: 3px solid var(--paper-line); color: #3a3a3a; font-style: italic; }
.r-text pre { white-space: pre-wrap; background: #e9e4d8; padding: 12px; border-radius: 6px; font-size: 14px; overflow-x: auto; }
.r-text code { font-size: .88em; }
.r-text a { color: var(--paper-accent); }
.r-text table { display: block; overflow-x: auto; border-collapse: collapse; font-family: var(--sans); font-size: 14px; }
.r-text td, .r-text th { border: 1px solid var(--paper-line); padding: 6px 8px; }
.r-text .media-link { font-family: var(--sans); font-weight: 600; }
.r-note { margin-top: 22px; padding: 16px; border: 1px solid var(--paper-line); border-radius: 10px; font-size: 14px; line-height: 1.5; color: var(--paper-mute); }
.r-note a { color: var(--paper-accent); font-weight: 600; }
.skel { height: 14px; border-radius: 7px; background: #e5dfd2; margin: 12px 0; animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .5; } }
.r-bar { position: absolute; left: 16px; right: 16px; bottom: calc(18px + var(--safe-b)); max-width: 460px; margin: 0 auto; height: 60px; border-radius: 30px; background: #000; color: var(--ink); display: flex; align-items: center; justify-content: space-around; padding: 0 8px; box-shadow: 0 8px 30px rgba(0,0,0,.25); }
.r-bar a, .r-bar button { min-width: 48px; height: 48px; display: flex; align-items: center; justify-content: center; color: var(--ink); text-decoration: none; border-radius: 24px; }
.r-bar .aa { font-family: var(--serif); font-size: 20px; }
.r-bar .share { background: var(--accent); color: #000; font-weight: 600; font-size: 14px; padding: 0 18px; gap: 8px; height: 44px; }
.r-bar .share svg { width: 18px; height: 18px; stroke-width: 2; }

.toast { position: fixed; left: 50%; bottom: calc(96px + var(--safe-b)); transform: translate(-50%, 20px); background: var(--ink); color: #000; padding: 10px 16px; border-radius: 20px; font-size: 13px; font-weight: 500; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 50; max-width: 90vw; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- desktop ---------- */
@media (min-width: 1024px) {
  #app { grid-template-columns: 240px minmax(0, 1fr) 0; transition: grid-template-columns .3s; }
  #app.reading { grid-template-columns: 240px minmax(0, 1fr) minmax(440px, 36vw); }
  #app.bare { grid-template-columns: minmax(0, 1fr); }
  .side { display: flex; flex-direction: column; gap: 4px; padding: 32px 22px 24px; border-right: 1px solid var(--line); height: 100dvh; overflow-y: auto; }
  .side .wordmark { font-size: 18px; text-align: left; padding: 0; margin-bottom: 26px; display: flex; align-items: center; gap: 12px; }
  .side .wordmark svg { width: 28px; height: 28px; stroke: none; }
  .side-label { font-size: 11px; letter-spacing: .2em; color: var(--mute); margin-bottom: 6px; text-transform: uppercase; }
  .side-topic { display: flex; justify-content: space-between; align-items: center; min-height: 40px; font-size: 14px; color: var(--mute); text-align: left; gap: 8px; }
  .side-topic:hover { color: var(--ink); }
  .side-topic b { font-family: var(--serif); font-weight: 700; color: var(--dim); }
  .side-topic.cur { color: var(--ink); }
  .side-topic.cur b { color: var(--accent); }
  .side .grow { flex: 1; }
  .side .pill { min-height: 44px; font-size: 12px; }
  .side .keys { margin-top: 14px; font-size: 12px; line-height: 1.8; color: var(--dim); }
  .side .keys kbd { font-family: var(--sans); border: 1px solid var(--line2); border-radius: 4px; padding: 0 5px; font-size: 11px; color: var(--mute); }

  .reader { position: relative; inset: auto; z-index: 1; transform: none; transition: none; height: 100dvh; min-width: 0; overflow: hidden; }
  .reader:not(.open) { visibility: hidden; }
  .r-hero { height: 300px; }
  .r-hero.none { height: 64px; }
  .r-body { padding: 28px 40px 130px; }
  .r-close svg { transform: rotate(-90deg); }

  .card-body { padding: 0 96px 56px; }
  .card-title { font-size: clamp(36px, 3.6vw, 58px); max-width: 780px; }
  .up-hint { align-self: flex-start; padding: 0; }
  .up-hint .lbl-touch { display: none; }
  .nav { display: flex; position: absolute; top: 50%; z-index: 4; width: 52px; height: 52px; margin-top: -26px; border-radius: 50%; border: 1px solid rgba(244,241,234,.35); background: rgba(0,0,0,.4); align-items: center; justify-content: center; }
  .nav:hover { background: rgba(0,0,0,.7); }
  .nav:disabled { opacity: .25; cursor: default; }
  .nav.prev { left: 20px; } .nav.next { right: 20px; }
  .deck-head { padding: 20px 32px 0; }
  .deck-head .brand-btn { visibility: hidden; }
  .topic-tog .bl { display: block; }
  #deck { touch-action: pan-y; }
}
@media (max-width: 1023px) { .up-hint .lbl-key { display: none; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition-duration: .01ms !important; } }
