:root {
  color-scheme: dark;
  --bg: #14110f;
  --surface: #1e1a17;
  --surface-2: #272220;
  --line: #38312d;
  --text: #ece7e2;
  --muted: #9c918a;
  --accent: #d97757;
  --accent-dim: #7a3f2c;
  --ok: #6fae72;
  --err: #dc6a5a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  overscroll-behavior: none;
}

.screen { height: 100%; }
[hidden] { display: none !important; }

/* ---- cards (login / picker) ---- */
.screen:not(.app) {
  display: grid;
  place-items: center;
  padding: 24px;
}

.card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card h1 { margin: 0 0 4px; font-size: 20px; }

label { font-size: 12px; color: var(--muted); margin-top: 6px; }

input, select, textarea, button {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 11px 12px;
}

textarea { resize: none; }

button {
  cursor: pointer;
  background: var(--accent);
  border-color: var(--accent);
  color: #1a1310;
  font-weight: 600;
}

button:hover { filter: brightness(1.08); }
button.ghost { background: var(--surface-2); border-color: var(--line); color: var(--text); font-weight: 500; }
button.link { background: none; border: none; color: var(--muted); font-weight: 500; text-decoration: underline; }
button.icon { background: none; border: none; color: var(--text); font-size: 20px; padding: 4px 10px; }

.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--err); font-size: 13px; }

#existing { display: flex; flex-direction: column; gap: 6px; }

.session-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 10px 12px;
  cursor: pointer;
}

.session-row small { color: var(--muted); display: block; }

/* ---- app shell ---- */
.app {
  display: grid;
  grid-template-rows: auto 1fr auto auto auto;
  height: 100dvh;
}

header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.head-text { flex: 1; min-width: 0; }
.head-text strong { display: block; font-size: 14px; }
.head-text span { display: block; font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge { font-size: 11px; color: var(--muted); }
.mode { padding: 6px 8px; font-size: 12px; }

main {
  overflow-y: auto;
  padding: 14px 12px 4px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

.msg { max-width: 92%; padding: 10px 13px; border-radius: 13px; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: var(--accent-dim); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.assistant.live { border-color: var(--accent-dim); }
.msg.thinking { align-self: flex-start; background: none; border: 1px dashed var(--line); color: var(--muted); font-size: 13px; font-style: italic; }
.msg.error { align-self: center; background: #3a1f1c; border: 1px solid var(--err); color: #ffd8d1; font-size: 13px; }
.msg.note { align-self: center; color: var(--muted); font-size: 12px; }

.tool { align-self: stretch; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.tool summary { cursor: pointer; padding: 9px 12px; font-size: 13px; list-style: none; display: flex; gap: 8px; align-items: center; }
.tool summary::-webkit-details-marker { display: none; }
.tool .tname { color: var(--accent); font-weight: 600; }
.tool .tsum { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.tool pre { margin: 0; padding: 10px 12px; border-top: 1px solid var(--line); background: #17130f; font: 12px/1.5 ui-monospace, Consolas, monospace; overflow-x: auto; white-space: pre-wrap; word-break: break-word; }
.tool.err { border-color: var(--err); }

/* ---- permission prompts ---- */
#permissions { display: flex; flex-direction: column; gap: 8px; padding: 0 12px; }

.perm {
  background: var(--surface-2);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 12px;
}

.perm h3 { margin: 0 0 4px; font-size: 14px; }
.perm pre { margin: 6px 0 10px; padding: 8px 10px; background: #17130f; border-radius: 7px; font: 12px/1.5 ui-monospace, Consolas, monospace; max-height: 30vh; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.perm .row { display: flex; gap: 8px; flex-wrap: wrap; }
.perm .row button { flex: 1; min-width: 90px; padding: 10px; }
.perm .deny { background: var(--surface); border-color: var(--line); color: var(--text); }

footer {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

footer textarea { flex: 1; max-height: 40vh; }
footer button { padding: 11px 16px; }
#stop { background: var(--surface-2); border-color: var(--err); color: var(--err); }

.status {
  padding: 0 12px max(8px, env(safe-area-inset-bottom));
  font-size: 11px;
  color: var(--muted);
  background: var(--surface);
  min-height: 18px;
}

/* ---- project management ---- */
#manage {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0 12px;
  margin-top: 6px;
}

#manage summary {
  cursor: pointer;
  padding: 11px 0;
  font-size: 13px;
  color: var(--muted);
  list-style: none;
}

#manage summary::-webkit-details-marker { display: none; }
#manage summary::before { content: '▸ '; }
#manage[open] summary::before { content: '▾ '; }
#manage[open] { padding-bottom: 14px; }
#manage > *:not(summary) { width: 100%; }
#manage select, #manage input { margin-top: 2px; }
#manage #add-project { margin-top: 12px; }
#registered { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }
