:root {
  --bg: #f3efe6;
  --bg-2: #e7dfd0;
  --ink: #1c1914;
  --muted: #6b6458;
  --line: rgba(28, 25, 20, 0.12);
  --accent: #c45c26;
  --accent-2: #0f6e56;
  --warn: #9a3412;
  --panel: rgba(255, 251, 245, 0.88);
  --shadow: 0 18px 50px rgba(28, 25, 20, 0.12);
  --radius: 18px;
  --font: "DM Sans", sans-serif;
  --display: "Syne", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 10% -10%, #f7d9b8 0%, transparent 55%),
    radial-gradient(900px 500px at 100% 0%, #cfe8dc 0%, transparent 50%),
    linear-gradient(180deg, var(--bg), var(--bg-2));
}

a { color: inherit; text-decoration: none; }
button, input { font: inherit; }

.app-shell {
  width: min(980px, calc(100% - 2rem));
  margin: 0 auto;
  padding: 1.25rem 0 3rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.75rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.brand-mark {
  font-family: var(--display);
  font-weight: 800;
  background: var(--ink);
  color: #fff8ef;
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 0.7rem;
  display: grid;
  place-items: center;
  letter-spacing: -0.04em;
}

.brand-text {
  font-family: var(--display);
  font-weight: 700;
  letter-spacing: -0.03em;
}

.nav { display: flex; gap: 1rem; color: var(--muted); }
.nav a:hover { color: var(--ink); }

.scanner-copy h1,
.admin-page h1 {
  font-family: var(--display);
  font-size: clamp(2rem, 5vw, 3.2rem);
  line-height: 0.98;
  letter-spacing: -0.045em;
  margin: 0.2rem 0 0.8rem;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-size: 0.72rem;
  color: var(--accent);
  font-weight: 700;
  margin: 0;
}

.lede {
  color: var(--muted);
  max-width: 42rem;
  line-height: 1.55;
  margin: 0 0 1rem;
}

.meta-row { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-bottom: 1.25rem; }

.chip {
  display: inline-flex;
  align-items: center;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.45);
  border-radius: 999px;
  font-size: 0.85rem;
}

.chip-warn {
  color: var(--warn);
  border-color: rgba(154, 52, 18, 0.25);
  background: rgba(254, 215, 170, 0.45);
}

.scanner-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  border-radius: var(--radius);
  overflow: hidden;
  background:
    linear-gradient(135deg, rgba(28,25,20,0.08), rgba(28,25,20,0.02)),
    repeating-linear-gradient(
      -12deg,
      transparent,
      transparent 12px,
      rgba(28,25,20,0.03) 12px,
      rgba(28,25,20,0.03) 13px
    );
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}

.camera, .preview, .snapshot {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.stage-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 0.35rem;
  text-align: center;
  color: var(--muted);
  padding: 1rem;
}

.stage-empty strong {
  font-family: var(--display);
  font-size: 1.35rem;
  color: var(--ink);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
}

.btn {
  border: 1px solid var(--line);
  background: rgba(255,251,245,0.8);
  color: var(--ink);
  border-radius: 999px;
  padding: 0.7rem 1.1rem;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease, opacity 0.15s ease;
}

.btn:hover:not(:disabled) { transform: translateY(-1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary {
  background: var(--ink);
  color: #fff8ef;
  border-color: var(--ink);
}

.btn-accent {
  background: var(--accent);
  color: #fff8ef;
  border-color: var(--accent);
}

.btn-file { display: inline-flex; align-items: center; }

.status {
  min-height: 1.4rem;
  color: var(--muted);
  margin: 0.9rem 0 0;
}

.modal[hidden] { display: none; }
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: end center;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(20, 16, 12, 0.48);
  backdrop-filter: blur(4px);
  animation: fadeIn 0.2s ease;
}

.modal-panel {
  position: relative;
  width: min(920px, 100%);
  max-height: min(88vh, 900px);
  overflow: auto;
  background: var(--panel);
  border-radius: 24px 24px 0 0;
  border: 1px solid var(--line);
  border-bottom: 0;
  box-shadow: var(--shadow);
  padding: 1.2rem 1.2rem 1.6rem;
  animation: slideUp 0.28s ease;
}

.modal-head {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: start;
  margin-bottom: 0.8rem;
}

.modal-head h2 {
  font-family: var(--display);
  margin: 0.15rem 0 0;
  letter-spacing: -0.03em;
}

.icon-btn {
  border: 0;
  background: transparent;
  font-size: 1.8rem;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
}

.modal-query {
  color: var(--muted);
  margin-bottom: 1rem;
  line-height: 1.45;
}

.match-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 0.9rem;
}

.match-card {
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  background: rgba(255,255,255,0.55);
  animation: riseIn 0.35s ease both;
}

.match-card:nth-child(2) { animation-delay: 0.05s; }
.match-card:nth-child(3) { animation-delay: 0.1s; }
.match-card:nth-child(4) { animation-delay: 0.15s; }

.match-visual {
  position: relative;
  aspect-ratio: 4 / 3;
  background: #ddd4c4;
  overflow: hidden;
}

.match-visual img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transform-origin: center;
}

.bbox-frame {
  position: absolute;
  border: 2px solid #fff;
  box-shadow: 0 0 0 2px rgba(196, 92, 38, 0.9);
  pointer-events: none;
}

.match-body { padding: 0.85rem 0.9rem 1rem; }
.match-code {
  font-family: var(--display);
  font-size: 1.45rem;
  letter-spacing: -0.03em;
  margin: 0;
}
.match-meta {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin: 0.45rem 0;
  color: var(--muted);
  font-size: 0.86rem;
}
.match-reason { margin: 0; color: var(--muted); font-size: 0.92rem; line-height: 1.4; }

.item-block {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(255,255,255,0.5);
  padding: 0.85rem;
  margin-bottom: 0.9rem;
}
.item-block-head {
  display: grid;
  grid-template-columns: 88px 1fr;
  gap: 0.8rem;
  align-items: center;
  margin-bottom: 0.75rem;
}
.item-thumb {
  width: 88px;
  height: 88px;
  border-radius: 12px;
  overflow: hidden;
  background: #ddd4c4;
}
.item-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.item-matches {
  margin-top: 0.25rem;
}

.score-pill {
  background: rgba(15, 110, 86, 0.12);
  color: var(--accent-2);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  font-weight: 700;
}

.modal-empty { color: var(--muted); }

.admin-page .stats-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.7rem;
  margin: 1.2rem 0;
}

.stat {
  background: rgba(255,251,245,0.7);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.9rem;
}

.stat span { display: block; color: var(--muted); font-size: 0.82rem; }
.stat strong {
  font-family: var(--display);
  font-size: 1.7rem;
  letter-spacing: -0.03em;
}

.provider-box {
  margin: 0 0 1.25rem;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(255,251,245,0.75);
}
.provider-label {
  display: block;
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 0.45rem;
}
.provider-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  align-items: center;
}
.provider-select {
  flex: 1;
  min-width: 220px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.65rem 1rem;
  background: #fff;
  color: var(--ink);
  font: inherit;
}
.provider-hint {
  margin: 0.55rem 0 0;
  font-size: 0.86rem;
  color: var(--muted);
  line-height: 1.45;
}

.page-label-input {
  display: block;
  width: min(420px, 100%);
  margin: 0.6rem 0 1rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.7rem 1rem;
  background: rgba(255,251,245,0.85);
}

.sheet-upload {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  margin: 0.4rem 0 0.5rem;
}

.sheet-list-wrap { margin: 1rem 0 1.5rem; }
.sheet-list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.7rem;
}

.sheet-list {
  display: grid;
  gap: 0.65rem;
}

.sheet-row {
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 0.8rem;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255,251,245,0.7);
  padding: 0.65rem;
}

.sheet-thumb {
  width: 92px;
  height: 92px;
  border-radius: 10px;
  overflow: hidden;
  background: #ddd4c4;
}
.sheet-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.sheet-fields {
  display: grid;
  gap: 0.4rem;
}
.sheet-fields label {
  display: grid;
  gap: 0.2rem;
  font-size: 0.8rem;
  color: var(--muted);
}
.sheet-fields input {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.45rem 0.65rem;
  background: #fff;
  color: var(--ink);
  font-size: 0.95rem;
}

@media (max-width: 720px) {
  .sheet-row { grid-template-columns: 72px 1fr; }
  .sheet-thumb { width: 72px; height: 72px; }
}

.path-note, .token-form label { color: var(--muted); }
.path-note code {
  background: rgba(255,255,255,0.55);
  padding: 0.15rem 0.4rem;
  border-radius: 6px;
}

.token-row { display: flex; gap: 0.6rem; margin-top: 0.45rem; }
.token-row input {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.7rem 1rem;
  background: rgba(255,251,245,0.85);
}

.log-box {
  margin-top: 1rem;
  max-height: 280px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255,251,245,0.65);
  padding: 0.8rem 1rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.82rem;
  white-space: pre-wrap;
}

.section-title {
  font-family: var(--display);
  margin: 1.5rem 0 0.6rem;
}

.error-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 0.55rem;
}

.error-list li {
  border: 1px solid rgba(154, 52, 18, 0.18);
  background: rgba(254, 215, 170, 0.28);
  border-radius: 12px;
  padding: 0.7rem 0.85rem;
}

.error-list span { display: block; color: var(--muted); margin-top: 0.2rem; }

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(24px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@keyframes riseIn {
  from { transform: translateY(10px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@media (max-width: 720px) {
  .admin-page .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .modal { place-items: end stretch; }
  .modal-panel { border-radius: 20px 20px 0 0; }
}
