:root {
  --bg: #0b0d10;
  --surface: #14181d;
  --surface-2: #1c2128;
  --border: #232a32;
  --text: #e6e8eb;
  --text-dim: #99a0aa;
  --text-faint: #6b727b;
  --accent: #4dabf7;
  --accent-glow: rgba(77, 171, 247, 0.18);

  --green: #51cf66;
  --yellow: #ffd43b;
  --orange: #ff922b;
  --red: #ff6b6b;
  --purple: #cc5de8;
  --blue: #4dabf7;
  --gray: #6b727b;

  --radius: 10px;
  --radius-sm: 6px;
  --pad: 0.6rem;
  --gap: 0.4rem;

  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI',
    Roboto, sans-serif;
  /* iOS Safari zooms inputs whose computed font-size is < 16px on focus.
     Base size of 16px ensures all inputs/selects inherit a non-zooming size. */
  font-size: 16px;
  line-height: 1.45;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

/* Honour the [hidden] attribute even when an element's class sets a non-default
   display (e.g. .update-banner uses display: flex which would otherwise win
   against the user-agent rule for [hidden]). */
[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  /* Suppress iOS rubber-band so our pull-to-refresh handler can claim the gesture. */
  overscroll-behavior-y: contain;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
}

a {
  color: var(--accent);
}

code,
.mono {
  font-family: 'SF Mono', Menlo, ui-monospace, Consolas, monospace;
  font-size: 0.875em;
}

button {
  font-family: inherit;
  font-size: 0.95em;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 0.55rem 1rem;
  cursor: pointer;
  transition: background 120ms, border-color 120ms;
}
button:hover {
  background: var(--border);
}
button.primary {
  background: var(--accent);
  color: #0b0d10;
  border-color: var(--accent);
  font-weight: 600;
}
button.primary:hover {
  filter: brightness(1.05);
}
button.ghost {
  background: transparent;
}
button.danger {
  color: var(--red);
  border-color: var(--red);
}

input,
select,
textarea {
  font-family: inherit;
  /* Must be ≥16px to prevent iOS Safari zoom-on-focus. */
  font-size: 16px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.75rem;
}
input:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.muted {
  color: var(--text-dim);
}
.small {
  font-size: 0.85em;
}
.dim {
  color: var(--text-faint);
}
.error-line {
  color: var(--red);
  margin-top: 0.5rem;
}

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: rgba(11, 13, 16, 0.86);
  backdrop-filter: saturate(140%) blur(10px);
  -webkit-backdrop-filter: saturate(140%) blur(10px);
  border-bottom: 1px solid var(--border);
  padding: env(safe-area-inset-top) var(--pad) 0.6rem;
  padding-top: max(env(safe-area-inset-top), 0.6rem);
  display: flex;
  align-items: center;
  gap: 1rem;
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.brand-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--gray);
  box-shadow: 0 0 0 0 currentColor;
  transition: background 200ms;
}
.brand-dot[data-state='live'] {
  background: var(--green);
  box-shadow: 0 0 8px rgba(81, 207, 102, 0.6);
}
.brand-dot[data-state='connecting'] {
  background: var(--yellow);
  animation: pulse 1.2s ease-in-out infinite;
}
.brand-dot[data-state='offline'] {
  background: var(--red);
}
.route-nav {
  display: flex;
  gap: 0.4rem;
}
.route-nav .route-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  padding: 0.4rem 0.85rem;
  font-size: 0.9em;
  border-radius: 999px;
}
.route-nav .route-btn[data-active='true'] {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--text);
}
.topbar-meta {
  margin-left: auto;
  display: flex;
  gap: 0.4rem;
}

.meta-chip {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.8em;
  color: var(--text-dim);
  white-space: nowrap;
}
.meta-chip.dim {
  background: transparent;
  border-color: transparent;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

/* ---------- main ---------- */

main {
  max-width: 880px;
  margin: 0 auto;
  /* Bottom padding accounts for the fixed footer (~22px after trim) + safe-area inset */
  padding: 0.75rem var(--pad)
    calc(1.6rem + env(safe-area-inset-bottom));
}

@media (min-width: 640px) {
  :root { --pad: 1rem; --gap: 0.5rem; }
}

main h1 {
  font-size: 1.4rem;
  margin: 0.5rem 0 1rem;
}

main h2 {
  font-size: 1.05rem;
  margin: 1.5rem 0 0.5rem;
  color: var(--text-dim);
  font-weight: 500;
}

.empty {
  padding: 2rem 0.5rem;
  text-align: center;
}

/* ---------- token gate ---------- */

.token-form {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
  max-width: 32rem;
  margin-inline: auto;
}
.token-form input {
  flex: 1 1 auto;
  font-family: 'SF Mono', Menlo, ui-monospace, monospace;
}

/* ---------- filters ---------- */

.filters {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}
.filters select,
.filters input[type='search'] {
  flex: 1 1 8rem;
  min-width: 0;
}

/* ---------- sessions list (2-column grid) ---------- */

.sessions-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--gap);
}

.session-card {
  /* Default surface — overridden per-state below */
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.65rem 0.7rem;
  display: grid;
  grid-template-rows: auto auto;
  gap: 0.2rem;
  cursor: pointer;
  transition: border-color 120ms, transform 120ms, background 120ms;
  min-width: 0;
}
.session-card:hover {
  border-color: var(--accent);
}
.session-card:active {
  transform: scale(0.995);
}

/* State-tinted cards — same palette as the pill, applied to the whole block. */
.session-card[data-state='working'],
.session-card[data-state='background'] {
  background: rgba(77, 171, 247, 0.10);
  border-color: rgba(77, 171, 247, 0.32);
}
.session-card[data-state='needs_input'] {
  background: rgba(255, 146, 43, 0.13);
  border-color: rgba(255, 146, 43, 0.45);
}
.session-card[data-state='hung'] {
  background: rgba(255, 107, 107, 0.13);
  border-color: rgba(255, 107, 107, 0.45);
}
.session-card[data-state='error'] {
  background: rgba(255, 107, 107, 0.13);
  border-color: rgba(255, 107, 107, 0.45);
}
.session-card[data-state='resumed'],
.session-card[data-state='ready'] {
  background: rgba(81, 207, 102, 0.13);
  border-color: rgba(81, 207, 102, 0.45);
}
.session-card[data-state='idle'] {
  background: var(--surface);
  border-color: var(--border);
  opacity: 0.85;
}
.session-card[data-state='ended'] {
  background: transparent;
  border-color: var(--border);
  opacity: 0.6;
}
.session-card .top {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}
.session-card .title {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1 1 auto;
  min-width: 0;
  font-size: 0.95em;
}
.session-card .top .state-pill {
  flex-shrink: 0;
}
.session-card .sub {
  font-size: 0.8em;
  color: var(--text-dim);
  display: flex;
  gap: 0.4rem;
  align-items: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.session-card .sub-dot {
  display: inline-block;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.5;
  flex-shrink: 0;
}
.session-card .machine,
.machine-icon {
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
  color: var(--text-dim);
}
.session-card .project-chip {
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.session-card .project-row {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.82em;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  margin-top: 0.15rem;
}
.session-card .project-name {
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.machine-icon {
  width: 16px;
  height: 16px;
}
.machine-icon svg {
  width: 100%;
  height: 100%;
}
.session-card .ago {
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

/* ---------- state pill ---------- */

.state-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
  font-size: 0.78em;
  font-weight: 600;
  text-transform: lowercase;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  white-space: nowrap;
}
.state-pill::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}
.state-pill[data-state='working'],
.state-pill[data-state='background'] {
  color: var(--blue);
  border-color: rgba(77, 171, 247, 0.3);
  background: rgba(77, 171, 247, 0.08);
}
.state-pill[data-state='blocked'] {
  color: #ff6b6b;
  border-color: rgba(255, 107, 107, 0.55);
  background: rgba(255, 107, 107, 0.14);
  animation: blocked-pulse 1.6s ease-in-out infinite;
}
@keyframes blocked-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 107, 107, 0.35); }
  50% { box-shadow: 0 0 0 4px rgba(255, 107, 107, 0); }
}
.session-card[data-state='blocked'] {
  border-left: 3px solid #ff6b6b;
}
.chat-blocked-note {
  margin: 8px 12px;
  padding: 10px 12px;
  border: 1px solid rgba(255, 107, 107, 0.5);
  background: rgba(255, 107, 107, 0.08);
  border-radius: 10px;
  font-size: 13px;
}
.chat-blocked-note .blocked-head { font-weight: 600; margin-bottom: 4px; }
.chat-blocked-note .blocked-sub { color: var(--text-faint); font-size: 12px; margin-bottom: 6px; }
.chat-blocked-note .blocked-snippet {
  margin: 6px 0;
  padding: 8px;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 6px;
  font-size: 11.5px;
  line-height: 1.35;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 160px;
  overflow: auto;
}
.chat-blocked-note .blocked-result { margin-top: 6px; font-size: 12px; color: var(--text-faint); }
.state-pill[data-state='needs_input'] {
  color: var(--orange);
  border-color: rgba(255, 146, 43, 0.4);
  background: rgba(255, 146, 43, 0.1);
}
.state-pill[data-state='ready'] {
  color: #6dd17a;
  border-color: rgba(81, 207, 102, 0.45);
  background: rgba(81, 207, 102, 0.12);
}
.state-pill[data-state='hung'] {
  color: var(--red);
  border-color: rgba(255, 107, 107, 0.4);
  background: rgba(255, 107, 107, 0.1);
}
.state-pill[data-state='idle'] {
  color: var(--text-faint);
}
.state-pill[data-state='ended'] {
  color: var(--text-faint);
  background: transparent;
}
.state-pill[data-state='error'] {
  color: var(--red);
}

/* ---------- session detail ---------- */

.back {
  background: transparent;
  border: none;
  color: var(--accent);
  padding: 0.25rem 0;
  cursor: pointer;
  font-size: 0.95em;
}
.back:hover {
  color: #74c0fc;
  background: transparent;
}

.session-header {
  margin: 0.5rem 0 1.5rem;
}
.session-header h1 {
  margin: 0 0 0.4rem;
}
.meta-row {
  display: flex;
  gap: 0.4rem;
  align-items: center;
  flex-wrap: wrap;
  margin: 0;
}
#session-id-line {
  margin: 0.3rem 0 0;
  word-break: break-all;
}
.events-title {
  margin-top: 0.5rem;
}

/* ---------- latest summary panel ---------- */

.latest-summary {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin: 0.25rem 0 1rem;
}
.latest-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.5rem;
  align-items: baseline;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.75rem;
  font-size: 0.95em;
  line-height: 1.4;
}
.latest-row .latest-label {
  font-size: 0.78em;
  font-weight: 600;
  color: var(--text-dim);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}
.latest-row .latest-text {
  color: var(--text);
  word-break: break-word;
  white-space: pre-wrap;
}
.latest-row .latest-text.muted {
  color: var(--text-dim);
}
.latest-now {
  border-color: rgba(255, 146, 43, 0.45);
  background: rgba(255, 146, 43, 0.08);
}
.latest-now .latest-label {
  color: var(--orange);
}

/* ---------- events list ---------- */

.events-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.event-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.55rem 0.75rem;
  font-size: 0.92em;
}
.event-card .head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85em;
  color: var(--text-dim);
}
.event-card .glyph {
  flex-shrink: 0;
  width: 1.1em;
  text-align: center;
}
.event-card .kind {
  font-weight: 500;
  color: var(--text-dim);
}
.event-card .stamp {
  margin-left: auto;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}
.event-card .body {
  margin-top: 0.2rem;
  color: var(--text);
  word-break: break-word;
  white-space: pre-wrap;
  line-height: 1.4;
}
.event-card .body.mono {
  font-size: 0.88em;
}
.event-card .body.muted {
  color: var(--text-dim);
}
.event-card.user-msg {
  border-left: 3px solid var(--accent);
}
.event-card.assistant-msg {
  border-left: 3px solid var(--green);
}
.event-card.tool-event .body {
  font-family: 'SF Mono', Menlo, ui-monospace, Consolas, monospace;
  font-size: 0.86em;
}
.event-card.input-needed {
  border-color: rgba(255, 146, 43, 0.45);
  background: rgba(255, 146, 43, 0.06);
}
.event-card.system {
  background: transparent;
  border-style: dashed;
  border-color: var(--border);
  font-size: 0.85em;
  padding: 0.3rem 0.6rem;
}
.event-card.system .body {
  margin-top: 0;
  color: var(--text-faint);
}
.event-card.recent {
  animation: flash 1.6s ease-out;
}
@keyframes flash {
  0% {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
  }
  100% {
    border-color: var(--border);
    box-shadow: 0 0 0 0 transparent;
  }
}

/* ---------- settings ---------- */

.setting-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  margin-bottom: 1rem;
}
.setting-block h3 {
  margin: 0 0 0.4rem;
  font-size: 1rem;
}
.setting-block label {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-right: 0.75rem;
  margin-top: 0.5rem;
  font-size: 0.9em;
  color: var(--text-dim);
}
.setting-block input[type='time'] {
  padding: 0.35rem 0.5rem;
}

/* ---------- pull-to-refresh + version + ws state ---------- */

.pull-indicator {
  position: fixed;
  top: 0;
  left: 50%;
  transform: translate(-50%, -100%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 999px 999px;
  padding: 0.4rem 1rem;
  font-size: 0.85em;
  color: var(--text-dim);
  z-index: 20;
  pointer-events: none;
  transition: transform 200ms ease-out;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.pull-indicator[data-active='true'] {
  transform: translate(-50%, 0);
}
.pull-spinner {
  width: 12px;
  height: 12px;
  border: 2px solid var(--text-faint);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.ws-label {
  font-size: 0.78em;
  color: var(--text-dim);
  font-weight: 500;
}

.app-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 4;
  background: rgba(11, 13, 16, 0.92);
  backdrop-filter: saturate(140%) blur(8px);
  -webkit-backdrop-filter: saturate(140%) blur(8px);
  border-top: 1px solid var(--border);
  /* Tightened from 0.45rem top + 0.45rem bottom (~14px chrome) to ~2px chrome. */
  padding: 0.15rem var(--pad);
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  justify-content: space-between;
  font-size: 0.78em;
  color: var(--text-faint);
  gap: 0.5rem;
  align-items: center;
}

/* Morph: short cross-fade when the text content swaps. */
.morph-out {
  opacity: 0;
  transition: opacity 140ms ease-out;
}
#version-tap, #footer-ws-state, .ws-label {
  transition: opacity 140ms ease-out;
}
.version-tap {
  cursor: pointer;
  user-select: none;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  border: 1px solid transparent;
  transition: border-color 120ms;
}
.version-tap:hover,
.version-tap:active {
  border-color: var(--border);
  color: var(--text-dim);
}
.version-tap[data-stale='true'] {
  color: var(--orange);
  border-color: var(--orange);
}

.update-banner {
  background: var(--surface-2);
  border: 1px solid var(--orange);
  color: var(--orange);
  border-radius: var(--radius-sm);
  padding: 0.55rem 0.85rem;
  margin-bottom: 0.75rem;
  font-size: 0.9em;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

.update-banner-action {
  margin-left: auto;
  text-decoration: underline;
}

/* ---------- responsive ---------- */

@media (max-width: 640px) {
  .topbar {
    flex-wrap: wrap;
    gap: 0.4rem;
  }
  .topbar-meta {
    margin-left: 0;
  }
  /* On phones, the colored card carries the state — drop the redundant pill
     so the title gets the full row width. Pill returns at ≥640px. */
  .session-card .top .state-pill {
    display: none;
  }
  .session-card {
    padding: 0.55rem 0.6rem;
  }
  .session-card .title {
    font-size: 0.92em;
  }
  .session-card .sub {
    font-size: 0.78em;
  }
}

/* ---------- chat (session detail = chat surface) ---------- */

/* When a session detail is open, the page becomes a fixed-height chat:
   header on top, scrollable bubble list in the middle, sticky input at
   bottom. No page scroll. */
body[data-sub='session-detail'] {
  overflow: hidden;
  overscroll-behavior: none;
}
body[data-sub='session-detail'] .pull-indicator,
body[data-sub='session-detail'] .app-footer {
  display: none;
}
body[data-sub='session-detail'] #session-view {
  position: fixed;
  top: var(--chat-top, 3rem);
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  padding: 0;
  background: var(--bg, #0b0d10);
  z-index: 1;
}
.session-detail-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem var(--pad);
  border-bottom: 1px solid var(--card-border, rgba(255,255,255,0.06));
  flex: 0 0 auto;
}
.session-detail-bar .back {
  background: transparent;
  border: 0;
  color: inherit;
  font-size: 1.4rem;
  padding: 0.25rem 0.5rem;
  flex: 0 0 auto;
}
.session-detail-meta {
  flex: 1 1 auto;
  min-width: 0;
}
.session-detail-meta h1 {
  font-size: 1.05rem;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session-detail-meta .meta-row {
  margin: 0.15rem 0 0;
  display: flex;
  gap: 0.35rem;
  align-items: center;
  flex-wrap: wrap;
  font-size: 0.78em;
}
.session-take-over,
.session-end {
  flex: 0 0 auto;
  padding: 0.4rem 0.7rem;
  font-size: 0.85em;
  border-radius: 8px;
  white-space: nowrap;
}
#session-view #latest-summary {
  margin: 0.5rem var(--pad) 0;
}

#events-list.chat-bubbles {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 0.6rem var(--pad) 0.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin: 0;
  list-style: none;
}

.bubble {
  max-width: 86%;
  padding: 0.55rem 0.75rem;
  border-radius: 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 0.95em;
  line-height: 1.35;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.06);
}
.bubble.user {
  align-self: flex-end;
  background: rgba(58, 132, 255, 0.18);
  border-color: rgba(58, 132, 255, 0.45);
  color: #d6e6ff;
}
.bubble.assistant {
  align-self: flex-start;
}
.bubble.tool {
  align-self: flex-start;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 0.82em;
  background: rgba(255,255,255,0.03);
  border-style: dashed;
  color: var(--muted, #9aa3ad);
  max-width: 92%;
  padding: 0.18rem 0.5rem;
  line-height: 1.3;
}
/* Consecutive tool bubbles merge visually — share a single padded column
   instead of leaving an air gap between every Bash/Read line. */
.bubble.tool + .bubble.tool {
  margin-top: -0.35rem;
  border-top-style: none;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}
.bubble.tool:has(+ .bubble.tool) {
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

/* Collapsible tool calls — <details>/<summary> with a custom chevron.
   Collapsed: single line "Bash · git checkout main…" with ellipsis.
   Expanded: full JSON-formatted tool input below in a scrollable pre. */
.bubble.tool .tool-call {
  margin: 0;
}
.bubble.tool .tool-call > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  white-space: nowrap;
  overflow: hidden;
  padding: 0;
  margin: 0;
  min-height: 0;
}
.bubble.tool .tool-call > summary::-webkit-details-marker { display: none; }
.bubble.tool .tool-chevron {
  display: inline-block;
  flex: 0 0 auto;
  transition: transform 120ms ease-out;
  color: rgba(255,255,255,0.4);
}
.bubble.tool .tool-call[open] > summary > .tool-chevron {
  transform: rotate(90deg);
}
.bubble.tool .tool-summary {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.bubble.tool .tool-call-body {
  margin: 0.4rem 0 0;
  padding: 0.4rem 0.55rem;
  background: rgba(0, 0, 0, 0.32);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 4px;
  font-size: 0.95em;
  line-height: 1.4;
  color: #d6d6d6;
  max-height: 18rem;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  -webkit-overflow-scrolling: touch;
}
.bubble.tool-post {
  opacity: 0.6;
}
.bubble.system {
  align-self: center;
  background: rgba(255, 146, 43, 0.12);
  border-color: rgba(255, 146, 43, 0.4);
  color: #ffd6a8;
  font-size: 0.85em;
}
.bubble.optimistic {
  opacity: 0.78;
}

/* Process-noise bubbles (tool_pre, tool_post, tool-only assistant turns)
   are kept in the DOM for state-finding logic but hidden from view. */
.bubble.hidden-tool { display: none; }

/* "Claude is thinking" indicator — three bouncing dots, shown whenever
   the session state is `working`. Hides as soon as state flips to
   needs_input / idle / hung / ended / error. */
.bubble.typing {
  align-self: flex-start;
  padding: 0.55rem 0.85rem;
  background: rgba(255,255,255,0.04);
  border: 1px solid rgba(255,255,255,0.08);
  display: inline-flex;
  align-items: center;
  width: auto;
  max-width: max-content;
}
.bubble.typing .dots {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}
.bubble.typing .dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted, #9aa3ad);
  display: inline-block;
  animation: typing-bounce 1.2s ease-in-out infinite;
}
.bubble.typing .dots span:nth-child(2) { animation-delay: 0.15s; }
.bubble.typing .dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing-bounce {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30%          { opacity: 1;    transform: translateY(-4px); }
}

/* Pulse the orange "Now" pill while a tool is running so the existing
   surface also shows life. */
.latest-now .latest-text {
  animation: now-pulse 1.6s ease-in-out infinite;
}
@keyframes now-pulse {
  0%, 100% { opacity: 0.7; }
  50%      { opacity: 1; }
}
.bubble .role {
  font-size: 0.7em;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  opacity: 0.65;
  margin-bottom: 0.15rem;
}
/* Markdown rendering inside assistant bubbles. Headings stay restrained so
   they don't dwarf the chat — bold weight + slightly larger font, not the
   browser-default huge h1/h2 sizes. */
.bubble .md p {
  margin: 0.4em 0;
}
.bubble .md p:first-child { margin-top: 0; }
.bubble .md p:last-child { margin-bottom: 0; }
.bubble .md h1,
.bubble .md h2,
.bubble .md h3,
.bubble .md h4,
.bubble .md h5,
.bubble .md h6 {
  margin: 0.6em 0 0.25em;
  font-weight: 700;
  font-size: 1em;
  color: #f0f4f8;
}
.bubble .md h1 { font-size: 1.1em; }
.bubble .md h2 { font-size: 1.05em; }
.bubble .md h1:first-child,
.bubble .md h2:first-child,
.bubble .md h3:first-child { margin-top: 0; }
.bubble .md ul,
.bubble .md ol {
  margin: 0.35em 0;
  padding-left: 1.2em;
}
.bubble .md li {
  margin: 0.15em 0;
}
.bubble .md code {
  background: rgba(255, 255, 255, 0.08);
  padding: 0.05em 0.3em;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
}
.bubble .md pre {
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 6px;
  padding: 0.5em 0.7em;
  overflow-x: auto;
  margin: 0.4em 0;
  font-size: 0.82em;
  line-height: 1.35;
  -webkit-overflow-scrolling: touch;
}
.bubble .md pre code {
  background: transparent;
  padding: 0;
  font-size: inherit;
}
.bubble .md a {
  color: #6aa8ff;
  text-decoration: underline;
}
.bubble .md strong {
  font-weight: 700;
  color: #f0f4f8;
}
.bubble .md blockquote {
  margin: 0.4em 0;
  padding-left: 0.7em;
  border-left: 2px solid rgba(255, 255, 255, 0.18);
  color: var(--muted, #9aa3ad);
}
.bubble .md hr {
  border: 0;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  margin: 0.7em 0;
}
.bubble .md table {
  border-collapse: collapse;
  margin: 0.4em 0;
  font-size: 0.88em;
}
.bubble .md th,
.bubble .md td {
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 0.25em 0.5em;
}

/* ---------- task panel (TodoWrite) ---------- */

.task-panel {
  flex: 0 0 auto;
  margin: 0.4rem var(--pad) 0;
  border: 1px solid var(--card-border, rgba(255,255,255,0.08));
  border-radius: 10px;
  background: var(--card-bg, #14171c);
  overflow: hidden;
}
.task-panel[data-expanded='true'] {
  border-color: rgba(58, 132, 255, 0.45);
}
.task-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.55rem 0.7rem;
  background: transparent;
  color: inherit;
  border: 0;
  font-size: 0.92em;
  text-align: left;
  cursor: pointer;
}
.task-icon-summary {
  font-size: 1em;
  flex: 0 0 auto;
}
.task-counter {
  flex: 1 1 auto;
  color: #d6e6ff;
  font-weight: 500;
}
.task-chevron {
  flex: 0 0 auto;
  color: var(--muted, #9aa3ad);
  font-size: 0.9em;
}
.task-list {
  list-style: none;
  margin: 0;
  padding: 0.2rem 0.7rem 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}
.task-item {
  display: flex;
  gap: 0.55rem;
  align-items: flex-start;
  padding: 0.3rem 0;
  font-size: 0.9em;
  line-height: 1.35;
}
.task-item .task-icon {
  flex: 0 0 1.2rem;
  text-align: center;
  font-weight: 700;
}
.task-item .task-text {
  flex: 1 1 auto;
  min-width: 0;
  word-wrap: break-word;
}
.task-item.task-completed {
  color: var(--muted, #9aa3ad);
  text-decoration: line-through;
}
.task-item.task-completed .task-icon {
  color: #6dd17a;
}
.task-item.task-in_progress {
  color: #f0f4f8;
}
.task-item.task-in_progress .task-icon {
  color: #6aa8ff;
  animation: task-pulse 1.4s ease-in-out infinite;
}
.task-item.task-pending {
  color: #cdd6df;
}
.task-item.task-pending .task-icon {
  color: var(--muted, #9aa3ad);
}
@keyframes task-pulse {
  0%, 100% { opacity: 0.5; }
  50%      { opacity: 1; }
}

.auq-panel {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.65rem var(--pad);
  padding-bottom: calc(0.65rem + env(safe-area-inset-bottom));
  background: rgba(58, 132, 255, 0.08);
  border-top: 1px solid rgba(58, 132, 255, 0.45);
  max-height: 60vh;
  overflow-y: auto;
}
.auq-question + .auq-question {
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  padding-top: 0.6rem;
}
.auq-header {
  font-weight: 600;
  font-size: 0.95em;
  color: #d6e6ff;
  margin-bottom: 0.15rem;
}
.auq-text {
  font-size: 0.88em;
  color: var(--muted, #9aa3ad);
  margin-bottom: 0.4rem;
  line-height: 1.35;
}
.auq-options {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.auq-option {
  display: flex;
  gap: 0.6rem;
  align-items: flex-start;
  text-align: left;
  padding: 0.6rem 0.7rem;
  background: var(--card-bg, #14171c);
  color: inherit;
  border: 1px solid rgba(58, 132, 255, 0.45);
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.95em;
  line-height: 1.3;
}
.auq-option:active,
.auq-option.selected {
  background: rgba(58, 132, 255, 0.28);
  border-color: rgba(58, 132, 255, 0.85);
}

/* Brief flash to call attention when a fresh AUQ replaces a prior one
   (claude posed a follow-up question after answering the first). */
@keyframes auq-flash {
  0%   { background: rgba(58, 132, 255, 0.32); }
  100% { background: rgba(58, 132, 255, 0.08); }
}
.auq-panel.auq-fresh {
  animation: auq-flash 1.4s ease-out;
}

/* "Sent X — waiting for claude…" placeholder while we bridge the gap
   between an AUQ tap and the next claude event. */
.auq-sending {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.2rem;
  color: var(--muted, #9aa3ad);
  font-size: 0.92em;
}
.auq-sending strong {
  color: #d6e6ff;
}
.auq-sending .dots {
  display: inline-flex;
  gap: 5px;
}
.auq-sending .dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #6aa8ff;
  display: inline-block;
  animation: typing-bounce 1.2s ease-in-out infinite;
}
.auq-sending .dots span:nth-child(2) { animation-delay: 0.15s; }
.auq-sending .dots span:nth-child(3) { animation-delay: 0.3s; }
.auq-submit {
  margin-top: 0.4rem;
  width: 100%;
  padding: 0.55rem;
  border-radius: 8px;
}
.auq-option-num {
  color: #6aa8ff;
  font-weight: 700;
  flex: 0 0 auto;
  width: 1.5rem;
}
.auq-option-body {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}
.auq-option-label {
  font-weight: 600;
  color: #f0f4f8;
}
.auq-option-desc {
  font-size: 0.82em;
  color: var(--muted, #9aa3ad);
}

.permission-bar {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding: 0.55rem var(--pad);
  background: rgba(255, 146, 43, 0.12);
  border-top: 1px solid rgba(255, 146, 43, 0.45);
}
.permission-hint {
  font-size: 0.85em;
  color: #ffd6a8;
}
.permission-tool {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.permission-tool-name {
  font-size: 0.92em;
  color: #ffd6a8;
  line-height: 1.35;
}
.permission-tool-name strong {
  color: white;
}
.permission-tool-arg {
  margin: 0;
  padding: 0.4rem 0.55rem;
  background: rgba(0, 0, 0, 0.32);
  border: 1px solid rgba(255, 146, 43, 0.35);
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.78em;
  line-height: 1.4;
  color: #f0e5d3;
  max-height: 8rem;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
  -webkit-overflow-scrolling: touch;
}
.permission-actions {
  display: flex;
  gap: 0.4rem;
}
.permission-actions button {
  flex: 1 1 auto;
  padding: 0.45rem 0.6rem;
  font-size: 0.95em;
  border-radius: 8px;
}

.transient-banner {
  position: fixed;
  top: env(safe-area-inset-top, 0);
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 1rem);
  padding: 0.45rem 0.85rem;
  background: rgba(15, 18, 22, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 0 0 10px 10px;
  font-size: 0.85em;
  color: #cdd6df;
  z-index: 200;
  backdrop-filter: blur(10px);
}
.transient-banner[hidden] { display: none !important; }

.chat-stuck-note {
  flex: 0 0 auto;
  padding: 0.55rem var(--pad);
  background: rgba(255, 70, 70, 0.12);
  border-top: 1px solid rgba(255, 70, 70, 0.45);
  color: #ffd0d0;
  font-size: 0.88em;
  line-height: 1.35;
}
.chat-stuck-note strong {
  color: white;
}
.chat-stuck-note .force-restart-btn {
  margin-top: 0.5rem;
  width: 100%;
  padding: 0.55rem 0.85rem;
  border-radius: 8px;
  font-size: 0.95em;
}

.chat-startup-note {
  flex: 0 0 auto;
  padding: 0.6rem var(--pad);
  padding-bottom: calc(0.6rem + env(safe-area-inset-bottom));
  background: var(--bg, #0b0d10);
  border-top: 1px solid var(--card-border, rgba(255,255,255,0.06));
  text-align: center;
  color: var(--muted, #9aa3ad);
  font-size: 0.9em;
}

.chat-input-form {
  flex: 0 0 auto;
  display: flex;
  gap: 0.4rem;
  align-items: flex-end;
  padding: 0.5rem var(--pad);
  padding-bottom: calc(0.5rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--card-border, rgba(255,255,255,0.06));
  background: var(--bg, #0b0d10);
}
.chat-input {
  flex: 1 1 auto;
  background: var(--card-bg, #14171c);
  color: inherit;
  border: 1px solid var(--card-border, rgba(255,255,255,0.08));
  border-radius: 14px;
  padding: 0.55rem 0.75rem;
  font-size: 16px;
  font-family: inherit;
  resize: none;
  line-height: 1.3;
  max-height: 160px;
  overflow-y: auto;
}
.chat-send {
  flex: 0 0 auto;
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  font-size: 1.1rem;
  padding: 0;
}

/* ---------- spawn modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  padding: 0.75rem;
  padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
}
@media (min-width: 640px) {
  .modal-backdrop {
    align-items: center;
  }
}
.modal-backdrop[hidden] {
  display: none !important;
}
.modal {
  background: var(--card-bg, #14171c);
  border: 1px solid var(--card-border, rgba(255,255,255,0.08));
  border-radius: 14px;
  padding: 1rem;
  width: 100%;
  max-width: 26rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.modal h2 {
  font-size: 1rem;
  margin: 0;
}
.modal-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.modal-row > span {
  flex: 0 0 4rem;
  font-size: 0.85em;
  color: var(--muted, #9aa3ad);
}
.modal-row > select,
.modal-row > .modal-prompt {
  flex: 1 1 auto;
  background: var(--bg, #0b0d10);
  color: inherit;
  border: 1px solid var(--card-border, rgba(255,255,255,0.08));
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
  font-size: 16px;
  font-family: inherit;
}
.modal-row-stack {
  flex-direction: column;
  align-items: stretch;
}
.modal-row-stack > span {
  flex: 0 0 auto;
  margin-bottom: 0.2rem;
}
.modal-prompt {
  resize: none;
  line-height: 1.35;
  height: 4.6rem;
  /* iOS Safari renders the caret outside an empty rows-only textarea —
     anchor it with -webkit-appearance + an explicit caret-color so it
     stays inside the field. */
  -webkit-appearance: none;
  appearance: none;
  caret-color: var(--accent, #3a84ff);
  display: block;
  width: 100%;
  box-sizing: border-box;
}

.quick-prompts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.45rem;
}
.quick-prompt {
  flex: 0 0 auto;
  background: rgba(58, 132, 255, 0.16);
  border: 1px solid rgba(58, 132, 255, 0.45);
  color: #d6e6ff;
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  font-size: 0.82em;
  cursor: pointer;
  white-space: nowrap;
}
.quick-prompt:active {
  background: rgba(58, 132, 255, 0.32);
}
.modal-status {
  margin: 0;
  min-height: 1.2em;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.3rem;
}

/* ---------- slide-to-confirm ---------- */

.end-modal h2 {
  color: #ff8a8a;
}

.slide-confirm {
  position: relative;
  height: 3.4rem;
  margin-top: 0.4rem;
  user-select: none;
  touch-action: none;
}
.slide-track {
  position: absolute;
  inset: 0;
  background: rgba(255, 70, 70, 0.12);
  border: 1px solid rgba(255, 70, 70, 0.45);
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 200, 200, 0.85);
  font-size: 0.95em;
  letter-spacing: 0.04em;
  overflow: hidden;
}
.slide-label {
  pointer-events: none;
}
.slide-handle {
  position: absolute;
  top: 0.25rem;
  left: 0.25rem;
  width: 2.9rem;
  height: 2.9rem;
  border-radius: 50%;
  background: #ff5252;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 700;
  box-shadow: 0 2px 6px rgba(255, 70, 70, 0.45);
  transition: left 200ms ease-out;
  cursor: grab;
}
.slide-handle.dragging {
  transition: none;
  cursor: grabbing;
}
.slide-confirm.armed .slide-track {
  background: rgba(255, 70, 70, 0.32);
  border-color: rgba(255, 70, 70, 0.85);
  color: white;
}
.slide-confirm.confirmed .slide-handle {
  background: #ff3b3b;
}

.new-session-btn {
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  font-size: 0.78em;
  font-weight: 600;
  white-space: nowrap;
  line-height: 1.2;
}
/* Only show in the header on the sessions list, not on session detail
   (where the chat input is the primary action) or settings. */
body:not([data-route='sessions']) .new-session-btn,
body[data-sub='session-detail'] .new-session-btn {
  display: none;
}

/* Reused by spawn modal — orange "authorize device" hint. */
.chat-cf-hint {
  padding: 0.55rem 0.7rem;
  background: rgba(255, 146, 43, 0.18);
  border: 1px solid rgba(255, 146, 43, 0.55);
  border-radius: 8px;
  font-size: 0.88em;
  color: #ffd6a8;
  line-height: 1.4;
}
.chat-cf-hint a {
  color: #ffb86b;
  font-weight: 600;
  text-decoration: underline;
}
