:root {
  --bg: #0b0b0f;
  --surface: #16171d;
  --surface-2: #1f2129;
  --border: #2a2d38;
  --text: #f2f3f7;
  --text-dim: #8b90a1;
  --accent: #0a84ff;
  --accent-dim: #2b6bd1;
  --danger: #ff3b30;
  --success: #30d158;
  --warning: #ffcc00;
  --nav-h: 64px;
  --header-h: 108px;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}
button { font-family: inherit; }
input, button { font-size: 16px; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.hidden { display: none !important; }

/* -------- Header + group picker -------- */
header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(11, 11, 15, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
  padding-top: var(--safe-top);
}

.group-picker {
  display: flex;
  gap: 8px;
  padding: 12px 12px 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.group-picker::-webkit-scrollbar { display: none; }

.group-chip {
  flex: 0 0 auto;
  padding: 10px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.group-chip .count {
  margin-left: 6px;
  color: var(--text-dim);
  font-size: 13px;
}
.group-chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.group-chip.active .count { color: rgba(255,255,255,0.85); }

.group-chip.add-custom { border-style: dashed; color: var(--text-dim); }

.status-bar {
  padding: 0 12px 8px;
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 13px;
  color: var(--text-dim);
}
.sync-status { font-size: 10px; line-height: 1; }
.sync-status.sync-idle { color: var(--text-dim); }
.sync-status.sync-syncing { color: var(--warning); animation: pulse 1s infinite; }
.sync-status.sync-ok { color: var(--success); }
.sync-status.sync-error { color: var(--danger); }
@keyframes pulse { 0%,100% {opacity:1} 50% {opacity:0.4} }

/* -------- Main + views -------- */
main {
  padding: 16px 16px calc(var(--nav-h) + var(--safe-bottom) + 24px);
  max-width: 720px;
  margin: 0 auto;
}
.view { display: none; }
.view.active { display: block; }
.view-header {
  display: flex; align-items: center; gap: 10px; margin-bottom: 12px;
}
.view h2 { margin: 0 0 12px; font-size: 22px; font-weight: 600; }

/* -------- Scan view -------- */
.scan-button {
  width: 100%;
  padding: 20px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--accent);
  background: linear-gradient(180deg, rgba(10,132,255,0.14), rgba(10,132,255,0.06));
  color: var(--text);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: transform 0.1s ease, background 0.15s;
  font-family: inherit;
}
.scan-button:active { transform: scale(0.99); background: rgba(10,132,255,0.24); }
.scan-icon { font-size: 32px; line-height: 1; }
.scan-text { font-size: 17px; font-weight: 600; }
.scan-sub { font-size: 12px; color: var(--text-dim); }

.scan-button.primary-scan { background: var(--accent); color: #fff; border-color: var(--accent); }
.scan-button.primary-scan .scan-sub { color: rgba(255,255,255,0.85); }

.scan-fallback {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 10px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 13px;
  text-decoration: underline;
  cursor: pointer;
}

.or-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 18px 0 10px;
  color: var(--text-dim);
  font-size: 12px;
}
.or-divider::before, .or-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.input-label { display: block; }
.input-title { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 6px; }
.input-hint { display: block; font-size: 12px; color: var(--text-dim); margin: 4px 0 8px; }
.input-hint b { color: var(--text); font-weight: 600; }

#serial-input {
  width: 100%;
  padding: 16px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 22px;
  font-weight: 500;
  letter-spacing: 0.06em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  outline: none;
}
#serial-input:focus { border-color: var(--accent); }

.warning {
  padding: 10px 12px;
  margin-top: 10px;
  border-radius: var(--radius);
  background: rgba(255, 204, 0, 0.12);
  border: 1px solid rgba(255, 204, 0, 0.35);
  color: var(--warning);
  font-size: 13px;
}

.scan-actions { margin-top: 14px; display: flex; gap: 10px; }

button.primary {
  flex: 1;
  padding: 16px 20px;
  border-radius: var(--radius);
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
}
button.primary:active { background: var(--accent-dim); }

button.secondary {
  padding: 12px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
}

button.danger {
  padding: 12px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--danger);
  background: transparent;
  color: var(--danger);
  font-size: 15px;
  cursor: pointer;
}

.recent-adds { margin-top: 24px; }
.recent-title { font-size: 13px; color: var(--text-dim); margin-bottom: 8px; }
#recent-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
#recent-list li {
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px;
}
#recent-list li .undo {
  background: transparent;
  border: none;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
}
#recent-list li .group-tag {
  font-size: 11px;
  color: var(--text-dim);
  font-family: var(--font);
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
}

/* -------- List view -------- */
.pill {
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
}
.search-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  margin-bottom: 12px;
  outline: none;
}
#serial-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#serial-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 15px;
}
#serial-list li.invalid { border-color: rgba(255, 204, 0, 0.5); }
#serial-list li .delete {
  background: transparent;
  border: none;
  color: var(--danger);
  font-size: 16px;
  padding: 4px 8px;
  cursor: pointer;
}
#serial-list li .meta {
  color: var(--text-dim);
  font-size: 11px;
  font-family: var(--font);
}

.empty {
  padding: 30px 20px;
  text-align: center;
  color: var(--text-dim);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* -------- Export view -------- */
.export-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 8px;
}
.export-row .name { flex: 1; font-weight: 500; }
.export-row .count { color: var(--text-dim); font-size: 13px; }
.export-row button {
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent);
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
}
.export-row button:disabled { opacity: 0.4; border-color: var(--border); color: var(--text-dim); cursor: not-allowed; }

/* -------- Settings -------- */
.setting {
  margin-bottom: 24px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.setting label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 8px;
}
.setting .hint { font-size: 12px; color: var(--text-dim); margin: 8px 0 0; }
.sync-url { display: flex; gap: 8px; }
.sync-url input {
  flex: 1;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.danger-zone { border-color: rgba(255, 59, 48, 0.3); }
.about { font-size: 13px; color: var(--text-dim); }
.about code { color: var(--text); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

/* -------- Bottom nav -------- */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: rgba(11, 11, 15, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  z-index: 10;
}
.bottom-nav button {
  background: transparent;
  border: none;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  padding: 8px 4px;
  font-family: inherit;
}
.bottom-nav .nav-icon { font-size: 20px; line-height: 1; }
.bottom-nav .nav-label { font-size: 11px; }
.bottom-nav button.active { color: var(--accent); }

/* -------- Modal -------- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 100;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  max-width: 440px;
  width: 100%;
}
.modal h3 { margin: 0 0 8px; font-size: 18px; }
.modal p { margin: 0 0 16px; color: var(--text-dim); font-size: 14px; }
.modal input {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
  margin-bottom: 12px;
}
.modal .actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.modal .actions button {
  padding: 10px 16px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 15px;
  font-weight: 500;
}
.modal .actions .cancel { background: var(--surface-2); color: var(--text); }
.modal .actions .confirm { background: var(--accent); color: #fff; }
.modal .actions .confirm.danger { background: var(--danger); }

/* -------- Live scanner (fullscreen) -------- */
.live-scanner {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 300;
  display: flex;
  flex-direction: column;
}
.live-scanner video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}
.live-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  color: #fff;
  pointer-events: none;
}
.live-header {
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: linear-gradient(180deg, rgba(0,0,0,0.6), transparent);
  display: flex;
  justify-content: space-between;
  align-items: center;
  pointer-events: auto;
}
.live-header .live-title { font-weight: 600; }
.live-header .live-close {
  background: rgba(0,0,0,0.4);
  border: 1px solid rgba(255,255,255,0.2);
  color: #fff;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 14px;
  cursor: pointer;
}
.live-body {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.live-viewfinder {
  width: 92%;
  max-width: 560px;
  aspect-ratio: 5 / 2;
  border: 2px solid rgba(255,255,255,0.8);
  border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.35);
  background: transparent;
  pointer-events: auto;
  cursor: pointer;
  transition: border-color 0.15s;
}
.live-viewfinder.has-candidate { border-color: var(--success); }
.live-viewfinder:active { border-color: var(--accent); }
.live-status {
  padding: 16px;
  padding-bottom: calc(16px + var(--safe-bottom));
  background: linear-gradient(180deg, transparent, rgba(0,0,0,0.7));
  text-align: center;
  font-size: 14px;
  pointer-events: auto;
}
.live-status .status-line { color: rgba(255,255,255,0.9); }
.live-status .status-hint { color: rgba(255,255,255,0.6); font-size: 12px; margin-top: 4px; }

.live-confirm {
  position: absolute;
  left: 50%;
  bottom: calc(72px + var(--safe-bottom));
  transform: translate(-50%, 20px);
  background: rgba(20, 22, 30, 0.94);
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: 16px;
  padding: 16px 18px;
  min-width: 300px;
  max-width: 94%;
  opacity: 0;
  transition: transform 0.2s ease, opacity 0.2s ease;
  pointer-events: none;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.live-confirm.visible {
  opacity: 1;
  transform: translate(-50%, 0);
  pointer-events: auto;
}
.live-confirm .detected-label {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 4px;
}
.live-confirm .detected-serial {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 26px;
  font-weight: 600;
  color: #fff;
  letter-spacing: 0.06em;
  margin-bottom: 12px;
  text-align: center;
}
.live-confirm .actions {
  display: flex;
  gap: 8px;
}
.live-confirm .actions button {
  flex: 1;
  padding: 12px;
  border-radius: 10px;
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.live-confirm .accept { background: var(--success); color: #000; }
.live-confirm .skip { background: rgba(255,255,255,0.12); color: #fff; }

.live-flash {
  position: absolute;
  inset: 0;
  background: rgba(48, 209, 88, 0.35);
  opacity: 0;
  pointer-events: none;
}
.live-flash.on { animation: flash 0.4s ease; }
@keyframes flash { 0% { opacity: 0.6; } 100% { opacity: 0; } }

.live-counter {
  position: absolute;
  top: calc(var(--safe-top) + 60px);
  right: 12px;
  background: rgba(0,0,0,0.55);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 13px;
  color: #fff;
}

.live-error {
  padding: 30px;
  color: #fff;
  text-align: center;
  font-size: 15px;
}

/* -------- OCR modal -------- */
.ocr-modal { max-width: 520px; }
.ocr-preview {
  width: 100%;
  max-height: 200px;
  object-fit: contain;
  background: #000;
  border-radius: 8px;
  margin-bottom: 12px;
}
.ocr-status {
  padding: 12px 14px;
  background: var(--surface-2);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.ocr-spinner {
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.ocr-candidates {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}
.ocr-chip {
  padding: 10px 14px;
  background: var(--surface-2);
  border: 1px solid var(--accent);
  color: var(--text);
  border-radius: 999px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 15px;
  cursor: pointer;
  font-weight: 500;
}
.ocr-chip.best { background: var(--accent); color: #fff; }
.ocr-chip:active { transform: scale(0.98); }
.ocr-raw {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: var(--text-dim);
  background: var(--surface-2);
  padding: 8px;
  border-radius: 6px;
  max-height: 80px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
}
.ocr-actions { margin-top: 6px; }
.ocr-actions button {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
}

/* -------- Toast -------- */
#toast-root {
  position: fixed;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 16px);
  left: 0; right: 0;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  z-index: 200;
  pointer-events: none;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  animation: toast-in 0.2s ease, toast-out 0.3s ease 2.5s forwards;
}
.toast.success { border-color: var(--success); color: var(--success); }
.toast.error { border-color: var(--danger); color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(-8px); } }
