/* ============================================================
   CONSIGLIERE — v0.2 Phase 2 (editorial + density)
   Без AI-декораций. Источники: Stripe / Mercury / Bloomberg Terminal.
   ============================================================ */

:root,
[data-theme="light"] {
  --bg:            #f8f7f3;
  --bg-page:       #ffffff;
  --surface:       #ffffff;
  --surface-subtle:#f4f2ec;
  --surface-raised:#ffffff;
  --border:        #e4e0d5;
  --border-soft:   #ede9de;
  --border-strong: #cfc9b9;
  --rule:          #0f172a;

  --text:          #0f172a;
  --text-secondary:#3f4654;
  --text-muted:    #6b7280;
  --text-dim:      #9ca0aa;

  --brand-deep:    #0b2447;
  --brand:         #1e3a8a;
  --brand-mid:     #2563eb;
  --brand-hover:   #1d4ed8;
  --brand-soft:    rgba(30, 58, 138, 0.08);

  --positive:      #0f6b4c;
  --positive-soft: rgba(15, 107, 76, 0.09);
  --concerning:    #8a5a1a;
  --concerning-soft: rgba(138, 90, 26, 0.09);
  --alarming:      #7f1d1d;
  --alarming-soft: rgba(127, 29, 29, 0.09);

  --shadow-xs:  0 1px 2px rgba(15, 23, 42, 0.03);
  --shadow-sm:  0 0 0 1px rgba(15, 23, 42, 0.04);
  --shadow-md:  0 4px 16px rgba(15, 23, 42, 0.06), 0 0 0 1px rgba(15, 23, 42, 0.04);
  --shadow-lg:  0 20px 48px rgba(15, 23, 42, 0.1), 0 0 0 1px rgba(15, 23, 42, 0.04);

  --chart-bar: #1e3a8a;
  --chart-bar-from: #2563eb;
  --chart-bar-to: #1e3a8a;
  --chart-grid: #ece8dc;
  --chart-axis: #9ca0aa;
  --chart-positive: #0f6b4c;
  --chart-alarming: #7f1d1d;
  --chart-concerning: #8a5a1a;
  --chart-reference: #6b7280;
}

[data-theme="dark"] {
  --bg:            #05070d;
  --bg-page:       #05070d;
  --surface:       #0d1117;
  --surface-subtle:#0a0d13;
  --surface-raised:#151b24;
  --border:        #1f2832;
  --border-soft:   #181f28;
  --border-strong: #2d3744;
  --rule:          #f1f5f9;

  --text:          #f1efe8;
  --text-secondary:#b5b2a8;
  --text-muted:    #7c8089;
  --text-dim:      #52575f;

  --brand-deep:    #1e3a8a;
  --brand:         #3b82f6;
  --brand-mid:     #60a5fa;
  --brand-hover:   #93c5fd;
  --brand-soft:    rgba(59, 130, 246, 0.14);

  --positive:      #3bb983;
  --positive-soft: rgba(59, 185, 131, 0.15);
  --concerning:    #b8945a;
  --concerning-soft: rgba(184, 148, 90, 0.14);
  --alarming:      #c47474;
  --alarming-soft: rgba(196, 116, 116, 0.14);

  --shadow-xs:  0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-sm:  0 0 0 1px rgba(255, 255, 255, 0.04);
  --shadow-md:  0 4px 16px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.05);
  --shadow-lg:  0 20px 48px rgba(0, 0, 0, 0.65);

  --chart-bar: #60a5fa;
  --chart-bar-from: #93c5fd;
  --chart-bar-to: #3b82f6;
  --chart-grid: #1a2230;
  --chart-axis: #7c8089;
  --chart-positive: #3bb983;
  --chart-alarming: #c47474;
  --chart-concerning: #b8945a;
  --chart-reference: #7c8089;
}

/* ---- SLATE — прохладный светлый, scandinavian ---- */
[data-theme="slate"] {
  --bg:            #f7f8fa;
  --bg-page:       #ffffff;
  --surface:       #ffffff;
  --surface-subtle:#eef0f4;
  --surface-raised:#ffffff;
  --border:        #dee2e8;
  --border-soft:   #e7eaef;
  --border-strong: #c9cfd6;
  --rule:          #111827;

  --text:          #111827;
  --text-secondary:#374151;
  --text-muted:    #6b7280;
  --text-dim:      #9ca3af;

  --brand-deep:    #1e293b;
  --brand:         #334155;
  --brand-mid:     #475569;
  --brand-hover:   #1e293b;
  --brand-soft:    rgba(51, 65, 85, 0.08);

  --positive:      #047857;
  --positive-soft: rgba(4, 120, 87, 0.09);
  --concerning:    #b45309;
  --concerning-soft: rgba(180, 83, 9, 0.09);
  --alarming:      #991b1b;
  --alarming-soft: rgba(153, 27, 27, 0.09);

  --shadow-xs:  0 1px 2px rgba(17, 24, 39, 0.04);
  --shadow-sm:  0 0 0 1px rgba(17, 24, 39, 0.05);
  --shadow-md:  0 4px 16px rgba(17, 24, 39, 0.07), 0 0 0 1px rgba(17, 24, 39, 0.04);
  --shadow-lg:  0 20px 48px rgba(17, 24, 39, 0.12), 0 0 0 1px rgba(17, 24, 39, 0.05);

  --chart-bar: #334155;
  --chart-bar-from: #475569;
  --chart-bar-to: #1e293b;
  --chart-grid: #e7eaef;
  --chart-axis: #9ca3af;
  --chart-positive: #047857;
  --chart-alarming: #991b1b;
  --chart-concerning: #b45309;
  --chart-reference: #6b7280;
}

/* ---- INDIGO — deep navy фон + indigo акцент (Mercury-style) ---- */
[data-theme="indigo"] {
  --bg:            #0b0f1a;
  --bg-page:       #0b0f1a;
  --surface:       #141a2b;
  --surface-subtle:#101527;
  --surface-raised:#1c2339;
  --border:        #24304c;
  --border-soft:   #1a2237;
  --border-strong: #324066;
  --rule:          #e2e8f0;

  --text:          #e8eaf2;
  --text-secondary:#a8aec5;
  --text-muted:    #7a8299;
  --text-dim:      #525c75;

  --brand-deep:    #4338ca;
  --brand:         #6366f1;
  --brand-mid:     #818cf8;
  --brand-hover:   #a5b4fc;
  --brand-soft:    rgba(99, 102, 241, 0.15);

  --positive:      #34d399;
  --positive-soft: rgba(52, 211, 153, 0.14);
  --concerning:    #fbbf24;
  --concerning-soft: rgba(251, 191, 36, 0.13);
  --alarming:      #fb7185;
  --alarming-soft: rgba(251, 113, 133, 0.13);

  --shadow-xs:  0 1px 2px rgba(0, 0, 0, 0.5);
  --shadow-sm:  0 0 0 1px rgba(255, 255, 255, 0.04);
  --shadow-md:  0 4px 16px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(255, 255, 255, 0.05);
  --shadow-lg:  0 20px 48px rgba(0, 0, 0, 0.7);

  --chart-bar: #818cf8;
  --chart-bar-from: #a5b4fc;
  --chart-bar-to: #6366f1;
  --chart-grid: #1a2237;
  --chart-axis: #7a8299;
  --chart-positive: #34d399;
  --chart-alarming: #fb7185;
  --chart-concerning: #fbbf24;
  --chart-reference: #7a8299;
}

:root {
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-serif: "Source Serif 4", "Source Serif Pro", "Charter", "Georgia", serif;
  --font-mono: "JetBrains Mono", "Menlo", "Consolas", monospace;

  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 24px;
  --s-6: 32px;
  --s-7: 48px;
  --s-8: 64px;
  --s-9: 96px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: "ss01", "cv11";
  transition: background-color 0.2s ease, color 0.2s ease;
}

.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 var(--s-6) 240px;
}

/* В режиме вкладки «Чат» убираем нижний отступ — ChatDock не рендерится, */
/* а sticky input-row должен реально достигать низа экрана. */
.app.app-chat {
  padding-bottom: 0;
  max-width: 1400px;
}
.app.app-chat::after,
body:has(.app.app-chat)::after {
  display: none !important;
}

/* Sidebar layout — когда sidebar присутствует (любая view кроме Welcome) */
.app.has-sidebar {
  max-width: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}
.app.has-sidebar .app-main {
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 0 var(--s-6) 240px;
  width: 100%;
  justify-self: stretch;
}
/* content inner — central column с ограничением ширины и центрированием
   без пустых полей слева (они портят восприятие когда sidebar уже занял
   левую часть экрана) */
.app.has-sidebar .app-main > *:not(.topbar) {
  max-width: 1280px;
  width: 100%;
  align-self: center;
}
.app.has-sidebar.app-chat .app-main {
  padding-bottom: 0;
}
.app.has-sidebar.app-chat .app-main > *:not(.topbar) {
  max-width: 1400px;
}

/* Sidebar */
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 56px 10px 16px;
  border-right: 0.5px solid var(--border);
  background: color-mix(in srgb, var(--bg-page) 86%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 40;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px 18px;
  border-bottom: 0.5px solid var(--border-soft);
  margin-bottom: 8px;
}
.sidebar-brand-mark {
  font-size: 18px;
  color: var(--brand);
  line-height: 1;
}
.sidebar-brand-name {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}
.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.sidebar-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 12px;
  border: 0;
  background: transparent;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s ease;
}
.sidebar-item:hover {
  background: var(--surface-subtle);
  color: var(--text);
}
.sidebar-item.active {
  background: var(--brand-soft);
  color: var(--brand);
}
.sidebar-item-icon {
  width: 18px;
  height: 18px;
  display: inline-flex;
  flex-shrink: 0;
}
.sidebar-item-icon svg {
  width: 100%;
  height: 100%;
}
.sidebar-item-label {
  flex: 1;
}

/* Sidebar footer (logout) — прижимаем к низу */
.sidebar-footer {
  margin-top: auto;
  padding-top: 12px;
  border-top: 0.5px solid var(--border-soft);
}
.sidebar-logout {
  color: var(--text-muted);
  font-size: 13px;
}
.sidebar-logout:hover {
  color: var(--alarming);
  background: var(--alarming-soft);
}

/* ================ Login screen ================ */
.login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 200;
}
.login-card {
  background: var(--surface);
  border: 0.5px solid var(--border);
  border-radius: 20px;
  padding: 40px 36px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.login-title {
  font-family: var(--font-serif);
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text);
  text-align: center;
}
.login-subtitle {
  font-size: 14px;
  color: var(--text-muted);
  text-align: center;
  margin-top: -12px;
}
.login-input {
  padding: 12px 16px;
  border-radius: 12px;
  border: 0.5px solid var(--border);
  background: var(--surface-subtle);
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  transition: all 0.15s;
}
.login-input:focus {
  outline: none;
  border-color: var(--brand);
  background: var(--surface);
  box-shadow: 0 0 0 3px var(--brand-soft);
}
.login-btn {
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--brand);
  color: #fff;
  border: 0;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}
.login-btn:hover:not(:disabled) {
  background: var(--brand-hover);
}
.login-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.login-error {
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--alarming-soft);
  color: var(--alarming);
  font-size: 13px;
  border: 0.5px solid var(--alarming);
}
.login-hint {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.5;
  margin-top: 8px;
}
.app-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  color: var(--text-muted);
  font-size: 14px;
}

/* Responsive: sidebar -> bottom nav на узких экранах */
@media (max-width: 900px) {
  .app.has-sidebar {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    height: auto;
    flex-direction: row;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    border-right: 0;
    border-top: 0.5px solid var(--border);
    z-index: 60;
  }
  .sidebar-brand { display: none; }
  .sidebar-nav {
    flex-direction: row;
    width: 100%;
    justify-content: space-around;
    gap: 0;
  }
  .sidebar-item {
    flex-direction: column;
    gap: 4px;
    font-size: 10px;
    padding: 6px 12px;
  }
  .sidebar-item-icon {
    width: 22px;
    height: 22px;
  }
  .app.has-sidebar .app-main {
    padding-bottom: 88px;
  }
}

body::after {
  content: "";
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 240px;
  pointer-events: none;
  z-index: 40;
  background: linear-gradient(
    to bottom,
    transparent 0%,
    color-mix(in srgb, var(--bg) 30%, transparent) 35%,
    color-mix(in srgb, var(--bg) 78%, transparent) 58%,
    color-mix(in srgb, var(--bg) 96%, transparent) 80%,
    var(--bg) 100%
  );
}

/* ============ TOP BAR ============ */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--s-4) 0;
  margin-bottom: var(--s-7);
  border-bottom: 1px solid var(--border-soft);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 20;
}

.topbar-brand {
  display: flex;
  align-items: baseline;
  gap: var(--s-2);
  min-height: 36px;
}

.topbar-brand-name {
  font-family: var(--font-serif);
  font-size: 20px;
  font-weight: 500;
  font-style: italic;
  letter-spacing: -0.01em;
  color: var(--text);
  line-height: 1;
}

.topbar-meta {
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1;
}

.topbar-actions { display: flex; gap: var(--s-2); align-items: center; }

.view-toggle {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
  height: 34px;
  align-items: center;
}

.view-toggle button {
  padding: 0 12px;
  height: 26px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 5px;
  transition: all 0.15s ease;
  letter-spacing: -0.005em;
  line-height: 1;
}

.view-toggle button.active { background: var(--text); color: var(--bg-page); }
.view-toggle button:not(.active):hover { color: var(--text); background: var(--surface-subtle); }

.icon-btn {
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: all 0.12s ease;
}

.icon-btn:hover { border-color: var(--border-strong); color: var(--text); background: var(--surface-subtle); }

/* ============ THEME PICKER ============ */

.theme-picker-wrap {
  position: relative;
  display: inline-block;
}

.theme-trigger {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px 0 4px;
  height: 34px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 12px;
  cursor: pointer;
  border-radius: 8px;
  letter-spacing: -0.005em;
  transition: all 0.12s ease;
}

.theme-trigger:hover { border-color: var(--border-strong); color: var(--text); background: var(--surface-subtle); }

.theme-swatch {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  display: inline-block;
  flex-shrink: 0;
}

.theme-popover {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 220px;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  padding: 6px;
  z-index: 100;
  display: none;
  animation: popover-in 0.15s ease-out;
}

.theme-popover.open { display: block; }

@keyframes popover-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.theme-option {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  cursor: pointer;
  border-radius: 7px;
  text-align: left;
  transition: background 0.12s ease;
  height: auto;
  line-height: 1.3;
}

.theme-option:hover { background: var(--surface-subtle); }

.theme-option.selected { background: var(--brand-soft); color: var(--brand); }

.theme-option-swatch {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  flex-shrink: 0;
  position: relative;
}

.theme-option-swatch::after {
  content: "";
  position: absolute;
  inset: 4px;
  border-radius: 50%;
  background: var(--brand);
}

.theme-option-label {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.theme-option-name { font-weight: 500; color: var(--text); }
.theme-option-description { font-size: 11px; color: var(--text-muted); }

.theme-option.selected .theme-option-name { color: var(--brand); }

/* ============ SETTINGS TRIGGER ============ */

.settings-trigger {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  height: 34px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 12px;
  cursor: pointer;
  border-radius: 8px;
  letter-spacing: -0.005em;
  transition: all 0.12s ease;
  max-width: 280px;
}

.settings-trigger:hover {
  border-color: var(--brand);
  color: var(--brand);
  background: var(--brand-soft);
}

.settings-icon {
  font-size: 13px;
  line-height: 1;
  flex-shrink: 0;
}

.settings-label-client {
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 180px;
}

.settings-label-sep {
  color: var(--text-dim);
  font-weight: 400;
}

.settings-label-date {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ============ SETTINGS MODAL ============ */

.settings-backdrop {
  position: fixed;
  inset: 0;
  z-index: 99;
  background: rgba(15, 23, 42, 0.35);
  backdrop-filter: blur(3px);
  overflow-y: auto;
}

[data-theme="dark"] .settings-backdrop,
[data-theme="indigo"] .settings-backdrop { background: rgba(0, 0, 0, 0.55); }

.settings-modal {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  width: 460px;
  max-width: calc(100vw - 40px);
  margin: 80px auto 40px;
  display: block;
  min-height: 320px;
}

.settings-body {
  padding: var(--s-5) !important;
  display: block !important;
  min-height: 180px;
}

.settings-field {
  display: block !important;
  margin-bottom: var(--s-5);
}

.settings-field:last-child {
  margin-bottom: 0;
}

.settings-label {
  display: block !important;
  margin-bottom: var(--s-2);
}

.settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--s-4) var(--s-5);
  border-bottom: 1px solid var(--border-soft);
  position: relative;
}

.settings-title {
  font-family: var(--font-serif);
  font-size: 18px;
  font-weight: 500;
  color: var(--text);
}

.settings-body {
  padding: var(--s-5);
  display: flex;
  flex-direction: column;
  gap: var(--s-5);
}

.settings-field {
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
}

.settings-label {
  font-size: 10px;
  color: var(--text-muted);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-weight: 600;
  font-family: var(--font-sans);
}

.settings-hint {
  font-size: 11px;
  color: var(--text-dim);
  font-family: var(--font-sans);
  margin-top: 2px;
}

.settings-date-input {
  padding: 8px 12px;
  height: 38px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  border-radius: 7px;
  outline: none;
  font-variant-numeric: tabular-nums;
  width: 100%;
}

.settings-date-input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }

.settings-footer {
  display: flex;
  justify-content: flex-end;
  padding: var(--s-3) var(--s-5) var(--s-4);
  border-top: 1px solid var(--border-soft);
}

.client-options {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.client-option {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: 10px 12px;
  background: transparent;
  border: 1px solid var(--border-soft);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  cursor: pointer;
  border-radius: 7px;
  text-align: left;
  transition: all 0.12s ease;
  height: auto;
  width: 100%;
}

.client-option:hover { background: var(--surface-subtle); border-color: var(--border-strong); }

.client-option.selected {
  border-color: var(--brand);
  background: var(--brand-soft);
  color: var(--brand);
}

.client-option-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.client-option.selected .client-option-dot { background: var(--brand); }

.client-option-name { flex: 1; font-weight: 500; }

.client-option-badge {
  font-family: var(--font-mono);
  font-size: 9px;
  letter-spacing: 0.08em;
  padding: 2px 6px;
  background: var(--concerning-soft);
  color: var(--concerning);
  border-radius: 3px;
  font-weight: 600;
}

.client-empty {
  padding: 14px 12px;
  text-align: center;
  color: var(--text-muted);
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 13px;
  border: 1px dashed var(--border);
  border-radius: 7px;
}

.client-empty.error {
  color: var(--alarming);
  border-color: color-mix(in srgb, var(--alarming) 40%, transparent);
  background: var(--alarming-soft);
  font-style: normal;
  font-family: var(--font-sans);
}

/* Swatch previews — each theme has distinctive swatch */
.theme-swatch.paper, .theme-option-swatch.paper { background: #f8f7f3; border-color: #cfc9b9; }
.theme-swatch.paper::after { content: none; }
.theme-option-swatch.paper::after { background: #1e3a8a; }

.theme-swatch.slate, .theme-option-swatch.slate { background: #f7f8fa; border-color: #c9cfd6; }
.theme-option-swatch.slate::after { background: #334155; }

.theme-swatch.dark, .theme-option-swatch.dark { background: #05070d; border-color: #2d3744; }
.theme-option-swatch.dark::after { background: #3b82f6; }

.theme-swatch.indigo, .theme-option-swatch.indigo { background: #0b0f1a; border-color: #324066; }
.theme-option-swatch.indigo::after { background: #6366f1; }

.theme-swatch.apple, .theme-option-swatch.apple { background: #f5f5f7; border-color: rgba(60, 60, 67, 0.24); }
.theme-option-swatch.apple::after { background: #0071e3; }

.theme-swatch.apple-dark, .theme-option-swatch.apple-dark { background: #000000; border-color: rgba(84, 84, 88, 0.55); }
.theme-option-swatch.apple-dark::after { background: #0a84ff; }

/* ============ WELCOME ============ */

.welcome-stub {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 70vh;
  padding: 24px;
}
.welcome-stub-card {
  max-width: 640px;
  width: 100%;
  text-align: center;
}
.welcome-stub-title {
  font-size: 32px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0 0 12px;
  color: var(--text);
}
.welcome-stub-sub {
  font-size: 16px;
  color: var(--text-muted);
  margin-bottom: 36px;
}
.welcome-stub-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
@media (max-width: 600px) {
  .welcome-stub-actions { grid-template-columns: 1fr; }
}
.welcome-stub-btn {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: flex-start;
  justify-content: center;
  padding: 18px 22px;
  min-height: 88px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease;
  text-align: left;
  font: inherit;
  line-height: 1.3;
}
.welcome-stub-btn > * { display: block; width: 100%; }
.welcome-stub-btn:hover {
  transform: translateY(-1px);
  border-color: var(--accent, #0066cc);
  background: var(--surface-subtle);
}
.welcome-stub-btn.primary {
  background: var(--accent, #0066cc);
  border-color: var(--accent, #0066cc);
  color: #fff;
}
.welcome-stub-btn.primary:hover {
  background: var(--accent, #0066cc);
  filter: brightness(1.08);
}
.welcome-stub-btn-title { font-size: 16px; font-weight: 600; }
.welcome-stub-btn-sub { font-size: 13px; opacity: 0.7; }

.welcome { padding-top: var(--s-8); max-width: 720px; }

.welcome .greeting {
  font-family: var(--font-serif);
  font-size: 44px;
  font-weight: 400;
  color: var(--text);
  margin: 0 0 var(--s-6);
  letter-spacing: -0.02em;
  line-height: 1.12;
}

.subhead {
  font-size: 10px;
  color: var(--text-muted);
  margin-bottom: var(--s-3);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-weight: 600;
}

.bullets { list-style: none; padding: 0; margin: 0 0 var(--s-6); }

.bullet {
  padding: var(--s-4) var(--s-5);
  margin-bottom: var(--s-2);
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-left: 3px solid var(--text-muted);
  border-radius: 6px;
  font-family: var(--font-serif);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  box-shadow: var(--shadow-xs);
}

.bullet.positive { border-left-color: var(--positive); }
.bullet.concerning { border-left-color: var(--concerning); }
.bullet.alarming { border-left-color: var(--alarming); }

.refs { margin-top: var(--s-2); font-size: 11px; color: var(--text-muted); font-family: var(--font-sans); }

.ref-tag {
  display: inline-block;
  padding: 2px 8px;
  margin-right: var(--s-1);
  margin-top: 3px;
  background: var(--surface-subtle);
  border: 1px solid var(--border-soft);
  border-radius: 6px;
  cursor: pointer;
  font-size: 10px;
  color: var(--text-muted);
  transition: all 0.12s ease;
  font-variant-numeric: tabular-nums;
  font-family: var(--font-mono);
}

.ref-tag:hover { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }

.cta {
  margin-top: var(--s-6);
  display: flex;
  align-items: center;
  gap: var(--s-3);
}

.cta-text {
  font-family: var(--font-serif);
  font-size: 17px;
  color: var(--text-secondary);
  margin-right: auto;
  line-height: 1.4;
}

button {
  padding: 0 var(--s-4);
  height: 36px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 7px;
  transition: all 0.12s ease;
  letter-spacing: -0.005em;
  line-height: 1;
  white-space: nowrap;
}

button:hover { border-color: var(--border-strong); background: var(--surface-subtle); }

button.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: white;
}

button.primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); }

.loading, .error {
  padding: 120px 20px;
  text-align: center;
  color: var(--text-muted);
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 17px;
}

.error { color: var(--alarming); }

.error-card {
  max-width: 480px;
  margin: 120px auto;
  display: flex;
  gap: 16px;
  padding: 24px;
  border: 1px solid color-mix(in srgb, var(--alarming) 30%, var(--border));
  border-radius: 12px;
  background: var(--panel);
  align-items: flex-start;
}

.error-card-icon {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--alarming) 15%, transparent);
  color: var(--alarming);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 18px;
  flex-shrink: 0;
}

.error-card-body { flex: 1; min-width: 0; }

.error-card-title {
  font-weight: 600;
  font-size: 15px;
  color: var(--text);
  margin-bottom: 6px;
}

.error-card-message {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.5;
  margin-bottom: 16px;
}

.error-card-retry {
  font-size: 13px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
  transition: border-color 150ms, background 150ms;
}

.error-card-retry:hover {
  border-color: var(--brand);
  background: color-mix(in srgb, var(--brand) 6%, transparent);
}

.dev-banner {
  background: var(--concerning-soft);
  border: 1px solid color-mix(in srgb, var(--concerning) 40%, transparent);
  color: var(--concerning);
  padding: var(--s-2) var(--s-4);
  margin-bottom: var(--s-5);
  border-radius: 6px;
  font-size: 10px;
  font-family: var(--font-mono);
  letter-spacing: 0.04em;
}

/* ============ NARRATIVE REPORT ============ */

.report-narrative {
  max-width: 780px;
  margin: 0 auto;
  animation: page-fade-in 0.4s ease-out;
}

@keyframes page-fade-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.report-header {
  padding-bottom: var(--s-7);
  margin-bottom: var(--s-8);
  border-bottom: 1px solid var(--rule);
}

.report-eyebrow {
  font-size: 10px;
  color: var(--text-muted);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  margin-bottom: var(--s-5);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: var(--s-2);
  flex-wrap: wrap;
  line-height: 1.4;
  font-variant-numeric: tabular-nums;
}

.report-eyebrow > span:first-child { color: var(--text); }
.report-eyebrow .sep { color: var(--text-dim); }
.source-tag {
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0;
}

.report-title {
  font-family: var(--font-serif);
  font-size: 56px;
  font-weight: 400;
  letter-spacing: -0.03em;
  line-height: 1.05;
  margin: 0 0 var(--s-5);
  color: var(--text);
}

.report-summary {
  font-family: var(--font-serif);
  font-size: 19px;
  line-height: 1.55;
  color: var(--text);
  margin: 0;
  max-width: 64ch;
  font-weight: 400;
  letter-spacing: -0.01em;
}

.report-narrative .report-body {
  display: flex;
  flex-direction: column;
  gap: var(--s-8);
}

.section {
  animation: section-fade-in 0.5s ease-out backwards;
  position: relative;
}

.section:nth-child(1) { animation-delay: 0.05s; }
.section:nth-child(2) { animation-delay: 0.1s; }
.section:nth-child(3) { animation-delay: 0.15s; }
.section:nth-child(4) { animation-delay: 0.2s; }
.section:nth-child(5) { animation-delay: 0.25s; }
.section:nth-child(6) { animation-delay: 0.3s; }
.section:nth-child(7) { animation-delay: 0.35s; }
.section:nth-child(8) { animation-delay: 0.4s; }

@keyframes section-fade-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.section-head {
  margin-bottom: var(--s-4);
  padding-top: var(--s-3);
  border-top: 1px solid var(--border-soft);
  display: flex;
  align-items: flex-start;
  gap: var(--s-4);
}

.section-num {
  font-family: var(--font-serif);
  font-size: 13px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  padding-top: 2px;
  letter-spacing: 0.05em;
  min-width: 24px;
  font-style: italic;
}

.section-head-text { flex: 1; min-width: 0; }

.section-title {
  font-family: var(--font-sans);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin: 0 0 3px;
  line-height: 1.4;
}

.section-subtitle {
  font-size: 11px;
  color: var(--text-dim);
  font-family: var(--font-sans);
  line-height: 1.4;
  font-variant-numeric: tabular-nums;
}

.section-narrative {
  font-family: var(--font-serif);
  font-size: 16px;
  line-height: 1.6;
  color: var(--text-secondary);
  margin: var(--s-4) 0 0;
  max-width: 62ch;
  padding-left: var(--s-4);
  border-left: 1px solid var(--border);
}

.section.narrative p {
  font-family: var(--font-serif);
  font-size: 20px;
  line-height: 1.55;
  color: var(--text);
  margin: 0;
  max-width: 62ch;
  padding-left: var(--s-5);
  border-left: 2px solid var(--brand);
  font-weight: 400;
  letter-spacing: -0.01em;
}

/* ============ KPI ============ */

.kpi {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  padding: var(--s-5);
  box-shadow: var(--shadow-xs);
  cursor: pointer;
  transition: all 0.15s ease;
  position: relative;
}

.kpi:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
  border-color: var(--border-strong);
}

.kpi-head {
  display: flex;
  align-items: flex-start;
  gap: var(--s-3);
  margin-bottom: var(--s-3);
}

.kpi-title {
  font-size: 10px;
  color: var(--text-muted);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-weight: 700;
  line-height: 1.4;
  flex: 1;
}

.kpi-value {
  font-family: var(--font-serif);
  font-size: 40px;
  font-weight: 400;
  letter-spacing: -0.025em;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  margin-bottom: var(--s-2);
}

.kpi-delta-row {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  font-size: 11px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.kpi-delta {
  font-family: var(--font-sans);
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--surface-subtle);
  color: var(--text-muted);
  font-weight: 600;
  letter-spacing: -0.005em;
  line-height: 1.4;
  white-space: nowrap;
  font-size: 11px;
}

.kpi-delta.up { color: var(--positive); background: var(--positive-soft); }
.kpi-delta.down { color: var(--alarming); background: var(--alarming-soft); }

.kpi-positive { border-left: 2px solid var(--positive); }
.kpi-concerning { border-left: 2px solid var(--concerning); }
.kpi-alarming { border-left: 2px solid var(--alarming); }

.kpi-narrative-wrap {
  margin-top: var(--s-3);
  padding-top: var(--s-3);
  border-top: 1px solid var(--border-soft);
  font-family: var(--font-serif);
  font-size: 14px;
  line-height: 1.55;
  color: var(--text-secondary);
}

.kpi-narrative-wrap.collapsed { display: none; }

.kpi-toggle {
  color: var(--text-dim);
  font-size: 9px;
  user-select: none;
  margin-left: 2px;
  line-height: 1;
}

/* ============ TABLE ============ */

.table-container {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  box-shadow: var(--shadow-xs);
  overflow: hidden;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-sans);
  font-size: 13px;
  table-layout: auto;
}

.data-table thead { border-bottom: 1px solid var(--border); }

.data-table th {
  text-align: left;
  padding: var(--s-3) var(--s-4);
  font-weight: 600;
  font-size: 9px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  background: var(--surface-subtle);
  line-height: 1.4;
  white-space: nowrap;
}

.data-table th:not(:first-child) { text-align: right; }

.data-table tbody tr {
  border-bottom: 1px solid var(--border-soft);
  transition: background 0.1s ease;
}

.data-table tbody tr:last-child { border-bottom: none; }
.data-table tbody tr:hover { background: var(--surface-subtle); }

.data-table td {
  padding: var(--s-2) var(--s-4);
  color: var(--text);
  font-size: 13px;
  line-height: 1.45;
  font-variant-numeric: tabular-nums;
  position: relative;
}

.data-table td:first-child {
  font-weight: 500;
  letter-spacing: -0.005em;
  color: var(--text);
  font-variant-numeric: normal;
  white-space: normal;
  word-break: break-word;
}

.data-table td:not(:first-child) {
  text-align: right;
  color: var(--text);
  font-weight: 500;
}

/* Топ-3 выделение */
.data-table tbody tr:nth-child(1) td:not(:first-child) { font-weight: 600; }
.data-table tbody tr:nth-child(2) td:not(:first-child) { font-weight: 600; }
.data-table tbody tr:nth-child(3) td:not(:first-child) { font-weight: 600; }

/* ============ CHART ============ */

.chart-container {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  padding: var(--s-5);
  overflow-x: auto;
  box-shadow: var(--shadow-xs);
}

.chart {
  width: 100%;
  height: auto;
  max-width: 100%;
  display: block;
}

.chart .gridline { stroke: var(--chart-grid); stroke-width: 1; }
.chart .axis { stroke: var(--chart-axis); stroke-width: 1; }
.chart .axis-label { fill: var(--text-muted); font-size: 10px; font-family: var(--font-sans); font-variant-numeric: tabular-nums; }
.chart .bar-label { fill: var(--text); font-size: 11px; font-family: var(--font-sans); font-variant-numeric: tabular-nums; font-weight: 500; }
.chart .category-label { fill: var(--text-secondary); font-size: 11px; font-family: var(--font-sans); }
.chart .reference-line { stroke: var(--chart-reference); stroke-width: 1; stroke-dasharray: 4 2; }

.chart rect, .chart path, .chart circle {
  transition: opacity 0.18s ease;
}

.chart g:hover > rect,
.chart g:hover > circle { opacity: 0.82; }
.chart g:hover > path:not(:first-child) { opacity: 0.82; }

/* ============ CLASSIC (FIXED GRID) ============ */

.report-classic {
  max-width: 1280px;
  margin: 0 auto;
  animation: page-fade-in 0.4s ease-out;
}

.classic-header {
  margin-bottom: var(--s-6);
  padding-bottom: var(--s-4);
  border-bottom: 1px solid var(--rule);
}

.classic-title {
  font-family: var(--font-serif);
  font-size: 36px;
  font-weight: 400;
  letter-spacing: -0.025em;
  line-height: 1.1;
  margin: 0 0 var(--s-2);
  color: var(--text);
}

.classic-subtitle {
  font-size: 15px;
  color: var(--text-secondary);
  max-width: 80ch;
  line-height: 1.55;
  font-family: var(--font-serif);
}

.classic-grid {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: var(--s-3);
}

.classic-cell {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  padding: var(--s-4);
  box-shadow: var(--shadow-xs);
  overflow: hidden;
  position: relative;
  transition: all 0.15s ease;
  min-width: 0;
}

.classic-cell:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-sm);
}

.classic-cell.span-kpi { grid-column: span 3; }
.classic-cell.span-half { grid-column: span 6; }
.classic-cell.span-eight { grid-column: span 8; }
.classic-cell.span-four { grid-column: span 4; }
.classic-cell.span-full { grid-column: 1 / -1; }

.classic-cell .section-head {
  border-top: none;
  padding-top: 0;
  margin-bottom: var(--s-2);
}

.classic-cell .section-num { display: none; }
.classic-cell .chart { max-height: 280px; }

.classic-cell .kpi {
  padding: 0;
  border: none;
  box-shadow: none;
  background: transparent;
}

.classic-cell .kpi:hover { transform: none; box-shadow: none; }

.classic-cell .kpi-value { font-size: 30px; }

.classic-cell .chart-container,
.classic-cell .table-container {
  border: none;
  box-shadow: none;
  padding: 0;
  background: transparent;
  overflow: visible;
}

.classic-cell.span-full .table-container { overflow-x: auto; }

.classic-cell .data-table {
  font-size: 12px;
}

.classic-cell .data-table th {
  padding: var(--s-2) var(--s-3);
  font-size: 9px;
}

.classic-cell .data-table td {
  padding: var(--s-2) var(--s-3);
  font-size: 12px;
}

.classic-cell .data-table td:first-child {
  font-size: 13px;
  font-family: var(--font-sans);
  font-weight: 500;
}

/* ============ FIXED DASHBOARD ============ */

.classic-cell.interactive {
  cursor: pointer;
  position: relative;
}

.classic-cell.interactive::after {
  content: "→";
  position: absolute;
  top: var(--s-3);
  right: var(--s-4);
  color: var(--text-dim);
  font-size: 12px;
  opacity: 0;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.classic-cell.interactive:hover {
  border-color: var(--brand);
}

.classic-cell.interactive:hover::after {
  opacity: 0.6;
  transform: translateX(2px);
  color: var(--brand);
}

.fixed-head { margin-bottom: var(--s-2); }

.fixed-title {
  font-family: var(--font-sans);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  line-height: 1.4;
}

.fixed-subtitle {
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 2px;
}

.fixed-kpi {
  padding: 0;
  background: transparent;
  border: none;
  box-shadow: none;
}

.fixed-kpi-title {
  font-family: var(--font-sans);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  line-height: 1.4;
  margin-bottom: var(--s-2);
}

.fixed-kpi-value {
  font-family: var(--font-serif);
  font-size: 30px;
  font-weight: 400;
  letter-spacing: -0.025em;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  margin-bottom: var(--s-2);
}

.fixed-chart-body {
  max-height: 260px;
  overflow: hidden;
}

/* ============ DRAWER ============ */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0);
  z-index: 100;
  pointer-events: none;
  transition: background 0.3s ease;
}

.drawer-backdrop.open {
  background: rgba(15, 23, 42, 0.35);
  pointer-events: auto;
  backdrop-filter: blur(2px);
}

[data-theme="dark"] .drawer-backdrop.open { background: rgba(0, 0, 0, 0.6); }

.drawer {
  position: absolute;
  top: 0;
  right: 0;
  height: 100vh;
  width: 520px;
  max-width: 100%;
  background: var(--surface-raised);
  border-left: 1px solid var(--border);
  box-shadow: -20px 0 60px rgba(15, 23, 42, 0.15);
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.drawer.open { transform: translateX(0); }

.drawer-head {
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--surface-raised) 95%, transparent);
  backdrop-filter: blur(12px);
  padding: var(--s-5) var(--s-5) var(--s-4);
  border-bottom: 1px solid var(--border-soft);
  z-index: 1;
}

.drawer-eyebrow {
  font-size: 10px;
  color: var(--text-muted);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-weight: 600;
  margin-bottom: var(--s-2);
  font-variant-numeric: tabular-nums;
}

.drawer-title {
  font-family: var(--font-serif);
  font-size: 24px;
  font-weight: 400;
  color: var(--text);
  margin: 0;
  padding-right: 40px;
  line-height: 1.2;
  letter-spacing: -0.015em;
}

.drawer-close {
  position: absolute;
  top: var(--s-4);
  right: var(--s-4);
  width: 30px;
  height: 30px;
  padding: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
}

.drawer-close:hover { border-color: var(--border-strong); color: var(--text); background: var(--surface-subtle); }

.drawer-body {
  padding: var(--s-4) var(--s-5) var(--s-5);
  display: flex;
  flex-direction: column;
  gap: var(--s-5);
  flex: 1;
}

.drawer-narrative {
  font-family: var(--font-serif);
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  margin: 0;
}

.drawer-sources-label {
  font-size: 9px;
  color: var(--text-muted);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-weight: 600;
  margin-bottom: var(--s-2);
  font-family: var(--font-sans);
}

.drawer-sources-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.drawer-doc-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  padding: var(--s-2) var(--s-3);
  background: var(--surface-subtle);
  border: 1px solid var(--border-soft);
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 12px;
  color: var(--text);
  transition: all 0.12s ease;
  text-align: left;
  height: auto;
  width: 100%;
}

.drawer-doc-row:hover { border-color: var(--brand); background: var(--brand-soft); }

.drawer-doc-label {
  flex: 1;
  min-width: 0;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--font-mono);
  font-size: 11px;
}

.drawer-doc-amount {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  font-size: 12px;
}

.drawer-chat {
  margin-top: auto;
  border-top: 1px solid var(--border-soft);
  padding-top: var(--s-4);
}

.drawer-chat-label {
  font-size: 9px;
  color: var(--text-muted);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-weight: 600;
  margin-bottom: var(--s-2);
  font-family: var(--font-sans);
}

.drawer-chat-bubble {
  background: var(--surface-subtle);
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  padding: var(--s-3);
  font-family: var(--font-serif);
  font-size: 14px;
  line-height: 1.55;
  color: var(--text);
  margin-bottom: var(--s-2);
  position: relative;
}

.drawer-chat-bubble.error { border-color: color-mix(in srgb, var(--alarming) 40%, transparent); color: var(--alarming); }

.drawer-chat-bubble-text { padding-right: 22px; white-space: pre-wrap; }

.drawer-chat-bubble-clear {
  position: absolute;
  top: 4px;
  right: 6px;
  width: 22px;
  height: 22px;
  padding: 0;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
  line-height: 1;
  border-radius: 4px;
}

.drawer-chat-bubble-clear:hover { background: var(--bg-page); color: var(--text); }

.drawer-chat-thinking {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 12px;
  color: var(--text-muted);
  padding: var(--s-2) 0;
}

.drawer-chat-input-row {
  display: flex;
  gap: var(--s-2);
  align-items: center;
}

.drawer-chat-input {
  flex: 1;
  padding: 8px var(--s-3);
  height: 36px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  border-radius: 7px;
  outline: none;
}

.drawer-chat-input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.drawer-chat-input:disabled { opacity: 0.6; cursor: not-allowed; }

.drawer-chat-send {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 7px;
  background: var(--text);
  color: var(--bg-page);
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}

.drawer-chat-send:hover { background: var(--brand); color: white; }
.drawer-chat-send:disabled { opacity: 0.4; cursor: not-allowed; }

.report-classic ~ .chat-dock-wrap { display: none; }

/* ============ SOURCE DOCS (narrative mode) ============ */

.source-docs {
  margin-top: var(--s-3);
  padding-top: var(--s-3);
  border-top: 1px dashed var(--border);
}

.source-docs-label {
  font-size: 9px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: var(--s-2);
  font-family: var(--font-sans);
}

.source-docs-list { display: flex; flex-direction: column; gap: 4px; }

.source-doc-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  font-size: 12px;
}

.source-doc-label {
  color: var(--text-secondary);
  font-family: var(--font-serif);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-chip {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  background: var(--brand-soft);
  border: 1px solid color-mix(in srgb, var(--brand) 20%, transparent);
  color: var(--brand);
  border-radius: 5px;
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  letter-spacing: 0;
  white-space: nowrap;
  line-height: 1.4;
  flex-shrink: 0;
}

.doc-chip:hover {
  background: var(--brand);
  color: white;
  border-color: var(--brand);
}

.doc-chip.disabled { opacity: 0.5; cursor: not-allowed; }
.doc-chip.disabled:hover { background: var(--brand-soft); color: var(--brand); transform: none; }

/* ============ CHAT DOCK ============ */

.chat-dock-wrap {
  position: fixed;
  bottom: var(--s-5);
  left: 50%;
  transform: translateX(-50%);
  width: calc(100% - var(--s-7));
  max-width: 680px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
}

.chat-answer {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: var(--s-3) var(--s-4);
  box-shadow: var(--shadow-md);
  max-height: 280px;
  overflow-y: auto;
  animation: answer-in 0.2s ease-out;
}

@keyframes answer-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-answer.error { border-color: color-mix(in srgb, var(--alarming) 45%, transparent); }

.chat-answer-head {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  margin-bottom: var(--s-2);
}

.chat-answer-title {
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 600;
  font-family: var(--font-sans);
  flex: 1;
}

.chat-answer-close {
  width: 22px;
  height: 22px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  border-radius: 4px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.chat-answer-close:hover { background: var(--surface-subtle); color: var(--text); }

.chat-answer-body {
  font-family: var(--font-serif);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  white-space: pre-wrap;
}

.chat-icon {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--bg-page);
  font-size: 12px;
  flex-shrink: 0;
  line-height: 1;
  font-family: var(--font-sans);
  font-weight: 600;
}

.chat-icon.small { width: 16px; height: 16px; font-size: 10px; }

.chat-thinking {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  padding: var(--s-2);
}

.chat-dock {
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px 8px 6px var(--s-3);
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  gap: var(--s-2);
  backdrop-filter: saturate(180%) blur(18px);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
  position: relative;
}

.chat-input {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 13px;
  padding: var(--s-2) 0;
  outline: none;
}

.chat-input::placeholder { color: var(--text-muted); font-family: var(--font-sans); font-size: 13px; }
.chat-input:disabled { opacity: 0.6; cursor: not-allowed; }

.chat-send {
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 6px;
  background: var(--text);
  color: var(--bg-page);
  border: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 13px;
  flex-shrink: 0;
}

.chat-send:hover { background: var(--brand); color: white; }
.chat-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ============ TABS (Отчёт / Чат) ============ */

.topbar-tabs {
  display: flex;
  gap: 2px;
  background: var(--surface-2);
  padding: 3px;
  border-radius: 8px;
  margin-left: var(--s-5);
}

.topbar-tabs .tab {
  padding: var(--s-2) var(--s-4);
  background: transparent;
  border: 0;
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.topbar-tabs .tab:hover {
  color: var(--text);
  background: var(--surface);
}

.topbar-tabs .tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

/* ============ CHAT SCREEN (full tab) ============ */

.chat-screen {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 140px);
  max-width: 900px;
  margin: 0 auto;
  padding: var(--s-4) 0;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--s-4) var(--s-2);
  display: flex;
  flex-direction: column;
  gap: var(--s-4);
}

.chat-empty {
  margin: auto;
  max-width: 520px;
  text-align: center;
  color: var(--text-muted);
  padding: var(--s-6);
}

.chat-empty-title {
  font-size: 22px;
  font-weight: 500;
  color: var(--text);
  margin-bottom: var(--s-3);
}

.chat-empty-hint {
  font-size: 15px;
  line-height: 1.6;
}

.chat-msg {
  max-width: 78%;
  padding: var(--s-3) var(--s-4);
  border-radius: 12px;
  font-size: 15px;
  line-height: 1.55;
  word-wrap: break-word;
}

.chat-msg-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  opacity: 0.65;
  margin-bottom: var(--s-2);
}

.chat-msg-body {
  white-space: pre-wrap;
}

.chat-msg.user {
  align-self: flex-end;
  background: var(--brand);
  color: white;
}
.chat-msg.user .chat-msg-label { color: rgba(255,255,255,0.75); }

.chat-msg.assistant {
  align-self: flex-start;
  background: var(--surface-2);
  color: var(--text);
}

.chat-msg.error {
  align-self: flex-start;
  background: var(--danger-bg, #fee);
  color: var(--danger, #c33);
  border: 1px solid var(--danger, #c33);
}

.chat-msg.pending {
  align-self: flex-start;
  background: transparent;
  color: var(--text-muted);
  font-style: italic;
  padding: var(--s-2) 0;
}

.chat-input-row {
  display: flex;
  gap: var(--s-3);
  padding: var(--s-3) var(--s-2) 0;
  border-top: 1px solid var(--border);
  align-items: flex-end;
}

.chat-textarea {
  flex: 1;
  resize: vertical;
  min-height: 70px;
  max-height: 220px;
  padding: var(--s-3);
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.5;
}

.chat-textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-glow, rgba(60,80,240,0.15));
}

.chat-send-btn {
  padding: var(--s-3) var(--s-5);
  background: var(--brand);
  color: white;
  border: 0;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  min-width: 120px;
}

.chat-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.chat-send-btn:hover:not(:disabled) {
  filter: brightness(1.08);
}

/* ============ RESPONSIVE ============ */

@media (max-width: 1100px) {
  .classic-cell.span-eight { grid-column: span 12; }
  .classic-cell.span-four { grid-column: span 12; }
}

@media (max-width: 900px) {
  .classic-cell.span-kpi { grid-column: span 6; }
  .classic-cell.span-half { grid-column: span 12; }
}

@media (max-width: 680px) {
  .app { padding: 0 var(--s-4) 200px; }
  .report-title { font-size: 36px; }
  .classic-title { font-size: 26px; }
  .classic-cell.span-kpi { grid-column: span 12; }
  .topbar { padding: var(--s-3) 0; }
  .topbar-brand-name { font-size: 17px; }
  .chat-dock-wrap { width: calc(100% - 24px); bottom: 16px; }
}

/* ==========================================================================
   APPLE THEME — system-like light + dark, SF Pro stack, iOS/macOS palette
   ========================================================================== */

[data-theme="apple"] {
  --bg:            #f5f5f7;
  --bg-page:       #ffffff;
  --surface:       #ffffff;
  --surface-subtle:#f2f2f7;
  --surface-raised:#ffffff;
  --border:        rgba(60, 60, 67, 0.12);
  --border-soft:   rgba(60, 60, 67, 0.06);
  --border-strong: rgba(60, 60, 67, 0.24);
  --rule:          #1d1d1f;

  --text:          #1d1d1f;
  --text-secondary:#3c3c43;
  --text-muted:    rgba(60, 60, 67, 0.6);
  --text-dim:      rgba(60, 60, 67, 0.3);

  --brand-deep:    #0040dd;
  --brand:         #0071e3;
  --brand-mid:     #0a84ff;
  --brand-hover:   #0051d5;
  --brand-soft:    rgba(0, 113, 227, 0.08);

  --positive:      #34c759;
  --positive-soft: rgba(52, 199, 89, 0.12);
  --concerning:    #ff9500;
  --concerning-soft: rgba(255, 149, 0, 0.12);
  --alarming:      #ff3b30;
  --alarming-soft: rgba(255, 59, 48, 0.1);

  --shadow-xs:  0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-sm:  0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-md:  0 4px 16px rgba(0, 0, 0, 0.08);
  --shadow-lg:  0 20px 48px rgba(0, 0, 0, 0.12);

  --chart-bar: #0071e3;
  --chart-bar-from: #0a84ff;
  --chart-bar-to: #0040dd;
  --chart-grid: rgba(60, 60, 67, 0.08);
  --chart-axis: rgba(60, 60, 67, 0.5);
  --chart-positive: #34c759;
  --chart-alarming: #ff3b30;
  --chart-concerning: #ff9500;
  --chart-reference: rgba(60, 60, 67, 0.4);
}

[data-theme="apple-dark"] {
  --bg:            #000000;
  --bg-page:       #000000;
  --surface:       #1c1c1e;
  --surface-subtle:#2c2c2e;
  --surface-raised:#2c2c2e;
  --border:        rgba(84, 84, 88, 0.35);
  --border-soft:   rgba(84, 84, 88, 0.18);
  --border-strong: rgba(84, 84, 88, 0.55);
  --rule:          #ffffff;

  --text:          #f5f5f7;
  --text-secondary:#c7c7cc;
  --text-muted:    rgba(235, 235, 245, 0.6);
  --text-dim:      rgba(235, 235, 245, 0.3);

  --brand-deep:    #0a84ff;
  --brand:         #0a84ff;
  --brand-mid:     #409cff;
  --brand-hover:   #2b88ff;
  --brand-soft:    rgba(10, 132, 255, 0.15);

  --positive:      #30d158;
  --positive-soft: rgba(48, 209, 88, 0.15);
  --concerning:    #ff9f0a;
  --concerning-soft: rgba(255, 159, 10, 0.15);
  --alarming:      #ff453a;
  --alarming-soft: rgba(255, 69, 58, 0.15);

  --shadow-xs:  0 1px 2px rgba(0, 0, 0, 0.5);
  --shadow-sm:  0 2px 6px rgba(0, 0, 0, 0.6);
  --shadow-md:  0 8px 24px rgba(0, 0, 0, 0.7);
  --shadow-lg:  0 28px 64px rgba(0, 0, 0, 0.8);

  --chart-bar: #0a84ff;
  --chart-bar-from: #409cff;
  --chart-bar-to: #0a84ff;
  --chart-grid: rgba(235, 235, 245, 0.08);
  --chart-axis: rgba(235, 235, 245, 0.5);
  --chart-positive: #30d158;
  --chart-alarming: #ff453a;
  --chart-concerning: #ff9f0a;
  --chart-reference: rgba(235, 235, 245, 0.4);
}

/* Apple-полировка применяется к обеим Apple-темам */
[data-theme="apple"],
[data-theme="apple-dark"] {
  /* San Francisco system fonts on Apple devices, fallback stack elsewhere */
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
               "Inter", "Helvetica Neue", system-ui, sans-serif;
  font-feature-settings: "ss01" 1, "cv11" 1;  /* straight digits, tabular numerals friendly */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  letter-spacing: -0.011em;  /* SF-like tightness */
}

/* ========= TopBar: translucent glass ========= */

[data-theme="apple"] .topbar,
[data-theme="apple-dark"] .topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  /* backdrop-filter отключён намеренно: он создаёт containing block для
     fixed-потомков, из-за чего modal setting-backdrop не достигает viewport.
     Эмулируем translucency через сплошной near-opaque фон. */
  background: var(--bg-page);
  border-bottom: 0.5px solid var(--border);
  padding: 12px 0;
}

[data-theme="apple"] .topbar-brand-name,
[data-theme="apple-dark"] .topbar-brand-name {
  font-weight: 600;
  letter-spacing: -0.02em;
  font-size: 17px;
}

[data-theme="apple"] .topbar-meta,
[data-theme="apple-dark"] .topbar-meta {
  color: var(--text-muted);
  font-weight: 400;
}

/* ========= Tabs: iOS segmented control ========= */

[data-theme="apple"] .topbar-tabs,
[data-theme="apple-dark"] .topbar-tabs {
  background: var(--surface-subtle);
  padding: 2px;
  border-radius: 10px;
}

[data-theme="apple"] .topbar-tabs .tab,
[data-theme="apple-dark"] .topbar-tabs .tab {
  padding: 5px 14px;
  font-size: 13px;
  font-weight: 500;
  border-radius: 8px;
  color: var(--text);
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="apple"] .topbar-tabs .tab.active,
[data-theme="apple-dark"] .topbar-tabs .tab.active {
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 0 0 0.5px rgba(0, 0, 0, 0.04);
  color: var(--text);
}

/* ========= Buttons: pill + subtle ========= */

[data-theme="apple"] .view-toggle,
[data-theme="apple-dark"] .view-toggle {
  background: var(--surface-subtle);
  border-radius: 10px;
  padding: 2px;
}

[data-theme="apple"] .view-toggle button,
[data-theme="apple-dark"] .view-toggle button {
  padding: 5px 14px;
  font-size: 13px;
  font-weight: 500;
  border-radius: 8px;
  background: transparent;
  border: 0;
  color: var(--text);
}

[data-theme="apple"] .view-toggle button.active,
[data-theme="apple-dark"] .view-toggle button.active {
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

/* ========= Cards: soft rounded ========= */

[data-theme="apple"] .kpi-card,
[data-theme="apple-dark"] .kpi-card,
[data-theme="apple"] .classic-cell,
[data-theme="apple-dark"] .classic-cell {
  border-radius: 16px;
  border: 0.5px solid var(--border);
  box-shadow: var(--shadow-sm);
}

[data-theme="apple"] .narrative-section,
[data-theme="apple-dark"] .narrative-section {
  border-radius: 20px;
  padding: 28px 32px;
}

/* ========= Chat: iMessage-style bubbles ========= */

[data-theme="apple"] .chat-msg,
[data-theme="apple-dark"] .chat-msg {
  border-radius: 18px;
  padding: 10px 14px;
  font-size: 15px;
  line-height: 1.45;
  max-width: 75%;
  box-shadow: none;
}

[data-theme="apple"] .chat-msg.user,
[data-theme="apple-dark"] .chat-msg.user {
  background: #0a84ff;
  color: #ffffff;
  border-bottom-right-radius: 4px;
}

[data-theme="apple"] .chat-msg.assistant,
[data-theme="apple-dark"] .chat-msg.assistant {
  background: var(--surface-subtle);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

[data-theme="apple"] .chat-msg.error,
[data-theme="apple-dark"] .chat-msg.error {
  background: var(--alarming-soft);
  color: var(--alarming);
  border: 0.5px solid var(--alarming);
  border-bottom-left-radius: 4px;
}

[data-theme="apple"] .chat-msg-label,
[data-theme="apple-dark"] .chat-msg-label {
  display: none;  /* Apple iMessage не показывает метку у каждого bubble */
}

[data-theme="apple"] .chat-msg.pending,
[data-theme="apple-dark"] .chat-msg.pending {
  background: var(--surface-subtle);
  color: var(--text-muted);
  font-style: normal;
  padding: 14px 18px;
}

/* Typing indicator — 3 animated dots */
.chat-typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}
.chat-typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  display: inline-block;
  animation: chat-typing-bounce 1.2s infinite ease-in-out;
}
.chat-typing i:nth-child(2) { animation-delay: 0.15s; }
.chat-typing i:nth-child(3) { animation-delay: 0.3s; }
@keyframes chat-typing-bounce {
  0%, 70%, 100% { transform: translateY(0); opacity: 0.4; }
  35% { transform: translateY(-4px); opacity: 1; }
}

/* Message timestamp */
.chat-msg-meta {
  font-size: 11px;
  opacity: 0.55;
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
}
[data-theme="apple"] .chat-msg.user .chat-msg-meta,
[data-theme="apple-dark"] .chat-msg.user .chat-msg-meta {
  color: rgba(255, 255, 255, 0.7);
}

/* SQL details block */
.chat-sql-details {
  margin-top: 10px;
  border-top: 0.5px solid var(--border);
  padding-top: 8px;
}
.chat-sql-details summary {
  cursor: pointer;
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
  user-select: none;
  list-style: none;
}
.chat-sql-details summary::-webkit-details-marker { display: none; }
.chat-sql-details summary::before {
  content: "▸ ";
  display: inline-block;
  transition: transform 0.15s;
}
.chat-sql-details[open] summary::before {
  content: "▾ ";
}
.chat-sql-list {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-sql-query {
  background: var(--bg-page);
  border: 0.5px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: "SF Mono", ui-monospace, "JetBrains Mono", Consolas, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text-secondary);
  margin: 0;
  line-height: 1.5;
}
.chat-sql-idx {
  display: block;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* Quick-prompt chips в empty state */
.chat-quick-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 24px;
  max-width: 620px;
  margin-left: auto;
  margin-right: auto;
}
.chat-quick-chip {
  padding: 8px 16px;
  border-radius: 980px;
  background: var(--surface-subtle);
  border: 0.5px solid var(--border);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}
.chat-quick-chip:hover {
  background: var(--brand-soft);
  border-color: var(--brand);
  color: var(--brand);
  transform: translateY(-1px);
}

/* ================ Markdown rendering in assistant messages ================ */

.chat-md {
  font-size: 15px;
  line-height: 1.55;
}

.chat-md > *:first-child { margin-top: 0; }
.chat-md > *:last-child { margin-bottom: 0; }

.chat-md p {
  margin: 0.6em 0;
}

.chat-md h1, .chat-md h2, .chat-md h3, .chat-md h4 {
  font-weight: 600;
  letter-spacing: -0.015em;
  margin: 1em 0 0.45em;
  line-height: 1.3;
}
.chat-md h1 { font-size: 1.35em; }
.chat-md h2 { font-size: 1.2em; }
.chat-md h3 { font-size: 1.08em; }
.chat-md h4 { font-size: 1em; }

.chat-md strong { font-weight: 600; }
.chat-md em { font-style: italic; }

.chat-md ul, .chat-md ol {
  margin: 0.6em 0;
  padding-left: 1.4em;
}
.chat-md li {
  margin: 0.25em 0;
}
.chat-md li > p {
  margin: 0.2em 0;
}

.chat-md blockquote {
  border-left: 3px solid var(--brand);
  padding: 0.3em 0 0.3em 1em;
  margin: 0.8em 0;
  color: var(--text-secondary);
  background: var(--brand-soft);
  border-radius: 0 10px 10px 0;
}

.chat-md code {
  font-family: "SF Mono", ui-monospace, "JetBrains Mono", Consolas, monospace;
  font-size: 0.9em;
  background: color-mix(in srgb, var(--text) 8%, transparent);
  padding: 1px 6px;
  border-radius: 5px;
  white-space: nowrap;
}
.chat-md pre {
  background: color-mix(in srgb, var(--text) 6%, transparent);
  border: 0.5px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0.8em 0;
}
.chat-md pre code {
  background: transparent;
  padding: 0;
  white-space: pre;
  font-size: 13px;
  line-height: 1.5;
}

.chat-md a {
  color: var(--brand);
  text-decoration: none;
  border-bottom: 1px solid color-mix(in srgb, var(--brand) 50%, transparent);
}
.chat-md a:hover { border-bottom-color: var(--brand); }

.chat-md hr {
  border: 0;
  border-top: 0.5px solid var(--border);
  margin: 1.4em 0;
}

/* Tables — Apple-like */
.chat-md table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  margin: 0.8em 0;
  font-size: 13.5px;
  font-variant-numeric: tabular-nums;
  border-radius: 10px;
  overflow: hidden;
  border: 0.5px solid var(--border);
}
.chat-md thead {
  background: color-mix(in srgb, var(--text) 5%, transparent);
}
.chat-md th {
  text-align: left;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-secondary);
  padding: 10px 14px;
  border-bottom: 0.5px solid var(--border);
  white-space: nowrap;
}
.chat-md td {
  padding: 8px 14px;
  border-bottom: 0.5px solid var(--border-soft);
  vertical-align: top;
}
.chat-md tbody tr:last-child td {
  border-bottom: 0;
}
.chat-md tbody tr:hover {
  background: color-mix(in srgb, var(--text) 3%, transparent);
}
/* Numeric columns — right-align ячейки, которые содержат только цифры */
.chat-md td:has(> :is(code)),
.chat-md th[align="right"],
.chat-md td[align="right"] {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* User bubble markdown — белый текст, corrected link color */
[data-theme="apple"] .chat-msg.user .chat-md a,
[data-theme="apple-dark"] .chat-msg.user .chat-md a {
  color: #fff;
  border-bottom-color: rgba(255, 255, 255, 0.5);
}

/* ================ Artifact card (preview в чате) + side panel ================ */

/* Chat-screen — два независимых scroll-контейнера (chat + artifact panel).
   Каждый занимает viewport-height от верха ниже topbar. */
.chat-screen {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0;
  height: calc(100vh - 80px);
  overflow: hidden;
  transition: grid-template-columns 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.chat-screen.with-artifact {
  grid-template-columns: minmax(360px, 5fr) minmax(480px, 7fr);
  gap: 20px;
  max-width: none;
}

/* Левая колонка: messages scroll, input прилипает снизу */
.chat-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
}

/* Scroll только внутри .chat-messages */
.chat-main .chat-messages {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: 12px;
  min-height: 0;
}

/* Input-row фиксируется внизу chat-main, translucent blur */
.chat-input-row {
  flex: 0 0 auto;
  background: color-mix(in srgb, var(--bg-page) 82%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  padding: 14px 16px calc(14px + env(safe-area-inset-bottom));
  border-top: 0.5px solid var(--border);
}

/* Чат уменьшается визуально — bubbles становятся `narrower`. */
.chat-screen.with-artifact .chat-msg {
  max-width: 92%;
}

/* Preview-card внутри lane */
.chat-artifact-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px 16px;
  background: var(--surface);
  border: 0.5px solid var(--border);
  border-radius: 14px;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  font-size: inherit;
  color: var(--text);
  transition: all 0.2s ease;
  width: 100%;
  max-width: 78%;
  align-self: flex-start;
}
.chat-artifact-card:hover {
  background: var(--surface-subtle);
  border-color: var(--brand);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
}
.chat-artifact-card.active {
  background: var(--brand-soft);
  border-color: var(--brand);
}

.chat-artifact-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  line-height: 18px;
}
.chat-artifact-icon {
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--brand);
}
.chat-artifact-icon svg {
  width: 100%;
  height: 100%;
  display: block;
}
.chat-artifact-kind {
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.005em;
  flex: 1;
  line-height: 18px;
}
.chat-artifact-open {
  width: 14px;
  height: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--brand);
}
.chat-artifact-open svg {
  width: 100%;
  height: 100%;
  display: block;
}
/* Если текст превью пустой — не показываем пустую строку под header'ом
   (иначе из-за gap карточка выглядит несимметрично сверху). */
.chat-artifact-preview:empty {
  display: none;
}

.chat-artifact-preview {
  color: var(--text-secondary);
  font-size: 13.5px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

/* ========= Side panel (artifact detail) ========= */

.chat-artifact-panel {
  height: 100%;
  background: var(--surface);
  border: 0.5px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
  animation: chat-panel-in 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes chat-panel-in {
  from { opacity: 0; transform: translateX(24px); }
  to { opacity: 1; transform: translateX(0); }
}

.chat-artifact-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 0.5px solid var(--border);
  background: var(--surface-subtle);
}
.chat-artifact-panel-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}
.chat-artifact-panel-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}
.chat-artifact-close {
  background: transparent;
  border: 0;
  color: var(--text-muted);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  padding: 0 6px;
  border-radius: 6px;
  transition: all 0.15s;
}
.chat-artifact-close:hover {
  background: var(--border-soft);
  color: var(--text);
}

.chat-artifact-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 24px 28px;
  font-size: 15px;
  line-height: 1.6;
}

/* Respect small screens — при узком экране панель становится модалкой поверх чата */
@media (max-width: 900px) {
  .chat-screen.with-artifact {
    grid-template-columns: 1fr;
  }
  .chat-artifact-panel {
    position: fixed;
    top: 72px;
    left: 8px;
    right: 8px;
    bottom: 8px;
    height: auto;
    z-index: 100;
  }
}

/* ================ Charts rendered by LLM (render_chart tool) ================ */

.chat-charts {
  margin: 14px 0 4px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.chat-charts .chart {
  background: var(--bg-page);
  border: 0.5px solid var(--border);
  border-radius: 14px;
  padding: 16px 18px;
  margin: 0;
}

.chat-charts .chart-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin: 0 0 14px;
  letter-spacing: -0.01em;
}

.chart-fallback {
  color: var(--text-muted);
  font-size: 13px;
  font-style: italic;
}

/* Pie */
.chart-pie-body {
  display: flex;
  gap: 20px;
  align-items: center;
}
.chart-pie-svg {
  width: 160px;
  height: 160px;
  flex-shrink: 0;
}
.chart-pie-hole {
  fill: var(--bg-page);
}
.chart-legend {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  font-size: 13px;
}
.chart-legend-row {
  display: grid;
  grid-template-columns: 12px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 4px 0;
  border-bottom: 0.5px solid var(--border-soft);
}
.chart-legend-row:last-child { border-bottom: 0; }
.chart-legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}
.chart-legend-label {
  color: var(--text);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chart-legend-value {
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  white-space: nowrap;
}

/* Bar */
.chart-bar-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chart-bar-row {
  display: grid;
  grid-template-columns: minmax(110px, 140px) 1fr minmax(80px, auto);
  gap: 12px;
  align-items: center;
  font-size: 13px;
}
.chart-bar-label {
  color: var(--text);
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chart-bar-track {
  background: var(--surface-subtle);
  border-radius: 5px;
  height: 16px;
  overflow: hidden;
}
.chart-bar-fill {
  height: 100%;
  border-radius: 5px;
  transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}
.chart-bar-value {
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  text-align: right;
}

/* Line */
.chart-line-svg {
  width: 100%;
  height: auto;
  max-height: 280px;
}
.chart-line-path {
  fill: none;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.chart-grid {
  stroke: var(--border-soft);
  stroke-width: 0.5;
}
.chart-axis {
  fill: var(--text-muted);
  font-size: 10px;
  font-variant-numeric: tabular-nums;
}

/* В user-bubble charts не должны появляться, но если вдруг — стили сохраняют читаемость */
[data-theme="apple"] .chat-msg.user .chart,
[data-theme="apple-dark"] .chat-msg.user .chart {
  background: rgba(255,255,255,0.1);
  border-color: rgba(255,255,255,0.2);
}

/* Toolbar над чатом (заголовок + clear) */
.chat-toolbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 8px 4px 12px;
}
.chat-toolbar-btn {
  padding: 6px 14px;
  border-radius: 980px;
  background: transparent;
  border: 0.5px solid var(--border);
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.chat-toolbar-btn:hover {
  background: var(--surface-subtle);
  color: var(--text);
}

/* Chat input — pill textarea + icon button */
[data-theme="apple"] .chat-input-row,
[data-theme="apple-dark"] .chat-input-row {
  border-top: 0.5px solid var(--border);
  padding-top: 16px;
  gap: 10px;
  align-items: flex-end;
}

[data-theme="apple"] .chat-textarea,
[data-theme="apple-dark"] .chat-textarea {
  border-radius: 20px;
  border: 0.5px solid var(--border);
  background: var(--surface-subtle);
  padding: 12px 18px;
  font-size: 15px;
  line-height: 1.4;
  min-height: 44px;
  box-shadow: none;
  transition: all 0.15s ease;
}

[data-theme="apple"] .chat-textarea:focus,
[data-theme="apple-dark"] .chat-textarea:focus {
  background: var(--surface);
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-soft);
  outline: none;
}

[data-theme="apple"] .chat-send-btn,
[data-theme="apple-dark"] .chat-send-btn {
  border-radius: 20px;
  padding: 10px 22px;
  font-size: 15px;
  font-weight: 500;
  background: var(--brand);
  color: #ffffff;
  border: 0;
  min-width: auto;
  box-shadow: none;
  transition: all 0.15s ease;
}

[data-theme="apple"] .chat-send-btn:hover:not(:disabled),
[data-theme="apple-dark"] .chat-send-btn:hover:not(:disabled) {
  background: var(--brand-hover);
  transform: scale(1.02);
}

[data-theme="apple"] .chat-send-btn:disabled,
[data-theme="apple-dark"] .chat-send-btn:disabled {
  background: var(--surface-subtle);
  color: var(--text-muted);
  opacity: 1;
}

/* Пустой чат */
[data-theme="apple"] .chat-empty-title,
[data-theme="apple-dark"] .chat-empty-title {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text);
}

[data-theme="apple"] .chat-empty-hint,
[data-theme="apple-dark"] .chat-empty-hint {
  color: var(--text-muted);
  font-size: 15px;
  line-height: 1.5;
  max-width: 460px;
  margin: 8px auto 0;
}

/* Tables: Apple-подобные hairline lines */
[data-theme="apple"] table,
[data-theme="apple-dark"] table {
  border-collapse: separate;
  border-spacing: 0;
}

[data-theme="apple"] th,
[data-theme="apple-dark"] th,
[data-theme="apple"] td,
[data-theme="apple-dark"] td {
  border-bottom: 0.5px solid var(--border-soft);
}

/* ========= KPI · Apple Widget-стиль ========= */
[data-theme="apple"] .kpi,
[data-theme="apple-dark"] .kpi {
  background: var(--surface);
  border: 0.5px solid var(--border);
  border-radius: 20px;
  padding: 20px 22px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 0 0 0.5px rgba(0, 0, 0, 0.02);
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
  border-left: 0.5px solid var(--border);
}
[data-theme="apple"] .kpi::before,
[data-theme="apple-dark"] .kpi::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, var(--brand-soft) 0%, transparent 50%);
  opacity: 0.5;
  pointer-events: none;
}
[data-theme="apple"] .kpi:hover,
[data-theme="apple-dark"] .kpi:hover {
  transform: translateY(-1px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
}
[data-theme="apple"] .kpi.kpi-positive,
[data-theme="apple-dark"] .kpi.kpi-positive { border-left: 0.5px solid var(--border); }
[data-theme="apple"] .kpi.kpi-concerning,
[data-theme="apple-dark"] .kpi.kpi-concerning { border-left: 0.5px solid var(--border); }
[data-theme="apple"] .kpi.kpi-alarming,
[data-theme="apple-dark"] .kpi.kpi-alarming { border-left: 0.5px solid var(--border); }
[data-theme="apple"] .kpi.kpi-positive::before,
[data-theme="apple-dark"] .kpi.kpi-positive::before {
  background: linear-gradient(180deg, var(--positive-soft) 0%, transparent 55%);
}
[data-theme="apple"] .kpi.kpi-concerning::before,
[data-theme="apple-dark"] .kpi.kpi-concerning::before {
  background: linear-gradient(180deg, var(--concerning-soft) 0%, transparent 55%);
}
[data-theme="apple"] .kpi.kpi-alarming::before,
[data-theme="apple-dark"] .kpi.kpi-alarming::before {
  background: linear-gradient(180deg, var(--alarming-soft) 0%, transparent 55%);
}
[data-theme="apple"] .kpi-head,
[data-theme="apple-dark"] .kpi-head { position: relative; z-index: 1; }
[data-theme="apple"] .kpi-title,
[data-theme="apple-dark"] .kpi-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}
[data-theme="apple"] .kpi-value,
[data-theme="apple-dark"] .kpi-value {
  font-size: 40px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  margin-top: 8px;
  color: var(--text);
  position: relative;
  z-index: 1;
}
[data-theme="apple"] .kpi-delta-row,
[data-theme="apple-dark"] .kpi-delta-row { position: relative; z-index: 1; }
[data-theme="apple"] .kpi-delta,
[data-theme="apple-dark"] .kpi-delta {
  padding: 3px 10px;
  border-radius: 980px;
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
[data-theme="apple"] .kpi-delta.up,
[data-theme="apple-dark"] .kpi-delta.up {
  background: var(--positive-soft);
  color: var(--positive);
}
[data-theme="apple"] .kpi-delta.down,
[data-theme="apple-dark"] .kpi-delta.down {
  background: var(--alarming-soft);
  color: var(--alarming);
}

/* ========= Tables · Apple Numbers-style ========= */
[data-theme="apple"] .report-table,
[data-theme="apple-dark"] .report-table,
[data-theme="apple"] .narrative-section table,
[data-theme="apple-dark"] .narrative-section table {
  border-radius: 12px;
  overflow: hidden;
  border: 0.5px solid var(--border);
  font-variant-numeric: tabular-nums;
}
[data-theme="apple"] .report-table thead th,
[data-theme="apple-dark"] .report-table thead th,
[data-theme="apple"] .narrative-section table thead th,
[data-theme="apple-dark"] .narrative-section table thead th {
  background: var(--surface-subtle);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
  padding: 10px 14px;
  border-bottom: 0.5px solid var(--border);
}
[data-theme="apple"] .report-table tbody tr:nth-child(even),
[data-theme="apple-dark"] .report-table tbody tr:nth-child(even) {
  background: color-mix(in srgb, var(--text) 2%, transparent);
}
[data-theme="apple"] .report-table tbody tr:hover,
[data-theme="apple-dark"] .report-table tbody tr:hover {
  background: var(--brand-soft);
}

/* ========= Charts · Apple Health-style ========= */
/* В Canvas-чартах напрямую цвета заданы через CSS vars (chart-bar, chart-grid и т.д.).
   Здесь полируем контейнер и legend. */
[data-theme="apple"] .chart,
[data-theme="apple-dark"] .chart,
[data-theme="apple"] .classic-cell .chart,
[data-theme="apple-dark"] .classic-cell .chart {
  background: linear-gradient(180deg, var(--brand-soft) 0%, transparent 40%),
              var(--surface);
  border: 0.5px solid var(--border);
  border-radius: 16px;
  padding: 18px;
}
[data-theme="apple"] .chart h3,
[data-theme="apple-dark"] .chart h3,
[data-theme="apple"] .chart-title,
[data-theme="apple-dark"] .chart-title {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: -0.005em;
  color: var(--text);
}

/* Bar chart в отчётах — rounded tops */
[data-theme="apple"] canvas.chart-canvas,
[data-theme="apple-dark"] canvas.chart-canvas {
  border-radius: 10px;
}

/* В chat-bar внутри chat-tab — rounded ends */
[data-theme="apple"] .chart-bar-fill,
[data-theme="apple-dark"] .chart-bar-fill {
  border-radius: 6px;
  background: linear-gradient(90deg,
    color-mix(in srgb, var(--brand) 85%, transparent) 0%,
    var(--brand) 100%);
}

/* Line chart — плавные stroke, gradient-friendly */
[data-theme="apple"] .chart-line-path,
[data-theme="apple-dark"] .chart-line-path {
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 1px 2px color-mix(in srgb, var(--brand) 35%, transparent));
}

/* Pie chart — более насыщенные segments, inner hole чуть меньше для Apple-look */
[data-theme="apple"] .chart-pie-hole,
[data-theme="apple-dark"] .chart-pie-hole {
  fill: var(--bg-page);
}

/* ============================================================
   Mobile responsive (≤ 768px)
   ============================================================ */
.topbar-burger {
  display: none;
  background: transparent;
  border: 0;
  color: var(--text);
  padding: 6px;
  margin-right: 8px;
  border-radius: 8px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
}
.topbar-burger:hover { background: var(--surface-subtle); }
.sidebar-backdrop { display: none; }

@media (max-width: 768px) {
  .topbar-burger { display: inline-flex; }

  /* Layout: убираем grid, sidebar становится overlay */
  .app.has-sidebar {
    display: block;
    grid-template-columns: none;
  }
  .app.has-sidebar .app-main {
    padding: 0 12px 120px;
  }
  .app.has-sidebar .app-main > *:not(.topbar) {
    max-width: none;
  }

  /* Sidebar — fixed overlay слева, по умолчанию скрыт */
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 240px;
    height: 100vh;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 4px 0 24px rgba(0,0,0,0.18);
  }
  .app.mobile-menu-open .sidebar {
    transform: translateX(0);
  }
  /* Backdrop при открытом меню */
  .sidebar-backdrop {
    display: block;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.4);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.22s ease;
    z-index: -1;
  }
  .app.mobile-menu-open .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  /* Topbar компактнее */
  .topbar { padding: 10px 12px; gap: 6px; flex-wrap: wrap; }
  .topbar-brand { gap: 4px; min-width: 0; flex: 1; }
  .topbar-brand-name { font-size: 18px; }
  .topbar-meta { display: none; }
  .topbar-actions { gap: 4px; flex-wrap: wrap; }
  .view-toggle button { padding: 6px 10px; font-size: 12px; }

  /* Settings/theme-picker кнопки — компактнее */
  .settings-button, .theme-picker-trigger {
    padding: 6px 8px;
    font-size: 12px;
  }

  /* Artifact panel — fullscreen overlay вместо split */
  .chat-screen.with-artifact {
    grid-template-columns: 1fr;
  }
  .chat-artifact-panel {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    z-index: 150;
    background: var(--bg-page);
    border-radius: 0;
    border-left: 0;
  }
  .chat-artifact-card { max-width: 100%; }

  /* Chat input — safe-area для iOS Home Bar */
  .chat-input-row {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  /* Welcome — кнопки колонкой (уже было через @media 600px, на 768
     просто чтобы было точно) */
  .welcome-stub-actions { grid-template-columns: 1fr; }
  .welcome-stub-title { font-size: 26px; }

  /* Таблицы — горизонтальный скролл вместо переноса */
  .chat-md table {
    display: block;
    overflow-x: auto;
    max-width: 100%;
  }

  /* Графики масштабируются по ширине */
  .chart svg { width: 100%; height: auto; }
}

/* Очень узкие экраны (≤ 380px) — iPhone SE и подобные */
@media (max-width: 380px) {
  .topbar-brand-name { font-size: 16px; }
  .topbar { padding: 8px 10px; }
  .app.has-sidebar .app-main { padding: 0 8px 100px; }
  .welcome-stub-title { font-size: 22px; }
  .welcome-stub-sub { font-size: 14px; }
}

/* ============================================================
   Mobile responsive — full coverage (≤ 768px)
   Дополнение к базовому блоку выше: login, settings, chat,
   report, метрики, iOS-specific (zoom on focus, safe-area, taps).
   ============================================================ */
@media (max-width: 768px) {
  /* iOS не зумит при focus на input если font-size >= 16px */
  input, textarea, select {
    font-size: 16px !important;
  }

  /* Кликабельные элементы — минимум 44×44 для удобства тапа (Apple HIG) */
  button {
    -webkit-tap-highlight-color: transparent;
  }

  /* Login */
  .login-screen { padding: 16px; }
  .login-card {
    padding: 28px 20px;
    border-radius: 16px;
    gap: 14px;
  }
  .login-title { font-size: 24px; }
  .login-subtitle { font-size: 13px; margin-top: -8px; }
  .login-input, .login-btn {
    padding: 14px 16px;
    border-radius: 12px;
  }

  /* Settings modal — почти fullscreen */
  .settings-modal {
    width: 100%;
    max-width: none;
    margin: 8px;
    border-radius: 14px;
    min-height: auto;
  }
  .settings-backdrop {
    padding: max(12px, env(safe-area-inset-top)) 0 max(12px, env(safe-area-inset-bottom));
  }
  .settings-body { padding: 16px !important; }

  /* Theme picker — открывается над всем, не выезжает за viewport */
  .theme-picker-wrap .theme-picker-menu {
    position: fixed;
    top: auto;
    right: 8px;
    left: 8px;
    bottom: max(60px, env(safe-area-inset-bottom));
    width: auto;
    max-height: 60vh;
    overflow-y: auto;
  }

  /* Chat — общая верстка */
  .chat-screen { padding: 0; }
  .chat-main { min-width: 0; }
  .chat-toolbar {
    padding: 8px 12px;
    gap: 6px;
    flex-wrap: wrap;
  }
  .chat-toolbar-btn {
    padding: 6px 10px;
    font-size: 13px;
  }
  .chat-messages {
    padding: 8px 12px;
    gap: 12px;
  }
  /* Bubbles чата — шире, без 70% ограничения */
  .chat-msg .chat-bubble,
  .chat-msg .chat-md {
    max-width: 100%;
  }
  .chat-msg.user .chat-bubble {
    max-width: 88%;
  }
  /* Длинные слова (UUID, длинные имена) — с переносом */
  .chat-md, .chat-bubble {
    word-break: break-word;
    overflow-wrap: anywhere;
  }
  .chat-md pre {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-size: 12px;
  }

  /* Chat textarea — без resize вручную, авто по высоте */
  .chat-textarea {
    resize: none;
    min-height: 48px;
    font-size: 16px;
    border-radius: 14px;
    padding: 12px 14px;
  }
  .chat-input-row {
    padding: 8px 10px;
    gap: 8px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    border-top: 0.5px solid var(--border);
    background: var(--bg-page);
  }
  .chat-send-btn {
    min-width: 44px;
    min-height: 44px;
  }

  /* Artifact card */
  .chat-artifact-card {
    padding: 12px 14px;
    max-width: 100%;
  }
  .chat-artifact-panel-head {
    padding: 12px 14px;
  }
  .chat-artifact-panel-body { padding: 14px; }
  .chat-artifact-close { font-size: 28px; padding: 4px 10px; }

  /* Report screen */
  .report-section, .report-grid {
    padding: 0;
    margin-bottom: 16px;
  }
  .report-section h2, .section-title {
    font-size: 18px;
  }
  /* Метрики/KPI — одна колонка на мобиле, без cramped grid */
  .metric-grid, .kpi-grid, .snapshot-grid {
    grid-template-columns: 1fr !important;
    gap: 10px;
  }
  .metric-card, .kpi-card {
    padding: 14px 16px;
  }

  /* Bullets / narrative cards */
  .bullets li, .bullet {
    padding: 12px 14px;
    font-size: 14px;
  }
  .greeting { font-size: 22px; }
  .subhead { font-size: 14px; }

  /* Welcome — компактные размеры */
  .welcome-stub { min-height: 60vh; padding: 16px; }
  .welcome-stub-card { max-width: none; }
  .welcome-stub-actions { gap: 10px; }
  .welcome-stub-btn { padding: 14px 16px; min-height: 76px; }

  /* Errors / loading — не такие большие */
  .error-card, .loading {
    padding: 20px 16px;
    font-size: 14px;
  }

  /* Charts — pie/bar/line — масштабируем и убираем фикс ширины */
  .chart {
    padding: 12px;
    margin: 12px 0;
  }
  .chart-pie-body, .chart-bar-body {
    max-width: 100%;
  }
  .chart-pie-svg, .chart-line-svg, .chart-bar-svg {
    max-width: 100%;
    height: auto;
  }
  .chart-legend {
    font-size: 12px;
    gap: 6px;
  }

  /* Quick prompts (стартовый чат) — flex wrap, поменьше */
  .chat-quick-prompts {
    gap: 6px;
    padding: 8px 12px;
  }
  .chat-quick-prompt {
    padding: 8px 12px;
    font-size: 13px;
  }
}

/* Очень узкие (≤ 380px) — iPhone SE, складные */
@media (max-width: 380px) {
  .login-card { padding: 24px 16px; }
  .login-title { font-size: 22px; }
  .chat-toolbar-btn { padding: 6px 8px; font-size: 12px; }
  .chat-msg.user .chat-bubble { max-width: 92%; }
  .chat-input-row { padding: 6px 8px; }
  .chat-textarea { padding: 10px 12px; }
  .topbar { gap: 4px; }
  .topbar-actions > * { font-size: 12px !important; padding: 5px 8px !important; }
}

/* Landscape на телефоне (низкая высота) — sidebar короче */
@media (max-width: 900px) and (max-height: 500px) {
  .sidebar { padding-top: 16px; }
  .welcome-stub { min-height: auto; }
}

/* ============================================================
   MiniApp режим (Telegram WebApp) — только чат, без sidebar/topbar.
   ============================================================ */
.app.app-miniapp {
  max-width: none;
  padding: 0;
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  /* TG WebApp header (close-button, menu) перекрывает наш контент сверху.
     --tg-header-h устанавливается JS из (screen.height − viewportHeight).
     Fallback — safe-area-inset-top для notch на чистом мобильном браузере. */
  padding-top: var(--tg-header-h, env(safe-area-inset-top, 0px));
}
.app.app-miniapp .app-main {
  padding: 0;
  flex: 1;
  min-height: 0;
}
.app.app-miniapp .chat-screen {
  height: 100%;
}
.app.app-miniapp .chat-toolbar {
  border-bottom: 0.5px solid var(--border);
}
.app.app-miniapp .chat-messages {
  padding: 12px;
}
.app.app-miniapp .chat-input-row {
  padding: 8px 10px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
/* Fixed-overlay artifact panel внутри TG WebApp — отступ от TG-controls */
html.tg-webapp .chat-artifact-panel {
  top: var(--tg-header-h, env(safe-area-inset-top, 0px));
}
html.tg-webapp .chat-artifact-panel-head {
  padding-top: 14px;
}
/* В TG WebApp Blob-download заблокирован — скрываем кнопки HTML/PDF
   везде, добавляем «Открыть в браузере» (см. open_in_browser в Rust). */
html.tg-webapp .export-html-btn,
html.tg-webapp .export-pdf-btn {
  display: none !important;
}

/* Topbar — резиновый brand с ellipsis (welcome screen + узкие экраны) */
.topbar-brand {
  min-width: 0;
  overflow: hidden;
}
.topbar-brand-name {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.topbar-actions {
  flex-shrink: 0;
}

/* На очень узких — settings/theme button-ы максимально компактные */
@media (max-width: 480px) {
  .settings-button-text,
  .theme-picker-trigger-text {
    display: none; /* оставляем только иконки */
  }
  .settings-button,
  .theme-picker-trigger {
    padding: 6px;
    min-width: 36px;
    justify-content: center;
  }
  .topbar-meta { display: none; }
}
