:root {
  --bg-bg: #f4f6f8;
  --bg-panel: #ffffff;
  --bg-line: #d9dee5;
  --bg-line-soft: #eaeef2;
  --bg-head: #f7f9fb;
  --bg-head-2: #eef2f6;
  --bg-text: #1c2430;
  --bg-muted: #6b7684;
  --bg-accent: #1c7ed6;
  --bg-accent-soft: #e7f2fc;
  --bg-dirty: #fff3cd;
  --bg-saved: #d3f9d8;
  --bg-error: #ffe3e3;
  --bg-sel: #cfe4f7;
  --bg-radius: 8px;
  --bg-shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .08);
  --bg-mono: "Cascadia Mono", "Consolas", "SF Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--bg-text);
  background: var(--bg-bg);
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ------------------------------------------------------------------ topbar */

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--bg-line);
  flex: 0 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 650;
  letter-spacing: -.01em;
  white-space: nowrap;
}

.brand svg { width: 20px; height: 20px; color: var(--bg-accent); }

.brand small {
  font-weight: 400;
  color: var(--bg-muted);
  font-size: 12px;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 7px;
  flex-wrap: wrap;
  flex: 1 1 auto;
}

.spacer { flex: 1 1 auto; }

/* ------------------------------------------------------------------ controls */

button, .btn {
  font: inherit;
  font-size: 13px;
  color: var(--bg-text);
  background: var(--bg-panel);
  border: 1px solid var(--bg-line);
  border-radius: 6px;
  padding: 6px 11px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  transition: background .12s, border-color .12s;
}

button:hover, .btn:hover { background: var(--bg-head); border-color: #c3ccd6; }
button:active { background: var(--bg-head-2); }
button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--bg-accent);
  outline-offset: 1px;
}
button[disabled] { opacity: .5; cursor: not-allowed; }

button.primary {
  background: var(--bg-accent);
  border-color: var(--bg-accent);
  color: #fff;
}
button.primary:hover { background: #1868b4; }

button.danger { color: #c92a2a; }
button.danger:hover { background: #fff5f5; border-color: #ffc9c9; }

button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover { background: var(--bg-head); }

button svg, .btn svg { width: 15px; height: 15px; }

input[type="text"], input[type="search"], select {
  font: inherit;
  font-size: 13px;
  padding: 6px 9px;
  border: 1px solid var(--bg-line);
  border-radius: 6px;
  background: var(--bg-panel);
  color: var(--bg-text);
}

.segmented {
  display: inline-flex;
  border: 1px solid var(--bg-line);
  border-radius: 6px;
  overflow: hidden;
  background: var(--bg-panel);
}
.segmented button {
  border: 0;
  border-radius: 0;
  border-right: 1px solid var(--bg-line);
  padding: 6px 14px;
}
.segmented button:last-child { border-right: 0; }
.segmented button[aria-pressed="true"] {
  background: var(--bg-accent);
  color: #fff;
}

.search {
  position: relative;
  display: inline-flex;
  align-items: center;
}
.search svg {
  position: absolute;
  left: 8px;
  width: 14px;
  height: 14px;
  color: var(--bg-muted);
  pointer-events: none;
}
.search input { padding-left: 27px; width: 230px; }

/* --------------------------------------------------------------- save state */

.save-state {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--bg-muted);
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--bg-head);
  border: 1px solid var(--bg-line-soft);
  white-space: nowrap;
}
.save-state .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--bg-muted);
}
.save-state[data-state="pending"] { color: #8a6d1f; background: #fff8e6; border-color: #f5e2ae; }
.save-state[data-state="pending"] .dot { background: #e8b93c; }
.save-state[data-state="saving"] { color: #1868b4; background: var(--bg-accent-soft); border-color: #bcdcfa; }
.save-state[data-state="saving"] .dot { background: var(--bg-accent); animation: pulse 1s infinite; }
.save-state[data-state="saved"] { color: #2b7a3d; background: #edfbee; border-color: #c3e9cd; }
.save-state[data-state="saved"] .dot { background: #40c057; }
.save-state[data-state="error"] { color: #c92a2a; background: #fff5f5; border-color: #ffc9c9; }
.save-state[data-state="error"] .dot { background: #fa5252; }

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

/* ------------------------------------------------------------------- scan */

.scanbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--bg-accent-soft);
  border-bottom: 1px solid #cfe3f7;
  flex: 0 0 auto;
}
.scanbar .scan-label {
  font-size: 12px;
  font-weight: 600;
  color: #14568c;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.scanbar .scan-feedback {
  font-size: 13px;
  color: #14568c;
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.scanbar.is-found { background: #ebfbee; border-bottom-color: #c3e9cd; }
.scanbar.is-found .scan-label { color: #2b7a3d; }
.scanbar.is-found .scan-feedback { color: #2b7a3d; }
  .scanbar.is-new { background: #fff8e6; border-bottom-color: #f5e2ae; }
  .scanbar.is-new .scan-label { color: #8a6d1f; }
  .scanbar.is-new .scan-feedback { color: #8a6d1f; }
  .scanbar.is-error { background: #fff0f0; border-bottom-color: #f3c2c2; }
  .scanbar.is-error .scan-label { color: #a02a2a; }
  .scanbar.is-error .scan-feedback { color: #a02a2a; }

/* ------------------------------------------------------------------ sheet */

.sheetbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 16px 0;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--bg-line);
  flex: 0 0 auto;
}
.sheet-tab {
  border: 0;
  border-radius: 6px 6px 0 0;
  background: transparent;
  padding: 7px 16px;
  font-size: 13px;
  font-weight: 550;
  color: var(--bg-muted);
  border-bottom: 2px solid transparent;
  border-radius: 0;
}
.sheet-tab[aria-selected="true"] {
  color: var(--bg-accent);
  border-bottom-color: var(--bg-accent);
  background: var(--bg-accent-soft);
}
.sheet-tab .count {
  display: inline-block;
  margin-left: 6px;
  font-size: 11px;
  color: var(--bg-muted);
  background: var(--bg-head-2);
  border-radius: 999px;
  padding: 1px 7px;
}

/* ------------------------------------------------------------------- grid */

.grid-wrap {
  flex: 1 1 auto;
  min-height: 0;
  padding: 10px 16px 0;
  display: flex;
}

.bg {
  --row-h: 30px;
  position: relative;
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 58px 1fr;
  grid-template-rows: 38px 1fr;
  background: var(--bg-panel);
  border: 1px solid var(--bg-line);
  border-bottom: 0;
  border-radius: var(--bg-radius) var(--bg-radius) 0 0;
  box-shadow: var(--bg-shadow);
  overflow: hidden;
  outline: none;
}

.bg-corner {
  grid-column: 1;
  grid-row: 1;
  background: var(--bg-head-2);
  border-right: 1px solid var(--bg-line);
  border-bottom: 1px solid var(--bg-line);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: var(--bg-muted);
  font-weight: 600;
  user-select: none;
}

.bg-head {
  grid-column: 2;
  grid-row: 1;
  overflow: hidden;
  background: var(--bg-head);
  border-bottom: 1px solid var(--bg-line);
  position: relative;
}
.bg-head-inner { position: absolute; top: 0; left: 0; height: 100%; display: flex; will-change: transform; }

.bg-th {
  position: relative;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 650;
  color: #3d4757;
  border-right: 1px solid var(--bg-line-soft);
  cursor: pointer;
  user-select: none;
  overflow: hidden;
  white-space: nowrap;
}
.bg-th:hover { background: var(--bg-head-2); }
.bg-th.is-locked { color: var(--bg-muted); background: #f2f4f7; }
.bg-th-label { overflow: hidden; text-overflow: ellipsis; }

.bg-sort { width: 0; }
.bg-sort.is-asc::after  { content: "\25B2"; font-size: 8px; color: var(--bg-accent); margin-left: 5px; }
.bg-sort.is-desc::after { content: "\25BC"; font-size: 8px; color: var(--bg-accent); margin-left: 5px; }

.bg-resizer {
  position: absolute;
  top: 0;
  right: -3px;
  width: 7px;
  height: 100%;
  cursor: col-resize;
  z-index: 3;
}
.bg-resizer:hover { background: var(--bg-accent); opacity: .45; }
body.is-resizing { cursor: col-resize; user-select: none; }

.bg-rownum {
  grid-column: 1;
  grid-row: 2;
  overflow: hidden;
  background: var(--bg-head);
  border-right: 1px solid var(--bg-line);
  position: relative;
}
.bg-rownum-inner { position: absolute; top: 0; left: 0; right: 0; will-change: transform; }
.bg-rn {
  height: var(--row-h);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 8px;
  font-size: 11px;
  color: var(--bg-muted);
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--bg-line-soft);
}
.bg-rn:hover { background: var(--bg-head-2); }

.bg-body {
  grid-column: 2;
  grid-row: 2;
  overflow: auto;
  position: relative;
  outline: none;
}
.bg-canvas { position: relative; min-width: 100%; }
.bg-layer { position: absolute; left: 0; top: 0; }

.bg-row { display: flex; position: relative; }
.bg-row.is-flash { animation: flash 1.4s ease-out; }
@keyframes flash {
  0%, 40% { background: #fff3bf; }
  100% { background: transparent; }
}

.bg-cell {
  flex: 0 0 auto;
  height: var(--row-h);
  line-height: calc(var(--row-h) - 1px);
  padding: 0 9px;
  border-right: 1px solid var(--bg-line-soft);
  border-bottom: 1px solid var(--bg-line-soft);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: cell;
  position: relative;
  user-select: none;
  background: var(--bg-panel);
}
.bg-cell:hover { background: #f7fafc; }
.bg-cell.is-mono { font-family: var(--bg-mono); font-size: 12px; letter-spacing: -.01em; }
.bg-cell.is-locked {
  background: #f2f4f7;
  color: var(--bg-muted);
  cursor: default;
  font-variant-numeric: tabular-nums;
}
.bg-cell.is-selected { background: var(--bg-sel); }
.bg-cell.is-active {
  outline: 2px solid var(--bg-accent);
  outline-offset: -2px;
  z-index: 2;
  background: #fff;
}
.bg-cell.is-dirty { background: var(--bg-dirty); }
.bg-cell.is-dirty.is-selected { background: #fbeaa0; }
.bg-cell.is-error { background: var(--bg-error) !important; }
.bg-cell.is-error::after {
  content: "";
  position: absolute;
  right: 2px; top: 2px;
  width: 0; height: 0;
  border-left: 5px solid transparent;
  border-bottom: 5px solid #e03131;
}
.bg-val { pointer-events: none; }

.bg-editor {
  position: absolute;
  z-index: 20;
  box-shadow: 0 0 0 2px var(--bg-accent), var(--bg-shadow);
  border-radius: 2px;
  background: #fff;
  overflow: hidden;
}
.bg-editor input {
  width: 100%;
  height: 100%;
  border: 0;
  padding: 0 8px;
  font: inherit;
  font-size: 13px;
  outline: none;
  background: transparent;
}
.bg-editor[hidden] { display: none; }

/* ------------------------------------------------------------------ pager */

.pager {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: var(--bg-panel);
  border-top: 1px solid var(--bg-line);
  flex: 0 0 auto;
  font-size: 13px;
  color: var(--bg-muted);
}
.pager b { color: var(--bg-text); font-variant-numeric: tabular-nums; }
.pager .spacer { flex: 1 1 auto; }

/* ------------------------------------------------------------------ toasts */

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  pointer-events: none;
}
.toast {
  background: #1c2430;
  color: #fff;
  padding: 9px 14px;
  border-radius: 6px;
  font-size: 13px;
  box-shadow: var(--bg-shadow);
  max-width: 380px;
  animation: toast-in .18s ease-out;
  pointer-events: auto;
}
.toast.ok    { background: #2b7a3d; }
.toast.warn  { background: #9a6700; }
.toast.error { background: #c92a2a; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

/* ------------------------------------------------------------------ modals */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 20px;
}
.modal-backdrop[hidden] { display: none; }

.modal {
  background: var(--bg-panel);
  border-radius: 10px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .28);
  width: min(1000px, 100%);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal > header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--bg-line);
}
.modal > header h2 { margin: 0; font-size: 16px; font-weight: 650; }
.modal > .body { padding: 18px; overflow: auto; }
.modal > footer {
  display: flex;
  align-items: center;
  gap: 9px;
  justify-content: flex-end;
  padding: 12px 18px;
  border-top: 1px solid var(--bg-line);
  background: var(--bg-head);
}
.modal > footer .spacer { flex: 1 1 auto; }

.map-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.map-table th, .map-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--bg-line-soft);
  text-align: left;
}
.map-table th { font-size: 12px; color: var(--bg-muted); text-transform: uppercase; letter-spacing: .03em; }
.map-table td { font-family: var(--bg-mono); font-size: 12px; }
.map-table select { width: 100%; font-family: inherit; }

.preview {
  margin-top: 16px;
  border: 1px solid var(--bg-line);
  border-radius: 6px;
  overflow: auto;
  max-height: 260px;
}
.preview table { border-collapse: collapse; font-size: 12px; white-space: nowrap; }
.preview th, .preview td {
  padding: 5px 9px;
  border-bottom: 1px solid var(--bg-line-soft);
  border-right: 1px solid var(--bg-line-soft);
  max-width: 190px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.preview thead th { position: sticky; top: 0; background: var(--bg-head-2); z-index: 1; }
.preview tbody tr.is-empty td { color: var(--bg-muted); font-style: italic; }

.dupe {
  border: 1px solid #f0c36d;
  background: #fff9e8;
  border-radius: 6px;
  padding: 10px 12px;
  margin-top: 16px;
  font-size: 12.5px;
}
.dupe.is-clear { border-color: #b5dfc3; background: #f0fbf3; }
.dupe h4 { margin: 0 0 4px; font-size: 13px; font-weight: 650; }
.dupe ul { margin: 6px 0 0; padding: 0; list-style: none; }
.dupe li { font-family: var(--bg-mono); font-size: 12px; padding: 2px 0; }
.dupe li .dupe-rows { color: var(--bg-muted); }
.dupe code {
  font-family: var(--bg-mono);
  background: #fff;
  border: 1px solid #f0c36d;
  border-radius: 4px;
  padding: 0 4px;
}

.dropzone {
  border: 2px dashed var(--bg-line);
  border-radius: 8px;
  padding: 30px;
  text-align: center;
  color: var(--bg-muted);
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.is-over { border-color: var(--bg-accent); background: var(--bg-accent-soft); }
.dropzone strong { color: var(--bg-text); display: block; margin-bottom: 4px; font-size: 15px; }

.field { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.field label { color: var(--bg-muted); }
.hint { font-size: 12px; color: var(--bg-muted); line-height: 1.5; }

#camera-video {
  width: 100%;
  max-height: 60vh;
  border-radius: 8px;
  background: #000;
  object-fit: contain;
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 60px 20px;
  color: var(--bg-muted);
  text-align: center;
}
/* An author `display` beats the browser's [hidden] rule, so the panel stayed
   on top of a populated grid until this restored it. */
.empty[hidden] { display: none; }
.empty svg { width: 40px; height: 40px; opacity: .4; }
.empty h3 { margin: 0; font-size: 15px; color: var(--bg-text); }
.empty p { margin: 0; max-width: 420px; font-size: 13px; }

.busy {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, .7);
  z-index: 10;
  font-size: 13px;
  color: var(--bg-muted);
}
.busy[hidden] { display: none; }
.grid-wrap { position: relative; }

/* ----------------------------------------------------------------- sign-in */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  border: 1px solid var(--bg-line);
  border-radius: 12px;
  box-shadow: var(--bg-shadow);
  padding: 26px 26px 22px;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 650;
  letter-spacing: -.01em;
  color: var(--bg-muted);
  margin-bottom: 18px;
}
.login-brand svg { width: 20px; height: 20px; }

.login-card h1 {
  font-size: 19px;
  margin: 0 0 16px;
  letter-spacing: -.01em;
}

.login-card label {
  font-size: 12px;
  font-weight: 600;
  color: var(--bg-muted);
  margin-bottom: 5px;
}

.login-card input[type="text"],
.login-card input[type="password"] {
  font: inherit;
  font-size: 14px;
  padding: 9px 11px;
  margin-bottom: 14px;
  border: 1px solid var(--bg-line);
  border-radius: 6px;
  background: var(--bg-panel);
  color: var(--bg-text);
}
.login-card input:focus-visible {
  outline: 2px solid var(--bg-accent);
  outline-offset: 1px;
  border-color: var(--bg-accent);
}

.login-card button[type="submit"] {
  margin-top: 4px;
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
}

.login-error {
  margin: 0 0 14px;
  padding: 9px 11px;
  font-size: 13px;
  color: #8b1a1a;
  background: var(--bg-error);
  border: 1px solid #f2b8b8;
  border-radius: 6px;
}

.login-hint {
  margin: 0 0 14px;
  font-size: 12px;
  color: var(--bg-muted);
}

/* Signed-in badge in the toolbar. */
.who {
  font-size: 12px;
  color: var(--bg-muted);
  white-space: nowrap;
  padding-left: 4px;
}

@media (max-width: 900px) {
  .search input { width: 150px; }
  .brand small { display: none; }
  .who { display: none; }
}
