:root {
  --bg: #faf9f5;
  --surface: #ffffff;
  --border: rgba(0, 0, 0, 0.10);
  --text: #1a1a18;
  --muted: #6b6a64;
  --faint: #97968d;
  --accent: #2a2a6a;
  --danger: #a32d2d;
  --ok: #2f7d57;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html { scrollbar-gutter: stable; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
.wrap { max-width: 1100px; margin: 0 auto; padding: 32px 22px 72px; }

/* ---- header ---- */
header { margin-bottom: 22px; }
h1 { font-size: 23px; font-weight: 600; letter-spacing: -0.01em; margin: 0 0 6px; }
.sub { color: var(--muted); font-size: 14.5px; max-width: 64ch; margin: 0 0 16px; }
.sub code { font-family: var(--mono); font-size: 12.5px; background: #efeee8; padding: 1px 5px; border-radius: 5px; }

.bar { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.bar .sep { width: 1px; align-self: stretch; background: var(--border); margin: 2px 3px; }
.range { color: var(--faint); font-size: 12px; font-family: var(--mono); }

button {
  font: inherit; font-size: 13px; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 9px; padding: 7px 13px; color: var(--text);
  transition: background .12s, border-color .12s, transform .04s;
}
button:hover { background: #f1f0ea; }
button:active { transform: scale(0.98); }
button:disabled { opacity: .5; cursor: default; }
button.primary {
  background: var(--text); color: var(--bg); border-color: var(--text); font-weight: 500;
}
button.primary:hover { background: #000; }

.field { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.field input[type="number"] { width: 84px; }
input[type="number"] {
  font: inherit; font-size: 13px; width: 100px; padding: 7px 10px;
  border: 1px solid var(--border); border-radius: 9px;
  background: var(--surface); color: var(--text);
}
input[type="number"]:focus { outline: none; border-color: var(--accent); }

.error {
  background: #fbecec; border: 1px solid #e7c4c4; color: var(--danger);
  border-radius: 10px; padding: 12px 14px; font-size: 13.5px; margin-bottom: 16px;
}
.loading { color: var(--faint); padding: 40px 0; font-size: 14px; }

/* ---- layout ---- */
.layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 248px;
  gap: 20px; align-items: start;
  transition: opacity .12s;
}
.layout.busy { opacity: .45; pointer-events: none; }
@media (max-width: 800px) { .layout { grid-template-columns: 1fr; } aside { order: -1; } }

/* ---- stream ---- */
.stream-wrap {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; overflow: hidden;
}
.stream {
  font-family: var(--mono); font-size: 13px; line-height: 2.1;
  white-space: pre-wrap; word-break: break-word;
  padding: 18px 20px;
}
.doc { border-radius: 4px; scroll-margin: 80px; }
.tok { border-radius: 2px; padding: 1px 0; }
.tok:nth-child(even) { box-shadow: inset 0 0 0 99px rgba(0, 0, 0, 0.035); }
.tok:hover {
  box-shadow: inset 0 0 0 99px rgba(42, 42, 106, 0.16);
  outline: 1px solid rgba(42, 42, 106, 0.35);
}
.tok-tip {
  position: fixed; display: none; z-index: 30; pointer-events: none;
  background: #1f1f1d; color: #f3f2ec;
  font-family: var(--mono); font-size: 11.5px; white-space: pre;
  padding: 4px 8px; border-radius: 7px;
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.22);
}
.eos {
  font-family: var(--sans); font-size: 10.5px; font-weight: 600; letter-spacing: .02em;
  background: var(--danger); color: #fceaea; border-radius: 4px;
  padding: 1px 6px; margin: 0 4px; white-space: nowrap; vertical-align: 1px;
}
.edge {
  display: flex; align-items: center; gap: 7px;
  font-size: 11.5px; color: var(--faint); padding: 9px 20px;
  background: #fbfbf9;
}
.edge.top { border-bottom: 1px solid var(--border); }
.edge.bot { border-top: 1px solid var(--border); }
.edge.cut { color: var(--danger); background: #fdf4f4; }
.edge.clean { color: var(--ok); background: #f3faf6; }

/* (i) explainer */
.info {
  position: relative; flex: none; cursor: help;
  width: 14px; height: 14px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-family: var(--sans); font-size: 9.5px; font-weight: 700; font-style: italic;
  color: var(--muted); background: rgba(0, 0, 0, 0.06); border: 1px solid var(--border);
}
.info:hover, .info:focus { outline: none; color: var(--text); background: rgba(0, 0, 0, 0.1); }
.info .tip {
  position: absolute; z-index: 10; left: 0; top: calc(100% + 7px);
  width: 280px; max-width: 76vw; padding: 9px 11px;
  background: #1f1f1d; color: #f3f2ec; border-radius: 9px;
  font-size: 11.5px; font-weight: 400; font-style: normal; line-height: 1.5;
  text-align: left; letter-spacing: 0;
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.22);
  opacity: 0; visibility: hidden; transform: translateY(-3px);
  transition: opacity .12s, transform .12s, visibility .12s;
}
.info:hover .tip, .info:focus .tip { opacity: 1; visibility: visible; transform: translateY(0); }
.edge.bot .info .tip { top: auto; bottom: calc(100% + 7px); }

/* ---- sidebar ---- */
aside { position: sticky; top: 18px; font-size: 12.5px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px 14px; margin-bottom: 14px;
}
.row { display: flex; justify-content: space-between; gap: 12px; padding: 3.5px 0; }
.row .k { color: var(--muted); }
.row .v { font-family: var(--mono); text-align: right; }

.legend { display: flex; flex-direction: column; gap: 2px; max-height: 60vh; overflow: auto; padding-right: 2px; }
.doc-item {
  display: flex; gap: 8px; align-items: flex-start; text-align: left;
  font: inherit; background: none; border: none; border-radius: 7px;
  padding: 5px 6px; cursor: pointer; width: 100%;
}
.doc-item:hover { background: #f1f0ea; }
.doc-item:active { transform: none; }
.swatch { flex: none; width: 11px; height: 11px; border-radius: 3px; margin-top: 3px; border: 1px solid var(--border); }
.dl { display: flex; flex-direction: column; min-width: 0; }
.dn { font-weight: 500; }
.dp { color: var(--faint); font-family: var(--mono); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.st { font-weight: 600; }
.st.complete { color: var(--ok); }
.st.head-cut, .st.tail-cut, .st.fragment { color: var(--danger); }

/* ---- footer ---- */
footer {
  margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: baseline; gap: 16px;
  color: var(--faint); font-size: 12px; flex-wrap: wrap;
}
kbd {
  font-family: var(--mono); font-size: 11px; background: #efeee8;
  border: 1px solid var(--border); border-radius: 4px; padding: 0 5px;
}
.about { max-width: 60ch; }
.link { background: none; border: none; padding: 0; color: var(--accent); text-decoration: underline; font-size: 12px; }
.link:hover { background: none; }
.about p { margin: 8px 0 0; color: var(--muted); font-size: 12px; line-height: 1.55; }

/* ---- narrow / mobile overrides ----
   Must come AFTER the base `aside`/`.legend` rules above: media queries add no
   specificity, so an earlier same-selector rule would lose to the later base one.
   Stacked single column -> the sidebar can't stay sticky (it would float down
   over the stream), and the doc legend should flow instead of nesting a 60vh
   scroll region. */
@media (max-width: 800px) {
  aside { position: static; }
  .legend { max-height: none; overflow: visible; }
}
