:root {
  --bg: #f2f4f6;
  --surface: #ffffff;
  --text: #17202a;
  --muted: #66707c;
  --line: #e2e6ea;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #e3f2ef;
  --bubble-in: #ffffff;
  --bubble-ai: #eceffc;
  --bubble-human: #0f766e;
  --bubble-human-ink: #ffffff;
  --human: #9a4a05;
  --human-bg: #fdefd6;
  --ai: #3d43b8;
  --ai-bg: #e5e7fb;
  --danger: #c0392b;
  --free: #0b7a3e;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 16px rgba(16, 24, 40, 0.08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1114;
    --surface: #171b1f;
    --text: #e8ebee;
    --muted: #98a2ad;
    --line: #262c32;
    --primary: #2bb5a6;
    --primary-ink: #04201d;
    --primary-soft: #133632;
    --bubble-in: #1f2429;
    --bubble-ai: #1f2542;
    --bubble-human: #1b6d64;
    --bubble-human-ink: #eafaf7;
    --human: #f2b263;
    --human-bg: #3b2a12;
    --ai: #a9afff;
    --ai-bg: #242a4d;
    --danger: #ff7b6b;
    --free: #5fd39a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { height: 100%; }
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; max-width: 760px; margin: 0 auto; background: var(--bg); }
@media (min-width: 761px) { #app { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
[hidden] { display: none !important; }
.splash { margin: auto; color: var(--muted); font-weight: 600; letter-spacing: 0.1em; }
.center { flex: 1; display: grid; place-items: center; padding: 24px 16px; }
.muted { color: var(--muted); }
.error { color: var(--danger); margin: 0; min-height: 1.5em; }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

/* 按鈕 */
.btn {
  appearance: none; border: 1px solid transparent; border-radius: 12px; padding: 8px 14px;
  font-weight: 600; cursor: pointer; background: var(--surface); color: var(--text); text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; touch-action: manipulation; white-space: nowrap;
}
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.big { width: 100%; min-height: 52px; font-size: 17px; border-radius: 14px; }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.small { min-height: 34px; padding: 4px 10px; font-size: 14px; }
.btn.icon { width: 40px; padding: 0; font-size: 30px; line-height: 1; border: 0; background: transparent; color: var(--text); }
.btn:focus-visible, a:focus-visible, textarea:focus-visible, input:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
#notif.on { color: var(--free); border-color: var(--free); }

/* 上方標題列 */
.bar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.bar-title { flex: 1; min-width: 0; }
.bar h1 { margin: 0; font-size: 18px; line-height: 1.3; }
.sub { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* 對話列表 */
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.convs { list-style: none; margin: 0; padding: 0; }
.conv {
  display: flex; gap: 12px; align-items: center; padding: 12px 14px; color: inherit; text-decoration: none;
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.conv:active { background: var(--primary-soft); }
.conv-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.conv-top, .conv-bottom { display: flex; align-items: center; gap: 6px; min-width: 0; }
.conv .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv time { margin-left: auto; font-size: 12px; color: var(--muted); flex: none; }
.conv .preview { flex: 1; min-width: 0; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv.unread .preview { color: var(--text); font-weight: 600; }
.badge {
  flex: none; min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px; background: var(--danger); color: #fff;
  font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center;
}
.pill { flex: none; font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 999px; line-height: 18px; }
.pill.human { color: var(--human); background: var(--human-bg); }
.pill.ai { color: var(--ai); background: var(--ai-bg); }
.pill.pending { color: var(--muted); background: var(--line); }
.avatar { flex: none; width: 46px; height: 46px; border-radius: 50%; object-fit: cover; background: var(--line); }
.avatar.initial { display: inline-flex; align-items: center; justify-content: center; color: #fff; font-weight: 700; font-size: 18px; }
.avatar.small { width: 36px; height: 36px; font-size: 15px; }
.actions { padding: 10px 14px calc(env(safe-area-inset-bottom) + 10px); background: var(--surface); border-top: 1px solid var(--line); }

/* 聊天 */
.chat-bar { gap: 4px; padding-left: 4px; }
.who { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; background: none; border: 0; padding: 4px; text-align: left; cursor: pointer; }
.who-text { min-width: 0; display: flex; flex-direction: column; }
.who .name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn.mode { flex: none; min-height: 38px; }
.btn.mode.to-human { background: var(--human-bg); color: var(--human); }
.btn.mode.to-ai { background: var(--ai-bg); color: var(--ai); }
.msgs { flex: 1; overflow-y: auto; padding: 12px 12px 8px; display: flex; flex-direction: column; gap: 6px; -webkit-overflow-scrolling: touch; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); padding: 1px 10px; border-radius: 999px; margin: 8px 0 4px; }
.sys { align-self: center; font-size: 12px; color: var(--muted); }
.more { align-self: center; }
.msg { max-width: 82%; display: flex; flex-direction: column; gap: 2px; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg .text { padding: 9px 12px; border-radius: 18px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.in .text { background: var(--bubble-in); border: 1px solid var(--line); border-top-left-radius: 6px; }
.msg.out.human .text { background: var(--bubble-human); color: var(--bubble-human-ink); border-top-right-radius: 6px; }
.msg.out.ai .text { background: var(--bubble-ai); border-top-right-radius: 6px; }
.msg .text.muted { color: var(--muted); }
.msg a.text { display: block; text-decoration: none; }
.photo { display: block; max-width: min(240px, 68vw); max-height: 320px; border-radius: 14px; border: 1px solid var(--line); background: var(--line); }
.sticker { display: block; width: 110px; height: auto; }
.meta { display: flex; gap: 6px; align-items: center; font-size: 11px; color: var(--muted); padding: 0 4px; }
.meta .by { font-weight: 600; }
.st.free { color: var(--free); font-weight: 600; }
.st.failed { border: 0; background: none; color: var(--danger); font-weight: 700; font-size: 11px; padding: 0; cursor: pointer; text-decoration: underline; }
.hint { font-size: 12px; color: var(--muted); padding: 5px 14px; background: var(--surface); border-top: 1px solid var(--line); }
.hint.free { color: var(--free); font-weight: 600; }
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 6px 10px calc(env(safe-area-inset-bottom) + 8px); background: var(--surface); }
.composer textarea {
  flex: 1; resize: none; max-height: 160px; padding: 10px 14px; border-radius: 20px;
  border: 1px solid var(--line); background: var(--bg); line-height: 1.4; font-size: 16px;
}
.btn.send { min-height: 44px; border-radius: 22px; padding: 0 18px; }

/* 下方滑出的資料卡 */
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); opacity: 0; transition: opacity 0.2s; z-index: 20; }
.backdrop.show { opacity: 1; }
.sheet {
  position: fixed; left: 50%; bottom: 0; width: min(760px, 100%); transform: translate(-50%, 100%); transition: transform 0.22s ease-out;
  background: var(--surface); border-radius: 20px 20px 0 0; z-index: 21; max-height: 85dvh; display: flex; flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom); box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.18);
}
.sheet.show { transform: translate(-50%, 0); }
.sheet-head { display: flex; align-items: center; gap: 12px; padding: 16px; border-bottom: 1px solid var(--line); }
.sheet-head > div { flex: 1; min-width: 0; }
.sheet-body { overflow-y: auto; padding: 8px 16px 20px; }
.sheet-body p { line-height: 1.7; }
.card-list { display: grid; grid-template-columns: max-content 1fr; gap: 10px 16px; margin: 8px 0 0; }
.card-list dt { color: var(--muted); font-size: 14px; }
.card-list dd { margin: 0; overflow-wrap: anywhere; }

/* 登入 */
.auth { width: min(380px, 100%); display: flex; flex-direction: column; gap: 14px; background: var(--surface); padding: 24px 20px; border-radius: 20px; box-shadow: var(--shadow); }
.brand { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-bottom: 4px; }
.brand img { border-radius: 16px; }
.brand h1 { margin: 0; font-size: 22px; }
.auth label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.auth input { padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); font-size: 16px; color: var(--text); }
.note { font-size: 13px; color: var(--muted); margin: 0; }

.toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 12px); transform: translate(-50%, -20px); opacity: 0; transition: all 0.2s;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; z-index: 50; max-width: calc(100% - 32px); font-size: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }
