:root {
  --bg: #0f0e13;
  --surface: #18161f;
  --surface-2: #211e2a;
  --surface-3: #2b2736;
  --border: #2f2b3a;
  --text: #f1eee8;
  --muted: #9a94a6;
  --accent: #e0a458;
  --accent-2: #f2c58a;
  --caramel: #c8813a;
  --caramel-light: #dd9a52;
  --on-caramel: #24150a;
  --good: #5fd39a;
  --warn: #f5c451;
  --bad: #ff6b6b;
  --radius: 12px;
  --rail: 52px;
  --chat: 400px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent-2); }
code { font: 12px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-3); padding: 1px 5px; border-radius: 5px; }
h1, h2, h3 { margin: 0 0 12px; line-height: 1.2; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.good { color: var(--good); }
.bad { color: var(--bad); }
.spacer { flex: 1; }
.break { word-break: break-all; }
[hidden] { display: none !important; }

/* ---- buttons & inputs */
/* Toffee buttons: a flat face with a darker "candy" edge underneath that
   the button sinks into when pressed. */
.btn {
  --edge: #0a090d;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 7px 14px 8px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: inherit; font-weight: 600; letter-spacing: .01em;
  box-shadow: 0 3px 0 var(--edge); transform: translateY(0);
  cursor: pointer; text-decoration: none; white-space: nowrap; transition: transform .06s, box-shadow .06s, background .12s;
}
.btn:hover { background: var(--surface-3); }
.btn:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 var(--edge); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary, .btn-publish { --edge: #9c5a1c; background: var(--caramel); border-color: var(--caramel-light); color: var(--on-caramel); }
.btn-primary:hover, .btn-publish:hover { background: var(--caramel-light); }
.btn-danger { color: var(--bad); }
.btn-wide { width: 100%; }
.btn-publish { text-transform: uppercase; font-size: 12px; letter-spacing: .08em; padding: 8px 16px 9px; }
.btn-publish .when-busy, .btn-publish.htmx-request .when-idle { display: none; }
.btn-publish.htmx-request .when-busy { display: inline; }
/* Round Send / Stop share one slot, so the bar never shifts when a run starts. */
.btn-round { width: 36px; height: 36px; padding: 0; border-radius: 50%; box-shadow: none; }
.btn-round:active:not(:disabled) { transform: scale(.94); box-shadow: none; }
.btn-send { border: 0; }
.btn-round.btn-stop { background: transparent; color: var(--accent-2); border: 1.5px solid color-mix(in srgb, var(--accent) 70%, transparent); }
.btn-round.btn-stop:hover { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.btn-round.btn-stop .icon { fill: currentColor; }
/* While the agent works the picker stays put but is locked. */
.is-running .tier-picker { opacity: .45; pointer-events: none; }

/* "Toffee is working…" at the end of the chat while a run is going. */
.typing { display: none; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; padding: 2px 2px 4px; }
.is-running .typing { display: flex; }
.typing-dots { display: inline-flex; gap: 4px; }
.typing-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: typing 1.2s ease-in-out infinite; }
.typing-dots i:nth-child(2) { animation-delay: .15s; }
.typing-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes typing { 0%, 60%, 100% { opacity: .25; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 9px;
  background: transparent; border: 0; color: var(--text); font-size: 18px; cursor: pointer; text-decoration: none;
}
.icon-btn:hover { background: var(--surface-3); }
.link-btn { background: none; border: 0; color: var(--muted); cursor: pointer; font: inherit; font-size: 12px; }
.link-btn:hover { color: var(--text); }
input, textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; resize: vertical;
}
input:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: -1px; }
input[type=radio] { width: auto; }

/* ---- top bar */
.topbar {
  display: flex; align-items: center; gap: 10px; height: 56px; padding: 0 12px;
  border-bottom: 1px solid var(--border); background: var(--bg);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 700; }
.brand-mark {
  display: inline-grid; place-items: center; width: 48px; height: 48px; border-radius: 14px;
  background: var(--accent); color: #1a1208; font-weight: 800; font-size: 24px;
}
.brand-mark.sm { width: 28px; height: 28px; border-radius: 8px; font-size: 15px; }
.divider { width: 1px; height: 22px; background: var(--border); }
.project-name input { background: transparent; border-color: transparent; font-weight: 600; width: 240px; padding: 6px 10px; }
.project-name input:hover { border-color: var(--border); }
/* Tabs: text with a caramel underline, no pill. */
.segmented { display: flex; gap: 4px; align-self: stretch; }
.seg {
  display: inline-flex; align-items: center; gap: 7px;
  background: transparent; border: 0; border-bottom: 2px solid transparent; color: var(--muted);
  padding: 0 10px; font: inherit; font-weight: 600; cursor: pointer;
}
.seg:hover { color: var(--text); }
.seg.active { color: var(--text); border-bottom-color: var(--caramel); }
.device-switch { gap: 0; padding-left: 8px; border-left: 1px solid var(--border); margin: 14px 0; align-self: auto; }
.device-switch .seg { display: grid; place-items: center; padding: 4px 8px; border-bottom: 0; border-radius: 4px; }
.device-switch .seg.active { background: var(--surface-3); color: var(--caramel-light); }
.user-menu { display: flex; align-items: center; gap: 8px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; object-fit: cover; }
.avatar-letter { display: inline-grid; place-items: center; background: var(--surface-3); font-weight: 700; }
.avatar-btn { display: grid; padding: 2px; border: 0; border-radius: 50%; background: transparent; color: var(--text); cursor: pointer; }
.avatar-btn:hover .avatar, .avatar-btn:focus-visible .avatar { box-shadow: 0 0 0 2px var(--caramel); }
.avatar-btn:focus-visible { outline: none; }
/* Account menu: a popover pinned under the avatar in the top-right corner. */
.menu-pop {
  inset: auto; top: 52px; right: 12px; margin: 0; min-width: 220px; padding: 6px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2); color: var(--text);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .45);
}
.menu-head { display: grid; gap: 2px; padding: 8px 10px 10px; border-bottom: 1px solid var(--border); margin-bottom: 6px; }
.menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 10px; border: 0; border-radius: 6px;
  background: transparent; color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--surface-3); outline: none; }

/* ---- login */
.page-login { display: grid; place-items: center; }
.login-card { width: min(380px, calc(100% - 32px)); text-align: center; display: grid; gap: 12px; justify-items: center; }
.login-card form { width: 100%; }

/* ---- dashboard & settings */
.dashboard, .settings { max-width: 1040px; margin: 0 auto; padding: 32px 16px 64px; display: grid; gap: 40px; }
.settings { max-width: 720px; gap: 20px; }
.new-project h1 { font-size: 28px; }
.new-form { display: grid; gap: 10px; max-width: 640px; }
.new-form .btn { justify-self: start; }
.project-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.project-card { display: grid; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; color: var(--text); text-decoration: none; }
.project-card:hover { border-color: var(--accent); }
.project-thumb { aspect-ratio: 16/9; display: grid; place-items: center; background: linear-gradient(135deg, #2b2140, #3d2a1c); font-size: 42px; font-weight: 800; color: var(--accent-2); }
.project-meta { display: grid; gap: 4px; padding: 12px; justify-items: start; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--surface-3); color: var(--muted); }
.badge-good { background: color-mix(in srgb, var(--good) 18%, transparent); color: var(--good); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.stack { display: grid; gap: 12px; margin-top: 16px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.radio-row { border: 0; padding: 0; margin: 0; display: grid; gap: 6px; }
.radio-row label { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.flash { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); margin: 0; }
.flash.bad { background: color-mix(in srgb, var(--bad) 14%, transparent); color: #ffb3b3; }
.flash.good { background: color-mix(in srgb, var(--good) 14%, transparent); color: var(--good); }
.pre { white-space: pre-wrap; font: 12px/1.5 ui-monospace, Menlo, monospace; }
.grow { flex: 1 1 200px; width: auto; min-width: 0; }
.mono { font: 13px ui-monospace, Menlo, monospace; }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.people { display: grid; gap: 14px; }
.people h2, .people p { margin: 0; }
.new-link { display: grid; gap: 10px; color: var(--text); }
.people-list { list-style: none; margin: 0; padding: 0; display: grid; }
.people-list li { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; padding: 10px 0; border-top: 1px solid var(--border); }
.person { display: grid; gap: 2px; min-width: 0; }
.person .small { overflow-wrap: anywhere; }

/* ---- studio layout */
.page-studio { overflow: hidden; }
.studio { display: flex; height: calc(100dvh - 56px); }
.rail { flex: 0 0 var(--rail); }
.drawer { flex: 0 0 300px; }
.chat { flex: 0 0 var(--chat); width: var(--chat); }
/* Icon-only rail; the active item gets a caramel bar on its left edge. */
.rail { display: flex; flex-direction: column; align-items: stretch; gap: 2px; padding: 8px 0; border-right: 1px solid var(--border); }
.rail-btn {
  position: relative; display: grid; place-items: center; height: 44px;
  background: none; border: 0; border-left: 3px solid transparent; color: var(--muted); font: inherit; cursor: pointer;
}
.rail-btn:hover:not(:disabled) { color: var(--text); background: var(--surface); }
.rail-btn.active { color: var(--caramel-light); border-left-color: var(--caramel); background: var(--surface); }
.rail-btn:disabled { opacity: .35; cursor: default; }
.rail-icon { display: grid; }
.icon { display: block; flex: none; }
.rail-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.rail-badge:empty { display: none; }
.rail-badge {
  position: absolute; top: 4px; right: 10px; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 99px;
  background: var(--surface-3); color: var(--text); font-size: 10px; line-height: 16px; font-weight: 700;
}
.rail-badge.bad { background: var(--bad); color: #fff; }
.rail-badge.warn { background: var(--warn); color: #1a1208; }

.drawer { width: 300px; border-right: 1px solid var(--border); background: var(--surface); overflow-y: auto; }
.panel > header { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 0 16px; }
.panel > header h3 { margin: 0; font-size: 15px; }
.panel-body { padding: 12px 16px 16px; display: grid; gap: 10px; align-content: start; }
.panel-note { padding: 0 16px; margin: 4px 0 0; }
.issues { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.issue { padding: 8px 10px; border-radius: 8px; background: var(--surface-2); border-left: 3px solid var(--border); font-size: 13px; }
.issue.error { border-left-color: var(--bad); }
.issue.warning { border-left-color: var(--warn); }
.issue-code { font-weight: 700; margin-right: 6px; }
.issue-file { font: 11px ui-monospace, Menlo, monospace; color: var(--muted); }
.files, .releases { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
.files li { display: flex; justify-content: space-between; gap: 8px; font: 12px ui-monospace, Menlo, monospace; padding: 3px 0; }
.releases li { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }

.stage { flex: 1; display: flex; flex-direction: column; gap: 8px; padding: 8px 16px 16px; min-width: 0; background:
  radial-gradient(circle at 50% 40%, #1f1b29 0, var(--bg) 70%); }
.frame-wrap { align-self: center; margin: auto 0; background: #000; border-radius: var(--radius); overflow: hidden; box-shadow: 0 0 0 1px var(--border), 0 20px 60px rgba(0,0,0,.4); }
.frame-wrap.device-fill { align-self: stretch; flex: 1; margin: 0; }
.frame-wrap.device-phone { width: 390px; height: 640px; max-height: 100%; border-radius: 28px; box-shadow: 0 0 0 10px #000, 0 0 0 11px var(--border); }
.frame-wrap.device-landscape { width: 844px; height: 390px; max-width: 100%; border-radius: 28px; box-shadow: 0 0 0 10px #000, 0 0 0 11px var(--border); }
#preview-frame { display: block; width: 100%; height: 100%; border: 0; background: #000; }
.stage-tools { display: flex; justify-content: flex-end; gap: 4px; }

/* ---- chat */
.chat { display: flex; flex-direction: column; min-height: 0; border-left: 1px solid var(--border); background: var(--surface); }
.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--border); font-weight: 600; }
.chat-scroll { flex: 1; overflow-y: auto; padding: 16px; display: grid; align-content: start; gap: 10px; }
#messages { display: grid; gap: 10px; }
.msg { max-width: 100%; overflow-wrap: anywhere; }
.msg-user { justify-self: end; max-width: 88%; background: var(--surface-3); padding: 10px 12px; border-radius: 14px 14px 4px 14px; }
.msg-author { font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.msg-text { white-space: pre-wrap; }
.msg-assistant { padding: 2px 2px; }
.msg-assistant.streaming { white-space: pre-wrap; }
.msg-assistant.streaming::after { content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; }
.md p { margin: 0 0 8px; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 4px 0 8px; padding-left: 20px; }
.md pre { background: var(--bg); padding: 10px; border-radius: 8px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.msg-tool { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; margin: -4px 0; }
.tool-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex: none; }
.msg-system { font-size: 13px; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); color: var(--muted); }
.msg-system.error { background: color-mix(in srgb, var(--bad) 14%, transparent); color: #ffb3b3; }

.empty-chat { display: grid; gap: 8px; margin-top: 8px; }
.bubble { background: var(--text); color: var(--bg); padding: 12px 14px; border-radius: 16px; margin: 0 0 12px; font-weight: 500; }
.suggestion {
  display: flex; align-items: center; gap: 12px; text-align: left; padding: 12px 14px; border-radius: var(--radius);
  background: var(--surface-2); border: 1px solid transparent; color: var(--text); font: inherit; cursor: pointer;
}
.suggestion:hover { border-color: var(--accent); }
.suggestion .chev { margin-left: auto; color: var(--muted); font-size: 18px; }

.composer { margin: 12px; padding: 10px; border: 1px solid var(--border); border-radius: 16px; background: var(--surface-2); display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; }
.composer textarea { border: 0; background: transparent; padding: 4px; resize: none; }
.composer textarea:focus { outline: none; }
.composer-bar { display: flex; align-items: center; gap: 10px; }
.composer-bar .run-status { margin-left: auto; flex: none; }
.tier-picker { position: relative; display: inline-flex; align-items: center; color: var(--text); flex: none; }
.tier-picker select {
  appearance: none; -webkit-appearance: none; background: transparent; border: 1px solid transparent; border-radius: 6px;
  color: inherit; font: inherit; font-weight: 600; font-size: 13px; padding: 5px 26px 5px 8px; cursor: pointer;
  /* Fit the chosen label exactly; the fallback leaves room for the longest one. */
  field-sizing: content; min-width: 9.5em;
}
@supports (field-sizing: content) { .tier-picker select { min-width: 0; } }
.tier-picker select:hover, .tier-picker select:focus-visible { border-color: var(--border); background: var(--surface-3); outline: none; }
.tier-picker select option { background: var(--surface-2); color: var(--text); }
.tier-picker .icon { position: absolute; right: 8px; pointer-events: none; color: var(--muted); }
.tier-hint { flex: 1 1 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.run-status { display: flex; align-items: center; gap: 10px; }

/* play mode: the game gets the screen */
.play-mode .stage-tools { display: none; }
.play-mode .rail, .play-mode .drawer, .play-mode .chat { display: none; }

#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 10; }
.toast { padding: 10px 14px; border-radius: 10px; background: var(--surface-3); box-shadow: 0 8px 30px rgba(0,0,0,.4); }
.toast.error { background: #4a1f24; color: #ffd0d0; }

@keyframes blink { 50% { opacity: 0; } }
@keyframes pulse { 50% { transform: scale(1.6); opacity: .4; } }

@media (max-width: 1100px) {
  :root { --chat: 340px; }
  .device-switch { display: none; }
}
@media (max-width: 760px) {
  .studio { flex-direction: column; }
  .stage { flex: 0 0 45dvh; }
  .chat { flex: 1; width: auto; min-height: 0; }
  .rail, .drawer { display: none; }
  .chat { border-left: 0; border-top: 1px solid var(--border); }
  .project-name input { width: 140px; }
  .segmented:not(.device-switch) { display: none; }
}
