/* Roles follow the dataviz reference palette; dark values are their own validated steps. */
:root {
  color-scheme: light;
  --page: #f4f4f1;
  --surface: #fcfcfb;
  --raised: #ffffff;
  --sunken: #f6f6f3;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --border: rgba(11, 11, 11, 0.09);
  --border-strong: rgba(11, 11, 11, 0.16);
  --series-1: #2a78d6;   /* candidate bars, focus, primary action */
  --series-2: #eb6834;   /* selection controls */
  --intervention: #8761ad;
  --focus-ring: rgba(42, 120, 214, 0.28);
  --chip: rgba(11, 11, 11, 0.055);
  --hover-wash: rgba(42, 120, 214, 0.09);
  --critical: #d03b3b;
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 18px rgba(0, 0, 0, 0.04);
  --shadow-float: 0 10px 34px rgba(0, 0, 0, 0.16), 0 2px 6px rgba(0, 0, 0, 0.06);
  --radius-card: 14px;
  --radius: 8px;
  --control-h: 34px;
  --mono: ui-monospace, "Cascadia Mono", "JetBrains Mono", Consolas, "Sarasa Mono SC", "Microsoft YaHei UI", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", "Microsoft YaHei UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --raised: #232322;
    --sunken: #151514;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --border: rgba(255, 255, 255, 0.09);
    --border-strong: rgba(255, 255, 255, 0.18);
    --series-1: #3987e5;
    --series-2: #d95926;
    --intervention: #c3a0e6;
    --focus-ring: rgba(57, 135, 229, 0.35);
    --chip: rgba(255, 255, 255, 0.08);
    --hover-wash: rgba(57, 135, 229, 0.15);
    --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-float: 0 10px 34px rgba(0, 0, 0, 0.6);
  }
}

/* ---------------------------------------------------------------- base */
* { box-sizing: border-box; }
/* Always reserve the scrollbar's space so the layout doesn't shift sideways when
   the page starts or stops overflowing (e.g. opening Sampling). */
html { scrollbar-gutter: stable; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button {
  font: inherit;
  font-size: 13.5px;
  font-weight: 500;
  height: var(--control-h);
  padding: 0 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--raised);
  color: var(--text-primary);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.12s, border-color 0.12s, box-shadow 0.12s, opacity 0.12s;
}
button:hover:not(:disabled) { background: var(--hover-wash); border-color: var(--series-1); }
button:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--focus-ring); }
button:disabled { opacity: 0.42; cursor: default; }
button.primary { background: var(--series-1); border-color: var(--series-1); color: #fff; }
button.primary:hover:not(:disabled) { background: var(--series-1); filter: brightness(1.08); }
button.danger:not(:disabled) { color: var(--critical); border-color: var(--critical); }
button.danger:hover:not(:disabled) { background: rgba(208, 59, 59, 0.08); border-color: var(--critical); }
button.ghost { border-color: transparent; background: transparent; }
input, textarea {
  font: inherit;
  color: var(--text-primary);
  background: var(--raised);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  transition: border-color 0.12s, box-shadow 0.12s;
}
input:focus, textarea:focus { outline: none; border-color: var(--series-1); box-shadow: 0 0 0 3px var(--focus-ring); }
input[type="number"] { width: 100%; height: var(--control-h); padding: 0 10px; font-variant-numeric: tabular-nums; }

.container { width: 100%; max-width: 1120px; margin: 0 auto; padding: 0 24px; }
@media (max-width: 700px) { .container { padding: 0 12px; } }

/* ---------------------------------------------------------------- top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner { display: flex; align-items: center; justify-content: space-between; gap: 16px; height: 56px; }
.brand { display: inline-flex; align-items: center; gap: 10px; font-size: 16px; font-weight: 650; letter-spacing: -0.01em; }
.logo { font-size: 20px; line-height: 1; }
.model-chip {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--chip);
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* ---------------------------------------------------------------- layout & cards */
.layout { display: flex; flex-direction: column; gap: 20px; padding-top: 24px; padding-bottom: 40px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  padding: 20px;
}
.card-head { display: flex; align-items: center; margin-bottom: 12px; }
.card-head h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

/* ---------------------------------------------------------------- prompt */
.prompt {
  display: block;
  width: 100%;
  min-height: 120px;
  resize: vertical;
  padding: 12px 14px;
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.65;
}
.prompt-actions { display: flex; justify-content: flex-end; align-items: center; margin-top: 12px; }
.prompt-actions .primary { min-width: 120px; }

/* ---------------------------------------------------------------- sampling */
.sampling-head { margin-bottom: 0; }
.sampling-head h2 { width: 100%; }
.sampling-toggle { display: flex; align-items: center; justify-content: space-between; width: 100%; height: auto; padding: 0; border: 0; background: transparent; color: inherit; font: inherit; letter-spacing: inherit; text-transform: inherit; }
.sampling-toggle:hover:not(:disabled) { background: transparent; color: var(--text-primary); }
.chevron {
  width: 7px;
  height: 7px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: translateY(-2px) rotate(45deg);
  transition: transform 0.15s;
}
.sampling-toggle[aria-expanded="true"] .chevron { transform: translateY(1px) rotate(-135deg); }
.param-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px 14px;
  margin-top: 10px;
  padding: 14px;
  background: var(--sunken);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.param-grid label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--text-secondary); font-weight: 500; }
.param-grid label.wide { grid-column: 1 / -1; }
.param-grid textarea { font-family: var(--mono); font-size: 12.5px; padding: 7px 10px; resize: vertical; }
.label-note { color: var(--text-muted); font-weight: 400; }

/* ---------------------------------------------------------------- output toolbar */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; margin-bottom: 16px; }
.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: var(--chip);
  border-radius: 10px;
}
.segmented button {
  height: 28px;
  padding: 0 12px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text-secondary);
}
.segmented button:hover:not(:disabled) { background: transparent; color: var(--text-primary); }
.segmented button[aria-checked="true"] {
  background: var(--raised);
  color: var(--text-primary);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1), 0 0 0 1px var(--border);
}
.legend { display: inline-flex; align-items: center; gap: 10px; flex: 1 1 auto; max-width: 320px; }
.legend.hidden { visibility: hidden; }
.legend-caption { font-size: 13px; color: var(--text-secondary); white-space: nowrap; }
.legend-scale { flex: 1 1 200px; min-width: 140px; max-width: 200px; padding-top: 13px; }
.colorbar { height: 8px; border-radius: 4px; box-shadow: inset 0 0 0 1px var(--border); }
.ticks { position: relative; height: 14px; margin-top: 3px; font-size: 10.5px; line-height: 14px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.ticks span { position: absolute; transform: translateX(-50%); white-space: nowrap; }
.ticks span:first-child { transform: none; }
.ticks span:last-child { transform: translateX(-100%); }
.origin-legend { display: inline-flex; gap: 14px; margin-right: 8px; font-size: 12px; color: var(--text-muted); }
@media (max-width: 760px) { .origin-legend { display: none; } }
.origin-legend span { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.mark-intervention { display: inline-block; color: var(--intervention); font: normal 18px/1 var(--sans); }
.mark-handover { display: inline-block; width: 3px; height: 14px; background: var(--text-secondary); border-radius: 2px; }

/* ---------------------------------------------------------------- stats */
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
@media (max-width: 760px) { .stats { grid-template-columns: minmax(0, 1fr); } }
.stat {
  background: var(--raised);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  min-width: 0;
}
.stat .label { font-size: 12px; font-weight: 500; color: var(--text-secondary); }
.stat .value { font-size: 20px; font-weight: 650; letter-spacing: -0.01em; margin-top: 2px; }
.stat .unit { font-size: 13px; font-weight: 500; color: var(--text-secondary); margin-left: 4px; letter-spacing: 0; }
.stat .sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------------------------------------------------------------- document */
.doc-frame {
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--raised);
  overflow: hidden;
}
.doc {
  font-family: var(--mono);
  font-size: 14.5px;
  line-height: 2;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  min-height: 180px;
  max-height: 68vh;
  overflow-y: auto;
  padding: 16px 18px;
  outline: none;
}
.doc:empty::before { content: "Type a prefix above and press Generate."; color: var(--text-muted); font-family: var(--sans); }
.doc-panes { display: grid; grid-template-columns: minmax(0, 1fr); }
.doc-panes.preview-open { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.doc-panes > .doc { min-width: 0; }
#btn-markdown[aria-pressed="true"] { background: var(--hover-wash); border-color: var(--series-1); color: var(--series-1); }
.markdown-preview {
  min-width: 0;
  min-height: 180px;
  max-height: 68vh;
  overflow: auto;
  padding: 16px 18px;
  border-left: 1px solid var(--border-strong);
  font: 14.5px/1.75 var(--sans);
  overflow-wrap: anywhere;
}
.markdown-preview:empty::before { content: "Live Markdown preview appears here."; color: var(--text-muted); }
.markdown-preview > :first-child { margin-top: 0; }
.markdown-preview > :last-child { margin-bottom: 0; }
.markdown-preview h1, .markdown-preview h2, .markdown-preview h3 { line-height: 1.35; margin: 1.2em 0 .5em; }
.markdown-preview h1 { font-size: 1.7em; }
.markdown-preview h2 { font-size: 1.4em; }
.markdown-preview h3 { font-size: 1.15em; }
.markdown-preview p, .markdown-preview ul, .markdown-preview ol { margin: .7em 0; }
.markdown-preview code { font-family: var(--mono); background: var(--sunken); border-radius: 4px; padding: 2px 4px; }
.markdown-preview pre { padding: 12px; overflow: auto; background: var(--sunken); border-radius: 8px; }
.markdown-preview pre code { padding: 0; }
.markdown-preview blockquote { margin: 1em 0; padding-left: 12px; border-left: 3px solid var(--border-strong); color: var(--text-secondary); }
.markdown-preview table { display: block; overflow: auto; border-collapse: collapse; }
.markdown-preview th, .markdown-preview td { border: 1px solid var(--border-strong); padding: 6px 10px; }
.markdown-preview a { color: var(--series-1); }
.markdown-preview img { max-width: 100%; }
.markdown-preview hr { border: 0; border-top: 1px solid var(--border-strong); }
.markdown-preview .katex-display { overflow-x: auto; overflow-y: hidden; padding: .2em 0; }
.doc-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 8px 10px 8px 18px;
  border-top: 1px solid var(--border);
  background: var(--sunken);
}
.doc-status {
  margin-right: auto;
  font-size: 12.5px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.doc-status.live { color: var(--series-1); }
.doc-bar button { height: 30px; min-width: 88px; }

.pt { border-radius: 3px; padding: 2px 0; cursor: pointer; position: relative; }
/* How the per-token value (--heat, set by colorize) is drawn. The underline sits at the
   bottom edge, so the intervention marker goes on top of the token instead. */
/* A light fill keeps the shared red/green palette readable in both themes. */
.heat-soft .pt { background-color: color-mix(in srgb, var(--heat, transparent) 35%, transparent); }
.heat-underline .pt { border-bottom: 3px solid var(--heat, transparent); border-radius: 0; }
.pt:hover, .pt.active { outline: 2px solid var(--series-1); outline-offset: 0; }
.pt.twin { outline: 1px dashed var(--series-1); outline-offset: 0; }
.pt.focused { outline: 2px solid var(--text-primary); }
.pt.intervention::after {
  content: '◆';
  position: absolute;
  top: -0.55em;
  left: 50%;
  transform: translateX(-50%);
  color: var(--intervention);
  font: normal 16px/1 var(--sans);
  pointer-events: none;
}
.pt.handover::before {
  content: "";
  display: inline-block;
  width: 3px;
  height: 1.05em;
  margin: 0 3px -0.15em 1px;
  background: var(--text-secondary);
  border-radius: 2px;
}
.pt.pending { color: var(--text-muted); }
.pt.selected-range { outline: 2px solid var(--series-2); }
.glyph { color: var(--text-muted); font-size: 0.85em; }
.eot, .hex {
  font-size: 0.78em;
  padding: 0 4px;
  border-radius: 4px;
  background: var(--chip);
  color: var(--text-secondary);
}
.doc .cursor { display: inline-block; width: 7px; height: 1.05em; margin-left: 1px; vertical-align: text-bottom; background: var(--series-1); border-radius: 1px; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* ---------------------------------------------------------------- hover panel */
.panel {
  position: fixed;
  z-index: 30;
  max-width: min(92vw, 900px);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--raised);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-float);
  padding: 10px;
  font-size: 12.5px;
}
/* Only the body scrolls; the footer sits outside it and never moves. */
.panel-body { flex: 0 1 auto; min-height: 0; overflow: auto; overscroll-behavior: contain; }
.panel-cols { display: flex; gap: 12px; align-items: stretch; }
.tok-col { position: relative; min-width: 260px; }
.tok-col + .tok-col::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: -6px;
  width: 1px;
  background: var(--border-strong);
  pointer-events: none;
}
.tok-stats {
  display: grid;
  grid-template-columns: repeat(4, max-content);
  gap: 10px 24px;
  padding: 10px 12px;
  margin-bottom: 8px;
  background: var(--sunken);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.ts { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.ts-label { font-size: 11px; color: var(--text-muted); white-space: nowrap; }
.ts-value { font-size: 13px; font-weight: 600; color: var(--text-primary); font-variant-numeric: tabular-nums; white-space: nowrap; }
.ts-value .piece { font-size: 13.5px; font-weight: 500; }
.ts.scoring .ts-value { color: var(--text-muted); font-weight: 400; }
.cand-hint { width: 0; min-width: 100%; margin-bottom: 6px; padding: 0 4px; color: var(--text-secondary); font-size: 12px; line-height: 1.5; }
.cand-table {
  display: grid;
  grid-template-columns: minmax(22px, max-content) minmax(70px, auto) minmax(60px, 1fr) 54px 48px;
  column-gap: 8px;
  padding: 0 4px;
}
.cand-head, .cand {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: subgrid;
  align-items: center;
  gap: 8px;
  padding: 2px 0;
}
.cand-head { color: var(--text-muted); font-size: 11px; padding-bottom: 4px; }
.cand-head span:first-child, .cand-head span:nth-child(n+4) { text-align: right; }
.cand { border-radius: 6px; cursor: pointer; min-height: 25px; }
.cand:hover { background: var(--hover-wash); }
.cand.chosen { font-weight: 650; }
.cand .rank { color: var(--text-muted); font-size: 11px; text-align: right; font-variant-numeric: tabular-nums; }
.cand.chosen .rank { color: var(--text-primary); }
.cand .bar { height: 6px; background: var(--grid); border-radius: 3px; overflow: hidden; }
.cand .bar i { display: block; height: 100%; background: var(--series-1); border-radius: 0 3px 3px 0; }
.cand .p, .cand .sp { text-align: right; font-variant-numeric: tabular-nums; }
.cand .sp { color: var(--text-muted); }
.cand.miss { cursor: default; font-style: italic; }
.piece {
  font-family: var(--mono);
  white-space: pre;
  background: var(--chip);
  border-radius: 4px;
  padding: 0 5px;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  justify-self: start;
}
.piece .glyph { font-size: 0.9em; }
/* width: 0 + min-width: 100% — the footer fills the panel but doesn't widen it,
   so the panel is exactly as wide as its candidate columns. */
.panel-foot {
  flex: none;
  width: 0;
  min-width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-top: 1px solid var(--grid);
  margin-top: 8px;
  padding-top: 10px;
}
.foot-row { display: flex; gap: 8px; align-items: center; }
.panel-foot button { height: 30px; padding: 0 12px; font-size: 12.5px; }
.panel-input {
  flex: 1 1 auto;
  min-width: 0;
  height: 30px;
  padding: 0 10px;
  font-family: var(--mono);
  font-size: 13px;
}
.panel-foot .pos { color: var(--text-muted); font-size: 11px; margin-left: auto; font-family: var(--mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------------------------------------------------------------- editor & floating */
.editor {
  position: fixed;
  z-index: 40;
  width: min(560px, 92vw);
  background: var(--raised);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-float);
  padding: 10px;
}
.editor textarea { display: block; width: 100%; min-height: 76px; font-family: var(--mono); font-size: 13.5px; padding: 8px 10px; resize: vertical; }
.editor-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
.editor-actions button { height: 30px; font-size: 12.5px; }
.selection-button { position: fixed; z-index: 35; height: 30px; box-shadow: var(--shadow-float); border-color: var(--series-2); }
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  z-index: 50;
  max-width: 80vw;
  background: var(--text-primary);
  color: var(--surface);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  box-shadow: var(--shadow-float);
}
.toast.error { background: var(--critical); color: #fff; }
