:root {
  --parchment: #f5edd6;
  --parchment-dark: #e8dcc0;
  --parchment-deep: #d4c5a3;
  --ink: #2c2416;
  --ink-light: #5a4d3a;
  --ink-faint: #8a7d6a;
  --accent: #8b2500;
  --accent-light: #a83a15;
  --gold: #b8860b;
  --gold-light: #daa520;
  --emerald: #2d6a4f;
  --emerald-light: #40916c;
  --red: #9b2226;
  --radius: 6px;
  --font-display: 'Uncial Antiqua', cursive;
  --font-body: 'Crimson Pro', Georgia, serif;
  --font-mono: 'Fira Code', monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font-body);
  background: var(--parchment);
  color: var(--ink);
  min-height: 100dvh;
  overflow-x: hidden;
}

/* Parchment texture */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background: url("data:image/svg+xml,%3Csvg viewBox='0 0 512 512' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='5' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.06'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 0;
}

/* Header */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1rem;
  background: var(--ink);
  position: sticky;
  top: 0;
  z-index: 100;
}
header h1 {
  font-family: var(--font-display);
  font-size: 1.3rem;
  color: var(--gold-light);
  font-weight: 400;
  letter-spacing: 0.03em;
}
nav { display: flex; gap: 0.3rem; }
.nav-btn {
  background: transparent;
  border: 1px solid rgba(218,165,32,0.2);
  color: var(--parchment-deep);
  padding: 0.35rem 0.8rem;
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.82rem;
  font-weight: 600;
  border-radius: 4px;
  transition: all 0.2s;
}
.nav-btn.active {
  background: rgba(218,165,32,0.15);
  color: var(--gold-light);
  border-color: rgba(218,165,32,0.4);
}
.nav-btn:hover:not(.active) { border-color: rgba(218,165,32,0.3); }

/* Views */
.view { display: none; padding: 0.8rem; position: relative; z-index: 1; }
.view.active { display: block; }
.hidden { display: none !important; }

/* Camera */
#camera-container {
  position: relative;
  width: 100%;
  max-width: 440px;
  margin: 0 auto;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  aspect-ratio: 3/4;
  border: 2px solid var(--parchment-deep);
}
#camera {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
#camera-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.card-frame {
  position: absolute;
  top: 8%;
  left: 8%;
  right: 8%;
  bottom: 8%;
  border: 1.5px solid rgba(218,165,32,0.3);
  border-radius: 10px;
}

#preview-container {
  width: 100%;
  max-width: 440px;
  margin: 0 auto;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--parchment-dark);
  text-align: center;
  border: 1px solid var(--parchment-deep);
}
#preview-img {
  max-width: 100%;
  max-height: 500px;
  border-radius: var(--radius);
}

#camera-controls {
  display: flex;
  gap: 0.8rem;
  justify-content: center;
  padding: 1rem;
  max-width: 440px;
  margin: 0 auto;
}

/* Buttons */
.btn-primary, .btn-secondary {
  padding: 0.55rem 1.2rem;
  border-radius: 4px;
  font-family: var(--font-body);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all 0.2s;
  display: inline-block;
  text-align: center;
}
.btn-primary {
  background: var(--accent);
  color: var(--parchment);
  box-shadow: 0 2px 6px rgba(139,37,0,0.2);
}
.btn-primary:hover { background: var(--accent-light); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary {
  background: transparent;
  color: var(--ink-light);
  border: 1px solid var(--parchment-deep);
}
.btn-secondary:hover { border-color: var(--ink-faint); color: var(--ink); }

/* Result Panel */
#result-panel { max-width: 440px; margin: 0.5rem auto; }

/* Scroll frame */
.scroll-frame {
  background: var(--parchment);
  border: 2px solid var(--parchment-deep);
  border-radius: var(--radius);
  position: relative;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(44,36,22,0.1), inset 0 0 40px rgba(44,36,22,0.04);
}
.scroll-frame::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: linear-gradient(90deg, transparent, var(--gold), var(--gold-light), var(--gold), transparent);
  opacity: 0.6;
  z-index: 1;
}

.result-card {
  overflow: hidden;
}

/* Side-by-side images */
.result-images {
  display: flex;
  gap: 0.5rem;
  padding: 0.7rem 0.7rem 0.5rem;
  background: linear-gradient(180deg, rgba(44,36,22,0.04), transparent);
}
.result-image-scan,
.result-image-match {
  flex: 1;
  text-align: center;
}
.result-image-scan img,
.result-image-match img {
  width: 100%;
  border-radius: 4px;
  aspect-ratio: 488/680;
  object-fit: cover;
  border: 1px solid var(--parchment-deep);
  box-shadow: 2px 3px 8px rgba(44,36,22,0.15);
}
.image-label {
  display: block;
  font-size: 0.65rem;
  color: var(--ink-faint);
  font-style: italic;
  margin-top: 0.25rem;
  letter-spacing: 0.03em;
}

/* Card info */
.result-info {
  padding: 0.7rem;
}
.result-info h2 {
  font-family: var(--font-body);
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--ink);
  line-height: 1.15;
  margin-bottom: 0.15rem;
}
.text-muted {
  color: var(--ink-faint);
  font-size: 0.82rem;
  margin-bottom: 0.1rem;
  font-style: italic;
}

/* Ornamental divider */
.ornament {
  text-align: center;
  margin: 0.4rem 0;
  color: var(--parchment-deep);
  font-size: 0.9rem;
  letter-spacing: 0.3em;
  user-select: none;
}

/* Prices */
.price-row {
  display: flex;
  border: 1px solid var(--parchment-deep);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 0.5rem;
}
.price-tag {
  flex: 1;
  text-align: center;
  padding: 0.4rem 0.3rem;
  border-right: 1px solid var(--parchment-deep);
}
.price-tag:last-child { border-right: none; }
.price-label {
  display: block;
  font-size: 0.6rem;
  color: var(--ink-faint);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-family: var(--font-mono);
  margin-bottom: 0.1rem;
}
.price-value {
  font-family: var(--font-mono);
  font-size: 1rem;
  font-weight: 500;
  color: var(--emerald);
}
.price-foil { color: var(--accent); }

/* Form controls */
.form-compact {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.foil-check {
  font-size: 0.82rem;
  color: var(--ink-light);
  display: flex;
  align-items: center;
  gap: 0.25rem;
  cursor: pointer;
}
select, input[type="number"] {
  font-family: var(--font-body);
  background: var(--parchment);
  border: 1px solid var(--parchment-deep);
  color: var(--ink);
  padding: 0.35rem 0.5rem;
  border-radius: 4px;
  font-size: 0.85rem;
  outline: none;
}
select:focus, input[type="number"]:focus {
  border-color: var(--gold);
}
input[type="number"] { width: 2.8rem; text-align: center; }
input[type="checkbox"] { accent-color: var(--accent); }

.result-actions {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.6rem;
}

/* Printings */
#printings-panel {
  background: var(--parchment);
  border: 2px solid var(--parchment-deep);
  border-radius: var(--radius);
  padding: 1rem;
  margin-top: 0.8rem;
  box-shadow: 0 1px 3px rgba(44,36,22,0.1);
}
#printings-panel h3 {
  margin-bottom: 0.8rem;
  font-family: var(--font-body);
  font-weight: 700;
}
#printings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 0.5rem;
  max-height: 350px;
  overflow-y: auto;
  margin-bottom: 0.8rem;
}
.printing-item {
  background: var(--parchment);
  border: 1px solid var(--parchment-deep);
  border-radius: 4px;
  padding: 0.4rem;
  cursor: pointer;
  text-align: center;
  transition: all 0.2s;
}
.printing-item:hover {
  border-color: var(--gold);
  box-shadow: 1px 2px 6px rgba(44,36,22,0.1);
}
.printing-item.selected { border-color: var(--emerald); border-width: 2px; }
.printing-item img {
  width: 100%;
  border-radius: 3px;
  margin-bottom: 0.25rem;
  border: 1px solid var(--parchment-deep);
}
.printing-item .set-label { font-size: 0.65rem; color: var(--ink-faint); font-style: italic; }

/* Spinner */
.spinner {
  width: 30px;
  height: 30px;
  border: 2px solid var(--parchment-deep);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 2rem auto 0.8rem;
}
@keyframes spin { to { transform: rotate(360deg); } }
#result-loading { text-align: center; padding: 3rem 2rem; }
#result-loading p {
  color: var(--ink-faint);
  font-size: 1rem;
  font-style: italic;
}

/* Inventory */
.inventory-header {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: baseline;
  margin-bottom: 0.5rem;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--parchment-deep);
}
.stats-bar {
  display: flex;
  gap: 1rem;
  flex: 1;
  font-size: 0.9rem;
}
.stats-bar span {
  color: var(--ink-faint);
  font-style: italic;
}
.stats-bar span:first-child {
  color: var(--ink);
  font-weight: 700;
  font-style: normal;
}
#search-input {
  font-family: var(--font-body);
  background: var(--parchment);
  border: 1px solid var(--parchment-deep);
  color: var(--ink);
  padding: 0.4rem 0.7rem;
  border-radius: 4px;
  font-size: 0.85rem;
  width: 160px;
  outline: none;
  font-style: italic;
}
#search-input:focus { border-color: var(--gold); }
#search-input::placeholder { color: var(--ink-faint); }
#sort-select {
  font-family: var(--font-body);
  background: var(--parchment);
  border: 1px solid var(--parchment-deep);
  color: var(--ink);
  padding: 0.4rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8rem;
}

/* Inventory cards */
#inventory-list {
  display: grid;
  gap: 0.4rem;
}
.inv-card {
  display: flex;
  gap: 0.6rem;
  background: var(--parchment);
  border: 1px solid var(--parchment-deep);
  border-radius: var(--radius);
  padding: 0.5rem;
  align-items: center;
  transition: all 0.2s;
  box-shadow: 1px 2px 4px rgba(44,36,22,0.06);
}
.inv-card:hover {
  border-color: var(--gold);
  box-shadow: 1px 2px 8px rgba(44,36,22,0.1);
}
.inv-card img {
  width: 46px;
  height: 64px;
  object-fit: cover;
  border-radius: 3px;
  flex-shrink: 0;
  border: 1px solid var(--parchment-deep);
}
.inv-card-info { flex: 1; min-width: 0; }
.inv-card-name {
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.inv-card-set { font-size: 0.72rem; color: var(--ink-faint); font-style: italic; }
.inv-card-meta {
  display: flex;
  gap: 0.4rem;
  font-size: 0.72rem;
  color: var(--ink-faint);
  margin-top: 0.2rem;
}
.inv-card-price {
  font-family: var(--font-mono);
  font-weight: 500;
  color: var(--emerald);
  font-size: 0.9rem;
  flex-shrink: 0;
}
.inv-card-actions { flex-shrink: 0; }
.btn-delete {
  background: transparent;
  border: 1px solid var(--parchment-deep);
  color: var(--red);
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.75rem;
}
.btn-delete:hover { border-color: var(--red); }

.inv-empty {
  text-align: center;
  padding: 3rem;
  color: var(--ink-faint);
  font-style: italic;
}

/* Rarity colors */
.rarity-common { color: var(--ink-faint); }
.rarity-uncommon { color: var(--ink-light); }
.rarity-rare { color: var(--gold); }
.rarity-mythic { color: var(--accent); }

/* Toast */
#toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--emerald);
  color: var(--parchment);
  padding: 0.6rem 1.4rem;
  border-radius: 4px;
  font-weight: 600;
  z-index: 200;
  animation: fadeIn 0.3s;
  box-shadow: 0 4px 12px rgba(44,36,22,0.2);
}
@keyframes fadeIn { from { opacity: 0; transform: translateX(-50%) translateY(10px); } }

/* Screen-reader only */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Mobile */
@media (max-width: 600px) {
  header { padding: 0.5rem 0.8rem; }
  header h1 { font-size: 1.1rem; }
  .nav-btn { padding: 0.3rem 0.6rem; font-size: 0.78rem; }
  #search-input { width: 130px; }
  .result-info h2 { font-size: 1.3rem; }
  .result-actions { gap: 0.3rem; }
  .result-actions button { padding: 0.45rem 0.7rem; font-size: 0.8rem; }
  .form-compact select,
  .form-compact input[type="number"] { padding: 0.3rem 0.4rem; font-size: 0.82rem; }
  .form-compact input[type="number"] { width: 2.5rem; }
}
