:root {
  --color-primary: #DA0070;
  --color-plum: #551C3E;
  --color-orange: #FFA633;
  --color-sky: #2ECAF0;
  --color-lemon: #FFF347;
  --color-sand: #DCD2C8;
  --color-powder: #F6E4E4;
  --surface: rgba(255, 255, 255, 0.96);
  --text-primary: #1a0a12;
  --text-secondary: #551C3E;
  --border: rgba(85, 28, 62, 0.14);
  --radius: 12px;
  --shadow: 0 2px 14px rgba(85, 28, 62, 0.10);
  --shadow-lg: 0 4px 24px rgba(85, 28, 62, 0.18);
}

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: linear-gradient(140deg, #3a1228 0%, #7a3060 35%, #f6e4e4 100%);
  background-attachment: fixed;
  color: var(--text-primary);
  min-height: 100vh;
  display: grid;
  grid-template-rows: 56px 1fr;
  grid-template-columns: 270px 1fr;
  grid-template-areas:
    "header  header"
    "sidebar main";
  padding-bottom: 48px;
  transition: padding-right 0.25s, padding-bottom 0.25s;
}
body.docked-right {
  padding-bottom: 0;
  padding-right: var(--panel-w, 400px);
}

/* ── Header ── */
header {
  grid-area: header;
  background: var(--color-plum);
  color: #fff;
  padding: 0 1.75rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
  z-index: 10;
}
header h1 { font-size: 1.25rem; font-weight: 700; letter-spacing: -0.02em; }
header p  { font-size: 0.8rem; opacity: 0.72; }

/* ── Sidebar ── */
.sidebar {
  grid-area: sidebar;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1.25rem 1rem;
  overflow-y: auto;
  scrollbar-width: thin;
}

.filter-group { margin-bottom: 1.5rem; }

.filter-group > label {
  display: block;
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  margin-bottom: 0.45rem;
}

.filter-group select,
.filter-group input[type="text"] {
  width: 100%;
  padding: 0.45rem 0.7rem;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 0.82rem;
  color: var(--text-primary);
  background: #fff;
  transition: border-color 0.18s;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23551C3E'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.6rem center;
}
.filter-group input[type="text"] {
  background-image: none;
}
.filter-group select:focus,
.filter-group input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(218,0,112,0.12);
}

.checkbox-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.checkbox-list label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-primary);
  cursor: pointer;
  padding: 0.1rem 0;
}
.checkbox-list input[type="checkbox"] {
  accent-color: var(--color-primary);
  width: 14px; height: 14px;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.toggle-row > label:first-child {
  font-size: 0.82rem;
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-primary);
}
.toggle {
  position: relative;
  width: 38px; height: 21px;
  flex-shrink: 0;
}
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--color-sand);
  border-radius: 21px;
  transition: background 0.25s;
}
.toggle-slider::before {
  content: '';
  position: absolute;
  height: 15px; width: 15px;
  left: 3px; bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.25s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.toggle input:checked + .toggle-slider { background: var(--color-primary); }
.toggle input:checked + .toggle-slider::before { transform: translateX(17px); }

/* ── Main ── */
main {
  grid-area: main;
  padding: 1.25rem 1.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  scrollbar-width: thin;
}

/* ── KPI Cards ── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(165px, 1fr));
  gap: 0.875rem;
}

.kpi-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.875rem 1rem;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  position: relative;
  transition: transform 0.14s, box-shadow 0.14s;
}
.kpi-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}
.kpi-card.hidden { display: none; }

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

.kpi-label {
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.kpi-info {
  position: relative;
  cursor: help;
  color: var(--color-plum);
  font-size: 0.85rem;
  opacity: 0.55;
  transition: opacity 0.15s;
  line-height: 1;
  user-select: none;
}
.kpi-info:hover { opacity: 1; }
.kpi-info .tooltip {
  display: none;
  position: absolute;
  right: 0; top: 1.4rem;
  background: var(--color-plum);
  color: #fff;
  padding: 0.5rem 0.7rem;
  border-radius: 8px;
  font-size: 0.72rem;
  width: 210px;
  line-height: 1.45;
  z-index: 50;
  box-shadow: 0 4px 16px rgba(0,0,0,0.28);
  pointer-events: none;
}
.kpi-info:hover .tooltip { display: block; }

.kpi-value {
  font-size: 1.65rem;
  font-weight: 800;
  color: var(--color-primary);
  line-height: 1;
  letter-spacing: -0.03em;
}

.kpi-badge {
  position: absolute;
  top: 0.45rem; left: 0.45rem;
  background: var(--color-orange);
  color: #1a0a12;
  font-size: 0.6rem;
  font-weight: 800;
  padding: 0.12rem 0.38rem;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Skeleton */
.kpi-card.loading .kpi-value,
.kpi-card.loading .kpi-label {
  background: linear-gradient(90deg, var(--color-sand) 25%, #ede5e0 50%, var(--color-sand) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: 5px;
  color: transparent;
}
.kpi-card.loading .kpi-value { height: 1.8rem; width: 65%; }
.kpi-card.loading .kpi-label { height: 0.65rem; width: 55%; margin-bottom: 0.5rem; }
@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ── Charts ── */
.charts-section { display: flex; flex-direction: column; gap: 0.875rem; }

.chart-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.875rem;
}

.chart-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem 1rem 0.75rem;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
.chart-card.full-width { grid-column: 1 / -1; }

.chart-card-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.6rem;
}
.chart-card-header h3 {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-secondary);
  margin: 0;
}

.chart-container { height: 270px; }
.chart-container.tall { height: 300px; }

.chart-select {
  font-size: 0.72rem;
  padding: 0.22rem 0.5rem;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  background: #fff;
  cursor: pointer;
}
.chart-select:focus { outline: none; border-color: var(--color-primary); }

/* ── Table ── */
.table-section {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
}

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  gap: 0.5rem;
  flex-wrap: wrap;
}
.table-toolbar h3 {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text-secondary);
}

.btn {
  padding: 0.38rem 0.875rem;
  border-radius: 8px;
  border: none;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: #b8005e; }
.btn-secondary { background: var(--color-sand); color: var(--text-primary); }
.btn-secondary:hover { background: #c8b8ae; }
.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1.5px solid var(--border);
}
.btn-ghost:hover { background: var(--color-powder); }

.table-wrapper { overflow-x: auto; max-height: 380px; overflow-y: auto; }

table { width: 100%; border-collapse: collapse; font-size: 0.78rem; }

thead th {
  padding: 0.5rem 0.875rem;
  text-align: left;
  background: var(--color-powder);
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 2;
  border-bottom: 2px solid var(--border);
  transition: background 0.12s;
}
thead th:hover { background: #ecd4d4; }
thead th.sorted .sort-arrow { color: var(--color-primary); font-weight: 900; }
.sort-arrow { margin-left: 0.2rem; font-size: 0.75rem; opacity: 0.45; }
thead th.sorted .sort-arrow { opacity: 1; }

tbody tr {
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
}
tbody tr:hover { background: var(--color-powder); }
tbody td { padding: 0.5rem 0.875rem; vertical-align: middle; }

.badge-anomaly {
  display: inline-block;
  background: var(--color-orange);
  color: #1a0a12;
  font-size: 0.58rem;
  font-weight: 800;
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  text-transform: uppercase;
  margin-left: 0.3rem;
  vertical-align: middle;
  letter-spacing: 0.03em;
}
.badge-lowvol {
  display: inline-block;
  background: var(--color-sky);
  color: #032830;
  font-size: 0.58rem;
  font-weight: 800;
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  text-transform: uppercase;
  margin-left: 0.3rem;
  vertical-align: middle;
  letter-spacing: 0.03em;
}

/* ── Side Panel ── */
.panel-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(30,5,20,0.35);
  z-index: 190;
  backdrop-filter: blur(2px);
}
.panel-overlay.open { display: block; }

.side-panel {
  position: fixed;
  top: 0; right: -440px;
  width: 440px;
  height: 100vh;
  background: #fff;
  box-shadow: -6px 0 32px rgba(85,28,62,0.2);
  z-index: 200;
  transition: right 0.3s cubic-bezier(.4,0,.2,1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.side-panel.open { right: 0; }

.side-panel-header {
  background: var(--color-plum);
  color: #fff;
  padding: 0.875rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.side-panel-title {
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1.3;
  flex: 1;
}

.btn-close {
  background: none;
  border: none;
  color: #fff;
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0.2rem 0.35rem;
  border-radius: 6px;
  opacity: 0.8;
  transition: opacity 0.15s, background 0.15s;
  flex-shrink: 0;
}
.btn-close:hover { opacity: 1; background: rgba(255,255,255,0.15); }

.side-panel-body {
  padding: 1.1rem 1.25rem;
  overflow-y: auto;
  flex: 1;
  scrollbar-width: thin;
}

.panel-anomaly-notice {
  border-radius: 8px;
  padding: 0.6rem 0.875rem;
  margin-bottom: 1rem;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.4;
}

.panel-field { margin-bottom: 0.65rem; }
.panel-field-label {
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 0.1rem;
}
.panel-field-value {
  font-size: 0.82rem;
  color: var(--text-primary);
  word-break: break-word;
}
.panel-section-title {
  font-size: 0.7rem;
  font-weight: 800;
  color: var(--color-plum);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  margin: 1rem 0 0.5rem;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

/* ── Inspector ── */
.inspector-section {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
}
.inspector-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 1rem;
}
.inspector-header span {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
}
.inspector-body { display: none; }
.inspector-body.open { display: block; }
.inspector-body pre {
  padding: 1rem;
  background: #160810;
  color: #f6e4e4;
  font-size: 0.72rem;
  overflow: auto;
  max-height: 420px;
  line-height: 1.55;
  scrollbar-width: thin;
}

/* ── KPI Assistant bar ── */
.assistant-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--color-plum);
  z-index: 200;
}

/* Right-docked mode */
.assistant-bar.docked-right {
  top: 56px;
  bottom: 0;
  right: 0;
  left: auto;
  width: var(--panel-w, 400px);
  display: flex;
  flex-direction: column;
  border-left: 3px solid var(--color-primary);
  box-shadow: -4px 0 24px rgba(85,28,62,0.22);
  transition: width 0s;
}

.assistant-resize-handle {
  display: none;
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 6px;
  cursor: ew-resize;
  z-index: 10;
  background: transparent;
  transition: background 0.15s;
}
.assistant-bar.docked-right .assistant-resize-handle { display: block; }
.assistant-resize-handle:hover,
.assistant-resize-handle.dragging { background: var(--color-primary); opacity: 0.55; }

.assistant-tab {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0 1.5rem;
  height: 48px;
  width: 100%;
  background: none;
  border: none;
  color: #fff;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s;
  flex-shrink: 0;
}
.assistant-tab:hover { background: rgba(255,255,255,0.08); }
.assistant-tab-icon { font-size: 1rem; }
.assistant-tab-open-btn {
  margin-left: auto;
  padding: 0.25rem 0.85rem;
  border-radius: 20px;
  background: var(--color-primary);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  pointer-events: none;
}
.assistant-tab[aria-expanded="true"] .assistant-tab-open-btn { display: none; }
.docked-right .assistant-tab-open-btn { display: none; }
.assistant-tab-arrow { margin-left: 0.6rem; font-size: 0.7rem; opacity: 0.7; }
.docked-right .assistant-tab {
  order: -1;
  cursor: default;
  border-bottom: 1px solid rgba(255,255,255,0.15);
}
.docked-right .assistant-tab:hover { background: none; }
.docked-right .assistant-tab-arrow { display: none; }
.docked-right #btn-assistant-expand,
.docked-right #btn-assistant-shrink { display: none !important; }

.assistant-drawer {
  display: none;
  flex-direction: column;
  background: var(--surface);
  border-top: 3px solid var(--color-primary);
  position: absolute;
  bottom: 48px;
  left: 0; right: 0;
  height: 42vh;
  z-index: 25;
  box-shadow: 0 -4px 24px rgba(85,28,62,0.18);
}
.assistant-drawer.open { display: flex; }
.assistant-drawer.fullscreen { height: calc(100vh - 56px - 48px); }

/* Drawer fills the right panel when docked */
.docked-right .assistant-drawer,
.docked-right .assistant-drawer.open,
.docked-right .assistant-drawer.fullscreen {
  position: static;
  flex: 1;
  height: auto;
  border-top: none;
  box-shadow: none;
  overflow: hidden;
}

.assistant-controls {
  display: flex;
  gap: 0.4rem;
  padding: 0.4rem 1rem 0.2rem;
  justify-content: flex-end;
  border-bottom: 1px solid var(--border);
  background: var(--color-powder);
}

.assistant-history {
  flex: 1;
  overflow-y: auto;
  padding: 0.875rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  scrollbar-width: thin;
}

.chat-msg { max-width: 88%; }
.chat-msg.user {
  align-self: flex-end;
  background: var(--color-primary);
  color: #fff;
  border-radius: 12px 12px 2px 12px;
  padding: 0.55rem 0.875rem;
  font-size: 0.82rem;
  line-height: 1.4;
}
.chat-msg.assistant {
  align-self: flex-start;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px 12px 12px 2px;
  padding: 0.65rem 0.875rem;
  font-size: 0.78rem;
  line-height: 1.5;
  box-shadow: var(--shadow);
}
.chat-section-heading {
  font-size: 0.66rem;
  font-weight: 800;
  color: var(--color-plum);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  margin: 0.55rem 0 0.2rem;
}
.chat-section-heading:first-child { margin-top: 0; }

.assistant-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0.4rem 1rem 0.1rem;
}
.chip {
  padding: 0.28rem 0.65rem;
  border-radius: 20px;
  background: var(--color-powder);
  color: var(--color-plum);
  border: 1.5px solid var(--border);
  font-size: 0.72rem;
  font-weight: 500;
  cursor: pointer;
  transition: 0.15s;
}
.chip:hover {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}

.assistant-input-row {
  display: flex;
  gap: 0.5rem;
  padding: 0.5rem 1rem 0.65rem;
  border-top: 1px solid var(--border);
}
.assistant-input-row input {
  flex: 1;
  padding: 0.45rem 0.75rem;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 0.82rem;
  background: #fff;
}
.assistant-input-row input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(218,0,112,0.1);
}

/* ── API Settings Panel ── */
.api-key-indicator {
  font-size: 0.7rem;
  font-weight: 600;
  margin-left: auto;
  padding: 0.1rem 0.4rem;
}
.api-settings-panel {
  padding: 0.75rem 1rem 0.6rem;
  background: #fdf5f9;
  border-bottom: 1px solid var(--border);
  flex-direction: column;
  gap: 0.4rem;
}
.api-settings-panel:not([hidden]) {
  display: flex;
}
.api-settings-panel label {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.api-settings-panel input[type="password"] {
  width: 100%;
  padding: 0.4rem 0.65rem;
  border: 1.5px solid var(--border);
  border-radius: 7px;
  font-size: 0.82rem;
  font-family: monospace;
  background: #fff;
}
.api-settings-panel input[type="password"]:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(218,0,112,0.1);
}
.api-settings-row {
  display: flex;
  gap: 0.4rem;
  align-items: center;
  flex-wrap: wrap;
}
.api-settings-row select {
  flex: 1;
  min-width: 160px;
  padding: 0.35rem 0.55rem;
  border: 1.5px solid var(--border);
  border-radius: 7px;
  font-size: 0.78rem;
  background: #fff;
  cursor: pointer;
}
.api-settings-hint {
  font-size: 0.68rem;
  color: #94a3b8;
  line-height: 1.4;
}

/* ── Typing / streaming indicator ── */
.typing-indicator {
  display: inline-flex;
  gap: 3px;
  align-items: center;
  height: 16px;
}
.typing-indicator span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--color-primary);
  opacity: 0.5;
  animation: typing-dot 1s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-dot {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ── Responsive ── */
@media (max-width: 860px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: 56px auto 1fr 48px;
    grid-template-areas:
      "header"
      "sidebar"
      "main"
      "asst";
  }
  .chart-row { grid-template-columns: 1fr; }
  .chart-card.full-width { grid-column: 1; }
  .side-panel { width: 100%; right: -100%; }
}
