:root {
  color-scheme: light dark;
  --bg: #faf9f5;
  --bg-elev: #ffffff;
  --bg-soft: #f1efe8;
  --text: #1f1e1d;
  --muted: #6f6d66;
  --line: #e5e2d8;
  --accent: #c96442;
  --accent-ink: #ffffff;
  --user-bubble: #eceae1;
  --danger: #b3261e;
  --code-bg: #f4f2ec;
  --shadow: 0 10px 30px rgba(0,0,0,.10);
  --radius: 18px;
  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624;
    --bg-elev: #2e2d2b;
    --bg-soft: #1f1e1d;
    --text: #f0eee6;
    --muted: #a6a39a;
    --line: #3b3a36;
    --accent: #d97757;
    --accent-ink: #1f1e1d;
    --user-bubble: #3a3936;
    --danger: #ff8a80;
    --code-bg: #1f1e1d;
    --shadow: 0 10px 30px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
  overscroll-behavior: none;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* ---------- login ---------- */
#login {
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 28px 22px;
  box-shadow: var(--shadow);
  text-align: center;
}
.mark { font-size: 34px; color: var(--accent); line-height: 1; }
.mark.small { font-size: 18px; }
.login-card h1 { font-size: 22px; margin: 14px 0 4px; }
.login-card p { margin: 0 0 18px; }
.login-card input {
  width: 100%;
  padding: 13px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.err { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 8px; }

button { font-family: inherit; font-size: 15px; }
.primary {
  margin-top: 12px;
  width: 100%;
  padding: 13px 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
.primary:active { opacity: .85; }
.ghost {
  padding: 13px 16px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
  font-weight: 600;
}
.ghost.danger { color: var(--danger); }

/* ---------- app shell ---------- */
#app { background: var(--bg); }

.topbar {
  position: relative;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 4px;
  height: 56px;
  padding: 0 8px;
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(14px);
}
.topbar .title {
  flex: 1;
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 6px;
}
.icon-btn {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 0;
  background: transparent;
  color: var(--text);
  border-radius: 12px;
}
.icon-btn:active { background: var(--bg-soft); }

/* drawer */
#drawer {
  position: fixed;
  z-index: 20;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(84vw, 340px);
  background: var(--bg-soft);
  border-right: 1px solid var(--line);
  transform: translateX(-102%);
  transition: transform .22s ease;
  display: flex;
  flex-direction: column;
  padding: calc(12px + env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
}
#drawer.open { transform: translateX(0); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 12px; }
.brand { font-size: 18px; font-weight: 650; display: flex; align-items: center; gap: 8px; }
.new-chat {
  margin: 4px 6px 14px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px dashed var(--line);
  background: transparent;
  color: var(--text);
  text-align: left;
  font-weight: 600;
}
.drawer-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .06em; padding: 0 10px 6px; }
.session-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.session-item {
  padding: 11px 12px;
  border-radius: 12px;
  margin-bottom: 2px;
  cursor: pointer;
}
.session-item:active { background: var(--bg-elev); }
.session-item.active { background: var(--bg-elev); }
.session-item .st { font-size: 14.5px; font-weight: 550; }
.session-item .sd { font-size: 12px; color: var(--muted); margin-top: 2px; }
.drawer-foot { display: flex; align-items: center; gap: 8px; padding: 10px 10px 2px; color: var(--muted); font-size: 12px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #c9a227; }
.dot.on { background: #2e9e5b; }
.dot.off { background: #b3261e; }

#scrim {
  position: fixed;
  inset: 0;
  z-index: 15;
  background: rgba(0,0,0,.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease;
}
#scrim.show { opacity: 1; pointer-events: auto; }

/* ---------- transcript ---------- */
.transcript {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(150px + env(safe-area-inset-bottom));
  scroll-behavior: smooth;
}
.transcript > * { max-width: 760px; margin-left: auto; margin-right: auto; }

.msg { margin: 0 0 18px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble);
  padding: 10px 14px;
  border-radius: 18px 18px 6px 18px;
  max-width: 82%;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.assistant .body { word-break: break-word; }
.msg.assistant .body p { margin: 0 0 10px; }
.msg.assistant .body .h { font-weight: 650; margin: 14px 0 6px; }
.msg.assistant code {
  font-family: var(--mono);
  font-size: 13.5px;
  background: var(--code-bg);
  padding: 1px 5px;
  border-radius: 5px;
}
.msg.assistant pre {
  background: var(--code-bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  overflow-x: auto;
  margin: 8px 0 12px;
}
.msg.assistant pre code { background: transparent; padding: 0; font-size: 12.8px; line-height: 1.5; }
.msg.assistant a { color: var(--accent); }
.sys { text-align: center; color: var(--muted); font-size: 12.5px; margin: 6px auto 14px; }
.sys.err { color: var(--danger); }

/* thinking */
.thinking { margin: 0 0 12px; }
.thinking summary {
  list-style: none;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 0;
}
.thinking summary::-webkit-details-marker { display: none; }
.thinking .t-body {
  color: var(--muted);
  font-size: 13.5px;
  border-left: 2px solid var(--line);
  padding: 4px 0 4px 12px;
  margin: 2px 0 6px;
  white-space: pre-wrap;
}

/* tool cards */
.tool {
  border: 1px solid var(--line);
  background: var(--bg-elev);
  border-radius: 14px;
  margin: 0 0 12px;
  overflow: hidden;
}
.tool-head {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 11px 13px;
  cursor: pointer;
  user-select: none;
}
.tool-ico {
  width: 24px; height: 24px;
  display: grid; place-items: center;
  border-radius: 7px;
  background: var(--bg-soft);
  color: var(--muted);
  font-size: 13px;
  flex: 0 0 auto;
}
.tool-name { font-weight: 600; font-size: 14px; }
.tool-sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.tool-state { font-size: 11px; color: var(--muted); flex: 0 0 auto; }
.tool-state.err { color: var(--danger); }
.tool-body { border-top: 1px solid var(--line); padding: 10px 13px 12px; }
.tool-body pre {
  margin: 0 0 8px;
  background: var(--code-bg);
  border-radius: 9px;
  padding: 10px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 260px;
}
.tool-body .lbl { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 4px 0 4px; }

/* ---------- permission sheet ---------- */
.sheet {
  position: fixed;
  z-index: 30;
  left: 8px; right: 8px;
  bottom: calc(84px + env(safe-area-inset-bottom));
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 10px 14px 14px;
  max-width: 640px;
  margin: 0 auto;
  animation: rise .18s ease;
}
@keyframes rise { from { transform: translateY(14px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-grab { width: 36px; height: 4px; border-radius: 3px; background: var(--line); margin: 0 auto 10px; }
.sheet-head { display: flex; gap: 10px; align-items: flex-start; }
.sheet-title { font-weight: 650; font-size: 15px; }
.perm-input {
  margin: 10px 0 12px;
  background: var(--code-bg);
  border-radius: 10px;
  padding: 10px;
  font-family: var(--mono);
  font-size: 12px;
  max-height: 180px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.sheet-actions { display: flex; gap: 8px; }
.sheet-actions button { flex: 1; padding: 12px 10px; }
.sheet-actions .primary { margin-top: 0; }

/* ---------- composer ---------- */
.composer {
  position: fixed;
  z-index: 10;
  left: 0; right: 0; bottom: 0;
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 72%, transparent);
}
.composer-row {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 6px 6px 6px 14px;
  box-shadow: 0 2px 10px rgba(0,0,0,.05);
}
#input {
  flex: 1;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 16px;
  line-height: 1.45;
  max-height: 38vh;
  padding: 9px 0;
}
#input::placeholder { color: var(--muted); }
.round {
  width: 38px; height: 38px;
  flex: 0 0 auto;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
}
.round.send { background: var(--accent); color: var(--accent-ink); }
.round.send:disabled { opacity: .4; }
.round.stop { background: var(--bg-soft); color: var(--text); }
.thinking-pill {
  max-width: 760px;
  margin: 0 auto 6px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--muted);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 12px;
  margin-left: max(10px, calc((100% - 760px) / 2));
}
.pulse {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: .35; transform: scale(.85); } 50% { opacity: 1; transform: scale(1); } }

.toast {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  font-size: 13.5px;
  padding: 9px 15px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  max-width: 88vw;
  text-align: center;
}

@media (min-width: 720px) {
  #drawer { width: 320px; }
  .transcript { padding-left: 24px; padding-right: 24px; }
}
