/* ═══════════════════════════════════════════════════════════
   SD Modeler — Layout
   Main application grid, header, toolbar, panels
   ═══════════════════════════════════════════════════════════ */

/* ── Application Shell ───────────────────────────────────── */
.app {
  display: grid;
  grid-template-rows: var(--header-height) 1fr var(--status-bar-height);
  grid-template-columns: var(--toolbar-width) 1fr var(--inspector-width);
  grid-template-areas:
    "header   header    header"
    "toolbar  canvas    inspector"
    "status   status    status";
  height: 100vh;
  width: 100vw;
  background: var(--bg-primary);
  color: var(--text-primary);
}

/* ── Header Bar ──────────────────────────────────────────── */
.header {
  grid-area: header;
  display: flex;
  align-items: center;
  padding: 0 var(--space-3);
  gap: var(--space-1);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  z-index: var(--z-header);
  -webkit-app-region: drag;
}

.header__logo {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-right: var(--space-3);
  flex-shrink: 0;
  -webkit-app-region: no-drag;
}

.header__logo-icon {
  width: 22px;
  height: 22px;
  color: var(--accent-stock);
}

.header__logo-text {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.02em;
  white-space: nowrap;
}

.header__group {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  -webkit-app-region: no-drag;
}

.header__spacer {
  flex: 1;
}

/* Editable model name */
.header__model-name-wrapper {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  -webkit-app-region: no-drag;
}

.header__model-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  outline: none;
  cursor: text;
  min-width: 60px;
  max-width: 280px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: all var(--transition-fast);
}

.header__model-name:hover {
  color: var(--text-primary);
  border-color: var(--border);
  background: var(--bg-surface);
}

.header__model-name:focus {
  color: var(--text-primary);
  border-color: var(--accent-stock);
  background: var(--bg-surface);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.15);
}

.header__edit-icon {
  width: 12px;
  height: 12px;
  color: var(--text-muted);
  opacity: 0;
  transition: opacity var(--transition-fast);
  pointer-events: none;
  flex-shrink: 0;
}

.header__model-name-wrapper:hover .header__edit-icon {
  opacity: 0.6;
}

/* ── Toolbar (Left Sidebar) ──────────────────────────────── */
.toolbar {
  grid-area: toolbar;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: var(--space-2) 0;
  gap: var(--space-1);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  z-index: var(--z-toolbar);
  overflow-y: visible;
  overflow-x: visible;
}

/* Toolbar tooltips appear on the right */
.toolbar [data-tooltip]::after {
  left: calc(100% + 8px);
  top: 50%;
  transform: translateY(-50%);
  bottom: auto;
}

/* Header tooltips appear below the buttons */
.header [data-tooltip]::after {
  top: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  bottom: auto;
  z-index: 9999;
}

/* ── Canvas Area ─────────────────────────────────────────── */
.canvas-area {
  grid-area: canvas;
  position: relative;
  overflow: hidden;
  background: var(--bg-canvas);
  cursor: default;
}

.canvas-container {
  position: absolute;
  inset: 0;
}

#sd-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Canvas grid dots */
.canvas-area::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image: radial-gradient(circle, var(--bg-canvas-dot) 1px, transparent 1px);
  background-size: 20px 20px;
  pointer-events: none;
  z-index: 0;
  opacity: 0.6;
}

/* Canvas overlay for empty state */
.canvas-empty-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 2;
}

/* ── Inspector Panel (Right Sidebar) ─────────────────────── */
.inspector {
  grid-area: inspector;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  border-left: 1px solid var(--border);
  z-index: var(--z-panel);
  overflow-y: auto;
  overflow-x: hidden;
}

.inspector__section {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
}

.inspector__section:last-child {
  border-bottom: none;
}

.inspector__field {
  margin-bottom: var(--space-3);
}

.inspector__field:last-child {
  margin-bottom: 0;
}

/* ── Minimap ── */
.minimap {
  position: absolute;
  bottom: 12px;
  right: 12px;
  width: 180px;
  height: 120px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  z-index: 20;
  opacity: 0.85;
  transition: opacity var(--transition-fast);
  cursor: pointer;
}

.minimap:hover {
  opacity: 1;
}

.minimap canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.minimap__viewport {
  position: absolute;
  border: 2px solid var(--accent);
  border-radius: 2px;
  background: rgba(59, 130, 246, 0.08);
  pointer-events: none;
}

/* Hide minimap when chart panel is open */
.chart-panel.open ~ .minimap,
.minimap.hidden {
  display: none;
}

/* ── Chart Panel (Bottom — collapsible, inside canvas-area) ── */
.chart-panel {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 280px;
  min-height: 120px;
  max-height: 80vh;
  background: var(--bg-primary);
  border-top: 1px solid var(--border);
  z-index: var(--z-panel);
  transform: translateY(100%);
  transition: transform var(--transition-slow);
  display: flex;
  flex-direction: column;
}

.chart-panel__resize-handle {
  height: 8px;
  cursor: ns-resize;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
}

.chart-panel__resize-handle:hover {
  background: var(--bg-secondary);
}

.chart-panel__resize-grip {
  width: 40px;
  height: 3px;
  border-radius: 2px;
  background: var(--border);
}

.chart-panel__resize-handle:hover .chart-panel__resize-grip {
  background: var(--accent);
}

.chart-panel.open {
  transform: translateY(0);
}

.chart-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-3);
  flex-shrink: 0;
}

.chart-panel__body {
  flex: 1;
  overflow: hidden;
  padding: var(--space-2) var(--space-3) var(--space-3);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#table-body {
  overflow: auto;
}

.chart-canvas-wrap {
  flex: 1;
  min-height: 150px;
  position: relative;
  overflow: hidden;
}

#chart-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Panel tabs */
.panel-header__tabs {
  display: flex;
  gap: 2px;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  padding: 2px;
}

.panel-tab {
  padding: 4px 12px;
  font-size: 11px;
  font-weight: 600;
  font-family: 'Inter', sans-serif;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
}

.panel-tab:hover {
  color: var(--text-primary);
}

.panel-tab.active {
  background: var(--surface);
  color: var(--text-primary);
  box-shadow: var(--shadow-sm);
}

/* Secondary header bar */
.panel-header__secondary {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Chart/Table view toggle */
.view-toggle {
  display: flex;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
  padding: 2px;
  margin-right: 6px;
}

.view-toggle__btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 22px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 3px;
  transition: all var(--transition-fast);
}

.view-toggle__btn:hover {
  color: var(--text-primary);
}

.view-toggle__btn.active {
  background: var(--surface);
  color: var(--accent);
  box-shadow: var(--shadow-sm);
}

/* Chart type selector */
.chart-type-select {
  font-size: 11px;
  font-family: 'Inter', sans-serif;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  cursor: pointer;
  margin-right: 6px;
  outline: none;
}

.chart-type-select:hover {
  border-color: var(--accent);
}

.chart-type-select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.15);
}

/* Interactive legend bar */
.chart-legend-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 8px;
  padding: 4px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  max-height: 52px;
  overflow-y: auto;
}

.chart-legend-bar:empty {
  display: none;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 6px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 10px;
  font-family: 'Inter', sans-serif;
  font-weight: 500;
  color: var(--text-secondary);
  transition: all 0.15s ease;
  user-select: none;
  white-space: nowrap;
}

.legend-item:hover {
  background: var(--surface-hover);
}

.legend-item.hidden {
  opacity: 0.35;
  text-decoration: line-through;
}

.legend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ── Variable multi-select picker ────────────────────── */
.var-picker {
  position: relative;
}

.var-picker__trigger {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-primary);
  color: var(--text-secondary);
  font-size: 11px;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s ease;
}

.var-picker__trigger:hover {
  border-color: var(--accent-blue);
  color: var(--text-primary);
}

.var-picker__caret {
  transition: transform 0.2s ease;
}

.var-picker.open .var-picker__caret {
  transform: rotate(180deg);
}

.var-picker__dropdown {
  position: absolute;
  bottom: calc(100% + 4px);
  right: 0;
  width: 240px;
  max-height: 320px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  z-index: 200;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.var-picker__dropdown[hidden] {
  display: none;
}

.var-picker__search-row {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
}

.var-picker__search {
  width: 100%;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 11px;
  font-family: 'Inter', sans-serif;
  outline: none;
}

.var-picker__search:focus {
  border-color: var(--accent-blue);
}

.var-picker__actions {
  display: flex;
  gap: 4px;
  padding: 4px 8px;
  border-bottom: 1px solid var(--border);
}

.var-picker__action-btn {
  flex: 1;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-primary);
  color: var(--text-secondary);
  font-size: 10px;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  transition: all 0.15s ease;
}

.var-picker__action-btn:hover {
  background: var(--accent-blue);
  color: white;
  border-color: var(--accent-blue);
}

.var-picker__list {
  overflow-y: auto;
  max-height: 220px;
  padding: 4px 0;
}

.var-picker__item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 11px;
  font-family: 'Inter', sans-serif;
  color: var(--text-secondary);
  transition: background 0.1s ease;
  user-select: none;
}

.var-picker__item:hover {
  background: var(--surface-hover);
}

.var-picker__item input[type="checkbox"] {
  width: 13px;
  height: 13px;
  accent-color: var(--accent-blue);
  cursor: pointer;
  flex-shrink: 0;
}

.var-picker__item-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}

.var-picker__item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Simulation table */
.sim-table-wrap {
  width: 100%;
  height: 100%;
  overflow: auto;
}

.sim-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
}

.sim-table th {
  position: sticky;
  top: 0;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 6px 10px;
  text-align: right;
  border-bottom: 1px solid var(--border);
  z-index: 1;
  white-space: nowrap;
}

.sim-table th:first-child {
  text-align: left;
}

.sim-table td {
  padding: 3px 10px;
  text-align: right;
  color: var(--text-primary);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.sim-table td:first-child {
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
}

.sim-table tr:nth-child(even) {
  background: var(--bg-secondary);
}

.sim-table tr:hover {
  background: var(--surface);
}

/* ── Status Bar ──────────────────────────────────────────── */
.status-bar {
  grid-area: status;
}

/* ── Responsive: Tablet ──────────────────────────────────── */
@media (max-width: 1200px) {
  .app {
    grid-template-columns: var(--toolbar-width) 1fr;
    grid-template-areas:
      "header  header"
      "toolbar canvas"
      "status  status";
  }

  .inspector {
    position: fixed;
    right: 0;
    top: var(--header-height);
    bottom: var(--status-bar-height);
    width: var(--inspector-width);
    transform: translateX(100%);
    transition: transform var(--transition-slow);
    box-shadow: var(--shadow-lg);
  }

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

  /* On narrow screens, chat stays as overlay (no grid column) */
  .app.chat-open {
    grid-template-columns: var(--toolbar-width) 1fr;
    grid-template-areas:
      "header  header"
      "toolbar canvas"
      "status  status";
  }

  /* P46: Tablet + view tabs visible */
  .app:has(.view-tabs-bar:not([hidden])) {
    grid-template-rows: var(--header-height) 34px 1fr var(--status-bar-height);
    grid-template-columns: var(--toolbar-width) 1fr;
    grid-template-areas:
      "header  header"
      "toolbar tabs"
      "toolbar canvas"
      "status  status";
  }

  /* P46: Tablet + chat open + view tabs visible */
  .app.chat-open:has(.view-tabs-bar:not([hidden])) {
    grid-template-rows: var(--header-height) 34px 1fr var(--status-bar-height);
    grid-template-columns: var(--toolbar-width) 1fr;
    grid-template-areas:
      "header  header"
      "toolbar tabs"
      "toolbar canvas"
      "status  status";
  }

  .app.chat-open .chat-panel {
    position: fixed;
    grid-area: unset;
    top: var(--header-height);
    right: 0;
    bottom: var(--status-bar-height);
    z-index: 200;
  }
}

/* ── Mobile-only elements: hidden on desktop ─── */
.mobile-burger,
.mobile-chat-toggle,
.mobile-menu,
.mobile-menu-backdrop {
  display: none;
}

/* ── Responsive: Mobile ──────────────────────────────────── */
@media (max-width: 768px) {
  :root {
    --toolbar-width: 40px;
  }

  .app {
    grid-template-columns: var(--toolbar-width) 1fr;
    grid-template-rows: var(--header-height) 1fr var(--status-bar-height);
    grid-template-areas:
      "header  header"
      "toolbar canvas"
      "status  status";
  }

  /* ── Toolbar: thin strip, always visible ── */
  .toolbar {
    display: flex;
    padding: var(--space-1) 0;
    gap: 1px;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
  }

  .toolbar .icon-btn {
    width: 30px;
    height: 30px;
    min-width: 30px;
  }

  .toolbar .icon-btn svg {
    width: 14px;
    height: 14px;
  }

  .toolbar .divider {
    margin: 2px 6px;
  }

  /* Hide toolbar tooltips on mobile (use touch) */
  .toolbar [data-tooltip]::after {
    display: none;
  }

  /* ── Header: only logo + model name + burger + chat ── */
  .header {
    padding: 0 var(--space-2);
    gap: var(--space-1);
  }

  /* Hide all header groups by default */
  .header > .header__group,
  .header > .divider,
  .header > .toolbar-divider {
    display: none;
  }

  /* Show: logo, model name, spacer */
  .header > .header__logo,
  .header > .header__model-name-wrapper,
  .header > .header__spacer {
    display: flex;
  }

  .header__logo {
    margin-right: var(--space-1);
  }

  .header__logo-text {
    font-size: 11px;
  }

  .header__model-name {
    font-size: 11px;
    max-width: 120px;
  }

  /* Burger menu button (added via JS) */
  .mobile-burger {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
    -webkit-app-region: no-drag;
    flex-shrink: 0;
  }

  .mobile-burger:hover,
  .mobile-burger.active {
    background: var(--surface);
    color: var(--text-primary);
  }

  /* Chat toggle stays visible on mobile */
  .mobile-chat-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: none;
    background: var(--accent-stock);
    color: #fff;
    cursor: pointer;
    border-radius: var(--radius-sm);
    -webkit-app-region: no-drag;
    flex-shrink: 0;
  }

  /* Mobile dropdown menu */
  .mobile-menu {
    position: fixed;
    top: var(--header-height);
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 8px 32px rgba(0,0,0,0.12);
    z-index: 500;
    display: none;
    flex-wrap: wrap;
    padding: var(--space-2);
    gap: var(--space-1);
    max-height: 60vh;
    overflow-y: auto;
  }

  .mobile-menu.open {
    display: flex;
  }

  .mobile-menu .icon-btn {
    width: 36px;
    height: 36px;
  }

  .mobile-menu .divider,
  .mobile-menu .toolbar-divider {
    width: 100%;
    height: 1px;
    background: var(--border);
    margin: var(--space-1) 0;
  }

  .mobile-menu-backdrop {
    position: fixed;
    inset: 0;
    top: var(--header-height);
    background: rgba(0,0,0,0.3);
    z-index: 499;
    display: none;
  }

  .mobile-menu-backdrop.open {
    display: block;
  }

  /* ── Inspector: overlay on mobile ── */
  .inspector {
    position: fixed;
    right: 0;
    top: var(--header-height);
    bottom: var(--status-bar-height);
    width: 280px;
    max-width: 80vw;
    transform: translateX(100%);
    transition: transform var(--transition-slow);
    box-shadow: var(--shadow-lg);
    z-index: 300;
  }

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

  /* ── Chat: full-screen on mobile ── */
  .chat-panel {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100% !important;
    max-width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important;
    z-index: 1000 !important;
    border-left: none !important;
    box-shadow: none !important;
    border-radius: 0 !important;
  }

  .chat-panel .chat-resize-handle {
    display: none;
  }

  /* Chat header: add back button feel */
  .chat-panel__header {
    padding: 10px 12px;
    padding-top: max(10px, env(safe-area-inset-top));
  }

  /* Chat input area: respect safe area on phones */
  .chat-panel__input {
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  /* When chat is open on mobile, hide grid override */
  .app.chat-open {
    grid-template-columns: var(--toolbar-width) 1fr !important;
    grid-template-areas:
      "header  header"
      "toolbar canvas"
      "status  status" !important;
  }

  .app.chat-open.inspector-hidden {
    grid-template-columns: var(--toolbar-width) 1fr !important;
    grid-template-areas:
      "header  header"
      "toolbar canvas"
      "status  status" !important;
  }

  /* Minimap: smaller on mobile */
  .minimap {
    width: 100px;
    height: 66px;
    bottom: 8px;
    right: 8px;
  }

  /* Status bar: compact */
  .status-bar {
    font-size: 9px;
    padding: 0 var(--space-2);
  }
}

/* ── Minimap ──────────────────────────────────────────────── */
.minimap {
  position: absolute;
  bottom: 12px;
  right: 12px;
  width: 180px;
  height: 120px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-primary);
  box-shadow: var(--shadow-md);
  opacity: 0.85;
  pointer-events: none;
  z-index: 10;
}

/* ── Inspector Hidden ──────────────────────────────────── */
.app.inspector-hidden {
  grid-template-columns: var(--toolbar-width) 1fr;
  grid-template-areas:
    "header   header"
    "toolbar  canvas"
    "status   status";
}

.app.inspector-hidden .inspector {
  display: none !important;
  width: 0;
  min-width: 0;
  padding: 0;
  border: none;
  overflow: hidden;
}

/* ── Chat-open layout: Inspector + Chat side by side ── */
.app.chat-open {
  grid-template-columns: var(--toolbar-width) 1fr var(--inspector-width) var(--chat-width, 380px);
  grid-template-areas:
    "header   header    header      header"
    "toolbar  canvas    inspector   chat"
    "status   status    status      status";
  transition: grid-template-columns 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Chat open + inspector hidden */
.app.chat-open.inspector-hidden {
  grid-template-columns: var(--toolbar-width) 1fr var(--chat-width, 380px);
  grid-template-areas:
    "header   header    header"
    "toolbar  canvas    chat"
    "status   status    status";
}

/* P46: Chat open + view tabs visible */
.app.chat-open:has(.view-tabs-bar:not([hidden])) {
  grid-template-rows: var(--header-height) 34px 1fr var(--status-bar-height);
  grid-template-columns: var(--toolbar-width) 1fr var(--inspector-width) var(--chat-width, 380px);
  grid-template-areas:
    "header   header    header      header"
    "toolbar  tabs      inspector   chat"
    "toolbar  canvas    inspector   chat"
    "status   status    status      status";
}

/* P46: Chat open + view tabs visible + inspector hidden */
.app.chat-open.inspector-hidden:has(.view-tabs-bar:not([hidden])) {
  grid-template-rows: var(--header-height) 34px 1fr var(--status-bar-height);
  grid-template-columns: var(--toolbar-width) 1fr var(--chat-width, 380px);
  grid-template-areas:
    "header   header    header"
    "toolbar  tabs      chat"
    "toolbar  canvas    chat"
    "status   status    status";
}

/* Disable transition during resize drag */
.app.chat-resizing {
  transition: none !important;
}

/* ── AI Chat Panel ───────────────────────────────────── */
.chat-panel {
  position: fixed;
  top: var(--header-height);
  right: 0;
  bottom: var(--status-bar-height);
  width: 380px;
  max-width: 100vw;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
  border-left: 1px solid var(--border);
  box-shadow: -4px 0 24px rgba(0,0,0,0.08);
  z-index: 200;
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}

/* When chat-open is on grid, switch to grid positioning */
.app.chat-open .chat-panel {
  position: relative;
  grid-area: chat;
  transform: none;
  top: auto;
  right: auto;
  bottom: auto;
  width: 100%;            /* Fill the grid column */
  z-index: auto;
  min-height: 0;       /* Critical: allows flex children to shrink & scroll */
  overflow: hidden;    /* Contain scrollable content within grid cell */
}

.chat-panel.open {
  transform: translateX(0);
}

/* Resize handle on left edge of chat panel */
.chat-resize-handle {
  position: absolute;
  left: -3px;
  top: 0;
  bottom: 0;
  width: 6px;
  cursor: col-resize;
  z-index: 10;
  background: transparent;
  transition: background 0.15s;
}

.chat-resize-handle:hover,
.chat-resize-handle.active {
  background: var(--accent-stock);
  opacity: 0.5;
}

.chat-resize-handle.active {
  opacity: 0.8;
}

.chat-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-panel__title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

.chat-panel__badge {
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 1px 6px;
  border-radius: 9px;
  background: var(--accent-flow);
  color: #fff;
}

.chat-panel__messages {
  flex: 1;
  min-height: 0;    /* Critical: allows flex item to shrink below content size */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;  /* Smooth touch scrolling on iOS */
  overscroll-behavior: contain;       /* Don't scroll parent when hitting bounds */
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

/* Message bubbles */
.chat-msg {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  max-width: 100%;
  animation: chatFadeIn 0.2s ease-out;
}

@keyframes chatFadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-msg__avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: #fff;
}

.chat-msg--assistant .chat-msg__avatar {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
}

.chat-msg--user .chat-msg__avatar {
  background: linear-gradient(135deg, #3b82f6, #06b6d4);
}

.chat-msg__body {
  flex: 1;
  min-width: 0;
}

.chat-msg__text {
  font-size: 13px;
  line-height: 1.55;
  color: var(--text-primary);
  background: var(--bg-secondary);
  border-radius: 2px 12px 12px 12px;
  padding: 8px 12px;
  word-wrap: break-word;
  user-select: text;
  -webkit-user-select: text;
  cursor: text;
}

.chat-msg--user .chat-msg__text {
  background: var(--accent-stock);
  color: #fff;
  border-radius: 12px 2px 12px 12px;
}

.chat-msg--user .chat-msg__text::selection {
  background: rgba(255, 255, 255, 0.35);
  color: #fff;
}

.chat-msg--user .chat-msg__text *::selection {
  background: rgba(255, 255, 255, 0.35);
  color: #fff;
}

.chat-msg--user {
  flex-direction: row-reverse;
}

.chat-msg__text code {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 12px;
  background: rgba(0,0,0,0.1);
  padding: 1px 4px;
  border-radius: 3px;
}

.chat-msg--user .chat-msg__text code {
  background: rgba(255,255,255,0.2);
}

.chat-msg__text strong {
  font-weight: 600;
}

.chat-msg__result {
  margin-top: 6px;
  background: var(--bg-canvas);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  overflow-x: auto;
}

.chat-msg__result pre {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 11px;
  line-height: 1.4;
  color: var(--text-secondary);
  margin: 0;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 200px;
  overflow-y: auto;
}

.chat-msg__time {
  font-size: 10px;
  color: var(--text-tertiary);
  margin-top: 4px;
  padding-left: 12px;
}

.chat-msg--user .chat-msg__time {
  text-align: right;
  padding-left: 0;
  padding-right: 12px;
}

/* Chat message context menu */
.chat-ctx-menu {
  position: fixed;
  z-index: 10000;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
  padding: 4px;
  min-width: 190px;
  animation: ctxMenuIn 0.12s ease-out;
}

@keyframes ctxMenuIn {
  from { opacity: 0; transform: scale(0.95); }
  to   { opacity: 1; transform: scale(1); }
}

.chat-ctx-menu__item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 12px;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 500;
  border-radius: 7px;
  cursor: pointer;
  text-align: left;
  transition: background 0.1s;
}

.chat-ctx-menu__item:hover {
  background: var(--bg-secondary);
}

.chat-ctx-menu__item span {
  font-size: 14px;
  width: 20px;
  text-align: center;
}

/* Edit & Resend textarea */
.chat-edit-textarea {
  width: 100%;
  min-height: 80px;
  max-height: 200px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 13px;
  line-height: 1.5;
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
}

.chat-edit-textarea:focus {
  border-color: var(--accent-stock);
}

/* Image preview (above input before sending) */
.chat-image-preview {
  position: relative;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
}

.chat-image-preview img {
  max-width: 200px;
  max-height: 120px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.chat-image-preview__remove {
  position: absolute;
  top: 4px;
  right: 8px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  background: rgba(0,0,0,0.5);
  color: white;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.chat-image-preview__remove:hover {
  background: #ef4444;
}

/* Inline image in chat messages */
.chat-msg__image {
  margin-bottom: 6px;
}

.chat-msg__image img {
  max-width: 100%;
  max-height: 300px;
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
}

.chat-msg__image img:hover {
  opacity: 0.9;
}

/* Typing indicator */
.chat-typing__dots {
  display: flex;
  gap: 4px;
  padding: 8px 12px;
  background: var(--bg-secondary);
  border-radius: 2px 12px 12px 12px;
  width: fit-content;
}

.chat-typing__dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-tertiary);
  animation: typingBounce 1.2s infinite;
}

.chat-typing__dots span:nth-child(2) { animation-delay: 0.2s; }
.chat-typing__dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* Agentic loop progress ring */
.chat-progress-ring {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--bg-secondary);
  border-radius: 2px 12px 12px 12px;
  width: fit-content;
  font-size: 12px;
  color: var(--text-secondary);
  animation: chatFadeIn 0.2s ease-out;
}

.chat-progress-ring svg {
  flex-shrink: 0;
  animation: progressSpin 2s linear infinite;
}

@keyframes progressSpin {
  to { transform: rotate(360deg); }
}

/* Quick action chips */
.chat-panel__chips {
  display: flex;
  gap: 6px;
  padding: 8px 14px;
  flex-wrap: wrap;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-chip {
  font-size: 11px;
  font-weight: 500;
  padding: 4px 10px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.chat-chip:hover {
  background: var(--accent-stock);
  color: #fff;
  border-color: var(--accent-stock);
  transform: translateY(-1px);
}

/* Input area */
.chat-panel__input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.4;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg-primary);
  color: var(--text-primary);
  resize: none;
  min-height: 36px;
  max-height: calc(1.4em * 4 + 16px); /* 4 lines + padding */
  overflow-y: hidden;
  transition: border-color 0.15s;
}

.chat-input::placeholder {
  color: var(--text-tertiary);
  font-size: 12px;
}

.chat-input:focus {
  outline: none;
  border-color: var(--accent-stock);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.15);
}

.chat-send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--accent-stock);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.15s ease;
}

.chat-send-btn:hover {
  background: #2563eb;
  transform: scale(1.05);
}

.chat-send-btn:active {
  transform: scale(0.95);
}

/* Chat toggle button accent */
.icon-btn--accent {
  color: var(--accent-stock);
}

.icon-btn--accent:hover {
  background: var(--accent-stock);
  color: #fff;
}

/* ── Onboarding Tour ─────────────────────────────────── */
.tour-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 9990;
  cursor: pointer;
}

.tour-spotlight {
  position: fixed;
  border-radius: 8px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.55);
  z-index: 9991;
  pointer-events: none;
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

.tour-tooltip {
  position: fixed;
  width: 320px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.2);
  z-index: 9992;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.25s, transform 0.25s;
}

.tour-tooltip.visible {
  opacity: 1;
  transform: translateY(0);
}

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

.tour-tooltip__step {
  font-size: 10px;
  font-weight: 700;
  color: var(--accent-stock);
  background: var(--accent-stock-bg);
  padding: 2px 6px;
  border-radius: 8px;
}

.tour-tooltip__title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
}

.tour-tooltip__content {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-secondary);
  margin-bottom: 14px;
}

.tour-tooltip__content kbd {
  display: inline-flex;
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-secondary);
  font-size: 11px;
  font-weight: 600;
}

.tour-tooltip__content code {
  font-family: 'SF Mono', monospace;
  font-size: 12px;
  background: var(--bg-secondary);
  padding: 1px 5px;
  border-radius: 3px;
}

.tour-tooltip__actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.tour-tooltip__nav {
  display: flex;
  gap: 6px;
}

.tour-tooltip__btn {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-family: 'Inter', sans-serif;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.12s;
}

.tour-tooltip__btn:hover {
  background: var(--surface-hover);
}

.tour-tooltip__btn--primary {
  background: var(--accent-stock);
  color: #fff;
  border-color: var(--accent-stock);
}

.tour-tooltip__btn--primary:hover {
  filter: brightness(1.1);
}

.tour-tooltip__btn--skip {
  border: none;
  background: none;
  color: var(--text-tertiary);
  font-size: 11px;
  padding: 4px 8px;
}

.tour-tooltip__btn--skip:hover {
  color: var(--text-primary);
}

.tour-tooltip__progress {
  display: flex;
  justify-content: center;
  gap: 5px;
}

.tour-tooltip__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--border);
  transition: all 0.2s;
}

.tour-tooltip__dot.active {
  background: var(--accent-stock);
  transform: scale(1.3);
}

.tour-tooltip__dot.done {
  background: var(--accent-flow);
}

/* ── Chat Provider Selector ────────────────────────────── */

.chat-provider-select {
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  max-width: 140px;
}

.chat-provider-select:focus {
  outline: 1px solid var(--accent-stock);
}

.chat-msg__provider {
  display: inline-block;
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 3px;
  background: var(--accent-stock);
  color: white;
  opacity: 0.7;
  margin-top: 4px;
}

/* ── Cloud Model Dialog ──────────────────────────────── */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.2s ease;
}

.modal-overlay[hidden] {
  display: none !important;
}

.cloud-modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 500px;
  max-width: 90vw;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal__header h2 {
  margin: 0;
  font-size: 16px;
  color: var(--text);
}

.btn--icon {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--text-muted);
  border-radius: 6px;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.15s;
  flex-shrink: 0;
}

.btn--icon:hover {
  background: var(--hover);
  color: var(--text);
}

.modal__body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 20px;
  min-height: 200px;
}

.modal__footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

.cloud-models-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.cloud-models-loading,
.cloud-models-empty {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
  font-size: 14px;
}

.cloud-model-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  transition: background 0.15s;
}

.cloud-model-item:hover {
  background: var(--hover);
}

.cloud-model-item__info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.cloud-model-item__name {
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
}

.cloud-model-item__meta {
  font-size: 11px;
  color: var(--text-muted);
}

.cloud-model-item__actions {
  display: flex;
  gap: 6px;
}

.btn--sm {
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 4px;
}

.btn--danger {
  background: #ef4444;
  color: white;
  border: none;
  cursor: pointer;
}

.btn--danger:hover {
  background: #dc2626;
}

.btn--primary {
  background: var(--accent-stock);
  color: white;
  border: none;
  cursor: pointer;
}

.btn--primary:hover {
  opacity: 0.9;
}

.btn--secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  cursor: pointer;
}

.btn--secondary:hover {
  background: var(--hover);
}

/* ── Toolbar Divider ─────────────────────────────────── */

.toolbar-divider {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 4px;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideIn {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* ── Settings Modal ──────────────────────────────────── */

.settings-modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 560px;
  max-width: 90vw;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.settings-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
}

.settings-tab {
  padding: 10px 16px;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  border-bottom: 2px solid transparent;
  transition: all 0.2s;
}

.settings-tab:hover {
  color: var(--text);
}

.settings-tab.active {
  color: var(--accent-stock);
  border-bottom-color: var(--accent-stock);
}

.settings-body {
  overflow-y: auto;
  padding: 16px 20px;
}

.settings-content {
  display: none;
}

.settings-content.active {
  display: block;
}

.settings-group {
  margin-bottom: 20px;
}

.settings-group h3 {
  font-size: 14px;
  margin: 0 0 12px;
  color: var(--text);
}

.settings-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0 0 12px;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  font-size: 13px;
  color: var(--text);
}

.settings-row input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent-stock);
}

.settings-select,
.settings-input {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  min-width: 140px;
}

.settings-select:focus,
.settings-input:focus {
  outline: 1px solid var(--accent-stock);
  border-color: var(--accent-stock);
}

.settings-save {
  margin-top: 12px;
  padding: 8px 20px;
  font-size: 13px;
  border-radius: 6px;
}

/* Billing */

.billing-status {
  padding: 12px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 13px;
}

.billing-status--active {
  border-color: #10b981;
  background: rgba(16, 185, 129, 0.1);
}

.billing-status p {
  margin: 4px 0;
}

.billing-plan {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  text-align: center;
}

.billing-plan__price {
  margin-bottom: 16px;
}

.billing-plan__amount {
  font-size: 28px;
  font-weight: 700;
  color: var(--accent-stock);
}

.billing-plan__interval {
  font-size: 14px;
  color: var(--text-muted);
}

.billing-plan__features {
  list-style: none;
  padding: 0;
  margin: 12px 0 20px;
  text-align: left;
  font-size: 13px;
}

.billing-plan__features li {
  padding: 4px 0;
}

.btn--lg {
  padding: 12px 28px;
  font-size: 15px;
}

/* ── Document Intelligence ───────────────────────────── */

.doc-upload-zone {
  border: 2px dashed var(--border);
  border-radius: 12px;
  padding: 32px 20px;
  text-align: center;
  transition: all 0.2s;
  cursor: pointer;
}

.doc-upload-zone:hover,
.doc-upload-zone.dragover {
  border-color: var(--accent-stock);
  background: rgba(var(--accent-stock-rgb, 59, 130, 246), 0.05);
}

.doc-entity-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 8px 0;
}

.doc-entity {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}

.doc-entity:hover {
  background: var(--hover);
}

.doc-entity input[type="checkbox"] {
  accent-color: var(--accent-stock);
}

/* ── BYOM Presets ────────────────────────────────────── */

.byom-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

/* ── Data Panel ──────────────────────────────────────── */

.data-panel {
  position: absolute;
  right: 360px;
  top: 0;
  bottom: 0;
  width: 320px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 50;
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.1);
  animation: slideInRight 0.2s ease;
}

@keyframes slideInRight {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.data-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}

.data-panel__header h3 {
  margin: 0;
  font-size: 14px;
}

.data-panel__search {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}

.data-panel__body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.data-section {
  padding: 8px 12px;
}

.data-section h4 {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.data-series-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.data-series-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: none;
  background: none;
  border-radius: 6px;
  cursor: pointer;
  width: 100%;
  text-align: left;
  transition: background 0.15s;
}

.data-series-item:hover {
  background: var(--hover);
}

.data-series-item--nested {
  padding-left: 18px;
}

.data-series-icon {
  font-size: 16px;
  width: 24px;
  text-align: center;
  flex-shrink: 0;
}

.data-series-info {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.data-series-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.data-series-meta {
  font-size: 10px;
  color: var(--text-muted);
}

.data-source-group {
  margin-bottom: 4px;
}

.data-source-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  width: 100%;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  border-radius: 6px;
  transition: background 0.15s;
}

.data-source-header:hover {
  background: var(--hover);
}

.data-source-count {
  font-size: 11px;
  color: var(--text-muted);
}

.data-api-key-row {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  align-items: center;
}

/* Preview */

.data-panel__preview {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
}

.data-panel__preview canvas {
  width: 100%;
  height: 100px;
  border-radius: 6px;
  background: var(--bg);
}

#data-preview-info {
  margin-top: 6px;
}

#data-preview-info p {
  margin: 2px 0;
}

.data-panel__actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  align-items: center;
}

/* ── Custom Confirm / Alert Dialog ─────────────────────── */
.confirm-dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.confirm-dialog-backdrop.open {
  opacity: 1;
  visibility: visible;
}

.confirm-dialog {
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(255,255,255,0.05);
  padding: 28px 32px 24px;
  max-width: 420px;
  width: 90vw;
  text-align: center;
  transform: scale(0.92) translateY(10px);
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.confirm-dialog-backdrop.open .confirm-dialog {
  transform: scale(1) translateY(0);
}

.confirm-dialog__icon {
  font-size: 36px;
  margin-bottom: 12px;
  line-height: 1;
}

.confirm-dialog__title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0 0 8px;
}

.confirm-dialog__message {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 0 0 24px;
  line-height: 1.5;
}

.confirm-dialog__actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.confirm-dialog__btn {
  padding: 9px 24px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--border);
  transition: all 0.15s ease;
  min-width: 100px;
}

.confirm-dialog__btn:focus-visible {
  outline: 2px solid var(--accent-stock);
  outline-offset: 2px;
}

.confirm-dialog__btn--cancel {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.confirm-dialog__btn--cancel:hover {
  background: var(--bg-tertiary, var(--border));
}

.confirm-dialog__btn--confirm {
  background: var(--accent-stock);
  color: #fff;
  border-color: var(--accent-stock);
}

.confirm-dialog__btn--confirm:hover {
  filter: brightness(1.1);
}

.confirm-dialog__btn--danger {
  background: var(--accent-converter, #e74c3c);
  border-color: var(--accent-converter, #e74c3c);
}

.confirm-dialog__btn--danger:hover {
  filter: brightness(1.1);
}

/* ═══════════════════════════════════════════════════════════
   P46 — Views & Stories
   Tab bar, view content, diagram rendering, context menu
   ═══════════════════════════════════════════════════════════ */

/* ── View Tabs Bar ──────────────────────────────────────── */
.view-tabs-bar {
  display: flex;
  align-items: center;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 0 8px;
  height: 34px;
  gap: 2px;
  z-index: 12;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.view-tabs-bar::-webkit-scrollbar {
  display: none;
}

/* When tabs bar is visible, insert a new row for tabs */
.app:has(.view-tabs-bar:not([hidden])) {
  grid-template-rows: var(--header-height) 34px 1fr var(--status-bar-height);
  grid-template-columns: var(--toolbar-width) 1fr var(--inspector-width);
  grid-template-areas:
    "header   header    header"
    "toolbar  tabs      inspector"
    "toolbar  canvas    inspector"
    "status   status    status";
}

.app:has(.view-tabs-bar:not([hidden])) .view-tabs-bar {
  grid-area: tabs;
}

/* Inspector-hidden variant */
.app.inspector-hidden:has(.view-tabs-bar:not([hidden])) {
  grid-template-columns: var(--toolbar-width) 1fr;
  grid-template-areas:
    "header   header"
    "toolbar  tabs"
    "toolbar  canvas"
    "status   status";
}

.view-tabs-bar__tabs {
  display: flex;
  align-items: center;
  gap: 1px;
  flex: 1;
  min-width: 0;
}

/* ── Individual Tab ──────────────────────────────────────── */
.view-tab {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  font-size: 12px;
  font-weight: 500;
  font-family: 'Inter', -apple-system, sans-serif;
  color: var(--text-muted);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s ease;
  position: relative;
  border-radius: 4px 4px 0 0;
  flex-shrink: 0;
}

.view-tab svg {
  flex-shrink: 0;
  opacity: 0.6;
}

.view-tab:hover {
  color: var(--text-primary);
  background: var(--bg-surface, rgba(128, 128, 128, 0.08));
}

.view-tab:hover svg {
  opacity: 1;
}

.view-tab.active {
  color: var(--accent, var(--accent-stock));
  border-bottom-color: var(--accent, var(--accent-stock));
  background: var(--bg-primary);
}

.view-tab.active svg {
  opacity: 1;
  color: var(--accent, var(--accent-stock));
}

.view-tab--model {
  font-weight: 600;
}

.view-tab--add {
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 4px;
  border-bottom: none;
  opacity: 0.6;
  transition: all 0.15s ease;
}

.view-tab--add:hover {
  opacity: 1;
  background: var(--bg-surface, rgba(128, 128, 128, 0.08));
  color: var(--accent, var(--accent-stock));
}

.view-tab__label {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.view-tab__dropdown {
  display: none;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 3px;
  padding: 0;
  margin-left: 2px;
  transition: all 0.1s ease;
}

.view-tab:hover .view-tab__dropdown,
.view-tab.active .view-tab__dropdown {
  display: flex;
}

.view-tab__dropdown:hover {
  background: rgba(128, 128, 128, 0.15);
  color: var(--text-primary);
}

/* ── View Content Container ─────────────────────────────── */
.view-content {
  position: absolute;
  inset: 0;
  background: var(--bg-primary);
  z-index: 10;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: viewFadeIn 0.2s ease;
}

/* When a view tab is active, hide canvas children */
.canvas-area.view-active .canvas-container,
.canvas-area.view-active .canvas-empty-overlay,
.canvas-area.view-active .minimap,
.canvas-area.view-active .chart-panel {
  display: none !important;
}

/* Hide the grid dots behind the view */
.canvas-area.view-active::before {
  display: none;
}

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

.view-content__header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  flex-shrink: 0;
  min-height: 38px;
}

.view-content__title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.view-content__desc {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.view-content__spacer {
  flex: 1;
}

.view-action-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-primary);
  color: var(--text-secondary);
  font-size: 11px;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
  flex-shrink: 0;
}

.view-action-btn:hover {
  border-color: var(--accent, var(--accent-stock));
  color: var(--accent, var(--accent-stock));
  background: rgba(59, 130, 246, 0.06);
}

.view-content__body {
  flex: 1;
  overflow: auto;
  padding: 24px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

/* ── Type Badge ─────────────────────────────────────────── */
.view-type-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  flex-shrink: 0;
}

.view-type-badge--flow     { background: rgba(59, 130, 246, 0.12); color: #3b82f6; }
.view-type-badge--sankey   { background: rgba(139, 92, 246, 0.12); color: #8b5cf6; }
.view-type-badge--mindmap  { background: rgba(16, 185, 129, 0.12); color: #10b981; }
.view-type-badge--sequence { background: rgba(245, 158, 11, 0.12); color: #f59e0b; }
.view-type-badge--table    { background: rgba(99, 102, 241, 0.12); color: #6366f1; }
.view-type-badge--text     { background: rgba(156, 163, 175, 0.12); color: #9ca3af; }
.view-type-badge--story    { background: rgba(236, 72, 153, 0.12); color: #ec4899; }

/* ── Mermaid Diagram Wrapper ────────────────────────────── */
.view-mermaid-wrapper {
  max-width: 100%;
  overflow: auto;
}

.view-mermaid-wrapper svg {
  max-width: 100%;
  height: auto;
}

/* ── Table Wrapper (inside view) ────────────────────────── */
.view-table-wrapper {
  width: 100%;
  overflow: auto;
}

.view-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  font-family: 'Inter', -apple-system, sans-serif;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.view-table th {
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 10px 14px;
  text-align: left;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}

.view-table td {
  padding: 8px 14px;
  color: var(--text-primary);
  border-bottom: 1px solid var(--border);
}

.view-table tr:nth-child(even) {
  background: var(--bg-secondary);
}

.view-table tr:hover {
  background: rgba(59, 130, 246, 0.04);
}

.view-table tr:last-child td {
  border-bottom: none;
}

/* ── Markdown Wrapper ─────────────────────────────────── */
.view-markdown-wrapper {
  max-width: 720px;
  width: 100%;
  line-height: 1.7;
  color: var(--text-primary);
  font-size: 14px;
}

.view-markdown-wrapper h1 { font-size: 24px; margin: 0 0 12px; font-weight: 700; }
.view-markdown-wrapper h2 { font-size: 20px; margin: 16px 0 10px; font-weight: 600; }
.view-markdown-wrapper h3 { font-size: 16px; margin: 14px 0 8px; font-weight: 600; }
.view-markdown-wrapper h4 { font-size: 14px; margin: 12px 0 6px; font-weight: 600; }

.view-markdown-wrapper code {
  background: var(--bg-secondary);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;
  font-family: 'JetBrains Mono', monospace;
}

.view-markdown-wrapper ul,
.view-markdown-wrapper ol {
  padding-left: 20px;
  margin: 8px 0;
}

.view-markdown-wrapper li {
  margin: 4px 0;
}

.view-markdown-wrapper strong {
  font-weight: 600;
  color: var(--text-primary);
}

/* ── SVG Wrapper ──────────────────────────────────────── */
.view-svg-wrapper {
  max-width: 100%;
  overflow: auto;
}

.view-svg-wrapper svg {
  max-width: 100%;
  height: auto;
}

/* ── Story Canvas ─────────────────────────────────────── */
.story-canvas {
  width: 100%;
  height: 100%;
  min-height: 400px;
  overflow: hidden;
  cursor: grab;
  background: var(--bg-canvas, var(--bg-primary));
  border-radius: 8px;
  border: 1px solid var(--border);
}

.story-canvas:active {
  cursor: grabbing;
}

.story-canvas svg {
  display: block;
  transition: transform 0.05s ease;
}

/* ── Error Display ────────────────────────────────────── */
.view-error-wrapper {
  width: 100%;
  max-width: 560px;
}

.view-error {
  padding: 24px;
  background: rgba(239, 68, 68, 0.06);
  border: 1px solid rgba(239, 68, 68, 0.2);
  border-radius: 12px;
  text-align: center;
}

.view-error__icon {
  color: #ef4444;
  margin-bottom: 12px;
}

.view-error__title {
  font-size: 15px;
  font-weight: 600;
  color: #ef4444;
  margin-bottom: 6px;
}

.view-error__message {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.view-error__details {
  text-align: left;
}

.view-error__details summary {
  font-size: 11px;
  color: var(--text-muted);
  cursor: pointer;
  margin-bottom: 6px;
}

.view-error__details pre {
  font-size: 11px;
  font-family: 'JetBrains Mono', monospace;
  background: var(--bg-secondary);
  padding: 10px;
  border-radius: 6px;
  overflow-x: auto;
  white-space: pre-wrap;
  max-height: 200px;
  overflow-y: auto;
  color: var(--text-secondary);
}

/* ── Context Menu ─────────────────────────────────────── */
.view-context-menu {
  position: fixed;
  min-width: 160px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
  z-index: 1000;
  padding: 4px;
  animation: contextMenuIn 0.12s ease;
}

@keyframes contextMenuIn {
  from { opacity: 0; transform: translateY(-4px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.view-context-menu[hidden] {
  display: none;
}

.view-context-menu__item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 12px;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 12px;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  border-radius: 5px;
  transition: background 0.1s ease;
}

.view-context-menu__item:hover {
  background: var(--accent, var(--accent-stock));
  color: #fff;
}

.view-context-menu__item:hover svg {
  color: #fff;
}

.view-context-menu__item--danger {
  color: #ef4444;
}

.view-context-menu__item--danger:hover {
  background: #ef4444;
  color: #fff;
}

.view-context-menu__divider {
  height: 1px;
  background: var(--border);
  margin: 3px 8px;
}

/* ── Loading Indicator ───────────────────────────────── */
.view-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 48px 24px;
  color: var(--text-muted);
  font-size: 13px;
  width: 100%;
}

.view-loading__spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent, var(--accent-stock));
  border-radius: 50%;
  animation: viewSpin 0.8s linear infinite;
}

@keyframes viewSpin {
  to { transform: rotate(360deg); }
}

.view-loading__text {
  font-family: 'Inter', sans-serif;
}

