:root {
  --bg: #f7faf8; --surface: #ffffff; --surface-2: #ecf1ee; --surface-3: #e2e9e5;
  --text: #171d1b; --muted: #4f5b57; --line: #c8d1cd;
  --primary: #0b6b57; --on-primary: #ffffff; --primary-soft: #c5ede1; --on-primary-soft: #00382c;
  --bubble-in: #e5ebe8; --danger: #b3261e; --danger-soft: #f9dedc; --on-danger-soft: #410e0b;
  --scrim: rgba(0, 0, 0, .45); --shadow: 0 8px 28px rgba(0, 0, 0, .18);
  --r: 12px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f1513; --surface: #171d1b; --surface-2: #1b2220; --surface-3: #252d2a;
  --text: #e3eae7; --muted: #b4c0bb; --line: #39433f;
  --primary: #6fdbbe; --on-primary: #00382c; --primary-soft: #155545; --on-primary-soft: #d3f5ea;
  --bubble-in: #252d2a; --danger: #f2b8b5; --danger-soft: #8c1d18; --on-danger-soft: #f9dedc;
  --scrim: rgba(0, 0, 0, .6); --shadow: 0 8px 28px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; background: var(--bg); }
body { color: var(--text); font: 15px/1.45 system-ui, "Segoe UI", Roboto, sans-serif; overflow: hidden; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; user-select: none; }
input, textarea { font: inherit; font-size: 16px; color: inherit; }
h1, h2, h3, p { margin: 0; }
[hidden] { display: none !important; }
* { scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ---------- buttons ---------- */
.btn { min-height: 44px; padding: 0 20px; border-radius: 22px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 8px; transition: background .15s, opacity .15s; }
.btn.primary { background: var(--primary); color: var(--on-primary); }
.btn.ghost { color: var(--primary); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.outline { border: 1px solid var(--line); }
.btn.outline:hover { background: var(--surface-2); }
.btn.danger { background: var(--danger); color: var(--bg); }
.btn.danger-outline { border: 1px solid var(--line); color: var(--danger); }
.btn:active { opacity: .8; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { width: 44px; height: 44px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; color: var(--muted); flex: none; transition: background .15s; }
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn:active { background: var(--line); }

/* ---------- app shell ---------- */
.shell { display: grid; grid-template-columns: 76px 380px minmax(0, 1fr); height: 100dvh; }
.rail { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 16px 0; background: var(--surface-2); }
.rail-item { width: 64px; padding: 6px 0; display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; color: var(--muted); border-radius: var(--r); }
.rail-item .pill { width: 56px; height: 32px; border-radius: 16px; display: flex; align-items: center; justify-content: center; transition: background .15s; }
.rail-item:hover .pill { background: var(--surface-3); }
.rail-item[aria-current="page"] { color: var(--text); }
.rail-item[aria-current="page"] .pill { background: var(--primary); color: var(--on-primary); }
.list-pane { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--bg); }
.main-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--bg); }

.list-head { padding: 12px 12px 8px; display: flex; flex-direction: column; gap: 10px; }
.list-top { display: flex; align-items: center; gap: 8px; }
.search { flex: 1; min-width: 0; height: 48px; border-radius: 24px; background: var(--surface-3); display: flex; align-items: center; gap: 10px; padding: 0 6px 0 16px; color: var(--muted); }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: none; color: var(--text); }
.search:focus-within { outline: 2px solid var(--primary); }
.filters { display: flex; gap: 8px; }
.chip { height: 36px; padding: 0 16px; border-radius: 18px; border: 1px solid var(--line); font-weight: 600; font-size: 14px; color: var(--muted); }
.chip[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.banner { margin: 0 12px 8px; padding: 10px 14px; border-radius: var(--r); background: var(--danger-soft); color: var(--on-danger-soft); font-size: 13px; }

.threads { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 8px 16px; }
.thread { width: 100%; display: flex; align-items: center; gap: 14px; padding: 10px 12px; border-radius: 20px; text-align: left; margin-bottom: 2px; transition: background .15s; }
.thread:hover { background: var(--surface-2); }
.thread.unread { background: var(--surface-3); }
.thread[aria-current="true"] { background: var(--primary-soft); color: var(--on-primary-soft); }
.thread[aria-current="true"] .snippet, .thread[aria-current="true"] .when { color: var(--on-primary-soft); }
.thread-body { flex: 1; min-width: 0; }
.thread-line { display: flex; align-items: baseline; gap: 10px; }
.thread .name { flex: 1; min-width: 0; font-size: 16px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread .when { font-size: 12px; color: var(--muted); flex: none; }
.thread .snippet { flex: 1; min-width: 0; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread.unread .name { font-weight: 700; }
.thread.unread .snippet { color: var(--text); font-weight: 600; }
.thread.unread .when { color: var(--primary); font-weight: 700; }
.thread .snippet.failed { color: var(--danger); }
.count { min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px; background: var(--primary); color: var(--on-primary); font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; flex: none; align-self: center; }
.section-label { padding: 14px 12px 6px; font-size: 13px; font-weight: 700; color: var(--primary); }

.avatar { border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 600; flex: none; width: 48px; height: 48px; font-size: 19px; }
.avatar.small { width: 40px; height: 40px; font-size: 16px; }
.av0 { background: #d7e8ff; color: #0b4a8f; } .av1 { background: #ffdcc2; color: #8a3c00; } .av2 { background: #d5f0dc; color: #135c2b; } .av3 { background: #f6d9f0; color: #7a1f68; }
.av4 { background: #ffe9a8; color: #6b5200; } .av5 { background: #e2ddff; color: #43389e; } .av6 { background: #c9f0ee; color: #00534f; } .av7 { background: #ffd9d9; color: #8c1d18; }
[data-theme="dark"] .av0 { background: #1f3a5c; color: #b9d7ff; } [data-theme="dark"] .av1 { background: #5a3214; color: #ffcfa8; } [data-theme="dark"] .av2 { background: #1d4a2b; color: #b4e8c2; } [data-theme="dark"] .av3 { background: #55244b; color: #f5c4ea; }
[data-theme="dark"] .av4 { background: #4d4010; color: #f2de96; } [data-theme="dark"] .av5 { background: #342f70; color: #cfc9ff; } [data-theme="dark"] .av6 { background: #134a47; color: #a9ebe7; } [data-theme="dark"] .av7 { background: #5e2320; color: #ffc2bd; }

.empty { margin: auto; padding: 32px; text-align: center; max-width: 360px; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.empty h2 { font-size: 17px; }
.empty p { color: var(--muted); }
.empty .btn { margin-top: 12px; }
.skeleton { display: flex; align-items: center; gap: 14px; padding: 10px 12px; }
.skeleton i { display: block; background: var(--surface-3); border-radius: 8px; animation: pulse 1.2s ease-in-out infinite; }
.skeleton .dot { width: 48px; height: 48px; border-radius: 50%; }
.skeleton .bars { flex: 1; display: flex; flex-direction: column; gap: 8px; }
.skeleton .bars i { height: 12px; }
.skeleton .bars i:first-child { width: 45%; }
.skeleton .bars i:last-child { width: 80%; }
@keyframes pulse { 50% { opacity: .45; } }

/* ---------- conversation ---------- */
.chat-head { height: 64px; flex: none; display: flex; align-items: center; gap: 12px; padding: 0 8px 0 16px; background: var(--surface-2); }
.chat-head .who { flex: 1; min-width: 0; }
.chat-head .title { font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back { display: none; }
.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 12px max(16px, calc((100% - 860px) / 2)); display: flex; flex-direction: column; }
.messages > :first-child { margin-top: auto; }
.day { align-self: center; margin: 14px 0 8px; font-size: 12px; font-weight: 600; color: var(--muted); }
.row { display: flex; flex-direction: column; align-items: flex-start; margin-top: 8px; max-width: 100%; }
.row.out { align-items: flex-end; }
.row.join { margin-top: 2px; }
.line { display: flex; align-items: center; gap: 4px; max-width: min(78%, 560px); }
.row.out .line { flex-direction: row-reverse; }
.bubble { min-width: 0; padding: 9px 14px; border-radius: 20px; background: var(--bubble-in); white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; }
.row.out .bubble { background: var(--primary); color: var(--on-primary); }
.row.out .bubble.failed { background: var(--danger-soft); color: var(--on-danger-soft); }
.row.in.join .bubble { border-top-left-radius: 6px; }
.row.in.join-next .bubble { border-bottom-left-radius: 6px; }
.row.out.join .bubble { border-top-right-radius: 6px; }
.row.out.join-next .bubble { border-bottom-right-radius: 6px; }
.bubble a { color: inherit; text-decoration: underline; }
.bubble.media { padding: 4px; }
.bubble.media .text { padding: 6px 10px 5px; }
.sender { font-size: 12px; font-weight: 600; color: var(--muted); margin: 0 0 3px 14px; }
.meta { font-size: 11px; color: var(--muted); margin: 3px 8px 0; display: flex; gap: 8px; align-items: center; }
.meta.failed { color: var(--danger); }
.meta button { font-weight: 700; color: var(--primary); min-height: 28px; }
.msg-more { opacity: 0; width: 36px; height: 36px; }
.line:hover .msg-more, .msg-more:focus-visible { opacity: 1; }
.att-img { display: block; width: 260px; max-width: 100%; height: 220px; object-fit: cover; border-radius: 16px; cursor: zoom-in; background: var(--surface-3); }
.att-video { display: block; width: 300px; max-width: 100%; max-height: 320px; border-radius: 16px; background: #000; }
.att-file { display: flex; align-items: center; gap: 10px; padding: 8px 12px; min-height: 44px; color: inherit; text-decoration: none; font-size: 14px; }
.att-note { padding: 8px 12px; font-size: 13px; opacity: .8; }
.audio { display: flex; align-items: center; gap: 10px; padding: 4px 10px 4px 4px; min-width: 220px; }
.audio .play { width: 40px; height: 40px; border-radius: 50%; background: var(--surface); color: var(--primary); display: flex; align-items: center; justify-content: center; flex: none; }
.audio .track { flex: 1; height: 4px; border-radius: 2px; background: color-mix(in srgb, currentColor 25%, transparent); position: relative; cursor: pointer; }
.audio .fill { position: absolute; inset: 0 auto 0 0; width: 0; border-radius: 2px; background: currentColor; }
.audio .time { font-size: 12px; font-variant-numeric: tabular-nums; }

.composer { flex: none; padding: 8px max(12px, calc((100% - 860px) / 2)) max(12px, env(safe-area-inset-bottom)); }
.composer-row { display: flex; align-items: flex-end; gap: 6px; background: var(--surface-3); border-radius: 26px; padding: 4px; }
.composer textarea { flex: 1; min-width: 0; border: 0; outline: 0; resize: none; background: none; padding: 11px 4px; max-height: 160px; line-height: 1.4; }
.send { width: 44px; height: 44px; border-radius: 50%; background: var(--primary); color: var(--on-primary); display: flex; align-items: center; justify-content: center; flex: none; }
.send:disabled { opacity: .4; cursor: default; }
.pending { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 4px 8px; }
.pending-item { display: flex; align-items: center; gap: 6px; padding: 4px 4px 4px 12px; border-radius: 18px; background: var(--surface-3); font-size: 13px; max-width: 240px; }
.pending-item span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pending-item .icon-btn { width: 32px; height: 32px; }

/* ---------- settings ---------- */
.page { flex: 1; min-height: 0; overflow-y: auto; padding: 24px max(16px, calc((100% - 720px) / 2)); }
.page h1 { font-size: 24px; font-weight: 600; margin-bottom: 20px; }
.section { background: var(--surface-2); border-radius: 16px; padding: 16px 20px; margin-bottom: 16px; }
.section h2 { font-size: 13px; font-weight: 700; color: var(--primary); margin-bottom: 10px; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; min-height: 44px; }
.setting .label { font-weight: 500; }
.setting .hint { font-size: 13px; color: var(--muted); }
.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 22px; padding: 3px; }
.segmented button { min-height: 38px; padding: 0 16px; border-radius: 19px; font-weight: 600; font-size: 14px; color: var(--muted); }
.segmented button[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); }

/* ---------- pairing and sign-in ---------- */
.center { height: 100dvh; overflow-y: auto; display: flex; padding: 24px; }
.card { margin: auto; width: 100%; max-width: 440px; display: flex; flex-direction: column; gap: 16px; }
.card h1 { font-size: 26px; font-weight: 600; }
.card p { color: var(--muted); }
.code-row { display: flex; gap: 6px; }
.code-box { flex: 1; min-width: 0; height: 56px; border-radius: var(--r); border: 1px solid var(--line); background: var(--surface); text-align: center; font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; outline: 0; }
.code-box:focus { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
span.code-box { display: flex; align-items: center; justify-content: center; }
.error { color: var(--danger); font-size: 14px; min-height: 20px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; font-weight: 600; color: var(--muted); }
.field input { height: 48px; border-radius: var(--r); border: 1px solid var(--line); background: var(--surface); padding: 0 14px; outline: 0; width: 100%; }
.field input:focus { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; background: var(--scrim); display: flex; padding: 16px; z-index: 20; animation: fade .15s; }
.dialog { margin: auto; width: 100%; max-width: 440px; max-height: 100%; overflow-y: auto; background: var(--surface); border-radius: 24px; padding: 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; }
.dialog h2 { font-size: 19px; font-weight: 600; overflow-wrap: anywhere; }
.dialog-body { color: var(--muted); display: flex; flex-direction: column; gap: 12px; }
.dialog-body .field, .dialog-body .code-row { color: var(--text); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.menu { position: fixed; z-index: 30; min-width: 200px; background: var(--surface); border-radius: 14px; padding: 6px; box-shadow: var(--shadow); border: 1px solid var(--line); }
.menu-item { width: 100%; min-height: 44px; padding: 0 12px; display: flex; align-items: center; gap: 12px; border-radius: 10px; text-align: left; }
.menu-item:hover { background: var(--surface-2); }
.menu-item.danger { color: var(--danger); }
.toast { position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 40; max-width: calc(100% - 32px); background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 12px; display: flex; gap: 16px; align-items: center; box-shadow: var(--shadow); font-size: 14px; }
.toast-action { font-weight: 700; color: var(--primary-soft); }
.viewer { position: fixed; inset: 0; z-index: 25; background: rgba(0, 0, 0, .92); display: flex; align-items: center; justify-content: center; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer .bar { position: absolute; top: 12px; right: 12px; display: flex; gap: 8px; }
.viewer .icon-btn { background: rgba(255, 255, 255, .14); color: #fff; }
.suggest { display: flex; flex-direction: column; max-height: 240px; overflow-y: auto; }
.suggest button { min-height: 48px; padding: 4px 8px; border-radius: 10px; text-align: left; display: flex; flex-direction: column; justify-content: center; color: var(--text); }
.suggest button:hover { background: var(--surface-2); }
.suggest small { color: var(--muted); }
.picked { display: flex; flex-wrap: wrap; gap: 6px; }
.picked:empty { display: none; }
@keyframes fade { from { opacity: 0; } }

/* ---------- admin ---------- */
.admin { height: 100dvh; display: flex; flex-direction: column; }
.admin-head { flex: none; display: flex; align-items: center; flex-wrap: wrap; gap: 12px; padding: 16px max(16px, calc((100% - 1100px) / 2)); background: var(--surface-2); }
.admin-head h1 { font-size: 20px; font-weight: 600; flex: 1; white-space: nowrap; }
.accounts { flex: 1; min-height: 0; overflow-y: auto; padding: 16px max(16px, calc((100% - 1100px) / 2)); container-type: inline-size; }
.account { display: grid; grid-template-columns: minmax(0, 2fr) 150px minmax(0, 1.4fr) 90px 110px 44px; align-items: center; gap: 16px; padding: 12px 8px 12px 20px; background: var(--surface-2); border-radius: 16px; margin-bottom: 8px; }
.account.head { background: none; padding-top: 0; padding-bottom: 4px; font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.account .name { font-weight: 600; overflow-wrap: anywhere; }
.account .sub { font-size: 13px; color: var(--muted); }
.account .num { text-align: right; font-variant-numeric: tabular-nums; }
.badge { display: inline-flex; align-items: center; height: 26px; padding: 0 10px; border-radius: 13px; font-size: 12px; font-weight: 700; background: var(--primary-soft); color: var(--on-primary-soft); white-space: nowrap; }
.badge.off { background: var(--danger-soft); color: var(--on-danger-soft); }
.badge.wait { background: var(--surface-3); color: var(--muted); }
.account .k { display: none; }
@container (max-width: 820px) {
  .account.head { display: none; }
  .account { grid-template-columns: minmax(0, 1fr) 44px; gap: 6px 12px; padding: 16px 8px 16px 20px; }
  .account > * { grid-column: 1; }
  .account > .icon-btn { grid-column: 2; grid-row: 1; }
  .account .num { text-align: left; }
  .account .k { display: inline; color: var(--muted); font-size: 13px; margin-right: 6px; }
}

.shell[data-wide] { grid-template-columns: 76px minmax(0, 1fr); }
.shell[data-wide] .list-pane { display: none; }
.main-pane .admin { height: auto; flex: 1; min-height: 0; }
.stepper { display: flex; align-items: center; gap: 8px; }
.stepper .icon-btn { border: 1px solid var(--line); }
.stepper-value { min-width: 40px; text-align: center; font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }
.device { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 8px 0; color: var(--text); }
.device + .device { border-top: 1px solid var(--line); }
.device .hint { font-size: 13px; color: var(--muted); }
.field input:disabled { opacity: .6; }
.card .btn { width: 100%; }
.or { text-align: center; font-size: 13px; color: var(--muted); }

/* ---------- tablet ---------- */
@media (max-width: 1099px) { .shell { grid-template-columns: 76px 320px minmax(0, 1fr); } .shell[data-wide] { grid-template-columns: 76px minmax(0, 1fr); } }

/* ---------- phone ---------- */
@media (max-width: 759px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  .rail { grid-row: 2; flex-direction: row; justify-content: space-around; padding: 8px 0 max(8px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
  .list-pane { border-right: 0; }
  .shell[data-pane="list"] .main-pane, .shell[data-pane="main"] .list-pane { display: none; }
  .shell[data-chat] .rail { display: none; }
  .back { display: inline-flex; }
  .chat-head { padding-left: 4px; }
  .line { max-width: 86%; }
  .msg-more { opacity: 1; }
  .overlay { padding: 0; align-items: flex-end; }
  .dialog { margin: auto 0 0; max-width: none; border-radius: 24px 24px 0 0; padding-bottom: max(24px, env(safe-area-inset-bottom)); }
  .dialog-actions .btn { flex: 1; }
}

a.btn { text-decoration: none; }

body:has(.doc) { overflow: auto; }
.doc { max-width: 680px; margin: 0 auto; padding: 40px 24px 64px; display: flex; flex-direction: column; gap: 12px; }
.doc h1 { font-size: 28px; font-weight: 600; }
.doc h2 { font-size: 17px; font-weight: 600; margin-top: 16px; }
.doc p { color: var(--muted); }
.doc a { color: var(--accent, inherit); }
