:root {
  --bg: #f6f3ec;
  --surface: #fffdf8;
  --sunken: #ede8dd;
  --ink: #1f1c16;
  --muted: #6f685b;
  --line: #ddd6c7;
  --accent: #c4612f;
  --accent-ink: #fff;
  --ok: #3d7a4f;
  --warn: #b3541e;
  --idle: #8a8270;
  --mono: ui-monospace, 'Cascadia Code', 'SF Mono', Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16140f;
    --surface: #201d17;
    --sunken: #2a261e;
    --ink: #ece6d9;
    --muted: #a39b8a;
    --line: #3a352b;
    --accent: #e07a45;
    --accent-ink: #1a120c;
    --ok: #74b585;
    --warn: #f0935a;
    --idle: #8f8774;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
}

button {
  font: inherit;
  color: inherit;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  min-height: 40px;
}
button:active { transform: translateY(1px); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.ghost { background: transparent; border-color: transparent; }
button:disabled { opacity: 0.5; cursor: default; }

/* ---- header ---- */

header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
header h1 { font-size: 17px; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
header .sub { color: var(--muted); font-size: 13px; font-weight: 400; }
header .back { padding: 6px 10px; min-height: 36px; }

.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--idle); flex: none; }
.conn.up { background: var(--ok); }

/* ---- Terminal list ---- */

.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }

.machine { padding: 16px 16px 6px; color: var(--muted); font-size: 12px; letter-spacing: 0.06em; text-transform: uppercase; }

.card {
  display: block;
  width: calc(100% - 32px);
  margin: 0 16px 10px;
  text-align: left;
  padding: 12px 14px;
  border-radius: 14px;
  background: var(--surface);
}
.card .top { display: flex; align-items: center; gap: 8px; }
.card .name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .preview { color: var(--muted); font-size: 13.5px; margin-top: 4px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .when { color: var(--muted); font-size: 12px; flex: none; }

.chip { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--sunken); color: var(--muted); flex: none; }
.chip.working { color: var(--ok); }
.chip.input { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.chip.ended { opacity: 0.7; }

.empty { color: var(--muted); padding: 40px 24px; text-align: center; }
.empty code { font-family: var(--mono); background: var(--sunken); padding: 2px 6px; border-radius: 6px; }

/* ---- chat ---- */

.chat { padding: 12px 16px 20px; display: flex; flex-direction: column; gap: 10px; }

.msg { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user {
  align-self: flex-end;
  max-width: 88%;
  background: var(--sunken);
  padding: 8px 12px;
  border-radius: 14px 14px 4px 14px;
}
.msg.assistant { max-width: 100%; }

.note { color: var(--muted); font-size: 13px; font-style: italic; text-align: center; }

.tool {
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--muted);
  overflow-wrap: anywhere;
}
.tool b { color: var(--ink); font-weight: 600; }

details.result { font-family: var(--mono); font-size: 12px; color: var(--muted); margin-top: -6px; }
details.result summary { cursor: pointer; list-style: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
details.result summary::before { content: '└ '; }
details.result.error summary { color: var(--warn); }
details.result pre { white-space: pre-wrap; overflow-wrap: anywhere; margin: 6px 0 0; padding: 8px; background: var(--sunken); border-radius: 8px; max-height: 50vh; overflow: auto; }

/* ---- bottom dock: Input request + composer ---- */

.dock {
  border-top: 1px solid var(--line);
  background: var(--bg);
  padding: 10px 12px max(10px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.request {
  background: var(--surface);
  border: 1.5px solid var(--accent);
  border-radius: 14px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.request .title { font-weight: 600; }
.request .what { font-family: var(--mono); font-size: 12.5px; background: var(--sunken); padding: 8px; border-radius: 8px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 30vh; overflow: auto; }
.request .q { color: var(--muted); }
.request .opts { display: flex; flex-direction: column; gap: 6px; }
.request .opts button { text-align: left; }
.request .opts button small { display: block; color: var(--muted); font-size: 12.5px; }
.row { display: flex; gap: 6px; flex-wrap: wrap; }
.row button { flex: 1; }
.keys button { flex: 1 0 auto; padding: 6px 10px; min-height: 36px; font-family: var(--mono); font-size: 13px; }

.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1;
  resize: none;
  font: inherit;
  font-size: 16px; /* no zoom-on-focus on iOS */
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 9px 12px;
  max-height: 40vh;
  min-height: 42px;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---- sign-in and settings ---- */

button.icon { min-height: 36px; padding: 4px 10px; font-size: 18px; color: var(--muted); }

.hint { color: var(--muted); font-size: 13.5px; margin-top: 8px; min-height: 1.2em; }

.auth { max-width: 420px; margin: 0 auto; padding: 40px 20px; display: flex; flex-direction: column; gap: 12px; }
.auth h2 { margin: 0; font-size: 22px; }
.auth p { margin: 0 0 8px; color: var(--muted); }

.field {
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.field:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.settings { padding: 8px 0 24px; }
.panel { margin: 12px 16px; padding: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; display: flex; flex-direction: column; gap: 10px; }
.panel h3 { margin: 0; font-size: 15px; }
.panel p { margin: 0; color: var(--muted); font-size: 14px; }
.list { display: flex; flex-direction: column; }
.item { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.item:first-child { border-top: 0; }
.item small { color: var(--muted); }
.grow { flex: 1; min-width: 0; }
.pairing { display: flex; flex-direction: column; gap: 8px; }
code.block { font-family: var(--mono); font-size: 13px; background: var(--sunken); padding: 10px; border-radius: 8px; overflow-wrap: anywhere; user-select: all; }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; }
