/* ============ Design tokens ============ */
:root {
  --bg: #f5f7fb;
  --bg-grad-1: rgba(99, 102, 241, 0.08);
  --bg-grad-2: rgba(14, 165, 233, 0.06);
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --accent: #6366f1;
  --accent-2: #06b6d4;
  --accent-soft: rgba(99, 102, 241, 0.12);
  --anomaly: #ef4444;
  --anomaly-soft: rgba(239, 68, 68, 0.12);
  --normal-line: #6366f1;
  --normal-fill: #818cf8;
  --grid: rgba(15, 23, 42, 0.06);
  --axis: rgba(15, 23, 42, 0.45);
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.06), 0 2px 6px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 20px 50px rgba(15, 23, 42, 0.10);
  --radius: 16px;
  --radius-sm: 10px;
}

[data-theme="dark"] {
  --bg: #0b1020;
  --bg-grad-1: rgba(99, 102, 241, 0.18);
  --bg-grad-2: rgba(6, 182, 212, 0.12);
  --surface: #131a2e;
  --surface-2: #1a2240;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: #283154;
  --accent: #818cf8;
  --accent-2: #22d3ee;
  --accent-soft: rgba(129, 140, 248, 0.18);
  --anomaly: #f87171;
  --anomaly-soft: rgba(248, 113, 113, 0.18);
  --normal-line: #818cf8;
  --normal-fill: #a5b4fc;
  --grid: rgba(226, 232, 240, 0.08);
  --axis: rgba(226, 232, 240, 0.55);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.35), 0 2px 6px rgba(0, 0, 0, 0.25);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.5);
}

/* ============ Reset & base ============ */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  background-image:
    radial-gradient(1200px 600px at -10% -20%, var(--bg-grad-1), transparent 60%),
    radial-gradient(900px 500px at 110% 0%, var(--bg-grad-2), transparent 55%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  transition: background-color .25s ease, color .25s ease;
}

h1, h2, h3, p { margin: 0; }
button { font: inherit; cursor: pointer; }
hr { border: none; }

/* ============ Layout ============ */
.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 28px 24px 40px;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-md);
}

.brand h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.subtitle {
  color: var(--text-muted);
  font-size: 12.5px;
  margin-top: 2px;
}

/* Theme toggle */
.theme-toggle {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-sm);
  transition: background-color .2s, transform .15s, border-color .2s;
}
.theme-toggle:hover { transform: translateY(-1px); border-color: var(--accent); }
.theme-toggle .icon-moon { display: none; }
[data-theme="dark"] .theme-toggle .icon-sun { display: none; }
[data-theme="dark"] .theme-toggle .icon-moon { display: block; }

/* Dashboard grid */
.dashboard {
  display: grid;
  grid-template-columns: 360px 1fr;
  grid-template-areas:
    "controls chart"
    "controls stats"
    "controls list"
    "table    table";
  gap: 18px;
}

.controls   { grid-area: controls; }
.chart-card { grid-area: chart; }
.stats-card { grid-area: stats; }
.list-card  { grid-area: list; }
.table-card { grid-area: table; }

@media (max-width: 960px) {
  .dashboard {
    grid-template-columns: 1fr;
    grid-template-areas:
      "controls"
      "chart"
      "stats"
      "list"
      "table";
  }
}

/* ============ Cards ============ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-md);
  transition: background-color .25s, border-color .25s;
}

.card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 12px;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.divider {
  height: 1px;
  background: var(--border);
  margin: 18px 0;
}

/* ============ Form ============ */
.field {
  display: block;
  margin-bottom: 14px;
}
.field-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}
.field-hint {
  display: block;
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--text-muted);
}
.threshold-value {
  font-variant-numeric: tabular-nums;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}

textarea, select {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  resize: vertical;
  transition: border-color .2s, box-shadow .2s, background-color .25s;
}
textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
}

textarea {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  min-height: 110px;
}

/* Range slider */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  background: linear-gradient(to right, var(--accent), var(--accent-2));
  border-radius: 999px;
  outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--surface);
  border: 2px solid var(--accent);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
  transition: transform .12s;
}
input[type="range"]::-webkit-slider-thumb:active { transform: scale(1.15); }
input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--surface);
  border: 2px solid var(--accent);
  cursor: pointer;
}

/* Buttons & file input */
.row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 4px;
}

.btn {
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  padding: 8px 12px;
  background: var(--surface-2);
  color: var(--text);
  transition: background-color .2s, border-color .2s, transform .12s;
}
.btn:hover { border-color: var(--accent); transform: translateY(-1px); }
.btn-ghost { background: transparent; }
.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border-color: transparent;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover { border-color: transparent; filter: brightness(1.05); }
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  filter: none;
}

.file-input {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex: 1 1 auto;
  min-width: 180px;
}
.file-input input[type="file"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}
.file-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 600;
  font-size: 12.5px;
  box-shadow: var(--shadow-sm);
  transition: transform .12s;
}
.file-input:hover .file-button { transform: translateY(-1px); }
.file-name {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}

/* Error banner */
.error-banner {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--anomaly-soft);
  color: var(--anomaly);
  font-size: 12.5px;
  font-weight: 500;
  border: 1px solid var(--anomaly-soft);
  animation: pop .2s ease-out;
}

/* ============ Chart ============ */
.chart-wrap {
  position: relative;
  height: 320px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  overflow: hidden;
}
#chart {
  width: 100%;
  height: 100%;
  display: block;
}
.tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--text);
  color: var(--surface);
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  box-shadow: var(--shadow-md);
  transform: translate(-50%, -130%);
  white-space: nowrap;
  transition: opacity .12s;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
  padding: 20px;
}

.legend {
  display: flex;
  gap: 14px;
  font-size: 12px;
  color: var(--text-muted);
}
.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
}
.dot-normal { background: var(--normal-fill); }
.dot-anomaly { background: var(--anomaly); }

/* ============ Stats ============ */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.stat {
  padding: 14px 16px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: transform .15s;
}
.stat:hover { transform: translateY(-1px); }
.stat-label {
  font-size: 11.5px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.stat-value {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  animation: pop .25s ease-out;
}
.accent-anomaly { color: var(--anomaly); }

@media (max-width: 520px) {
  .stats-grid { grid-template-columns: 1fr; }
}

/* ============ Anomaly list ============ */
.anomaly-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 160px;
  overflow-y: auto;
}
.anomaly-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--anomaly-soft);
  color: var(--anomaly);
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  border: 1px solid transparent;
  transition: transform .12s;
}
.anomaly-pill:hover { transform: translateY(-1px); }
.anomaly-pill .pill-idx {
  font-size: 11px;
  opacity: 0.7;
}
.muted { color: var(--text-muted); font-size: 13px; }

/* ============ Table ============ */
.table-wrap {
  max-height: 320px;
  overflow: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
th, td {
  text-align: left;
  padding: 9px 14px;
  font-variant-numeric: tabular-nums;
}
thead th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border-bottom: 1px solid var(--border);
  z-index: 1;
}
tbody tr:nth-child(odd) { background: var(--surface-2); }
tbody tr.is-anomaly {
  background: var(--anomaly-soft);
  color: var(--anomaly);
  font-weight: 600;
}
tbody tr.is-anomaly:nth-child(odd) { background: var(--anomaly-soft); }

/* ============ Footer ============ */
.app-footer {
  margin-top: 24px;
  text-align: center;
  color: var(--text-muted);
  font-size: 12px;
}
.app-footer a {
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}
.app-footer a:hover {
  text-decoration: underline;
}

/* ============ Animations ============ */
@keyframes pop {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}
