:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #eef1ef;
  --text: #17201b;
  --muted: #5d6a63;
  --line: #d6ddd9;
  --accent: #1b7a4f;
  --accent-text: #ffffff;
  --accent-soft: #e2f2e9;
  --warn: #8a5a00;
  --warn-bg: #fff3d4;
  --danger: #b42318;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1512;
    --surface: #18201c;
    --surface-2: #212b26;
    --text: #e8eeea;
    --muted: #9aa9a1;
    --line: #2e3a34;
    --accent: #3fbf80;
    --accent-text: #06140d;
    --accent-soft: #1c3529;
    --warn: #ffcc66;
    --warn-bg: #3a2e12;
    --danger: #ff7a6e;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
h1 { font-size: 24px; margin: 0 0 6px; letter-spacing: -0.01em; }
h2 { font-size: 18px; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 14px; }

/* ---------- Pages ---------- */
.page { min-height: 100%; padding: calc(var(--safe-top) + 12px) 16px calc(var(--safe-bottom) + 24px); }
.narrow { max-width: 440px; margin: 8vh auto 0; }
.brandmark { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.brandmark img { border-radius: 14px; }
.brandmark h1 { margin: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; max-width: 960px; margin: 0 auto; }
.topbar-title { font-size: 18px; margin: 0; }
.who { color: var(--muted); font-size: 14px; }

.stack { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; }
label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 10px; color: var(--text); font-weight: 500; }
label.check input { width: 22px; height: 22px; accent-color: var(--accent); }
input, select {
  font: inherit; font-size: 17px; font-weight: 500; color: var(--text);
  background: var(--surface); border: 1.5px solid var(--line); border-radius: 10px;
  padding: 12px; width: 100%; min-height: 48px;
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[readonly] { background: var(--surface-2); color: var(--muted); }
input.unsure { border-color: var(--warn); background: var(--warn-bg); }

.btn {
  font: inherit; font-weight: 600; font-size: 15px; color: var(--text);
  background: var(--surface); border: 1.5px solid var(--line); border-radius: 10px;
  padding: 10px 14px; min-height: 44px; cursor: pointer; white-space: nowrap;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.big { min-height: 54px; font-size: 17px; }
.btn.small { font-size: 14px; margin-top: 12px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row .btn { flex: 1; }

.badge {
  display: inline-block; min-width: 20px; padding: 1px 6px; border-radius: 10px;
  background: var(--warn); color: #fff; font-size: 12px; font-weight: 700; text-align: center;
}
.error { color: var(--danger); margin: 0; font-size: 14px; min-height: 1px; }
.warn { background: var(--warn-bg); color: var(--warn); border-radius: 10px; padding: 10px 12px; margin: 12px 0 0; font-size: 14px; font-weight: 600; }
.install-hint { margin-top: 28px; font-size: 14px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: -6px; }
.chips:empty { display: none; }
.chip {
  font: inherit; font-size: 13px; font-weight: 600; font-family: ui-monospace, Menlo, monospace;
  background: var(--accent-soft); color: var(--text); border: 0; border-radius: 16px; padding: 6px 10px; cursor: pointer;
}
.chip.plain { font-family: inherit; }

/* ---------- Camera ---------- */
.camera { position: fixed; inset: 0; background: #000; color: #fff; overflow: hidden; }
.camera video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.aim { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; }
.aim-box {
  width: min(86vw, 520px); aspect-ratio: 16 / 10; border-radius: 16px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.38); border: 2px solid rgba(255, 255, 255, 0.85);
  transition: border-color 0.2s;
}
.aim-box.found { border-color: #3fbf80; border-width: 3px; }
.cam-top {
  position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; gap: 8px;
  padding: calc(var(--safe-top) + 10px) 16px 0;
}
.pill {
  font: inherit; font-size: 15px; font-weight: 600; color: #fff; background: rgba(0, 0, 0, 0.55);
  border: 0; border-radius: 22px; padding: 10px 14px; min-height: 44px; display: flex; align-items: center; gap: 8px;
  max-width: 70vw; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.pill #cam-location { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #3fbf80; flex: none; }
.cam-hint {
  position: absolute; left: 16px; right: 16px; bottom: calc(var(--safe-bottom) + 132px);
  text-align: center; margin: 0; font-size: 15px; font-weight: 600; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}
.cam-bottom {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; align-items: center; justify-content: space-around;
  padding: 18px 16px calc(var(--safe-bottom) + 22px);
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.6));
}
.shutter {
  width: 78px; height: 78px; border-radius: 50%; border: 5px solid #fff; background: rgba(255, 255, 255, 0.25); cursor: pointer;
}
.shutter:active { background: #fff; }
.cam-side {
  font: inherit; font-size: 13px; font-weight: 600; color: #fff; background: none; border: 0;
  display: flex; flex-direction: column; align-items: center; gap: 4px; width: 72px; cursor: pointer;
}
.cam-side .icon { width: 30px; height: 30px; }
.cam-error {
  position: absolute; left: 16px; right: 16px; top: 40%; margin: 0; text-align: center;
  background: rgba(0, 0, 0, 0.75); padding: 16px; border-radius: var(--radius);
}

/* ---------- Review sheet ---------- */
dialog.sheet {
  width: min(100%, 560px); max-height: 92vh; margin: auto auto 0; padding: 16px 16px calc(var(--safe-bottom) + 16px);
  border: 0; border-radius: 18px 18px 0 0; background: var(--surface); color: var(--text); overflow-y: auto;
}
@media (min-width: 640px) { dialog.sheet { margin: auto; border-radius: 18px; } }
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
#review-title:focus { outline: none; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; }
.status { font-size: 13px; color: var(--muted); }
.status.busy::before {
  content: ""; display: inline-block; width: 12px; height: 12px; margin-right: 6px; vertical-align: -1px;
  border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#review-img { width: 100%; max-height: 150px; object-fit: contain; background: var(--surface-2); border-radius: 10px; margin-bottom: 10px; }
.fields { display: flex; flex-direction: column; gap: 12px; }
.fields input[name="serial"], .fields input[name="model"] { font-family: ui-monospace, Menlo, monospace; letter-spacing: 0.02em; }
.sheet-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.sheet-actions .btn { flex: 1 1 auto; }
.sheet-actions .primary { flex: 2 1 100%; min-height: 52px; font-size: 17px; order: -1; }

/* ---------- Records ---------- */
.tabs { display: flex; gap: 4px; max-width: 960px; margin: 12px auto; background: var(--surface-2); border-radius: 12px; padding: 4px; }
.tabs button {
  flex: 1; font: inherit; font-weight: 600; font-size: 14px; color: var(--muted); background: none; border: 0;
  border-radius: 9px; padding: 10px 6px; min-height: 40px; cursor: pointer;
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.tab { max-width: 960px; margin: 0 auto; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 8px 0 12px; }
.toolbar > span { flex: 1 1 100%; font-size: 14px; }
@media (min-width: 640px) { .toolbar > span { flex: 1; } }

.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.list:empty::after { content: "Nothing here yet."; display: block; padding: 20px; color: var(--muted); text-align: center; }
.list li { border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list button.item {
  width: 100%; text-align: left; font: inherit; color: inherit; background: none; border: 0; padding: 12px 14px;
  display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; cursor: pointer;
}
.item .serial { font-family: ui-monospace, Menlo, monospace; font-weight: 700; overflow-wrap: anywhere; }
.item .meta { grid-column: 1; font-size: 13px; color: var(--muted); }
.item .state { grid-row: 1 / span 2; grid-column: 2; align-self: center; font-size: 12px; font-weight: 700; }
.state.ok { color: var(--accent); }
.state.pending { color: var(--warn); }
.state.dup { color: var(--danger); }

.filters { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; align-items: end; }
.filters label { min-width: 0; }
/* iOS Safari gives date inputs a wide intrinsic size; let them shrink to the column. */
input[type="date"] { min-width: 0; max-width: 100%; display: block; -webkit-appearance: none; appearance: none; }
input[type="date"]::-webkit-date-and-time-value { text-align: left; }
.filters label:first-child, .filters label:nth-child(4) { grid-column: 1 / -1; }
.filters .btn { grid-column: 1 / -1; }
@media (min-width: 760px) {
  .filters { grid-template-columns: 1.4fr 1fr 1fr 1.4fr auto; }
  .filters label:first-child, .filters label:nth-child(4), .filters .btn { grid-column: auto; }
}
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table th { background: var(--surface-2); font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); position: sticky; top: 0; }
.table td:nth-child(2) { font-family: ui-monospace, Menlo, monospace; }
.table tbody tr:last-child td { border-bottom: 0; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 130px); transform: translate(-50%, 20px);
  background: #111a15; color: #fff; padding: 10px 16px; border-radius: 22px; font-weight: 600; font-size: 15px;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 50; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
