/* Art direction: LEGO collector tool → structured, confident, warm
   Palette: warm beige surfaces, LEGO-red accent
   Typography: DM Sans (body) — functional dashboard font
   Density: balanced-dense (data-heavy)
*/

/* ── Google Fonts ─────────────────────────────────────────── */
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@300..700&display=swap');

/* ── Tokens ───────────────────────────────────────────────── */
:root {
  --font-body: 'DM Sans', 'Helvetica Neue', sans-serif;

  --text-xs:   clamp(0.75rem,  0.7rem  + 0.25vw, 0.875rem);
  --text-sm:   clamp(0.875rem, 0.8rem  + 0.35vw, 1rem);
  --text-base: clamp(1rem,     0.95rem + 0.25vw, 1.125rem);
  --text-lg:   clamp(1.125rem, 1rem    + 0.75vw, 1.5rem);
  --text-xl:   clamp(1.5rem,   1.2rem  + 1.25vw, 2.25rem);

  --space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem;
  --space-4: 1rem;    --space-5: 1.25rem; --space-6: 1.5rem;
  --space-8: 2rem;    --space-10: 2.5rem; --space-12: 3rem;

  --radius-sm: 0.375rem; --radius-md: 0.5rem;
  --radius-lg: 0.75rem;  --radius-xl: 1rem; --radius-full: 9999px;

  --transition: 180ms cubic-bezier(0.16, 1, 0.3, 1);

  /* Surfaces */
  --color-bg:             #f7f5f0;
  --color-surface:        #faf9f6;
  --color-surface-2:      #ffffff;
  --color-surface-offset: #f0ede7;
  --color-divider:        #e0dbd2;
  --color-border:         #d4cfc6;

  /* Text */
  --color-text:           #1e1c18;
  --color-text-muted:     #6b6860;
  --color-text-faint:     #b0ada6;
  --color-text-inverse:   #f9f8f4;

  /* LEGO red accent */
  --color-primary:        #c4001a;
  --color-primary-hover:  #a80016;
  --color-primary-active: #860011;
  --color-primary-highlight: #f5dde0;

  /* Status */
  --color-success:        #2e7d32;
  --color-success-highlight: #d4edda;
  --color-warning:        #b45309;
  --color-warning-highlight: #fef3c7;
  --color-error:          #c62828;
  --color-error-highlight: #fde8e8;

  /* Shadows */
  --shadow-sm: 0 1px 3px oklch(0.15 0.01 60 / 0.07);
  --shadow-md: 0 4px 12px oklch(0.15 0.01 60 / 0.09);
  --shadow-lg: 0 12px 32px oklch(0.15 0.01 60 / 0.13);

  --sidebar-w: 240px;
  --topbar-h: 56px;
}

[data-theme="dark"] {
  --color-bg:             #131210;
  --color-surface:        #1a1917;
  --color-surface-2:      #1f1e1c;
  --color-surface-offset: #242320;
  --color-divider:        #2e2c29;
  --color-border:         #3a3834;
  --color-text:           #d4d2ce;
  --color-text-muted:     #7d7b77;
  --color-text-faint:     #504e4a;
  --color-text-inverse:   #1e1c18;
  --color-primary:        #ff3d55;
  --color-primary-hover:  #e02040;
  --color-primary-active: #c2132e;
  --color-primary-highlight: #3b2025;
  --color-success:        #4caf50;
  --color-success-highlight: #1b3820;
  --color-warning:        #f59e0b;
  --color-warning-highlight: #3b2e0e;
  --color-error:          #ef5350;
  --color-error-highlight: #3b1c1c;
  --shadow-sm: 0 1px 3px oklch(0 0 0 / 0.25);
  --shadow-md: 0 4px 12px oklch(0 0 0 / 0.35);
  --shadow-lg: 0 12px 32px oklch(0 0 0 / 0.45);
}

/* ── Base ─────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; scroll-behavior: smooth; }
html, body { overflow: visible; }
body {
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-bg);
  min-height: 100dvh;
  line-height: 1.6;
}
img { display: block; max-width: 100%; height: auto; }
input, button, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: none; }
table { border-collapse: collapse; width: 100%; }
:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 3px; border-radius: var(--radius-sm); }

/* ── Layout shell ─────────────────────────────────────────── */
.app-shell { display: flex; min-height: 100dvh; overflow: visible; }

.sidebar {
  width: var(--sidebar-w);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  transition: transform var(--transition);
}

.sidebar-logo {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-5) var(--space-5);
  border-bottom: 1px solid var(--color-divider);
  text-decoration: none;
}

.sidebar-logo-text {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.logo-name {
  font-size: var(--text-lg);
  font-weight: 700;
  color: var(--color-primary);
}

.sidebar-version {
  font-size: 10px;
  font-weight: 400;
  color: var(--color-text-faint);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.sidebar-logo svg { flex-shrink: 0; }

.sidebar-nav { flex: 1; padding: var(--space-4) var(--space-3); overflow-y: auto; }

.nav-label {
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-faint);
  padding: var(--space-4) var(--space-3) var(--space-2);
}

.nav-item {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  text-decoration: none;
  font-size: var(--text-sm);
  font-weight: 500;
  transition: background var(--transition), color var(--transition);
}
.nav-item:hover { background: var(--color-surface-offset); color: var(--color-text); }
.nav-item.active { background: var(--color-primary-highlight); color: var(--color-primary); }
.nav-item svg { flex-shrink: 0; opacity: 0.7; }
.nav-item.active svg { opacity: 1; }

.sidebar-footer {
  padding: var(--space-4) var(--space-4);
  border-top: 1px solid var(--color-divider);
  font-size: var(--text-xs);
  color: var(--color-text-faint);
}

.main-content {
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: visible;
}

.topbar {
  height: var(--topbar-h);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex; align-items: center;
  padding: 0 var(--space-6);
  gap: var(--space-4);
  position: sticky; top: 0; z-index: 50;
}
.topbar-title { font-size: var(--text-lg); font-weight: 600; flex: 1; }
.topbar-actions { display: flex; align-items: center; gap: var(--space-3); }

.page-content { padding: var(--space-8) var(--space-6); flex: 1; overflow: visible; }

/* ── Components ───────────────────────────────────────────── */
/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 500;
  text-decoration: none;
  transition: background var(--transition), color var(--transition), box-shadow var(--transition);
  white-space: nowrap;
}
.btn-primary {
  background: var(--color-primary);
  color: #fff;
}
.btn-primary:hover { background: var(--color-primary-hover); box-shadow: var(--shadow-sm); }
.btn-secondary {
  background: var(--color-surface-offset);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}
.btn-secondary:hover { background: var(--color-divider); }
.btn-danger {
  background: transparent;
  color: var(--color-error);
  border: 1px solid var(--color-error);
}
.btn-danger:hover { background: var(--color-error-highlight); }
.btn-sm { padding: var(--space-1) var(--space-3); font-size: var(--text-xs); }
.btn-icon { padding: var(--space-2); border-radius: var(--radius-md); color: var(--color-text-muted); }
.btn-icon:hover { background: var(--color-surface-offset); color: var(--color-text); }

/* Cards */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: visible;
}
.card-header {
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--color-divider);
  display: flex; align-items: center; justify-content: space-between;
}
.card-title { font-size: var(--text-lg); font-weight: 600; }
.card-body { padding: var(--space-5) var(--space-6); }

/* KPI cards */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: var(--space-4); margin-bottom: var(--space-8); }
.kpi-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
}
.kpi-label { font-size: var(--text-xs); color: var(--color-text-muted); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: var(--space-2); }
.kpi-value { font-size: var(--text-xl); font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.kpi-sub { font-size: var(--text-xs); color: var(--color-text-muted); margin-top: var(--space-1); }
.kpi-up { color: var(--color-success); }
.kpi-down { color: var(--color-error); }

/* Tables */
.table-wrap { overflow: visible; }
table { font-size: var(--text-sm); border-collapse: separate; border-spacing: 0; }
thead th {
  position: sticky;
  top: var(--topbar-h);
  z-index: 10;
  text-align: left;
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-text-muted);
  border-bottom: 2px solid var(--color-divider);
  background: var(--color-surface-offset);
  white-space: nowrap;
}
tbody td {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid oklch(from var(--color-text) l c h / 0.06);
  vertical-align: middle;
  background: var(--color-surface);
}
tbody tr:hover { background: var(--color-surface-offset); }
tbody tr:last-child td { border-bottom: none; }

/* Badges */
.badge {
  display: inline-flex; align-items: center;
  padding: 2px var(--space-2);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 500;
}
.badge-green { background: var(--color-success-highlight); color: var(--color-success); }
.badge-red   { background: var(--color-error-highlight);   color: var(--color-error); }
.badge-amber { background: var(--color-warning-highlight); color: var(--color-warning); }
.badge-neutral { background: var(--color-surface-offset); color: var(--color-text-muted); }

/* Forms */
.form-group { display: flex; flex-direction: column; gap: var(--space-1); }
.form-label { font-size: var(--text-sm); font-weight: 500; }
.form-input, .form-select, .form-textarea {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface-2);
  font-size: var(--text-sm);
  transition: border-color var(--transition), box-shadow var(--transition);
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px oklch(from var(--color-primary) l c h / 0.15);
}
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: var(--space-4); }

/* Alerts */
.alert {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  margin-bottom: var(--space-4);
}
.alert-error   { background: var(--color-error-highlight);   color: var(--color-error); }
.alert-success { background: var(--color-success-highlight); color: var(--color-success); }

/* Charts */
.chart-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(400px, 1fr)); gap: var(--space-6); margin-bottom: var(--space-8); }
.chart-card { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-lg); padding: var(--space-5); box-shadow: var(--shadow-sm); }
.chart-card-title { font-size: var(--text-base); font-weight: 600; margin-bottom: var(--space-4); }
.chart-container { position: relative; height: 220px; }

/* Set thumbnail */
.set-thumb { width: 56px; height: 42px; object-fit: contain; border-radius: var(--radius-sm); background: var(--color-surface-offset); }

/* Auth pages */
.auth-shell { min-height: 100dvh; display: flex; align-items: center; justify-content: center; background: var(--color-bg); }
.auth-card { width: min(420px, 90vw); background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-xl); padding: var(--space-10); box-shadow: var(--shadow-lg); }
.auth-title { font-size: var(--text-xl); font-weight: 700; margin-bottom: var(--space-6); }
.auth-logo { display: flex; align-items: center; gap: var(--space-3); margin-bottom: var(--space-8); color: var(--color-primary); font-size: var(--text-lg); font-weight: 700; }

/* HTMX loading indicator */
.htmx-indicator { display: none; }
.htmx-request .htmx-indicator { display: inline-flex; }
.htmx-request.htmx-indicator { display: inline-flex; }

/* Search bar */
.search-input {
  padding: var(--space-2) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  background: var(--color-surface-2);
  font-size: var(--text-sm);
  width: 280px;
  transition: border-color var(--transition), box-shadow var(--transition);
}
.search-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px oklch(from var(--color-primary) l c h / 0.12);
}

/* Responsive */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main-content { margin-left: 0; }
  .topbar { padding: 0 var(--space-4); }
  thead th { top: var(--topbar-h); }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .chart-grid { grid-template-columns: 1fr; }
  .page-content { padding: var(--space-4) var(--space-4); }
}

/* Dark mode system default */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --color-bg: #131210; --color-surface: #1a1917; --color-surface-2: #1f1e1c;
    --color-surface-offset: #242320; --color-divider: #2e2c29; --color-border: #3a3834;
    --color-text: #d4d2ce; --color-text-muted: #7d7b77; --color-text-faint: #504e4a;
    --color-primary: #ff3d55; --color-primary-hover: #e02040; --color-primary-highlight: #3b2025;
    --color-success: #4caf50; --color-success-highlight: #1b3820;
    --color-warning: #f59e0b; --color-warning-highlight: #3b2e0e;
    --color-error: #ef5350; --color-error-highlight: #3b1c1c;
    --shadow-sm: 0 1px 3px oklch(0 0 0 / 0.25); --shadow-md: 0 4px 12px oklch(0 0 0 / 0.35);
    --shadow-lg: 0 12px 32px oklch(0 0 0 / 0.45);
  }
}
