:root {
  color-scheme: light;
  --bg: #fff;
  --fg: hsl(240 10% 3.9%);
  --muted: hsl(240 3.8% 46.1%);
  --border: hsl(240 5.9% 90%);
  --soft: hsl(240 4.8% 96.5%);
  --primary: hsl(240 5.9% 10%);
  --on-primary: #fafafa;
  --ok: hsl(142 60% 30%);
  --live: hsl(212 90% 42%);
  --bad: hsl(0 72% 45%);
  --left-w: 280px;
  --right-w: 300px;
  --header-h: 48px;
  --composer-h: auto;
  --radius: .5rem;
  --font-display: "DM Serif Display", ui-serif, Georgia, serif;
  --font-sans: "Space Grotesk", ui-sans-serif, system-ui, sans-serif;
  --font-mono: "Roboto Mono", ui-monospace, monospace;
  --shadow: 0 8px 24px rgb(0 0 0 / .08);
}
* { box-sizing: border-box; margin: 0; }
html, body, #root {
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}
body {
  font: 14px/1.45 var(--font-sans);
  color: var(--fg);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { opacity: .4; cursor: default; }
.sr { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* ===== FIXED VIEWPORT SHELL ===== */
.workspace {
  display: grid;
  grid-template-columns: var(--left-w) 6px minmax(0, 1fr) 6px var(--right-w);
  grid-template-rows: 100%;
  height: 100%;
  max-height: 100dvh;
  width: 100%;
  overflow: hidden;
}
.workspace.left-collapsed { --left-w: 0px; }
.workspace.right-collapsed { --right-w: 0px; }
.workspace.left-collapsed { grid-template-columns: 0 0 minmax(0, 1fr) 6px var(--right-w); }
.workspace.right-collapsed { grid-template-columns: var(--left-w) 6px minmax(0, 1fr) 0 0; }
.workspace.left-collapsed.right-collapsed { grid-template-columns: 0 0 minmax(0, 1fr) 0 0; }

.gutter {
  background: var(--border);
  cursor: col-resize;
  position: relative;
  z-index: 5;
  touch-action: none;
}
.gutter:hover, .gutter.dragging { background: var(--primary); opacity: .35; }
.gutter.left-g { grid-column: 2; }
.gutter.right-g { grid-column: 4; }
.workspace.left-collapsed .gutter.left-g { display: none; }
.workspace.right-collapsed .gutter.right-g { display: none; }

.rail {
  min-width: 0;
  min-height: 0;
  height: 100%;
  max-height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.rail.left { grid-column: 1; border-right: 1px solid var(--border); }
.rail.right { grid-column: 5; border-left: 1px solid var(--border); }
.workspace.left-collapsed .rail.left { display: none; }
.workspace.right-collapsed .rail.right { display: none; }

.rail-head {
  flex: 0 0 var(--header-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 .65rem; border-bottom: 1px solid var(--border);
  font-family: var(--font-display); font-size: 1rem;
}
.rail-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}
.rail-section { border-bottom: 1px solid var(--border); }
.rail-section-h {
  display: flex; align-items: center; justify-content: space-between;
  padding: .45rem .65rem .25rem;
  font-size: .62rem; letter-spacing: .1em; text-transform: uppercase;
  color: var(--muted); font-weight: 600;
}
.rail-section-h button { border: 0; background: transparent; color: var(--muted); font-size: .9rem; padding: .1rem .3rem; border-radius: .3rem; }
.rail-section-h button:hover { background: var(--soft); color: var(--fg); }

.sess-list, .proj-list, .act-list { list-style: none; padding: .2rem .4rem .5rem; display: grid; gap: .15rem; }
.sess-list button, .proj-list button {
  width: 100%; text-align: left; border: 0; background: transparent;
  padding: .4rem .45rem; border-radius: var(--radius); display: grid; gap: .05rem;
}
.sess-list button:hover, .proj-list button:hover, .sess-list .active, .proj-list .active { background: var(--soft); }
.sess-title { font-size: .82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sess-meta, .proj-list span, .hint, .empty-li { color: var(--muted); font-size: .68rem; }
.proj-list strong { font-size: .8rem; }

.model-bar {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  padding: .45rem .55rem;
  background: var(--bg);
}
.model-bar label { display: block; font-size: .6rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin-bottom: .25rem; }
.model-bar select {
  width: 100%; height: 2rem; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); padding: 0 .4rem; font-size: .8rem;
}
.model-bar .hint { margin-top: .25rem; line-height: 1.3; }

/* CENTER */
.center {
  grid-column: 3;
  min-width: 0;
  min-height: 0;
  height: 100%;
  max-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.masthead {
  flex: 0 0 var(--header-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 .55rem; border-bottom: 1px solid var(--border);
  background: color-mix(in oklab, var(--bg) 94%, transparent); backdrop-filter: blur(8px);
}
.mast-left, .mast-right { display: flex; align-items: center; gap: .35rem; }
.brand { text-decoration: none; color: inherit; }
.brand h1 { font: 400 1.25rem/1 var(--font-display); }
.pulse { width: 7px; height: 7px; border-radius: 50%; background: var(--border); }
.pulse.on { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in oklab, var(--ok) 18%, transparent); }
.ghost, .icon-btn {
  border: 1px solid transparent; background: transparent; color: var(--muted);
  height: 1.9rem; padding: 0 .45rem; border-radius: var(--radius); font-size: .75rem;
}
.icon-btn { width: 1.9rem; padding: 0; border-color: var(--border); }
.ghost:hover, .icon-btn:hover { background: var(--soft); color: var(--fg); }
.icon-btn.rec { color: var(--bad); border-color: var(--bad); }

.msgs {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
  padding: .85rem .75rem 1rem;
  display: flex; flex-direction: column; gap: .55rem;
}

.row { display: flex; flex-direction: column; gap: .25rem; }
.row.user { align-items: flex-end; }
.row.assistant { align-items: flex-start; }
.bubble { max-width: min(94%, 40rem); padding: .55rem .75rem; border-radius: .85rem; font-size: .9rem; line-height: 1.5; }
.bubble.user { background: var(--primary); color: var(--on-primary); border-bottom-right-radius: .2rem; white-space: pre-wrap; }
.bubble.bot, .bubble.assistant { background: transparent; padding: .05rem 0; max-width: 100%; }
.bubble img { max-height: 200px; border-radius: .55rem; border: 1px solid var(--border); display: block; }
.msg-actions { display: flex; gap: .15rem; opacity: 0; transition: opacity .12s; }
.row.assistant:hover .msg-actions, .row.assistant:focus-within .msg-actions { opacity: 1; }

.prose { font-size: .9rem; line-height: 1.55; }
.prose :where(p,ul,ol,pre,blockquote) { margin: .4rem 0; }
.prose :where(h1,h2,h3) { font-family: var(--font-display); font-weight: 400; margin: .55rem 0 .25rem; }
.prose ul, .prose ol { padding-left: 1.1rem; }
.prose code { font-family: var(--font-mono); font-size: .8em; background: var(--soft); padding: .05rem .25rem; border-radius: 3px; }
.prose pre { font-family: var(--font-mono); font-size: .74rem; background: var(--soft); border: 1px solid var(--border); border-radius: var(--radius); padding: .55rem; overflow: auto; max-height: 40vh; }
.prose pre code { background: none; padding: 0; }

.tool-chip { color: var(--muted); font-size: .68rem; }
.tool-chip summary { list-style: none; display: inline-flex; align-items: center; gap: .25rem; cursor: pointer; padding: .1rem .35rem; border-radius: 999px; }
.tool-chip summary::-webkit-details-marker { display: none; }
.tool-chip summary:hover { background: var(--soft); color: var(--fg); }
.tool-chip .dot { width: 5px; height: 5px; border-radius: 50%; background: var(--border); }
.tool-chip.run .dot { background: var(--live); }
.tool-chip.done .dot { background: var(--ok); }
.tool-chip .name { font-family: var(--font-mono); }
.tool-body { margin: .2rem 0 .35rem; padding: .4rem; border: 1px solid var(--border); border-radius: var(--radius); background: var(--soft); max-width: 32rem; }
.tool-body pre { margin: 0; white-space: pre-wrap; max-height: 8rem; overflow: auto; font: 400 .66rem/1.3 var(--font-mono); color: var(--muted); }
.tool-body .out { color: var(--ok); }
.reason { color: var(--muted); font-size: .7rem; }
.reason summary { cursor: pointer; font-family: var(--font-mono); list-style: none; }
.reason summary::-webkit-details-marker { display: none; }
.reason-body { margin-top: .2rem; white-space: pre-wrap; font: 400 .68rem/1.35 var(--font-mono); max-height: 6rem; overflow: auto; }

.composer-wrap {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  background: color-mix(in oklab, var(--bg) 95%, transparent);
  backdrop-filter: blur(8px);
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.attach-row { max-width: 780px; margin: 0 auto; padding: .4rem .65rem 0; display: flex; flex-wrap: wrap; gap: .25rem; }
.attach-pill { display: inline-flex; gap: .2rem; align-items: center; padding: .12rem .4rem; border-radius: 999px; border: 1px solid var(--border); background: var(--soft); font-size: .68rem; }
.attach-pill button { border: 0; background: none; color: var(--muted); }
.composer { display: flex; gap: .35rem; align-items: flex-end; max-width: 780px; margin: 0 auto; padding: .55rem .65rem .7rem; }
.composer textarea {
  flex: 1; resize: none; min-height: 2.25rem; max-height: 7rem; padding: .5rem .6rem;
  border: 1px solid var(--border); border-radius: .75rem; background: var(--bg); outline: 0;
}
.composer textarea:focus { box-shadow: 0 0 0 3px color-mix(in oklab, var(--primary) 10%, transparent); }
.primary-action {
  min-height: 2.25rem; min-width: 4.2rem; padding: 0 .7rem; border-radius: .75rem;
  border: 1px solid var(--primary); background: var(--primary); color: var(--on-primary); font-weight: 600; font-size: .8rem;
}
.primary-action.secondary { background: transparent; color: var(--fg); border-color: var(--border); font-weight: 500; }

/* RIGHT inspector */
.tabs { display: flex; gap: .1rem; flex-wrap: wrap; }
.tabs button {
  border: 0; background: transparent; color: var(--muted); padding: .25rem .4rem; border-radius: .35rem; font-size: .72rem;
}
.tabs button.active, .tabs button:hover { background: var(--soft); color: var(--fg); }
.act-list { max-height: none; }
.act { border: 1px solid var(--border); border-radius: var(--radius); padding: .4rem .45rem; }
.act-top { display: flex; justify-content: space-between; gap: .3rem; margin-bottom: .15rem; }
.act-kind, .act-st { font: 500 .6rem var(--font-mono); color: var(--muted); text-transform: uppercase; }
.act.running .act-st { color: var(--live); }
.act.done .act-st { color: var(--ok); }
.act.error .act-st { color: var(--bad); }
.act strong { font-size: .76rem; display: block; }
.act pre { margin-top: .25rem; white-space: pre-wrap; max-height: 4.5rem; overflow: auto; font: 400 .64rem/1.3 var(--font-mono); color: var(--muted); }

.tree-list { list-style: none; padding: .35rem .45rem; font: 400 .72rem/1.35 var(--font-mono); }
.tree-list button {
  width: 100%; text-align: left; border: 0; background: transparent; padding: .25rem .35rem; border-radius: .3rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tree-list button:hover, .tree-list .active { background: var(--soft); }
.file-view {
  margin: .35rem .45rem .6rem; padding: .5rem; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--soft); max-height: 40vh; overflow: auto;
  font: 400 .68rem/1.35 var(--font-mono); white-space: pre-wrap; word-break: break-word;
}
.panel-pad { padding: .55rem; display: grid; gap: .4rem; }
.field { width: 100%; height: 2rem; border: 1px solid var(--border); border-radius: var(--radius); padding: 0 .45rem; background: var(--bg); }
.ghost.block { width: 100%; border: 1px solid var(--border); padding: .4rem; border-radius: var(--radius); background: var(--soft); text-align: center; }

.overlay {
  position: fixed; inset: 0; z-index: 80; background: rgb(0 0 0 / .28); backdrop-filter: blur(3px);
  display: grid; place-items: start center; padding: max(10vh, env(safe-area-inset-top)) 1rem 1rem;
}
.palette, .sheet {
  width: min(460px, 100%); background: var(--bg); border: 1px solid var(--border);
  border-radius: .85rem; box-shadow: var(--shadow); overflow: hidden; max-height: 70dvh; display: flex; flex-direction: column;
}
.palette input { width: 100%; border: 0; border-bottom: 1px solid var(--border); padding: .75rem .9rem; outline: 0; flex: 0 0 auto; }
.palette ul { list-style: none; overflow: auto; padding: .25rem; flex: 1; min-height: 0; }
.palette li button { width: 100%; text-align: left; border: 0; background: transparent; padding: .5rem .6rem; border-radius: .45rem; }
.palette li button:hover { background: var(--soft); }
.sheet header { display: flex; justify-content: space-between; align-items: center; padding: .65rem .85rem; border-bottom: 1px solid var(--border); flex: 0 0 auto; }
.sheet h2 { font: 400 1.1rem var(--font-display); }
.mcp-form { display: grid; gap: .4rem; padding: .75rem; }
.mcp-form input { height: 2.05rem; border: 1px solid var(--border); border-radius: var(--radius); padding: 0 .5rem; }
.mcp-list { list-style: none; padding: 0 .75rem .75rem; display: grid; gap: .35rem; overflow: auto; min-height: 0; }
.mcp-list li { display: flex; justify-content: space-between; gap: .4rem; padding: .4rem; border: 1px solid var(--border); border-radius: var(--radius); }
.mcp-list code { display: block; font: 400 .64rem var(--font-mono); color: var(--muted); word-break: break-all; }

.tts-status { font-size: .68rem; color: var(--muted); padding: 0 .65rem .35rem; max-width: 780px; margin: 0 auto; }

/* MOBILE: fixed viewport, rails as drawers */
@media (max-width: 860px) {
  .workspace, .workspace.left-collapsed, .workspace.right-collapsed,
  .workspace.left-collapsed.right-collapsed {
    grid-template-columns: 1fr !important;
  }
  .gutter { display: none !important; }
  .rail {
    position: fixed; z-index: 60; top: 0; bottom: 0;
    width: min(86vw, 320px); max-height: 100dvh;
    box-shadow: var(--shadow); background: var(--bg);
  }
  .rail.left { left: 0; transform: translateX(-105%); transition: transform .2s ease; }
  .rail.right { right: 0; left: auto; transform: translateX(105%); transition: transform .2s ease; border-left: 1px solid var(--border); }
  .workspace:not(.left-collapsed) .rail.left { transform: translateX(0); display: flex; }
  .workspace:not(.right-collapsed) .rail.right { transform: translateX(0); display: flex; }
  .workspace.left-collapsed .rail.left { display: flex; transform: translateX(-105%); pointer-events: none; }
  .workspace.right-collapsed .rail.right { display: flex; transform: translateX(105%); pointer-events: none; }
  .center { grid-column: 1 !important; }
  .backdrop {
    display: none; position: fixed; inset: 0; z-index: 55; background: rgb(0 0 0 / .35);
  }
  .workspace:not(.left-collapsed) .backdrop.left-bd,
  .workspace:not(.right-collapsed) .backdrop.right-bd { display: block; }
}


/* hard lock viewport — no horizontal bleed */
html, body, #root {
  width: 100%;
  max-width: 100vw;
  overflow: hidden !important;
}
.workspace {
  width: 100%;
  max-width: 100vw;
  box-sizing: border-box;
}
.rail.right, .rail.left {
  max-width: 100%;
  overflow: hidden;
}
.center {
  max-width: 100%;
  overflow: hidden;
}
/* when both open, fr absorbs — gutters fixed 6px */
@media (min-width: 861px) {
  .workspace {
    grid-template-columns: minmax(0, var(--left-w)) 6px minmax(0, 1fr) 6px minmax(0, var(--right-w));
  }
  .workspace.left-collapsed {
    grid-template-columns: 0 0 minmax(0, 1fr) 6px minmax(0, var(--right-w));
  }
  .workspace.right-collapsed {
    grid-template-columns: minmax(0, var(--left-w)) 6px minmax(0, 1fr) 0 0;
  }
  .workspace.left-collapsed.right-collapsed {
    grid-template-columns: 0 0 minmax(0, 1fr) 0 0;
  }
}
