/* ==========================================================================
   KAOYAN 28 · Zcode Liquid Glass UI Stylesheet
   Themes: light (晴空晨雾 · 默认液态玻璃) / dark (星芒夜幕) / paper (暖纸墨韵)
   ========================================================================== */

:root,
html[data-theme="light"] {
  color-scheme: light;
  --font-ui: "SF Pro Text", -apple-system, BlinkMacSystemFont, "PingFang SC", "HarmonyOS Sans SC", "MiSans", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  --r-lg: 32px;
  --r-md: 24px;
  --r-sm: 16px;
  --r-pill: 999px;
  --ease: cubic-bezier(0.2, 0, 0, 1);

  --ink: #16202a;
  --ink-2: #44525f;
  --ink-3: #5a6875;
  --line: rgb(22 32 42 / 0.10);
  --accent: #0f7b6c;
  --accent-rgb: 15 123 108;
  --on-accent: #ffffff;
  --accent-soft: rgb(15 123 108 / 0.12);
  --accent-ring: rgb(15 123 108 / 0.36);
  --danger: #bf342a;
  --danger-rgb: 191 52 42;
  --warn: #8a5a00;
  --warn-rgb: 138 90 0;
  --ok: #1a7043;
  --ok-rgb: 26 112 67;

  --white-rgb: 255 255 255;
  --glass: rgb(var(--white-rgb) / 0.56);
  --glass-strong: rgb(var(--white-rgb) / 0.80);
  --glass-clear: rgb(var(--white-rgb) / 0.32);
  --glass-tint: rgb(22 32 42 / 0.055);
  --ring: rgb(var(--white-rgb) / 0.55);
  --rim: rgb(var(--white-rgb) / 0.95);
  --spec: rgb(var(--white-rgb) / 0.50);
  --edge-1: rgb(var(--white-rgb) / 0.14);
  --edge-2: rgb(var(--white-rgb) / 0.04);
  --edge-3: rgb(var(--white-rgb) / 0.32);
  --shadow: 0 1px 2px rgb(22 32 42 / 0.05), 0 16px 36px -12px rgb(22 32 42 / 0.22);
  --shadow-press: 0 1px 2px rgb(22 32 42 / 0.05), 0 8px 18px -10px rgb(22 32 42 / 0.22);
  --scrim: rgb(22 32 42 / 0.32);

  --wall-a: rgb(246 203 166 / 0.80);
  --wall-b: rgb(180 219 235 / 0.85);
  --wall-c: rgb(184 228 206 / 0.85);
  --wall-d: rgb(240 206 206 / 0.60);
  --wall-top: #eef1f2;
  --wall-bottom: #e2e9ec;
  --wall-art: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1600 1000'%3E%3Cg fill='none' stroke='%2316202a' stroke-width='1.5'%3E%3Cpath d='M0 700H1600' stroke-opacity='.07'/%3E%3Cpath d='M800 0V1000' stroke-opacity='.07'/%3E%3Cpath d='M240 560C400 180 560 180 720 560S1040 940 1200 560 1520 180 1600 330' stroke-opacity='.14'/%3E%3Cpath d='M420 820Q800 120 1180 820' stroke-opacity='.11'/%3E%3Cpath d='M600 470H1000' stroke-opacity='.13'/%3E%3C/g%3E%3Cg fill='%2316202a'%3E%3Ccircle cx='800' cy='470' r='5' fill-opacity='.22'/%3E%3Ccircle cx='720' cy='560' r='5' fill-opacity='.22'/%3E%3C/g%3E%3C/svg%3E");
  --code-bg: rgb(22 32 42 / 0.055);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --ink: #edf2f5;
  --ink-2: #b4bfc8;
  --ink-3: #98a6b1;
  --line: rgb(var(--white-rgb) / 0.11);
  --accent: #63d5bf;
  --accent-rgb: 99 213 191;
  --on-accent: #0b2b26;
  --accent-soft: rgb(99 213 191 / 0.14);
  --accent-ring: rgb(99 213 191 / 0.38);
  --danger: #ff8b7e;
  --danger-rgb: 255 139 126;
  --warn: #f1c355;
  --warn-rgb: 241 195 85;
  --ok: #78d9a0;
  --ok-rgb: 120 217 160;

  --glass: rgb(21 30 36 / 0.52);
  --glass-strong: rgb(19 27 33 / 0.80);
  --glass-clear: rgb(var(--white-rgb) / 0.09);
  --glass-tint: rgb(var(--white-rgb) / 0.06);
  --ring: rgb(var(--white-rgb) / 0.14);
  --rim: rgb(var(--white-rgb) / 0.28);
  --spec: rgb(var(--white-rgb) / 0.15);
  --edge-1: rgb(var(--white-rgb) / 0.06);
  --edge-2: rgb(var(--white-rgb) / 0.02);
  --edge-3: rgb(var(--white-rgb) / 0.14);
  --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 16px 36px -12px rgb(0 0 0 / 0.6);
  --shadow-press: 0 1px 2px rgb(0 0 0 / 0.3), 0 8px 18px -10px rgb(0 0 0 / 0.6);
  --scrim: rgb(0 0 0 / 0.5);

  --wall-a: rgb(84 60 32 / 0.85);
  --wall-b: rgb(18 66 74 / 0.90);
  --wall-c: rgb(22 76 58 / 0.80);
  --wall-d: rgb(84 42 48 / 0.60);
  --wall-top: #121b1f;
  --wall-bottom: #0c1316;
  --wall-art: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1600 1000'%3E%3Cg fill='none' stroke='%23ffffff' stroke-width='1.5'%3E%3Cpath d='M0 700H1600' stroke-opacity='.08'/%3E%3Cpath d='M800 0V1000' stroke-opacity='.08'/%3E%3Cpath d='M240 560C400 180 560 180 720 560S1040 940 1200 560 1520 180 1600 330' stroke-opacity='.16'/%3E%3Cpath d='M420 820Q800 120 1180 820' stroke-opacity='.12'/%3E%3Cpath d='M600 470H1000' stroke-opacity='.14'/%3E%3C/g%3E%3Cg fill='%23ffffff'%3E%3Ccircle cx='800' cy='470' r='5' fill-opacity='.26'/%3E%3Ccircle cx='720' cy='560' r='5' fill-opacity='.26'/%3E%3C/g%3E%3C/svg%3E");
  --code-bg: rgb(12 19 22 / 0.72);
}

html[data-theme="paper"] {
  color-scheme: light;
  --ink: #2b2520;
  --ink-2: #584c42;
  --ink-3: #726458;
  --line: rgb(43 37 32 / 0.11);
  --accent: #0f7b6c;
  --accent-rgb: 15 123 108;
  --on-accent: #ffffff;
  --accent-soft: rgb(15 123 108 / 0.11);
  --accent-ring: rgb(15 123 108 / 0.30);
  --danger: #b83b26;
  --danger-rgb: 184 59 38;
  --warn: #9a5b08;
  --warn-rgb: 154 91 8;
  --ok: #1a7043;
  --ok-rgb: 26 112 67;

  --glass: rgb(255 252 245 / 0.66);
  --glass-strong: rgb(255 253 249 / 0.88);
  --glass-clear: rgb(var(--white-rgb) / 0.42);
  --glass-tint: rgb(43 37 32 / 0.055);
  --ring: rgb(var(--white-rgb) / 0.65);
  --rim: rgb(var(--white-rgb) / 0.95);
  --spec: rgb(var(--white-rgb) / 0.55);
  --edge-1: rgb(var(--white-rgb) / 0.20);
  --edge-2: rgb(var(--white-rgb) / 0.05);
  --edge-3: rgb(var(--white-rgb) / 0.38);
  --shadow: 0 2px 12px rgb(43 37 32 / 0.08), 0 14px 30px -12px rgb(43 37 32 / 0.16);
  --shadow-press: 0 1px 2px rgb(43 37 32 / 0.06), 0 8px 18px -10px rgb(43 37 32 / 0.16);
  --scrim: rgb(43 37 32 / 0.36);

  --wall-a: rgb(244 214 174 / 0.72);
  --wall-b: rgb(215 224 208 / 0.75);
  --wall-c: rgb(235 215 190 / 0.75);
  --wall-d: rgb(240 218 200 / 0.65);
  --wall-top: #f8f4eb;
  --wall-bottom: #eee6d6;
  --wall-art: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1600 1000'%3E%3Cg fill='none' stroke='%232b2520' stroke-width='1.4'%3E%3Cpath d='M0 700H1600' stroke-opacity='.06'/%3E%3Cpath d='M800 0V1000' stroke-opacity='.06'/%3E%3Cpath d='M240 560C400 180 560 180 720 560S1040 940 1200 560 1520 180 1600 330' stroke-opacity='.11'/%3E%3Cpath d='M420 820Q800 120 1180 820' stroke-opacity='.09'/%3E%3C/g%3E%3C/svg%3E");
  --code-bg: rgb(43 37 32 / 0.06);
}

/* ---------- Base ---------- */
*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  overflow: hidden;
  font-family: var(--font-ui);
  font-size: 15.5px;
  line-height: 1.65;
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  background-color: var(--wall-top);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -2;
  pointer-events: none;
  background:
    radial-gradient(58% 50% at 10% 6%, var(--wall-a), transparent 70%),
    radial-gradient(54% 46% at 94% 12%, var(--wall-b), transparent 70%),
    radial-gradient(62% 56% at 80% 94%, var(--wall-c), transparent 72%),
    radial-gradient(50% 46% at 4% 92%, var(--wall-d), transparent 70%),
    linear-gradient(180deg, var(--wall-top), var(--wall-bottom));
}

body::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background-image: var(--wall-art);
  background-size: cover;
  background-position: center;
  opacity: 0.5;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
a { color: var(--accent); }
[hidden], .hidden, .chat-only-hidden { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 999px; }

/* ---------- Liquid Glass Material ---------- */
.glass {
  position: relative;
  isolation: isolate;
  background: var(--glass);
  -webkit-backdrop-filter: blur(22px) saturate(1.6);
  backdrop-filter: blur(22px) saturate(1.6);
  box-shadow: inset 0 0 0 1px var(--ring), inset 0 1px 0 var(--rim), var(--shadow);
}

.glass::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  border-radius: inherit;
  background: radial-gradient(90% 70% at 18% 0%, var(--spec), transparent 62%);
  pointer-events: none;
}

.glass-strong { background: var(--glass-strong); }
.glass-clear {
  background: var(--glass-clear);
  box-shadow: inset 0 0 0 1px var(--ring), inset 0 1px 0 var(--rim), var(--shadow-press);
}

.lens::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  padding: 1.5px;
  background: conic-gradient(from 205deg at 50% 50%, var(--rim), var(--edge-1) 26%, var(--edge-2) 48%, var(--edge-3) 74%, var(--rim));
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  mask-composite: exclude;
  pointer-events: none;
}

/* ---------- Shell (NO z-index on app-shell so fixed drawer stays above scrim!) ---------- */
.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  height: 100dvh;
  overflow: hidden;
}

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 29;
  background: var(--scrim);
}

/* ---------- Sidebar (Mobile Slide-Over Drawer by default) ---------- */
.sidebar {
  position: fixed;
  z-index: 30;
  top: max(12px, env(safe-area-inset-top));
  bottom: max(12px, env(safe-area-inset-bottom));
  left: 12px;
  width: min(86vw, 336px);
  border-radius: var(--r-lg);
  padding: 16px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  visibility: hidden;
  transform: translateX(calc(-100% - 24px));
  transition: transform 240ms var(--ease), visibility 0s linear 240ms;
}

.sidebar.open {
  visibility: visible;
  transform: none;
  transition: transform 240ms var(--ease);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 2px 4px 14px;
  flex: none;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.sidebar-brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 15px;
  display: grid;
  place-items: center;
  color: var(--ink);
  flex: none;
}

.sidebar-brand-text {
  display: grid;
  line-height: 1.25;
}

.sidebar-brand-name {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.sidebar-brand-sub {
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--ink-3);
}

.btn-new-thread-side {
  width: 100%;
  min-height: 44px;
  padding: 0 16px;
  border-radius: var(--r-pill);
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: inset 0 1px 0 rgb(var(--white-rgb) / 0.35), var(--shadow-press);
  transition: transform 100ms var(--ease), opacity 150ms var(--ease);
}

.btn-new-thread-side:active { transform: scale(0.97); }

.thread-toolbar {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.thread-search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  padding: 0 14px;
  border-radius: var(--r-pill);
  background: var(--glass-tint);
}

.thread-search-wrap:focus-within {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.search-icon { color: var(--ink-3); flex: none; }

.thread-search {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  outline: none;
  font-size: 14px;
}

.thread-search::placeholder { color: var(--ink-3); }

.filter-pills {
  display: inline-flex;
  padding: 3px;
  border-radius: var(--r-pill);
  background: var(--glass-tint);
  align-self: flex-start;
}

.filter-pill {
  min-height: 34px;
  padding: 0 13px;
  border-radius: var(--r-pill);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
  transition: background-color 150ms var(--ease), color 150ms var(--ease);
}

.filter-pill.active {
  background: var(--glass-strong);
  color: var(--ink);
  font-weight: 600;
  box-shadow: inset 0 0 0 1px var(--ring), inset 0 1px 0 var(--rim), 0 1px 2px rgb(22 32 42 / 0.10);
}

.sidebar-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin-top: 16px;
}

.sidebar-section-label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-3);
  padding: 0 4px 8px;
  flex: none;
}

.thread-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  gap: 4px;
  align-content: start;
  padding: 2px;
}

.thread-item {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 48px;
  padding: 8px 12px;
  border-radius: var(--r-sm);
  color: var(--ink-2);
  cursor: pointer;
  transition: background-color 150ms var(--ease), color 150ms var(--ease);
}

.thread-item:hover { background: var(--glass-tint); color: var(--ink); }

.thread-item.active {
  background: var(--glass-strong);
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--ring), inset 0 1px 0 var(--rim), var(--shadow-press);
}

.thread-title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.thread-subject-icon {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--glass-tint);
  color: var(--ink-2);
  flex: none;
}

.thread-title-text {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-item.active .thread-title-text { font-weight: 600; }

.thread-del {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--ink-3);
  opacity: 0.7;
  flex: none;
}

.thread-del:hover {
  opacity: 1;
  color: var(--danger);
  background: rgb(var(--danger-rgb) / 0.12);
}

.sidebar-footer {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.btn-ghost-sm {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 38px;
  padding: 0 12px;
  border-radius: var(--r-pill);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--ink-2);
}

.btn-ghost-sm:hover { background: var(--glass-tint); color: var(--ink); }

.conn-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 4px rgb(var(--ok-rgb) / 0.18);
  flex: none;
}

.conn-dot.offline {
  background: var(--danger);
  box-shadow: 0 0 0 4px rgb(var(--danger-rgb) / 0.18);
}

/* ---------- Main Pane & Floating Pill Topbar ---------- */
.main-pane {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100dvh;
}

.topbar {
  flex: none;
  width: min(calc(100% - 16px), 840px);
  margin: max(8px, env(safe-area-inset-top)) auto 4px;
  padding: 6px;
  border-radius: var(--r-pill);
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 20;
}

.btn-icon-sm {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  flex: none;
  color: var(--ink-2);
  transition: transform 100ms var(--ease), background-color 150ms var(--ease), color 150ms var(--ease);
}

.btn-icon-sm:hover { color: var(--ink); background: var(--glass-tint); }
.btn-icon-sm:active { transform: scale(0.95); }

.topbar-title-wrap {
  flex: 1;
  min-width: 0;
  display: grid;
  line-height: 1.25;
  padding-left: 2px;
}

.topbar-title-wrap h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-title-wrap small {
  font-size: 11.5px;
  color: var(--ink-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.subject-switch,
.modal-engine-tabs {
  display: inline-flex;
  padding: 3px;
  border-radius: var(--r-pill);
  background: var(--glass-tint);
  flex: none;
}

.seg-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 13px;
  border-radius: var(--r-pill);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--ink-2);
  transition: background-color 150ms var(--ease), color 150ms var(--ease);
}

.seg-btn.active {
  background: var(--glass-strong);
  color: var(--ink);
  font-weight: 600;
  box-shadow: inset 0 0 0 1px var(--ring), inset 0 1px 0 var(--rim), 0 1px 2px rgb(22 32 42 / 0.12);
}

.theme-menu-wrap { position: relative; flex: none; }

.theme-popover {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  width: 230px;
  border-radius: var(--r-md);
  padding: 8px;
  z-index: 45;
  display: grid;
  gap: 2px;
}

.popover-action-btn,
.theme-option {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  padding: 6px 10px;
  border-radius: 12px;
  text-align: left;
  font-size: 13.5px;
  color: var(--ink-2);
}

.popover-action-btn:hover,
.theme-option:hover { background: var(--glass-tint); color: var(--ink); }

.popover-action-btn { justify-content: space-between; }
.popover-action-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.popover-action-text { display: grid; line-height: 1.25; }
.popover-action-text strong { font-size: 13.5px; font-weight: 600; color: var(--ink); }
.popover-action-text small { font-size: 11.5px; color: var(--ink-3); }

.turn-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: var(--r-pill);
  background: var(--glass-tint);
  color: var(--ink-2);
}

.popover-divider { height: 1px; background: var(--line); margin: 4px 6px; }
.popover-section-label { font-size: 11px; font-weight: 600; color: var(--ink-3); padding: 4px 10px 2px; }

.theme-option.active {
  background: var(--accent-soft);
  color: var(--ink);
  font-weight: 600;
}

.theme-swatch {
  margin-left: auto;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid var(--line);
}
.swatch-light { background: #eef1f2; }
.swatch-dark { background: #121b1f; }
.swatch-paper { background: #f6f1e7; }

.sync-banner {
  width: min(calc(100% - 24px), 820px);
  margin: 4px auto;
  padding: 8px 14px;
  border-radius: var(--r-pill);
  background: rgb(var(--warn-rgb) / 0.14);
  color: var(--warn);
  font-size: 13px;
  text-align: center;
}

/* ---------- Chat Stage & Messages ---------- */
.chat-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 16px 24px;
  scrollbar-width: thin;
  scrollbar-color: var(--line) transparent;
}

.msg {
  width: min(100%, 820px);
  margin: 0 auto 20px;
}

.msg.user {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.msg.user .msg-meta { margin-bottom: 4px; }

.msg.user .msg-bubble {
  max-width: min(86%, 560px);
  padding: 12px 16px;
  border-radius: var(--r-md);
  border-bottom-right-radius: 10px;
  background: var(--glass);
  -webkit-backdrop-filter: blur(20px) saturate(1.6);
  backdrop-filter: blur(20px) saturate(1.6);
  box-shadow: inset 0 0 0 1px var(--ring), inset 0 1px 0 var(--rim), var(--shadow-press);
  overflow-wrap: anywhere;
}

.msg-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.ai-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  overflow: hidden;
  flex: none;
  display: grid;
  place-items: center;
  box-shadow: inset 0 0 0 1px var(--ring), 0 2px 6px rgb(22 32 42 / 0.16);
}

.ai-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

.msg-meta {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--ink-3);
}

.msg-engine-name { font-size: 13px; font-weight: 600; color: var(--ink); }

.msg-mode-badge {
  padding: 1px 8px;
  border-radius: var(--r-pill);
  font-size: 11px;
  font-weight: 600;
}

.msg-mode-badge.write {
  color: var(--warn);
  background: rgb(var(--warn-rgb) / 0.15);
}

.msg.assistant .msg-bubble {
  border-radius: var(--r-md);
  padding: 20px 22px;
  background: var(--glass-strong);
  -webkit-backdrop-filter: blur(22px) saturate(1.6);
  backdrop-filter: blur(22px) saturate(1.6);
  box-shadow: inset 0 0 0 1px var(--ring), inset 0 1px 0 var(--rim), var(--shadow);
  overflow-wrap: anywhere;
}

.msg-body > :first-child { margin-top: 0; }
.msg-body > :last-child { margin-bottom: 0; }
.msg-body p { margin: 0 0 12px; }
.msg-body h1, .msg-body h2, .msg-body h3, .msg-body h4 {
  margin: 18px 0 8px;
  font-weight: 600;
  line-height: 1.35;
}
.msg-body h2 { font-size: 18px; }
.msg-body h3 { font-size: 16.5px; }
.msg-body ul, .msg-body ol { margin: 0 0 12px; padding-left: 1.35em; }
.msg-body li { margin-bottom: 4px; }
.msg-body blockquote {
  margin: 10px 0;
  padding: 4px 0 4px 14px;
  border-left: 2px solid var(--accent);
  color: var(--ink-2);
}
.msg-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 14px;
  display: block;
  overflow-x: auto;
}
.msg-body th, .msg-body td {
  padding: 7px 10px;
  border: 1px solid var(--line);
  text-align: left;
}
.msg-body th { background: var(--glass-tint); font-weight: 600; }
.msg-body code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  padding: 1px 5px;
  border-radius: 6px;
  background: var(--code-bg);
}
.msg-body pre {
  position: relative;
  margin: 12px 0;
  padding: 14px;
  border-radius: var(--r-sm);
  background: var(--code-bg);
  overflow-x: auto;
  border: 1px solid var(--line);
}
.msg-body pre code { padding: 0; background: none; font-size: 13.5px; }
.btn-code-copy {
  position: absolute;
  top: 8px;
  right: 8px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border-radius: var(--r-pill);
  font-size: 11.5px;
  background: var(--glass-strong);
  color: var(--ink-2);
  box-shadow: inset 0 0 0 1px var(--ring);
}
.btn-code-copy:hover { color: var(--ink); }

.katex { font-size: 1.08em; }
.katex-display { margin: 10px 0; overflow-x: auto; overflow-y: hidden; padding: 2px 0; }

.callout {
  margin: 10px 0;
  border-radius: var(--r-sm);
  background: var(--glass-tint);
  border: 1px solid var(--line);
  overflow: hidden;
}
.callout summary {
  padding: 10px 14px;
  font-weight: 600;
  cursor: pointer;
}
.callout-body { padding: 0 14px 12px; }

.msg-attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.msg-att-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: var(--r-pill);
  background: var(--glass-tint);
  font-size: 12.5px;
}

.live-status {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 4px 12px;
  border-radius: var(--r-pill);
  background: rgb(var(--warn-rgb) / 0.14);
  color: var(--warn);
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 10px;
}
.live-status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  animation: pulseDot 1.2s ease-in-out infinite;
}
@keyframes pulseDot {
  0%, 100% { opacity: 0.35; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1.15); }
}

.notice-box {
  display: grid;
  gap: 4px;
  margin-bottom: 10px;
  padding: 10px 14px;
  border-radius: var(--r-sm);
  background: rgb(var(--warn-rgb) / 0.12);
  color: var(--warn);
  font-size: 13px;
}

.tools-box, .files-box {
  margin-top: 12px;
  border-radius: var(--r-sm);
  background: var(--glass-tint);
  border: 1px solid var(--line);
}
.tools-box summary, .files-box summary {
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-2);
  cursor: pointer;
}
.tools-box > div, .files-box > div { padding: 0 14px 12px; display: grid; gap: 6px; }
.tool-row { font-size: 12.5px; color: var(--ink-2); font-family: var(--font-mono); overflow-wrap: anywhere; }
.file-row { display: flex; align-items: center; gap: 8px; font-size: 12.5px; font-family: var(--font-mono); overflow-wrap: anywhere; }
.file-badge {
  padding: 1px 7px;
  border-radius: var(--r-pill);
  font-size: 11px;
  font-weight: 600;
  flex: none;
}
.file-badge-A { color: var(--ok); background: rgb(var(--ok-rgb) / 0.15); }
.file-badge-M { color: var(--warn); background: rgb(var(--warn-rgb) / 0.16); }
.file-badge-D { color: var(--danger); background: rgb(var(--danger-rgb) / 0.15); }

.msg-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.btn-msg-copy {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 32px;
  padding: 0 12px;
  border-radius: var(--r-pill);
  font-size: 12.5px;
  color: var(--ink-3);
}
.btn-msg-copy:hover { background: var(--glass-tint); color: var(--ink); }

.retry-bar { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.btn-retry-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 38px;
  padding: 0 14px;
  border-radius: var(--r-pill);
  font-size: 13px;
  font-weight: 600;
  background: var(--glass-strong);
  box-shadow: inset 0 0 0 1px var(--ring), var(--shadow-press);
}
.btn-retry-pill.primary { background: var(--accent); color: var(--on-accent); }

/* ---------- Empty State (Zcode Calm Centered Layout) ---------- */
.main-pane.is-empty .messages { display: none; }
.main-pane.is-empty .chat-stage { overflow-y: auto; padding: 12px 16px; }
.main-pane.is-empty .composer-dock {
  margin: auto;
  width: min(100%, 760px);
  display: grid;
  gap: 18px;
}
.main-pane.is-empty .dock-chips,
.main-pane.is-empty .btn-scroll-bottom { display: none !important; }
.main-pane:not(.is-empty) .welcome-hero,
.main-pane:not(.is-empty) .empty-shortcuts { display: none !important; }

.welcome-hero {
  text-align: center;
  padding: 8px 12px 0;
}
.welcome-badge {
  width: 64px;
  height: 64px;
  margin: 0 auto 16px;
  border-radius: 22px;
  display: grid;
  place-items: center;
  color: var(--accent);
}
.welcome-hero h1 {
  margin: 0 0 10px;
  font-size: clamp(24px, 4vw, 31px);
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.2;
}
.welcome-hero p {
  margin: 0 auto;
  max-width: 30em;
  font-size: 15px;
  color: var(--ink-2);
}

.empty-shortcuts {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.empty-shortcut-pill {
  display: grid;
  align-content: start;
  gap: 6px;
  min-height: 104px;
  padding: 16px;
  border-radius: var(--r-md);
  text-align: left;
  transition: transform 120ms var(--ease), background-color 150ms var(--ease);
}
.empty-shortcut-pill:hover { background: var(--glass-strong); transform: translateY(-1px); }
.empty-shortcut-pill:active { transform: scale(0.97); }
.sc-icon { color: var(--accent); display: inline-flex; }
.sc-text { display: grid; gap: 2px; }
.sc-title { font-size: 15px; font-weight: 600; }
.sc-desc { font-size: 13px; color: var(--ink-2); }

/* ---------- Composer Dock ---------- */
.composer-dock { flex: none; }

.composer-wrap {
  position: relative;
  width: min(100%, 844px);
  margin: 0 auto;
  padding: 4px 12px max(12px, env(safe-area-inset-bottom));
}

.main-pane.is-empty .composer-wrap { padding: 0; }

.btn-scroll-bottom {
  position: absolute;
  right: 18px;
  bottom: calc(100% + 4px);
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  z-index: 15;
}

.dock-chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 2px 4px 8px;
  scrollbar-width: none;
  -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 24px), transparent);
  mask-image: linear-gradient(90deg, #000 calc(100% - 24px), transparent);
}
.dock-chips::-webkit-scrollbar { display: none; }

.dock-chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 13px;
  border-radius: var(--r-pill);
  font-size: 13.5px;
  white-space: nowrap;
  color: var(--ink);
  transition: transform 100ms var(--ease), background-color 150ms var(--ease);
}
.dock-chip:hover { background: var(--glass-strong); }
.dock-chip:active { transform: scale(0.96); }

.composer-card {
  border-radius: var(--r-lg);
  padding: 8px;
}

.attachment-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px 2px;
}
.attachment-items { display: flex; flex-wrap: wrap; gap: 8px; flex: 1; min-width: 0; }

.att-preview {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px 4px 4px;
  border-radius: var(--r-pill);
  background: var(--glass-tint);
  max-width: 100%;
}
.att-thumb-wrap {
  position: relative;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  overflow: hidden;
  display: grid;
  place-items: center;
  background: var(--glass-strong);
  flex: none;
}
.att-thumb-wrap img { width: 100%; height: 100%; object-fit: cover; }
.att-ring { position: absolute; inset: 0; border-radius: 50%; border: 2px solid var(--accent); opacity: 0.7; }
.att-info { display: grid; min-width: 0; line-height: 1.2; }
.att-name { font-size: 12px; font-weight: 500; max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att-meta { font-size: 10.5px; color: var(--ink-3); }
.att-meta.ok { color: var(--ok); }
.att-meta.err { color: var(--danger); }
.att-remove { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; color: var(--ink-3); }
.att-remove:hover { background: var(--line); color: var(--ink); }

.quality-pill {
  flex: none;
  padding: 4px 10px;
  border-radius: var(--r-pill);
  background: var(--glass-tint);
  font-size: 11.5px;
  font-weight: 600;
  color: var(--ink-2);
}

.composer-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#prompt-input {
  flex: 1;
  min-width: 0;
  max-height: 168px;
  padding: 11px 14px;
  border: 0;
  background: none;
  outline: none;
  resize: none;
  font-size: 16px;
  line-height: 1.5;
}
#prompt-input::placeholder { color: var(--ink-3); }

.composer-send-col { flex: none; display: flex; padding: 0 4px 4px 0; }

.btn-send, .btn-stop {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  transition: transform 100ms var(--ease);
}
.btn-send {
  background: var(--accent);
  color: var(--on-accent);
  box-shadow: inset 0 1px 0 rgb(var(--white-rgb) / 0.35), var(--shadow-press);
}
.btn-send:active, .btn-stop:active { transform: scale(0.95); }
.btn-stop { color: var(--danger); }

.composer-toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 4px 2px 6px;
}

.btn-tool-icon {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  color: var(--ink-2);
  cursor: pointer;
}
.btn-tool-icon:hover { color: var(--ink); background: var(--glass-tint); }

.model-trigger, .mode-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 12px;
  border-radius: var(--r-pill);
  font-size: 12.5px;
  color: var(--ink-2);
}
.model-trigger { max-width: 46vw; }
.model-trigger:hover, .mode-toggle:hover { background: var(--glass-tint); color: var(--ink); }
.model-trigger > span:not(.engine-dot) { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.engine-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-3); flex: none; }
.engine-dot.agy { background: var(--accent); }

.mode-toggle { margin-left: auto; font-weight: 600; color: var(--ink); }
.mode-toggle.write { color: var(--warn); background: rgb(var(--warn-rgb) / 0.14); }

.slash-menu {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: calc(100% + 6px);
  max-height: 55dvh;
  overflow-y: auto;
  padding: 8px;
  border-radius: var(--r-md);
  z-index: 45;
  display: grid;
  gap: 2px;
}
.main-pane.is-empty .slash-menu { left: 0; right: 0; }

.slash-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 6px 10px;
  border-radius: 12px;
  text-align: left;
}
.slash-item:hover, .slash-item.active { background: var(--glass-tint); }
.slash-icon {
  width: 30px;
  height: 30px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: var(--glass-tint);
  color: var(--accent);
  flex: none;
}
.slash-main { flex: 1; min-width: 0; display: grid; }
.slash-title-row { display: flex; align-items: center; gap: 8px; }
.slash-label { font-size: 13.5px; font-weight: 600; }
.slash-cmd { font-family: var(--font-mono); font-size: 12px; color: var(--accent); }
.slash-desc { font-size: 12px; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.slash-empty { padding: 12px; font-size: 13px; color: var(--ink-3); text-align: center; }

/* ---------- Modals, Sheets & Toast ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 16px;
  background: var(--scrim);
}

.sheet-overlay { align-items: end; padding: 12px; }

.modal-card {
  width: min(100%, 420px);
  border-radius: var(--r-lg);
  padding: 22px;
}

.login-card { display: grid; gap: 12px; text-align: center; }
.login-emblem { width: 58px; height: 58px; margin: 0 auto 4px; border-radius: 20px; display: grid; place-items: center; color: var(--accent); }
.login-card h1 { margin: 0; font-size: 22px; font-weight: 600; }
.modal-sub { margin: 0; font-size: 13.5px; color: var(--ink-2); }
.login-card input {
  width: 100%;
  min-height: 46px;
  padding: 0 14px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line);
  background: var(--glass-tint);
  outline: none;
  font-size: 15px;
}
.login-card input:focus { border-color: var(--accent); }
.login-error { font-size: 13px; color: var(--danger); min-height: 18px; }

.btn-accent-primary, .btn-write-primary, .btn-write-secondary, .btn-write-cancel {
  min-height: 46px;
  padding: 10px 16px;
  border-radius: var(--r-pill);
  font-size: 14.5px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn-accent-primary, .btn-write-primary {
  background: var(--accent);
  color: var(--on-accent);
  box-shadow: inset 0 1px 0 rgb(var(--white-rgb) / 0.35), var(--shadow-press);
}
.btn-write-secondary {
  background: var(--glass-tint);
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--ring);
}
.btn-write-cancel { color: var(--ink-2); }
.btn-write-primary, .btn-write-secondary { justify-content: flex-start; text-align: left; border-radius: var(--r-sm); }
.action-btn-copy { display: grid; line-height: 1.3; }
.action-btn-copy small { font-size: 12px; font-weight: 400; opacity: 0.85; }

.sheet-handle { width: 38px; height: 4px; border-radius: 999px; background: var(--line); margin: 0 auto 12px; }
.sheet-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.modal-title-row { margin: 0; display: flex; align-items: center; gap: 8px; font-size: 17.5px; font-weight: 600; }
.sheet-sub { margin: 2px 0 0; font-size: 12.5px; color: var(--ink-3); }
.modal-actions { display: grid; gap: 8px; margin-top: 12px; }
.modal-actions-row { grid-template-columns: 1fr 1fr; }

.model-sheet-card { width: min(100%, 500px); max-height: 82dvh; display: flex; flex-direction: column; }
.modal-engine-tabs { margin-bottom: 12px; align-self: flex-start; }
.sheet-scroll { flex: 1; min-height: 0; overflow-y: auto; display: grid; gap: 12px; padding-right: 2px; }
.sheet-section-title { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; color: var(--ink-3); }

.model-rec-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.model-rec-card, .model-option-btn {
  display: grid;
  gap: 2px;
  padding: 10px 12px;
  border-radius: var(--r-sm);
  background: var(--glass-tint);
  text-align: left;
  transition: background-color 140ms var(--ease);
}
.model-rec-card:hover, .model-option-btn:hover { background: var(--glass); }
.model-rec-card.active, .model-option-btn.active {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1.5px var(--accent);
}
.model-rec-title { font-size: 13px; font-weight: 600; }
.model-rec-id { font-size: 11px; color: var(--ink-3); font-family: var(--font-mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.model-groups { display: grid; gap: 12px; }
.model-group-name { font-size: 12px; font-weight: 600; color: var(--ink-3); margin-bottom: 6px; }
.model-option-list { display: grid; gap: 6px; }

.status-body { display: grid; gap: 8px; font-size: 13.5px; }
.status-row { display: flex; justify-content: space-between; gap: 12px; padding: 8px 12px; border-radius: 12px; background: var(--glass-tint); }
.status-label { color: var(--ink-3); }
.status-ok { color: var(--ok); font-weight: 600; }
.status-bad { color: var(--danger); font-weight: 600; }

.toast {
  position: fixed;
  left: 50%;
  top: calc(max(8px, env(safe-area-inset-top)) + 74px);
  transform: translateX(-50%);
  z-index: 70;
  max-width: calc(100vw - 32px);
  padding: 10px 18px;
  border-radius: var(--r-pill);
  font-size: 13.5px;
  text-align: center;
  pointer-events: none;
}

/* ---------- Responsive: Phone (<480px) & Desktop (>=840px) ---------- */
@media (max-width: 479px) {
  .topbar { width: calc(100% - 12px); gap: 4px; }
  .seg-btn { padding: 0 10px; min-height: 34px; font-size: 13px; }
  .seg-btn svg { display: none; }
  .messages { padding: 10px 12px 24px; }
  .msg.assistant .msg-bubble { padding: 16px; }
  .msg.user .msg-bubble { max-width: 92%; }
  .empty-shortcuts { grid-template-columns: 1fr 1fr; gap: 8px; }
  .empty-shortcut-pill { min-height: 92px; padding: 13px; }
  .model-rec-grid { grid-template-columns: 1fr; }
  .model-trigger { max-width: 42vw; padding: 0 8px; }
}

@media (min-width: 840px) {
  .app-shell {
    grid-template-columns: 300px minmax(0, 1fr);
    gap: 12px;
    padding: 12px;
    transition: grid-template-columns 200ms var(--ease), gap 200ms var(--ease);
  }
  body.sidebar-collapsed .app-shell {
    grid-template-columns: 0px minmax(0, 1fr);
    gap: 0;
  }
  .sidebar {
    position: relative;
    top: auto;
    bottom: auto;
    left: auto;
    width: 300px;
    visibility: visible;
    transform: none;
    z-index: 20;
    transition: transform 200ms var(--ease), opacity 200ms var(--ease);
  }
  body.sidebar-collapsed .sidebar {
    transform: translateX(-110%);
    opacity: 0;
    pointer-events: none;
  }
  .btn-drawer-close, .drawer-backdrop, .sheet-handle { display: none !important; }
  .topbar { width: min(100%, 840px); margin-top: 0; }
  .sheet-overlay { align-items: center; padding: 16px; }
  .model-option-list { grid-template-columns: 1fr 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
