:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-hover: #1c232c;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #2f81f7;
  --accent-text: #ffffff;
  --danger: #f85149;
  --ok: #3fb950;
  --warn: #d29922;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --vh: 100dvh;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

[hidden] { display: none !important; }
body.in-terminal { overflow: hidden; } /* nothing behind the terminal to scroll to */
.muted { color: var(--muted); font-weight: normal; }
.error { color: var(--danger); margin: 8px 0; }

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

button {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  touch-action: manipulation;
}
button:hover { background: var(--panel-hover); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.primary:hover { filter: brightness(1.1); }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.ghost:hover { color: var(--text); background: var(--panel); }
button.danger:hover { color: var(--danger); }

input, select, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 16px; /* below 16px iOS zooms the page on focus */
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ------------------------------------------------ list view */

#list-view {
  max-width: 1100px;
  margin: 0 auto;
  /* As an installed app the page extends under the status bar, notch and
     home indicator, so keep content inside the safe area on every side. */
  padding: calc(16px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    calc(32px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}
.topbar h1 { font-size: 22px; margin: 0; }
.topbar h1 .muted { font-size: 14px; margin-left: 6px; }
.topbar-actions { display: flex; gap: 6px; flex-shrink: 0; }

/* Phones: the action buttons get their own row under the title. */
@media (max-width: 560px) {
  .topbar { flex-wrap: wrap; row-gap: 10px; }
  .topbar-actions { width: 100%; }
  .topbar-actions #new-btn { margin-right: auto; }
  .topbar-actions .ghost { padding: 8px 10px; }
}

.projects { margin-bottom: 16px; }
.projects-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 8px; }
.projects-head h2 { margin: 0; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.projects-head .link { font-size: 13px; }
.project-list { display: flex; gap: 8px; flex-wrap: wrap; }
.project { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 10px; font-weight: 600; }
.project.running::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.project-count { font-size: 12px; font-weight: normal; color: var(--muted); }
.project.editing { border-style: dashed; }
.project.editing:hover { border-color: var(--danger); color: var(--danger); }
.project-x { color: var(--danger); font-size: 16px; line-height: 1; }

.sessions {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
  gap: 12px;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  cursor: pointer;
  text-align: left;
}
.card:hover, .card:focus-visible { border-color: var(--accent); background: var(--panel-hover); }

.card-head { display: flex; align-items: center; gap: 8px; min-width: 0; }
.card-name { font-weight: 600; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  font: 12px var(--mono);
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  flex-shrink: 0;
}
.badge.live { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 50%, transparent); }
.state {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  flex-shrink: 0;
  white-space: nowrap;
}
.state.working { color: var(--muted); border: 1px solid var(--border); }
.state.working::before { content: "● "; animation: pulse 1.4s ease-in-out infinite; }
.state.done { color: var(--accent-text); background: var(--accent); }
.state.permission { color: #1b1300; background: var(--warn); font-weight: 600; }
@keyframes pulse { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) { .state.working::before { animation: none; } }

.card.attention { border-color: color-mix(in srgb, var(--accent) 70%, transparent); }

.reply { display: flex; gap: 6px; align-items: center; cursor: default; }
.reply input { flex: 1; min-width: 0; padding: 7px 10px; }
.reply button { padding: 7px 12px; flex-shrink: 0; }
.reply .key { font-family: var(--mono); min-width: 40px; }
.reply .muted { font-size: 13px; }

.card-actions { margin-left: auto; display: flex; flex-shrink: 0; }
.card-actions button { padding: 2px 8px; font-size: 16px; line-height: 1.2; }

.card-meta {
  display: flex;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  min-width: 0;
}
.card-cwd { font-family: var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: rtl; text-align: left; }
.git { display: flex; gap: 6px; flex-shrink: 0; max-width: 55%; font: 12px/1.6 var(--mono); }
.git-branch { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.git-dirty { color: var(--warn); }
.card-age { flex-shrink: 0; margin-left: auto; }
.ports { display: flex; gap: 6px; flex-wrap: wrap; }
.port {
  font: 12px/1.6 var(--mono);
  padding: 1px 9px;
  border-radius: 999px;
  border: 1px solid color-mix(in srgb, var(--accent) 60%, transparent);
  color: var(--accent);
  background: none;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}
.port:hover { background: color-mix(in srgb, var(--accent) 15%, transparent); }
.port.unready { border-style: dashed; border-color: var(--border); color: var(--muted); }
.pane-ports { display: flex; gap: 4px; flex-shrink: 0; }
.code {
  margin: 6px 0 10px;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font: 12px/1.5 var(--mono);
  white-space: pre;
  overflow-x: auto;
  tab-size: 4;
  user-select: all;
  -webkit-user-select: all;
}

.card-preview {
  margin: 0;
  padding: 8px 10px;
  background: var(--bg);
  border-radius: 8px;
  font: 11px/1.35 var(--mono);
  color: #adbac7;
  height: calc(10 * 1.35em + 16px);
  overflow: hidden;
  white-space: pre;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.empty { color: var(--muted); text-align: center; padding: 48px 0; }

/* ------------------------------------------------ terminal view */

/* Pinned to the visible part of the screen. When the iOS keyboard opens, the
   page is panned upward behind it, which would push a plain fixed element's
   top edge off screen; --vtop follows that pan (set in app.js). */
#term-view {
  position: fixed;
  inset: var(--vtop, 0px) 0 auto 0;
  height: var(--vh);
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.termbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
  padding-top: calc(6px + env(safe-area-inset-top)); /* clear the status bar / notch as an installed app */
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
/* Installed on a phone, the reported safe area ends right at the status bar,
   which still leaves the bar's controls looking clipped; match the list's gap. */
@media (display-mode: standalone) {
  .termbar { padding-top: calc(16px + env(safe-area-inset-top)); }
}
.termbar button { padding: 6px 10px; white-space: nowrap; }
.solo-slot { flex: 1; min-width: 0; }
/* A select is as wide as its longest option; pin it so a long session name
   in the list cannot crowd the bar. */
#split-add { width: 92px; flex-shrink: 0; padding: 6px 8px; font-size: 14px; color: var(--muted); }
/* Phones: every pixel of the top bar goes to the session name and git status. */
@media (max-width: 560px) {
  .wide-only { display: none; }
  #back-btn { font-size: 22px; line-height: 1; padding: 2px 10px; }
  #split-add { width: 74px; padding: 6px 4px; }
  .termbar { gap: 4px; }
  .solo-slot .pane-head select { max-width: 40%; }
  /* Renaming is still on the session card; here the room goes to the branch. */
  .solo-slot .pane-rename, .solo-slot .pane-transcript, .solo-slot .pane-ports { display: none; } /* transcript is in the key row */
}
#split-add:disabled { opacity: 0.4; }

.status { width: 10px; height: 10px; border-radius: 50%; background: var(--warn); flex-shrink: 0; margin: 0 4px; }
.status[data-state="open"] { background: var(--ok); }
.status[data-state="closed"] { background: var(--danger); }

/* Up to four terminals: 1 full, 2 side by side, 3 as one tall + two stacked, 4 as a 2x2 grid. */
.panes {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: 4px;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
}
.panes[data-count="2"] { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.panes[data-count="3"], .panes[data-count="4"] {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
}
.panes[data-count="3"] .pane:first-child { grid-row: span 2; }

@media (orientation: portrait) {
  .panes[data-count="2"] {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: repeat(2, minmax(0, 1fr));
  }
  .panes[data-count="3"] .pane:first-child { grid-row: auto; grid-column: span 2; }
}

.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.panes:not([data-count="1"]) { padding: 4px; }
.panes:not([data-count="1"]) .pane { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.panes:not([data-count="1"]) .pane.active { border-color: var(--accent); }

.pane-head { display: flex; align-items: center; gap: 4px; min-width: 0; }
.pane > .pane-head { padding: 3px 4px; border-bottom: 1px solid var(--border); background: var(--panel); }
/* The picker takes only the room its names need, leaving space for git status. */
.pane-head select { flex: 0 1 auto; width: auto; min-width: 0; max-width: min(46%, 240px); padding: 5px 8px; font-size: 14px; font-weight: 600; }
.pane-head button { padding: 4px 8px; flex-shrink: 0; }
.pane-git { flex: 1 1 0; min-width: 0; display: flex; padding-left: 4px; }
.pane-git .git { max-width: 100%; min-width: 0; flex-shrink: 1; }
.pane-git .git span { flex-shrink: 0; }
.pane-git .git .git-branch { flex-shrink: 1; min-width: 3em; } /* always show at least the start of the branch */
.solo-slot .pane-close { display: none; } /* "‹ Sessions" already closes the only pane */

.pane-body {
  flex: 1;
  min-height: 0;
  padding: 4px 0 0 6px;
  touch-action: none;
  overflow: hidden;
}
.pane-body .xterm { height: 100%; }
.pane-body .xterm-viewport { overflow-y: hidden !important; }

.compose {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 6px max(6px, env(safe-area-inset-right)) 6px max(6px, env(safe-area-inset-left));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.compose textarea { flex: 1; min-width: 0; max-height: 35vh; resize: none; padding: 9px 12px; line-height: 1.35; }
.compose button { flex-shrink: 0; padding: 9px 14px; }
.compose #compose-attach { font-size: 20px; padding: 6px 10px; line-height: 1; }
.compose.busy #compose-attach { opacity: 0.4; }
/* With no touch keyboard row, the compose bar is the bottom edge. */
@media (pointer: fine) { .compose { padding-bottom: max(6px, env(safe-area-inset-bottom)); } }
/* On touch screens the toggle lives in the key row instead of the top bar. */
@media (pointer: coarse) { #compose-btn { display: none; } }
#compose-btn[aria-pressed="true"] { color: var(--text); background: var(--panel); }
.pane.dropping { outline: 2px dashed var(--accent); outline-offset: -4px; }

.keybar {
  display: none;
  gap: 4px;
  padding: 6px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
  scrollbar-width: none;
}
.keybar::-webkit-scrollbar { display: none; }
.keybar button {
  flex: 1 0 auto;
  min-width: 42px;
  padding: 9px 8px;
  font: 13px var(--mono);
  user-select: none;
  -webkit-user-select: none;
}
.keybar button.armed { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

/* Show the helper keys only where there is no hardware keyboard. */
@media (pointer: coarse) {
  .keybar { display: flex; }
}

/* ------------------------------------------------ dialog + login */

dialog {
  width: min(480px, calc(100vw - 24px));
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
}
/* Full-screen reading view. */
dialog.fullscreen {
  width: 100vw;
  max-width: 100vw;
  height: 100dvh;
  max-height: 100dvh;
  margin: 0;
  border: 0;
  border-radius: 0;
  padding: 0;
  background: var(--bg);
}
dialog.fullscreen[open] { display: flex; flex-direction: column; }
.transcript-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 8px max(10px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  flex-shrink: 0;
}
.transcript-bar button { padding: 4px 10px; font-size: 18px; line-height: 1.2; flex-shrink: 0; }
#transcript-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.transcript-find { display: flex; align-items: center; gap: 6px; margin-left: auto; flex: 0 1 420px; min-width: 0; }
.transcript-find input { flex: 1; min-width: 0; padding: 6px 10px; }
#transcript-count { font-size: 13px; white-space: nowrap; min-width: 3.5em; text-align: right; }
#transcript-note { margin: 0; padding: 6px 12px; border-bottom: 1px solid var(--border); }
.transcript-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px max(12px, env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  font: 13px/1.4 var(--mono);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}
/* Lines off screen are not laid out until scrolled to; keeps 20,000 lines quick. */
.transcript-body > div { min-height: 1.4em; content-visibility: auto; contain-intrinsic-size: auto 1.4em; }
.transcript-body > div.hit { background: color-mix(in srgb, var(--warn) 14%, transparent); }
.transcript-body > div.current { background: color-mix(in srgb, var(--warn) 32%, transparent); }
::highlight(transcript-match) { background: var(--warn); color: #1b1300; }
/* Phones: search gets its own row. */
@media (max-width: 640px) {
  .transcript-bar { flex-wrap: wrap; }
  #transcript-title { flex: 1; }
  .transcript-find { flex: 1 0 100%; margin-left: 0; }
  .transcript-body { font-size: 12px; }
}

dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog h2 { margin: 0 0 8px; font-size: 18px; }
label { display: block; margin: 14px 0 6px; font-size: 13px; font-weight: 600; }
.chips { display: flex; gap: 6px; margin-top: 8px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips:empty { display: none; }
.chip {
  flex-shrink: 0;
  padding: 4px 10px;
  border-radius: 999px;
  font: 12px var(--mono);
  color: var(--muted);
}
.chip:hover { color: var(--text); }

.dir-list {
  list-style: none;
  margin: 8px 0 0;
  padding: 4px;
  height: 196px;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font: 13px var(--mono);
}
.dir-list li {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dir-list li:hover { background: var(--panel-hover); }
.dir-list li.up { color: var(--muted); }
.dir-list li.none { color: var(--muted); cursor: default; background: none; font-family: inherit; }

.dialog-sub { margin: 0; font-size: 13px; overflow-wrap: anywhere; }
.dialog-sub strong { color: var(--text); }
.hint { margin: 6px 0 0; font-size: 12px; }
.hint code { font-family: var(--mono); color: var(--text); }
button:disabled { opacity: 0.5; cursor: default; }

.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

dialog.wide {
  width: min(600px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow-y: auto;
}
.dialog-head { display: flex; align-items: center; justify-content: space-between; }
.dialog-head h2 { margin: 0; }
.dialog-head button { font-size: 20px; padding: 2px 10px; }
dialog section { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border); }
dialog h3 { margin: 0 0 4px; font-size: 15px; }
.small { font-size: 13px; margin: 4px 0 10px; }
.small strong { color: var(--text); overflow-wrap: anywhere; }
.small code, .rows code { font-family: var(--mono); color: var(--text); overflow-wrap: anywhere; }
a { color: var(--accent); }

.rows { list-style: none; margin: 0 0 10px; padding: 0; }
.rows li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  min-width: 0;
}
.rows li:last-child { border-bottom: 0; }
.rows .grow { flex: 1; min-width: 0; }
.rows .sub { display: block; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rows button { padding: 4px 10px; flex-shrink: 0; }
.rows.events { max-height: 190px; overflow-y: auto; }
.rows.events li { padding: 6px 0; }
.rows .bad { color: var(--danger); }

.inline-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.inline-form input { flex: 1 1 160px; width: auto; }
.inline-form button { flex-shrink: 0; }

.check { display: flex; gap: 10px; align-items: flex-start; font-weight: normal; font-size: 14px; margin: 6px 0 12px; }
.check input { width: 18px; height: 18px; margin: 2px 0 0; flex-shrink: 0; padding: 0; }

.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); margin-right: 4px; }
.pill.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 50%, transparent); }
.qr { display: block; background: #fff; border-radius: 8px; margin: 10px 0; }

.nudge {
  margin: 0 0 14px;
  padding: 10px 12px;
  border: 1px solid color-mix(in srgb, var(--warn) 50%, transparent);
  border-radius: 10px;
  font-size: 14px;
}
button.link { border: 0; background: none; padding: 0; color: var(--accent); text-decoration: underline; }
button.link:hover { background: none; }

.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; margin: 16px 0 0; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

.login-page {
  display: grid;
  place-items: center;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
}
.login-card {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
}
.login-card h1 { margin: 0 0 12px; font-size: 22px; }
.login-card button { width: 100%; margin-top: 16px; padding: 10px; }
