:root {
  color-scheme: light dark;
  --bg: #faf9f7;
  --bg-soft: #f2efe9;
  --card: #ffffff;
  --fg: #1f1e1c;
  --muted: #6b6862;
  --line: #e6e2db;
  --accent: #c96442;
  --accent-soft: #f6e7e1;
  --ok: #3fa45b;
  --warn: #d08a2c;
  --err: #c0392b;
  --radius: 16px;
  --shadow: 0 8px 30px rgba(31, 30, 28, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b19;
    --bg-soft: #232220;
    --card: #242320;
    --fg: #eceae5;
    --muted: #9b978f;
    --line: #35322d;
    --accent: #d97757;
    --accent-soft: #3a2a24;
    --ok: #5fbf78;
    --warn: #e0a24a;
    --err: #e0715f;
    --shadow: 0 10px 34px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.55 ui-sans-serif, system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.tiny { font-size: 12px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- login */

.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 50% -10%, var(--accent-soft), transparent 60%),
    var(--bg);
}
.login-card {
  width: min(400px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 28px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-card input {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
}
.login-card button {
  padding: 12px 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.error { color: var(--err); min-height: 1.2em; margin: 0; font-size: 13px; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: -0.01em; }
.logo {
  width: 26px; height: 26px; border-radius: 8px; display: inline-block;
  background:
    radial-gradient(circle at 35% 35%, #f3b28f, transparent 60%),
    linear-gradient(135deg, var(--accent), #a84a2e);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.25);
}

/* ---------------------------------------------------------------- shell */

.app { display: grid; grid-template-columns: 300px 1fr; height: 100dvh; }

.sidebar {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--bg-soft);
  border-right: 1px solid var(--line);
  padding: 14px;
  gap: 12px;
}
.side-head { display: flex; align-items: center; justify-content: space-between; }
.device-row { display: flex; align-items: center; gap: 8px; }
.device-row select {
  flex: 1; min-width: 0;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card);
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: 0 0 auto; }
.dot.on { background: var(--ok); box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 18%, transparent); }

.new-btn {
  padding: 10px 12px; border-radius: 11px;
  border: 1px dashed var(--line); background: transparent; color: var(--fg);
  text-align: left;
}
.new-btn:hover { background: var(--card); }

.sessions { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 4px; margin: 2px -4px; padding: 0 4px; }
.session-item {
  display: flex; flex-direction: column; gap: 2px;
  width: 100%; text-align: left;
  padding: 9px 10px; border-radius: 10px; border: 1px solid transparent; background: transparent;
}
.session-item:hover { background: var(--card); }
.session-item.active { background: var(--card); border-color: var(--line); }
.session-item .t { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-item .m { font-size: 12px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.session-item .m .sp { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-item .st { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); flex: 0 0 auto; }
.session-item .st.running { background: var(--warn); animation: pulse 1.4s infinite; }
.session-item .st.error { background: var(--err); }
@keyframes pulse { 50% { opacity: .35; } }

.side-foot { display: flex; align-items: center; justify-content: space-between; padding-top: 8px; border-top: 1px solid var(--line); }
.link-btn { border: 0; background: transparent; color: var(--muted); padding: 4px; }
.link-btn:hover { color: var(--fg); }

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

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(12px);
}
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid transparent;
  background: transparent; font-size: 17px;
}
.icon-btn:hover { background: var(--bg-soft); }
.title-wrap { flex: 1; min-width: 0; }
.session-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.conn.on { background: var(--ok); }
.conn.warn { background: var(--warn); }

.chat {
  flex: 1; overflow: auto; min-height: 0;
  padding: 22px 18px 8px;
  display: flex; flex-direction: column; gap: 18px;
  scroll-behavior: smooth;
}
.chat > * { width: min(780px, 100%); margin-inline: auto; }
.empty {
  margin: auto; text-align: center; color: var(--muted); padding: 40px 16px;
}
.empty h2 { font: 600 22px/1.3 ui-serif, Georgia, "Songti SC", serif; color: var(--fg); margin: 0 0 6px; }

.msg { display: flex; flex-direction: column; gap: 6px; }
.msg .who { font-size: 12px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.msg .who .logo { width: 18px; height: 18px; border-radius: 6px; }
.msg.user { align-items: flex-end; }
.msg.user .bubble {
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--accent) 22%, var(--line));
  padding: 10px 14px; border-radius: 16px 16px 4px 16px;
  max-width: min(640px, 92%);
  white-space: pre-wrap; word-wrap: break-word;
}
.msg.assistant .text {
  white-space: pre-wrap; word-wrap: break-word;
  padding: 2px 2px 0;
}
.msg .text.streaming::after {
  content: "▍"; color: var(--accent); animation: blink 1s steps(1) infinite; margin-left: 2px;
}
@keyframes blink { 50% { opacity: 0; } }

.tool {
  border: 1px solid var(--line); background: var(--card);
  border-radius: 12px; overflow: hidden;
}
.tool summary {
  list-style: none; cursor: pointer;
  display: flex; align-items: center; gap: 9px;
  padding: 9px 12px; font-size: 13px;
}
.tool summary::-webkit-details-marker { display: none; }
.tool .tool-ico {
  width: 22px; height: 22px; border-radius: 6px; display: grid; place-items: center;
  background: var(--bg-soft); font-size: 12px; color: var(--muted); flex: 0 0 auto;
}
.tool .tool-name { font-weight: 600; }
.tool .tool-sum { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.tool .tool-state { font-size: 11px; color: var(--muted); }
.tool .tool-state.err { color: var(--err); }
.tool .tool-body { border-top: 1px solid var(--line); padding: 10px 12px; background: var(--bg); }
.tool pre {
  margin: 0; white-space: pre-wrap; word-wrap: break-word;
  font: 12.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  max-height: 320px; overflow: auto;
}
.tool .out { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line); color: var(--muted); }
.tool .out.err { color: var(--err); }

.notice {
  font-size: 13px; color: var(--muted); border: 1px dashed var(--line);
  border-radius: 10px; padding: 8px 12px; background: var(--bg-soft);
}
.notice.err { color: var(--err); border-color: color-mix(in srgb, var(--err) 30%, var(--line)); }
.foot { font-size: 11px; color: var(--muted); padding: 0 2px; }

/* ------------------------------------------------------------- approval */

.approval-wrap { width: min(780px, 100%); margin: 0 auto; padding: 0 18px; }
.approval {
  border: 1px solid color-mix(in srgb, var(--warn) 45%, var(--line));
  background: color-mix(in srgb, var(--warn) 8%, var(--card));
  border-radius: 14px; padding: 12px 14px; margin-bottom: 10px;
  box-shadow: var(--shadow);
}
.approval h3 { margin: 0 0 4px; font-size: 14px; }
.approval .desc { color: var(--muted); font-size: 13px; margin: 0 0 8px; }
.approval pre {
  margin: 0 0 10px; padding: 8px 10px; background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px;
  font: 12.5px/1.5 ui-monospace, Menlo, Consolas, monospace;
  max-height: 200px; overflow: auto; white-space: pre-wrap; word-wrap: break-word;
}
.approval .row { display: flex; gap: 8px; flex-wrap: wrap; }
.approval button {
  padding: 8px 12px; border-radius: 9px; border: 1px solid var(--line); background: var(--card);
}
.approval button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.approval button.danger { color: var(--err); }
.approval button:disabled { opacity: .55; cursor: default; }

/* ------------------------------------------------------------- composer */

.composer {
  width: min(780px, 100%); margin: 0 auto; padding: 8px 18px calc(14px + env(safe-area-inset-bottom));
  display: flex; gap: 10px; align-items: flex-end;
}
.composer textarea {
  flex: 1; min-height: 46px; max-height: 200px; resize: none;
  padding: 12px 14px; border-radius: 16px; border: 1px solid var(--line);
  background: var(--card); box-shadow: var(--shadow);
  outline: none;
}
.composer textarea:focus { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.composer-actions { display: flex; gap: 6px; }
.send, .stop {
  height: 46px; padding: 0 18px; border-radius: 14px; border: 0;
  background: var(--accent); color: #fff; font-weight: 600;
}
.stop { background: var(--bg-soft); color: var(--fg); border: 1px solid var(--line); }
.send:disabled { opacity: .45; cursor: default; }

.banner {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: var(--fg); color: var(--bg); font-size: 13px;
  padding: 9px 14px; border-radius: 999px; box-shadow: var(--shadow); z-index: 20;
}

/* ---------------------------------------------------------------- dialog */

.dialog {
  border: 1px solid var(--line); border-radius: 18px; padding: 0;
  background: var(--card); color: var(--fg); width: min(420px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(0,0,0,.35); }
.dialog form { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.dialog h2 { margin: 0 0 4px; font-size: 18px; }
.dialog label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.dialog input, .dialog select {
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--fg);
}
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.dialog-actions button { padding: 9px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); }
.dialog-actions button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------------------------------------------------------------- mobile */

.scrim { display: none; }
.only-mobile { display: none; }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 30;
    width: min(320px, 86vw); transform: translateX(-102%);
    transition: transform .22s ease; padding-top: calc(14px + env(safe-area-inset-top));
    box-shadow: var(--shadow);
  }
  .app.side-open .sidebar { transform: none; }
  .app.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.28); z-index: 25; }
  .only-mobile { display: inline-flex; align-items: center; justify-content: center; }
  .chat { padding: 16px 14px 6px; }
  .approval-wrap { padding: 0 14px; }
  .composer { padding: 8px 14px calc(12px + env(safe-area-inset-bottom)); }
  .msg.user .bubble { max-width: 88%; }
}
