:root {
  --ink: #1c2430;
  --ink-soft: #5a6472;
  --line: #dfe4ea;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --accent: #1f6feb;
  --accent-soft: #e8f0fe;
  --green: #1a7f4b;
  --amber: #9a6700;
  --amber-soft: #fff3cd;
  --radius: 10px;
}

body.dark {
  --ink: #e2e8f0;
  --ink-soft: #94a3b8;
  --line: #334155;
  --bg: #0f172a;
  --panel: #1e293b;
  --accent: #3b82f6;
  --accent-soft: #1e3a5f;
  --green: #22c55e;
  --amber: #fbbf24;
  --amber-soft: #78350f;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  transition: background 0.2s ease, color 0.2s ease;
}

body.dark .topbar {
  background: #0f172a;
  border-bottom-color: var(--accent);
}

body.dark tbody tr:hover {
  background: var(--accent-soft);
}

body.dark .btn {
  background: var(--panel);
  border-color: var(--line);
  color: var(--ink);
}

body.dark .btn:hover {
  background: #2d3748;
}

body.dark input, body.dark select, body.dark textarea {
  background: var(--panel);
  border-color: var(--line);
  color: var(--ink);
}

body.dark th {
  background: #1a2332;
  color: var(--ink-soft);
}

body.dark .table-wrap {
  border-color: var(--line);
}

body.dark .toast {
  background: #1e293b;
  color: var(--ink);
}

.muted { color: var(--ink-soft); font-size: 13px; }
.hidden { display: none !important; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  background: #12202f;
  color: #fff;
  border-bottom: 3px solid var(--accent);
}
.topbar-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.brand h1 { margin: 0; font-size: 19px; letter-spacing: -0.2px; }
.brand .muted { color: #9fb0c2; margin: 2px 0 0; }
.nav { display: flex; align-items: center; gap: 16px; }
.nav a { color: #d7e2ee; text-decoration: none; font-size: 14px; }
.nav a:hover { color: #fff; }

.btn, .btn-primary, .btn-ghost {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  text-decoration: none;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #1a5fd0; }
.btn { background: #fff; border-color: var(--line); color: var(--ink); }
.btn:hover { background: #f0f2f5; }.btn-ghost { background: transparent; color: var(--ink-soft); }
.btn-ghost:hover { color: var(--ink); }
.btn[disabled] { opacity: 0.55; cursor: default; }

/* ---------------------------------------------------------------- auth bar */
.auth-bar { display: flex; align-items: center; gap: 12px; margin-left: auto; color: #d7e2ee; font-size: 13px; }
.auth-label { color: #9fb0c2; }
.auth-email { color: #fff; font-weight: 500; }
.auth-name { color: #9fb0c2; }
.auth-bar .btn-ghost { color: #9fb0c2; font-size: 13px; padding: 4px 8px; }
.auth-bar .btn-ghost:hover { color: #fff; }

/* ---------------------------------------------------------------- photo upload */
.photo-upload { display: flex; flex-direction: column; gap: 8px; }
.photo-upload-actions { display: flex; align-items: center; gap: 10px; }
.photo-upload-actions .btn { padding: 8px 14px; }
.photo-upload-actions .muted { font-size: 13px; }
.photo-upload-actions .muted.error { color: #f5a5a5; }

/* ---------------------------------------------------------------- BOM editor */
.bom-add { display: flex; gap: 10px; align-items: center; margin-top: 14px; flex-wrap: wrap; }
.bom-add input[type="search"] { flex: 1 1 260px; font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); }
body.dark .bom-add input[type="search"], body.dark .qty-input { background: #0f172a; border-color: var(--line); color: var(--ink); }
.bom-results { margin-top: 8px; display: grid; gap: 6px; }
.bom-result { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.bom-result .code { font-weight: 600; min-width: 64px; }
.bom-result .desc { flex: 1 1 auto; font-size: 13px; }
.qty-input { width: 68px; font: inherit; padding: 5px 7px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink); }

/* ---------------------------------------------------------------- login */
.login-wrap { display: flex; justify-content: center; padding: 40px 0 20px; }
.login-card { width: 100%; max-width: 380px; }
.login-card h2 { margin: 0 0 2px; font-size: 20px; }
.login-fields { display: grid; gap: 12px; margin: 18px 0 16px; }
.login-fields input { width: 100%; font: inherit; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); }
body.dark .login-fields input { background: #0f172a; border-color: var(--line); color: var(--ink); }
.login-msg { margin: 10px 0 0; padding: 8px 10px; border-radius: 8px; background: var(--amber-soft); color: var(--amber); font-size: 13px; }
.login-hint { margin: 14px 0 0; }
.login-card .btn-primary { width: 100%; justify-content: center; }

/* ---------------------------------------------------------------- BOM photos */
.bom-thumb-col { width: 64px; }
.bom-thumb { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); display: block; }
.bom-thumb-empty { display: flex; align-items: center; justify-content: center; color: var(--ink-soft); background: #eef1f5; font-size: 13px; }
body.dark .bom-thumb-empty { background: #334155; }
.bom-actions { white-space: nowrap; text-align: right; }
.unit-photo { max-width: 220px; max-height: 160px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); display: block; margin-bottom: 6px; }

/* ---------------------------------------------------------------- BOM unlink */
.btn-unlink-bom { color: var(--amber); font-size: 12px; padding: 4px 10px; }
.btn-unlink-bom:hover { color: #b42318; border-color: #f1b0a9; background: var(--amber-soft); }

.sorted-th-up::after { content: " ▲"; font-size: 10px; color: var(--accent); }
sorted-th-down::after { content: " ▼"; font-size: 10px; color: var(--accent); }
.sort-th { cursor: pointer; user-select: none; }
.sort-th:hover { color: var(--accent); }

/* ------------------------------------------------------------------- page */
.wrap { max-width: 1180px; margin: 22px auto 60px; padding: 0 20px; }

.toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.toolbar input[type="search"], .toolbar select, .toolbar button {
  font: inherit;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}
.toolbar input[type="search"] { flex: 1 1 280px; min-width: 200px; }
.toolbar select { flex: 0 0 auto; }
.toolbar button {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
  padding: 9px 12px;
  cursor: pointer;
  color: var(--ink);
}
body.dark .toolbar button {
  background: var(--panel);
  border-color: var(--line);
  color: var(--ink);
}

.table-wrap {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
th {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-soft);
  background: #fbfcfd;
  font-weight: 600;
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--accent-soft); }
.col-id { width: 92px; }
.col-barcode { width: 140px; }
.col-tick { width: 38px; }
.col-tick input { cursor: pointer; }
.col-cat { width: 190px; }
.col-power { width: 170px; }
.col-status { width: 130px; }

.unit-id { font-weight: 600; font-variant-numeric: tabular-nums; }
/* Barcodes are read digit by digit, so keep them monospaced and unbroken. */
.barcode { font-variant-numeric: tabular-nums; letter-spacing: 0.02em; white-space: nowrap; }
.desc-main { font-weight: 500; }
.desc-sub { color: var(--ink-soft); font-size: 13px; }

.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  background: #eef1f5;
  color: var(--ink-soft);
  font-size: 12px;
  white-space: nowrap;
}
.pill.power { background: var(--accent-soft); color: #14428f; }

body.dark .pill {
  background: #334155;
  color: #fff;
}
body.dark .pill.power {
  background: var(--accent-soft);
  color: #fff;
}
.pill.completed-yes {
  background: var(--green);
  color: #fff;
}
.pill.completed-no {
  background: var(--amber);
  color: #000;
}
.empty { margin: 0; padding: 28px 14px; text-align: center; color: var(--ink-soft); }

/* Database page */
.kv-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 4px 22px; margin-top: 10px; }
.kv { display: flex; justify-content: space-between; gap: 10px; padding: 6px 0; border-bottom: 1px solid rgba(128, 128, 128, 0.25); font-size: 13px; }
.kv-k { color: var(--ink-soft); }
.kv-v { font-variant-numeric: tabular-nums; }
.sync-out, .query-out { margin-top: 14px; font-size: 13px; }
.sync-out .row { padding: 4px 0; border-bottom: 1px solid rgba(128, 128, 128, 0.18); }
.query-out { overflow-x: auto; }
.query-out table { border-collapse: collapse; width: 100%; font-size: 12px; }
.query-out th, .query-out td { border: 1px solid rgba(128, 128, 128, 0.3); padding: 4px 8px; text-align: left; vertical-align: top; }
.query-out th { font-weight: 600; }

/* ----------------------------------------------------------------- detail */
.detail-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.detail-head h2 { margin: 0 0 4px; font-size: 21px; }
.detail-actions { display: flex; gap: 8px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
}
.panel h3 {
  margin: 0 0 14px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 14px 22px;
}
.field .k { font-size: 12px; color: var(--ink-soft); display: block; margin-bottom: 2px; }
.field .v { font-size: 14px; }
.field .v.empty-value { color: #a9b2bd; }

/* ------------------------------------------------------------------- form */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}
label.field-label { display: block; font-size: 12px; color: var(--ink-soft); margin-bottom: 4px; }
.form-grid input, .form-grid select, .form-grid textarea, .spec-row input {
  width: 100%;
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}
.form-grid textarea { min-height: 74px; resize: vertical; }
.form-grid .span-2 { grid-column: 1 / -1; }

.spec-row { display: grid; grid-template-columns: 1fr 1fr 34px; gap: 8px; margin-bottom: 8px; }
.spec-row button {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  cursor: pointer;
  color: var(--ink-soft);
  font-size: 16px;
  line-height: 1;
}
.spec-row button:hover { color: #b42318; border-color: #f1b0a9; }

.form-actions { display: flex; gap: 10px; align-items: center; margin-top: 20px; }

/* ------------------------------------------------------------------ toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%) translateY(12px);
  background: #12202f;
  color: #fff;
  padding: 11px 18px;
  border-radius: 10px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
  max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: #7a271a; }

@media (max-width: 720px) {
  .col-cat, .col-power, .col-barcode { display: none; }
  th, td { padding: 10px; }
}

/* --------------------------------------------------- database table editor */
.db-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0 10px; }
.db-tab {
  display: inline-flex; align-items: center; gap: 7px; cursor: pointer;
  border: 1px solid var(--line); background: transparent; color: inherit;
  border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 13px;
}
.db-tab:hover { border-color: #8aa0d8; }
.db-tab.active { background: #31508f; border-color: #31508f; color: #fff; }
.db-count { font-variant-numeric: tabular-nums; opacity: 0.7; font-size: 11px; }
.db-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 10px; }
.db-bar input[type="search"] { min-width: 220px; flex: 1 1 220px; max-width: 380px; }
.db-spacer { flex: 1 1 auto; }
.db-grid-wrap { overflow: auto; max-height: 64vh; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.db-grid { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 12.5px; }
.db-grid th {
  position: sticky; top: 0; z-index: 1; background: var(--panel);
  text-align: left; font-weight: 600; font-size: 11px; letter-spacing: 0.02em;
  text-transform: uppercase; color: var(--ink-soft); white-space: nowrap;
  padding: 8px 6px; border-bottom: 1px solid var(--line); cursor: pointer;
}
.db-grid th.active { color: #31508f; box-shadow: inset 0 -2px 0 #31508f; }
.db-grid td { border-bottom: 1px solid rgba(128, 128, 128, 0.18); padding: 2px 4px; vertical-align: top; }
.db-grid tr:last-child td { border-bottom: 0; }
.db-grid input, .db-grid textarea {
  width: 100%; min-width: 84px; box-sizing: border-box; font: inherit; color: inherit;
  background: transparent; border: 1px solid transparent; border-radius: 6px;
  padding: 5px 6px; resize: vertical;
}
.db-grid input:focus, .db-grid textarea:focus { outline: 2px solid #6b8cff; outline-offset: -1px; background: rgba(107, 140, 255, 0.06); }
.db-grid input.db-dirty, .db-grid textarea.db-dirty { background: rgba(255, 186, 40, 0.18); border-color: rgba(220, 150, 20, 0.55); }
.db-grid tr.db-row-dirty td:first-child { box-shadow: inset 3px 0 0 #e0a032; }
.db-grid tr.db-new td { background: rgba(60, 160, 110, 0.10); }
.db-grid .db-ro { padding: 6px; color: var(--ink-soft); white-space: nowrap; font-variant-numeric: tabular-nums; }
.db-grid .db-actions { white-space: nowrap; text-align: right; }
.db-grid .db-actions .btn-ghost { padding: 2px 6px; font-size: 12px; }

/* ------------------------------------------------------------- roles, BOM */
[hidden] { display: none !important; }
.role-select {
  font: inherit; font-size: 12.5px; padding: 4px 6px; border-radius: 6px;
  border: 1px solid var(--line); background: var(--panel); color: inherit;
}
.bom-table input { width: 100%; max-width: 260px; font: inherit; }
.bom-table .bom-qty { max-width: 74px; }

/* ------------------------------------------------- grid selection + bulk */
.db-grid .db-sel { width: 34px; text-align: center; padding: 4px; }
.db-grid .db-sel input { min-width: 0; width: 15px; height: 15px; display: block; margin: 3px auto; }
.db-bulk {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  margin: 0 0 10px; padding: 8px 10px;
  border: 1px dashed var(--line); border-radius: 8px;
}
.db-bulk select, .db-bulk input {
  font: inherit; font-size: 12.5px; padding: 5px 7px; border-radius: 6px;
  border: 1px solid var(--line); background: var(--panel); color: inherit;
}
.db-bulk input { min-width: 210px; flex: 1 1 210px; max-width: 340px; }

/* ------------------------------------------------- column resizing (grid) */
.db-grid { table-layout: fixed; }
.db-grid td { overflow: hidden; }
.db-grid th { overflow: hidden; text-overflow: ellipsis; }
.db-grid input, .db-grid textarea { min-width: 0; }
.db-grid .db-ro { overflow: hidden; text-overflow: ellipsis; }
.db-grid .db-grip {
  position: absolute; top: 0; right: -2px; width: 9px; height: 100%;
  cursor: col-resize; z-index: 2;
}
.db-grid .db-grip::after {
  content: ''; position: absolute; top: 6px; bottom: 6px; left: 4px; width: 1px;
  background: rgba(128, 128, 128, 0.45);
}
.db-grid th:hover .db-grip::after, body.db-resizing .db-grid .db-grip::after {
  background: #31508f; width: 2px; left: 3px;
}
body.db-resizing { cursor: col-resize; user-select: none; }

/* --------------------------------------------------------- category check */
.tax-h { margin: 14px 0 6px; font-size: 13px; letter-spacing: 0.01em; }
#taxonomy-panel .list td, #taxonomy-panel .list th { font-size: 12.5px; }

/* ------------------------------------------- renaming categories / import */
.ren-table td { vertical-align: middle; }
.ren-table .ren-cell { min-width: 220px; }
.ren-input {
  font: inherit; font-size: 13px; width: 100%; max-width: 340px;
  padding: 5px 7px; border-radius: 6px; border: 1px solid var(--line);
  background: var(--panel); color: inherit;
}
.ren-input:focus { outline: 2px solid #6b8cff; outline-offset: -1px; }
.ren-n { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pill-stray { background: rgba(220, 150, 20, 0.18); color: #8a5a00; font-size: 11px; }
.imp-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
#imp-out .kv-grid { margin-top: 10px; }
#imp-table { font: inherit; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line); background: #fff; color: var(--ink); }

/* --------------------------------------------------------------- data health */
.hl-h { margin: 18px 0 8px; font-size: 13px; letter-spacing: 0.01em; }
.hl-h:first-child { margin-top: 0; }
.hl-table td { vertical-align: middle; }
.hl-n { font-variant-numeric: tabular-nums; white-space: nowrap; }
.hl-bar-cell { width: 170px; }
.hl-bar { display: block; height: 7px; border-radius: 999px; background: rgba(128, 128, 128, 0.18); overflow: hidden; }
.hl-fill { display: block; height: 100%; border-radius: 999px; background: #2f855a; }
.hl-fill.low { background: #c98a1b; }
.hl-list { margin: 6px 0 0; padding-left: 18px; columns: 3; font-size: 13px; }
.db-note {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  margin: 0 0 10px; padding: 8px 10px; font-size: 13px;
  border: 1px dashed var(--line); border-radius: 8px;
}

/* ------------------------------------------------- navigation (grouped) */
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.topbar-actions .btn-primary { white-space: nowrap; }
.brand h1 a { color: inherit; text-decoration: none; }
.navbar { background: #0d1926; border-top: 1px solid rgba(255, 255, 255, 0.07); }
.navbar-inner {
  max-width: 1180px; margin: 0 auto; padding: 6px 20px 8px;
  display: flex; align-items: center; gap: 2px; flex-wrap: wrap;
}
.nav-group { display: flex; align-items: center; gap: 2px; }
.nav-group + .nav-group {
  margin-left: 14px; padding-left: 14px;
  border-left: 1px solid rgba(255, 255, 255, 0.12);
}
.nav-label {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.09em;
  color: #7d93aa; margin-right: 8px;
}
.nav-link { padding: 6px 10px; border-radius: 7px; white-space: nowrap; }
.nav-link:hover { background: rgba(255, 255, 255, 0.09); color: #fff; }
.nav-link.active { background: var(--accent); color: #fff; }
#auth-bar { margin-left: auto; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.auth-label { color: #8ea3b8; font-size: 12px; }
.auth-email { color: #dbe7f3; font-size: 12.5px; }
@media (max-width: 760px) {
  .navbar-inner { padding: 6px 12px 8px; }
  .nav-group + .nav-group { margin-left: 8px; padding-left: 8px; }
  .nav-label { display: none; }
  #auth-bar { margin-left: 0; width: 100%; padding-top: 6px; }
}

/* ------------------------------------- breadcrumbs and the unit page layout */
.crumbs {
  display: flex; align-items: center; gap: 8px; font-size: 12.5px;
  color: var(--ink-soft); margin-bottom: 8px;
}
.crumbs a { color: var(--accent); text-decoration: none; }
.crumbs a:hover { text-decoration: underline; }
.crumb-sep { color: var(--ink-soft); }
.crumb-here { color: var(--ink); font-weight: 600; }
.detail-cols {
  display: grid; grid-template-columns: minmax(0, 1fr) 260px;
  gap: 26px; align-items: start;
}
.detail-fields { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.detail-side { border-left: 1px solid var(--line); padding-left: 22px; }
.detail-side .unit-photo { max-width: 100%; height: auto; border-radius: 10px; }
.notes-block { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); }
.notes-block .k {
  display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.07em;
  color: var(--ink-soft); margin-bottom: 6px;
}
.notes-block p { margin: 0; line-height: 1.6; max-width: 82ch; overflow-wrap: anywhere; }
@media (max-width: 820px) {
  .detail-cols { grid-template-columns: 1fr; }
  .detail-side {
    border-left: 0; padding-left: 0;
    border-top: 1px solid var(--line); padding-top: 16px;
  }
}
