/* SPDX-License-Identifier: AGPL-3.0-or-later
 * Copyright (C) 2026 CRC Group LTD */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --danger: #b91c1c;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 4px 12px rgb(0 0 0 / 0.05);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f14;
    --surface: #161b22;
    --text: #e6e8eb;
    --muted: #9aa3ad;
    --border: #2a313a;
    --accent: #14b8a6;
    --accent-text: #04201d;
    --danger: #f87171;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

main {
  max-width: 720px;
  margin: 0 auto;
  /* Extra bottom room keeps the last row clear of the iPhone Home indicator. */
  padding: 0 max(16px, env(safe-area-inset-right)) calc(64px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; }
a { color: var(--accent); }

.loading, .center { text-align: center; }
.loading { margin-top: 40vh; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.block { display: block; }
.grow { flex: 1; min-width: 0; }

/* ---- Top bar ---- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 0 10px;
  margin-bottom: 8px;
  background: var(--bg);
}
.topbar-title { flex: 1; font-size: 1.35rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-actions { display: flex; gap: 6px; }
.topbar-spacer { width: 0; }

/* ---- Buttons ---- */
.btn, .btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn:disabled, .btn-icon:disabled { opacity: 0.5; cursor: default; }
.btn-primary, .btn-icon-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-danger { color: var(--danger); }
.btn-quiet { border-color: transparent; background: transparent; }
.btn-wide { width: 100%; }
.btn-icon { width: 44px; padding: 0; }
.btn-icon.is-on .icon-star { background: #f59e0b; }

/* ---- Icons (CSS masks, no icon font) ---- */
.icon { display: inline-block; width: 22px; height: 22px; background: currentColor; mask: var(--i) center / contain no-repeat; -webkit-mask: var(--i) center / contain no-repeat; }
.icon-back { --i: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M15 18l-6-6 6-6'/%3E%3C/svg%3E"); }
.icon-add { --i: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M12 5v14M5 12h14'/%3E%3C/svg%3E"); }
.icon-settings { --i: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='M4 7h10M18 7h2M4 17h4M12 17h8'/%3E%3Ccircle cx='16' cy='7' r='2'/%3E%3Ccircle cx='10' cy='17' r='2'/%3E%3C/svg%3E"); }
.icon-import { --i: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M12 4v11M7 10l5 5 5-5M5 20h14'/%3E%3C/svg%3E"); }
.icon-star { --i: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 3l2.8 5.7 6.2.9-4.5 4.4 1.1 6.2L12 17.3 6.4 20.2l1.1-6.2L3 9.6l6.2-.9z'/%3E%3C/svg%3E"); }
.icon-edit { --i: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 20h4L19 9l-4-4L4 16z'/%3E%3C/svg%3E"); }
.icon-camera { --i: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linejoin='round'%3E%3Cpath d='M4 8h3l2-3h6l2 3h3v11H4z'/%3E%3Ccircle cx='12' cy='13' r='3.5'/%3E%3C/svg%3E"); }
.icon-image { --i: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linejoin='round'%3E%3Crect x='4' y='5' width='16' height='14' rx='2'/%3E%3Cpath d='M4 16l5-5 4 4 2-2 5 5'/%3E%3C/svg%3E"); }

/* ---- Forms ---- */
.stack { display: flex; flex-direction: column; gap: 14px; }
.stack-tight { display: flex; flex-direction: column; gap: 6px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-weight: 600; font-size: 0.9rem; }
.hint { color: var(--muted); }
input, select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* 16px stops iOS zooming into fields */
}
input[type="checkbox"] { width: 22px; min-height: 22px; height: 22px; margin: 0; accent-color: var(--accent); }
input[type="color"] { padding: 4px; height: 44px; }
.check { display: flex; gap: 10px; align-items: center; min-height: 44px; }
.form-error { color: var(--danger); margin: 0; min-height: 0; }
.form-error:empty { display: none; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow); }
.panel.highlight { border-color: var(--accent); }
details.panel > summary { cursor: pointer; font-weight: 600; min-height: 28px; }
details.panel[open] > summary { margin-bottom: 12px; }

.banner { background: #fef3c7; color: #78350f; border-radius: 10px; padding: 10px 12px; font-size: 0.9rem; }

/* ---- Auth ---- */
.auth { max-width: 380px; margin: 0 auto; padding-top: calc(48px + env(safe-area-inset-top)); display: flex; flex-direction: column; gap: 16px; text-align: center; }
.auth form { text-align: left; }
.auth-logo { align-self: center; border-radius: 18px; }
.divider { color: var(--muted); font-size: 0.85rem; margin: 4px 0; }

/* ---- Card list ---- */
.search-wrap { margin-bottom: 14px; }
.section-title { margin: 18px 0 10px; color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.tile {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 10px;
  /* A fixed height, not aspect-ratio: in iOS Safari, grid rows sized by
     aspect-ratio could end up shorter than the tiles, so the page stopped
     scrolling before the last row. Roughly a card's shape on a phone. */
  height: clamp(96px, 28vw, 140px);
  padding: 12px;
  border-radius: var(--radius);
  background: var(--card-color);
  color: #fff;
  text-decoration: none;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.tile-logo { width: 44px; height: 44px; border-radius: 10px; background: #fff; object-fit: contain; padding: 4px; }
.logo-initials { display: inline-flex; align-items: center; justify-content: center; color: #111; font-weight: 700; font-size: 0.95rem; }
.tile-name { font-weight: 700; text-shadow: 0 1px 2px rgb(0 0 0 / 0.35); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-badge { position: absolute; top: 10px; right: 10px; font-size: 0.7rem; background: rgb(0 0 0 / 0.35); padding: 2px 8px; border-radius: 99px; }
.empty { text-align: center; padding: 48px 0; }
.empty .row { justify-content: center; }

/* ---- Card view ---- */
.card-face { border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); margin-bottom: 12px; }
.card-head { display: flex; align-items: center; gap: 12px; padding: 14px 16px; background: var(--card-color); color: #fff; font-weight: 700; font-size: 1.1rem; }
.card-logo { width: 48px; height: 48px; border-radius: 10px; background: #fff; object-fit: contain; padding: 4px; }
/* Always black on white, even in dark mode: scanners need the contrast. */
.card-body { background: #fff; color: #000; padding: 20px 16px 16px; text-align: center; }
.card-number { margin: 12px 0 0; font: 600 1.25rem/1.3 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 0.06em; word-break: break-all; }
.barcode { display: flex; justify-content: center; }
.barcode svg { display: block; width: 100%; height: auto; }
.barcode-1d svg { max-height: 40vh; }
.barcode-2d svg { width: min(78vw, 340px); }
.barcode-error { color: #b91c1c; padding: 20px; }
.notes { white-space: pre-wrap; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.preview { background: #fff; border-radius: 10px; padding: 12px; }
.preview:empty { display: none; }

/* ---- Logos ---- */
.logo-thumb { width: 48px; height: 48px; object-fit: contain; background: #fff; border-radius: 8px; padding: 4px; border: 1px solid var(--border); }
.logo-results { display: flex; flex-wrap: wrap; gap: 8px; }
.logo-option { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); font: inherit; cursor: pointer; max-width: 120px; }
.logo-option small { font-size: 0.7rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

/* ---- Import review ---- */
.review { display: flex; flex-direction: column; gap: 10px; }
.review-row { display: flex; gap: 10px; align-items: flex-start; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px; }
.review-row.is-off { opacity: 0.5; }
.review-thumb { width: 64px; height: 96px; object-fit: cover; border-radius: 8px; flex: none; }
.review-msg { color: var(--muted); }
.review-msg.is-error { color: var(--danger); }

/* ---- Lists ---- */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.list-row { display: flex; gap: 10px; align-items: center; }
.tag { margin-left: 6px; font-size: 0.7rem; background: var(--accent); color: var(--accent-text); padding: 1px 6px; border-radius: 6px; vertical-align: middle; }
.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-items { position: absolute; right: 0; z-index: 3; display: flex; flex-direction: column; gap: 6px; padding: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 24px rgb(0 0 0 / 0.15); min-width: 160px; }

/* ---- Scanner ---- */
.scan-overlay { position: fixed; inset: 0; z-index: 20; background: #000; display: flex; align-items: center; justify-content: center; }
.scan-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scan-frame { position: relative; width: min(80vw, 420px); aspect-ratio: 1.6; border: 3px solid rgb(255 255 255 / 0.85); border-radius: 16px; box-shadow: 0 0 0 100vmax rgb(0 0 0 / 0.35); }
.scan-msg { position: absolute; top: calc(24px + env(safe-area-inset-top)); left: 16px; right: 16px; text-align: center; color: #fff; font-weight: 600; text-shadow: 0 1px 3px #000; }
.scan-close { position: absolute; bottom: calc(24px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); min-width: 160px; }

.source-footer { margin: 24px 0 8px; text-align: center; font-size: 0.8rem; color: var(--muted); }

/* ---- Toast ---- */
.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 30; max-width: calc(100% - 32px); padding: 12px 16px; border-radius: 12px; background: #111827; color: #fff; font-weight: 600; box-shadow: 0 8px 24px rgb(0 0 0 / 0.2); transition: opacity 0.4s; }
.toast-error { background: #991b1b; }
.toast-hide { opacity: 0; }
