:root {
  --bg: #111318;
  --panel: #171a21;
  --panel-2: #1e222b;
  --line: #2a2f3a;
  --text: #e6e8ee;
  --muted: #8b93a5;
  --accent: #d97757;
  --working: #4f9dff;
  --idle: #3fbf7f;
  --exited: #6b7280;
  --offline: #c9a227;
  --danger: #e5534b;
  --radius: 8px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
/* The app is a fixed screen: only its inner panels scroll, never the page itself. */
html:has(#app:not([hidden])), html:has(#app:not([hidden])) body { overflow: hidden; overscroll-behavior: none; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
}
button:hover { border-color: var(--muted); }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.error { color: var(--danger); }
[hidden] { display: none !important; }

/* login */
#login { height: 100%; display: grid; place-items: center; padding: 16px; }
#login-form {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 24px;
  display: grid;
  gap: 12px;
}
#login-form h1 { margin: 0; font-size: 20px; }
#login-form p { margin: 0; color: var(--muted); }
#login-form input, #spawn input, #spawn select {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  width: 100%;
}
#login-form button[type=submit], #spawn-start { background: var(--accent); border-color: var(--accent); color: #fff; }

/* layout */
#app { display: grid; grid-template-columns: 260px 1fr; height: var(--app-h, 100dvh); overflow: hidden; }
#sidebar { background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; }
.brand { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; font-weight: 600; border-bottom: 1px solid var(--line); }
.brand-right { display: flex; align-items: center; gap: 10px; }
button.link { border: 0; background: none; padding: 0; color: var(--muted); font-size: 12px; font-weight: 400; }
button.link:hover { color: var(--text); }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--offline); }
.conn.ok { background: var(--idle); }

.machine { border-bottom: 1px solid var(--line); padding: 8px 0; }
.machine-head { display: flex; align-items: center; gap: 8px; padding: 4px 16px; font-weight: 600; }
.machine-head .name { flex: 1; }
.machine-head.offline { color: var(--muted); }
.machine-head button { padding: 0 8px; line-height: 22px; }
.session {
  display: grid;
  grid-template-columns: 10px 1fr;
  column-gap: 8px;
  width: 100%;
  text-align: left;
  border: 0;
  border-radius: 0;
  background: none;
  padding: 6px 16px;
}
.session:hover { background: var(--panel-2); }
.session.active { background: var(--panel-2); box-shadow: inset 3px 0 var(--accent); }
.session .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session .sub { grid-column: 2; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.none { padding: 4px 16px 4px 34px; color: var(--muted); font-size: 12px; }

.dot { width: 8px; height: 8px; border-radius: 50%; align-self: center; background: var(--exited); flex: none; }
.dot.working { background: var(--working); animation: pulse 1.2s ease-in-out infinite; }
.dot.idle { background: var(--idle); }
.dot.offline { background: var(--offline); }
.dot.waiting { background: var(--accent); }

.section { padding: 8px 16px 2px; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
.session.observed .label { color: var(--muted); }
.session.observed:hover .label, .session.observed.active .label { color: var(--text); }

.pane.obs { padding: 12px 16px 24px; }
.notice { margin: 0 0 12px; padding: 8px 10px; border: 1px dashed var(--line); border-radius: 6px; color: var(--muted); font-size: 12px; }
.log { display: grid; gap: 10px; max-width: 900px; }
.entry .who { font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.entry .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.entry.user .text { background: var(--panel-2); border-radius: 6px; padding: 8px 10px; }
.entry.tool .text, .entry.result .text { font: 12px/1.4 "Cascadia Mono", Consolas, Menlo, monospace; color: var(--muted); }
.entry.result .text { max-height: 7.5em; overflow: hidden; }
.entry.error .text { color: var(--danger); }
@keyframes pulse { 50% { opacity: .35; } }

#main { display: grid; grid-template-rows: auto auto 1fr auto; min-width: 0; min-height: 0; }
#topbar { display: flex; align-items: stretch; border-bottom: 1px solid var(--line); background: var(--panel); min-width: 0; }
#back { display: none; border: 0; border-radius: 0; border-right: 1px solid var(--line); font-size: 26px; line-height: 1; padding: 0 16px; }
#title { display: none; }
#tabs { display: flex; overflow-x: auto; min-width: 0; }
.tab {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 0;
  border-right: 1px solid var(--line);
  border-radius: 0;
  background: none;
  padding: 10px 12px;
  white-space: nowrap;
  max-width: 240px;
}
.tab .label { overflow: hidden; text-overflow: ellipsis; }
.tab.active { background: var(--bg); box-shadow: inset 0 -2px var(--accent); }
.tab.unread .label { font-weight: 700; }
.tab .x { color: var(--muted); padding: 0 2px; }
.tab .x:hover { color: var(--text); }

#details { display: flex; align-items: center; gap: 12px; padding: 6px 12px; color: var(--muted); font-size: 12px; border-bottom: 1px solid var(--line); min-height: 33px; }
#details .grow { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#details button { font-size: 12px; padding: 2px 8px; }
#details button.danger { color: var(--danger); }
#details button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }

#terminals { position: relative; min-height: 0; background: var(--bg); }
.pane { position: absolute; inset: 0; padding: 6px 0 0 8px; overflow: auto; }
#empty { height: 100%; display: grid; place-content: center; text-align: center; padding: 16px; color: var(--text); }
#empty p { margin: 4px 0; }

#ended { margin: 0; padding: 12px; border-top: 1px solid var(--line); background: var(--panel); color: var(--muted); text-align: center; }
#composer { border-top: 1px solid var(--line); background: var(--panel); padding: 8px; display: grid; gap: 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.keys { display: flex; gap: 6px; overflow-x: auto; }
.keys button { flex: none; font-size: 12px; padding: 4px 10px; }
#compose-form { display: flex; gap: 8px; }
#compose {
  flex: 1;
  resize: none;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  max-height: 160px;
}
#compose-form button { background: var(--accent); border-color: var(--accent); color: #fff; }

dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  width: min(380px, calc(100% - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / .55); }
#spawn-form { display: grid; gap: 12px; }
#spawn-form h2 { margin: 0; font-size: 16px; }
#spawn-form label { display: grid; gap: 4px; color: var(--muted); }
#spawn-form menu { display: flex; justify-content: flex-end; gap: 8px; margin: 0; padding: 0; }

#toast {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 14px;
  max-width: calc(100% - 32px);
}

/* notifications box at the bottom of the session list */
.notify { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; padding: 14px 16px calc(14px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); font-size: 13px; }
.notify p { margin: 0; font-size: 12px; flex-basis: 100%; }
.notify > button:not(.link) { width: 100%; padding: 10px; }

/* review: artifacts grid + folder list, over the terminal area */
#details button.on { border-color: var(--accent); }
a { color: var(--accent); }
.review { position: absolute; inset: 0; display: grid; grid-template-rows: auto 1fr; background: var(--bg); z-index: 2; min-height: 0; }
.review-head { display: grid; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.seg { display: inline-flex; justify-self: start; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: none; padding: 6px 14px; }
.seg button.on { background: var(--panel-2); color: var(--text); box-shadow: inset 0 -2px var(--accent); }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button { flex: none; border-radius: 999px; font-size: 12px; padding: 4px 10px; }
.chips button.on { border-color: var(--accent); color: var(--text); background: var(--panel-2); }
.chips button:disabled { opacity: .4; cursor: default; }
.review-body { overflow-y: auto; overflow-x: hidden; min-height: 0; overscroll-behavior: contain; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; padding: 12px; }
.card {
  position: relative;
  display: grid;
  grid-template-rows: auto auto auto;
  gap: 2px;
  padding: 0 0 8px;
  text-align: left;
  border-radius: var(--radius);
  background: var(--panel);
  overflow: hidden;
  min-width: 0;
}
.card .thumb { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: #0b0d11; display: block; pointer-events: none; }
.card .thumb.icon { display: grid; place-content: center; justify-items: center; gap: 2px; font-size: 34px; }
.card .thumb.icon small { font-size: 11px; color: var(--muted); letter-spacing: .05em; }
.card-name { padding: 6px 8px 0; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-meta { padding: 0 8px; font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .badge, .file .badge { font-size: 10px; color: #fff; background: var(--accent); border-radius: 4px; padding: 1px 5px; }
.card .badge { position: absolute; top: 6px; left: 6px; }

.crumbs { display: flex; align-items: center; gap: 4px; padding: 8px 12px; border-bottom: 1px solid var(--line); overflow-x: auto; white-space: nowrap; }
.crumbs button { font-size: 12px; padding: 4px 10px; flex: none; }
.crumbs button + button::before { content: '/ '; color: var(--muted); }
.file-list { display: grid; }
.file {
  display: grid;
  grid-template-columns: 26px 1fr auto;
  grid-template-areas: "icon name badge" "icon meta meta";
  column-gap: 8px;
  width: 100%;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: none;
  padding: 10px 12px;
}
.file:hover { background: var(--panel-2); }
.file .icon { grid-area: icon; align-self: center; font-size: 18px; }
.file .name { grid-area: name; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file .meta { grid-area: meta; color: var(--muted); font-size: 12px; }
.file .badge { grid-area: badge; align-self: center; }
.pad { padding: 16px; }

/* full-screen viewer */
.viewer { position: fixed; inset: 0; z-index: 50; display: grid; grid-template-rows: auto 1fr; background: #07080b; height: var(--app-h, 100dvh); }
.viewer-bar { display: flex; align-items: center; gap: 6px; padding: 6px 8px; padding-top: max(6px, env(safe-area-inset-top)); background: rgb(17 19 24 / .92); border-bottom: 1px solid var(--line); }
.viewer-titles { flex: 1; min-width: 0; }
.viewer-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-meta { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.vbtn { flex: none; display: grid; place-items: center; min-width: 40px; height: 40px; padding: 0 8px; font-size: 18px; color: var(--text); text-decoration: none; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; }
.viewer-body { position: relative; min-height: 0; overflow: auto; display: grid; place-items: center; overscroll-behavior: contain; }
.viewer-body > img { max-width: 100%; max-height: 100%; object-fit: contain; cursor: zoom-in; }
.viewer-body.zoomed { place-items: start; }
.viewer-body.zoomed > img { max-width: none; max-height: none; cursor: zoom-out; }
.viewer-body > video { width: 100%; max-height: 100%; background: #000; }
.viewer-body > iframe { align-self: stretch; justify-self: stretch; width: 100%; height: 100%; border: 0; background: #fff; }
.viewer-body > .text-preview { align-self: start; justify-self: stretch; margin: 0; padding: 14px; white-space: pre-wrap; overflow-wrap: anywhere; font: 12px/1.5 "Cascadia Mono", Consolas, Menlo, monospace; color: var(--text); }
.audio-card { display: grid; justify-items: center; gap: 16px; padding: 24px; }
.audio-card audio { width: min(420px, 85vw); }
.big-icon { font-size: 64px; }
.vnav {
  position: fixed;
  top: 50%;
  z-index: 51;
  width: 44px;
  height: 64px;
  margin-top: -32px;
  font-size: 30px;
  line-height: 1;
  color: #fff;
  background: rgb(0 0 0 / .45);
  border: 0;
  border-radius: 10px;
}
.vnav.prev { left: 6px; }
.vnav.next { right: 6px; }

/* phone: two screens. The session list fills the screen; tapping a session opens it
   full screen with a back button. Nothing pans sideways and text boxes don't trigger
   iOS's zoom-on-focus (it zooms anything under 16px). */
@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  #app.show-session #sidebar { display: none; }
  #app:not(.show-session) #main { display: none; }
  #sidebar { border-right: 0; padding-bottom: env(safe-area-inset-bottom); }
  .brand { padding-top: max(14px, env(safe-area-inset-top)); }
  .session { padding: 10px 16px; }
  .machine-head button { padding: 4px 14px; font-size: 18px; }

  #topbar { padding-top: env(safe-area-inset-top); min-height: 48px; }
  #back { display: block; }
  #tabs { display: none; }
  #title { display: flex; align-items: center; gap: 8px; min-width: 0; padding: 0 12px; font-weight: 600; }
  #title .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  #details { flex-wrap: wrap; row-gap: 4px; }
  #details .grow { flex-basis: 100%; }
  #details button { padding: 6px 12px; }

  .pane { padding: 4px 0 0 4px; overflow: hidden; }
  .pane.obs { overflow-y: auto; overflow-x: hidden; padding: 10px 12px 24px; }

  input, select, textarea, #compose, #login-form input { font-size: 16px; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; padding: 8px; }
  .vnav { width: 38px; height: 56px; }
  .keys button { font-size: 14px; padding: 8px 12px; min-width: 44px; }
  #compose-form button { padding: 8px 16px; }

}
