/* SparkAsk — iOS-inspired design.
 *
 * Palette mirrors Apple's grayscale (light: f5f5f7 / fff / 1d1d1f,
 * dark: 0a0a0d / 1c1c1e / f5f5f7). Brand accent: warm peach #c87850.
 * Typography: SF Pro Text/Display + PingFang SC; mono only for code.
 */

:root {
  color-scheme: light dark;

  /* Light theme */
  --bg:               #f5f5f7;
  --surface:          #ffffff;
  --surface-elev:     #ffffff;
  --surface-sunken:   #ebebef;
  --border:           rgba(0, 0, 0, 0.08);
  --border-strong:    rgba(0, 0, 0, 0.14);
  --text:             #1d1d1f;
  --text-secondary:   #6e6e73;
  --text-tertiary:    #a1a1a6;
  --accent:           #c87850;
  --accent-strong:    #b46a45;
  --accent-soft:      rgba(200, 120, 80, 0.10);
  --accent-fg:        #ffffff;
  --user-bubble:      #ebebef;
  --user-bubble-text: #1d1d1f;
  --code-bg:          #f5f5f7;
  --code-border:      rgba(0, 0, 0, 0.06);
  --danger:           #d70015;
  --link:             #006edc;
  --shadow-sm:        0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow:           0 8px 28px rgba(0, 0, 0, 0.08);
  --shadow-lg:        0 12px 40px rgba(0, 0, 0, 0.12);
  --topbar-blur:      saturate(180%) blur(20px);
  --topbar-bg:        rgba(245, 245, 247, 0.78);

  --radius-sm:        8px;
  --radius:           12px;
  --radius-lg:        18px;
  --radius-xl:        22px;
  --header-h:         56px;
  --sidebar-w:        272px;

  --font-system: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
                 "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
                 "Helvetica Neue", Helvetica, Arial, system-ui, sans-serif;
  --font-mono:   ui-monospace, "SF Mono", "JetBrains Mono", "Fira Code",
                 Menlo, Consolas, monospace;
}

/* Dark palette. Applied two ways:
 *   • system, when no explicit theme is chosen  → :root:not([data-theme])
 *   • forced via Settings                       → :root[data-theme="dark"]
 * (Forced "light" needs nothing extra — the base :root above is light, and the
 *  media rule is scoped to :not([data-theme]) so it won't fight an explicit
 *  choice.) The shared declarations live in one place to avoid drift. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg:               #0a0a0d;
    --surface:          #1c1c1e;
    --surface-elev:     #2c2c2e;
    --surface-sunken:   #131316;
    --border:           rgba(255, 255, 255, 0.08);
    --border-strong:    rgba(255, 255, 255, 0.16);
    --text:             #f5f5f7;
    --text-secondary:   #98989d;
    --text-tertiary:    #6e6e73;
    --accent:           #e08a64;
    --accent-strong:    #ed9974;
    --accent-soft:      rgba(224, 138, 100, 0.16);
    --accent-fg:        #1c1c1e;
    --user-bubble:      #2c2c2e;
    --user-bubble-text: #f5f5f7;
    --code-bg:          #131316;
    --code-border:      rgba(255, 255, 255, 0.08);
    --danger:           #ff453a;
    --link:             #64a4ff;
    --shadow-sm:        0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow:           0 8px 28px rgba(0, 0, 0, 0.5);
    --shadow-lg:        0 12px 40px rgba(0, 0, 0, 0.6);
    --topbar-bg:        rgba(10, 10, 13, 0.72);
  }
}
:root[data-theme="dark"] {
  --bg:               #0a0a0d;
  --surface:          #1c1c1e;
  --surface-elev:     #2c2c2e;
  --surface-sunken:   #131316;
  --border:           rgba(255, 255, 255, 0.08);
  --border-strong:    rgba(255, 255, 255, 0.16);
  --text:             #f5f5f7;
  --text-secondary:   #98989d;
  --text-tertiary:    #6e6e73;
  --accent:           #e08a64;
  --accent-strong:    #ed9974;
  --accent-soft:      rgba(224, 138, 100, 0.16);
  --accent-fg:        #1c1c1e;
  --user-bubble:      #2c2c2e;
  --user-bubble-text: #f5f5f7;
  --code-bg:          #131316;
  --code-border:      rgba(255, 255, 255, 0.08);
  --danger:           #ff453a;
  --link:             #64a4ff;
  --shadow-sm:        0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow:           0 8px 28px rgba(0, 0, 0, 0.5);
  --shadow-lg:        0 12px 40px rgba(0, 0, 0, 0.6);
  --topbar-bg:        rgba(10, 10, 13, 0.72);
}
:root[data-theme="dark"]  { color-scheme: dark; }
:root[data-theme="light"] { color-scheme: light; }

/* Accent presets. Peach is the default (baked into the base palette + the dark
 * theme block above, so it needs no attribute). The alternates are a muted,
 * earthy set chosen to harmonise with the warm grayscale — no neon. Each has a
 * LIGHT value here and a brighter DARK value below (forced + system dark), with
 * dark text on the accent in dark mode (matching peach). These rules come after
 * the theme blocks so they win at equal specificity. */
:root[data-accent="rose"]   { --accent: #c2726f; --accent-strong: #ad5f5c; --accent-soft: rgba(194, 114, 111, 0.12); --accent-fg: #ffffff; --link: #c2726f; }
:root[data-accent="amber"]  { --accent: #bd9148; --accent-strong: #a67c38; --accent-soft: rgba(189, 145, 72, 0.14);  --accent-fg: #ffffff; --link: #a67c38; }
:root[data-accent="sage"]   { --accent: #7f9a63; --accent-strong: #6b8551; --accent-soft: rgba(127, 154, 99, 0.14);  --accent-fg: #ffffff; --link: #6b8551; }
:root[data-accent="teal"]   { --accent: #4f9a93; --accent-strong: #3d847d; --accent-soft: rgba(79, 154, 147, 0.14);  --accent-fg: #ffffff; --link: #3d847d; }
:root[data-accent="indigo"] { --accent: #6885b3; --accent-strong: #52709c; --accent-soft: rgba(104, 133, 179, 0.14); --accent-fg: #ffffff; --link: #52709c; }

/* Dark variants — forced (data-theme="dark"). Brighter accent + dark text. */
:root[data-theme="dark"][data-accent="rose"]   { --accent: #dd9090; --accent-strong: #e8a6a6; --accent-soft: rgba(221, 144, 144, 0.16); --accent-fg: #1c1c1e; --link: #dd9090; }
:root[data-theme="dark"][data-accent="amber"]  { --accent: #d8ad63; --accent-strong: #e6bd78; --accent-soft: rgba(216, 173, 99, 0.18);  --accent-fg: #1c1c1e; --link: #d8ad63; }
:root[data-theme="dark"][data-accent="sage"]   { --accent: #a6bf82; --accent-strong: #b6cd93; --accent-soft: rgba(166, 191, 130, 0.18); --accent-fg: #1c1c1e; --link: #a6bf82; }
:root[data-theme="dark"][data-accent="teal"]   { --accent: #6fbab1; --accent-strong: #85c9c0; --accent-soft: rgba(111, 186, 177, 0.18); --accent-fg: #1c1c1e; --link: #6fbab1; }
:root[data-theme="dark"][data-accent="indigo"] { --accent: #8ca5d6; --accent-strong: #a1b7e2; --accent-soft: rgba(140, 165, 214, 0.18); --accent-fg: #1c1c1e; --link: #8ca5d6; }

/* Dark variants — system (theme=system, OS is dark). Same values. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme])[data-accent="rose"]   { --accent: #dd9090; --accent-strong: #e8a6a6; --accent-soft: rgba(221, 144, 144, 0.16); --accent-fg: #1c1c1e; --link: #dd9090; }
  :root:not([data-theme])[data-accent="amber"]  { --accent: #d8ad63; --accent-strong: #e6bd78; --accent-soft: rgba(216, 173, 99, 0.18);  --accent-fg: #1c1c1e; --link: #d8ad63; }
  :root:not([data-theme])[data-accent="sage"]   { --accent: #a6bf82; --accent-strong: #b6cd93; --accent-soft: rgba(166, 191, 130, 0.18); --accent-fg: #1c1c1e; --link: #a6bf82; }
  :root:not([data-theme])[data-accent="teal"]   { --accent: #6fbab1; --accent-strong: #85c9c0; --accent-soft: rgba(111, 186, 177, 0.18); --accent-fg: #1c1c1e; --link: #6fbab1; }
  :root:not([data-theme])[data-accent="indigo"] { --accent: #8ca5d6; --accent-strong: #a1b7e2; --accent-soft: rgba(140, 165, 214, 0.18); --accent-fg: #1c1c1e; --link: #8ca5d6; }
}

/* Font-size preference — scales the conversation text (what matters for
 * reading); chrome stays fixed. */
[data-font="sm"] .message-content { font-size: 14px; }
[data-font="lg"] .message-content { font-size: 18px; }
[data-font="sm"] .message.user .message-content { font-size: 14px; }
[data-font="lg"] .message.user .message-content { font-size: 18px; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-system);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}
/* Only Latin text gets the SF Pro negative tracking — CJK shouldn't be
 * compressed (it already has its own visually-optical metrics). */
:lang(en) { letter-spacing: -0.011em; }

body {
  /* iOS safe-area awareness for PWA / Add-to-Home */
  padding: 0;
  /* Lock momentum scrolling outside scroll containers */
  overscroll-behavior: none;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

button { font: inherit; color: inherit; cursor: pointer; background: none; border: 0; }
button:disabled { cursor: not-allowed; opacity: 0.55; }
input, textarea, select { font: inherit; color: inherit; }

::selection { background: var(--accent-soft); color: var(--text); }

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ============================================================
   Login
   ============================================================ */
.login-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(200, 120, 80, 0.18), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(100, 164, 255, 0.10), transparent 60%),
    var(--bg);
  padding: 32px 24px;
  padding-top: max(32px, env(safe-area-inset-top));
  padding-bottom: max(32px, env(safe-area-inset-bottom));
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 36px 32px 28px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow);
  text-align: center;
}

.login-mark {
  width: 56px; height: 56px;
  border-radius: 14px;
  box-shadow: var(--shadow-sm);
  margin: 0 auto 14px;
  display: block;
}

.login-title {
  margin: 0 0 4px;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.022em;
  color: var(--text);
}

.login-subtitle {
  margin: 0 0 22px;
  color: var(--text-secondary);
  font-size: 14px;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#login-form input[type="password"] {
  padding: 13px 14px;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  background: var(--surface-sunken);
  outline: none;
  font-size: 16px; /* prevents iOS auto-zoom */
  transition: border-color 0.15s, background-color 0.15s;
}
#login-form input[type="password"]:focus {
  border-color: var(--accent);
  background: var(--surface);
}

#login-form button[type="submit"],
.btn-primary {
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.005em;
  transition: filter 0.15s, transform 0.06s;
}
#login-form button[type="submit"]:hover:not(:disabled),
.btn-primary:hover:not(:disabled) { filter: brightness(1.04); }
#login-form button[type="submit"]:active:not(:disabled),
.btn-primary:active:not(:disabled) { transform: scale(0.98); }

.login-error {
  margin: 4px 0 0;
  color: var(--danger);
  font-size: 13px;
  min-height: 18px;
}

/* ============================================================
   App shell
   ============================================================ */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;          /* fallback for old WebViews (WeChat/QQ in-app) */
  height: 100dvh;
  overflow: hidden;
}
.app.sidebar-collapsed { grid-template-columns: 0 1fr; }
.app.sidebar-collapsed .sidebar { transform: translateX(-100%); }

/* ----- Sidebar ----- */
.sidebar {
  background: var(--surface-sunken);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform 0.22s cubic-bezier(0.32, 0.72, 0, 1);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.sidebar-header {
  padding: 14px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 2px 4px 4px;
}
.brand-mark {
  width: 28px; height: 28px;
  border-radius: 7px;
  box-shadow: var(--shadow-sm);
}
.brand-name {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.018em;
  color: var(--text);
}

#new-session-btn {
  width: 100%;
  padding: 9px 12px;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.session-search input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 13.5px;
  outline: none;
  transition: border-color 0.12s, box-shadow 0.12s, background-color 0.12s;
}
.session-search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.session-search input::placeholder { color: var(--text-tertiary); }

.session-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
  scrollbar-width: thin;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 9px 11px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 14px;
  transition: background-color 0.12s, color 0.12s;
  margin-bottom: 1px;
  position: relative;
}
.session-item:hover { background: rgba(127, 127, 127, 0.08); color: var(--text); }
.session-item.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.session-item .session-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: -0.005em;
}
.session-item .session-delete {
  opacity: 0;
  padding: 2px 6px;
  border-radius: 6px;
  color: var(--text-tertiary);
  font-size: 16px;
  line-height: 1;
  transition: opacity 0.12s, color 0.12s, background-color 0.12s;
}
.session-item:hover .session-delete,
.session-item.active .session-delete { opacity: 1; }
.session-item .session-delete:hover {
  color: var(--danger);
  background: rgba(215, 0, 21, 0.08);
}

.sidebar-footer {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.btn-ghost {
  width: 100%;
  padding: 9px 11px;
  color: var(--text-secondary);
  border-radius: 10px;
  font-size: 14px;
  text-align: left;
  transition: background-color 0.12s, color 0.12s;
}
.btn-ghost:hover { background: rgba(127, 127, 127, 0.08); color: var(--text); }

/* ----- Main column ----- */
.main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;          /* fallback for old WebViews (WeChat/QQ in-app) */
  height: 100dvh;
  background: var(--bg);
}

/* Outer .topbar owns the safe-area inset (so its tinted background extends
 * up under the iOS status bar / Dynamic Island and content sits below).
 * The inner row owns the flex layout — mobile media queries override the
 * inner padding without ever touching the safe-area inset on the outer. */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--topbar-bg);
  backdrop-filter: var(--topbar-blur);
  -webkit-backdrop-filter: var(--topbar-blur);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
  padding-top: env(safe-area-inset-top);
}
/* Without backdrop-filter (old WebViews) the 0.78-alpha bar lets content
 * bleed through. Make it opaque so the sticky header stays legible. */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .topbar { background: var(--bg); }
}

/* iOS Safari (browser tab) regression: env(safe-area-inset-top) is 0 even
 * when the URL bar collapses on scroll and the iOS status bar / Dynamic
 * Island overlays our content. JS adds .ios-browser when we detect this,
 * and we reserve a 44pt fallback so the topbar content always clears the
 * status bar. PWA / standalone uses the real env() value above. */
.ios-browser .topbar { padding-top: max(env(safe-area-inset-top), 44px); }
.topbar-inner {
  min-height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
}

.topbar-title {
  flex: 1;
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.012em;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 7px;
  border-radius: 8px;
  color: var(--text-secondary);
  transition: background-color 0.12s, color 0.12s;
}
.icon-btn:hover { background: rgba(127, 127, 127, 0.10); color: var(--text); }
.icon-btn:active { transform: scale(0.96); }
/* The `display: inline-flex` above beats the UA stylesheet's [hidden] rule, so
 * `btn.hidden = true` wouldn't actually hide an icon button without this. Used
 * to hide the attach button when a text-only model is selected. */
.icon-btn[hidden] { display: none !important; }
.mobile-new-session,
.mobile-history { display: none; }

/* Segmented control — 中 / EN. Two pill-shaped buttons inside a tighter pill
 * container; the active one gets the accent fill. WAI-ARIA radiogroup pattern. */
.lang-switch {
  display: inline-flex;
  align-items: stretch;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}
.lang-switch button {
  padding: 4px 11px;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.15;
  border-radius: 999px;
  color: var(--text-secondary);
  min-width: 30px;
  text-align: center;
  transition: background-color 0.16s, color 0.16s;
  cursor: pointer;
}
.lang-switch button:hover:not(.active) { color: var(--text); }
.lang-switch button.active {
  background: var(--accent);
  color: var(--accent-fg);
}
.lang-switch button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.login-lang-switch {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  right: 16px;
  z-index: 10;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

/* min-width:0 lets this flex item actually shrink — flex items default to
   min-width:auto (won't shrink below content's intrinsic min-content size),
   so a <select> whose longest option is long ("✦ Sparkers (4 顾问)") was
   forcing the whole topbar to overflow the viewport on narrow phones. */
.model-select { position: relative; min-width: 0; }
.model-select select {
  padding: 5px 28px 5px 12px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text);
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 24 24'><path fill='none' stroke='%236e6e73' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round' d='M6 9l6 6 6-6'/></svg>");
  background-repeat: no-repeat;
  background-position: right 9px center;
  cursor: pointer;
  transition: border-color 0.12s;
  max-width: 108px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.model-select select:hover { border-color: var(--accent); }

.network-status {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 7px 14px;
  background: color-mix(in srgb, var(--danger) 10%, var(--surface));
  border-bottom: 1px solid color-mix(in srgb, var(--danger) 28%, var(--border));
  color: var(--danger);
  font-size: 13px;
  font-weight: 500;
}
.network-status[hidden] { display: none; }
.network-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--danger);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 15%, transparent);
}

/* ============================================================
   Messages
   ============================================================ */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0 12px;
  /* No CSS smooth-scroll: our JS does instant scrollTo({behavior:"auto"}) to
   * keep up with streaming. iOS Safari can ignore the per-call "auto" and
   * animate anyway when this is set, and the mid-animation scroll events then
   * flip autoScrollPinned off — so the reply stops auto-following. */
  -webkit-overflow-scrolling: touch;
  overflow-anchor: none;
}

.jump-latest {
  position: absolute;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 12;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 7px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  color: var(--text-secondary);
  box-shadow: var(--shadow-lg);
  font-size: 13px;
  font-weight: 500;
  transition: color 0.12s, border-color 0.12s, transform 0.12s;
}
.jump-latest:hover {
  color: var(--accent);
  border-color: var(--accent);
  transform: translateX(-50%) translateY(-1px);
}
.jump-latest[hidden] { display: none; }
.jump-latest svg { flex-shrink: 0; }

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  margin: 14vh auto 0;
  max-width: 540px;
  padding: 0 24px;
}
.empty-mark {
  width: 72px; height: 72px;
  border-radius: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}
.empty-headline {
  margin: 0 0 6px;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.026em;
  color: var(--text);
}
.empty-tagline {
  margin: 0 0 18px;
  color: var(--text-secondary);
  font-size: 15px;
  line-height: 1.5;
}
.empty-meta {
  margin: 0 0 6px;
  color: var(--text-tertiary);
  font-size: 12.5px;
  line-height: 1.6;
}
.empty-meta code {
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  margin: 0 1px;
}

/* ----- A single message ----- */
.message {
  max-width: 760px;
  margin: 0 auto 4px;
  padding: 10px 24px;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-start;
}

/* User: right-aligned bubble, no avatar */
.message.user {
  justify-content: flex-end;
}
.message.user .message-prefix { display: none; }
.message.user .message-body {
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  padding: 11px 16px;
  border-radius: 22px 22px 6px 22px;
  max-width: 80%;
  display: inline-block;
  box-shadow: var(--shadow-sm);
}
.message.user .message-content {
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  font-size: 15px;
  line-height: 1.5;
}

/* Assistant: avatar + flowing markdown, no bubble */
.message.assistant {
  margin-bottom: 16px;
}
.message.assistant .message-body {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  flex: 1;
  min-width: 0;
  max-width: 100%;
}
.message.assistant .message-prefix {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  flex-shrink: 0;
  font-size: 0;
  background: linear-gradient(155deg, #f2b289, #c6744f);
  box-shadow: var(--shadow-sm);
  position: relative;
  margin-top: 2px;
}
.message.assistant .message-prefix::before {
  /* A symmetric SVG sparkle instead of the "✦" text glyph: the glyph's ink
   * isn't vertically centred in the font's line-box (it rendered low / looked
   * off), whereas this path is symmetric about both axes, so it's always
   * perfectly centred regardless of font or OS. */
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 1.5C12.6 8.2 15.8 11.4 22.5 12 15.8 12.6 12.6 15.8 12 22.5 11.4 15.8 8.2 12.6 1.5 12 8.2 11.4 11.4 8.2 12 1.5Z' fill='%23ffffff'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: center;
  background-size: 16px 16px;
}
.message.assistant .message-body > .message-content {
  flex: 1;
  min-width: 0;
  padding-top: 3px;
}

.message-actions {
  width: 100%;
  display: flex;
  gap: 6px;
}
.message.user .message-actions {
  justify-content: flex-end;
  padding-right: 4px;
}
.message.assistant .message-actions {
  padding-left: 40px;
}
.message-action {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 28px;
  padding: 4px 8px;
  border-radius: 8px;
  color: var(--text-tertiary);
  font-size: 12px;
  line-height: 1;
  transition: background-color 0.12s, color 0.12s;
}
.message-action:hover {
  background: rgba(127, 127, 127, 0.10);
  color: var(--text);
}
.message-action svg { flex-shrink: 0; }

/* TTS button — swap play/stop SVGs via the .speaking class so the icon
 * matches the actual state (driven by the ccw:tts event in app.js). */
.tts-btn .tts-icon-stop { display: none; }
.tts-btn.speaking { color: var(--accent); }
.tts-btn.speaking .tts-icon-play { display: none; }
.tts-btn.speaking .tts-icon-stop { display: inline-block; }

/* System messages: small italic gray, full-width */
.message.system {
  color: var(--text-secondary);
  font-size: 13px;
  font-style: italic;
}
.message.system .message-prefix {
  color: var(--text-tertiary);
  display: inline;
  background: none;
  width: auto;
  height: auto;
  box-shadow: none;
  font-style: normal;
  margin-right: 4px;
}
.message.system .message-prefix::before { content: none; }

/* ----- Message content (markdown) ----- */
.message-content {
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.message-content > * { margin: 0 0 12px; }
.message-content > *:last-child { margin-bottom: 0; }

.message-content p { margin: 0 0 12px; }

.message-content h1, .message-content h2, .message-content h3, .message-content h4 {
  margin: 18px 0 8px;
  font-weight: 600;
  letter-spacing: -0.018em;
  color: var(--text);
}
.message-content h1 { font-size: 1.34em; }
.message-content h2 { font-size: 1.22em; }
.message-content h3 { font-size: 1.10em; }
.message-content h4 { font-size: 1em; }

.message-content strong { font-weight: 600; }

.message-content pre {
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  padding: 14px 16px;
  border-radius: 12px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.55;
  position: relative;
}

.code-copy-btn {
  position: absolute;
  top: 7px;
  right: 7px;
  z-index: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 12px;
  line-height: 1;
  box-shadow: var(--shadow-sm);
  transition: color 0.12s, border-color 0.12s, background-color 0.12s;
}
/* Icon-only in the code corner (the labelled "copy" lives in the action bar). */
.code-copy-btn span { display: none; }
.code-copy-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}
.code-copy-btn.copied { color: var(--accent); border-color: var(--accent); }
.code-copy-btn svg { flex-shrink: 0; }

.message-content code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  padding: 1.5px 6px;
  border-radius: 5px;
  color: var(--text);
}
.message-content pre code {
  background: transparent;
  border: 0;
  padding: 0;
  font-size: inherit;
}

.message-content ul, .message-content ol {
  padding-left: 22px;
  margin: 0 0 12px;
}
.message-content li { margin-bottom: 4px; }
.message-content li::marker { color: var(--text-tertiary); }

.message-content blockquote {
  margin: 0 0 12px;
  padding: 4px 14px;
  border-left: 3px solid var(--accent);
  color: var(--text-secondary);
}

.message-content table {
  border-collapse: collapse;
  margin-bottom: 12px;
  display: block;
  overflow-x: auto;
  font-size: 14px;
}
.message-content th, .message-content td {
  border: 1px solid var(--border);
  padding: 6px 10px;
}
.message-content th { background: var(--surface-sunken); font-weight: 600; }

.message-content a { color: var(--link); }

/* Attachments embedded in a user message */
.message-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 8px;
}
.message.user .message-attachments {
  justify-content: flex-end;
  margin-bottom: 6px;
}
.message-attachments img {
  max-width: 220px;
  max-height: 180px;
  border-radius: 12px;
  border: 1px solid var(--border);
  object-fit: cover;
}
.message-attachments .file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12.5px;
  color: var(--text-secondary);
}

/* ============================================================
   Persona button + dropdown + edit modal
   ============================================================ */
.persona-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 4px 8px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  color: var(--text);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.12s, background-color 0.12s;
  max-width: 180px;
}
.persona-btn:hover { border-color: var(--accent); }
.persona-btn[aria-expanded="true"] {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.persona-emoji { font-size: 14px; line-height: 1; }
.persona-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100px;
}
.persona-btn svg { flex-shrink: 0; color: var(--text-secondary); }

/* `display: grid` / `display: block` etc on these elements would otherwise
 * override the UA stylesheet's `[hidden] { display: none }`. Make the
 * attribute authoritative. */
.persona-menu[hidden],
.modal-backdrop[hidden] { display: none !important; }

.persona-menu {
  position: fixed;
  z-index: 60;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  padding: 6px;
  min-width: 260px;
  max-width: 320px;
  max-height: 70vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  backdrop-filter: var(--topbar-blur);
  -webkit-backdrop-filter: var(--topbar-blur);
}
.persona-menu-section {
  padding: 6px 10px 4px;
  font-size: 10.5px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  font-weight: 600;
}
.persona-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: background-color 0.1s;
  text-align: left;
  width: 100%;
  color: var(--text);
}
.persona-item:hover { background: var(--surface-sunken); }
.persona-item.active {
  background: var(--accent-soft);
}
.persona-item .persona-item-emoji {
  font-size: 18px;
  line-height: 1.1;
  flex-shrink: 0;
  width: 24px;
  text-align: center;
}
.persona-item-text {
  flex: 1;
  min-width: 0;
}
.persona-item-name {
  font-size: 13.5px;
  font-weight: 600;
  letter-spacing: -0.005em;
  margin: 0;
}
.persona-item-desc {
  font-size: 12px;
  color: var(--text-secondary);
  margin: 2px 0 0;
  line-height: 1.35;
}
.persona-item-controls {
  display: flex;
  gap: 4px;
  margin-top: 4px;
  opacity: 0;
  transition: opacity 0.12s;
}
.persona-item:hover .persona-item-controls,
.persona-item:focus-within .persona-item-controls { opacity: 1; }
.persona-item-controls button {
  font-size: 11px;
  color: var(--text-tertiary);
  padding: 2px 6px;
  border-radius: 6px;
}
.persona-item-controls button:hover { color: var(--text); background: var(--surface); }
.persona-add-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  margin-top: 4px;
  padding: 9px;
  border-top: 1px solid var(--border);
  color: var(--accent);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.persona-add-btn:hover { background: var(--surface-sunken); }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.45);
  display: grid;
  place-items: center;
  padding: 20px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.modal {
  background: var(--surface);
  border-radius: 18px;
  box-shadow: var(--shadow-lg);
  padding: 22px 22px 18px;
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-title {
  margin: 0 0 14px;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.014em;
}
.modal-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.modal-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12.5px;
  color: var(--text-secondary);
  font-weight: 500;
}
.modal-field input,
.modal-field textarea {
  padding: 9px 12px;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  color: var(--text);
  outline: none;
  transition: border-color 0.12s, background-color 0.12s;
  font-family: inherit;
  resize: vertical;
}
.modal-field input:focus,
.modal-field textarea:focus {
  border-color: var(--accent);
  background: var(--surface);
}
.modal-field-hint {
  color: var(--text-tertiary);
  font-size: 11.5px;
  font-weight: 400;
}
.modal-error {
  color: var(--danger);
  font-size: 12.5px;
  margin: 0;
  min-height: 16px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}
.btn-modal { padding: 8px 16px; font-size: 14px; }
.btn-ghost.btn-modal {
  padding: 8px 16px;
  background: var(--surface-sunken);
  border-radius: 10px;
  color: var(--text);
  font-weight: 500;
}
.btn-ghost.btn-modal:hover { background: var(--border); }

/* ============================================================
   Settings panel
   ============================================================ */
.settings-section { margin: 18px 0 6px; }
.settings-section:first-of-type { margin-top: 8px; }
.settings-section-title {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  font-weight: 700;
  margin: 0 0 6px 2px;
}
.settings-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 2px;
  border-bottom: 1px solid var(--border);
}
.settings-row:last-child { border-bottom: 0; }
.settings-row-main { flex: 1; min-width: 0; }
.settings-row-label { font-size: 15px; color: var(--text); }
.settings-row-desc { font-size: 12px; color: var(--text-secondary); margin-top: 2px; line-height: 1.35; }
.settings-row-control { flex-shrink: 0; }

/* iOS-style toggle switch (label.switch > input + span.track) */
.switch { position: relative; display: inline-block; width: 46px; height: 28px; flex-shrink: 0; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch .track {
  position: absolute; inset: 0; border-radius: 999px;
  background: var(--border-strong);
  transition: background-color 0.18s;
}
.switch .track::before {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 24px; height: 24px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.3);
  transition: transform 0.18s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::before { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch input:disabled + .track { opacity: 0.5; }

/* Segmented control reused for theme / font / language choices */
.seg {
  display: inline-flex;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}
.seg button {
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 999px;
  color: var(--text-secondary);
  white-space: nowrap;
}
.seg button.active { background: var(--accent); color: var(--accent-fg); }
.seg button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.settings-data-btns { display: flex; flex-direction: column; gap: 8px; }
.settings-data-btns .btn-ghost {
  width: 100%; text-align: left; padding: 10px 12px;
  background: var(--surface-sunken); border-radius: 10px; color: var(--text);
}
.settings-data-btns .btn-ghost:hover { background: var(--border); }
.settings-data-btns .danger { color: var(--danger); }
.settings-about { font-size: 13px; color: var(--text-secondary); line-height: 1.6; }
.settings-about b { color: var(--text); font-weight: 600; }
.settings-modal { max-width: 480px; }

/* The gear lives in the topbar action cluster */
.settings-btn { color: var(--text-secondary); }
.settings-btn:hover { color: var(--text); }

/* Accent-color swatches (Appearance section) */
.accent-swatches { display: flex; gap: 10px; align-items: center; }
.accent-sw {
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--sw);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
  padding: 0; flex-shrink: 0;
  transition: transform 0.1s ease;
}
.accent-sw:active { transform: scale(0.9); }
.accent-sw.active { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--sw); }
.accent-sw:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

/* Welcome-screen suggestion chips */
.empty-suggestions {
  display: flex; flex-wrap: wrap; gap: 8px;
  justify-content: center; align-items: flex-start;
  max-width: 540px; margin: 22px auto 0;
}
.suggestion-chip {
  font: inherit; font-size: 13.5px; line-height: 1.35;
  color: var(--text); text-align: left;
  background: var(--surface-elev);
  border: 1px solid var(--border);
  border-radius: 16px; padding: 9px 14px;
  max-width: 100%; cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.suggestion-chip:hover { background: var(--surface-sunken); border-color: var(--border-strong); }
.suggestion-chip:active { transform: scale(0.97); }
.suggestion-chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (max-width: 760px) {
  .persona-btn .persona-name { display: none; }
  .persona-btn { padding: 6px 8px 6px 8px; }
  .persona-menu {
    /* Centred bottom-sheet style on phones */
    left: 12px !important;
    right: 12px !important;
    top: auto !important;
    bottom: max(16px, env(safe-area-inset-bottom));
    min-width: 0;
    max-width: none;
  }
}

/* Math (KaTeX) — display blocks get a tiny scroll margin so wide equations
 * don't push the bubble */
.katex-display {
  margin: 12px 0;
  overflow-x: auto;
  overflow-y: hidden;
}
.katex { font-size: 1.05em; }

/* Mermaid diagrams — replace the code block with a clean container */
.mermaid-rendered {
  margin: 14px 0;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow-x: auto;
  text-align: center;
}
.mermaid-rendered svg { max-width: 100%; height: auto; }
pre.mermaid-error code::before {
  content: "⚠ mermaid render failed — showing source\A";
  color: var(--danger);
  font-style: italic;
  display: block;
  margin-bottom: 6px;
  font-size: 0.85em;
  white-space: pre;
}

/* Streaming cursor + writing indicator */
.message.assistant.streaming .message-content::after {
  content: "▍";
  display: inline-block;
  animation: blink 1s steps(2) infinite;
  color: var(--accent);
  margin-left: 1px;
  font-weight: 400;
}
.message.assistant.streaming .message-content:has(.writing-indicator)::after {
  display: none;
}

.writing-indicator {
  color: var(--text-secondary);
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.writing-indicator::before {
  content: "";
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; transform: scale(0.8); }
  50%      { opacity: 1;   transform: scale(1.15); }
}
@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

.message.error .message-content { color: var(--danger); }
.message.error .message-prefix { background: var(--danger); }

/* Retry button — appears at the bottom of an errored / empty assistant
 * reply. Small inline pill so it doesn't dominate the message. */
.retry-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 10px;
  padding: 5px 12px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  color: var(--text-secondary);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.12s, color 0.12s, border-color 0.12s;
}
.retry-btn:hover { color: var(--accent); border-color: var(--accent); }
.retry-btn svg { flex-shrink: 0; }

/* ============================================================
   Composer
   ============================================================ */
.composer {
  background: var(--bg);
  padding: 12px 16px max(14px, env(safe-area-inset-bottom));
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
}

/* Stacked layout: a prominent full-width textarea on top, then a toolbar row
 * (tools left, send right). Cleaner than cramming every button to the left of
 * a narrow input. The whole thing is one rounded, card-like surface. */
.composer-row {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 22px;
  padding: 10px 12px 8px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer-row:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft), var(--shadow-sm);
}

#prompt {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  padding: 4px 6px 0;
  font-family: inherit;
  font-size: 16px;
  resize: none;
  max-height: 240px;
  line-height: 1.5;
  letter-spacing: -0.011em;
  caret-color: var(--accent);
}
#prompt::placeholder { color: var(--text-tertiary); }

/* Toolbar row beneath the textarea. */
.composer-tools {
  display: flex;
  align-items: center;
  gap: 2px;
}
.composer-tools-left {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 1;
  min-width: 0;
}

.attach-btn,
.mic-btn,
.search-btn,
.geo-btn {
  border-radius: 999px;
  padding: 8px;
  color: var(--text-secondary);
}
.attach-btn:hover,
.mic-btn:hover,
.search-btn:hover,
.geo-btn:hover { color: var(--text); background: rgba(127, 127, 127, 0.10); }

/* Web-search + location toggles in "on" state — accent fill. */
.search-btn[aria-pressed="true"],
.geo-btn[aria-pressed="true"] {
  color: var(--accent);
  background: var(--accent-soft);
}
/* While acquiring GPS — gentle pulse. */
.geo-btn.locating { color: var(--accent); animation: geo-pulse 1s ease-in-out infinite; }
@keyframes geo-pulse { 0%,100% { opacity: 0.5; } 50% { opacity: 1; } }

/* Inline citation sources block above an assistant reply that used search. */
/* The assistant .message-body is a flex ROW (avatar | content). Let it wrap
 * so the sources banner can claim a full-width row of its own ABOVE the
 * avatar+answer instead of stealing a side column (the old "占半屏" bug). */
.message.assistant .message-body { flex-wrap: wrap; }
.search-sources {
  flex: 0 0 100%;   /* full row */
  order: -1;        /* above the avatar + answer */
  margin: 2px 0 8px 0;
  padding: 7px 10px;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-width: 100%;
  box-sizing: border-box;
}
.search-sources-header {
  font-size: 10.5px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  font-weight: 600;
}
.search-source {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  line-height: 1.3;
  text-decoration: none;
  color: var(--text-secondary);
  min-width: 0;
}
.search-source:hover { color: var(--accent); }
.search-source .search-source-num {
  flex-shrink: 0;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: inline-grid;
  place-items: center;
  font-size: 10px;
  font-weight: 700;
}
.search-source-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

/* Voice recording — pulsing red dot + red foreground. */
.mic-btn.listening {
  color: var(--danger);
  background: rgba(215, 0, 21, 0.10);
  position: relative;
}
.mic-btn.listening::after {
  content: "";
  position: absolute;
  top: 4px; right: 4px;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--danger);
  box-shadow: 0 0 0 2px var(--surface);
  animation: mic-pulse 1.1s ease-in-out infinite;
}
@keyframes mic-pulse {
  0%, 100% { opacity: 0.5; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1.1); }
}

.send-btn {
  border-radius: 999px;
  padding: 0;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: var(--accent-fg);
  transition: background-color 0.15s, filter 0.15s, transform 0.06s, opacity 0.15s;
  flex-shrink: 0;
}
.send-btn:hover:not(:disabled) { filter: brightness(1.05); }
.send-btn:active:not(:disabled) { transform: scale(0.94); }
.send-btn:disabled {
  background: var(--surface-sunken);
  color: var(--text-tertiary);
  cursor: not-allowed;
  opacity: 1; /* override global button:disabled rule */
}
.send-btn.stop {
  background: var(--danger);
  color: #fff;
}
.send-btn.stop svg { display: none; }
.send-btn.stop::before {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: currentColor;
}

.composer-hint {
  max-width: 760px;
  margin: 8px auto 0;
  text-align: center;
  font-size: 11.5px;
  color: var(--text-tertiary);
  letter-spacing: -0.005em;
}

/* ============================================================
   Attachment preview tray
   ============================================================ */
.attachments {
  max-width: 760px;
  margin: 0 auto 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.attachment-preview {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px 8px 6px 6px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  max-width: 240px;
  box-shadow: var(--shadow-sm);
}

.attachment-preview img.thumb {
  width: 36px; height: 36px;
  object-fit: cover;
  border-radius: 6px;
}

.attachment-preview .doc-icon {
  width: 36px; height: 36px;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  border-radius: 6px;
  color: var(--accent);
  font-size: 10px;
  font-weight: 700;
}

.attachment-preview .filename {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
  font-size: 13px;
}
.attachment-preview .filesize {
  color: var(--text-tertiary);
  font-size: 11px;
}

.attachment-preview .remove-btn {
  background: transparent;
  color: var(--text-tertiary);
  font-size: 16px;
  line-height: 1;
  padding: 0 4px;
  flex: 0 0 auto;
}
.attachment-preview .remove-btn:hover { color: var(--danger); }

/* ============================================================
   Toast
   ============================================================ */
.toast {
  position: fixed;
  bottom: max(24px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  font-size: 13.5px;
  z-index: 100;
  max-width: 90vw;
  backdrop-filter: var(--topbar-blur);
  -webkit-backdrop-filter: var(--topbar-blur);
}
.toast.error {
  border-color: var(--danger);
  color: var(--danger);
}

/* ============================================================
   Mobile
   ============================================================ */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 280px;
    z-index: 50;
    box-shadow: var(--shadow-lg);
    transform: translateX(-100%);
  }
  .app.sidebar-open .sidebar { transform: translateX(0); }
  .app.sidebar-open::before {
    content: "";
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 40;
    pointer-events: auto;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
  }

  /* Topbar: tighter inner padding only — never touch the outer .topbar
   * padding so env(safe-area-inset-top) stays intact under the iOS status
   * bar / Dynamic Island. */
  /* Gap/padding tightened (8px->3px, 10px->8px) — with the back button + 2
     icon buttons + persona/settings/model-select all in one row, the looser
     spacing pushed the row past 375px-wide phones (model-select's right edge
     landed ~21px past the viewport). Touch targets (.icon-btn min 40px) are
     untouched — this only removes dead space between them. */
  .topbar-inner { padding: 0 8px; min-height: 52px; gap: 3px; }
  #toggle-sidebar { display: none; }
  .mobile-new-session {
    display: inline-flex;
    color: var(--accent);
  }
  .mobile-history { display: inline-flex; }
  .topbar-actions { gap: 4px; }
  .lang-switch button { padding: 5px 10px; font-size: 13px; }
  .model-select select {
    padding: 6px 22px 6px 8px;
    font-size: 13px;
    max-width: 92px;
    text-overflow: ellipsis;
  }

  /* 44pt touch targets per Apple HIG */
  .icon-btn { min-width: 40px; min-height: 40px; padding: 9px; }

  .messages { padding: 12px 0; }
  .message { padding: 6px 14px; }
  .message.user .message-body { max-width: 88%; }
  .message-actions { opacity: 1; }
  .message.assistant .message-actions { padding-left: 40px; }
  .code-copy-btn { opacity: 1; }

  /* Composer: short placeholder + smaller hint that hides on phones */
  .composer { padding: 8px 12px max(12px, env(safe-area-inset-bottom)); }
  .jump-latest { bottom: calc(76px + env(safe-area-inset-bottom)); }
  .composer-row { padding: 8px 10px 6px; }
  .send-btn { width: 34px; height: 34px; }
  /* Tools live in their own row now, so the textarea always has full width;
   * just keep the icons compact. */
  .composer-tools .icon-btn { padding: 7px; }
  .composer-tools .icon-btn svg { width: 20px; height: 20px; }
  /* Hint is dead weight on small screens — placeholder + model menu carry the info */
  .composer-hint { display: none; }

  .empty-state { margin-top: 8vh; }
  .empty-mark { width: 64px; height: 64px; }
  .empty-headline { font-size: 24px; }
  /* Slash-command code chips look terrible wrapping in mid-sentence — let
   * the whole meta line wrap as a unit. */
  .empty-meta { line-height: 1.8; }
}

/* On very narrow phones, even the model menu eats too much space — collapse
 * it to ~80px and let it ellipsis. */
@media (max-width: 380px) {
  .model-select select { max-width: 46px; padding: 6px 16px 6px 6px; }
  .topbar-title { font-size: 14px; }
  .topbar-inner { gap: 2px; padding: 0 6px; }
  .topbar-actions { gap: 3px; }
}

/* Standalone PWA tweaks */
@media all and (display-mode: standalone) {
  /* When launched from home screen, kill any user-select on chrome */
  .topbar, .sidebar, .composer-hint, .empty-state, .lang-toggle, .session-item {
    -webkit-user-select: none;
    user-select: none;
  }
}

/* Back to Spark home — iOS-style, terracotta accent. Always visible at the very
   left of the topbar. (The sidebar's "← Spark" button still works on desktop;
   this one ensures mobile users always have a clear way back.) */
.back-to-spark {
  display: inline-flex; align-items: center; gap: 2px;
  padding: 8px 6px 8px 4px; margin-right: 4px;
  color: var(--accent); text-decoration: none;
  font-size: 16px; font-weight: 500; letter-spacing: -0.01em;
  border-radius: 8px;
  transition: opacity .15s ease;
  -webkit-tap-highlight-color: transparent;
}
.back-to-spark:hover { background: rgba(127,127,127,0.10); }
.back-to-spark:active { opacity: .55; }
.back-to-spark svg { flex-shrink: 0; }
