:root {
  color-scheme: light;
  /* Brand Palette - Modern Indigo/Violet */
  --color-brand-primary: #6366f1;
  /* Indigo 500 */
  --color-brand-secondary: #8b5cf6;
  /* Violet 500 */
  --color-brand-gradient: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);

  /* Semantic Colors */
  --color-bg-app: #f3f4f6;
  /* Cool Gray 100 */
  --color-bg-surface: #ffffff;
  --color-bg-sidebar: #ffffff;

  --color-text-main: #1f2937;
  /* Gray 800 */
  --color-text-muted: #6b7280;
  /* Gray 500 */
  --color-border: #e5e7eb;
  /* Gray 200 */

  --color-success: #10b981;
  --color-warning: #f59e0b;
  --color-danger: #ef4444;

  /* Dimensions & Effects */
  --sidebar-width: 260px;
  --header-height: 70px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  --glass-bg: rgba(255, 255, 255, 0.8);
  --glass-border: rgba(255, 255, 255, 0.5);

  --font-family: 'Inter', system-ui, -apple-system, sans-serif;
  --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --color-bg-app: #0f172a;
  /* Slate 900 */
  --color-bg-surface: #1e293b;
  /* Slate 800 */
  --color-bg-sidebar: #0f172a;

  --color-text-main: #f8fafc;
  /* Slate 50 */
  --color-text-muted: #94a3b8;
  /* Slate 400 */
  --color-border: #334155;
  /* Slate 700 */

  --glass-bg: rgba(30, 41, 59, 0.7);
  --glass-border: rgba(255, 255, 255, 0.05);
}

/* Reset & Base */
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-family);
  background: var(--color-bg-app);
  color: var(--color-text-main);
  height: 100vh;
  overflow: hidden;
  /* App Shell handles scroll */
}

/* --- App Shell Layout --- */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  height: 100vh;
  width: 100vw;
}

/* --- Sidebar --- */
.app-sidebar {
  background: var(--color-bg-sidebar);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  z-index: 20;
}

.sidebar-header {
  padding: 1.5rem;
  display: flex;
  align-items: center;
}

.branding {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo {
  width: 40px;
  height: 40px;
}

.brand-text h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: var(--color-brand-gradient);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.brand-text p {
  margin: 0;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  font-weight: 500;
}

.nav-tabs {
  flex: 1;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  overflow-y: auto;
}

.nav-section-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  font-weight: 600;
  margin: 1rem 0.5rem 0.5rem;
}

.nav-tabs a {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  transition: var(--transition);
  border: 1px solid transparent;
}

.nav-tabs a .icon {
  font-size: 1.1rem;
  filter: grayscale(1);
  transition: var(--transition);
}

.nav-tabs a:hover {
  background: color-mix(in srgb, var(--color-brand-primary) 5%, transparent);
  color: var(--color-text-main);
}

.nav-tabs a:hover .icon {
  filter: grayscale(0);
}

.nav-tabs a.active {
  background: var(--color-brand-gradient);
  color: white;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.nav-tabs a.active .icon {
  filter: grayscale(0) brightness(200%);
}

.sidebar-footer {
  padding: 1.5rem;
  border-top: 1px solid var(--color-border);
  font-size: 0.75rem;
  color: var(--color-text-muted);
  line-height: 1.5;
}

/* --- Main Content Area --- */
.app-content {
  display: flex;
  flex-direction: column;
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.app-topbar {
  height: var(--header-height);
  padding: 0 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--glass-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--color-border);
  z-index: 10;
  position: sticky;
  /* Fix: Keep header visible on scroll if needed, though app-shell should handle valid flex layout */
  top: 0;
  flex-shrink: 0;
  /* Prevent shrinking in flex container */
}

.topbar-controls,
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

label.compact {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--color-text-muted);
  letter-spacing: 0.05em;
}

input,
select {
  background: var(--color-bg-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.6rem;
  font-size: 0.85rem;
  color: var(--color-text-main);
  outline: none;
  transition: var(--transition);
  font-family: inherit;
}

input:focus-visible,
select:focus-visible {
  border-color: var(--color-brand-primary);
  outline: 2px solid var(--color-brand-primary);
  outline-offset: 1px;
  box-shadow: none;
}

/* Remove default focus ring for mouse users if supported */
input:focus:not(:focus-visible),
select:focus:not(:focus-visible) {
  outline: none;
  border-color: var(--color-border);
  box-shadow: none;
}

.icon-btn {
  background: transparent;
  border: 1px solid var(--color-border);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
  cursor: pointer;
  transition: var(--transition);
}

.icon-btn:hover {
  border-color: var(--color-brand-primary);
  color: var(--color-brand-primary);
  background: var(--color-bg-surface);
}

/* Theme Toggle Icons */
.theme-icon-moon {
  display: none;
}

html[data-theme="dark"] .theme-icon-sun {
  display: none;
}

html[data-theme="dark"] .theme-icon-moon {
  display: block;
}


.scroll-area {
  flex: 1;
  overflow-y: auto;
  padding: 2rem;
  scroll-behavior: smooth;
}

main#app {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  padding-bottom: 4rem;
}

/* --- Card Styles --- */
.card {
  background: var(--color-bg-surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  /* Subtle border for definition */
  box-shadow: var(--shadow-sm);
  padding: 1.5rem;
  transition: var(--transition);
}

.card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: color-mix(in srgb, var(--color-brand-primary) 30%, transparent);
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.5rem;
}

.card h2 {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 0 0 0.5rem 0;
}

.kpi-value {
  font-size: 2rem;
  font-weight: 700;
  color: var(--color-text-main);
  letter-spacing: -0.03em;
}

.kpi-trend {
  font-size: 0.85rem;
  margin-top: 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-weight: 500;
}

/* Tone Colors */
.card[data-tone="positive"] .kpi-value,
.card[data-tone="positive"] .kpi-trend {
  color: var(--color-success);
}

.card[data-tone="negative"] .kpi-value,
.card[data-tone="negative"] .kpi-trend {
  color: var(--color-danger);
}

/* --- Charts --- */
.chart-workbench__header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 1.5rem;
}

.chart-workbench__header h2 {
  font-size: 1.25rem;
  text-transform: none;
  letter-spacing: normal;
  color: var(--color-text-main);
  margin: 0;
}

.chart-board {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  /* 2 columns is better for dense dashboards */
  gap: 1.5rem;
}

.chart-board__item {
  background: var(--color-bg-app);
  /* Slight contrast from card surface */
  border-radius: var(--radius-md);
  padding: 1rem;
  border: 1px solid transparent;
  transition: var(--transition);
}

.chart-board__item:hover {
  border-color: var(--color-border);
  background: var(--color-bg-surface);
}

.chart-board__item[data-size="wide"] {
  grid-column: span 2;
}

/* --- Charts Elements --- */
.canvas-wrapper canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.chart-tooltip {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  background: var(--color-bg-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.75rem;
  box-shadow: var(--shadow-lg);
  font-size: 0.75rem;
  color: var(--color-text-main);
  opacity: 0;
  transform: translate(-50%, -120%);
  transition: opacity 0.1s ease;
  z-index: 50;
  white-space: nowrap;
}

.chart-tooltip[data-visible="true"] {
  opacity: 1;
}

.chart-legend {
  list-style: none;
  margin: 1rem 0 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  font-size: 0.8rem;
  color: var(--color-text-muted);
}

.chart-legend li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.chart-legend__swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  background-color: var(--swatch-color);
}

/* --- Tables --- */
.table-header {
  margin-bottom: 1rem;
}

.table-controls {
  display: flex;
  flex-wrap: wrap;
  /* Fix: Allow wrapping so button stays inside box */
  gap: 1rem;
  margin-bottom: 1rem;
  align-items: center;
}

.table-controls input {
  flex: 1;
  max-width: 300px;
}

.table-wrapper {
  overflow-x: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  white-space: nowrap;
}

th,
td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}

th {
  background: var(--color-bg-app);
  font-weight: 600;
  color: var(--color-text-muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  position: sticky;
  top: 0;
}

th:hover {
  color: var(--color-brand-primary);
  cursor: pointer;
}

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

tr:hover td {
  background: color-mix(in srgb, var(--color-brand-primary) 3%, transparent);
}

/* --- Badges & Lists --- */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--color-bg-app);
  color: var(--color-text-main);
  border: 1px solid var(--color-border);
}

.badge.warning {
  background: color-mix(in srgb, var(--color-warning) 10%, transparent);
  color: var(--color-warning);
  border-color: color-mix(in srgb, var(--color-warning) 30%, transparent);
}

.insights ul,
.persona-detail {
  padding-left: 1.25rem;
  margin: 0.5rem 0 0;
  color: var(--color-text-muted);
  font-size: 0.9rem;
  line-height: 1.6;
}

.insights li::marker {
  color: var(--color-brand-primary);
}

button.primary {
  background: var(--color-brand-gradient);
  color: white;
  border: none;
  padding: 0.6rem 1.2rem;
  border-radius: var(--radius-sm);
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 6px rgba(99, 102, 241, 0.25);
  transition: var(--transition);
}

button.primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 10px rgba(99, 102, 241, 0.35);
}

.chip-btn {
  background: transparent;
  border: 1px solid var(--color-border);
  color: var(--color-text-muted);
  padding: 0.3rem 0.8rem;
  border-radius: 999px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: var(--transition);
}

.chip-btn:hover {
  border-color: var(--color-brand-primary);
  color: var(--color-brand-primary);
}

/* --- Mobile / Responsive --- */
@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: 100vh;
  }

  .app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: auto;
    flex-direction: row;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
    padding: 0;
    /* branding hidden on mobile, or managed differently */
    z-index: 100;
    background: var(--color-bg-surface);
    /* Ensure opaque background */
  }

  /* Hide Desktop Sidebar elements on Mobile */
  .sidebar-header,
  .sidebar-footer,
  .nav-section-label {
    display: none;
  }

  .nav-tabs {
    flex-direction: row;
    overflow-x: auto;
    padding: 0.5rem;
    gap: 0.5rem;
  }

  .nav-tabs a {
    flex-direction: column;
    padding: 0.5rem;
    font-size: 0.7rem;
    gap: 0.25rem;
    min-width: 65px;
    justify-content: center;
    border-radius: 8px;
  }

  .nav-tabs a .icon {
    font-size: 1.25rem;
  }

  /* Top Bar Adjustments */
  .app-topbar {
    height: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    align-items: stretch;
  }

  .topbar-controls,
  .topbar-actions {
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 0.75rem;
  }

  .topbar-controls {
    border-bottom: 1px solid var(--color-border);
    padding-bottom: 1rem;
  }

  .control-group {
    flex: 1;
    min-width: 120px;
  }

  /* Adjust Branding for Mobile Header context if needed */
  /* Here we might want to inject a mobile logo into the topbar, but for now we focus on layout */

  .app-content {
    height: 100vh;
    padding-top: 72px;
    /* Space for top nav */
    padding-bottom: 0;
  }

  /* Scroll area needs to respect the fixed bottom nav */
  .scroll-area {
    padding: 1rem;
    padding-bottom: 2rem;
  }

  .chart-board {
    grid-template-columns: 1fr;
    gap: 2rem;
    /* More breathing room */
  }

  /* Give charts more height on mobile so they aren't squashed */
  .canvas-wrapper {
    min-height: 300px;
  }

  .chart-board__item[data-size="wide"] {
    grid-column: auto;
  }
}

/* --- Utilities --- */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}