:root {
  --sidebar-width: 240px;
  --bg: #0c0f1a;
  --surface: #111827;
  --surface-2: #1a2234;
  --surface-3: #212d42;
  --border: #252f45;
  --text: #e8eaf3;
  --muted: #8892ab;
  --accent: #6366f1;
  --accent-hover: #4f52d9;
  --accent-light: rgba(99,102,241,0.12);
  --success: #22c55e;
  --success-light: rgba(34,197,94,0.12);
  --error: #f43f5e;
  --error-light: rgba(244,63,94,0.12);
  --warning: #f59e0b;
  --warning-light: rgba(245,158,11,0.12);
  --sidebar-bg: #0f1623;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }

/* ─── App Shell ─────────────────────────────────────── */
.app-shell {
  display: flex;
  min-height: 100vh;
}

/* ─── Sidebar ───────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  position: fixed;
  top: 0; left: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  z-index: 100;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 16px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-logo {
  width: 32px;
  height: 32px;
  object-fit: contain;
  border-radius: 6px;
}

.sidebar-logo-placeholder {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  color: white;
  flex-shrink: 0;
}

.sidebar-site-name {
  font-weight: 600;
  font-size: 13.5px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-nav {
  flex: 1;
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar-section {
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 14px 8px 6px;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  border-radius: 7px;
  color: var(--muted);
  font-size: 13.5px;
  font-weight: 500;
  transition: background 0.12s, color 0.12s;
  cursor: pointer;
  text-decoration: none;
}

.sidebar-link:hover {
  background: var(--surface-2);
  color: var(--text);
}

.sidebar-link.active {
  background: var(--accent-light);
  color: var(--accent);
}

.sidebar-link svg {
  flex-shrink: 0;
  opacity: 0.8;
}

.sidebar-link.active svg { opacity: 1; }

.sidebar-footer {
  padding: 12px 10px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 8px 10px;
}

.sidebar-avatar {
  width: 30px;
  height: 30px;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  flex-shrink: 0;
}

.sidebar-user-info { display: flex; flex-direction: column; min-width: 0; }
.sidebar-username { font-size: 13px; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-role { font-size: 11px; color: var(--muted); }

.sidebar-logout {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 7px;
  padding: 7px;
  font-size: 12.5px;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}

.sidebar-logout:hover {
  background: var(--error-light);
  border-color: var(--error);
  color: var(--error);
}

/* ─── Main Content ──────────────────────────────────── */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  padding: 36px 40px;
  min-height: 100vh;
  max-width: calc(100vw - var(--sidebar-width));
}

.page-header {
  margin-bottom: 28px;
}

.page-header h1 {
  font-size: 1.5em;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
}

.page-header p {
  color: var(--muted);
  font-size: 0.9em;
}

/* ─── Cards ─────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 20px;
}

.card h2 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

.card > .muted {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 16px;
  display: block;
}

/* ─── Stats Grid (Dashboard) ────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat-card-label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.stat-card-value { font-size: 2em; font-weight: 700; color: var(--text); line-height: 1; }
.stat-card-sub { font-size: 12px; color: var(--muted); }
.stat-card.accent { border-color: var(--accent); background: var(--accent-light); }
.stat-card.accent .stat-card-value { color: var(--accent); }
/* Live-status cards keep the "accent" class throughout, so these need the
   same .stat-card-value descendant specificity as the rule above (not just
   a class on the value itself) to actually win once a check resolves —
   and declared after it, so source order settles anything still tied. */
.stat-card.status-online { border-color: var(--success); background: var(--success-light); }
.stat-card.status-online .stat-card-value { color: var(--success); }
.stat-card.status-offline { border-color: var(--error); background: var(--error-light); }
.stat-card.status-offline .stat-card-value { color: var(--error); }

/* ─── Forms ─────────────────────────────────────────── */
.grid-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.grid-form label,
.stack-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}

.grid-form .span-2 { grid-column: 1 / -1; }
.grid-form .button-row,
.grid-form .result-line { grid-column: 1 / -1; }

.checkbox-label { flex-direction: row !important; align-items: center; gap: 8px !important; }

.stack-form { display: flex; flex-direction: column; gap: 12px; }

input[type="text"],
input[type="password"],
input[type="number"],
input[type="url"],
input[type="email"],
input[type="color"],
select,
textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 13.5px;
  font-family: inherit;
  transition: border-color 0.15s;
  width: 100%;
}

input[type="text"]:focus,
input[type="password"]:focus,
input[type="number"]:focus,
input[type="url"]:focus,
input[type="email"]:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
}

input[type="color"] { padding: 4px 6px; height: 36px; cursor: pointer; }

textarea { resize: vertical; }

/* ─── Buttons ───────────────────────────────────────── */
.primary-button,
.secondary-button,
.danger-button {
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.12s, opacity 0.12s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.primary-button { background: var(--accent); color: white; }
.primary-button:hover { background: var(--accent-hover); color: white; }
.primary-button:disabled { opacity: 0.5; cursor: default; }

.secondary-button { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.secondary-button:hover { border-color: var(--accent); color: var(--accent); }
.secondary-button:disabled { opacity: 0.5; cursor: default; }

.danger-button { background: var(--error-light); color: var(--error); border: 1px solid transparent; }
.danger-button:hover { border-color: var(--error); }

.link-button { background: none; border: none; color: var(--accent); cursor: pointer; font-size: 13px; padding: 0; font-family: inherit; }
.link-button:hover { text-decoration: underline; }

.button-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 8px; }

/* ─── Alerts ────────────────────────────────────────── */
.alert {
  padding: 12px 16px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 13.5px;
  border: 1px solid;
}

.alert-error { background: var(--error-light); border-color: var(--error); color: var(--error); }
.alert-success { background: var(--success-light); border-color: var(--success); color: var(--success); }
.alert-warning { background: var(--warning-light); border-color: var(--warning); color: var(--warning); }
.alert-info { background: var(--accent-light); border-color: var(--accent); color: var(--accent); }

/* ─── Tables ────────────────────────────────────────── */
.data-table, .warehouse-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
}

.data-table th, .warehouse-table th,
.data-table td, .warehouse-table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.data-table th, .warehouse-table th {
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: var(--surface-2);
}

.data-table tr:hover td, .warehouse-table tr:hover td { background: var(--surface-2); }

.warehouse-table input[type="text"] { width: 100%; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11.5px;
  font-weight: 600;
}

.badge-green { background: var(--success-light); color: var(--success); }
.badge-red { background: var(--error-light); color: var(--error); }
.badge-blue { background: var(--accent-light); color: var(--accent); }
.badge-gray { background: var(--surface-3); color: var(--muted); }

/* ─── Permission matrix (Users page) ────────────────── */
.perms-row td { background: var(--surface-2); padding: 16px; }
.perm-matrix {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}
.perm-group { display: flex; flex-direction: column; gap: 6px; }
.perm-group-label { font-weight: 600; font-size: 12.5px; margin-bottom: 2px; }

/* ─── Preview Table (product test results) ──────────── */
.preview-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  display: block;
  overflow-x: auto;
  white-space: nowrap;
}

.preview-table th, .preview-table td {
  text-align: left;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
}

.preview-table th { color: var(--muted); font-weight: 600; background: var(--surface-2); }
.preview-table .row-error td { background: rgba(244,63,94,0.07); }

.error-detail { font-size: 11.5px; white-space: pre-wrap; margin: 4px 0 0; color: var(--muted); max-height: 100px; overflow-y: auto; }
.error-msg { color: var(--error); font-size: 12.5px; }

/* ─── Sync stats ────────────────────────────────────── */
.sync-stat-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.stat-chip { padding: 4px 12px; border-radius: 20px; font-size: 12.5px; font-weight: 600; }
.stat-total { background: var(--surface-2); color: var(--text); }
.stat-ok { background: var(--success-light); color: var(--success); }
.stat-error { background: var(--error-light); color: var(--error); }

/* ─── Active job progress ───────────────────────────── */
.job-row {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 12px;
  background: var(--surface-2);
}
.job-row-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; gap: 12px; flex-wrap: wrap; }
.job-row-title { font-weight: 600; font-size: 13.5px; }
.job-progress-track { background: var(--surface-3); border-radius: 20px; height: 8px; overflow: hidden; margin-bottom: 8px; }
.job-progress-fill { background: var(--accent); height: 100%; transition: width 0.3s ease; }
.job-progress-fill.job-progress-error { background: var(--error); }

/* ─── Scan grid ─────────────────────────────────────── */
.scan-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
  margin-top: 12px;
}

.scan-block {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
}

.scan-block .block-header {
  margin-bottom: 10px;
}

/* ─── Result lines ──────────────────────────────────── */
.result-line { min-height: 1.2em; font-size: 12.5px; color: var(--muted); }
.result-ok { color: var(--success); }
.result-error { color: var(--error); }

/* ─── Database block ────────────────────────────────── */
.db-block {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 24px;
  margin-bottom: 20px;
  background: var(--surface);
}

.db-block .block-header {
  padding: 16px 0;
}

.key-tag { color: var(--muted); font-weight: 400; font-size: 11.5px; }

.db-block .block-body { padding-bottom: 20px; }

/* ─── Collapsible blocks (toggled via inline onclick) ────────── */
[data-collapsible] .block-header {
  cursor: pointer;
  font-weight: 600;
  font-size: 13.5px;
  display: flex;
  align-items: center;
  gap: 8px;
  user-select: none;
}

[data-collapsible] .block-header::before {
  content: '▾';
  font-size: 11px;
  color: var(--muted);
  transition: transform 0.15s ease;
}

[data-collapsible].collapsed .block-header::before {
  transform: rotate(-90deg);
}

[data-collapsible].collapsed .block-body {
  display: none;
}

/* ─── Field checklist ───────────────────────────────── */
.field-checklist {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 8px;
  list-style: none;
  margin-top: 4px;
}

.field-checklist li {
  padding: 8px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12.5px;
}

/* ─── Test controls ─────────────────────────────────── */
.test-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
}

.inline-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 500;
}

.inline-label select,
.inline-label input[type="text"] {
  width: auto;
  padding: 6px 10px;
}

/* ─── Category / mapping table ──────────────────────── */
.cat-table, .mapping-table { width: 100%; border-collapse: collapse; margin-top: 12px; }
.cat-table th, .cat-table td,
.mapping-table th, .mapping-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.cat-table th, .mapping-table th { color: var(--muted); font-size: 12px; font-weight: 600; background: var(--surface-2); text-transform: uppercase; letter-spacing: 0.04em; }

/* ─── Stale boxes ───────────────────────────────────── */
.stale-box { margin-top: 12px; }
.stale-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }

/* ─── Auth pages ────────────────────────────────────── */
.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 40px;
  width: 360px;
}

.auth-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 24px;
}

.auth-logo-mark {
  width: 36px;
  height: 36px;
  background: var(--accent);
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 14px;
  color: white;
}

.auth-logo-name { font-size: 17px; font-weight: 700; }

.auth-card h1 { font-size: 20px; font-weight: 700; margin-bottom: 4px; }
.auth-subtitle { color: var(--muted); font-size: 13px; margin-bottom: 24px; }

.auth-field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; font-size: 13px; font-weight: 500; }
.auth-field input { background: var(--surface-2); }

.auth-submit { width: 100%; margin-top: 6px; padding: 10px; font-size: 14px; font-weight: 600; justify-content: center; }

/* ─── 2FA card ──────────────────────────────────────── */
.tfa-card { text-align: center; }
.tfa-card .qr { margin: 16px auto; display: block; border-radius: 8px; max-width: 180px; }
.tfa-secret { font-family: monospace; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 8px 14px; font-size: 13px; letter-spacing: 0.05em; margin: 8px 0 16px; display: inline-block; }

/* ─── Users table ───────────────────────────────────── */
.users-table { width: 100%; border-collapse: collapse; margin-top: 12px; }
.users-table th, .users-table td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); font-size: 13px; }
.users-table th { color: var(--muted); font-size: 12px; font-weight: 600; background: var(--surface-2); text-transform: uppercase; letter-spacing: 0.04em; }
.users-table tr:hover td { background: var(--surface-2); }

/* ─── Color swatch ──────────────────────────────────── */
.color-row { display: flex; align-items: center; gap: 10px; }
.color-row input[type="color"] { width: 48px; flex-shrink: 0; }

/* ─── Misc ──────────────────────────────────────────── */
.muted { color: var(--muted); }
code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 1px 6px; font-size: 12px; font-family: 'Menlo', 'Consolas', monospace; }
h1 { font-size: 1.4em; font-weight: 700; }
h2 { font-size: 1em; font-weight: 600; }
.inline-form { display: inline; }
.preview-wrap { margin-top: 10px; }
.add-save-button { width: 100%; justify-content: center; margin-top: 10px; }
