:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #e9ece6;
  --border: #d9ded5;
  --text: #171a17;
  --muted: #6f776e;
  --accent: #1e7355;
  --accent-strong: #155840;
  --danger: #b74343;
  --warn: #a66b16;
  --shadow: 0 12px 30px rgba(24, 35, 28, 0.08);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(circle at 90% 0%, rgba(30, 115, 85, 0.09), transparent 28%),
    var(--bg);
  color: var(--text);
  font-family: "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  overflow: hidden;
}
button, textarea, select { font: inherit; }
button { color: inherit; }
[hidden] { display: none !important; }

.topbar {
  height: calc(64px + env(safe-area-inset-top));
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(16px);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  background: var(--text);
  color: white;
  font: 700 17px/1 Georgia, serif;
}
.brand b { display: block; font-size: 15px; }
.brand-sub { margin-top: 2px; font-size: 11px; color: var(--muted); }
.status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: #9ca39b; }
.status.online { color: var(--accent); }
.status.online .status-dot {
  background: #2ea876;
  box-shadow: 0 0 0 4px rgba(46, 168, 118, 0.13);
}

main { height: calc(100% - 64px - env(safe-area-inset-top)); }
.view { height: 100%; overflow: hidden; }
#listView { overflow-y: auto; padding: 16px 14px 32px; }
.list-toolbar {
  display: flex;
  align-items: end;
  gap: 10px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.select-wrap { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 5px; font-size: 11px; color: var(--muted); }
.select-wrap select {
  width: 100%;
  height: 38px;
  padding: 0 34px 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.primary-btn, .send-btn {
  border: 0;
  border-radius: 9px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
}
.primary-btn { height: 38px; padding: 0 16px; }
.primary-btn:active, .send-btn:active { background: var(--accent-strong); }
.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 22px 2px 10px;
}
.section-heading h2 { margin: 0; font-size: 15px; }
.icon-btn {
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
}
.session-list { display: flex; flex-direction: column; gap: 8px; }
.session-row {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  padding: 13px 14px;
  text-align: left;
  cursor: pointer;
}
.session-row:active { background: var(--surface-2); }
.session-title {
  display: block;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.session-meta {
  margin-top: 5px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 11px;
  color: var(--muted);
}
.empty-state {
  padding: 70px 24px;
  text-align: center;
  color: var(--muted);
}
.empty-state b, .empty-state span { display: block; }
.empty-state b { color: var(--text); font-size: 15px; margin-bottom: 8px; }

.chat-view {
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.chat-head {
  min-height: 58px;
  padding: 9px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.chat-title { flex: 1; min-width: 0; }
.chat-title b {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 14px;
}
.chat-title span { display: block; margin-top: 2px; font-size: 11px; color: var(--muted); }
.stop-btn {
  height: 32px;
  padding: 0 12px;
  border: 1px solid rgba(183, 67, 67, 0.3);
  border-radius: 8px;
  background: rgba(183, 67, 67, 0.07);
  color: var(--danger);
  cursor: pointer;
}
.activity {
  margin: 8px 12px 0;
  padding: 8px 11px;
  border: 1px solid #dfd8c8;
  border-radius: 8px;
  background: #fffaf0;
  color: #765b29;
  font-size: 11.5px;
}
.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.message {
  max-width: 88%;
  padding: 10px 12px;
  border-radius: 11px;
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.message.user {
  align-self: flex-end;
  background: var(--accent);
  color: white;
  border-bottom-right-radius: 3px;
}
.message.assistant {
  align-self: flex-start;
  border: 1px solid var(--border);
  background: var(--surface);
  border-bottom-left-radius: 3px;
}
.message.system {
  align-self: center;
  max-width: 95%;
  background: #fff4dd;
  color: #765b29;
  font-size: 12px;
}
.message.busy::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 14px;
  margin-left: 3px;
  vertical-align: -2px;
  background: var(--accent);
  animation: blink 0.9s steps(2, jump-none) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.confirm-card {
  margin: 8px 12px;
  padding: 12px;
  border: 1px solid #e4c98b;
  border-radius: 10px;
  background: #fff8e8;
}
.confirm-card b { display: block; color: #76531a; font-size: 13px; }
.confirm-detail {
  margin: 7px 0 11px;
  color: #5d5038;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.confirm-actions { display: flex; gap: 8px; }
.confirm-actions button {
  flex: 1;
  height: 36px;
  border-radius: 8px;
  cursor: pointer;
}
.confirm-deny { border: 1px solid var(--border); background: var(--surface); }
.confirm-allow { border: 0; background: var(--accent); color: white; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 9px 10px calc(9px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.composer textarea {
  flex: 1;
  min-height: 40px;
  max-height: 130px;
  resize: none;
  padding: 10px 11px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.send-btn {
  width: 42px;
  height: 42px;
  flex: none;
  font-size: 21px;
}
.send-btn:disabled { opacity: 0.45; }
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(22px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 9px 14px;
  border-radius: 8px;
  background: rgba(23, 26, 23, 0.92);
  color: white;
  font-size: 12px;
  z-index: 20;
}

@media (min-width: 720px) {
  body { padding-inline: max(0px, calc((100vw - 720px) / 2)); }
  .topbar { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
  main { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}

.login-page {
  display: grid;
  place-items: center;
  padding: 24px;
  overflow-y: auto;
}
.login-shell { width: min(100%, 380px); }
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.login-brand h1 { margin: 0; font-size: 20px; }
.login-brand p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.login-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
.login-card input {
  height: 42px;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-card button {
  height: 43px;
  border: 0;
  border-radius: 9px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
}
.login-card button:disabled { opacity: 0.55; }
.login-error { color: var(--danger); font-size: 12px; }
.login-note { margin: 14px 2px 0; color: var(--muted); font-size: 11px; line-height: 1.5; }
