/* ================= Tokens ================= */
:root {
  --accent: #0e9f6e;
  --accent-hover: #057a55;
  --accent-soft: #e5f6ee;
  --accent-text: #03724f;
  --on-accent: #ffffff;

  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --surface-3: #eef0f3;
  --border: #e4e7ec;
  --border-strong: #d0d5dd;
  --text: #101828;
  --text-2: #475467;
  --muted: #8a94a6;

  --chat-bg: #eef0ec;
  --chat-dot: rgba(16, 24, 40, 0.045);
  --bubble-in: #ffffff;
  --bubble-out: #d9f7e7;
  --bubble-text: #101828;
  --link: #0b6bcb;
  --tick-read: #1d8cf8;

  --amber: #b54708;
  --amber-soft: #fef3e2;
  --danger: #d92d20;
  --danger-soft: #fdecea;
  --info-soft: #eaf3fe;
  --info: #175cd3;

  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 12px -2px rgba(16, 24, 40, 0.08), 0 2px 4px -2px rgba(16, 24, 40, 0.04);
  --shadow-lg: 0 24px 48px -12px rgba(16, 24, 40, 0.22);
  --ring: 0 0 0 3px rgba(14, 159, 110, 0.25);

  --radius: 12px;
  --radius-sm: 8px;
  --rail: 76px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --accent: #12b886;
    --accent-hover: #34d399;
    --accent-soft: rgba(18, 184, 134, 0.14);
    --accent-text: #4ade9f;
    --on-accent: #04130d;
    --bg: #0a0d11;
    --surface: #111519;
    --surface-2: #161b21;
    --surface-3: #1d232a;
    --border: #232a32;
    --border-strong: #313a44;
    --text: #e8ebef;
    --text-2: #aab3be;
    --muted: #6f7a87;
    --chat-bg: #0c1014;
    --chat-dot: rgba(255, 255, 255, 0.035);
    --bubble-in: #1b2128;
    --bubble-out: #0f3a2b;
    --bubble-text: #e8ebef;
    --link: #6cb6ff;
    --tick-read: #53b4ff;
    --amber: #fdb022;
    --amber-soft: rgba(253, 176, 34, 0.12);
    --danger: #f97066;
    --danger-soft: rgba(249, 112, 102, 0.12);
    --info-soft: rgba(83, 180, 255, 0.12);
    --info: #84caff;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 12px -2px rgba(0, 0, 0, 0.5);
    --shadow-lg: 0 24px 48px -12px rgba(0, 0, 0, 0.7);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --accent: #12b886;
  --accent-hover: #34d399;
  --accent-soft: rgba(18, 184, 134, 0.14);
  --accent-text: #4ade9f;
  --on-accent: #04130d;
  --bg: #0a0d11;
  --surface: #111519;
  --surface-2: #161b21;
  --surface-3: #1d232a;
  --border: #232a32;
  --border-strong: #313a44;
  --text: #e8ebef;
  --text-2: #aab3be;
  --muted: #6f7a87;
  --chat-bg: #0c1014;
  --chat-dot: rgba(255, 255, 255, 0.035);
  --bubble-in: #1b2128;
  --bubble-out: #0f3a2b;
  --bubble-text: #e8ebef;
  --link: #6cb6ff;
  --tick-read: #53b4ff;
  --amber: #fdb022;
  --amber-soft: rgba(253, 176, 34, 0.12);
  --danger: #f97066;
  --danger-soft: rgba(249, 112, 102, 0.12);
  --info-soft: rgba(83, 180, 255, 0.12);
  --info: #84caff;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 12px -2px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 24px 48px -12px rgba(0, 0, 0, 0.7);
  color-scheme: dark;
}

/* ================= Base ================= */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-feature-settings: "cv11", "ss01";
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; letter-spacing: -0.01em; }
p { margin: 0; }
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
[hidden] { display: none !important; }
:focus-visible { outline: none; box-shadow: var(--ring); }

i[data-icon] { display: inline-grid; place-items: center; width: 20px; height: 20px; flex: none; font-style: normal; }
i[data-icon] svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.logo {
  width: 40px; height: 40px; border-radius: 12px; background: var(--accent);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M9 10.5h14a2 2 0 0 1 2 2v7.5a2 2 0 0 1-2 2h-7.5L11 25v-3H9a2 2 0 0 1-2-2v-7.5a2 2 0 0 1 2-2z' fill='white'/%3E%3C/svg%3E");
  background-size: 100%; flex: none;
}
.logo-lg { width: 56px; height: 56px; border-radius: 16px; margin: 0 auto; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 38px; padding: 0 14px; border-radius: 10px; font-weight: 600; font-size: 13.5px;
  text-decoration: none; white-space: nowrap; transition: background .12s, border-color .12s, color .12s;
  border: 1px solid transparent;
}
.btn i[data-icon] { width: 17px; height: 17px; }
.btn-sm { height: 32px; padding: 0 11px; font-size: 13px; border-radius: 9px; }
.btn-block { width: 100%; height: 44px; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: .55; cursor: default; }
.btn-ghost { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--surface-2); border-color: var(--border-strong); }
.btn-danger { color: var(--danger); }
.link-btn { color: var(--accent-text); font-weight: 600; font-size: 12.5px; display: inline-flex; align-items: center; gap: 3px; }
.link-btn i[data-icon] { width: 14px; height: 14px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 10px; display: inline-grid; place-items: center; color: var(--text-2); flex: none;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn-sm { width: 28px; height: 28px; border-radius: 8px; }
.icon-btn-sm i[data-icon] { width: 16px; height: 16px; }

.field { display: flex; flex-direction: column; gap: 6px; border: 0; margin: 0 0 16px; padding: 0; min-width: 0; }
.field > span, .field > legend { font-size: 13px; font-weight: 600; color: var(--text); }
.field em { font-style: normal; font-weight: 400; color: var(--muted); }
.field small { color: var(--muted); font-size: 12px; }
.field-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.field input, .field select, .field textarea, .login-card input {
  width: 100%; border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 9px 12px; color: var(--text); transition: border-color .12s, box-shadow .12s;
}
.field textarea { resize: vertical; min-height: 120px; line-height: 1.5; }
.field input:focus, .field select:focus, .field textarea:focus, .login-card input:focus {
  outline: none; border-color: var(--accent); box-shadow: var(--ring);
}
.field input.invalid, .field textarea.invalid { border-color: var(--danger); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.form-error { color: var(--danger); font-size: 13px; min-height: 18px; }
.form-error:empty { min-height: 0; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-sm); }

.segmented { display: flex; gap: 4px; padding: 4px; margin: 0 14px 10px; background: var(--surface-3); border-radius: 10px; }
.segmented button {
  flex: 1; height: 28px; border-radius: 7px; font-size: 12.5px; font-weight: 600; color: var(--text-2);
}
.segmented button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }
.segmented-inline { display: inline-flex; margin: 0 0 18px; }
.segmented-inline button { flex: none; padding: 0 14px; }

.switch {
  position: relative; width: 38px; height: 22px; border-radius: 11px; background: var(--border-strong);
  transition: background .15s; flex: none;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.2); transition: transform .15s;
}
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"]::after { transform: translateX(16px); }

.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none; display: grid; place-items: center;
  color: #fff; font-weight: 600; font-size: 15px; letter-spacing: .02em;
}
.avatar i[data-icon] { width: 22px; height: 22px; }

.pill {
  display: inline-flex; align-items: center; gap: 4px; height: 20px; padding: 0 7px; border-radius: 6px;
  font-size: 11px; font-weight: 600; white-space: nowrap; flex: none;
}
.pill i[data-icon] { width: 12px; height: 12px; }
.pill-ai { background: var(--accent-soft); color: var(--accent-text); }
.pill-you { background: var(--amber-soft); color: var(--amber); }
.pill-ok { background: var(--accent-soft); color: var(--accent-text); }
.pill-wait { background: var(--amber-soft); color: var(--amber); }
.pill-bad { background: var(--danger-soft); color: var(--danger); }
.pill-muted { background: var(--surface-3); color: var(--text-2); }

/* ================= Login ================= */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px; padding: 36px 28px 28px; box-shadow: var(--shadow-md); text-align: center;
}
.login-card h1 { font-size: 22px; font-weight: 700; margin: 18px 0 6px; }
.login-card p { color: var(--text-2); margin-bottom: 22px; }
.login-card .field { text-align: left; }
.login-card .form-error { margin-top: 12px; }

/* ================= App shell ================= */
.app { height: 100%; display: flex; }
.rail {
  width: var(--rail); flex: none; display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 14px 0; background: var(--surface); border-right: 1px solid var(--border);
}
.rail .logo { margin-bottom: 14px; }
.rail-btn {
  position: relative; width: 60px; padding: 8px 0 6px; border-radius: 12px; display: flex; flex-direction: column;
  align-items: center; gap: 3px; color: var(--text-2); text-decoration: none; font-size: 10.5px; font-weight: 600;
}
.rail-btn i[data-icon] { width: 22px; height: 22px; }
.rail-btn:hover { background: var(--surface-3); color: var(--text); }
.rail-btn.active { background: var(--accent-soft); color: var(--accent-text); }
.rail-spacer { flex: 1; }
.rail-badge {
  position: absolute; top: 3px; right: 9px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--accent); color: var(--on-accent); font-size: 10.5px; display: grid; place-items: center;
  border: 2px solid var(--surface);
}
.view { flex: 1; min-width: 0; display: flex; }

/* ================= Chats ================= */
.chat-list-pane {
  width: 360px; flex: none; display: flex; flex-direction: column; background: var(--surface);
  border-right: 1px solid var(--border);
}
.pane-head { height: 64px; flex: none; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 14px 0 18px; }
.pane-head h1 { font-size: 20px; font-weight: 700; }
.pane-head h2 { font-size: 16px; font-weight: 700; }
.search-box {
  display: flex; align-items: center; gap: 8px; margin: 0 14px 10px; padding: 0 12px; height: 38px;
  background: var(--surface-3); border-radius: 10px; color: var(--muted); border: 1px solid transparent;
}
.search-box:focus-within { background: var(--surface); border-color: var(--accent); box-shadow: var(--ring); }
.search-box i[data-icon] { width: 17px; height: 17px; }
.search-box input { flex: 1; min-width: 0; border: 0; background: none; outline: none; color: var(--text); }
.chat-list { flex: 1; overflow-y: auto; padding: 2px 8px 12px; }
.chat-item {
  width: 100%; display: flex; align-items: center; gap: 12px; padding: 10px; border-radius: 12px; text-align: left;
}
.chat-item + .chat-item { margin-top: 2px; }
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--accent-soft); }
.ci-body { flex: 1; min-width: 0; }
.ci-top, .ci-bottom { display: flex; align-items: center; gap: 6px; }
.ci-name { flex: 1; min-width: 0; font-weight: 500; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item.unread .ci-name { font-weight: 700; }
.ci-time { font-size: 11.5px; color: var(--muted); flex: none; }
.chat-item.unread .ci-time { color: var(--accent-text); font-weight: 600; }
.ci-bottom { margin-top: 2px; }
.ci-preview {
  flex: 1; min-width: 0; color: var(--text-2); font-size: 13px; display: flex; align-items: center; gap: 3px;
}
.ci-preview span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci-preview .who { color: var(--muted); flex: none; }
.ci-preview .ticks { flex: none; }
.chat-item.unread .ci-preview { color: var(--text); }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent); color: var(--on-accent);
  font-size: 11px; font-weight: 700; display: grid; place-items: center; flex: none;
}
.list-empty { padding: 48px 20px; text-align: center; color: var(--muted); }
.list-empty strong { display: block; color: var(--text); margin-bottom: 4px; font-size: 14.5px; }

/* ---------- Conversation ---------- */
.conversation-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--chat-bg); }
.empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  padding: 32px; color: var(--text-2); background: var(--surface-2);
}
.empty-state h2 { font-size: 22px; font-weight: 700; color: var(--text); margin: 20px 0 8px; }
.empty-state p { max-width: 440px; }
.empty-art {
  width: 84px; height: 84px; border-radius: 24px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent-text);
}
.empty-art i[data-icon] { width: 40px; height: 40px; }
.conversation { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.convo-head {
  height: 64px; flex: none; display: flex; align-items: center; gap: 10px; padding: 0 12px 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.convo-who { flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px; text-align: left; border-radius: 10px; padding: 4px; margin-left: -4px; }
.convo-who .avatar { width: 40px; height: 40px; font-size: 14px; }
.convo-names { min-width: 0; display: flex; flex-direction: column; }
.convo-name { font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.convo-sub { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; white-space: nowrap; overflow: hidden; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; }
.dot.you { background: var(--amber); }
.ai-toggle {
  display: flex; align-items: center; gap: 10px; padding: 6px 8px 6px 12px; border: 1px solid var(--border);
  border-radius: 999px; cursor: pointer; flex: none;
}
.ai-toggle-label { display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; color: var(--text-2); }
.ai-toggle-label i[data-icon] { width: 16px; height: 16px; color: var(--accent-text); }

.messages {
  flex: 1; overflow-y: auto; padding: 16px max(16px, 5%) 10px;
  background-color: var(--chat-bg);
  background-image: radial-gradient(var(--chat-dot) 1.2px, transparent 1.2px);
  background-size: 22px 22px;
}
.load-more { display: flex; justify-content: center; margin: 4px 0 8px; }
.day {
  position: sticky; top: 0; z-index: 1; margin: 14px auto 10px; width: fit-content; font-size: 11.5px; font-weight: 600;
  color: var(--text-2); background: var(--surface); padding: 4px 12px; border-radius: 999px; box-shadow: var(--shadow-sm);
}
.system-note {
  margin: 4px auto 14px; max-width: 460px; text-align: center; font-size: 12px; color: var(--text-2);
  background: var(--surface); border: 1px solid var(--border); padding: 8px 14px; border-radius: 12px;
}
.msg { display: flex; flex-direction: column; align-items: flex-start; margin: 2px 0; }
.msg.out { align-items: flex-end; }
.msg.first { margin-top: 12px; }
.bubble {
  position: relative; max-width: min(72%, 600px); padding: 7px 10px 7px 11px; border-radius: 14px;
  background: var(--bubble-in); color: var(--bubble-text); box-shadow: var(--shadow-sm);
  overflow-wrap: anywhere; font-size: 14.2px; line-height: 1.45;
}
.msg.out .bubble { background: var(--bubble-out); }
.msg.first.in .bubble { border-top-left-radius: 4px; }
.msg.first.out .bubble { border-top-right-radius: 4px; }
.bubble.template { border: 1px dashed color-mix(in srgb, var(--accent) 45%, transparent); }
.bubble-label {
  display: flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 700; color: var(--accent-text); margin-bottom: 3px;
}
.bubble-label i[data-icon] { width: 13px; height: 13px; }
.bubble .text { white-space: pre-wrap; }
.bubble .text a { color: var(--link); }
.bubble code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12.5px; }
.meta {
  float: right; margin: 7px 0 -4px 12px; font-size: 10.5px; color: var(--muted); display: inline-flex; align-items: center;
  gap: 3px; white-space: nowrap; position: relative; top: 2px;
}
.ticks { display: inline-flex; }
.ticks svg { width: 16px; height: 11px; }
.ticks.read { color: var(--tick-read); }
.ticks.failed { color: var(--danger); }
.ticks.failed svg { width: 13px; height: 13px; }
.msg-error {
  display: flex; align-items: center; gap: 5px; max-width: min(72%, 600px); margin-top: 4px; font-size: 12px; color: var(--danger);
}
.msg-error i[data-icon] { width: 14px; height: 14px; }
.media-img { display: block; width: 300px; max-width: 100%; max-height: 360px; object-fit: cover; border-radius: 9px; margin: -2px -4px 6px -5px; cursor: zoom-in; background: var(--surface-3); min-height: 120px; }
.sticker { width: 140px; height: 140px; object-fit: contain; display: block; }
.media-video { display: block; width: 300px; max-width: 100%; border-radius: 9px; margin-bottom: 6px; }
.bubble audio { display: block; width: 260px; max-width: 100%; margin-bottom: 4px; }
.doc {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 9px; margin-bottom: 6px; min-width: 220px;
  background: color-mix(in srgb, var(--text) 6%, transparent); text-decoration: none; color: inherit; font-weight: 500;
}
.doc i[data-icon] { width: 26px; height: 26px; color: var(--text-2); }
.doc span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.composer-wrap { flex: none; padding: 6px 16px 14px; background: var(--chat-bg); }
.window-note { font-size: 11.5px; color: var(--muted); text-align: center; margin-bottom: 6px; min-height: 16px; }
.composer {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow-md);
  padding: 6px;
}
.composer:focus-within { border-color: var(--border-strong); }
.composer-row { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea {
  flex: 1; min-width: 0; border: 0; outline: none; resize: none; background: none; padding: 9px 6px; max-height: 160px;
  line-height: 20px; font-size: 14.5px;
}
.send-btn {
  width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; flex: none;
  background: var(--surface-3); color: var(--muted); transition: background .12s, color .12s;
}
.send-btn i[data-icon] { width: 18px; height: 18px; }
.composer.ready .send-btn { background: var(--accent); color: var(--on-accent); }
.attachment {
  display: flex; align-items: center; gap: 8px; margin: 2px 2px 6px; padding: 8px 8px 8px 12px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--border); font-size: 13px;
}
.attachment-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.window-closed {
  display: flex; align-items: center; gap: 16px; padding: 14px 16px; border-radius: 14px;
  background: var(--amber-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--amber) 25%, transparent);
}
.window-closed div { flex: 1; font-size: 13px; }
.window-closed strong { display: block; }
.window-closed span { color: var(--text-2); }

/* ---------- Details ---------- */
.details-pane {
  width: 330px; flex: none; display: flex; flex-direction: column; background: var(--surface); border-left: 1px solid var(--border);
}
.details-body { flex: 1; overflow-y: auto; padding: 4px 18px 24px; }
.profile { text-align: center; padding: 8px 0 18px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.profile .avatar { width: 84px; height: 84px; font-size: 28px; margin: 0 auto 12px; }
.profile-name { display: flex; align-items: center; justify-content: center; gap: 4px; font-size: 18px; font-weight: 700; }
.profile-name input { font-size: 16px; font-weight: 600; text-align: center; border: 1px solid var(--accent); border-radius: 8px; padding: 4px 8px; box-shadow: var(--ring); outline: none; background: var(--surface); width: 100%; }
.profile-number { display: inline-flex; align-items: center; gap: 4px; color: var(--text-2); margin-top: 2px; }
.profile-number i[data-icon] { width: 14px; height: 14px; }
.profile-number:hover { color: var(--text); }
.profile-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.detail-section { margin-bottom: 16px; }
.detail-section h3 { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 8px; }
.detail-card { border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; }
.detail-row { display: flex; align-items: center; gap: 12px; }
.detail-row + .detail-row { margin-top: 10px; }
.detail-row .grow { flex: 1; min-width: 0; }
.detail-row strong { display: block; font-size: 13.5px; }
.detail-row small { color: var(--text-2); font-size: 12.5px; }
.detail-row > i[data-icon] { color: var(--text-2); }

/* ================= Templates ================= */
.view-templates { overflow-y: auto; }
.page { width: 100%; max-width: 1180px; margin: 0 auto; padding: 28px 32px 48px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 24px; margin-bottom: 20px; }
.page-head h1 { font-size: 24px; font-weight: 700; }
.page-head p { color: var(--text-2); margin-top: 6px; max-width: 640px; }
.page-actions { display: flex; gap: 8px; flex: none; }
.back-link { display: inline-flex; align-items: center; gap: 4px; color: var(--text-2); text-decoration: none; font-weight: 600; font-size: 13px; margin-bottom: 10px; }
.back-link i[data-icon] { width: 16px; height: 16px; }
.back-link:hover { color: var(--text); }
.template-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.tpl-card { display: flex; flex-direction: column; padding: 16px; gap: 10px; }
.tpl-top { display: flex; align-items: flex-start; gap: 8px; }
.tpl-name { flex: 1; min-width: 0; font-weight: 700; font-size: 14.5px; overflow-wrap: anywhere; }
.tpl-meta { display: flex; flex-wrap: wrap; gap: 6px; }
.tpl-body {
  font-size: 13px; color: var(--text-2); white-space: pre-wrap; overflow-wrap: anywhere; background: var(--surface-2);
  border-radius: 10px; padding: 10px 12px; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden;
}
.tpl-reason { font-size: 12.5px; color: var(--danger); display: flex; gap: 6px; }
.tpl-reason i[data-icon] { width: 15px; height: 15px; margin-top: 1px; }
.tpl-actions { display: flex; gap: 8px; margin-top: auto; padding-top: 4px; }
.tpl-actions .btn { flex: 1; }
.tpl-actions .icon-btn { border: 1px solid var(--border); }
.tpl-actions .icon-btn:hover { color: var(--danger); background: var(--danger-soft); border-color: transparent; }
.notice-card { padding: 28px; text-align: center; color: var(--text-2); }
.notice-card strong { display: block; font-size: 15px; color: var(--text); margin-bottom: 6px; }
.notice-card code { background: var(--surface-3); padding: 2px 6px; border-radius: 6px; font-size: 12.5px; }
.skeleton { height: 210px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

.editor { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 24px; align-items: start; }
.editor-form { padding: 22px; }
.editor-preview { position: sticky; top: 20px; }
.preview-label { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 8px; }
.phone {
  border-radius: 28px; padding: 10px; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-md);
}
.phone-chat {
  min-height: 420px; border-radius: 20px; padding: 18px 14px; background-color: var(--chat-bg);
  background-image: radial-gradient(var(--chat-dot) 1.2px, transparent 1.2px); background-size: 22px 22px;
}
.phone-sm .phone-chat { min-height: 300px; }
.phone-chat .bubble { max-width: 100%; border-top-left-radius: 4px; }
.tpl-preview-buttons { display: grid; gap: 4px; margin-top: 4px; }
.tpl-preview-buttons div {
  display: flex; align-items: center; justify-content: center; gap: 6px; padding: 9px; border-radius: 10px;
  background: var(--bubble-in); color: var(--link); font-weight: 600; font-size: 13.5px; box-shadow: var(--shadow-sm);
}
.tpl-preview-buttons i[data-icon] { width: 15px; height: 15px; }
.tpl-header { font-weight: 700; margin-bottom: 3px; }
.tpl-footer { font-size: 12px; color: var(--muted); margin-top: 4px; }
.preview-empty { color: var(--muted); text-align: center; padding-top: 120px; font-size: 13px; }

.choice-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.choice { display: flex; gap: 10px; padding: 12px; border: 1px solid var(--border); border-radius: 12px; cursor: pointer; }
.choice:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.choice input { accent-color: var(--accent); margin-top: 3px; }
.choice strong { display: block; font-size: 13.5px; }
.choice small { color: var(--text-2); font-size: 12px; line-height: 1.4; display: block; margin-top: 2px; }
.examples { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 0 12px; }
.examples:empty { display: none; }
.examples .field { margin-bottom: 14px; }
.buttons-editor { display: grid; gap: 8px; }
.button-row { display: grid; grid-template-columns: 110px 1fr 1.4fr auto; gap: 8px; align-items: center; padding: 8px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); }
.button-row.no-extra { grid-template-columns: 110px 1fr auto; }
.button-row .kind { display: flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 600; color: var(--text-2); }
.button-row .kind i[data-icon] { width: 15px; height: 15px; }
.button-row input { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 7px 10px; width: 100%; min-width: 0; }
.button-row input:focus { outline: none; border-color: var(--accent); box-shadow: var(--ring); }
.button-row .url-example { grid-column: 2 / -2; }
.add-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; padding-top: 16px; border-top: 1px solid var(--border); }

/* ================= Modal ================= */
.modal {
  border: 0; padding: 0; background: none; width: min(860px, calc(100% - 32px)); max-height: calc(100% - 32px); color: var(--text);
}
.modal::backdrop { background: rgba(10, 13, 17, 0.55); backdrop-filter: blur(2px); }
.modal-card { background: var(--surface); border-radius: 18px; box-shadow: var(--shadow-lg); display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px 22px; }
.modal-head h2 { font-size: 18px; font-weight: 700; }
.modal-body { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 22px; padding: 8px 22px 6px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 22px 18px; border-top: 1px solid var(--border); }
.recipient-fixed { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; margin-bottom: 16px; }
.recipient-fixed:empty { display: none; }
.recipient-fixed .avatar { width: 36px; height: 36px; font-size: 13px; }
.recipient-fixed small { display: block; color: var(--muted); }
.var-group-title { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin: 4px 0 10px; }

/* ================= Misc ================= */
.lightbox { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 24px; background: rgba(5, 8, 11, 0.9); cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60; max-width: calc(100% - 32px);
  background: #101828; color: #fff; padding: 11px 16px; border-radius: 12px; font-size: 13.5px; box-shadow: var(--shadow-lg);
}
.toast.error { background: #b42318; }
.mobile-only { display: none; }

/* ================= Responsive ================= */
@media (max-width: 1280px) {
  .details-pane { position: fixed; top: 0; right: 0; bottom: 0; z-index: 30; box-shadow: var(--shadow-lg); }
}
@media (max-width: 1100px) {
  .ai-toggle-label span { display: none; }
  .editor { grid-template-columns: 1fr; }
  .editor-preview { position: static; }
}
@media (max-width: 860px) {
  .modal-body { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .app { flex-direction: column-reverse; }
  .rail {
    width: 100%; height: calc(60px + env(safe-area-inset-bottom)); flex-direction: row; justify-content: space-around;
    padding: 4px 4px env(safe-area-inset-bottom); border-right: 0; border-top: 1px solid var(--border);
  }
  .rail .logo, .rail-spacer { display: none; }
  .rail-btn { width: auto; flex: 1; }
  .rail-badge { right: calc(50% - 22px); }
  .view { min-height: 0; }
  .chat-list-pane { width: 100%; border-right: 0; }
  .conversation-pane { display: none; }
  .view-chats.show-convo .chat-list-pane { display: none; }
  .view-chats.show-convo .conversation-pane { display: flex; }
  body.in-convo .rail { display: none; }
  .mobile-only { display: inline-grid; }
  .convo-head { padding: 0 8px; gap: 6px; }
  .messages { padding: 12px 10px 8px; }
  .bubble { max-width: 86%; }
  .composer-wrap { padding: 4px 8px calc(8px + env(safe-area-inset-bottom)); }
  .details-pane { width: 100%; }
  .page { padding: 20px 16px 32px; }
  .page-head { flex-direction: column; gap: 14px; }
  .grid-2, .choice-row { grid-template-columns: 1fr; }
  .button-row, .button-row.no-extra { grid-template-columns: 1fr auto; }
  .button-row .kind { grid-column: 1 / -1; }
  .button-row .url-example { grid-column: 1 / -2; }
  .window-closed { flex-direction: column; align-items: stretch; text-align: center; gap: 10px; }
  .modal { width: 100%; max-width: none; max-height: 100%; height: 100%; margin: 0; }
  .modal-card { border-radius: 0; height: 100%; max-height: 100vh; }
  .template-grid { grid-template-columns: 1fr; }
}

/* ================= Fixes for generic field styles ================= */
.field .choice input { width: auto; flex: none; padding: 0; border: 0; box-shadow: none; }
.btn:disabled, .btn[disabled] { opacity: .45; cursor: not-allowed; }
.composer textarea:focus-visible { box-shadow: none; }

/* ================= Clerk ================= */
.clerk-login { display: flex; flex-direction: column; align-items: center; gap: 18px; }
.clerk-brand { display: flex; align-items: center; gap: 10px; font-size: 18px; font-weight: 700; }
.clerk-brand .logo { width: 36px; height: 36px; border-radius: 10px; }
.rail-user { width: 60px; height: 52px; display: grid; place-items: center; }
@media (max-width: 760px) { .rail-user { width: auto; flex: 1; height: 100%; } }

/* ================= Installable app ================= */
.app-banner {
  display: flex; align-items: center; gap: 10px; margin: 0 14px 10px; padding: 10px 8px 10px 12px;
  border-radius: 12px; background: var(--accent-soft); border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
}
.app-banner p { flex: 1; font-size: 12.5px; color: var(--text-2); }
.app-banner p strong { color: var(--text); }
.app-banner-icon { width: 32px; height: 32px; border-radius: 9px; flex: none; background: url("/icons/icon-192.png") center / cover; }
.share-glyph {
  display: inline-block; width: 14px; height: 16px; vertical-align: -2px; background: var(--info);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8'/%3E%3Cpath d='m16 6-4-4-4 4'/%3E%3Cpath d='M12 2v13'/%3E%3C/svg%3E") center / contain no-repeat;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8'/%3E%3Cpath d='m16 6-4-4-4 4'/%3E%3Cpath d='M12 2v13'/%3E%3C/svg%3E") center / contain no-repeat;
}
.modal-narrow { width: min(480px, calc(100% - 32px)); }
.modal-body-single { grid-template-columns: 1fr; padding-bottom: 12px; }
.choice-col { grid-template-columns: 1fr; }
.notify-status { display: flex; gap: 12px; padding: 14px; border-radius: 12px; margin-bottom: 16px; background: var(--surface-2); border: 1px solid var(--border); }
.notify-status:empty { display: none; }
.notify-status i[data-icon] { width: 22px; height: 22px; margin-top: 1px; }
.notify-status strong { display: block; }
.notify-status span { color: var(--text-2); font-size: 13px; }
.notify-status.on { background: var(--accent-soft); border-color: transparent; }
.notify-status.on i[data-icon] { color: var(--accent-text); }
.notify-status.off i[data-icon] { color: var(--amber); }
.ios-steps { margin: 0 0 8px; padding-left: 20px; display: grid; gap: 8px; color: var(--text-2); }
.ios-steps strong { color: var(--text); }
@media (max-width: 760px) {
  .modal-narrow { width: 100%; }
}
/* Installed app: keep headers clear of the notch / status bar. */
@media (display-mode: standalone) {
  .chat-list-pane .pane-head, .convo-head { padding-top: env(safe-area-inset-top); height: calc(64px + env(safe-area-inset-top)); }
  .page { padding-top: calc(20px + env(safe-area-inset-top)); }
  .modal-card { padding-top: env(safe-area-inset-top); }
}
@media (max-width: 760px) {
  /* Small dialogs become a bottom sheet instead of a full-screen page. */
  .modal.modal-narrow { inset: auto 0 0 0; height: auto; margin: 0; max-height: 90vh; }
  .modal-narrow .modal-card { height: auto; border-radius: 18px 18px 0 0; padding-bottom: env(safe-area-inset-bottom); }
}
