:root {
  color-scheme: light;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-soft: #f7f9fc;
  --text: #172033;
  --muted: #6b7382;
  --line: #e2e7f0;
  --primary: #3867e8;
  --primary-hover: #2855cc;
  --primary-soft: #eaf0ff;
  --success: #18875a;
  --danger: #c63c4b;
  --shadow: 0 18px 60px rgba(27, 39, 71, .13);
  --radius: 18px;
  --contacts-width: 320px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; overflow: hidden; background: var(--bg); color: var(--text); }
button, input, textarea, select { font: inherit; color: inherit; }
button, input, textarea, select { min-height: 48px; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .5; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 3px solid rgba(56, 103, 232, .24); outline-offset: 2px;
}
[hidden] { display: none !important; }
.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; }

.eyebrow { margin: 0 0 5px; color: var(--primary); font-size: 13px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 30px; }
h2 { font-size: 22px; }
label { display: block; margin: 17px 0 8px; font-size: 14px; font-weight: 650; }
input, textarea, select { width: 100%; border: 1px solid var(--line); border-radius: 13px; background: var(--surface); padding: 11px 13px; }
input:focus, textarea:focus, select:focus { border-color: var(--primary); }
.consent-row { display: flex; align-items: flex-start; gap: 8px; margin: 12px 0 0; color: var(--muted); font-size: 12px; font-weight: 400; line-height: 1.55; }
.consent-row input { width: 20px; min-height: 20px; margin: 2px 0 0; }
.primary-button, .send-button { border: 0; color: #fff; background: var(--primary); font-weight: 700; }
.primary-button { width: 100%; margin-top: 14px; border-radius: 13px; }
.primary-button:hover, .send-button:hover { background: var(--primary-hover); }
.api-actions { display: flex; gap: 10px; margin-top: 14px; }
.api-actions button { flex: 1; margin-top: 0; padding: 10px; }
.contacts-note { margin: 12px 18px; }
.field-help, .sheet-intro { color: var(--muted); font-size: 13px; line-height: 1.65; }
.version-label { margin: 12px 0 0; color: var(--muted); font-size: 12px; text-align: center; }

.app-shell { display: grid; grid-template-columns: var(--contacts-width) minmax(0, 1fr); height: 100dvh; max-width: 1500px; margin: auto; background: var(--surface); box-shadow: 0 0 70px rgba(28,39,68,.08); }
.contacts { display: flex; min-width: 0; flex-direction: column; border-right: 1px solid var(--line); background: var(--surface-soft); overflow: hidden; }
.contacts-heading, .conversation-header, .sheet-header { display: flex; align-items: center; gap: 12px; }
.contacts-heading { min-height: 78px; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.brand-name { display: block; font-weight: 750; }
.contacts-heading small { display: block; margin-top: 3px; color: var(--muted); }
.icon-button, .quiet-button, .danger-button, .danger-outline { min-width: 48px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.icon-button:hover, .quiet-button:hover { background: var(--surface-soft); }
.role-filter { display: flex; gap: 8px; padding: 14px 14px 8px; overflow-x: auto; }
.filter-chip { min-height: 40px; padding: 7px 13px; border: 0; border-radius: 999px; background: transparent; color: var(--muted); white-space: nowrap; }
.filter-chip.is-active { background: var(--primary-soft); color: var(--primary); font-weight: 700; }
.role-list { flex: 1; overflow: auto; padding: 8px; }
.role-item { display: grid; grid-template-columns: 46px minmax(0, 1fr); gap: 12px; width: 100%; min-height: 72px; padding: 10px; border: 0; border-radius: 14px; background: transparent; text-align: left; }
.role-item:hover { background: #eef2f8; }
.role-item.is-active { background: var(--surface); box-shadow: 0 4px 18px rgba(34,48,82,.08); }
.role-avatar, .active-role-avatar { display: grid; place-items: center; border-radius: 16px; color: #fff; background: linear-gradient(145deg, #718ee4, #4b63a8); font-weight: 750; }
.role-avatar { width: 46px; height: 46px; }
.role-meta { min-width: 0; }
.role-meta strong, .role-meta span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.role-meta span { margin-top: 5px; color: var(--muted); font-size: 12px; }
.contacts-logout { margin: 10px 14px max(14px, env(safe-area-inset-bottom)); }

.conversation { position: relative; display: grid; grid-template-rows: auto minmax(0, 1fr) auto auto auto; min-width: 0; height: 100dvh; background: var(--surface); }
.conversation-header { min-height: 72px; padding: max(10px, env(safe-area-inset-top)) 18px 10px; border-bottom: 1px solid var(--line); }
#showContacts { display: none; }
.active-role-avatar { width: 44px; height: 44px; flex: 0 0 44px; }
.active-role-copy { min-width: 0; flex: 1; }
.active-role-copy strong, .active-role-copy span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.active-role-copy span { margin-top: 3px; color: var(--muted); font-size: 12px; }
.wallet-pill { display: flex; align-items: center; gap: 8px; min-height: 44px; border: 0; border-radius: 999px; padding: 8px 14px; color: var(--primary); background: var(--primary-soft); }
.messages { overflow-y: auto; overscroll-behavior: contain; padding: 22px clamp(14px, 4vw, 48px); scroll-behavior: smooth; }
.message-row { display: flex; align-items: flex-end; gap: 8px; max-width: 760px; margin: 0 auto 16px; }
.message-row.is-user { justify-content: flex-end; }
.message-select { flex: 0 0 20px; width: 20px; min-height: 20px; margin: 0 2px 15px 0; }
.message-bubble { max-width: min(82%, 650px); padding: 12px 14px 8px; border: 1px solid var(--line); border-radius: 6px 18px 18px 18px; background: var(--surface-soft); line-height: 1.68; overflow-wrap: anywhere; }
.is-user .message-bubble { border-color: transparent; border-radius: 18px 6px 18px 18px; background: var(--primary); color: #fff; }
.message-content { margin: 0; white-space: pre-wrap; }
.message-actions { display: flex; gap: 3px; margin-top: 5px; opacity: .72; }
.message-action { min-height: 32px; border: 0; border-radius: 8px; padding: 3px 8px; background: transparent; color: inherit; font-size: 12px; }
.message-action:hover { background: rgba(128,128,128,.12); }
.message-status { margin-top: 6px; color: var(--muted); font-size: 12px; }
.is-user .message-status { color: rgba(255,255,255,.75); }
.empty-chat { max-width: 470px; margin: 12vh auto 0; text-align: center; color: var(--muted); line-height: 1.8; }
.empty-chat strong { display: block; margin-bottom: 9px; color: var(--text); font-size: 21px; }
.message-tools { display: flex; align-items: center; gap: 8px; min-height: 50px; padding: 4px clamp(12px, 3vw, 32px); border-top: 1px solid var(--line); }
.quiet-button, .danger-button, .danger-outline { padding: 8px 14px; }
.danger-button { border-color: transparent; color: white; background: var(--danger); }
.danger-outline { width: 100%; margin-top: 28px; color: var(--danger); border-color: rgba(198,60,75,.3); }
.selection-count { color: var(--muted); font-size: 13px; }
.composer { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: end; gap: 9px; padding: 10px clamp(12px, 3vw, 32px); background: var(--surface); }
.composer textarea { max-height: 160px; min-height: 48px; resize: none; overflow-y: auto; line-height: 1.5; }
.send-button { min-width: 74px; border-radius: 13px; padding: 0 17px; }
.bottom-nav { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); padding: 4px 4px max(4px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--surface); }
.nav-item { min-width: 0; border: 0; background: transparent; color: var(--muted); font-size: 12px; }
.nav-item.is-active { color: var(--primary); font-weight: 750; }

.sheet-backdrop { position: fixed; inset: 0; z-index: 20; background: rgba(18,25,40,.32); }
.side-sheet { position: fixed; z-index: 21; top: 0; right: 0; width: min(100%, 430px); height: 100dvh; padding: max(24px, env(safe-area-inset-top)) 22px max(24px, env(safe-area-inset-bottom)); overflow-y: auto; background: var(--surface); box-shadow: var(--shadow); }
.sheet-header { justify-content: space-between; margin-bottom: 22px; }
.balance-card { display: flex; align-items: baseline; gap: 8px; padding: 22px; border-radius: var(--radius); color: white; background: linear-gradient(140deg, #315fdc, #5880ea); }
.balance-card span { margin-right: auto; }
.balance-card strong { font-size: 30px; }
.beta-notice { display: grid; gap: 4px; margin: 16px 0; padding: 14px; border-radius: 14px; color: #126447; background: #e9f8f1; font-size: 13px; }
.credit-packages { display: grid; gap: 10px; }
.package-card { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; align-items: center; padding: 15px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); }
.package-card span { color: var(--muted); font-size: 13px; }
.package-card button { grid-column: 2; grid-row: 1 / span 2; border: 0; border-radius: 11px; padding: 0 13px; color: var(--primary); background: var(--primary-soft); font-weight: 700; }
.memory-list { display: grid; gap: 10px; }
.memory-item { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: start; padding: 14px; border: 1px solid var(--line); border-radius: 14px; }
.memory-item p { margin: 0; line-height: 1.6; }
.memory-item button { min-height: 36px; border: 0; background: transparent; color: var(--danger); }
.relationship-meters { display: grid; gap: 18px; }
.meter-heading { display: flex; justify-content: space-between; margin-bottom: 7px; }
.meter-track { height: 9px; overflow: hidden; border-radius: 999px; background: var(--line); }
.meter-fill { height: 100%; border-radius: inherit; background: linear-gradient(90deg, #5f7de1, #65b998); }
.settings-group { margin: 14px 0 22px; padding: 14px; border: 1px solid var(--line); border-radius: 14px; }
.toast { position: fixed; z-index: 120; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); max-width: calc(100% - 32px); padding: 11px 17px; border-radius: 999px; color: #fff; background: rgba(20,28,44,.92); box-shadow: var(--shadow); }

html[data-theme="dark"] { color-scheme: dark; --bg: #11151d; --surface: #181e28; --surface-soft: #202733; --text: #f2f5fa; --muted: #a8b0be; --line: #303947; --primary: #7595f5; --primary-hover: #89a5f8; --primary-soft: #26365f; --shadow: 0 18px 60px rgba(0,0,0,.35); }

@media (max-width: 899px) {
  .app-shell { display: block; }
  .contacts { position: fixed; z-index: 15; inset: 0 auto 0 0; width: min(86vw, 340px); box-shadow: var(--shadow); transform: translateX(-105%); transition: transform .18s ease; }
  .app-shell.contacts-open .contacts { transform: translateX(0); }
  #showContacts { display: inline-grid; place-items: center; }
  .conversation { grid-template-rows: auto minmax(0, 1fr) auto auto auto; }
  .conversation-header { min-height: 64px; padding-inline: 10px; }
  .active-role-avatar { width: 38px; height: 38px; flex-basis: 38px; border-radius: 13px; }
  .wallet-pill { padding-inline: 10px; }
  .wallet-pill span { display: none; }
  .messages { padding: 16px 11px; }
  .message-bubble { max-width: 88%; }
  .message-tools { min-height: 44px; padding-block: 0; }
  .message-tools button { min-height: 40px; }
  .composer { padding: 8px; }
  #pasteMessage { min-width: 52px; padding-inline: 5px; font-size: 12px; }
  .send-button { min-width: 62px; padding-inline: 10px; }
  .bottom-nav { display: grid; }
}

@media (max-width: 420px) {
  .message-actions { opacity: 1; }
  .side-sheet { padding-inline: 17px; }
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) { color-scheme: dark; --bg: #11151d; --surface: #181e28; --surface-soft: #202733; --text: #f2f5fa; --muted: #a8b0be; --line: #303947; --primary: #7595f5; --primary-hover: #89a5f8; --primary-soft: #26365f; --shadow: 0 18px 60px rgba(0,0,0,.35); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
