:root {
  --bg: #090b12;
  --bg-2: #0e1119;
  --line: #1c2130;
  --line-2: #283046;
  --ink: #e9ecf4;
  --ink-2: #9aa3b8;
  --ink-3: #636c82;
  --you: #c7cbe0;          /* text you wrote */
  --calm: #dfe5f5;         /* a token the model expected */
  --hot: #ffcf6e;          /* a token that surprised it */
  --violet: #b9a2ff;       /* a token you chose */
  --accent: #ffd27a;
  --danger: #ff8a8a;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  --pad: clamp(16px, 4vw, 40px);
  --bar-h: 64px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(1px 1px at 12% 18%, #ffffff22 50%, transparent 51%),
    radial-gradient(1px 1px at 72% 9%, #ffffff1c 50%, transparent 51%),
    radial-gradient(1.4px 1.4px at 88% 64%, #ffd27a22 50%, transparent 51%),
    radial-gradient(1px 1px at 33% 82%, #ffffff18 50%, transparent 51%),
    radial-gradient(1200px 700px at 80% -10%, #1a1f3a55, transparent 60%),
    radial-gradient(900px 600px at -10% 110%, #2a1d3a44, transparent 60%),
    var(--bg);
  background-attachment: fixed;
  color: var(--ink);
  font: 14px/1.4 var(--sans);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ---------------------------------------------------------------- header */
.top {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) var(--pad) 10px;
  border-bottom: 1px solid var(--line);
  background: #090b12cc;
  backdrop-filter: blur(8px);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--accent); font-weight: 600; letter-spacing: .02em; }
.brand span { color: var(--ink); }
.model { color: var(--ink-3); font-size: 12px; font-family: var(--mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.spacer { flex: 1; }
.chip {
  border: 1px solid var(--line-2); background: transparent; color: var(--ink-2);
  border-radius: 999px; padding: 5px 11px; font-size: 12px; letter-spacing: .02em;
}
.chip:hover { color: var(--ink); border-color: var(--ink-3); }
.chip[aria-pressed="true"], .chip[aria-expanded="true"] { color: var(--bg); background: var(--accent); border-color: var(--accent); }

/* ---------------------------------------------------------------- document */
.stage { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.doc {
  min-height: calc(100% - 52px);
  padding: 28px var(--pad) 40vh;
  max-width: 980px; margin: 0 auto;
  outline: none;
  white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere;
  font-family: var(--mono); font-size: 15px; line-height: 1.6;
  color: var(--you);
  caret-color: var(--accent);
  tab-size: 4;
}
.doc.serif { font-family: var(--serif); font-size: 18px; line-height: 1.65; }
.doc.sans { font-family: var(--sans); font-size: 16px; line-height: 1.65; }
.doc:empty::before, .doc.blank::before { content: attr(data-placeholder); color: var(--ink-3); pointer-events: none; }
.doc[contenteditable="false"] { caret-color: transparent; }

.colophon {
  max-width: 980px; margin: 0 auto; padding: 14px var(--pad) 22px;
  display: flex; align-items: center; gap: 8px;
  color: var(--ink-3); font: 11px/1.4 var(--mono); letter-spacing: .03em;
}
.colophon svg { color: var(--accent); opacity: .8; flex: none; }

/* A token: --t is its surprisal mapped to 0..1 (0 = the model expected it). */
.tk {
  --t: 0;
  color: color-mix(in oklab, var(--calm), var(--hot) calc(var(--t) * 100%));
  border-radius: 3px;
  transition: background-color .15s;
}
.glow .tk {
  text-shadow: 0 0 calc(2px + var(--t) * 12px) color-mix(in oklab, var(--hot) calc(var(--t) * 75%), transparent);
}
.tk.forced { color: var(--violet); text-decoration: underline dotted color-mix(in oklab, var(--violet), transparent 30%); text-underline-offset: 4px; text-shadow: none; }
.tk.fresh { animation: arrive .5s ease-out; }
.tk.healed { text-decoration: underline dotted color-mix(in oklab, var(--accent), transparent 45%); text-underline-offset: 4px; }
.tk:hover, .tk.focus { background: #ffffff12; }
@keyframes arrive { from { background: #ffd27a30; } to { background: transparent; } }
.doc.inspecting .tk { cursor: pointer; }

/* ---------------------------------------------------------------- bottom bar */
.bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px var(--pad) calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: #090b12e6;
  backdrop-filter: blur(8px);
}
.run {
  min-width: 92px; height: 40px; border-radius: 10px; border: 0;
  background: var(--ink); color: var(--bg); font-weight: 600; letter-spacing: .01em;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.run:hover { background: #fff; }
.run.running { background: var(--accent); }
.run.running::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--bg);
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 50% { transform: scale(.55); opacity: .5; } }
.run:disabled { opacity: .4; cursor: default; }
.ghost {
  height: 40px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--line-2); background: transparent; color: var(--ink-2);
}
.ghost:hover:not(:disabled) { color: var(--ink); border-color: var(--ink-3); }
.ghost:disabled { opacity: .35; cursor: default; }
.readout { flex: 1; min-width: 0; text-align: right; font-family: var(--mono); font-size: 12px; line-height: 1.35; }
.status { color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status.err { color: var(--danger); white-space: normal; }
.meter { color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------------------------------------------------------------- settings */
.sheet {
  position: fixed; z-index: 20; top: 56px; right: 12px; width: min(360px, calc(100vw - 24px));
  max-height: calc(100dvh - 140px); overflow: auto;
  background: var(--bg-2); border: 1px solid var(--line-2); border-radius: 14px;
  padding: 14px 16px 16px; box-shadow: 0 20px 60px #0009;
}
.sheet[hidden] { display: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet h2 { font-size: 11px; text-transform: uppercase; letter-spacing: .12em; color: var(--ink-3); margin: 14px 0 6px; font-weight: 600; }
.sheet-head h2 { margin-top: 2px; }
.row { display: grid; grid-template-columns: 1fr 120px 44px; align-items: center; gap: 10px; padding: 6px 0; color: var(--ink-2); }
.row > span { font-size: 13px; }
.row em { font-style: normal; color: var(--ink-3); font-size: 11px; display: block; }
.row input[type="number"], .row select, .row textarea {
  grid-column: 2 / 4; width: 100%; background: var(--bg); border: 1px solid var(--line-2);
  border-radius: 8px; padding: 6px 8px; font-family: var(--mono); font-size: 13px;
}
.row input[type="range"] { width: 100%; accent-color: var(--accent); }
.row output { font-family: var(--mono); font-size: 12px; color: var(--ink); text-align: right; }
.row.col { grid-template-columns: 1fr; }
.row.col textarea { grid-column: 1; resize: vertical; }
.row.check { grid-template-columns: 20px 1fr; }
.row.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.fine { color: var(--ink-3); font-size: 11px; line-height: 1.5; margin: 14px 0 0; font-family: var(--mono); word-break: break-word; }

.history { width: min(460px, calc(100vw - 24px)); }
.ghost.small { height: 32px; font-size: 12px; margin-top: 8px; }
.ghost.danger { color: var(--danger); border-color: color-mix(in oklab, var(--danger), transparent 50%); }
.runs li { position: relative; }
.runs li .del {
  position: absolute; top: 6px; right: 6px; border: 0; background: transparent; color: var(--ink-3);
  font: 11px var(--mono); padding: 2px 6px; border-radius: 6px;
}
.runs li .del:hover { color: var(--danger); background: #ffffff0a; }
.notice {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px var(--pad); border-top: 1px solid var(--line); background: #121626;
  color: var(--ink-2); font-size: 12.5px; line-height: 1.45;
}
.notice[hidden] { display: none; }
.notice b { color: var(--ink); font-weight: 600; }
.notice > span:first-child { flex: 1 1 320px; }
.notice-actions { display: flex; gap: 8px; }
.storage-warning { color: var(--danger); background: #24151c; }
.runs { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.runs li button {
  width: 100%; text-align: left; border: 1px solid var(--line); background: var(--bg); border-radius: 10px;
  padding: 8px 10px; font: 12px/1.45 var(--mono); color: var(--ink-2);
}
.runs li button:hover { border-color: var(--ink-3); color: var(--ink); }
.runs .meta { display: flex; gap: 8px; color: var(--ink-3); font-size: 11px; margin-bottom: 3px; }
.runs .meta b { color: var(--accent); font-weight: 500; }
.runs .note { display: block; color: var(--accent); font-size: 11.5px; margin: 1px 0 4px; }
.runs .tail { color: var(--ink-3); }
.runs .head { color: var(--calm); }
.runs .snip { white-space: pre-wrap; word-break: break-word; max-height: 4.4em; overflow: hidden; display: block; }
.runs .empty { color: var(--ink-3); font: 12px var(--mono); padding: 8px 2px; }

/* ---------------------------------------------------------------- token popover */
.pop {
  position: fixed; z-index: 30; width: min(320px, calc(100vw - 24px));
  background: #0f1320f2; border: 1px solid var(--line-2); border-radius: 12px;
  box-shadow: 0 18px 50px #000a; padding: 10px; backdrop-filter: blur(10px);
  font-family: var(--mono); font-size: 12.5px;
}
.pop[hidden] { display: none; }
.pop-head { display: flex; align-items: baseline; gap: 8px; padding: 2px 4px 8px; border-bottom: 1px solid var(--line); margin-bottom: 6px; }
.pop-tok { color: var(--ink); background: #ffffff10; border-radius: 4px; padding: 1px 5px; white-space: pre; overflow: hidden; text-overflow: ellipsis; max-width: 150px; }
.pop-stat { color: var(--ink-3); margin-left: auto; white-space: nowrap; }
.alt {
  display: grid; grid-template-columns: 1fr 52px; align-items: center; gap: 8px; width: 100%;
  border: 0; background: transparent; text-align: left; border-radius: 7px; padding: 5px 6px; position: relative;
}
.alt:hover:not(:disabled) { background: #ffffff0d; }
.alt:disabled { cursor: default; }
.alt .bar-fill { position: absolute; left: 6px; bottom: 2px; height: 2px; border-radius: 2px; background: var(--accent); opacity: .55; }
.alt.chosen .bar-fill { background: var(--ink); opacity: .8; }
.alt .t { white-space: pre; overflow: hidden; text-overflow: ellipsis; color: var(--ink); }
.alt.chosen .t::after { content: "  ← sampled"; color: var(--ink-3); }
.alt .p { text-align: right; color: var(--ink-2); }
.ws { color: var(--ink-3); }
.pop-actions { display: flex; gap: 6px; padding-top: 8px; margin-top: 6px; border-top: 1px solid var(--line); }
.pop-actions button { flex: 1; border: 1px solid var(--line-2); background: transparent; color: var(--ink-2); border-radius: 8px; padding: 6px 0; font-size: 12px; }
.pop-actions button:hover:not(:disabled) { color: var(--ink); border-color: var(--ink-3); }
.pop-hint { color: var(--ink-3); font-size: 11px; padding: 6px 4px 0; }

@media (max-width: 640px) {
  :root { --pad: 16px; }
  .model { display: none; }
  .doc { font-size: 14px; padding-top: 18px; }
  .doc.serif { font-size: 17px; }
  .bar { flex-wrap: wrap; }
  .readout { flex-basis: 100%; order: -1; text-align: left; display: flex; flex-direction: column; gap: 1px; }
  .status { white-space: normal; }
  .meter { font-size: 11px; }
  .run { flex: 1; }
  .sheet { top: auto; bottom: 0; right: 0; left: 0; width: 100%; border-radius: 16px 16px 0 0; max-height: 75dvh; padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
  .pop { left: 8px !important; right: 8px; bottom: calc(118px + env(safe-area-inset-bottom)) !important; top: auto !important; width: auto; }
}
