:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --ink: #0f172a;
  --muted: #475569;
  --border: #e2e8f0;
  --primary: #0d9488;
  --primary-hover: #0f766e;
  --danger: #dc2626;
  --amber: #d97706;
  --green: #059669;
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  overflow-x: clip;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
}

.hidden { display: none !important; }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.txt-red { color: var(--danger); }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* Login */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  width: 100%;
  max-width: 390px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.06);
}
.brand-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--primary);
  background: #ccfbf1;
  padding: 4px 10px;
  border-radius: 999px;
  margin-bottom: 10px;
}
.login-card h1 { margin: 0 0 6px; font-size: 22px; }

/* Topbar & Tabs */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.topbar-left { display: flex; align-items: center; gap: 10px; }
.brand-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--primary); }
.topbar-title { font-weight: 700; font-size: 16px; }
.topbar-sub { font-size: 12px; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.tabs {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  display: flex;
  gap: 4px;
  overflow-x: auto;
}
.tab {
  border: none;
  background: transparent;
  padding: 12px 14px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  cursor: pointer;
  white-space: nowrap;
}
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab:hover { color: var(--ink); }

.container { max-width: 1280px; margin: 0 auto; padding: 20px 16px 48px; }

/* KPI & Panels */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.kpi-card, .panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
  max-width: 100%;
}
.kpi-head { display: flex; align-items: center; justify-content: space-between; gap: 6px; flex-wrap: wrap; }
.kpi-label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.kpi-val { font-size: 24px; font-weight: 700; margin: 4px 0; }
.kpi-sub { font-size: 12px; color: var(--muted); }
.delta-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  white-space: nowrap;
}
.delta-badge.up { background: #d1fae5; color: #065f46; }
.delta-badge.down { background: #fee2e2; color: #991b1b; }
.delta-badge.neutral { background: #f1f5f9; color: #334155; }
.mini-track { height: 5px; background: #e2e8f0; border-radius: 999px; overflow: hidden; margin: 5px 0 6px; }
.mini-fill { height: 100%; background: var(--primary); border-radius: 999px; transition: width 240ms ease; }

/* Overview Analytics & Statistics */
.analytics-hero {
  display: grid;
  grid-template-columns: 1.55fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}
.analytics-hero > *, .stats-grid > * { min-width: 0; }
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1.1fr 1.25fr;
  gap: 16px;
  margin-bottom: 16px;
}
.ops-row { margin-top: 0; }
.chart-headline { font-size: 22px; font-weight: 700; color: var(--ink); margin-top: 2px; }
.chart-legend { display: flex; align-items: center; gap: 12px; font-size: 12px; color: var(--muted); font-weight: 600; }
.leg-item { display: inline-flex; align-items: center; gap: 5px; }
.leg-dot { width: 9px; height: 9px; border-radius: 3px; display: inline-block; }
.chart-wrap { width: 100%; overflow: hidden; }
.chart-svg { width: 100%; height: auto; display: block; }
.chart-empty-note { font-size: 12px; color: var(--muted); text-align: center; margin-top: 6px; }

.ledger-list { display: flex; flex-direction: column; gap: 8px; font-size: 13.5px; }
.ledger-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  padding-bottom: 7px;
  border-bottom: 1px dashed var(--border);
}
.ledger-row.total {
  border-top: 1.5px solid var(--ink);
  border-bottom: none;
  padding-top: 8px;
  padding-bottom: 2px;
  font-size: 15px;
  font-weight: 700;
}
.ledger-row.sub-total { font-weight: 600; }
.ledger-neg { color: var(--muted); }
.ledger-pos { color: var(--green); }
.ledger-footnote {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}

.donut-layout { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.donut-svg { width: 124px; height: 124px; flex: none; }
.cat-legend { flex: 1; min-width: 140px; display: flex; flex-direction: column; gap: 10px; }
.cat-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 13px; }
.cat-row-sub { font-size: 11.5px; color: var(--muted); margin-top: 1px; }

.bar-rank-list { display: flex; flex-direction: column; gap: 10px; }
.bar-rank-item { display: flex; flex-direction: column; gap: 4px; }
.bar-rank-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 13px; }
.bar-rank-name { font-weight: 600; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-rank-val { font-weight: 700; white-space: nowrap; }
.bar-rank-track { height: 7px; background: #f1f5f9; border-radius: 999px; overflow: hidden; }
.bar-rank-fill { height: 100%; background: var(--primary); border-radius: 999px; }
.bar-rank-meta { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--muted); }

.two-col { display: grid; grid-template-columns: 1.25fr 1fr; gap: 16px; }
.two-col > * { min-width: 0; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.panel-head h2, .panel-head h3 { margin: 0; font-size: 16px; }

.telemetry-list { display: flex; flex-direction: column; gap: 7px; font-size: 13px; }
.tel-row { display: flex; justify-content: space-between; gap: 10px; border-bottom: 1px dashed var(--border); padding-bottom: 5px; flex-wrap: wrap; word-break: break-word; }
.tel-row:last-child { border-bottom: none; padding-bottom: 0; }

.toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.toolbar h2 { margin: 0 0 2px; font-size: 18px; }
.toolbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.search-input {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  min-width: 240px;
  font-size: 14px;
}

.seg-group {
  display: inline-flex;
  background: #e2e8f0;
  padding: 3px;
  border-radius: 9px;
  gap: 3px;
}
.period-btn {
  background: transparent;
  color: var(--muted);
  border: none;
}
.period-btn.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}
.custom-range-bar {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 14px;
}
.cr-field { display: flex; flex-direction: column; gap: 3px; font-size: 12px; font-weight: 600; color: var(--muted); }
.cr-field input {
  padding: 6px 9px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13px;
  color: var(--ink);
}

/* Trays & Slots */
.tray-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 16px;
}
.tray-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  flex-wrap: wrap;
  gap: 8px;
}
.tray-title { font-weight: 700; font-size: 15px; }
.slot-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 10px;
}
.slot-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: #fff;
}
.slot-card.empty { border-color: #fca5a5; background: #fef2f2; }
.slot-card.low { border-color: #fcd34d; background: #fffbeb; }
.slot-card.disabled { opacity: 0.55; }
.slot-top { display: flex; align-items: center; justify-content: space-between; }
.slot-num { font-weight: 700; font-size: 13px; background: #f1f5f9; padding: 2px 7px; border-radius: 5px; }
.slot-img { width: 52px; height: 52px; object-fit: contain; align-self: center; }
.slot-img-placeholder { width: 52px; height: 52px; display: flex; align-items: center; justify-content: center; color: var(--muted); align-self: center; }
.slot-name { font-size: 12px; font-weight: 600; min-height: 32px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.slot-price { font-size: 12px; color: var(--muted); }
.qty-row { display: flex; align-items: center; justify-content: space-between; gap: 4px; margin-top: 2px; }
.qty-btn {
  width: 30px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #f8fafc;
  font-weight: 700;
  cursor: pointer;
}
.qty-btn:hover:not(:disabled) { background: #e2e8f0; }
.qty-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.qty-badge { font-weight: 700; font-size: 13px; }
.qty-badge.zero { color: var(--danger); }
.qty-badge.low { color: var(--amber); }
.qty-badge.ok { color: var(--green); }

/* Tables */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); text-align: left; }
.data-table th { background: #f8fafc; font-weight: 600; color: var(--muted); font-size: 12px; white-space: nowrap; }
.prod-cell { display: flex; align-items: center; gap: 10px; }
.prod-thumb { width: 34px; height: 34px; object-fit: contain; border-radius: 4px; border: 1px solid var(--border); background: #fff; }

/* Buttons, Pills, Forms */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  text-decoration: none;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-full { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: #f1f5f9; color: var(--ink); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: #e2e8f0; }
.btn-ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { color: var(--ink); background: #f8fafc; }
.btn-danger { background: var(--danger); color: #fff; }

.pill {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  text-transform: uppercase;
  white-space: nowrap;
}
.pill-green { background: #d1fae5; color: #065f46; }
.pill-amber { background: #fef3c7; color: #92400e; }
.pill-red { background: #fee2e2; color: #991b1b; }
.pill-slate { background: #e2e8f0; color: #334155; }

.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; }
.field input, .field select {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-weight: 400;
}
.check-field { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }

.alert { padding: 10px 12px; border-radius: 8px; font-size: 13px; }
.alert-error { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; font-weight: 600; }
.alert-warn { background: #fef3c7; color: #92400e; border: 1px solid #fde68a; word-break: break-all; }

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--ink);
  color: #fff;
  padding: 11px 16px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 500;
  z-index: 90;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
}

.audit-list { display: flex; flex-direction: column; gap: 8px; max-height: 300px; overflow-y: auto; }
.audit-item { font-size: 12px; border-bottom: 1px solid var(--border); padding-bottom: 7px; }
.audit-meta { color: var(--muted); font-size: 11px; }

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 80;
}
.modal-card {
  background: var(--surface);
  border-radius: 12px;
  padding: 20px;
  width: 100%;
  max-width: 460px;
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.18);
  max-height: 92vh;
  overflow-y: auto;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

@media (max-width: 960px) {
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .analytics-hero { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr; }
  .two-col { grid-template-columns: 1fr; }
  .slot-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 520px) {
  .topbar { padding: 10px 12px; }
  .container { padding: 14px 10px 36px; }
  .kpi-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi-val { font-size: 20px; }
  .slot-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .row-2 { grid-template-columns: 1fr; }
  .search-input { width: 100%; min-width: 0; }
  .seg-group { flex-wrap: wrap; max-width: 100%; }
  .toolbar-actions { width: 100%; }
  .data-table th, .data-table td { padding: 8px 9px; font-size: 12px; }
}

/* Biometric WebAuthn & PWA */
.bio-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 14px 0 12px;
  color: var(--muted);
  font-size: 12px;
}
.bio-divider::before,
.bio-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}
.btn-bio {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 42px;
  background: #f0fdfa;
  color: #0f766e;
  border: 1.5px solid #5eead4;
  font-weight: 600;
}
.btn-bio:hover:not(:disabled) {
  background: #ccfbf1;
  border-color: #14b8a6;
}
.bio-device-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}
.bio-device-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f8fafc;
}
.bio-device-meta {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}
.pwa-box {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  background: #f8fafc;
  border: 1px dashed #cbd5e1;
}
