:root {
  --bg: #f6f3ef;
  --panel: #ffffff;
  --ink: #2b2622;
  --muted: #857a72;
  --line: #e6dfd8;
  --accent: #8c7b6f;
  --accent-ink: #ffffff;
  --accent-soft: #efe8e1;
  --out-bubble: #e9e0d6;
  --in-bubble: #ffffff;
  --danger: #c0392b;
  --danger-soft: #fdecea;
  --warn: #b26a00;
  --warn-soft: #fff4e0;
  --ok: #2e7d4f;
  --unread: #d9534f;
  --shadow: 0 1px 2px rgba(0,0,0,.06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1917; --panel: #24211e; --ink: #ede7e1; --muted: #a59a91; --line: #37322d;
    --accent: #b39f8f; --accent-ink: #1b1917; --accent-soft: #332d28;
    --out-bubble: #4a4038; --in-bubble: #2d2926;
    --danger-soft: #3d2220; --warn-soft: #3a2e1a; --shadow: none;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1917; --panel: #24211e; --ink: #ede7e1; --muted: #a59a91; --line: #37322d;
  --accent: #b39f8f; --accent-ink: #1b1917; --accent-soft: #332d28;
  --out-bubble: #4a4038; --in-bubble: #2d2926;
  --danger-soft: #3d2220; --warn-soft: #3a2e1a; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: var(--ink); background: var(--panel); }

#app, .view { height: 100%; }

/* ---------- 共用元件 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  padding: 10px 16px; min-height: 44px; font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.small { padding: 6px 12px; min-height: 36px; font-size: 14px; }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: transparent; font-size: 22px; min-width: 44px; min-height: 44px; }
.avatar { border-radius: 50%; object-fit: cover; background: var(--accent-soft); flex: none; }
.avatar.sm { width: 36px; height: 36px; }
.avatar.md { width: 44px; height: 44px; }
.avatar.lg { width: 72px; height: 72px; }
.err { color: var(--danger); min-height: 1.5em; margin: 8px 0 0; }
.ok { color: var(--ok); min-height: 1.5em; margin: 8px 0 0; font-size: 14px; }
.hint { color: var(--muted); font-size: 14px; margin: 0 0 12px; }
.empty, .placeholder { color: var(--muted); text-align: center; padding: 32px 16px; }

.bar {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px 6px 16px;
  padding-top: max(6px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line); min-height: 56px;
}
.bar-title { font-weight: 700; font-size: 18px; flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 50;
  max-width: calc(100% - 32px); font-size: 14px;
}

/* ---------- 登入 ---------- */
.login { display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel); border-radius: 16px; padding: 28px 24px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; border: 1px solid var(--line);
}
.login-logo { width: 64px; height: 64px; border-radius: 16px; align-self: center; }
.login-card h1 { font-size: 20px; text-align: center; margin: 0 0 4px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); }
label input, label select, label textarea {
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: 44px; font-size: 16px;
}

/* ---------- 主畫面：手機一欄、電腦兩欄 ---------- */
.main { display: flex; }
.pane { height: 100%; }
.pane.list { width: 100%; display: flex; flex-direction: column; background: var(--panel); }
.pane.chat { width: 100%; display: none; }
.main.chat-open .pane.list { display: none; }
.main.chat-open .pane.chat { display: flex; }
@media (min-width: 900px) {
  .pane.list { width: 360px; flex: none; border-right: 1px solid var(--line); display: flex !important; }
  .pane.chat { display: flex !important; flex: 1; }
  .icon-btn.back { display: none; }
}

.tabs { display: flex; gap: 4px; padding: 8px 12px 0; overflow-x: auto; }
.tabs button {
  border: 0; background: transparent; padding: 8px 12px; border-radius: 999px; white-space: nowrap;
  color: var(--muted); font-size: 15px; min-height: 36px;
}
.tabs button.active { background: var(--accent-soft); color: var(--ink); font-weight: 700; }
.search { padding: 8px 12px; }
.search input { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; font-size: 16px; }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv {
  display: flex; gap: 12px; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.conv.active { background: var(--accent-soft); }
.conv .meta { flex: 1; min-width: 0; }
.conv .row1 { display: flex; gap: 8px; align-items: baseline; }
.conv .nm { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.conv .tm { color: var(--muted); font-size: 12px; flex: none; }
.conv .row2 { display: flex; gap: 6px; align-items: center; margin-top: 2px; }
.conv .pv { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.badge { background: var(--unread); color: #fff; font-size: 12px; font-weight: 700; border-radius: 999px; padding: 0 7px; min-width: 20px; text-align: center; }
.chip { font-size: 12px; border-radius: 6px; padding: 1px 6px; background: var(--accent-soft); color: var(--muted); white-space: nowrap; }
.chip.medical { background: var(--warn-soft); color: var(--warn); }
.chip.urgent { background: var(--danger-soft); color: var(--danger); font-weight: 700; }
.chip.needs { background: var(--accent); color: var(--accent-ink); }

/* ---------- 對話 ---------- */
.chat-inner { display: flex; flex-direction: column; width: 100%; height: 100%; }
.chat-bar { padding-left: 4px; }
.chat-title { flex: 1; min-width: 0; }
.chat-title .name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title .sub { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.chat-title select { font-size: 13px; padding: 2px 4px; border-radius: 6px; border: 1px solid var(--line); max-width: 150px; }
.note-box { padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); display: flex; gap: 8px; }
.note-box textarea { flex: 1; border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; resize: vertical; }

.messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 78%; display: flex; flex-direction: column; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg.system {
  align-self: center; color: var(--muted); font-size: 12px; max-width: 90%;
  background: var(--accent-soft); border-radius: 999px; padding: 3px 12px; margin: 4px 0;
}
.bubble {
  padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; word-break: break-word;
  box-shadow: var(--shadow);
}
.msg.in .bubble { background: var(--in-bubble); border-top-left-radius: 4px; border: 1px solid var(--line); }
.msg.out .bubble { background: var(--out-bubble); border-top-right-radius: 4px; }
.msg.failed .bubble { outline: 2px solid var(--danger); }
.msg .info { font-size: 11px; color: var(--muted); margin: 2px 4px 0; }
.msg img.media, .msg video.media { max-width: 220px; max-height: 260px; border-radius: 12px; }
.msg img.sticker { width: 110px; height: auto; }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); background: var(--accent-soft); border-radius: 999px; padding: 2px 10px; margin: 8px 0; }

.composer {
  background: var(--panel); border-top: 1px solid var(--line);
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
}
.ai-panel { margin-bottom: 8px; }
.ai-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.ai-label { font-size: 13px; font-weight: 700; color: var(--accent); }
.spacer { flex: 1; }
.risk { font-size: 13px; border-radius: 8px; padding: 4px 10px; font-weight: 700; margin: 6px 0 2px; }
.ai-count { font-size: 12px; color: var(--muted); }
@media (min-width: 900px) { .ai-count { display: none; } }
.risk.medical { background: var(--warn-soft); color: var(--warn); }
.risk.urgent { background: var(--danger-soft); color: var(--danger); }
.ai-note { font-size: 13px; color: var(--warn); margin: 4px 0; }
.ai-status { font-size: 13px; color: var(--muted); }
.ai-list { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 2px; scroll-snap-type: x mandatory; }
.sugg {
  flex: 0 0 min(82%, 340px); scroll-snap-align: start; text-align: left; border: 1px solid var(--line);
  background: var(--bg); border-radius: 12px; padding: 8px 10px; font-size: 14px; white-space: pre-wrap;
  max-height: 132px; overflow-y: auto; line-height: 1.45;
}
.sugg:active { background: var(--accent-soft); }
.sugg .ph { background: var(--warn-soft); color: var(--warn); border-radius: 4px; padding: 0 2px; }
@media (min-width: 900px) {
  .ai-list { flex-direction: column; overflow-x: visible; }
  .sugg { flex: none; max-height: none; }
}
.input-row { display: flex; gap: 8px; align-items: flex-end; }
.input-row textarea {
  flex: 1; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; resize: none;
  font-size: 16px; max-height: 40vh; min-height: 44px;
}
.input-btns { display: flex; flex-direction: column; gap: 6px; }
.send-as { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ---------- 設定 ---------- */
.settings { display: flex; flex-direction: column; overflow-y: auto; }
.settings-body { padding: 16px; display: flex; flex-direction: column; gap: 16px; max-width: 760px; width: 100%; margin: 0 auto; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.card h2 { font-size: 17px; margin: 0; }
.card > .btn, .persona-row .btn { align-self: flex-start; }
.card h3 { font-size: 15px; margin: 8px 0 0; }
.persona-row { display: flex; align-items: center; gap: 16px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 560px) { .grid2 { grid-template-columns: 1fr; } }
table.users { width: 100%; border-collapse: collapse; font-size: 14px; display: block; overflow-x: auto; }
table.users th, table.users td { text-align: left; padding: 6px 6px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.users input[type=text] { width: 110px; border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; }
.status-box { white-space: pre-wrap; word-break: break-all; font-size: 12px; background: var(--bg); padding: 10px; border-radius: 8px; margin: 0; max-height: 360px; overflow: auto; }
.back-always { font-size: 28px; }
.icon-btn.back { font-size: 28px; }
