:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f1f3f5;
  --surface-3: #e9ebee;
  --border: #e5e7eb;
  --border-strong: #d7dbe0;
  --amber: #f59e0b;
  --amber-dark: #b45309;
  --amber-soft: rgba(245, 158, 11, 0.12);
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --blue-soft: rgba(37, 99, 235, 0.1);
  --green: #15803d;
  --green-soft: rgba(21, 128, 61, 0.1);
  --red: #dc2626;
  --red-soft: rgba(220, 38, 38, 0.1);
  --text: #14181d;
  --text-muted: #667085;
  --text-faint: #98a2b3;
  --sans: "Inter", "Segoe UI", -apple-system, Roboto, Arial, sans-serif;
  --mono: "JetBrains Mono", "Cascadia Mono", Consolas, "SFMono-Regular", Menlo, monospace;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.06), 0 1px 2px rgba(16, 24, 40, 0.04);
  --radius: 14px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 32px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.2px;
  color: var(--text);
}

.portRow {
  display: flex;
  align-items: center;
  gap: 8px;
}

.navLink {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 14px;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: none;
}

.navLink:hover {
  color: var(--text);
  background: var(--surface-2);
}

select, input[type="text"], input[type="date"], input[type="number"], button {
  font-family: var(--sans);
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 9px 14px;
}

button {
  cursor: pointer;
  font-weight: 600;
  transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease, transform 0.05s ease;
}

button:hover:not(:disabled) {
  border-color: var(--text-faint);
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#connectBtn {
  background: var(--text);
  color: #fff;
  border-color: var(--text);
  box-shadow: var(--shadow-sm);
}

#connectBtn:hover:not(:disabled) {
  background: #000;
  border-color: #000;
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
}

.dot-off { background: var(--border-strong); }
.dot-on { background: var(--green); box-shadow: 0 0 0 3px var(--green-soft); }

.panel {
  max-width: 1360px;
  margin: 0 auto;
  padding: 36px 32px 72px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

/* Station page: wide two-column layout - live readout/controls on the
   left, barcode + history on the right, instead of one narrow stacked
   column with wasted space either side on real monitors. */
.stationGrid {
  display: grid;
  grid-template-columns: minmax(380px, 480px) minmax(0, 1fr);
  gap: 28px;
  align-items: start;
}

.stationCol {
  display: flex;
  flex-direction: column;
  gap: 28px;
  min-width: 0;
}

@media (max-width: 980px) {
  .stationGrid {
    grid-template-columns: minmax(0, 1fr);
  }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 24px;
}

.readoutSection {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.readoutWindow {
  width: 100%;
  background: linear-gradient(180deg, #1a1d21 0%, #0f1114 100%);
  border-radius: var(--radius-sm);
  padding: 32px 20px;
  text-align: center;
  box-shadow: var(--shadow-md);
}

#readoutValue {
  font-family: var(--mono);
  font-size: 60px;
  font-weight: 600;
  color: var(--amber);
  letter-spacing: 1px;
}

.unit {
  font-family: var(--mono);
  font-size: 20px;
  color: #8a9199;
  margin-left: 10px;
}

.badge {
  padding: 7px 20px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 14px;
}

.badge-idle { background: var(--surface-2); color: var(--text-muted); }
.badge-testing {
  background: var(--amber-soft);
  color: var(--amber-dark);
  animation: pulse 1.2s ease-in-out infinite;
}
.badge-pass { background: var(--green-soft); color: var(--green); }
.badge-fail { background: var(--red-soft); color: var(--red); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.stopwatch {
  font-family: var(--mono);
  font-size: 20px;
  font-weight: 600;
  color: var(--text-faint);
  letter-spacing: 1px;
}

.stopwatch.stopwatch-running {
  color: var(--amber-dark);
}

.scanSection label {
  display: block;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 8px;
  font-size: 13px;
}

.scanSection .secondLabel {
  margin-top: 18px;
}

#batchInput {
  width: 100%;
  font-family: var(--mono);
  font-size: 15px;
  padding: 11px 14px;
  background: var(--surface-2);
}

.scanRow {
  display: flex;
  gap: 10px;
}

#partInput {
  flex: 1;
  font-family: var(--mono);
  font-size: 16px;
  padding: 12px 14px;
  background: var(--surface-2);
}

#partInput:focus, #batchInput:focus {
  outline: none;
  border-color: var(--amber);
  background: var(--surface);
}

.toggleBtn {
  min-width: 130px;
  box-shadow: var(--shadow-sm);
}

.toggleBtn-start:not(:disabled) {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}

.toggleBtn-start:hover:not(:disabled) {
  background: var(--blue-dark);
  border-color: var(--blue-dark);
}

.toggleBtn-stop:not(:disabled) {
  background: var(--red);
  color: #fff;
  border-color: var(--red);
}

.hint {
  margin-top: 12px;
  font-size: 13px;
  font-family: var(--mono);
  color: var(--amber-dark);
  min-height: 16px;
}

.barcodeSection, .historySection {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 24px;
}

.barcodeCard {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#barcodeSvg {
  max-width: 100%;
}

.barcodeLabel {
  font-family: var(--sans);
  font-weight: 600;
  font-size: 15px;
  color: var(--text-muted);
  text-align: center;
}

.barcodeLabel.ok { color: var(--green); }
.barcodeLabel.fail { color: var(--red); }

.historyHeader {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 16px;
}

.historyHeader h2 {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  margin: 0;
}

.summaryLine {
  font-family: var(--mono);
  font-size: 13px;
  color: var(--text-muted);
}

.historyTableWrap {
  max-height: 420px;
  overflow-y: auto;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.historyTable {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--mono);
  font-size: 13px;
}

.historyTable th {
  position: sticky;
  top: 0;
  text-align: left;
  color: var(--text-muted);
  font-weight: 600;
  padding: 10px 12px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.historyTable td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.historyTable tr:hover td {
  background: var(--surface-2);
}

.row-pass td:last-child { color: var(--green); font-weight: 600; }
.row-fail td:last-child { color: var(--red); font-weight: 600; }

/* ---- Dashboard ---- */
.dashboardPanel {
  max-width: 1520px;
}

.filterBar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 16px 20px;
}

.filterPresets {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.filterBtn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 7px 16px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.filterBtn:hover { border-color: var(--text-faint); color: var(--text); }
.filterBtn.active {
  background: var(--amber-soft);
  border-color: var(--amber);
  color: var(--amber-dark);
}

.filterCustom {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.filterCustom input[type="date"] {
  font-family: var(--sans);
  font-size: 13px;
  padding: 7px 10px;
  background: var(--surface-2);
}

.filterCustom span { color: var(--text-muted); font-size: 13px; }

.exportBtn {
  background: var(--green);
  color: #fff;
  border-color: var(--green);
  box-shadow: var(--shadow-sm);
}

.exportBtn:hover:not(:disabled) {
  background: #0f5c2c;
  border-color: #0f5c2c;
}

.statGrid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}

.statTile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 20px 22px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.statTile .statLabel {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.statTile .statValue {
  font-family: var(--mono);
  font-size: 30px;
  font-weight: 600;
  color: var(--text);
}

.statTile.stat-pass .statValue { color: var(--green); }
.statTile.stat-fail .statValue { color: var(--red); }
.statTile.stat-rate .statValue { color: var(--amber-dark); }
.statTile.stat-fastest .statValue { color: var(--green); font-size: 21px; }

.chartGrid {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  gap: 18px;
}

.chartGrid.chartGrid-even {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}

@media (max-width: 900px) {
  .chartGrid, .chartGrid.chartGrid-even { grid-template-columns: minmax(0, 1fr); }
  .filterCustom { margin-left: 0; }
}

.chartCard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 22px;
}

.chartCard h3 {
  margin: 0 0 16px;
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}

.chartCard canvas {
  max-height: 280px;
}

.emptyNote {
  color: var(--text-muted);
  font-size: 13px;
  text-align: center;
  padding: 24px 0;
}

/* ---- Mobile (phone-width) ---- */
@media (max-width: 480px) {
  .topbar {
    flex-wrap: wrap;
    gap: 10px;
    padding: 14px 18px;
  }

  .portRow {
    flex-wrap: wrap;
    justify-content: flex-end;
    row-gap: 8px;
  }

  .panel {
    padding: 22px 16px 48px;
    gap: 20px;
  }

  .card, .barcodeSection, .historySection, .chartCard, .statTile, .filterBar {
    padding: 18px;
  }

  #readoutValue {
    font-size: 44px;
  }

  .unit {
    font-size: 16px;
  }

  .scanRow {
    flex-direction: column;
  }

  .toggleBtn {
    width: 100%;
  }

  .statGrid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .filterCustom {
    width: 100%;
  }

  .filterCustom input[type="date"] {
    flex: 1;
    min-width: 0;
  }

  .exportBtn {
    width: 100%;
  }
}

/* ---- Settings modal ---- */
.modalOverlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 29, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.modalOverlay[hidden] {
  display: none;
}

.modalCard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 28px;
  width: 100%;
  max-width: 380px;
}

.modalCard h2 {
  margin: 0 0 18px;
  font-size: 18px;
  font-weight: 700;
}

.modalCard label {
  display: block;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 8px;
  font-size: 13px;
}

.modalCard label.secondLabel {
  margin-top: 16px;
}

.modalCard input[type="number"] {
  width: 100%;
  font-family: var(--mono);
  font-size: 15px;
  padding: 10px 14px;
  background: var(--surface-2);
}

.modalCard input[type="number"]:focus {
  outline: none;
  border-color: var(--amber);
  background: var(--surface);
}

.settingsError {
  margin-top: 14px;
  font-size: 13px;
  color: var(--red);
}

.modalActions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 22px;
}

/* ---- Scan page ---- */
.scanPanel {
  max-width: 520px;
}

.cameraSection {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.cameraFrame {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #14181d;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-md);
}

.cameraFrame video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cameraReticle {
  position: absolute;
  inset: 15% 10%;
  border: 2px solid var(--amber);
  border-radius: var(--radius-sm);
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.manualSection label {
  display: block;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 8px;
  font-size: 13px;
}

#manualPartInput {
  width: 100%;
  font-family: var(--mono);
  font-size: 16px;
  padding: 12px 14px;
  background: var(--surface-2);
}

#manualPartInput:focus {
  outline: none;
  border-color: var(--amber);
  background: var(--surface);
}

.resultSection {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 24px;
}

.resultSection[hidden] {
  display: none;
}

.resultBadge {
  font-size: 20px;
  padding: 10px 28px;
}

.resultPart {
  font-family: var(--mono);
  font-size: 20px;
  font-weight: 600;
  margin-top: 8px;
}

.resultMeta {
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 10px;
}

.resultSection .historyHeader {
  width: 100%;
  margin-top: 10px;
}

.resultSection .historyTableWrap {
  width: 100%;
}
