/* ==========================================================================
   AlphaBOT Dashboard — design system
   Light-first, dark mode via [data-theme="dark"]. No Tailwind, no build.
   ========================================================================== */

@import url('../fonts/fonts.css'); /* self-hosted (Geist + JetBrains Mono) */

:root {
  /* Surfaces */
  --bg: #fafaf9;
  --surface: #ffffff;
  --surface-2: #f4f4f5;
  --surface-3: #e7e7ea;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  /* Text */
  --text: #18181b;
  --text-2: #52525b;
  --text-3: #a1a1aa;
  /* Semantic */
  --pos: #15803d;
  --pos-bg: #dcfce7;
  --neg: #b91c1c;
  --neg-bg: #fee2e2;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --info: #1d4ed8;
  --info-bg: #dbeafe;
  --neutral: #71717a;
  --neutral-bg: #f4f4f5;
  /* Typography */
  --font-sans: 'Geist', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  /* Layout */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --container-max: 1280px;
  --topbar-h: 56px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,.06);
}

[data-theme="dark"] {
  --bg: #09090b;
  --surface: #141417;
  --surface-2: #1c1c20;
  --surface-3: #27272a;
  --border: #27272a;
  --border-strong: #3f3f46;
  --text: #fafafa;
  --text-2: #a1a1aa;
  --text-3: #71717a;
  --pos: #4ade80;
  --pos-bg: rgba(20, 83, 45, .35);
  --neg: #f87171;
  --neg-bg: rgba(127, 29, 29, .35);
  --warn: #fbbf24;
  --warn-bg: rgba(120, 53, 15, .35);
  --info: #60a5fa;
  --info-bg: rgba(30, 58, 138, .35);
  --neutral: #a1a1aa;
  --neutral-bg: #27272a;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,.4);
}

/* ==========================================================================
   Reset / base
   ========================================================================== */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  min-height: 100vh;
}
a { color: inherit; text-decoration: none; }
button { font-family: inherit; }

/* ==========================================================================
   Layout
   ========================================================================== */
.container {
  max-width: var(--container-max);
  margin: 0 auto;
  padding: 0 24px;
}
.page { padding: 28px 0 64px; }

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  height: var(--topbar-h);
  display: flex;
  align-items: center;
}
.topbar-inner {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  max-width: var(--container-max);
  margin: 0 auto;
  padding: 0 24px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 14px;
  letter-spacing: -0.01em;
}
.brand-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--pos);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--pos) 22%, transparent);
  flex-shrink: 0;
}
.brand-dot.is-off { background: var(--neg); box-shadow: 0 0 0 3px color-mix(in srgb, var(--neg) 22%, transparent); }
.brand-version {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-3);
  padding: 2px 6px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
}
.nav {
  display: flex;
  gap: 2px;
  flex: 1;
  justify-content: center;
}
.nav a {
  font-size: 13px;
  padding: 6px 12px;
  border-radius: var(--radius-md);
  color: var(--text-2);
  font-weight: 500;
  transition: background .12s, color .12s;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--surface-2); color: var(--text); }
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-2);
}
.user-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: var(--radius-md);
  background: var(--surface-2);
  font-weight: 500;
  color: var(--text);
}

/* ==========================================================================
   Typography
   ========================================================================== */
.page-title {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0 0 4px;
}
.page-sub {
  font-size: 13px;
  color: var(--text-2);
  margin: 0 0 24px;
}
.section-h {
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-2);
  margin: 32px 0 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.section-h:first-child { margin-top: 0; }
.section-h .ti { font-size: 14px; color: var(--text-3); }
.section-h .badge { margin-left: 6px; }

/* ==========================================================================
   KPI cards
   ========================================================================== */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 18px;
  transition: border-color .12s;
}
.kpi:hover { border-color: var(--border-strong); }
.kpi-label {
  font-size: 11px;
  color: var(--text-2);
  display: flex;
  align-items: center;
  gap: 6px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
}
.kpi-val {
  font-family: var(--font-mono);
  font-size: 24px;
  font-weight: 500;
  margin-top: 10px;
  letter-spacing: -0.02em;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}
.kpi-val.is-sm { font-size: 18px; display: flex; align-items: center; gap: 8px; }
.kpi-val .ti { font-size: 18px; }
.kpi-val sup, .kpi-val .qual {
  font-size: 12px;
  color: var(--text-3);
  margin-left: 4px;
  vertical-align: top;
  font-weight: 400;
}
.kpi-sub {
  font-size: 11px;
  color: var(--text-3);
  font-family: var(--font-mono);
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
}

/* ==========================================================================
   Badges + dots
   ========================================================================== */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  font-weight: 500;
  font-family: var(--font-mono);
  padding: 3px 8px;
  border-radius: var(--radius-md);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  white-space: nowrap;
}
.badge-pos { background: var(--pos-bg); color: var(--pos); }
.badge-neg { background: var(--neg-bg); color: var(--neg); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-info { background: var(--info-bg); color: var(--info); }
.badge-neutral { background: var(--neutral-bg); color: var(--text-2); }
.badge-outline {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-2);
}

.dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot-pos { background: var(--pos); }
.dot-neg { background: var(--neg); }
.dot-warn { background: var(--warn); }
.dot-info { background: var(--info); }
.dot-mute { background: var(--text-3); }

/* ==========================================================================
   Panels + tables + rows
   ========================================================================== */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.panel-title {
  font-size: 13px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
}
.panel-body { padding: 0; }
.panel-body.padded { padding: 16px; }

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  transition: background .1s;
}
.row:last-child { border-bottom: 0; }
.row:hover { background: var(--surface-2); }
.row-l { display: flex; align-items: center; gap: 10px; min-width: 0; }
.row-r { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }

.num {
  font-family: var(--font-mono);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.text-pos { color: var(--pos); }
.text-neg { color: var(--neg); }
.text-warn { color: var(--warn); }
.text-info { color: var(--info); }
.text-mute { color: var(--text-2); }
.text-dim { color: var(--text-3); }
.text-strong { color: var(--text); font-weight: 500; }
.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* Tables */
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.data thead th {
  text-align: left;
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-2);
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  position: sticky;
  top: var(--topbar-h);
}
table.data td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table.data tbody tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover td { background: var(--surface-2); }
table.data .num-cell {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}
table.data th.tar, table.data td.tar { text-align: right; }
table.data th.tac, table.data td.tac { text-align: center; }

/* Metrics grid (split panel) */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.metric { background: var(--surface); padding: 14px 16px; }
.metric-label {
  font-size: 11px;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
}
.metric-val {
  font-family: var(--font-mono);
  font-size: 18px;
  font-weight: 500;
  margin-top: 6px;
  font-variant-numeric: tabular-nums;
}

/* ==========================================================================
   Buttons
   ========================================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all .12s;
  font-family: inherit;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn .ti { font-size: 16px; }

.btn-primary { background: var(--text); color: var(--bg); border-color: var(--text); }
.btn-primary:hover { background: var(--text-2); border-color: var(--text-2); }
.btn-danger { border-color: var(--neg); color: var(--neg); }
.btn-danger:hover { background: var(--neg-bg); }
.btn-success { border-color: var(--pos); color: var(--pos); }
.btn-success:hover { background: var(--pos-bg); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-icon { padding: 7px; }

.btn-group { display: inline-flex; gap: 6px; flex-wrap: wrap; }

/* ==========================================================================
   Empty state
   ========================================================================== */
.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 36px 24px;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  text-align: center;
}
.empty.is-compact { padding: 20px 16px; }
.empty-icon {
  font-size: 28px;
  color: var(--text-3);
  margin-bottom: 10px;
}
.empty-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}
.empty-sub {
  font-size: 12px;
  color: var(--text-3);
  margin-top: 4px;
  max-width: 380px;
  line-height: 1.5;
}

/* ==========================================================================
   Forms
   ========================================================================== */
input[type="text"], input[type="password"], input[type="email"],
input[type="number"], input[type="search"], input[type="tel"],
textarea, select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 13px;
  transition: border-color .12s, box-shadow .12s;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--info);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--info) 16%, transparent);
}
label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-2);
  margin-bottom: 6px;
}
.form-row { margin-bottom: 16px; }
.form-help { font-size: 11px; color: var(--text-3); margin-top: 4px; }

/* ==========================================================================
   Filter bar
   ========================================================================== */
.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 16px;
}
.filter-bar input[type="search"],
.filter-bar select {
  width: auto;
  flex: 0 1 200px;
}
.filter-bar .spacer { flex: 1; }

/* ==========================================================================
   Logs viewer
   ========================================================================== */
.log-viewer {
  background: var(--surface-2);
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 14px 16px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  max-height: 640px;
  overflow-y: auto;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
.log-viewer .log-error { color: var(--neg); }
.log-viewer .log-warn { color: var(--warn); }
.log-viewer .log-info { color: var(--text-2); }
.log-viewer .log-time { color: var(--text-3); }

/* ==========================================================================
   Chart canvas wrappers
   ========================================================================== */
.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 18px;
}
.chart-title {
  font-size: 13px;
  font-weight: 500;
  margin: 0 0 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.chart-wrap {
  position: relative;
  height: 240px;
}
.chart-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* ==========================================================================
   Login
   ========================================================================== */
.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(circle at top right, color-mix(in srgb, var(--info) 6%, transparent), transparent 50%),
    radial-gradient(circle at bottom left, color-mix(in srgb, var(--pos) 5%, transparent), transparent 50%),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px 28px;
  box-shadow: var(--shadow-md);
}
.auth-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  margin-bottom: 4px;
}
.auth-title {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 12px 0 4px;
}
.auth-sub {
  font-size: 13px;
  color: var(--text-2);
  margin: 0 0 24px;
}
.auth-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}

/* ==========================================================================
   Toasts (inline confirmation)
   ========================================================================== */
.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 13px;
  margin-bottom: 12px;
}
.toast .ti { font-size: 16px; flex-shrink: 0; margin-top: 2px; }
.toast.is-success { border-color: var(--pos); background: var(--pos-bg); color: var(--pos); }
.toast.is-warn { border-color: var(--warn); background: var(--warn-bg); color: var(--warn); }
.toast.is-danger { border-color: var(--neg); background: var(--neg-bg); color: var(--neg); }
.toast.is-info { border-color: var(--info); background: var(--info-bg); color: var(--info); }

/* ==========================================================================
   Skeleton (loading)
   ========================================================================== */
.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 0%, var(--surface-3) 50%, var(--surface-2) 100%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.4s infinite;
  border-radius: var(--radius-md);
}
@keyframes skeleton-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ==========================================================================
   Utility
   ========================================================================== */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.flex-row { display: flex; align-items: center; gap: 8px; }
.flex-col { display: flex; flex-direction: column; gap: 8px; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.flex-wrap { flex-wrap: wrap; }
.flex-1 { flex: 1; min-width: 0; }
.mt-1 { margin-top: 4px; } .mt-2 { margin-top: 8px; } .mt-3 { margin-top: 16px; } .mt-4 { margin-top: 24px; } .mt-5 { margin-top: 32px; }
.mb-1 { margin-bottom: 4px; } .mb-2 { margin-bottom: 8px; } .mb-3 { margin-bottom: 16px; } .mb-4 { margin-bottom: 24px; }
.gap-1 { gap: 4px; } .gap-2 { gap: 8px; } .gap-3 { gap: 16px; }
.w-full { width: 100%; }
.hidden { display: none !important; }
.text-center { text-align: center; }
.text-right { text-align: right; }

/* HTMX indicator */
.htmx-indicator { opacity: 0; transition: opacity .2s; }
.htmx-request .htmx-indicator { opacity: 1; }
.htmx-request.htmx-indicator { opacity: 1; }

/* Spinner */
.spinner {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  border-top-color: var(--text-2);
  animation: spin 0.8s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Subtle entry animation for first load */
.page > * { animation: page-in .35s ease-out both; }
.page > *:nth-child(1) { animation-delay: .02s; }
.page > *:nth-child(2) { animation-delay: .06s; }
.page > *:nth-child(3) { animation-delay: .10s; }
.page > *:nth-child(4) { animation-delay: .14s; }
.page > *:nth-child(5) { animation-delay: .18s; }
.page > *:nth-child(6) { animation-delay: .22s; }
@keyframes page-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
/* Rete di sicurezza: l'entry animation non deve MAI lasciare un elemento
   invisibile. Con prefers-reduced-motion molti browser non eseguono
   l'animazione e 'both' manterrebbe opacity:0 (riga posizioni sparita). */
@media (prefers-reduced-motion: reduce) {
  .page > * { animation: none !important; opacity: 1 !important; transform: none !important; }
}

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 900px) {
  .nav { gap: 0; }
  .nav a { padding: 6px 8px; font-size: 12px; }
  .chart-grid { grid-template-columns: 1fr; }
}
@media (max-width: 700px) {
  .container { padding: 0 16px; }
  .topbar-inner { padding: 0 16px; gap: 12px; }
  .brand-version { display: none; }
  .nav { display: none; }
  .nav.is-mobile-open { display: flex; flex-direction: column; position: absolute; top: var(--topbar-h); left: 0; right: 0; background: var(--surface); border-bottom: 1px solid var(--border); padding: 12px 16px; gap: 4px; }
  .nav.is-mobile-open a { padding: 10px 12px; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .page-title { font-size: 20px; }
}
@media (min-width: 700px) {
  .mobile-only { display: none; }
}

/* Print: hide chrome */
@media print {
  .topbar, .filter-bar, .btn-group { display: none; }
  .page { padding: 0; }
}
