:root {
  --ink: #1c2822;
  --manila: #f3efe4;
  --sheet: #fffdf8;
  --spine: #1b4332;
  --rule: #d9d3c6;
  --alert: #8d2f2a;
  --quiet: #5c675f;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--manila);
  color: var(--ink);
}

body {
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  font-family: Newsreader, "Iowan Old Style", Palatino, Georgia, serif;
  font-size: 16px;
  line-height: 1.35;
}

.mast {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: nowrap;
  gap: 0.75rem 1.25rem;
  align-items: end;
  justify-content: space-between;
  padding: 0.45rem 0.9rem 0.4rem;
  border-bottom: 1px solid var(--rule);
  background: var(--sheet);
}

.brand h1 {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 600;
  letter-spacing: -0.03em;
  white-space: nowrap;
}

.brand p {
  margin: 0.1rem 0 0;
  color: var(--quiet);
}

.settings {
  display: flex;
  flex: 1;
  flex-wrap: nowrap;
  justify-content: flex-end;
  gap: 0.55rem 0.7rem;
  align-items: end;
  min-width: 0;
}

.settings label {
  display: grid;
  gap: 0.2rem;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.72rem;
  letter-spacing: 0.01em;
  color: var(--quiet);
}

.settings select,
.settings input,
.composer textarea,
button {
  font: inherit;
  color: var(--ink);
  background: var(--sheet);
  border: 1px solid var(--rule);
  border-radius: 2px;
}

.settings select,
.settings input {
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.78rem;
  padding: 0.32rem 0.45rem;
  min-width: 6.5rem;
}

.settings .key {
  flex: 1 1 12rem;
  min-width: 8rem;
  max-width: 22rem;
}

.settings .key input {
  width: 100%;
  min-width: 0;
}

button {
  cursor: pointer;
  background: var(--spine);
  color: #f7f4ec;
  border-color: var(--spine);
  padding: 0.45rem 0.75rem;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.78rem;
}

button:disabled {
  cursor: progress;
  opacity: 0.55;
}

.key-note {
  margin: 0;
  padding: 0.4rem 1.25rem;
  background: #f6e7e4;
  color: var(--alert);
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.75rem;
}

.desk {
  display: grid;
  grid-template-columns: minmax(0, 3fr) minmax(22rem, 2fr);
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.rail {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  border-right: 8px solid var(--spine);
  background: var(--sheet);
}

.demos {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  border-top: 1px solid var(--rule);
  background: var(--sheet);
}

.scripts {
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  align-items: stretch;
  width: 100%;
  min-width: 0;
  gap: 0.28rem;
  padding: 0.4rem 0.6rem 0.45rem;
}

.scripts-intro {
  margin: 0;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.72rem;
  line-height: 1.35;
  color: var(--quiet);
}

.scripts-intro strong {
  color: var(--spine);
  font-weight: 500;
}

.script-row {
  display: grid;
  grid-template-columns: 4.4rem minmax(0, 1fr);
  gap: 0.2rem 0.4rem;
  align-items: center;
  min-width: 0;
}

.script-label {
  margin: 0;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  color: var(--quiet);
}

.script-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  min-width: 0;
}

.scripts button {
  flex: 0 0 auto;
  white-space: nowrap;
  background: transparent;
  color: var(--spine);
  border-color: var(--spine);
  padding: 0.22rem 0.4rem;
  font-size: 0.7rem;
}

.scripts button .count {
  margin-left: 0.4rem;
  font-size: 0.62rem;
  color: var(--quiet);
}

.play-status {
  margin: 0;
  padding: 0 0.6rem 0.3rem;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.75rem;
  color: var(--quiet);
}

.thread {
  list-style: none;
  margin: 0;
  padding: 0.35rem 0.9rem 0.6rem;
  overflow: auto;
  flex: 1;
  min-height: 0;
}

.thread li {
  max-width: min(26rem, 82%);
  margin: 0 0 0.55rem;
  padding: 0;
}

.thread li.user,
.thread li.assistant {
  display: flex;
  flex-direction: column;
}

.thread li.user {
  margin-left: auto;
  align-items: flex-end;
}

.thread li.assistant {
  align-items: flex-start;
}

.thread li.user p,
.thread li.assistant p {
  padding: 0.4rem 0.7rem;
  border: 1px solid var(--rule);
  line-height: 1.35;
}

.thread li.user p {
  background: #e7f0ea;
  border-radius: 0.85rem 0.85rem 0.2rem 0.85rem;
}

.thread li.assistant p {
  background: #fff;
  border-radius: 0.85rem 0.85rem 0.85rem 0.2rem;
}

.thread p.waiting {
  display: flex;
  align-items: center;
  min-height: 1.35rem;
  min-width: 3.2rem;
}

.thread .dots {
  display: inline-flex;
  gap: 0.28rem;
  align-items: center;
  height: 0.7rem;
}

.thread .dots i {
  width: 0.38rem;
  height: 0.38rem;
  border-radius: 50%;
  background: var(--spine);
  animation: desk-think 1s infinite ease-in-out;
}

.thread .dots i:nth-child(2) { animation-delay: 0.15s; }
.thread .dots i:nth-child(3) { animation-delay: 0.3s; }

.thread li.system {
  max-width: none;
}

.thread .who {
  display: block;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  color: var(--quiet);
  margin-bottom: 0.15rem;
}

.thread p {
  margin: 0;
  white-space: pre-wrap;
}

.thread li.system p {
  color: var(--alert);
}

.demo-board {
  flex: 0 0 auto;
  max-height: none;
  overflow: visible;
  margin: 0 0.6rem 0.55rem;
  padding: 0.35rem 0.55rem 0.45rem;
  background: #f7f4ec;
  border: 1px solid var(--rule);
  border-left: 3px solid var(--spine);
}

.demo-board[hidden] { display: none; }

.demo-board .who {
  display: block;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  color: var(--spine);
  margin-bottom: 0.15rem;
}

.script-sheet .sheet-note {
  margin: 0.05rem 0 0.15rem;
  color: var(--quiet);
  font-size: 0.78rem;
}

.script-sheet ol {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  margin: 0.25rem 0 0;
  padding: 0;
}

.script-sheet .step {
  display: flex;
  gap: 0.45rem;
  align-items: baseline;
  padding: 0.15rem 0;
  background: transparent;
  border: 0;
}

.script-sheet .mark {
  flex: 0 0 auto;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  color: var(--quiet);
}

.script-sheet .step.thinking .mark { color: var(--spine); }
.script-sheet .step.answered .mark { color: var(--spine); }
.script-sheet .step.failed .expect { color: var(--alert); }

.script-sheet .tag {
  display: block;
  margin-bottom: 0.05rem;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  color: var(--quiet);
}

.script-sheet .q {
  margin: 0.22rem 0 0;
}

.script-sheet .expect {
  margin: 0.12rem 0 0;
  color: var(--quiet);
  font-size: 0.75rem;
  line-height: 1.25;
}

.script-sheet .expect .tag {
  font-size: 0.62rem;
}

.script-sheet .live {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.15rem 0.45rem;
  margin: 0.4rem 0 0;
  padding: 0.4rem 0.55rem 0.45rem;
  background: var(--sheet);
  border: 1px solid #c5d5cb;
  border-left: 3px solid var(--spine);
}

.script-sheet .live .tag {
  display: inline;
  margin: 0;
  color: var(--spine);
}

.script-sheet .live .reply {
  flex: 1 1 8rem;
  min-width: 0;
  color: var(--ink);
}

.script-sheet .step.thinking .reply {
  flex: 0 0 auto;
}

.script-sheet .step.queued .live {
  background: transparent;
  border-color: var(--rule);
  border-left-color: var(--rule);
}

.script-sheet .step.queued .live .tag,
.script-sheet .step.queued .reply {
  color: var(--quiet);
}

.script-sheet .step.thinking .live {
  background: #e7f0ea;
  border-color: var(--spine);
}

.script-sheet .step.failed .live {
  background: #f6e7e4;
  border-color: #e4c8c4;
  border-left-color: var(--alert);
}

.script-sheet .step.failed .live .tag,
.script-sheet .step.failed .reply {
  color: var(--alert);
}

.script-sheet .dots {
  display: inline-flex;
  gap: 0.22rem;
  height: 0.7rem;
  align-items: center;
}

.script-sheet .dots[hidden] {
  display: none;
}

.script-sheet .dots i {
  width: 0.32rem;
  height: 0.32rem;
  border-radius: 50%;
  background: var(--spine);
  animation: desk-think 1s infinite ease-in-out;
}

.script-sheet .dots i:nth-child(2) { animation-delay: 0.15s; }
.script-sheet .dots i:nth-child(3) { animation-delay: 0.3s; }

@keyframes desk-think {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

.composer {
  display: flex;
  flex: 0 0 auto;
  gap: 0.45rem;
  align-items: end;
  padding: 0.45rem 0.75rem 0.55rem;
  border-top: 1px solid var(--rule);
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 2.35rem;
  max-height: 2.35rem;
  padding: 0.4rem 0.55rem;
  font-size: 0.95rem;
}

.ledger {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: min-content;
  align-content: start;
  flex: 0 1 auto;
  gap: 0.45rem 0.7rem;
  max-height: 52%;
  min-height: 0;
  overflow: auto;
  padding: 0.45rem 0.6rem 0.5rem;
  background: var(--sheet);
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  line-height: 1.3;
  font-variant-numeric: tabular-nums;
}

.ledger h2 {
  margin: 0 0 0.18rem;
  font-family: Newsreader, "Iowan Old Style", Palatino, Georgia, serif;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.15;
  letter-spacing: -0.02em;
  color: var(--ink);
}

.ledger h2:first-child { margin-top: 0; }

.stats {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.2rem 0.35rem;
  margin: 0;
}

.stats div {
  min-width: 0;
}

.stats dt {
  color: var(--quiet);
  font-size: 0.58rem;
  font-weight: 400;
}

.stats dd {
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.72rem;
}

.phases {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.12rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.phases li {
  display: flex;
  justify-content: space-between;
  gap: 0.3rem;
  min-width: 0;
  padding: 0.06rem 0.28rem;
  border-radius: 0.2rem;
  color: var(--quiet);
}

.phases li.current {
  color: var(--spine);
  background: #e7f0ea;
}

.phases .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.phases .freedom {
  flex: 0 0 auto;
  font-size: 0.58rem;
  color: var(--quiet);
}

.rows {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.02rem;
  margin: 0;
}

.rows div {
  display: grid;
  grid-template-columns: 4.7rem minmax(0, 1fr);
  gap: 0.35rem;
  align-items: baseline;
}

.rows dt { color: var(--quiet); }
.rows dd {
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pane { min-width: 0; }
.pane.wide { grid-column: 1 / -1; }

.outbox {
  white-space: pre-wrap;
  margin: 0.2rem 0 0;
  padding: 0.35rem 0.45rem;
  background: #f7f4ec;
  border: 1px solid var(--rule);
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  line-height: 1.35;
  max-height: 8.5rem;
  overflow: auto;
}

.ledger .quiet {
  margin: 0;
  color: var(--quiet);
}

.guard-yes { color: var(--alert); }

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

@media (max-width: 1100px) {
  .desk { grid-template-columns: minmax(0, 1.2fr) minmax(18rem, 1fr); }
  .ledger { font-size: 0.64rem; gap: 0.2rem 0.55rem; }
  .scripts { gap: 0.25rem; }
  .scripts button { padding: 0.3rem 0.4rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto; }
  .script-sheet .dots i,
  .thread .dots i { animation: none; opacity: 0.8; }
}
