/* ── Reset & base ─────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --accent:   #FFD700;
  --bg:       #f4f4f4;
  --surface:  #ffffff;
  --border:   #e0e0e0;
  --text:     #1a1a1a;
  --muted:    #888888;
  --danger:   #e53935;
  --success:  #43a047;
  --radius:   12px;
  --tap:      56px;   /* minimum tap target */
  --gap:      12px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* ── Header ───────────────────────────────────────────── */
#app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  padding: 0 12px;
  background: var(--text);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 100;
}

#header-left, #header-right { flex: 0 0 auto; min-width: 44px; }
#header-title { flex: 1; text-align: center; font-size: 0.85rem; overflow: hidden; }

.icon-btn {
  background: none;
  border: none;
  color: inherit;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  padding: 8px;
  min-width: var(--tap);
  min-height: var(--tap);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Header box button badge ──────────────────────────────── */
#box-btn { position: relative; display: none; }
.header-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  background: var(--accent);
  color: var(--text);
  font-size: 0.6rem;
  font-weight: 700;
  min-width: 14px;
  height: 14px;
  border-radius: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
  pointer-events: none;
  line-height: 1;
}

#lang-select {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.4);
  color: #fff;
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 0.8rem;
  cursor: pointer;
}
#lang-select option { color: var(--text); background: var(--surface); }

#sort-select {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.4);
  color: #fff;
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 0.8rem;
  cursor: pointer;
  max-width: 100px;
}
#sort-select option { color: var(--text); background: var(--surface); }

.toggle-label {
  font-size: 0.7rem;
  color: rgba(255,255,255,0.7);
  margin-left: 4px;
  user-select: none;
}

/* ── Main layout ──────────────────────────────────────── */
#app {
  padding: var(--gap);
  min-height: calc(100vh - 52px);
}

/* ── Grid for buttons (cash-register style) ───────────── */
.btn-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: var(--gap);
}

.btn-grid.grid-2 { grid-template-columns: 1fr 1fr; }
.btn-grid.grid-1 { grid-template-columns: 1fr; }

/* ── Generic card button ──────────────────────────────── */
.card-btn {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  min-height: var(--tap);
  overflow: hidden;
  transition: border-color 0.15s, transform 0.1s;
  position: relative;
  -webkit-user-select: none;
  user-select: none;
}

.card-btn:active { transform: scale(0.96); border-color: var(--accent); }

.card-btn img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: contain;
  padding: 8px;
  background: #fafafa;
  -webkit-touch-callout: none; /* suppress native iOS long-press popover */
  pointer-events: none;        /* route all events to the button, not the img */
}

.card-btn .label {
  width: 100%;
  padding: 6px 8px;
  font-size: 0.78rem;
  text-align: center;
  line-height: 1.2;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

/* By-set box cards: always visible regardless of label toggle */
.card-btn .box-label {
  width: 100%;
  padding: 6px 8px;
  font-size: 0.78rem;
  text-align: center;
  line-height: 1.2;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.card-btn .box-label .set-num {
  font-size: 0.7rem;
  opacity: 0.6;
}

/* ── Quantity badge on part buttons ───────────────────── */
.qty-badge {
  position: absolute;
  top: 6px;
  right: 6px;
  background: var(--accent);
  color: var(--text);
  font-size: 0.75rem;
  font-weight: 700;
  min-width: 22px;
  height: 22px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  pointer-events: none;
}

.spare-tag {
  position: absolute;
  bottom: 6px;
  left: 6px;
  font-size: 0.75rem;
  line-height: 1;
  pointer-events: none;
  opacity: 0.85;
}

/* ── Home screen large buttons ────────────────────────── */
.home-btn {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  font-size: 1rem;
  font-weight: 600;
  transition: border-color 0.15s, background 0.15s;
  min-height: 120px;
}
.home-btn:active { border-color: var(--accent); background: #fffbea; }
.home-btn .home-icon { font-size: 2.5rem; line-height: 1; }

/* ── Manage Sets ──────────────────────────────────────── */
.set-card {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  margin-bottom: var(--gap);
}
.set-card img { width: 64px; height: 64px; object-fit: contain; border-radius: 6px; background: #fafafa; }
.set-card-info { flex: 1; min-width: 0; }
.set-card-name { font-weight: 600; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.set-card-meta { font-size: 0.78rem; color: var(--muted); margin-top: 2px; }
.progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  margin-top: 6px;
  overflow: hidden;
}
.progress-fill { height: 100%; background: var(--accent); border-radius: 3px; transition: width 0.3s; }

/* ── Form elements ────────────────────────────────────── */
.input-row {
  display: flex;
  gap: 8px;
  margin-bottom: var(--gap);
}
.input-row input {
  flex: 1;
  height: var(--tap);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 0 14px;
  font-size: 1rem;
  background: var(--surface);
  color: var(--text);
  outline: none;
}
.input-row input:focus { border-color: var(--accent); }

/* ── Primary / danger buttons ─────────────────────────── */
.btn {
  height: var(--tap);
  border: none;
  border-radius: var(--radius);
  padding: 0 20px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: opacity 0.15s;
}
.btn:active { opacity: 0.8; }
.btn-primary { background: var(--accent); color: var(--text); }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-ghost   { background: transparent; border: 2px solid var(--border); color: var(--text); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Breadcrumb ───────────────────────────────────────── */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.78rem;
  flex-wrap: wrap;
}
.breadcrumb-item {
  background: none;
  border: none;
  color: rgba(255,255,255,0.7);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
  white-space: nowrap;
}
.breadcrumb-item:hover { color: #fff; }
.breadcrumb-item.active { color: var(--accent); font-weight: 600; cursor: default; }
.breadcrumb-sep { color: rgba(255,255,255,0.4); font-size: 0.7rem; }

/* ── Color swatch in drill-down ───────────────────────── */
.color-swatch {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 8px 8px 0 0;
  border-bottom: 1px solid var(--border);
}

/* ── Quantity picker ──────────────────────────────────── */
.qty-picker {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px 0;
}
.qty-picker-val {
  font-size: 2rem;
  font-weight: 700;
  min-width: 60px;
  text-align: center;
}
.qty-step {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface);
  font-size: 1.6rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.qty-step:active { border-color: var(--accent); background: #fffbea; }

/* ── Status / feedback ────────────────────────────────── */
.status-msg {
  text-align: center;
  padding: 24px;
  color: var(--muted);
  font-size: 0.95rem;
}
.complete-screen {
  text-align: center;
  padding: 40px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.complete-screen .emoji { font-size: 4rem; }
.complete-screen h2 { font-size: 1.4rem; }

/* ── Add-set progress ─────────────────────────────────── */
.progress-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 200;
  gap: 16px;
  color: #fff;
}
.spinner {
  width: 40px; height: 40px;
  border: 4px solid rgba(255,255,255,0.3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Delete confirm state ─────────────────────────────── */
.btn-confirm-delete {
  background: var(--danger) !important;
  color: #fff !important;
}

/* ── Part detail popover ──────────────────────────────── */
.part-popover-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  animation: fadeIn 0.12s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.part-popover-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 320px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.25);
}
.part-popover-card img {
  width: 120px;
  height: 120px;
  object-fit: contain;
}
.part-popover-card .pop-name {
  font-weight: 700;
  font-size: 1rem;
  text-align: center;
}
.part-popover-card .pop-detail {
  font-size: 0.85rem;
  color: var(--muted);
  text-align: left;
  line-height: 1.8;
  width: 100%;
}
.part-popover-card .pop-label {
  display: inline-block;
  width: 1.6em;
  text-align: center;
  margin-right: 4px;
}
.pop-more-btn {
  align-self: stretch;
  margin-top: 4px;
  font-size: 1.1rem;
  letter-spacing: 2px;
  color: var(--muted);
}
.pop-ext {
  width: 100%;
}
.pop-ext-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  margin-bottom: 8px;
}
.pop-ext-table td {
  padding: 3px 6px;
  vertical-align: top;
}
.pop-ext-label {
  color: var(--muted);
  white-space: nowrap;
  font-weight: 600;
  width: 1%;
}
.pop-rb-link {
  display: block;
  text-align: center;
  font-size: 0.82rem;
  color: var(--accent);
  text-decoration: none;
  padding: 4px 0;
}

/* ── Responsive tweaks ────────────────────────── */
@media (max-width: 400px) {
  .btn-grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
}
@media (min-width: 600px) {
  #app { max-width: 960px; margin: 0 auto; padding: var(--gap) 20px; }
}
@media (min-width: 960px) {
  #app { max-width: 1440px; padding: var(--gap) 32px; }
  .btn-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
}
@media (min-width: 1440px) {
  #app { max-width: 1920px; padding: var(--gap) 48px; }
  .btn-grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
}

/* ── Label toggle switch ──────────────────────────────────── */
#header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toggle-switch {
  display: flex;
  align-items: center;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}
.toggle-switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-track {
  width: 32px;
  height: 18px;
  background: rgba(255,255,255,0.25);
  border-radius: 9px;
  position: relative;
  transition: background 0.2s;
  flex-shrink: 0;
}
.toggle-switch input:checked + .toggle-track { background: var(--accent); }
.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
}
.toggle-switch input:checked + .toggle-track > .toggle-thumb {
  transform: translateX(14px);
}

/* ── Progress footer (by-set parts list) ─────────────────── */
#progress-footer {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: var(--surface);
  border-top: 2px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 99;
}

.pf-track {
  position: relative;
  flex: 1;
  height: 28px;
  background: var(--border);
  border-radius: 14px;
  overflow: hidden;
}

.pf-fill {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 0%;
  background: var(--accent);
  border-radius: 14px;
  transition: width 0.4s ease;
}

.pf-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text);
  pointer-events: none;
  mix-blend-mode: difference;
  filter: invert(1);
}

/* Push app content up so footer doesn't overlap last row */
#app.has-progress-footer {
  padding-bottom: calc(var(--gap) + 48px);
}

/* ── Hide part-button labels when toggle is off ───────────── */
.no-labels .card-btn .label { display: none; }

/* ── Import / Export view ─────────────────────────────────── */
.io-section {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  text-align: center;
}
.io-label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 8px;
}
.io-id {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
  display: inline-block;
}
.io-divider {
  border: none;
  border-top: 2px solid var(--border);
  margin: 20px 0 16px;
}
.io-warning {
  font-size: 0.8rem;
  color: var(--danger);
  text-align: center;
  margin-bottom: 8px;
}
.io-input {
  width: 100%;
  height: var(--tap);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 0 14px;
  font-size: 1.2rem;
  letter-spacing: 0.12em;
  text-align: center;
  background: var(--surface);
  color: var(--text);
  outline: none;
  box-sizing: border-box;
}
.io-input:focus { border-color: var(--accent); }

/* ── Minifigures section ───────────────────────────────────────────────────── */
.minifigs-section { margin-top: 24px; }
.minifigs-heading {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 0 0 10px 4px;
}
.minifig-card {
  opacity: 0.75;
  cursor: default;
}
.minifig-card:active { transform: none; }
