/* Box Search — clean & simple mobile UI.
   Soft elevated cards on an off-white canvas, indigo for primary actions,
   Inter for type. One-hand iPhone use: thumb-zone actions, 44pt targets.
   See docs/DESIGN.md. The whole UI restyles from the tokens in :root. */

/* Inter (vendored, latin variable, BSD-licensed) with the system stack as a
   non-blocking fallback so first paint never waits on the font. */
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url('/fonts/inter-variable-latin.woff2') format('woff2');
}

:root {
  /* color */
  --bg: #f7f8fb;
  --surface: #ffffff;
  --surface-alt: #eef0f4; /* filled inputs, placeholders */
  --ink: #16181d;
  --ink-soft: #6b7280;
  --ink-faint: #9aa0ab;
  --line: #e6e8ee; /* hairlines, used sparingly */
  --accent: #4f46e5;
  --accent-press: #4338ca;
  --accent-soft: #eef0fe; /* tints, match highlight */
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --warn-soft: #fff7ed;
  --warn-ink: #9a5b00;
  --ok: #059669;
  --ok-soft: #ecfdf5;

  /* depth — layered, low-opacity shadows replace most hard borders */
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.10);
  --shadow-md: 0 4px 16px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.16);

  /* shape */
  --radius: 16px;
  --radius-sm: 10px;
  --radius-pill: 999px;

  /* rhythm (8px scale) */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s5: 24px;
  --s6: 32px;
  --pad: 16px;
  --tap: 44px; /* Apple HIG minimum */

  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
  font-feature-settings: 'cv02', 'cv03', 'cv04', 'cv11'; /* Inter: cleaner glyphs */
  letter-spacing: -0.011em;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + var(--pad)) var(--pad)
           calc(env(safe-area-inset-bottom) + 108px); /* room for the floating bar */
}

h1 { font-size: 28px; line-height: 1.2; font-weight: 700; letter-spacing: -0.02em; margin: 0 0 var(--s3); }
h2 { font-size: 19px; line-height: 1.3; font-weight: 650; letter-spacing: -0.015em; margin: var(--s5) 0 var(--s2); }
p  { margin: var(--s2) 0; }

.muted { color: var(--ink-soft); }
.small { font-size: 14px; }

/* --- buttons ------------------------------------------------------------- */

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  letter-spacing: -0.01em;
  text-decoration: none;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}
button:active, .btn:active { transform: scale(0.98); background: var(--surface-alt); }

.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 1px 2px rgba(79, 70, 229, 0.30), 0 6px 16px rgba(79, 70, 229, 0.24);
}
.btn-primary:active { background: var(--accent-press); }

.btn-danger {
  color: var(--danger);
  border-color: transparent;
  background: var(--danger-soft);
  box-shadow: none;
}

.btn-big {
  width: 100%;
  min-height: 54px;
  font-size: 17px;
}

.btn-icon {
  min-width: var(--tap);
  min-height: var(--tap);
  padding: 0;
  border: none;
  background: none;
  box-shadow: none;
  color: var(--ink-soft);
  font-size: 20px;
}
.btn-icon:active { background: var(--surface-alt); transform: scale(0.94); }

/* --- forms ---------------------------------------------------------------- */

label { display: block; font-weight: 600; font-size: 14px; color: var(--ink-soft); margin: var(--s4) 0 var(--s1); }

input[type="text"], input[type="search"], input[type="number"],
input[type="email"], input[type="password"], select {
  width: 100%;
  min-height: var(--tap);
  padding: 11px 14px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 16px; /* prevents iOS zoom-on-focus */
  background: var(--surface-alt);
  color: var(--ink);
  -webkit-appearance: none;
  appearance: none;
}
input::placeholder { color: var(--ink-faint); }
input:focus, select:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Home/Search "find anything" field → a rounded pill with a leading magnifier
   (inline SVG data-URI keeps it CSS-only; no markup change, strict-CSP safe). */
input[type="search"] {
  border-radius: var(--radius-pill);
  padding-left: 42px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 15px center;
}

/* ALTCHA login widget: match the full-width inputs and sit flush with them. */
altcha-widget {
  display: block;
  width: 100%;
  --altcha-max-width: 100%;
  --altcha-border-radius: var(--radius-sm);
  margin: var(--s4) 0;
}

/* --- floating bottom action bar (thumb zone) ------------------------------- */

.action-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  gap: 10px;
  max-width: 640px;
  margin: 0 auto;
  padding: 10px var(--pad) calc(env(safe-area-inset-bottom) + 10px);
  background: rgba(247, 248, 251, 0.82);
  backdrop-filter: saturate(160%) blur(14px);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
}
.action-bar::before {
  /* hairline that fades, instead of a hard top border */
  content: '';
  position: absolute;
  top: 0; left: var(--pad); right: var(--pad);
  height: 1px;
  background: var(--line);
}
.action-bar > * { flex: 1; }

/* --- bottom tab bar (primary navigation) ----------------------------------- */

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 20;
  display: flex;
  max-width: 640px;
  margin: 0 auto;
  padding: 8px 4px calc(env(safe-area-inset-bottom) + 6px);
  background: rgba(247, 248, 251, 0.85);
  backdrop-filter: saturate(160%) blur(14px);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
}
.tabbar[hidden] { display: none; }
.tabbar::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 1px;
  background: var(--line);
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 4px 0;
  text-decoration: none;
  color: var(--ink-faint);
  font-size: 11px;
  font-weight: 600;
}
.tab svg { width: 25px; height: 25px; display: block; }
/* Outline (Phosphor Regular) when idle, solid (Fill) when active — iOS pattern. */
.tab .icon-fill { display: none; }
.tab.active .icon-out { display: none; }
.tab.active .icon-fill { display: block; }
.tab.active { color: var(--accent); }
.tab.active svg { transform: translateY(-1px); } /* subtle lift on the active tab */
.tab:active { color: var(--accent-press); }

/* --- sticky top back-bar (pushed/deep screens) ----------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 40px 1fr 40px;
  align-items: center;
  /* bleed past #app's safe-area + side padding so it spans the column edge-to-edge */
  margin: calc(-1 * (env(safe-area-inset-top) + var(--pad))) calc(-1 * var(--pad)) var(--s3);
  padding: calc(env(safe-area-inset-top) + 4px) 8px 8px;
  background: rgba(247, 248, 251, 0.82);
  backdrop-filter: saturate(160%) blur(14px);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
}
.topbar::after {
  content: '';
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 1px;
  background: var(--line);
}
.topbar-back {
  min-width: 40px; min-height: 40px;
  padding: 0;
  border: none;
  background: none;
  box-shadow: none;
  color: var(--accent);
}
.topbar-back svg { width: 26px; height: 26px; }
.topbar-back:active { background: var(--surface-alt); border-radius: 50%; transform: scale(0.92); }
.topbar-title {
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -0.01em;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* --- inline button row (e.g. Edit / Print QR on box detail) ----------------- */

.btn-row {
  display: flex;
  gap: 10px;
  margin: var(--s3) 0;
}
.btn-row > * { flex: 1; }

/* --- cards / lists ---------------------------------------------------------- */

.card {
  display: flex;
  gap: var(--s3);
  align-items: center;
  width: 100%;
  padding: var(--s3) var(--s4);
  margin: var(--s2) 0;
  border: none;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  text-align: left;
  text-decoration: none;
  color: inherit;
  transition: transform 120ms ease, box-shadow 120ms ease;
}
.card:active { transform: scale(0.985); box-shadow: var(--shadow-md); }

.card img.thumb,
.thumb-placeholder {
  width: 60px;
  height: 60px;
  flex: none;
  border-radius: var(--radius-sm);
  object-fit: cover;
  background: var(--surface-alt);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
}

.card .card-body { min-width: 0; flex: 1; }
.card .card-title { font-weight: 650; letter-spacing: -0.01em; }
.card .card-sub { color: var(--ink-soft); font-size: 14px; margin-top: 2px; }
.match {
  background: var(--accent-soft);
  color: var(--accent-press);
  border-radius: 6px;
  padding: 1px 6px;
  font-weight: 550;
}

/* --- photo strip -------------------------------------------------------------- */

.photo-strip {
  display: flex;
  gap: var(--s2);
  overflow-x: auto;
  padding: var(--s2) 0;
  -webkit-overflow-scrolling: touch;
}
.photo-strip img {
  height: 110px;
  border-radius: var(--radius-sm);
  flex: none;
  cursor: zoom-in;
  box-shadow: var(--shadow-sm);
}

/* --- photo lightbox ----------------------------------------------------------- */

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.lightbox-close {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 10px);
  right: 14px;
  min-width: var(--tap);
  min-height: var(--tap);
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.16);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: none;
  color: #fff;
  font-size: 26px;
}

.lightbox-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  min-width: var(--tap);
  min-height: var(--tap);
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.16);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: none;
  color: #fff;
  font-size: 30px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lightbox-nav:active { transform: translateY(-50%) scale(0.94); }
.lightbox-prev { left: calc(env(safe-area-inset-left) + 8px); }
.lightbox-next { right: calc(env(safe-area-inset-right) + 8px); }

.lightbox-count {
  position: absolute;
  bottom: calc(env(safe-area-inset-bottom) + 14px);
  left: 0;
  right: 0;
  text-align: center;
  color: #fff;
  font-size: 14px;
}

/* --- item rows ------------------------------------------------------------------ */

.item-row {
  display: flex;
  gap: var(--s2);
  align-items: center;
  padding: var(--s2) 0;
  border-bottom: 1px solid var(--line);
}
.item-row input.item-name { flex: 2; }
.item-row input.item-cat { flex: 1; }
.item-row .src-badge {
  flex: none;
  font-size: 12px;
  color: var(--ink-soft);
  width: 24px;
  text-align: center;
}

/* --- misc ------------------------------------------------------------------------ */

.spinner {
  display: inline-block;
  width: 22px; height: 22px;
  border: 3px solid var(--surface-alt);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.banner {
  padding: var(--s3) var(--s4);
  margin: var(--s3) 0;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
}
.banner-error { background: var(--danger-soft); color: var(--danger); }
.banner-warn  { background: var(--warn-soft); color: var(--warn-ink); }
.banner-ok    { background: var(--ok-soft); color: var(--ok); }

.qr-preview { width: 100%; max-width: 360px; display: block; margin: var(--s3) auto; }

.label-preview {
  display: block;
  width: 320px;
  max-width: 100%;
  margin: var(--s2) auto;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  image-rendering: pixelated; /* preview the 1-bit art crisply */
}

.empty-state {
  text-align: center;
  color: var(--ink-soft);
  padding: var(--s6) var(--s4);
}

/* --- identity / admin -------------------------------------------------------- */

.top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s2);
}

.inline-form {
  display: flex;
  gap: var(--s2);
  margin: var(--s3) 0;
}
.inline-form input { flex: 1; }

.user-card {
  border: none;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  padding: var(--s4);
  margin: var(--s3) 0;
  display: grid;
  gap: var(--s2);
}

.check-row {
  display: flex;
  align-items: center;
  gap: var(--s2);
  min-height: 32px;
  font-weight: 400;
  margin: 0;
}
.check-row input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }

.scan-video {
  width: 100%;
  border-radius: var(--radius);
  background: #000;
  box-shadow: var(--shadow-sm);
}

a { color: var(--accent); }
hr { border: none; border-top: 1px solid var(--line); margin: var(--s5) 0; }

/* Map view: Leaflet needs an explicit container height, and the emoji pin must
   shed Leaflet's default white box/border on .leaflet-div-icon. */
/* z-index:0 keeps Leaflet's internal panes/controls (which climb to ~1000) in
   their own stacking context, so the map never paints over the lightbox, the
   floating action bar, or any other overlay. overflow clips tiles to the radius. */
#map-box,
.box-map {
  position: relative;
  z-index: 0;
  overflow: hidden;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
#map-box {
  height: 70vh;
  min-height: 320px;
  margin: var(--s3) 0;
}
.box-map {
  height: 220px;
  margin: var(--s2) 0;
}
.leaflet-div-icon.map-pin {
  background: transparent;
  border: none;
  font-size: 22px;
  line-height: 1;
  text-align: center;
}

/* Honor reduced-motion: keep state feedback, drop movement. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
  .spinner { animation-duration: 0.8s !important; }
  button:active, .btn:active, .card:active { transform: none; }
}
