:root {
  --primary: #2b6cb0;
  --primary-light: #4a9eff;
  --bg: #ffffff;
  --bg-muted: #f4f6f9;
  --text: #1a202c;
  --text-muted: #5a6472;
  --border: #d9e0e8;
  --danger: #c0392b;
  --danger-bg: #fdecea;
  --success: #1f8a4c;
  --success-bg: #e8f7ee;
}

@media (prefers-color-scheme: dark) {
  :root {
    --primary: #4a9eff;
    --primary-light: #6bb6ff;
    --bg: #0d1117;
    --bg-muted: #1a2332;
    --text: #e0e6ff;
    --text-muted: #8a94ab;
    --border: #2d3748;
    --danger: #ff6b6b;
    --danger-bg: #3a1f1f;
    --success: #3ecf7e;
    --success-bg: #1a3326;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  font-style: italic;
  background: var(--bg);
  color: var(--text);
}

.app-shell {
  display: flex;
  align-items: stretch;
  min-height: 100vh;
}

.sidebar {
  flex: 0 0 auto;
  width: 280px;
  min-width: 200px;
  max-width: 480px;
  overflow: auto;
  resize: horizontal;
  border-right: 1px solid var(--border);
  background: var(--bg-muted);
  padding: 1.25rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.app-shell.sidebar-collapsed .sidebar {
  width: 56px;
  min-width: 56px;
  resize: none;
  padding: 1.25rem 0.6rem;
  overflow: hidden;
}

.app-shell.sidebar-collapsed .sidebar-section,
.app-shell.sidebar-collapsed .sidebar-footnote,
.app-shell.sidebar-collapsed .sidebar-header h1 {
  display: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.sidebar-header h1 {
  margin: 0;
  line-height: 1.3;
  color: var(--primary-light);
  min-width: 0;
}

.sidebar-logo {
  display: block;
  height: 36px;
  width: auto;
  max-width: 100%;
  object-fit: contain;
}

.icon-btn {
  flex: 0 0 auto;
  width: 2rem;
  height: 2rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
}

.icon-btn:hover {
  border-color: var(--primary-light);
}

.app-shell.sidebar-collapsed .icon-btn {
  transform: scaleX(-1);
}

.sidebar-section h2 {
  margin: 0 0 0.35rem;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.sidebar-hint {
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1.4;
  margin: 0 0 0.6rem;
}

.settings-section summary {
  cursor: pointer;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  list-style: none;
}

.settings-section summary::-webkit-details-marker {
  display: none;
}

.settings-section summary::after {
  content: "";
  flex: 0 0 auto;
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
  transform: rotate(45deg);
  transition: transform 0.15s ease;
}

.settings-section[open] summary::after {
  transform: rotate(-135deg);
}

.settings-section .sidebar-hint {
  margin-top: 0.5rem;
}

.sidebar-footnote {
  margin-top: auto;
  color: var(--text-muted);
  font-size: 0.75rem;
  line-height: 1.4;
}

.app-shell:has(.content.has-results) .sidebar-footnote {
  display: none;
}

.pill-list {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.pill {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.5rem 1rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.pill:hover {
  border-color: var(--primary-light);
}

.pill.active {
  background: var(--primary-light);
  border-color: var(--primary-light);
  color: #0d1117;
  font-weight: 600;
}

.content {
  flex: 1 1 auto;
  min-width: 0;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.results {
  order: 1;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 1.75rem 1.5rem 1rem;
  position: relative;
}

.content.has-results .results {
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0;
}

.content-logo-bar {
  order: 1;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  min-height: 0;
}

.content.has-results .content-logo-bar {
  display: none;
}

.content-logo {
  width: 220px;
  height: auto;
  max-width: 60%;
}

.composer-wrap {
  order: 2;
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  padding: 1.5rem 1.5rem 3rem;
}

.content.has-results .composer-wrap {
  display: none;
}

.content.has-results .composer-wrap.force-visible {
  display: flex;
  position: fixed;
  left: 280px;
  right: 0;
  bottom: 0;
  padding: 1rem 1.5rem 1.5rem;
  background: linear-gradient(to top, var(--bg) 60%, transparent);
  z-index: 500;
}

.app-shell.sidebar-collapsed .content.has-results .composer-wrap.force-visible {
  left: 56px;
}

.fab {
  position: fixed;
  right: 1.5rem;
  bottom: 1.5rem;
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--primary-light);
  color: #0d1117;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  z-index: 400;
}

.fab:hover {
  filter: brightness(1.08);
}

.settings-row {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin: 0.75rem 0;
  font-size: 0.9rem;
  max-width: 360px;
}

.settings-row input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  display: inline;
  margin-right: 0.4rem;
}

.settings-row input[type="password"],
.settings-row select {
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}

.drop-bar {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  max-width: 640px;
  margin: 0 auto;
  padding: 0.9rem 1.25rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-muted);
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.drop-bar:hover,
.drop-bar:focus-visible {
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(74, 158, 255, 0.15);
  outline: none;
}

.drop-bar.drag-over {
  border-color: var(--primary-light);
  background: var(--bg);
  box-shadow: 0 0 0 3px rgba(74, 158, 255, 0.25);
}

.drop-bar-icon {
  flex: 0 0 auto;
  color: var(--text-muted);
}

.drop-bar-text {
  color: var(--text-muted);
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status {
  min-height: 1.4rem;
  margin: 0.75rem 0 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.status.error {
  color: var(--danger);
}

.doc-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.85rem;
}

.doc-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-muted);
  color: var(--text-muted);
}

.doc-chip button {
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1;
  padding: 0;
}

.doc-chip button:hover {
  color: var(--danger);
}

.dashboard {
  position: relative;
  width: 100%;
  height: 100%;
}

.dashboard-toolbar {
  position: fixed;
  top: 0.85rem;
  right: 1.1rem;
  z-index: 500;
}

.chart-card {
  padding: 1rem 1.1rem;
  min-width: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: auto;
}

.widget .chart-card-header {
  display: none;
}

/* ---- Free-form glass widgets ---- */

.widget {
  position: absolute;
  border-radius: 0;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: color-mix(in srgb, var(--bg-muted) 55%, transparent);
  backdrop-filter: blur(18px) saturate(160%);
  -webkit-backdrop-filter: blur(18px) saturate(160%);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  touch-action: none;
  transition: opacity 0.15s ease, box-shadow 0.15s ease;
  min-width: 220px;
  min-height: 150px;
}

@media (prefers-color-scheme: light) {
  .widget {
    border-color: rgba(20, 30, 45, 0.1);
    box-shadow: 0 8px 32px rgba(30, 40, 60, 0.12), inset 0 1px 0 rgba(255, 255, 255, 0.6);
  }
}

.widget--dragging,
.widget--resizing {
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
}

.widget--ghost {
  opacity: 0.5;
  box-shadow: 0 0 0 2px var(--primary-light) inset, 0 16px 48px rgba(0, 0, 0, 0.4);
}

.widget--selected {
  border-color: var(--primary-light);
  box-shadow: 0 0 0 2px var(--primary-light), 0 16px 48px rgba(0, 0, 0, 0.4);
  z-index: 300;
}

.widget-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.6rem 0.7rem 0.5rem 0.9rem;
  cursor: move;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  user-select: none;
}

.widget-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: font-size 0.15s ease;
}

.widget--selected .widget-title {
  font-size: 1.05rem;
}

.widget-toggle {
  flex: 0 0 auto;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.8rem;
  line-height: 1;
}

.widget-toggle:hover {
  border-color: var(--primary-light);
  color: var(--text);
}

.widget-face {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.5rem 1.1rem 1.1rem;
  cursor: pointer;
  user-select: none;
}

.widget-kpi-value {
  font-size: 1.9rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.1;
  transition: font-size 0.15s ease;
}

.widget--selected .widget-kpi-value {
  font-size: 2.5rem;
}

.widget-kpi-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  transition: font-size 0.15s ease;
}

.widget--selected .widget-kpi-label {
  font-size: 0.95rem;
}

.widget-kpi-hint {
  margin-top: 0.4rem;
  font-size: 0.7rem;
  color: var(--text-muted);
  opacity: 0.7;
}

.widget-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

.widget-face[hidden],
.widget-body[hidden] {
  display: none;
}

.widget-body .chart-card {
  flex: 1 1 auto;
}

/* ---- Swipeable widget stack (e.g. category overview + its trend) ---- */

.widget-body--stack {
  flex-direction: column;
  gap: 0.5rem;
}

.widget-stack-nav {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.widget-stack-label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  text-align: center;
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.widget-stack-arrow {
  flex: 0 0 auto;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1;
}

.widget-stack-arrow:hover:not(:disabled) {
  border-color: var(--primary-light);
  color: var(--text);
}

.widget-stack-arrow:disabled {
  opacity: 0.35;
  cursor: default;
}

.widget-stack-viewport {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

.widget-stack-track {
  display: flex;
  width: 100%;
  height: 100%;
  transition: transform 0.25s ease;
}

.widget-stack-slide {
  flex: 0 0 100%;
  min-width: 0;
  height: 100%;
  overflow: auto;
  display: flex;
}

.widget-stack-dots {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
}

.widget-stack-dot {
  width: 0.4rem;
  height: 0.4rem;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--text-muted);
  opacity: 0.4;
  cursor: pointer;
}

.widget-stack-dot--active {
  opacity: 1;
  background: var(--primary-light);
}

.chart-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.chart-card-header h2 {
  margin: 0;
  font-size: 1.05rem;
}

.chart-card-body {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  gap: 1rem;
  flex: 1 1 auto;
  min-height: 120px;
}

.chart-card-canvas-wrap {
  position: relative;
  flex: 3 1 220px;
  min-width: 0;
  min-height: 120px;
}

.chart-composition-wrap {
  flex: 2 1 160px;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.chart-composition-canvas-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 100px;
}

.waterfall-canvas-wrap {
  position: relative;
  width: 100%;
  flex: 1 1 auto;
  min-height: 160px;
}

.waterfall-canvas-wrap svg {
  display: block;
}

.waterfall-tooltip {
  z-index: 1;
}

.chart-card .outlier-summary {
  margin-top: 0.85rem;
}

/* ---- Executive KPI grid ---- */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.85rem;
  flex: 1 1 auto;
  align-content: start;
  overflow: auto;
}

.kpi-tile {
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  min-width: 0;
}

.kpi-tile-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.kpi-tile-value {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.15;
}

.kpi-tile-delta {
  font-size: 0.75rem;
  font-weight: 600;
  width: fit-content;
  padding: 0.1rem 0.4rem;
  border-radius: 6px;
  color: var(--text-muted);
}

.kpi-tile-delta.up {
  color: var(--success);
  background: var(--success-bg);
}

.kpi-tile-delta.down {
  color: var(--danger);
  background: var(--danger-bg);
}

.kpi-tile-sparkline-wrap {
  position: relative;
  height: 32px;
  margin-top: 0.3rem;
}

/* ---- Balance sheet ratios ---- */

.ratio-tile-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin: 1rem 0;
}

.ratio-tile {
  padding: 0.5rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  min-width: 130px;
}

.ratio-tile-label {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.ratio-tile-value {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
}

.balance-stack-wrap {
  position: relative;
  width: 100%;
  flex: 1 1 auto;
  min-height: 140px;
}

.chart-trend-wrap {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.chart-trend-title {
  margin: 0 0 0.6rem;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.chart-trend-canvas-wrap {
  position: relative;
  height: 160px;
}

.chart-trend-card {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

.chart-trend-card .chart-trend-canvas-wrap {
  flex: 1 1 auto;
  height: auto;
  min-height: 160px;
}

.chart-trend-details {
  margin-top: 0.85rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  max-height: 140px;
  overflow-y: auto;
}

.chart-trend-details h4 {
  margin: 0 0 0.4rem;
  font-size: 0.8rem;
  color: var(--text);
}

.chart-trend-details-grid {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.chart-trend-details-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-muted);
  padding: 0.15rem 0;
}

.chart-trend-details-row span:last-child {
  color: var(--text);
  font-weight: 600;
}

.btn-secondary {
  padding: 0.45rem 0.9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
}

.btn-secondary:hover {
  border-color: var(--primary-light);
}

.note-editor {
  margin-top: 1rem;
  border: 1px solid var(--primary-light);
  border-radius: 10px;
  padding: 0.85rem 1rem;
  background: var(--bg-muted);
}

.note-editor label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.4rem;
}

.note-editor textarea {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  resize: vertical;
}

.note-editor-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.6rem;
}

.outlier-summary {
  margin-top: 1rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.outlier-summary .outlier-item {
  color: var(--danger);
  background: var(--danger-bg);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.4rem;
}

