:root {
  --plane: #E6EAE7;
  --grid: #C9D1CC;
  --ghost: #B4BFB8;
  --ink: #1C2930;
  --ink2: #4E5D62;
  --ink3: #7F8D88;
  --card: #FCFDFB;
  --line: #BFCAC4;
  --read: #D5EBDC;
  --mute: #D9E0DC;
  --read-ink: #7E8C87;
  --op: #EDC33F;
  --op-soft: #F6E7AE;
  --focus: #2F55D4;
  --lift: 0 1px 0 rgba(28, 41, 48, .05), 0 14px 34px -14px rgba(28, 41, 48, .34);
  --sans: "Golos Text", system-ui, -apple-system, "Segoe UI", sans-serif;
  --read-serif: "Literata", Georgia, "Times New Roman", serif;
  --bar-h: 60px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  background: var(--plane);
  color: var(--ink);
  font: 400 15px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
kbd {
  font: 500 11px/1 var(--sans);
  padding: 3px 5px 2px;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink2);
  font-variant-numeric: tabular-nums;
}

/* the plane */
#plane {
  position: fixed; inset: 0; width: 100%; height: 100%;
  display: block; touch-action: none; cursor: grab; outline: none;
}
#plane.dragging { cursor: grabbing; }
#plane.pointing { cursor: pointer; }

/* top bar */
.bar {
  position: fixed; top: 0; left: 0; right: 0; height: var(--bar-h);
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
  z-index: 20; pointer-events: none;
}
.bar > * { pointer-events: auto; }
.bar::before { /* the plane fades out under the header, so cards never collide with its controls */
  content: ""; position: absolute; inset: 0 0 -18px; z-index: -1; pointer-events: none;
  background: linear-gradient(var(--plane) 0 58%, color-mix(in srgb, var(--plane) 0%, transparent));
}
body.is-landing .bar::before { display: none; }
.mark {
  display: inline-flex; align-items: center; gap: 8px; height: 36px;
  text-decoration: none; color: var(--ink); font-weight: 600; font-size: 15px; letter-spacing: -.01em;
  padding-right: 6px;
}
.barform { flex: 0 1 440px; min-width: 120px; }
#barInput {
  width: 100%; height: 36px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--card);
  font-size: 13.5px; color: var(--ink2); text-overflow: ellipsis;
}
#barInput:focus { outline: none; border-color: var(--ink2); color: var(--ink); box-shadow: 0 0 0 3px rgba(47, 85, 212, .16); }
.tools { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.pill, .icon {
  height: 36px; border: 1px solid var(--line); background: var(--card); border-radius: 999px;
  display: inline-flex; align-items: center; justify-content: center; gap: 7px; color: var(--ink);
}
.pill { padding: 0 13px 0 11px; font-size: 13px; font-weight: 500; font-variant-numeric: tabular-nums; }
.icon { width: 36px; border-radius: 10px; color: var(--ink2); }
.pill:hover, .icon:hover { border-color: var(--ink3); color: var(--ink); }
#ring { transition: stroke-dasharray .35s ease; }
body.is-landing .barform, body.is-landing .tools { display: none; }
body.is-stage .tools { display: none; }

/* landing */
#landing {
  position: fixed; inset: 0; z-index: 10;
  display: flex; flex-direction: column; align-items: center;
  padding: 0 16px; padding-top: calc(30vh - 60px);
  transition: opacity .22s ease, transform .22s ease;
}
body:not(.is-landing) #landing { opacity: 0; pointer-events: none; transform: translateY(-8px); }
.land {
  position: relative; width: min(600px, 100%);
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 1px 0 rgba(28, 41, 48, .04), 0 18px 40px -26px rgba(28, 41, 48, .5);
  transition: border-color .15s, box-shadow .15s;
}
.land:focus-within { border-color: var(--ink2); box-shadow: 0 0 0 4px rgba(47, 85, 212, .14), 0 18px 40px -26px rgba(28, 41, 48, .5); }
#landInput {
  width: 100%; height: 58px; border: 0; background: transparent; outline: none;
  padding: 0 60px 0 20px; font-size: 17px; color: var(--ink);
}
#landInput::placeholder, #barInput::placeholder { color: var(--ink3); }
.go {
  position: absolute; right: 9px; top: 9px; width: 40px; height: 40px;
  border: 0; border-radius: 10px; background: var(--ink); color: var(--card);
  display: grid; place-items: center; opacity: .18; transition: opacity .15s;
}
.land.has-text .go { opacity: 1; }
.land.shake { animation: shake .32s; }
@keyframes shake { 25% { transform: translateX(-6px); } 50% { transform: translateX(5px); } 75% { transform: translateX(-3px); } }
.land-title { margin: 0 0 18px; font: 500 22px/1.25 var(--sans); letter-spacing: -.01em; text-align: center; color: var(--ink); max-width: 22em; text-wrap: balance; }
.land-how { margin: 26px 0 0; max-width: 30em; text-align: center; color: var(--ink3); font-size: 13.5px; line-height: 1.5; text-wrap: pretty; }
.land-how a { color: var(--ink2); text-underline-offset: 3px; text-decoration-color: var(--line); }
.land-err { min-height: 0; margin: 0; color: #9B3B2E; font-size: 13.5px; text-align: center; }
.land-err:not(:empty) { margin-top: 10px; }
.land-tree { width: min(360px, 82vw); height: auto; color: var(--ghost); display: block; margin-top: -1px; }
.land-tree .land-tree-op { fill: var(--op); opacity: .55; }
.tries { margin-top: 22px; display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center; color: var(--ink3); font-size: 13.5px; }
.tries a {
  text-decoration: none; color: var(--ink2); padding: 6px 12px; border-radius: 999px;
  border: 1px solid var(--line); background: rgba(252, 253, 251, .6);
}
.tries a:hover { color: var(--ink); border-color: var(--ink3); background: var(--card); }

.foot {
  position: fixed; left: 16px; right: 16px; bottom: max(14px, env(safe-area-inset-bottom));
  display: flex; justify-content: space-between; align-items: center; z-index: 12;
  font-size: 13px; color: var(--ink3);
}
body:not(.is-landing) .foot { display: none; }
.link { background: none; border: 0; padding: 4px 0; color: var(--ink2); text-decoration: underline; text-decoration-color: var(--line); text-underline-offset: 3px; }
.link:hover { color: var(--ink); text-decoration-color: var(--ink3); }
.link.danger { color: #8E3A2D; }
.foot a.link { font-size: 13px; }
@media (hover: none), (max-width: 640px) { .desk-only { display: none; } }
.sources { display: inline-flex; align-items: center; gap: 7px; cursor: default; }
.sources::before { content: ""; width: 7px; height: 7px; border-radius: 2px; background: var(--ink3); }
.sources.live::before { background: #3E8A5B; }

/* stage: preview + "needs a key" / loading / errors (one card, no tree yet) */
.stage {
  position: fixed; inset: 0; z-index: 8; display: flex; flex-direction: column; align-items: center;
  padding: calc(var(--bar-h) + 8vh) 16px 32px; pointer-events: none; overflow-y: auto;
}
.stage > * { pointer-events: auto; }
.pv {
  width: min(440px, 100%); background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 14px 16px 12px 18px; position: relative; overflow: hidden;
}
.pv::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 5px; background: var(--op); }
.pv-head { display: flex; gap: 10px; align-items: center; }
.pv-head img { width: 32px; height: 32px; border-radius: 50%; background: var(--mute); }
.pv-name { font-weight: 600; font-size: 14px; line-height: 1.2; }
.pv-sub { color: var(--ink3); font-size: 12.5px; display: flex; gap: 8px; }
.pv-text { margin: 10px 0 8px; font: 400 15.5px/1.5 var(--read-serif); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 9.2em; overflow: hidden; }
.pv-meta { color: var(--ink3); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.pv.skeleton .pv-text { color: transparent; background: linear-gradient(var(--mute) 0 0) 0 .35em / 92% .7em no-repeat, linear-gradient(var(--mute) 0 0) 0 1.85em / 64% .7em no-repeat; height: 3em; }
.stem { width: 0; height: 34px; border-left: 1.5px dashed var(--ghost); }
.slot {
  width: min(440px, 100%); border: 1.5px dashed var(--ghost); border-radius: 12px;
  padding: 14px 16px; display: flex; flex-direction: column; gap: 12px; background: rgba(252, 253, 251, .35);
}
.slot-line { display: flex; align-items: center; gap: 8px; color: var(--ink2); font-size: 14.5px; }
.slot-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.slot-line b { color: var(--ink); font-weight: 600; }
.slot-why { margin: -4px 0 0; color: var(--ink2); font-size: 14px; line-height: 1.5; text-wrap: pretty; }
.slot-steps { margin: 0; padding: 0; list-style: none; counter-reset: s; display: grid; gap: 8px; font-size: 14px; color: var(--ink); }
.slot-steps li { counter-increment: s; display: grid; grid-template-columns: 24px 1fr; gap: 8px; align-items: baseline; }
.slot-steps li::before { content: counter(s); width: 22px; height: 22px; border-radius: 6px; border: 1px solid var(--line); background: var(--card); display: grid; place-items: center; font-size: 12px; font-weight: 600; color: var(--ink2); }
.slot-wait { display: flex; align-items: center; gap: 10px; color: var(--ink3); font-size: 12.5px; border-top: 1px solid var(--mute); padding-top: 10px; }
.slot-wait .dots i { width: 5px; height: 5px; }
.info {
  width: 20px; height: 20px; border-radius: 50%; border: 1px solid var(--line); color: var(--ink3);
  display: inline-grid; place-items: center; font-size: 12px; font-weight: 600; cursor: help; flex: none; background: var(--card);
}
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 7px; height: 7px; border-radius: 2px; background: var(--ink3); animation: blink 1.1s infinite; }
.dots i:nth-child(2) { animation-delay: .15s; } .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 100% { opacity: .2; } 40% { opacity: .9; } }

/* buttons */
.btn {
  height: 36px; padding: 0 13px; border-radius: 10px; border: 1px solid var(--line); background: var(--card);
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  font-size: 13.5px; font-weight: 500; color: var(--ink); text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--ink3); }
.btn.ghost { background: transparent; }
.btn.primary { background: var(--ink); border-color: var(--ink); color: var(--card); }
.btn.primary:hover { background: #2A3A42; }
.btn.primary kbd { background: rgba(255, 255, 255, .12); border-color: rgba(255, 255, 255, .2); color: #DDE3E0; }
.btn.file { position: relative; overflow: hidden; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* the reading card: side panel on desktop, bottom sheet on phones */
.card {
  position: fixed; z-index: 30; top: calc(var(--bar-h) + 4px); right: 16px; width: 404px;
  max-height: calc(100% - var(--bar-h) - 20px);
  display: flex; flex-direction: column;
  background: var(--card); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--lift);
  transform: translateX(calc(100% + 28px)); visibility: hidden;
  transition: transform .26s cubic-bezier(.2, .8, .2, 1), visibility 0s linear .26s;
}
.card.open { transform: none; visibility: visible; transition: transform .26s cubic-bezier(.2, .8, .2, 1); }
.card.is-op { box-shadow: inset 5px 0 0 var(--op), var(--lift); }
.grab { display: none; }
.card-scroll { overflow-y: auto; padding: 16px 18px 6px 20px; overscroll-behavior: contain; }
.card-head { display: grid; grid-template-columns: 40px 1fr auto auto; gap: 10px; align-items: center; }
.card .av { width: 40px; height: 40px; border-radius: 50%; background: var(--mute); object-fit: cover; }
.who { min-width: 0; }
.who-line { display: flex; align-items: center; gap: 7px; min-width: 0; }
.name { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.op-chip { flex: none; font-size: 11.5px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--op-soft); color: #5C4A0C; }
.card:not(.is-op) .op-chip { display: none; }
.handle { display: block; font-size: 13px; color: var(--ink3); text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.handle:hover { color: var(--ink2); text-decoration: underline; }
.when { font-size: 12.5px; color: var(--ink3); white-space: nowrap; font-variant-numeric: tabular-nums; }
.close { width: 32px; height: 32px; border-radius: 9px; border: 0; background: transparent; color: var(--ink3); display: grid; place-items: center; margin-right: -6px; }
.close:hover { background: var(--mute); color: var(--ink); }
.ctx {
  display: inline-flex; gap: 5px; align-items: center; margin: 12px 0 0; padding: 0; border: 0; background: none;
  color: var(--ink3); font-size: 13px; text-align: left; max-width: 100%;
}
.ctx:hover { color: var(--ink); }
.ctx b { font-weight: 500; color: var(--ink2); }
.ctx:empty { display: none; }
.text { margin: 10px 0 0; font: 400 17px/1.58 var(--read-serif); white-space: pre-wrap; overflow-wrap: anywhere; color: var(--ink); }
.text a { color: var(--focus); text-decoration: none; }
.text a:hover { text-decoration: underline; }
.quote {
  display: block; margin-top: 12px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px;
  text-decoration: none; color: var(--ink2); font-size: 14px; line-height: 1.45; overflow-wrap: anywhere;
}
.quote:hover { border-color: var(--ink3); }
.quote b { color: var(--ink); font-weight: 600; }
.quote:empty { display: none; }
.media { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 12px; }
.media:empty { display: none; }
.media a:only-child { grid-column: 1 / -1; }
.media img { width: 100%; height: 100%; max-height: 260px; object-fit: cover; border-radius: 10px; display: block; background: var(--mute); }
.more {
  display: inline-flex; margin-top: 12px; padding: 5px 11px; border: 1.2px dashed var(--ink3); border-radius: 999px;
  font-size: 12.5px; color: var(--ink2); text-decoration: none;
}
.more:hover { color: var(--ink); border-style: solid; }
.more:empty { display: none; }
.metrics { display: flex; flex-wrap: wrap; gap: 16px; margin: 14px 0 10px; color: var(--ink3); font-size: 13px; font-variant-numeric: tabular-nums; }
.metrics span { display: inline-flex; gap: 5px; align-items: center; }
.card-foot { display: flex; gap: 8px; padding: 12px 16px 14px; border-top: 1px solid var(--mute); flex-wrap: wrap; }
.card-foot .next { margin-left: auto; }

.next-fab {
  position: fixed; z-index: 25; right: 20px; bottom: max(20px, env(safe-area-inset-bottom));
  height: 46px; padding: 0 14px 0 14px; border-radius: 999px; border: 0;
  background: var(--ink); color: var(--card); box-shadow: var(--lift);
  display: inline-flex; align-items: center; gap: 9px; font-size: 14px; font-weight: 500;
}
.next-fab kbd { background: rgba(255, 255, 255, .12); border-color: rgba(255, 255, 255, .2); color: #DDE3E0; }
.next-fab:hover { background: #2A3A42; }
.next-fab.done { background: var(--card); color: var(--ink2); border: 1px solid var(--line); box-shadow: none; }
.next-fab.done kbd { display: none; }
body:not(.is-tree) .next-fab, body.card-open .next-fab { display: none; }

.minimap {
  position: fixed; z-index: 15; left: 16px; bottom: 16px; width: 184px; height: 124px;
  border-radius: 12px; border: 1px solid var(--line); background: rgba(252, 253, 251, .9); cursor: crosshair;
}
body:not(.is-tree) .minimap, body.no-minimap .minimap { display: none; }

/* popovers, tooltips, toast, dialog */
.pop {
  position: fixed; z-index: 40; top: calc(var(--bar-h) + 2px); right: 16px; width: 280px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--lift);
  padding: 14px 16px; font-size: 13.5px;
}
.keys { display: grid; grid-template-columns: auto 1fr; gap: 9px 14px; margin: 0; align-items: center; }
.keys dt { display: flex; gap: 3px; }
.keys dd { margin: 0; color: var(--ink2); }
.pop-note { margin: 14px 0 6px; padding-top: 12px; border-top: 1px solid var(--mute); color: var(--ink3); font-size: 12.5px; }
.keys-pop { top: auto; right: auto; width: min(360px, calc(100vw - 32px)); }
.keys-pop h3 { margin: 0 0 10px; font-size: 14px; font-weight: 600; }
.keys-pop ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.keys-pop li { display: grid; gap: 2px; }
.keys-pop li b { font-weight: 600; }
.keys-pop code { font: 500 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--plane); padding: 1px 5px; border-radius: 5px; }
.keys-pop .cost { color: var(--ink3); font-size: 12.5px; }
.keys-pop a { color: var(--focus); font-size: 12.5px; }

.tip {
  position: fixed; z-index: 60; pointer-events: none; left: 0; top: 0;
  max-width: 290px; padding: 6px 9px; border-radius: 8px;
  background: var(--ink); color: #EEF2EF; font-size: 12.5px; line-height: 1.4;
  opacity: 0; transform: translateY(2px); transition: opacity .12s, transform .12s;
}
.tip.on { opacity: 1; transform: none; }
.tip b { font-weight: 600; color: #fff; }
.toast {
  position: fixed; z-index: 50; left: 50%; bottom: max(24px, env(safe-area-inset-bottom));
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  display: flex; align-items: center; gap: 12px;
  background: var(--ink); color: #EEF2EF; border-radius: 999px; padding: 9px 16px; font-size: 13.5px;
  transition: opacity .18s, transform .18s; box-shadow: var(--lift);
  width: max-content; max-width: calc(100vw - 32px); line-height: 1.35; text-wrap: balance;
}
.toast.on { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast button { background: none; border: 0; color: var(--op); font-weight: 600; padding: 0; }
body.is-tree:not(.card-open) .toast { bottom: max(80px, calc(env(safe-area-inset-bottom) + 60px)); }

.dlg {
  width: min(560px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 16px;
  background: var(--card); color: var(--ink); padding: 18px 18px 16px; box-shadow: var(--lift);
}
.dlg::backdrop { background: rgba(28, 41, 48, .22); }
.dlg h2 { margin: 0 0 12px; font-size: 16px; font-weight: 600; }
.dlg textarea {
  width: 100%; height: 200px; resize: vertical; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  font: 400 12.5px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; background: #F6F8F6; color: var(--ink);
}
.dlg textarea:focus { outline: none; border-color: var(--ink2); }
.dlg-row { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.dlg-row .grow { flex: 1; }
.dlg-err { margin: 8px 0 0; color: #9B3B2E; font-size: 13px; min-height: 0; }
.dlg-err:empty { display: none; }
.drop {
  position: fixed; inset: 12px; z-index: 70; border: 2px dashed var(--ink2); border-radius: 18px;
  background: rgba(230, 234, 231, .88); display: grid; place-items: center; font-size: 16px; font-weight: 500; color: var(--ink2);
}

/* phones */
@media (max-width: 640px) {
  :root { --bar-h: 56px; }
  .bar { gap: 8px; padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top)); }
  body:not(.is-landing) .mark span { display: none; }
  .barform { flex: 1 1 auto; }
  #barInput { font-size: 16px; height: 36px; }
  .pill { padding: 0 10px 0 9px; }
  #fitBtn { display: none; }
  #landing { padding-top: 16vh; }
  .land-title { font-size: 20px; }
  .stage { padding-top: calc(var(--bar-h) + 16px); }
  #landInput { font-size: 16px; }
  .card {
    top: auto; left: 0; right: 0; bottom: 0; width: auto; max-height: 66%;
    border-radius: 18px 18px 0 0; border-bottom: 0;
    transform: translateY(105%);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .card.is-op { box-shadow: inset 0 4px 0 var(--op), var(--lift); }
  .grab { display: block; width: 100%; height: 22px; flex: none; position: relative; touch-action: none; cursor: grab; }
  .grab::after { content: ""; position: absolute; left: 50%; top: 9px; width: 38px; height: 4px; margin-left: -19px; border-radius: 4px; background: var(--line); }
  .card-scroll { padding: 2px 16px 4px; }
  .text { font-size: 16.5px; }
  .card-foot { padding: 10px 12px 12px; gap: 6px; }
  .card-foot .open span { display: none; }
  .next-fab { height: 52px; padding: 0 22px 0 18px; border-radius: 999px; right: 16px; bottom: max(18px, calc(env(safe-area-inset-bottom) + 8px)); font-size: 16px; }
  .next-fab kbd { display: none; }
  .next-fab svg { width: 20px; height: 20px; }
  .toast, body.is-tree:not(.card-open) .toast { top: calc(var(--bar-h) + 8px); bottom: auto; transform: translate(-50%, -12px); }
  .toast.on, body.is-tree:not(.card-open) .toast.on { transform: translate(-50%, 0); }
  .minimap { display: none; }
  .pop { right: 12px; left: 12px; width: auto; }
  .tries { font-size: 13px; }
  .toast { border-radius: 14px; }
  body.card-open .toast { top: calc(var(--bar-h) + 8px); bottom: auto; }
}
@media (max-width: 380px) {
  .card-foot .keep { padding: 0 10px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 1ms !important; animation: none !important; }
}
