/* ===================== Design tokens ===================== */
:root {
  --bg: #0a0e14;
  --bg-elevated: #10161f;
  --bg-panel: #131a24;
  --bg-inset: #0d131b;
  --border: #23303f;
  --border-soft: #1a2432;
  --text: #e7edf5;
  --text-muted: #8494a8;
  --accent: #00e5ff;
  --accent-soft: rgba(0, 229, 255, 0.15);
  --accent-2: #ff3d81;
  --success: #34e28a;
  --warning: #ffb84d;
  --danger: #ff5470;
  --sev-low: #ffd23f;
  --sev-mid: #ff8a3d;
  --sev-high: #e8283d;
  --die-empty-fill: #16202c;
  --die-empty-stroke: #2a3949;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  --radius: 14px;
  --font-ui: 'Inter', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'SFMono-Regular', Consolas, monospace;
  /* Categorical series palette for the Pareto chart's "Group by" stacked
     segments (up to 8 distinct values; a 9th folds into a shared "Other"
     segment in --text-muted rather than generating a color). Validated with
     the dataviz skill's palette validator against this app's actual dark
     chart surface (--bg-inset #0d131b): CVD-safe adjacent-pair separation
     and >=3:1 contrast on all 8 steps. Fixed hue order is the safety
     mechanism -- never reorder or cycle it per-chart. */
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
}

:root[data-theme="light"] {
  --bg: #eef2f7;
  --bg-elevated: #ffffff;
  --bg-panel: #ffffff;
  --bg-inset: #f4f7fb;
  --border: #d9e1ec;
  --border-soft: #e6ecf3;
  --text: #101826;
  --text-muted: #5b6b80;
  --accent: #0091a8;
  --accent-soft: rgba(0, 145, 168, 0.12);
  --accent-2: #d6266f;
  --success: #12925a;
  --warning: #b3730c;
  --danger: #d13456;
  --sev-low: #b3830c;
  --sev-mid: #d9660c;
  --sev-high: #c81e34;
  --die-empty-fill: #e6ecf3;
  --die-empty-stroke: #c7d3e0;
  --shadow: 0 16px 40px rgba(20, 30, 50, 0.12);
  /* Same 8 hues, light-mode steps -- validated against this app's actual
     light chart surface (--bg-inset #f4f7fb). Four steps sit below 3:1
     contrast by design (orange/aqua/yellow/magenta); the Pareto chart's
     legend and defect list always carry the value in text, so color is
     never the only way to read a segment (dataviz skill's "relief rule"). */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;
}

* { box-sizing: border-box; }
/* Author rules that set `display` on an element (e.g. `display: flex` for a
   flex container) otherwise beat the browser's built-in `[hidden] { display:
   none }` rule, since author-normal styles always win over user-agent-normal
   styles regardless of specificity. Without this, toggling `.hidden = true`
   in JS leaves the element visually laid out (and still catching pointer
   events) even though it's semantically hidden. */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  background-image:
    radial-gradient(circle at 15% 0%, var(--accent-soft), transparent 45%),
    repeating-linear-gradient(0deg, rgba(255,255,255,0.02) 0, rgba(255,255,255,0.02) 1px, transparent 1px, transparent 32px),
    repeating-linear-gradient(90deg, rgba(255,255,255,0.02) 0, rgba(255,255,255,0.02) 1px, transparent 1px, transparent 32px);
  transition: background-color 0.25s ease, color 0.25s ease;
}
:root[data-theme="light"] body {
  background-image:
    radial-gradient(circle at 15% 0%, var(--accent-soft), transparent 45%),
    repeating-linear-gradient(0deg, rgba(10,20,40,0.03) 0, rgba(10,20,40,0.03) 1px, transparent 1px, transparent 32px),
    repeating-linear-gradient(90deg, rgba(10,20,40,0.03) 0, rgba(10,20,40,0.03) 1px, transparent 1px, transparent 32px);
}

/* ===================== Shell ===================== */
.app-shell { min-height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, var(--bg-elevated), transparent);
  position: sticky; top: 0; z-index: 20;
  backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 50%;
  background: conic-gradient(from 90deg, var(--accent), var(--accent-2), var(--accent));
  box-shadow: 0 0 18px var(--accent-soft);
  position: relative;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 6px; border-radius: 50%; background: var(--bg);
}
.brand-text h1 { font-size: 16px; margin: 0; letter-spacing: 0.3px; }
.brand-sub { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 1px; }

.topbar-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg-panel); color: var(--text);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
  transition: border-color 0.2s ease, transform 0.15s ease;
}
.icon-btn:hover { border-color: var(--accent); transform: translateY(-1px); }
.mobile-only { display: none; }

.layout { flex: 1; display: grid; grid-template-columns: minmax(0, 300px) minmax(0, 1fr); gap: 20px; padding: 20px 24px; align-items: start; }

/* ===================== Sidebar ===================== */
.sidebar { display: flex; flex-direction: column; gap: 16px; position: sticky; top: 78px; min-width: 0; }
.panel {
  background: var(--bg-panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.panel-title {
  font-size: 12px; text-transform: uppercase; letter-spacing: 1.2px; color: var(--accent);
  margin: 0 0 12px; font-weight: 600;
}
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; margin-bottom: 12px; min-width: 0; }
.field:last-child { margin-bottom: 0; }
.field span { color: var(--text-muted); font-weight: 500; }
.field-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.label-with-btn { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.small-icon-btn { width: 22px; height: 22px; border-radius: 6px; padding: 0; }
.small-icon-btn[aria-pressed="false"] { color: var(--text-muted); border-color: var(--border-soft); opacity: 0.55; }
select, input[type="number"] {
  background: var(--bg-inset); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 10px; font-family: var(--font-mono); font-size: 13px;
  outline: none; transition: border-color 0.2s ease;
  width: 100%; min-width: 0;
}
select:focus, input[type="number"]:focus { border-color: var(--accent); }

.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  border: 1.5px dashed var(--border); border-radius: 10px; padding: 22px 12px;
  color: var(--text-muted); cursor: pointer; text-align: center; font-size: 13px;
  transition: border-color 0.2s ease, background 0.2s ease;
}
.dropzone:hover, .dropzone.dragover { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.col-list { margin: 6px 0; padding-left: 18px; }
.col-list code { font-family: var(--font-mono); color: var(--accent); }
.error-text { color: var(--danger); font-size: 12px; margin-top: 8px; }
.hint-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: rgba(255, 184, 77, 0.12); border: 1px solid var(--warning); border-radius: 8px;
  padding: 8px 10px; font-size: 11.5px; color: var(--text); margin-top: -4px; margin-bottom: 12px;
}
.hint-row .text-btn { flex-shrink: 0; padding: 4px 10px; color: var(--warning); border-color: var(--warning); }
.hint-row .text-btn:hover { color: var(--bg); background: var(--warning); }
code { font-family: var(--font-mono); }

.stats { display: flex; flex-direction: column; gap: 8px; margin: 0 0 12px; }
.stats > div { display: flex; justify-content: space-between; font-size: 13px; }
.stats dt { color: var(--text-muted); }
.stats dd { margin: 0; font-family: var(--font-mono); font-weight: 600; }
.legend { display: flex; gap: 8px; flex-wrap: wrap; }
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--text-muted); }
.legend-swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.excluded-swatch { background: var(--die-empty-fill); border: 1.5px dashed var(--text-muted); opacity: 0.7; }

/* ===================== Content ===================== */
.content { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
@media (min-width: 1200px) {
  .content { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
}
/* Between 1200-1599px the two maps are already side by side, leaving each
   die-stage too narrow to also fit its SVG and defect list side by side --
   stack them there; wide screens (>=1600px) have room for both. */
@media (min-width: 1200px) and (max-width: 1599px) {
  .die-stage, .pareto-stage { grid-template-columns: 1fr; }
  .die-defect-list, .pareto-list { max-height: 280px; }
}
.stage-card {
  background: var(--bg-panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px; min-width: 0;
}
.stage-header { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.stage-hint { margin: -6px 0 12px; }
.stage-header h2 { margin: 0; font-size: 15px; }
.text-btn {
  background: none; border: 1px solid var(--border); color: var(--text-muted);
  border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 12px;
}
.text-btn:hover { color: var(--danger); border-color: var(--danger); }

.gds-calibration { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.die-toolbar { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; margin-bottom: 12px; }
.calib-input {
  width: 70px; background: var(--bg-inset); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 8px; font-family: var(--font-mono); font-size: 12px; outline: none;
}
.calib-input:focus { border-color: var(--accent); }
.calib-select { width: auto; min-width: 68px; cursor: pointer; }
.calib-color {
  width: 32px; height: 30px; padding: 2px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--bg-inset); cursor: pointer;
}

/* ===================== Searchable combobox ===================== */
/* A plain <select> gets slow to scroll through once a CSV has many columns
   -- this pairs a text input (type to filter) with a floating option list,
   for the Pareto/Trend column pickers. */
.combo { position: relative; display: inline-block; }
.combo-input { width: 160px; padding-right: 24px; cursor: text; }
.combo-input:disabled { cursor: not-allowed; opacity: 0.6; }
.combo::after {
  content: ""; position: absolute; right: 9px; top: 50%; width: 7px; height: 7px;
  border-right: 1.5px solid var(--text-muted); border-bottom: 1.5px solid var(--text-muted);
  transform: translateY(-65%) rotate(45deg); pointer-events: none;
}
.combo-list {
  position: absolute; top: calc(100% + 4px); left: 0; min-width: 100%; width: max-content; max-width: 320px;
  max-height: 220px; overflow-y: auto; background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: var(--shadow); z-index: 50; font-family: var(--font-mono); font-size: 12px;
}
.combo-option {
  padding: 7px 10px; cursor: pointer; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.combo-option:hover, .combo-option.active { background: var(--accent-soft); }
.combo-option.selected { color: var(--accent); font-weight: 600; }
.combo-empty { padding: 7px 10px; color: var(--text-muted); font-style: italic; }

.wafer-stage { position: relative; display: flex; justify-content: center; cursor: default; user-select: none; }
.selection-box {
  position: fixed; border: 1.5px dashed var(--accent); background: var(--accent-soft);
  pointer-events: none; z-index: 60;
}
#waferSvg { width: 100%; max-width: 640px; height: auto; }
.empty-state {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  text-align: center; color: var(--text-muted); font-size: 13px; padding: 20px; margin: 0;
}

.die-stage, .pareto-stage { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 260px); gap: 16px; align-items: start; }
.die-svg-wrap, .pareto-svg-wrap, .trend-svg-wrap { position: relative; }
#dieSvg, #paretoSvg, #trendSvg { width: 100%; height: auto; background: var(--bg-inset); border-radius: 10px; border: 1px solid var(--border-soft); }
.die-defect-list, .pareto-list { max-height: 480px; overflow: auto; display: flex; flex-direction: column; gap: 8px; }
.defect-row, .pareto-row {
  border: 1px solid var(--border-soft); border-radius: 8px; padding: 8px 10px; font-size: 11.5px;
  background: var(--bg-inset); font-family: var(--font-mono);
}
.defect-row div { display: flex; justify-content: space-between; gap: 8px; }
.defect-row span.k { color: var(--text-muted); }
.pareto-row-head, .pareto-row-sub { display: flex; justify-content: space-between; gap: 8px; }
.pareto-row-sub { color: var(--text-muted); margin-top: 2px; }
.pareto-row.vital { border-color: var(--accent-2); }
.pareto-row-groups { display: flex; flex-wrap: wrap; gap: 6px 10px; margin-top: 6px; padding-top: 6px; border-top: 1px dashed var(--border-soft); }
.pareto-row-group { display: flex; align-items: center; gap: 5px; color: var(--text-muted); }
/* Legend: the dependable identity channel for a grouped Pareto's >=2 series
   -- text carries the value (never the colored text itself), a swatch
   beside it carries the color identity. Lives inside .pareto-svg-wrap so it
   travels into Full View along with the chart. */
.pareto-legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 11.5px; color: var(--text-muted); margin-bottom: 8px; }
.pareto-legend-item { display: flex; align-items: center; gap: 6px; }
.pareto-legend-swatch { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; display: inline-block; }
.pareto-bar { cursor: pointer; transition: filter 0.15s ease; }
.pareto-bar:hover { filter: brightness(1.3); }
.pareto-line, .trend-line { fill: none; stroke-width: 2; }
.pareto-point, .trend-point { cursor: pointer; }
.pareto-point:hover, .trend-point:hover { filter: brightness(1.3); }
.pareto-cutoff-line { stroke: var(--warning); }
.pareto-cutoff-label { fill: var(--warning); }

/* die / defect svg elements */
.die-rect { stroke-width: 1; cursor: pointer; transition: filter 0.15s ease, stroke-width 0.15s ease; }
.die-rect:hover { filter: brightness(1.35); stroke-width: 2; stroke: var(--accent); }
.die-rect.selected { stroke: var(--accent-2); stroke-width: 2.5; }
.die-rect.excluded { opacity: 0.4; stroke-dasharray: 1.5 1.5; }
.wafer-circle { fill: none; stroke: var(--border); stroke-width: 2; }
.exclusion-ring { fill: none; stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 5 4; opacity: 0.7; }
.notch-mark { fill: var(--bg); stroke: var(--border); stroke-width: 1.5; }
.defect-dot { fill: var(--accent-2); stroke: var(--bg); stroke-width: 1; cursor: pointer; transition: r 0.1s ease; }
.defect-dot:hover { filter: brightness(1.4); r: 7; }
.defect-dot.highlighted { fill: var(--accent-2); stroke: var(--bg); stroke-width: 1.5; opacity: 1; }
.defect-dot.dimmed { fill: var(--text-muted); stroke: none; opacity: 0.4; }
.defect-dot.dimmed:hover { opacity: 0.85; }
.die-outline { fill: var(--bg-inset); stroke: var(--border-soft); stroke-width: 1.5; }
.axis-line { stroke: var(--border-soft); stroke-width: 1; stroke-dasharray: 4 4; }
.axis-label { fill: var(--text-muted); font-family: var(--font-mono); font-size: 10px; }
.tick-mark { stroke: var(--border-soft); stroke-width: 1; }
.grid-line { stroke: var(--border-soft); stroke-width: 0.75; opacity: 0.5; }
.tick-label { font-size: 9px; }
#dieSvg.hide-coord-numbers .tick-label { display: none; }

/* ===================== Tooltip ===================== */
.tooltip {
  position: fixed; z-index: 100; pointer-events: none;
  background: var(--bg-elevated); border: 1px solid var(--accent); border-radius: 8px;
  padding: 8px 10px; font-size: 12px; font-family: var(--font-mono); color: var(--text);
  box-shadow: var(--shadow); max-width: 260px; white-space: pre-line;
}

.app-footer { display: flex; flex-direction: column; gap: 4px; text-align: center; padding: 14px; border-top: 1px solid var(--border); }

/* ===================== Modals ===================== */
.modal-overlay {
  position: fixed; inset: 0; z-index: 200; background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal-content {
  background: var(--bg-panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); width: 100%; max-width: 560px; max-height: 90vh;
  display: flex; flex-direction: column; overflow: hidden;
}
.modal-content-wide { max-width: 1400px; }
.modal-header {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 16px 20px; border-bottom: 1px solid var(--border-soft); flex-shrink: 0;
}
.modal-header h2 { margin: 0; font-size: 15px; }
.modal-body { padding: 20px; overflow: auto; }
.modal-body h3 { font-size: 13px; color: var(--accent); margin: 20px 0 8px; text-transform: uppercase; letter-spacing: 1px; }
.modal-body h3:first-child { margin-top: 0; }
.guide-list { padding-left: 20px; margin: 0; display: flex; flex-direction: column; gap: 8px; color: var(--text-muted); }
.guide-list strong { color: var(--text); }
#fullViewBody .die-stage, #fullViewBody .pareto-stage { grid-template-columns: minmax(0, 1fr) minmax(0, 300px); }
#fullViewBody #dieSvg, #fullViewBody #waferSvg, #fullViewBody #paretoSvg, #fullViewBody #trendSvg { max-height: 80vh; }
/* #waferSvg is capped to 640px normally so it doesn't dominate the page at
   its usual in-line size -- that cap defeats the purpose of full view, so
   it's lifted here. The other plots' SVGs have no such cap to begin with. */
#fullViewBody #waferSvg { max-width: none; }

/* ===================== Responsive ===================== */
.sidebar-backdrop { display: none; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .mobile-only { display: flex; }
  .sidebar {
    position: fixed; top: 0; left: 0; height: 100vh; width: 280px; z-index: 40;
    transform: translateX(-100%); transition: transform 0.25s ease; overflow-y: auto;
    padding: 78px 16px 16px; background: var(--bg);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop.open {
    display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 30;
  }
  .die-stage, .pareto-stage { grid-template-columns: 1fr; }
  .die-defect-list, .pareto-list { max-height: 240px; }
}

@media (max-width: 520px) {
  .topbar { padding: 12px 14px; }
  .layout { padding: 14px; }
  .field-row { grid-template-columns: 1fr; }
}
