/* ============================================================================
   OutlierLens — Client-Side ML Anomaly Detector for CSVs
   Monochrome base + red (High-risk anomaly) & orange (Duplicate / Watch)
   Dark (#0D0D10) & Light (#FBFBFD) synced via /theme.js
   ============================================================================ */

:root {
  --bg: #0d0d10;
  --surface: #141419;
  --card: #18181f;
  --card-hover: #21212b;
  --header-bg: rgba(13, 13, 16, 0.82);
  --border: rgba(255, 255, 255, 0.10);
  --border-strong: rgba(255, 255, 255, 0.20);
  --heading: #f5f5f7;
  --text: #d1d1d6;
  --muted: #8e8e93;
  --inv-bg: #f5f5f7;
  --inv-fg: #0d0d10;
  --accent-red: #ff453a;
  --accent-orange: #ff9f0a;
  --red-tint: rgba(255, 69, 58, 0.14);
  --orange-tint: rgba(255, 159, 10, 0.14);
  --chip-bg: rgba(255, 255, 255, 0.07);
  --radius: 14px;
}

:root[data-theme="light"] {
  --bg: #fbfbfd;
  --surface: #f2f2f6;
  --card: #ffffff;
  --card-hover: #f5f5f7;
  --header-bg: rgba(251, 251, 253, 0.82);
  --border: rgba(0, 0, 0, 0.09);
  --border-strong: rgba(0, 0, 0, 0.18);
  --heading: #1d1d1f;
  --text: #424245;
  --muted: #6e6e73;
  --inv-bg: #1d1d1f;
  --inv-fg: #fbfbfd;
  --accent-red: #d92d20;
  --accent-orange: #ea580c;
  --red-tint: rgba(217, 45, 32, 0.10);
  --orange-tint: rgba(234, 88, 12, 0.11);
  --chip-bg: rgba(0, 0, 0, 0.055);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Inter", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.2s ease, color 0.2s ease;
}

a { color: inherit; text-decoration: none; }
button, input, select { font-family: inherit; color: inherit; }
strong { color: var(--heading); }
code {
  font-family: "SF Mono", "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.88em;
  padding: 0.1em 0.35em;
  border-radius: 5px;
  background: var(--chip-bg);
  color: var(--heading);
}
.mono { font-family: "SF Mono", "JetBrains Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.muted-text { color: var(--muted); font-size: 0.78rem; }

:focus-visible { outline: 2px solid var(--accent-orange); outline-offset: 2px; border-radius: 4px; }

/* FULL-BLEED STICKY TOPBAR */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  min-height: 50px;
  padding: max(0.6rem, env(safe-area-inset-top, 0px)) max(clamp(1rem, 4vw, 2.5rem), env(safe-area-inset-right, 0px)) 0.6rem max(clamp(1rem, 4vw, 2.5rem), env(safe-area-inset-left, 0px));
  border-bottom: 1px solid var(--border);
  background: var(--header-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 0.65rem; }
.back-link { font-size: 0.8rem; color: var(--muted); transition: color 0.15s; white-space: nowrap; }
.back-link:hover { color: var(--heading); }
.sep { color: var(--border-strong); user-select: none; }
.logo-wrap { display: flex; align-items: center; gap: 0.5rem; color: var(--heading); }
.logo-gem {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--inv-fg);
  background: var(--inv-bg);
}
.tag {
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding: 0.18rem 0.48rem;
  border-radius: 6px;
  background: var(--chip-bg);
}

.icon-btn, .theme-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  height: 32px;
  padding: 0 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  font-size: 0.76rem;
  color: var(--text);
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}
.theme-btn { width: 32px; padding: 0; }
.icon-btn:hover, .theme-btn:hover { background: var(--chip-bg); border-color: var(--border-strong); color: var(--heading); }
.theme-btn svg { width: 15px; height: 15px; }
.theme-btn .icon-sun { display: block; }
.theme-btn .icon-moon { display: none; }
:root[data-theme="light"] .theme-btn .icon-sun { display: none; }
:root[data-theme="light"] .theme-btn .icon-moon { display: block; }

/* MAIN SHELL */
.shell {
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding: clamp(1.25rem, 3vw, 2rem) clamp(1rem, 3vw, 2rem) 3rem;
  flex: 1;
}

/* DROPZONE */
.drop-section {
  display: grid;
  place-items: center;
  min-height: calc(100vh - 140px);
}
.drop-section[hidden], .workspace[hidden] { display: none; }

.dropzone {
  width: 100%;
  max-width: 660px;
  padding: clamp(2rem, 5vw, 3.25rem) clamp(1.5rem, 4vw, 2.5rem);
  border-radius: 18px;
  border: 1.5px dashed var(--border-strong);
  background: var(--card);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;
}
.dropzone.is-dragover {
  border-color: var(--accent-orange);
  background: var(--surface);
}
.drop-badge {
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  color: var(--muted);
  padding: 0.25rem 0.65rem;
  border-radius: 999px;
  background: var(--chip-bg);
}
.dropzone h1 {
  font-size: clamp(1.45rem, 3vw, 1.9rem);
  line-height: 1.2;
  letter-spacing: -0.025em;
  color: var(--heading);
  max-width: 24ch;
}
.drop-sub {
  font-size: 0.94rem;
  line-height: 1.55;
  color: var(--muted);
  max-width: 52ch;
}
.drop-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.65rem;
  margin-top: 0.35rem;
}
.drop-foot { font-size: 0.72rem; color: var(--muted); margin-top: 0.25rem; }
.error-msg { font-size: 0.78rem; color: var(--accent-red); }

/* BUTTONS */
.btn-primary {
  height: 38px;
  padding: 0 1rem;
  border-radius: 10px;
  border: 1px solid transparent;
  background: var(--inv-bg);
  color: var(--inv-fg);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.14s;
}
.btn-primary:hover { opacity: 0.9; }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-secondary {
  height: 38px;
  padding: 0 1rem;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--heading);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.14s;
}
.btn-secondary:hover { background: var(--card-hover); }

.mini-btn {
  height: 26px;
  padding: 0 0.55rem;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.72rem;
  cursor: pointer;
}
.mini-btn:hover { border-color: var(--border-strong); color: var(--heading); }

/* WORKSPACE */
.workspace {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
}

.toolbar-top {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding-bottom: 0.85rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.85rem;
}
.file-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
}
.file-name { font-size: 0.95rem; }

.controls-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.85rem;
  align-items: end;
}
.ctrl-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.ctrl-label {
  font-size: 0.75rem;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
}
.ctrl-field select {
  height: 34px;
  padding: 0 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--heading);
  font-size: 0.82rem;
}
.threshold-field input[type="range"] {
  width: 100%;
  height: 34px;
  accent-color: var(--accent-red);
  cursor: pointer;
}

/* STATS STRIP */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.85rem;
}
.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1.05rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}
.stat-label { font-size: 0.75rem; color: var(--muted); }
.stat-val { font-size: 1.35rem; font-weight: 600; color: var(--heading); letter-spacing: -0.02em; }
.stat-val.is-red { color: var(--accent-red); }
.stat-val.is-orange { color: var(--accent-orange); }

/* SCATTER CHART */
.panel-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.7rem;
}
.panel-head h2 { font-size: 0.92rem; font-weight: 600; color: var(--heading); }
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.9rem;
  font-size: 0.72rem;
  color: var(--muted);
}
.legend span { display: inline-flex; align-items: center; gap: 0.35rem; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot-red { background: var(--accent-red); }
.dot-orange { background: var(--accent-orange); }
.dot-muted { background: var(--muted); opacity: 0.6; }

.chart-wrap {
  position: relative;
  width: 100%;
  height: 210px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  overflow: hidden;
}
#scatterCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}
.chart-tooltip {
  position: absolute;
  left: 10px;
  top: 10px;
  padding: 0.28rem 0.55rem;
  border-radius: 7px;
  font-size: 0.73rem;
  background: var(--header-bg);
  color: var(--heading);
  border: 1px solid var(--border);
  backdrop-filter: blur(10px);
  pointer-events: none;
}

/* TABLE */
.table-toolbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.7rem;
  margin-bottom: 0.8rem;
}
.filter-tabs {
  display: inline-flex;
  gap: 0.22rem;
  padding: 0.22rem;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.tab-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 500;
  padding: 0.34rem 0.7rem;
  border-radius: 7px;
  cursor: pointer;
}
.tab-btn:hover { color: var(--heading); }
.tab-btn.is-active { background: var(--card); color: var(--heading); }
.tab-count { font-size: 0.72rem; opacity: 0.75; }

.search-input {
  height: 34px;
  width: min(280px, 100%);
  padding: 0 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--heading);
  font-size: 0.82rem;
}
.search-input::placeholder { color: var(--muted); }

.table-scroll {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  text-align: left;
}
.data-table th {
  padding: 0.6rem 0.75rem;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.data-table td {
  padding: 0.58rem 0.75rem;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  white-space: nowrap;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.data-table tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: var(--surface); }
.data-table tbody tr.is-highlighted { background: var(--orange-tint); }

.score-pill {
  display: inline-block;
  padding: 0.14rem 0.45rem;
  border-radius: 6px;
  font-size: 0.74rem;
  font-weight: 600;
}
.score-high { background: var(--red-tint); color: var(--accent-red); }
.score-med { background: var(--orange-tint); color: var(--accent-orange); }
.score-ok { background: var(--chip-bg); color: var(--muted); }

.reason-list {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}
.reason-chip {
  font-size: 0.72rem;
  padding: 0.14rem 0.44rem;
  border-radius: 6px;
  background: var(--chip-bg);
  color: var(--heading);
}
.table-foot {
  margin-top: 0.65rem;
  font-size: 0.74rem;
  color: var(--muted);
}

/* MODAL */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.58);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  padding: 1rem;
}
.modal-backdrop[hidden] { display: none; }
.modal-card {
  width: 100%;
  max-width: 490px;
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  padding: 1.25rem 1.35rem;
}
.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.85rem;
}
.modal-head h2 { font-size: 1rem; color: var(--heading); }
.modal-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  font-size: 0.88rem;
  line-height: 1.55;
  color: var(--text);
}
.guide-list {
  padding-left: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* RESPONSIVE */
@media (max-width: 820px) {
  .controls-grid, .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tag { display: none; }
}
@media (max-width: 520px) {
  .controls-grid, .stats-grid { grid-template-columns: 1fr; }
  .back-link { font-size: 0.74rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
}
