/* ============================================
   Explorit
   ============================================ */

/* CSS Variables */
:root {
  --bg: #0d0e10;
  --bg-card: rgba(20, 21, 24, 0.85);
  --bg-hover: rgba(28, 29, 34, 0.85);
  --bg-field: #0a0a0a;
  --border: #25262b;
  --border-soft: rgba(255,255,255,0.05);
  --border-hover: #3f4046;
  --text: #c9c9ce;
  --text-muted: #82838b;
  --text-soft: #7d7e89;
  --accent: rgb(74, 158, 158);
  --accent-green: rgb(74, 158, 158);
  --accent-orange: rgb(255, 107, 74);
  --accent-red: #ef4444;
  --accent-light: rgb(110, 185, 185);
  --accent-teal: rgb(74, 158, 158);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-full: 100px;
  --font-sans: "Inter", "SF Pro Text", "PingFang SC", "Microsoft YaHei", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", "Fira Code", "Cascadia Code", monospace;
  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;
  --noise: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.05'/%3E%3C/svg%3E");
}

:root[data-theme="light"] {
  --bg: #f5f5f7;
  --bg-card: rgba(255, 255, 255, 0.78);
  --bg-hover: rgba(232, 232, 237, 0.65);
  --bg-field: rgba(245, 245, 247, 0.85);
  --border: #d2d2d7;
  --border-soft: rgba(0,0,0,0.06);
  --border-hover: #c7c7cc;
  --text: #1d1d1f;
  --text-muted: #6e6e75;
  --text-soft: #707077;
  --accent: #3d8a8a;
  --accent-green: #3d8a8a;
  --accent-orange: #e36209;
  --accent-red: #dc2626;
  --accent-light: #4a9e9e;
  --accent-teal: #3d8a8a;
}

/* Accent presets (dark) */
:root[data-accent="teal"] { --accent: rgb(74,158,158); --accent-green: rgb(74,158,158); --accent-light: rgb(110,185,185); --accent-teal: rgb(74,158,158); }
:root[data-accent="violet"] { --accent: rgb(139,92,246); --accent-green: rgb(139,92,246); --accent-light: rgb(169,140,249); --accent-teal: rgb(139,92,246); }
:root[data-accent="blue"] { --accent: rgb(59,130,246); --accent-green: rgb(59,130,246); --accent-light: rgb(127,177,251); --accent-teal: rgb(59,130,246); }
:root[data-accent="magenta"] { --accent: rgb(228,69,154); --accent-green: rgb(228,69,154); --accent-light: rgb(240,127,192); --accent-teal: rgb(228,69,154); }
/* Accent presets (light) */
:root[data-theme="light"][data-accent="violet"] { --accent: rgb(106,63,224); --accent-green: rgb(106,63,224); --accent-light: rgb(139,110,242); --accent-teal: rgb(106,63,224); }
:root[data-theme="light"][data-accent="blue"] { --accent: rgb(37,99,235); --accent-green: rgb(37,99,235); --accent-light: rgb(91,154,247); --accent-teal: rgb(37,99,235); }
:root[data-theme="light"][data-accent="magenta"] { --accent: rgb(194,36,126); --accent-green: rgb(194,36,126); --accent-light: rgb(224,103,168); --accent-teal: rgb(194,36,126); }

/* Reset & Base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; font-size: 16px; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; overscroll-behavior: none; }
input, textarea { font-size: 16px; touch-action: manipulation; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overflow: hidden;
  position: relative;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

/* Ambient depth layer (subtle brand-tinted gradient + film grain, dark & light) */
body::before {
  content: '';
  position: fixed; inset: -30%;
  background:
    radial-gradient(ellipse 55% 40% at 22% 15%, color-mix(in srgb, var(--accent) 8%, transparent), transparent 70%),
    radial-gradient(ellipse 48% 36% at 80% 28%, rgba(124,92,252,.06), transparent 65%),
    radial-gradient(ellipse 52% 42% at 58% 86%, rgba(255,107,74,.04), transparent 70%),
    radial-gradient(ellipse 60% 45% at 50% 50%, rgba(255,255,255,.03), transparent 60%);
  pointer-events: none; z-index: 0;
}
body::after {
  content: '';
  position: fixed; inset: 0;
  pointer-events: none; z-index: 0;
  opacity: .03; mix-blend-mode: overlay;
  background-image: var(--noise);
}

:root[data-theme="light"] body::before {
  background:
    radial-gradient(ellipse 55% 40% at 22% 15%, rgba(61,138,138,.10), transparent 70%),
    radial-gradient(ellipse 48% 36% at 80% 28%, rgba(124,92,252,.07), transparent 65%),
    radial-gradient(ellipse 52% 42% at 58% 86%, rgba(227,98,9,.05), transparent 70%);
}
:root[data-theme="light"] body::after { opacity: .025; }
:root[data-theme="light"] .header { background: rgba(245,245,247,0.72); }

::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* Consistent keyboard focus ring */
:focus-visible {
  outline: 2px solid var(--accent-green);
  outline-offset: 2px;
}
.chat-input:focus-visible,
.modal-input:focus-visible { outline: none; }

/* App Container */
.app { position: relative; z-index: 1; display: flex; flex-direction: column; min-height: 100vh; min-height: 100dvh; }

/* ============================================
   Header
   ============================================ */
.header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px;
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  background: rgba(10,10,10,0.68);
  border-bottom: 1px solid var(--border-soft);
  position: sticky; top: 0; z-index: 100;
}
.header-left { display: flex; align-items: center; gap: 16px; }
.logo { display: flex; align-items: center; gap: 10px; }
.logo-icon { width: 24px; height: 24px; color: var(--text); }
.logo-text { font-size: 1rem; font-weight: 700; letter-spacing: -.02em; color: var(--text); }
.header-right { display: flex; align-items: center; gap: 10px; }

/* Theme Toggle */
.theme-toggle {
  width: 34px; height: 34px; display: flex; align-items: center; justify-content: center;
  background: transparent; border: 1px solid var(--border); border-radius: var(--radius-full);
  color: var(--text-muted); cursor: pointer; transition: all var(--transition-normal); flex-shrink: 0;
}
.theme-toggle:hover { color: var(--text); border-color: var(--border-hover); }
.theme-toggle svg { width: 15px; height: 15px; }
.theme-toggle .icon-sun { display: none; }
.theme-toggle .icon-moon { display: block; }
:root[data-theme="light"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="light"] .theme-toggle .icon-moon { display: none; }

/* Accent picker */
.accent-picker { display: flex; align-items: center; gap: 6px; }
.accent-dot {
  width: 16px; height: 16px; border-radius: 50%;
  border: 1px solid var(--border); cursor: pointer; flex-shrink: 0;
  transition: transform .18s cubic-bezier(.34,1.56,.64,1), box-shadow .18s ease;
}
.accent-dot:hover { transform: scale(1.12); }
.accent-dot.active { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px currentColor; }
.accent-dot[data-accent="teal"] { background: rgb(74,158,158); color: rgb(74,158,158); }
.accent-dot[data-accent="violet"] { background: rgb(139,92,246); color: rgb(139,92,246); }
.accent-dot[data-accent="blue"] { background: rgb(59,130,246); color: rgb(59,130,246); }
.accent-dot[data-accent="magenta"] { background: rgb(228,69,154); color: rgb(228,69,154); }

/* ============================================
   Main / Workspace
   ============================================ */
.main {
  display: flex; flex-direction: column; flex: 1;
  align-items: center; justify-content: center;
  padding: 24px;
}
.main.has-chat {
  position: relative; overflow: hidden;
  justify-content: flex-end;
}
/* Empty page: lift the centered group (empty state + input bar) off the bottom */
.main:not(.has-chat) { padding-bottom: 8vh; }
.main.has-chat .empty-state { display: none; }

/* ============================================
   Empty State
   ============================================ */
.empty-state {
  display: flex; flex-direction: column; align-items: center; text-align: center;
  padding: 0 20px 14px; animation: emptyFadeIn .6s ease;
  transform: translateY(-50px);
}
@keyframes emptyFadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(-50px); }
}
.empty-state-content { display: flex; flex-direction: column; align-items: center; }
.empty-logo {
  width: 56px; height: 56px;
  color: var(--accent-green); margin-bottom: 10px;
  filter: drop-shadow(0 0 10px color-mix(in srgb, var(--accent) 35%, transparent));
}
.empty-title {
  font-size: 1.8rem; font-weight: 700; letter-spacing: -.02em;
  margin-bottom: 8px;
  color: var(--accent-green);
}
.empty-desc { font-size: .95rem; color: var(--text-muted); margin-bottom: 4px; }
.empty-hint { font-size: .8rem; color: var(--text-soft); margin-bottom: 16px; }
.empty-term {
  border-bottom: 2px dotted var(--accent-green);
  cursor: default;
}
.empty-prompt-row {
  display: flex; flex-direction: row; align-items: center; justify-content: center;
  gap: 10px; margin-top: 12px; max-width: 100%; flex-wrap: wrap;
}
.empty-prompt-chips {
  display: flex; flex-wrap: wrap; justify-content: center; align-items: center;
  gap: 8px; min-width: 0; min-height: 30px;
}
.empty-prompt-chip {
  padding: 8px 16px;
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 18%, transparent);
  border-radius: var(--radius-full);
  color: var(--accent-green);
  font-family: var(--font-sans); font-size: .8rem;
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast), transform var(--transition-fast);
}
.empty-prompt-chip:hover {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
  transform: translateY(-1px);
}
.empty-prompt-chip:active { transform: translateY(0); }
.empty-prompt-chip.placeholder {
  color: var(--text-soft);
  background: rgba(255,255,255,0.03);
  border-color: var(--border);
  cursor: default;
  animation: chipPulse 1.6s ease-in-out infinite;
}
.empty-prompt-chip.placeholder:hover {
  background: rgba(255,255,255,0.03);
  border-color: var(--border);
  transform: none;
}
:root[data-theme="light"] .empty-prompt-chip.placeholder {
  background: rgba(0,0,0,0.04);
}
@keyframes chipPulse {
  0%, 100% { opacity: .55; }
  50% { opacity: 1; }
}
.chips-refresh {
  height: 30px; padding: 0 6px; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: transparent; border: 1px solid color-mix(in srgb, var(--accent) 18%, transparent);
  border-radius: var(--radius-full); color: var(--text-muted); cursor: pointer; flex-shrink: 0;
  transition: color var(--transition-fast), border-color var(--transition-fast);
}
.chips-refresh:hover { color: var(--accent-green); border-color: color-mix(in srgb, var(--accent) 35%, transparent); }
.chips-refresh svg { width: 13px; height: 13px; }
.chips-refresh.loading svg { animation: chipSpin 1s linear infinite; }
.chips-refresh.has-status { padding: 0 12px; }
.chips-refresh.error { border-color: var(--accent-red); color: var(--accent-red); }
.chips-refresh .chips-status-text {
  display: none;
  font-size: .72rem; color: var(--text-soft);
  white-space: nowrap;
  max-width: 170px; overflow: hidden; text-overflow: ellipsis;
}
.chips-refresh.has-status .chips-status-text { display: inline-block; }
.chips-refresh.error .chips-status-text { color: var(--accent-red); }
@keyframes chipSpin { to { transform: rotate(360deg); } }

/* ============================================
   History (archived workspaces on main page)
   ============================================ */
.history-section {
  display: none;
  width: min(600px, 94vw);
  margin-top: 0;
  max-height: calc(100vh - 430px);
  overflow-y: auto;
  text-align: left;
}
.history-group { display: contents; }
.main.has-chat .history-group { display: none !important; }
.history-header {
  display: flex; align-items: center; justify-content: space-between;
  font-size: .75rem; color: var(--text-soft);
  margin-top: 16px; margin-bottom: 8px;
  width: min(600px, 94vw);
}
.history-sync {
  padding: 2px 10px;
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 22%, transparent);
  border-radius: var(--radius-full);
  color: var(--accent-green);
  font-size: .68rem; font-family: var(--font-sans);
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast);
}
.history-sync:hover { background: color-mix(in srgb, var(--accent) 16%, transparent); border-color: color-mix(in srgb, var(--accent) 35%, transparent); }
.history-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}
.history-clear {
  padding: 2px 10px;
  background: rgba(190,80,80,0.08);
  border: 1px solid rgba(190,80,80,0.22);
  border-radius: var(--radius-full);
  color: #e08a8a;
  font-size: .68rem; font-family: var(--font-sans);
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast);
}
.history-clear:hover { background: rgba(190,80,80,0.16); border-color: rgba(190,80,80,0.4); }
.history-list { display: flex; flex-direction: column; gap: 6px; }
.history-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  background: var(--bg-card);
  backdrop-filter: blur(12px) saturate(180%);
  -webkit-backdrop-filter: blur(12px) saturate(180%);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  user-select: none;
  transition: border-color var(--transition-fast), background var(--transition-fast), transform .18s ease;
  animation: historyIn .3s cubic-bezier(.34,1.56,.64,1) backwards;
}
.history-item:hover { border-color: var(--border-hover); }
.history-item.dragging {
  opacity: .96;
  background: rgba(20,21,24,0.96);
  backdrop-filter: blur(28px) saturate(180%);
  -webkit-backdrop-filter: blur(28px) saturate(180%);
  box-shadow: 0 6px 18px rgba(0,0,0,.25);
  position: relative; z-index: 2;
  transition: none;
  animation: none;
}
:root[data-theme="light"] .history-item.dragging {
  background: rgba(255,255,255,0.97);
}
@keyframes historyIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
.history-drop {
  position: fixed; z-index: 30;
  height: 3px; border-radius: 2px;
  background: var(--accent-green);
  pointer-events: none;
}
.history-placeholder {
  border: 1px dashed var(--text-soft);
  border-radius: 10px;
  pointer-events: none;
  flex-shrink: 0;
}
.history-grip {
  display: flex; align-items: center;
  color: var(--text-soft); cursor: grab;
  touch-action: none; flex-shrink: 0;
}
.history-grip:active { cursor: grabbing; }
.history-grip svg { width: 14px; height: 14px; }
.history-title {
  flex: 1; min-width: 0;
  font-size: .8rem; color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.history-ts { font-size: .68rem; color: var(--text-soft); flex-shrink: 0; }
.history-del {
  width: 26px; height: 26px; display: flex; align-items: center; justify-content: center;
  background: transparent; border: none; border-radius: 8px;
  color: var(--text-muted); cursor: pointer; flex-shrink: 0;
  transition: color var(--transition-fast), background var(--transition-fast);
}
.history-del:hover { background: rgba(239,68,68,0.12); color: var(--accent-red); }
.history-del svg { width: 14px; height: 14px; }


/* ============================================
   Card Tree - dot + line tree structure
   ============================================ */
.card-tree {
  display: none;
  position: fixed; right: 12px; bottom: 80px;
  min-width: 100px; max-height: 280px;
  background: rgba(20,21,24,0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-y: auto; overflow-x: hidden;
  z-index: 100;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.04), 0 6px 18px rgba(0,0,0,.18);
}
.main.has-chat .card-tree { display: block; }
:root[data-theme="light"] .card-tree { background: rgba(245,245,247,0.85); border-color: var(--border); }

/* Card Tree Toggle — mobile only */
.card-tree-toggle { display: none; }

.card-tree-list { padding: 6px 0; }
.card-tree-list::-webkit-scrollbar { width: 3px; }
.card-tree-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Card Node */
.card-node {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 12px;
  cursor: pointer; user-select: none;
  transition: background .15s;
  position: relative;
  z-index: 1;
}
.card-node:hover { background: rgba(255,255,255,0.06); }
.card-node::after {
  content: '';
  position: absolute; left: 0; top: 25%; bottom: 25%;
  width: 2px;
  background: var(--accent-green);
  transform: scaleY(0);
  transform-origin: center;
  transition: transform .18s ease;
}
/* Keep the hover bar on the tree's leftmost edge (root position) at every depth,
   instead of letting it sit over the branch connectors */
.card-tree-children > .card-node::after { left: -28px; }
.card-tree-children .card-tree-children > .card-node::after { left: -56px; }
.card-tree-children .card-tree-children .card-tree-children > .card-node::after { left: -84px; }
.card-node:hover::after { transform: scaleY(1); }

.card-tree-children { position: relative; padding-left: 16px; margin-left: 12px; }
.card-tree-children > .card-node { position: relative; }
/* Horizontal connector: from trunk center to the child dot center */
.card-tree-children > .card-node::before {
  content: '';
  position: absolute;
  left: -13px; top: 50%;
  width: 28px; height: 2px;
  background: var(--border);
  transform: translateY(-50%);
}
/* Vertical trunk: single continuous segment from the parent dot to the last child dot center */
.card-tree-children::before {
  content: '';
  position: absolute;
  left: var(--trunk-left, 2px);
  top: calc(-1 * var(--root-drop, 16px));
  bottom: var(--trunk-end, 0);
  width: 2px;
  background: var(--border);
}

.card-node-dot {
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--text-soft); flex-shrink: 0;
  position: relative; z-index: 1;
}
.card-node.active .card-node-dot {
  background: var(--accent-green);
  transform: scale(1.35);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 16%, transparent);
  transition: transform .18s cubic-bezier(.34,1.56,.64,1), background .18s ease, box-shadow .18s ease;
}
.card-node:not(.active):hover .card-node-dot { background: #5a5b63; }

.card-node-label {
  font-size: .75rem; color: var(--text-muted);
  white-space: nowrap; line-height: 1.4;
}
.card-node.active .card-node-label { color: var(--accent-green); }
.card-node.collapsed { opacity: .5; }


/* ============================================
   Chat Area
   ============================================ */
/* Chat Window */
.chat-window {
  position: absolute;
  width: 66vw; min-width: 360px;
  height: 85%; min-height: 240px;
  display: flex; flex-direction: row;
  background:
    var(--noise),
    linear-gradient(180deg, rgba(28,29,34,.98), rgba(20,21,24,.96));
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid var(--border); border-radius: var(--radius-lg);
  overflow: hidden;
  z-index: 1;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.04), 0 2px 8px rgba(0,0,0,0.12);
  transition: border-color var(--transition-normal), box-shadow .3s ease;
}
.chat-window::after {
  content: '';
  position: absolute; inset: 0;
  border-radius: inherit;
  pointer-events: none;
  padding: 1px;
  background: linear-gradient(135deg, rgba(255,255,255,.14), rgba(255,255,255,.02) 40%, rgba(255,255,255,.06));
  -webkit-mask: linear-gradient(180deg,#fff 0 0) content-box, linear-gradient(180deg,#fff 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
}
.chat-window.focused {
  z-index: 10;
  border-color: var(--accent-green);
  box-shadow: 0 6px 22px rgba(0,0,0,0.22), 0 0 18px color-mix(in srgb, var(--accent) 8%, transparent);
}
.chat-window.focused .chat-window-header {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border-right-color: color-mix(in srgb, var(--accent) 20%, transparent);
}
.chat-window.focused .chat-window-title { color: var(--accent-green); }
/* During streaming, all card header buttons are inert and grayed out */
body.streaming .chat-window-header button {
  opacity: .4;
  pointer-events: none;
  cursor: default;
}
:root[data-theme="light"] .chat-window.focused .chat-window-header {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  border-right-color: color-mix(in srgb, var(--accent) 20%, transparent);
}
:root[data-theme="light"] .chat-window {
  background:
    var(--noise),
    linear-gradient(180deg,#ffffff,#f0f0f3);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.6), 0 2px 8px rgba(0,0,0,0.06);
}
:root[data-theme="light"] .chat-window.focused { box-shadow: 0 8px 24px rgba(0,0,0,0.10); }

/* Window Header — left sidebar */
.chat-window-header {
  display: flex; flex-direction: column; align-items: center;
  width: 36px; padding: 8px 0;
  border-right: 1px solid var(--border);
  cursor: grab; user-select: none; flex-shrink: 0;
  gap: 6px;
  touch-action: none;
}
.chat-window-header:active { cursor: grabbing; }
.chat-window-title {
  font-size: .75rem; font-weight: 600; color: var(--text);
  flex: 1; min-height: 0;
  padding: 6px 0;
  overflow: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.chat-window-title .vt-char {
  display: block;
  width: 1.5em;
  height: 1.5em;
  line-height: 1.5;
  text-align: center;
  flex-shrink: 0;
}

.chat-window-actions { display: flex; flex-direction: column; align-items: center; gap: 2px; }

.card-action-btn {
  width: 26px; height: 26px; display: flex; align-items: center; justify-content: center;
  background: none; border: 1px solid transparent; border-radius: var(--radius-sm);
  color: var(--text-soft); cursor: pointer; transition: all var(--transition-fast);
}
.card-action-btn:hover {
  background: color-mix(in srgb, var(--accent) 12%, transparent); border-color: color-mix(in srgb, var(--accent) 25%, transparent);
  color: var(--accent-green);
}
.card-action-btn svg { width: 14px; height: 14px; }

.chat-window-close {
  width: 26px; height: 26px; display: flex; align-items: center; justify-content: center;
  background: none; border: none; border-radius: var(--radius-sm);
  color: var(--text-muted); cursor: pointer; transition: all var(--transition-fast); flex-shrink: 0;
}
.chat-window-close:hover { background: rgba(239,68,68,0.12); color: var(--accent-red); }
.chat-window-close svg { width: 13px; height: 13px; }

/* Resize Handles */
.chat-resize-handle {
  position: absolute; opacity: 0.25; transition: opacity var(--transition-fast); z-index: 5;
  touch-action: none;
}
.chat-resize-handle:hover { opacity: 1; }

.chat-resize-se {
  right: 0; bottom: 0; width: 28px; height: 28px; cursor: nwse-resize;
}
.chat-resize-e {
  right: 0; top: 0; width: 4px; height: 100%; cursor: e-resize;
}
.chat-resize-s {
  left: 0; bottom: 0; width: 100%; height: 4px; cursor: s-resize;
}

/* Chat Messages */
.chat-messages { flex: 1; overflow-y: auto; padding: 20px; }

/* Scroll-to-bottom button */
.scroll-bottom-btn {
  position: absolute; right: 12px; bottom: 12px;
  width: 32px; height: 32px; display: flex; align-items: center; justify-content: center;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-full); color: var(--text-muted);
  cursor: pointer; z-index: 6;
  opacity: 0; pointer-events: none; transform: translateY(8px);
  transition: all .25s ease;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
.scroll-bottom-btn.visible {
  opacity: 1; pointer-events: auto; transform: translateY(0);
}
.scroll-bottom-btn:hover { border-color: var(--accent-green); color: var(--accent-green); }
.scroll-bottom-btn svg { width: 16px; height: 16px; }
.scroll-top-btn {
  position: absolute; right: 12px; bottom: 52px;
  width: 32px; height: 32px; display: flex; align-items: center; justify-content: center;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-full); color: var(--text-muted);
  cursor: pointer; z-index: 6;
  opacity: 0; pointer-events: none; transform: translateY(8px);
  transition: all .25s ease;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
.scroll-top-btn.visible {
  opacity: 1; pointer-events: auto; transform: translateY(0);
}
.scroll-top-btn:hover { border-color: var(--accent-green); color: var(--accent-green); }
.scroll-top-btn svg { width: 16px; height: 16px; }
.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ============================================
   Chat Bubbles
   ============================================ */
.chat-msg { display: flex; margin-bottom: 20px; animation: msgSpringIn .45s cubic-bezier(.34,1.56,.64,1); }
@keyframes msgSpringIn { from { opacity: 0; transform: translateY(16px) scale(.96); } to { opacity: 1; transform: translateY(0) scale(1); } }
.chat-msg.user { justify-content: flex-end; }
.chat-msg.system { justify-content: center; margin-bottom: 10px; animation: none; }
.chat-msg.system .chat-bubble {
  background: transparent; border: none;
  color: var(--text-muted);
  font-size: .72rem; padding: 2px 10px; text-align: center;
}

.chat-bubble {
  max-width: 90%; padding: 12px 18px; border-radius: 18px;
  font-size: .92rem; line-height: 1.5; word-break: break-word;
  font-family: "PingFang SC", "Microsoft YaHei", var(--font-sans);
}
/* Heading sizes inside AI bubbles: h1/h2 pulled down and gap narrowed (h3+ stay as default) */
.chat-bubble h1 { font-size: 1.24rem; }
.chat-bubble h2 { font-size: 1.15rem; }
/* Inline images from the model: size-limited, rounded; streaming placeholder */
.chat-bubble img {
  display: block; max-width: 100%; max-height: 260px;
  border-radius: 10px; margin: .6em 0;
  border: 1px solid var(--border);
}
.chat-bubble .img-ph {
  display: block; max-width: 100%; height: 150px;
  border-radius: 10px; margin: .6em 0;
  background: rgba(255,255,255,.04);
  border: 1px solid var(--border);
  position: relative; overflow: hidden;
}
.chat-bubble .img-ph::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 30%, rgba(255,255,255,.06) 50%, transparent 70%);
  background-size: 200% 100%;
  animation: imgShimmer 1.3s linear infinite;
}
@keyframes imgShimmer {
  from { background-position: 150% 0; }
  to   { background-position: -50% 0; }
}
.chat-msg.user .chat-bubble {
  background: color-mix(in srgb, var(--accent) 15%, transparent); border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
  color: var(--text); border-bottom-right-radius: 6px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.04);
}
:root[data-theme="light"] .chat-msg.user .chat-bubble {
  color: #1a1a1c;
  background: color-mix(in srgb, var(--accent) 12%, transparent); border-color: color-mix(in srgb, var(--accent) 25%, transparent);
}
.chat-msg.ai .chat-bubble {
  background: var(--bg-card); border: 1px solid var(--border);
  color: var(--text); border-bottom-left-radius: 6px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.04), 0 2px 8px rgba(0,0,0,.07);
}
:root[data-theme="light"] .chat-msg.ai .chat-bubble { color: #1a1a1c; }

.chat-bubble p { margin-bottom: .6em; }
.chat-bubble p:last-child { margin-bottom: 0; }
.chat-bubble code {
  font-family: var(--font-mono); font-size: .85em;
  padding: .15em .4em; background: var(--bg-hover);
  border-radius: 4px; color: var(--accent-light);
}
.chat-bubble pre {
  margin: .6em 0; padding: 14px; background: var(--bg);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  overflow-x: auto; font-size: .82rem;
}
.chat-bubble pre code { padding: 0; background: none; color: var(--text); }
.chat-bubble ul, .chat-bubble ol { margin: .4em 0; padding-left: 1.5em; }
.chat-bubble li { margin-bottom: .3em; }
.chat-bubble a {
  color: var(--accent-light);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.chat-bubble a:hover { opacity: .8; }
.chat-bubble table {
  width: 100%; margin: .6em 0; border-collapse: collapse;
  font-size: .85em; border-radius: var(--radius-sm); overflow: hidden;
}
.chat-bubble th, .chat-bubble td {
  padding: 8px 12px; border: 1px solid var(--border); text-align: left;
}
.chat-bubble th { background: var(--bg-hover); font-weight: 600; }
.chat-bubble tr:nth-child(even) { background: rgba(255,255,255,0.02); }
:root[data-theme="light"] .chat-bubble tr:nth-child(even) { background: rgba(0,0,0,0.03); }

/* ============================================
   Glossary Term - dotted underline
   ============================================ */
.glossary-term:hover {
  border-bottom-style: solid;
  border-bottom-width: 3px;
  background: color-mix(in srgb, var(--accent) 8%, transparent);
}
.glossary-term.asked {
  border-bottom-color: var(--accent-orange);
  animation: none;
}

/* Text selection toolbar */
.text-action-toolbar {
  position: fixed; z-index: 200;
  display: none; align-items: center; gap: 2px;
  padding: 4px;
  background: var(--bg-card);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 20px rgba(0,0,0,.35);
  white-space: nowrap;
}
.text-action-btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 5px 11px;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--text);
  font-size: .78rem; font-family: var(--font-sans);
  cursor: pointer; white-space: nowrap;
  transition: all .15s ease;
}
.text-action-btn:hover {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  color: var(--accent-green);
}
.text-action-btn svg { width: 13px; height: 13px; }

/* Thinking dots */
.thinking-dots { display: flex; gap: 4px; padding: 6px 0; }
.thinking-dots span {
  width: 6px; height: 6px; background: var(--text-muted);
  border-radius: 50%; animation: dotBounce 1.2s infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: .2s; }
.thinking-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes dotBounce {
  0%, 60%, 100% { opacity: .3; transform: scale(.8); }
  30% { opacity: 1; transform: scale(1); }
}

/* Status Indicator (searching / thinking) */
.status-indicator {
  display: flex; align-items: center; gap: 8px;
  font-size: .85rem; color: var(--text-muted); padding: 4px 0;
}
.status-indicator .thinking-dots { padding: 0; }
.search-status svg {
  width: 16px; height: 16px;
  animation: searchPulse 1.5s ease infinite;
}
@keyframes searchPulse {
  0%, 100% { opacity: .5; }
  50% { opacity: 1; }
}

/* ============================================
   Chat Input
   ============================================ */
.chat-input-container { width: 100%; max-width: 720px; position: relative; z-index: 100; }
.chat-input-wrapper {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px;
  background: var(--bg-card);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid var(--border); border-radius: var(--radius-lg);
  transition: border-color var(--transition-normal);
}
/* Empty page: lift the input bar by half of its own height */
.main:not(.has-chat) .chat-input-wrapper { transform: translateY(-50%); }
.chat-input-wrapper:focus-within {
  border-color: var(--accent-green);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 10%, transparent);
}

/* Input tool buttons */
.input-tool-btn {
  height: 30px; padding: 0 8px;
  display: inline-flex; align-items: center; gap: 4px;
  background: transparent; border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-family: var(--font-sans); font-size: .75rem; font-weight: 500;
  cursor: pointer; flex-shrink: 0;
  transition: all var(--transition-fast);
}
.input-tool-btn:hover { border-color: var(--border-hover); color: var(--text); }
.input-tool-btn.active { background: var(--accent-green); border-color: var(--accent-green); color: #fff; }
.input-tool-btn.active svg { color: #fff; }
.input-tool-btn svg { width: 14px; height: 14px; }

.chat-input {
  flex: 1; background: transparent; border: none; outline: none; resize: none;
  font-family: "PingFang SC", "Microsoft YaHei", var(--font-sans); font-size: .95rem; font-weight: 400; line-height: 1.5;
  color: var(--text); padding: 2px 0; max-height: 200px; overflow-y: auto;
}
.chat-input::placeholder { color: var(--text-muted); }
.chat-input::-webkit-scrollbar { width: 4px; }
.chat-input::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.chat-send-btn {
  width: 30px; height: 30px; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; background: transparent; border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text-muted); cursor: pointer;
  transition: background var(--transition-normal), border-color var(--transition-normal), color var(--transition-normal), transform .16s cubic-bezier(.34,1.56,.64,1);
}
.chat-send-btn.has-text { border-color: var(--accent-green); color: var(--accent-green); }
.chat-send-btn:hover { background: var(--accent-green); border-color: var(--accent-green); color: #0a0a0a; }
.chat-send-btn:disabled { opacity: .4; cursor: not-allowed; }
.chat-send-btn:disabled:hover { background: transparent; border-color: var(--border); color: var(--text-muted); }
.chat-send-btn svg { width: 14px; height: 14px; }
:root[data-theme="light"] .chat-send-btn:hover { color: #fff; }

/* Stop button state */
.chat-send-btn .icon-stop { display: none; width: 14px; height: 14px; }
.chat-send-btn.streaming { border-color: var(--accent-red); color: var(--accent-red); }
.chat-send-btn.streaming .icon-send { display: none; }
.chat-send-btn.streaming .icon-stop { display: block; }
.chat-send-btn.streaming:hover { background: var(--accent-red); border-color: var(--accent-red); color: #fff; }
.chat-send-btn.streaming:disabled { opacity: 1; cursor: pointer; }

/* ============================================
   Buttons
   ============================================ */
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 11px 24px; font-family: var(--font-sans);
  font-size: .9rem; font-weight: 600; border: none;
  border-radius: var(--radius-full); cursor: pointer; transition: all .3s ease;
}
.btn-primary { background: rgba(255,255,255,.90); color: #0a0a0a; }
.btn-primary:hover { transform: translateY(-1px); }
:root[data-theme="light"] .btn-primary { background: rgba(29,29,31,.90); color: #fff; }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: rgba(255,255,255,.04); color: var(--text); }
.btn-danger { background: var(--accent-red); color: #fff; }
.btn-danger:hover { background: #f05a5a; }

.modal-error {
  display: none;
  margin-top: 8px;
  font-size: .78rem; color: var(--accent-red);
}
.modal-error.show { display: block; }

/* Confirm dialog is a compact dialog, not a full-width form */
#confirmModal .modal { max-width: 320px; }
#confirmModal .modal-header { padding: 10px 20px; }
#confirmModal .modal-header h3 { font-size: .9rem; }
#confirmModal .modal-body { padding: 10px 20px; }
#confirmModal #confirmMessage { font-size: .76rem; line-height: 1.4; }
#confirmModal .modal-footer { padding: 8px 20px; }
#confirmModal .modal-footer .btn { padding: 8px 18px; font-size: .8rem; }

/* ============================================
   Modal
   ============================================ */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.7);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  display: none; align-items: center; justify-content: center; z-index: 1000;
}
.modal-overlay.active { display: flex; }
.modal {
  width: 100%; max-width: 440px;
  background: rgba(22,23,26,0.96);
  border: 1px solid var(--border); border-radius: var(--radius-lg);
  animation: modalIn .25s ease;
}
:root[data-theme="light"] .modal { background: rgba(255,255,255,0.96); }
@keyframes modalIn { from { opacity: 0; transform: scale(.96) translateY(8px); } to { opacity: 1; transform: scale(1) translateY(0); } }

/* Card Window Spring Entrance */
@keyframes cardSpringIn {
  0%   { opacity: 0; transform: scale(.82); }
  55%  { opacity: 1; transform: scale(1.04); }
  75%  { transform: scale(.97); }
  100% { opacity: 1; transform: scale(1); }
}
.chat-window.entering { animation: cardSpringIn .5s cubic-bezier(.22,.61,.36,1); }

/* Card Window Exit */
@keyframes cardExit {
  0%   { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(.88); }
}
.chat-window.exiting { animation: cardExit .25s ease-in forwards; pointer-events: none; }

/* Window Focus Pulse */
@keyframes focusPulse {
  0%   { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 45%, transparent); }
  70%  { box-shadow: 0 0 0 10px color-mix(in srgb, var(--accent) 0%, transparent); }
  100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 0%, transparent); }
}
.chat-window.just-focused { animation: focusPulse .8s ease-out; }

/* Card Tree Node Entrance */
/* Active node: gentle scale pulse on dot + label only, so connector lines
   stay glued to the trunk and the horizontal/vertical lines move together */
@keyframes nodeActive {
  0%   { transform: scale(1); }
  45%  { transform: scale(1.08); }
  100% { transform: scale(1); }
}
.card-node.active .card-node-dot,
.card-node.active .card-node-label { animation: nodeActive .35s cubic-bezier(.34,1.56,.64,1); }

/* ============================================
   Term Hover Tooltip
   ============================================ */
.term-tooltip {
  position: fixed; z-index: 300;
  display: none;
  max-width: 340px; min-width: 160px;
  background: rgba(19,20,23,0.97);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid var(--accent-green);
  border-radius: 10px;
  box-shadow: 0 8px 28px rgba(0,0,0,.40);
  font-size: .82rem; line-height: 1.55;
  color: var(--text);
  pointer-events: auto;
  animation: tooltipIn .18s ease-out;
}
:root[data-theme="light"] .term-tooltip {
  background: rgba(255,255,255,0.96);
  box-shadow: 0 8px 24px rgba(0,0,0,.12);
}
@keyframes tooltipIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}
.term-tooltip-inner {
  padding: 10px 14px;
  max-height: 200px; overflow-y: auto;
}
.term-tooltip-inner::-webkit-scrollbar { width: 3px; }
.term-tooltip-inner::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.term-tooltip-loading {
  display: flex; align-items: center; gap: 8px;
  color: var(--text-muted); font-size: .8rem;
}
.term-tooltip-spinner {
  width: 12px; height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--accent-green);
  border-radius: 50%;
  animation: termSpinner .7s linear infinite;
}
@keyframes termSpinner { to { transform: rotate(360deg); } }

.term-tooltip-content {
  color: var(--text); font-size: .82rem; line-height: 1.55;
  word-break: break-word;
}
.term-tooltip-content code {
  font-family: var(--font-mono); font-size: .85em;
  padding: 1px 4px; background: var(--bg-hover);
  border-radius: 3px; color: var(--accent-light);
}
.term-tooltip-content a { color: var(--accent); }

/* Click-opened tooltip footer: deepen action */
.term-tooltip-footer {
  display: none;
  justify-content: flex-end;
  padding: 8px 14px;
  border-top: 1px solid var(--border-soft);
}
.term-tooltip-deepen {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 6px 12px;
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
  border-radius: var(--radius-full);
  color: var(--accent-green);
  font-family: var(--font-sans); font-size: .75rem;
  cursor: pointer;
  transition: background var(--transition-fast), border-color var(--transition-fast);
}
.term-tooltip-deepen:hover { background: color-mix(in srgb, var(--accent) 20%, transparent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }

/* PC hover hint: Ctrl+click to deepen */
.term-hint {
  position: fixed; z-index: 350;
  display: none;
  padding: 5px 11px;
  background: rgba(19,20,23,0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  color: var(--text-muted);
  font-size: .72rem; line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: 0 4px 16px rgba(0,0,0,.35);
  animation: hintIn .18s ease-out;
}
:root[data-theme="light"] .term-hint { background: rgba(255,255,255,0.95); }
.term-hint kbd {
  display: inline-block;
  padding: 1px 5px;
  border: 1px solid var(--border-hover);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--bg-hover);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: .68rem;
}
@keyframes hintIn { from { opacity: 0; transform: translateY(3px); } }


.glossary-term {
  border-bottom: 2.5px dotted var(--accent-green);
  cursor: pointer; transition: border .15s ease;
}

/* Send Button Tap Feedback (transition-based so rapid taps do not restart from 0) */
.chat-send-btn.tapped,
.chat-send-btn:active { transform: scale(.92); }

/* New Card Button Spin */
@keyframes btnSpinIn {
  from { transform: rotate(-90deg); opacity: 0; }
  to   { transform: rotate(0deg); opacity: 1; }
}
.card-action-btn svg { transition: transform .3s cubic-bezier(.34,1.56,.64,1); }
.card-action-btn:active svg { transform: rotate(90deg); }

.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 20px 24px; border-bottom: 1px solid var(--border); }
.modal-header h3 { font-size: 1.05rem; font-weight: 600; letter-spacing: -.01em; color: var(--text); }
.modal-close {
  width: 32px; height: 32px; display: flex; align-items: center; justify-content: center;
  background: none; border: none; border-radius: var(--radius-sm);
  color: var(--text-muted); cursor: pointer; transition: all var(--transition-fast);
}
.modal-close:hover { background: rgba(255,255,255,.06); color: var(--text); }
.modal-close svg { width: 18px; height: 18px; }
.modal-body { padding: 24px; }
.modal-hint { font-size: .82rem; color: var(--text-muted); margin-bottom: 14px; }
.modal-input {
  width: 100%; padding: 11px 16px;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-family: var(--font-mono); font-size: .88rem; color: var(--text);
  transition: border-color var(--transition-fast);
}
:root[data-theme="light"] .modal-input { background: rgba(0,0,0,0.04); }
.modal-input:focus { outline: none; border-color: var(--accent-green); }
.modal-input::placeholder { color: var(--text-muted); }
.api-guide { margin-top: 18px; padding: 0; background: none; border: none; }
.api-guide-title { font-size: .82rem; font-weight: 500; color: var(--text-muted); margin-bottom: 10px; }
.api-guide-steps { margin: 0; padding-left: 20px; font-size: .8rem; color: var(--text-muted); line-height: 1.8; }
.api-guide-steps a { color: var(--accent); text-decoration: none; transition: opacity var(--transition-fast); }
.api-guide-steps a:hover { opacity: .8; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; padding: 16px 24px; border-top: 1px solid var(--border); }
.modal-footer .btn { border-radius: var(--radius-full); }

/* ============================================
   Responsive
   ============================================ */

/* -- Tablet / narrow desktop ----------------- */
@media (max-width: 768px) {
  .chat-window { width: 92vw; min-width: 0; }
  .card-tree { right: 8px; bottom: 70px; max-height: 220px; min-width: 80px; }
  .chat-bubble { max-width: 88%; font-size: .88rem; padding: 10px 15px; }
  .chat-window-header { width: 30px; }
  .chat-window-title { font-size: .65rem; }
  .card-action-btn { width: 30px; height: 30px; }
  .chat-window-close { width: 30px; height: 30px; }
  .chat-input-wrapper { padding: 10px 12px; }
  .input-tool-btn { height: 32px; padding: 0 10px; }
  .chat-send-btn { width: 32px; height: 32px; }
  .modal-body { padding: 18px 20px; }
  .modal-header { padding: 16px 20px; }
  .modal-footer { padding: 14px 20px; }
}

/* -- Mobile ----------------- */
@media (max-width: 640px) {
  .header { padding: 8px 12px; min-height: 44px; }
  .logo-icon { width: 20px; height: 20px; }
  .logo-text { font-size: .85rem; }
  .theme-toggle { width: 40px; height: 40px; }
  .main { padding: 8px; }

  /* Full-width windows, leave room for input */
  .chat-window {
    width: calc(100vw - 12px); min-width: 0;
    left: 6px !important; top: 44px !important;
    /* main height minus top offset 44, input bar 52, bottom padding 24, gap 12,
       and the bottom safe-area inset in full-screen standalone mode */
    height: calc(100% - 132px - constant(safe-area-inset-bottom));
    height: calc(100% - 132px - env(safe-area-inset-bottom));
    border-radius: 12px;
  }
  .chat-window-header { width: 28px; }
  .chat-window-title { font-size: .8rem; line-height: 1.5; }

  /* Hide resize handles on mobile */
  .chat-resize-handle { display: none !important; }

  /* Compact card tree — top-left */
  .card-tree { top: 67px; left: 4px; right: auto; bottom: auto; max-height: 200px; max-width: 140px; }
  .card-node { padding: 3px 6px; min-height: 28px; gap: 4px; }
  .card-node-dot { width: 4px; height: 4px; }
  .card-node-label { font-size: .58rem; max-width: 90px; }
  .card-tree-children { padding-left: 8px; margin-left: 0; }
  .card-tree-children::before,
  .card-tree-children > .card-node::before { display: none; }
  .card-tree-list { padding: 2px 0; }

  .card-tree-toggle {
    display: flex; align-items: center; justify-content: center;
    width: 100%; height: 32px;
    background: rgba(255,255,255,.04); border: none; border-bottom: 1px solid var(--border-soft);
    color: var(--text-muted); cursor: pointer;
    font-size: .6rem; font-family: var(--font-sans);
  }
  .card-tree.collapsed .card-tree-list { display: none; }
  .card-tree.collapsed { height: auto; min-width: 56px; overflow: hidden; }

  /* Touch-friendly action buttons */
  .chat-window-header { width: 40px; }
  .card-action-btn { width: 40px; height: 40px; }
  .card-action-btn svg { width: 14px; height: 14px; }
  .chat-window-close { width: 40px; height: 40px; }
  .chat-window-close svg { width: 15px; height: 15px; }

  /* Chat input */
  .chat-input-wrapper { padding: 8px 10px; gap: 6px; border-radius: 12px; }
  .chat-input { font-size: 1rem; }
  .input-tool-btn { height: 34px; padding: 0 8px; font-size: .7rem; min-width: 34px; }
  .input-tool-btn svg { width: 14px; height: 14px; }
  .chat-send-btn { width: 34px; height: 34px; }
  .chat-send-btn svg { width: 16px; height: 16px; }

  /* Messages */
  .chat-messages { padding: 10px; }
  .chat-bubble { max-width: 94%; font-size: 1rem; line-height: 1.6; padding: 11px 15px; }
  .chat-msg.system .chat-bubble { font-size: .8rem; }
  .chat-msg { margin-bottom: 14px; }

  /* Text selection toolbar \u2014 touch-friendly on mobile */
  .text-action-toolbar { gap: 4px; padding: 6px; }
  .text-action-btn { padding: 7px 14px; font-size: .8rem; }
  .text-action-btn svg { width: 15px; height: 15px; }

  /* Empty state mobile */
  .empty-state { padding: 0 15px 10px; }
  .empty-logo { width: 42px; height: 42px; margin-bottom: 8px; }
  .empty-title { font-size: 1.45rem; }
  .empty-desc { font-size: .95rem; }
  .empty-hint { font-size: .85rem; margin-bottom: 10px; }
  .empty-prompt-chip { padding: 8px 14px; font-size: .78rem; }
  .chips-refresh { min-width: 40px; height: 40px; padding: 0 10px; }

  /* History (mobile) */
  .history-section { width: 94vw; max-height: calc(100vh - 400px); }
  .history-header { width: 94vw; margin-top: 10px; }
  .history-item { padding: 12px 10px; }
  .history-title { font-size: .85rem; }
  .history-del { width: 40px; height: 40px; }
  .history-del svg { width: 16px; height: 16px; }
  .history-grip svg { width: 16px; height: 16px; }

  /* Scroll button mobile */
  .scroll-bottom-btn { right: 8px; bottom: 8px; width: 28px; height: 28px; }
  .scroll-bottom-btn svg { width: 14px; height: 14px; }
  .scroll-top-btn { right: 8px; bottom: 44px; width: 28px; height: 28px; }
  .scroll-top-btn svg { width: 14px; height: 14px; }

  /* Modal */
  .modal { max-width: calc(100% - 24px); margin: 0 12px; }
  #confirmModal .modal { max-width: calc(100% - 28px); margin: 0 14px; }
  .modal-body { padding: 14px 16px; }
  #confirmModal .modal-body { padding: 20px 18px; }
  .modal-header { padding: 12px 16px; }
  .modal-header h3 { font-size: .95rem; }
  .modal-footer { padding: 12px 16px; }
  .modal-footer .btn { padding: 12px 26px; min-height: 44px; }
  #confirmModal #confirmMessage { font-size: .95rem; line-height: 1.65; }
  .modal-input { padding: 10px 12px; }

  /* Reduce some animations for performance */
  .chat-window.entering { animation-duration: .35s; }
  .chat-msg { animation-duration: .3s; }

  /* Tooltip — narrower on mobile */
  .term-tooltip { max-width: 280px; }
  .term-tooltip-inner { padding: 8px 12px; }
}

/* ============================================
   Accessibility media queries
   (kept last so they win the cascade)
   ============================================ */
@media (prefers-reduced-motion: reduce) {
  .chat-msg { animation: none; }
  .chat-window.entering { animation: none; }
  .chat-window.exiting { animation: none; }
  .chat-window.just-focused { animation: none; }
  .card-node.active .card-node-dot,
  .card-node.active .card-node-label { animation: none; }
  .empty-prompt-chip.placeholder { animation: none; }
  .term-hint { animation: none; }
}

/* Frostier surfaces when the user asks for less transparency */
@media (prefers-reduced-transparency: reduce) {
  .header,
  .chat-window,
  .chat-input-wrapper,
  .card-tree,
  .history-item,
  .term-tooltip,
  .text-action-toolbar {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  .header { background: rgba(10,10,10,0.96); }
  .chat-window { background: rgba(22,23,26,0.98); }
  .chat-input-wrapper { background: rgba(20,21,24,0.98); }
  .card-tree { background: rgba(10,10,10,0.97); }
  .history-item { background: rgba(20,21,24,0.98); }
  .term-tooltip { background: rgba(19,20,23,0.99); }
  .text-action-toolbar { background: rgba(20,21,24,0.98); }
  :root[data-theme="light"] .header { background: rgba(245,245,247,0.98); }
  :root[data-theme="light"] .chat-window { background: rgba(255,255,255,0.99); }
  :root[data-theme="light"] .chat-input-wrapper { background: rgba(255,255,255,0.99); }
  :root[data-theme="light"] .card-tree { background: rgba(245,245,247,0.99); }
  :root[data-theme="light"] .history-item { background: rgba(255,255,255,0.99); }
  :root[data-theme="light"] .term-tooltip { background: rgba(255,255,255,0.99); }
  :root[data-theme="light"] .text-action-toolbar { background: rgba(255,255,255,0.99); }
}

/* Stronger edges and text when the user asks for more contrast */
@media (prefers-contrast: more) {
  :root { --border: #3a3c44; --border-hover: #50525c; }
  :root[data-theme="light"] { --border: #9a9aa2; --border-hover: #6f6f78; }
  .header,
  .chat-input-wrapper,
  .card-tree,
  .history-item,
  .term-tooltip,
  .modal { border-width: 2px; }
}

/* ============================================
   Safe area (full-screen devices, Apple HIG)
   Kept last so responsive overrides cannot win.
   ============================================ */
@supports (padding-bottom: constant(safe-area-inset-bottom)) {
  .header { padding-top: calc(14px + constant(safe-area-inset-top)); }
  .main { padding-bottom: calc(24px + constant(safe-area-inset-bottom)); }
  .card-tree { bottom: calc(80px + constant(safe-area-inset-bottom)); }
}
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .header { padding-top: calc(14px + env(safe-area-inset-top)); }
  .main { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
  .card-tree { bottom: calc(80px + env(safe-area-inset-bottom)); }
}
@media (max-width: 640px) {
  .header { padding-top: calc(8px + env(safe-area-inset-top)); }
  .card-tree { bottom: auto; }
}

/* Keyboard open (mobile): drop the bottom safe-area inset so the input bar
   hugs the keyboard and the card keeps a clean 12px gap above it. */
@media (max-width: 640px) {
  body.keyboard-open .main { padding-bottom: 8px; }
  body.keyboard-open .chat-window { height: calc(100% - 116px); }
}
