/* ── Reset & base ────────────────────────────────────────────────────────── */

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

:root {
  --bg:       #0d0d0d;
  --surface:  #181818;
  --surface2: #222222;
  --border:   #2e2e2e;
  --text:     #f0f0f0;
  --text-muted: #888;
  --accent:   #e8c84a;
  --accent-dim: #a88e2a;
  --used:     #3a3a3a;
  --danger:   #c0392b;
  --radius:   8px;
  --sidebar-w: 260px;
  --header-h: 57px;
  /* Touch target minimum — 44px per Apple HIG / WCAG 2.5.5 */
  --touch-min: 44px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
}
@view-transition {
  navigation: auto;
}

/* ── View routing ────────────────────────────────────────────────────────── */

.view { display: none; flex-direction: column; min-height: 100vh; }
.view.active { display: flex; }

/* ── Header ─────────────────────────────────────────────────────────────── */

header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
}

header h1 {
  font-size: 18px;
  font-weight: 600;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.count-badge {
  font-size: 13px;
  color: var(--text-muted);
  background: var(--surface2);
  padding: 3px 10px;
  border-radius: 99px;
  border: 1px solid var(--border);
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */

button { cursor: pointer; border: none; background: none; color: inherit; font: inherit; }

.btn-primary {
  background: var(--accent);
  color: #111;
  font-weight: 600;
  padding: 10px 20px;
  border-radius: var(--radius);
  width: 100%;
  font-size: 15px;
  transition: background 0.15s;
}
.btn-primary:hover:not(:disabled) { background: #f5d96a; }
.btn-primary:disabled {
  background: var(--surface2);
  color: var(--text-muted);
  cursor: not-allowed;
}

.btn-ghost {
  color: var(--text-muted);
  padding: 8px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: 13px;
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
  min-height: var(--touch-min);
  display: inline-flex;
  align-items: center;
}
.btn-ghost:hover { color: var(--text); border-color: #555; }

.btn-sm {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 5px;
  border: 1px solid var(--border);
  color: var(--text-muted);
  background: var(--surface2);
  transition: color 0.15s;
  min-height: var(--touch-min);
  display: inline-flex;
  align-items: center;
}
.btn-sm:hover { color: var(--text); }

.btn-danger {
  background: var(--danger);
  color: #fff;
  font-weight: 600;
  padding: 10px 20px;
  border-radius: var(--radius);
  font-size: 15px;
  transition: background 0.15s;
}
.btn-danger:hover { background: #e74c3c; }

.btn-icon-danger {
  font-size: 13px;
  padding: 3px 8px;
  border-radius: 5px;
  border: 1px solid transparent;
  color: var(--text-muted);
  background: none;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  line-height: 1;
}
.btn-icon-danger:hover {
  color: #e74c3c;
  border-color: #c0392b;
  background: rgba(192, 57, 43, 0.1);
}

/* ── View toggle ─────────────────────────────────────────────────────────── */

.view-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.view-toggle-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  color: var(--text-muted);
  background: var(--surface2);
  border: none;
  border-radius: 0;
  transition: color 0.15s, background 0.15s;
}
.view-toggle-btn + .view-toggle-btn {
  border-left: 1px solid var(--border);
}
.view-toggle-btn:hover { color: var(--text); background: var(--surface); }
.view-toggle-btn.active { color: var(--accent); background: var(--surface); }

/* ── Player grid (View 1) ────────────────────────────────────────────────── */

.player-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 20px;
  padding: 24px;
  align-content: start;
}

.player-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
}
.player-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.player-thumb {
  width: 100%;
  aspect-ratio: 5 / 7;
  object-fit: cover;
  display: block;
  background: var(--surface2);
}

.player-thumb-placeholder {
  width: 100%;
  aspect-ratio: 5 / 7;
  background: var(--surface2);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 32px;
}

.player-info {
  padding: 8px 12px 12px;
}

.player-name {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.player-meta .zero { color: var(--danger); }

/* ── Player grid — list view ─────────────────────────────────────────────── */

.player-grid.list-view {
  grid-template-columns: 1fr;
  gap: 4px;
  padding: 16px 24px;
}

.player-grid.list-view .player-card {
  display: flex;
  flex-direction: row;
  align-items: center;
  transform: none !important;
}

.player-grid.list-view .player-thumb,
.player-grid.list-view .player-thumb-placeholder {
  width: 48px;
  height: 48px;
  aspect-ratio: unset;
  flex-shrink: 0;
  font-size: 18px;
}

.player-grid.list-view .player-info {
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.player-grid.list-view .player-name {
  flex: 1;
  min-width: 0;
}

.player-grid.list-view .player-meta {
  margin-top: 0;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ── Card selector layout (View 2) ──────────────────────────────────────── */

.selector-layout {
  display: flex;
  flex: 1;
  align-items: flex-start;
}

.selector-main {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.selector-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: sticky;
  top: var(--header-h);
  z-index: 9;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  margin-left: auto;
}
.toggle-label input { cursor: pointer; accent-color: var(--accent); }

/* ── Card grid ───────────────────────────────────────────────────────────── */

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 24px;
  padding: 20px;
}

.card-pair {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s, opacity 0.15s;
  background: var(--surface);
}

.card-pair.selected { border-color: var(--accent); }

.card-pair.used {
  display: none;
}

.card-grid.show-used .card-pair.used {
  display: block;
  opacity: 0.5;
  cursor: not-allowed;
}

.card-images {
  position: relative;
  width: 100%;
  aspect-ratio: 5 / 7;
  perspective: 1000px;
  background: var(--surface2);
}

.card-flip-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.45s ease;
}

/* Flip previews only on true hover-capable pointers.
   Mobile taps should select cards without flipping. */
@media (hover: hover) and (pointer: fine) {
  .card-tile:hover .card-flip-inner,
  .card-tile:focus-within .card-flip-inner,
  .card-pair:hover .card-flip-inner,
  .card-pair:focus-within .card-flip-inner {
    transform: rotateY(180deg);
  }
}

.card-face {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.card-face--back {
  transform: rotateY(180deg);
}

@media (prefers-reduced-motion: reduce) {
  .card-flip-inner {
    transition: none;
  }
}
@media (hover: none), (pointer: coarse) {
  .card-flip-inner {
    transition: none;
  }
}

.card-check {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--surface);
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  transition: background 0.1s, border-color 0.1s;
}
.card-pair.selected .card-check {
  background: var(--accent);
  border-color: var(--accent);
  color: #111;
}
.card-pair.selected .card-check::after { content: "✓"; font-weight: 700; }

/* ── Card bottom bar ─────────────────────────────────────────────────────── */

.card-bottom-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  font-size: 10px;
  padding: 4px 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
}

.card-bottom-bar span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-bottom-bar--used {
  background: rgba(0,0,0,0.75);
  color: var(--text-muted);
}

.card-bottom-bar--meta {
  background: rgba(0,0,0,0.72);
  color: #c8b96a;
}

.card-bottom-bar--meta.meta-low-confidence {
  color: #aaa;
}

.card-bottom-bar--empty {
  background: rgba(0,0,0,0.6);
  justify-content: space-between;
}

.card-id-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  color: var(--text-muted);
  min-width: 0;
}

.card-id-status__icon {
  flex-shrink: 0;
}

.card-id-status__text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-id-status--pending {
  color: #b5bdd2;
}

.card-id-status--processing {
  color: #8db4ff;
}

.card-id-status--completed {
  color: #76d39d;
}

.card-id-status--failed {
  color: #f58a8a;
}

.unmark-btn, .edit-meta-btn {
  flex-shrink: 0;
  font-size: 13px;
  line-height: 1;
  color: var(--text-muted);
  padding: 0 2px;
  border-radius: 3px;
  transition: color 0.1s, background 0.1s;
}
.unmark-btn:hover { color: #fff; background: var(--danger); }
.edit-meta-btn:hover { color: var(--accent); }

.identify-btn {
  font-size: 11px;
  color: var(--accent);
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 3px;
  border: 1px solid rgba(232,200,74,0.4);
  transition: background 0.1s;
  flex-shrink: 0;
}
.identify-btn:hover { background: rgba(232,200,74,0.15); }
.identify-btn:disabled { opacity: 0.5; cursor: default; }

/* ── Identification status bottom bars ───────────────────────────────────── */

.card-bottom-bar--identifying {
  background: rgba(0,0,0,0.72);
  color: var(--accent);
}

.card-bottom-bar--failed {
  background: rgba(0,0,0,0.72);
  color: var(--danger);
}

.id-status-label {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.id-spinner {
  display: inline-block;
  width: 10px;
  height: 10px;
  border: 1.5px solid rgba(232,200,74,0.3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: id-spin 0.8s linear infinite;
  flex-shrink: 0;
}

@keyframes id-spin {
  to { transform: rotate(360deg); }
}

/* ── Identification progress bar ─────────────────────────────────────────── */

.id-progress {
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 12px;
}

.id-progress__bar {
  flex: 1;
  height: 6px;
  background: var(--surface2);
  border-radius: 3px;
  overflow: hidden;
}

.id-progress__fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.4s ease;
  width: 0%;
}

.id-progress__label {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ── Copy quantity stepper ───────────────────────────────────────────────── */

.copy-stepper {
  position: absolute;
  bottom: 32px; /* sit just above the bottom bar (~28px tall) */
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(0,0,0,0.82);
  border: 1px solid rgba(232,200,74,0.5);
  border-radius: 6px;
  padding: 4px 6px;
  z-index: 2;
  pointer-events: auto;
}

.stepper-btn {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  border: 1px solid rgba(232,200,74,0.4);
  background: rgba(232,200,74,0.12);
  color: var(--accent);
  font-size: 14px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.1s;
  flex-shrink: 0;
}
.stepper-btn:hover:not(:disabled) { background: rgba(232,200,74,0.25); }
.stepper-btn:disabled { opacity: 0.3; cursor: default; }

.stepper-count {
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  min-width: 16px;
  text-align: center;
}

/* In list view the stepper doesn't make sense as an absolute overlay — hide it */
.card-grid.list-view .copy-stepper { display: none; }

/* ── Group badge & link button ───────────────────────────────────────────── */

.group-badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 10px;
  font-weight: 700;
  color: #111;
  background: var(--accent);
  border-radius: 4px;
  padding: 1px 5px;
  line-height: 1.4;
  flex-shrink: 0;
  width: fit-content;
}

.link-btn {
  font-size: 11px;
  color: var(--text-muted);
  padding: 2px 5px;
  border-radius: 3px;
  border: 1px solid var(--border);
  line-height: 1;
  transition: color 0.1s, border-color 0.1s;
  flex-shrink: 0;
}
.link-btn:hover { color: var(--accent); border-color: var(--accent); }
.link-btn.is-grouped { color: var(--accent); border-color: var(--accent); }

/* ── Group-select mode ───────────────────────────────────────────────────── */

/* Dim the whole grid, un-dim via individual classes */
.card-grid.grouping-mode .card-pair {
  opacity: 0.4;
  transition: opacity 0.15s, border-color 0.15s;
}
.card-grid.grouping-mode .card-pair.group-source,
.card-grid.grouping-mode .card-pair.group-target {
  opacity: 1;
}
.card-grid.grouping-mode .card-pair.group-source {
  border-color: var(--accent);
}
.card-grid.grouping-mode .card-pair.group-target {
  border-color: #6abfff;
}
.card-grid.grouping-mode .card-pair:not(.group-source):not(.group-target):hover {
  opacity: 1;
  border-color: #555;
  cursor: pointer;
}

/* Toolbar banner shown during grouping mode */
.grouping-banner {
  display: none;
  align-items: center;
  gap: 10px;
  padding: 8px 20px;
  background: rgba(232,200,74,0.12);
  border-bottom: 1px solid rgba(232,200,74,0.3);
  font-size: 13px;
  color: var(--accent);
  flex-shrink: 0;
}
.grouping-banner.active { display: flex; }
.grouping-banner strong { font-weight: 600; }
.grouping-banner .btn-ghost { margin-left: auto; }

/* ── Card grid — list view ───────────────────────────────────────────────── */

.card-grid.list-view {
  grid-template-columns: 1fr;
  gap: 4px;
  padding: 12px 20px;
}

.card-grid.list-view .card-pair,
.card-grid.list-view .card-tile {
  display: flex;
  flex-direction: row;
  align-items: center;
  border-radius: var(--radius);
}

.card-grid.list-view .card-images {
  flex-shrink: 0;
  width: 48px;
  height: 64px;
  aspect-ratio: unset;
  perspective: 600px;
}

.card-grid.list-view .card-face {
  width: 100%;
  height: 100%;
  aspect-ratio: unset;
}

/* In list view the check lives in the row, not absolutely positioned */
.card-grid.list-view .card-check {
  position: static;
  margin: 0 12px 0 auto;
  flex-shrink: 0;
}

/* Bottom bar becomes a middle info area */
.card-grid.list-view .card-bottom-bar {
  position: static;
  flex: 1;
  padding: 0 12px;
  font-size: 12px;
  background: none !important;
  color: var(--text-muted);
}

.card-grid.list-view .card-bottom-bar--meta {
  color: #c8b96a;
}

.card-grid.list-view .card-bottom-bar--meta.meta-low-confidence {
  color: #aaa;
}

.card-grid.list-view .card-bottom-bar--empty {
  justify-content: space-between;
}

.card-grid.list-view.show-used .card-pair.used,
.card-grid.list-view.show-used .card-tile.used {
  display: flex;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */

.selector-sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
}

.selector-sidebar h2 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.field-label {
  font-size: 12px;
  color: var(--text-muted);
  display: block;
  margin-bottom: 4px;
}

.field-input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 8px 10px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
}
.field-input:focus { border-color: var(--accent); }

/* ── Background presets & images ────────────────────────────────────────── */

.bg-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.bg-preset {
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text-muted);
  transition: color 0.15s, border-color 0.15s;
  min-height: var(--touch-min);
  display: inline-flex;
  align-items: center;
}
.bg-preset:hover { color: var(--text); border-color: #555; }
.bg-preset.active {
  border-color: var(--accent);
  color: var(--accent);
}

.custom-bg { display: flex; flex-direction: column; }
.custom-bg.hidden { display: none; }

.bg-images-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.btn-upload {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 5px;
  border: 1px solid var(--border);
  color: var(--text-muted);
  background: var(--surface2);
  cursor: pointer;
  transition: color 0.15s;
}
.btn-upload:hover { color: var(--text); }

.bg-image-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.bg-image-thumb {
  aspect-ratio: 4 / 3;
  border-radius: 5px;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s;
}
.bg-image-thumb:hover { border-color: #555; }
.bg-image-thumb.selected { border-color: var(--accent); }
.bg-image-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.slider {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ── Error message ───────────────────────────────────────────────────────── */

.error-msg {
  font-size: 13px;
  color: #ff6b6b;
  background: rgba(192, 57, 43, 0.15);
  border: 1px solid rgba(192, 57, 43, 0.3);
  border-radius: 5px;
  padding: 8px 10px;
}
.error-msg.hidden { display: none; }

/* ── Drop zone ───────────────────────────────────────────────────────────── */

.drop-zone {
  margin: 12px 20px 0;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 14px 20px;
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
  transition: border-color 0.15s, background 0.15s;
  flex-shrink: 0;
}

.drop-zone.drag-over {
  border-color: var(--accent);
  background: rgba(232, 200, 74, 0.06);
  color: var(--text);
}

.drop-zone.uploading {
  border-color: var(--accent);
  color: var(--text);
}

.drop-zone-browse {
  color: var(--accent);
  text-decoration: underline;
  font: inherit;
  cursor: pointer;
}
.drop-zone-browse:hover { color: #f5d96a; }

/* ── Modal ───────────────────────────────────────────────────────────────── */

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 32px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-box h2 {
  font-size: 17px;
  font-weight: 600;
}

.modal-fields {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 4px;
}

/* ── Results (View 3) ────────────────────────────────────────────────────── */

.results-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.results-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  padding: 24px;
  align-content: start;
}

.result-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: border-color .15s;
  display: flex;
  flex-direction: column;
}

.result-item:hover {
  border-color: var(--accent-dim);
}

.result-canvas {
  width: 100%;
  height: auto;
  display: block;
}

.result-item-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  gap: 8px;
  flex-shrink: 0;
}

.result-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: capitalize;
}

/* ── History (View: history) ─────────────────────────────────────────────── */

.history-list {
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.history-name { font-weight: 600; flex: 1; }
.history-player { color: var(--text-muted); font-size: 13px; }
.history-date { font-size: 12px; color: var(--text-muted); }
.history-item .btn-icon-danger { margin-left: auto; flex-shrink: 0; }

/* ── Overlay ─────────────────────────────────────────────────────────────── */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.overlay.hidden { display: none; }

.overlay-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 48px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Lot description panel ───────────────────────────────────────────────── */

.lot-description-panel {
  margin: 0 24px 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  flex-shrink: 0;
}

.lot-description-panel.hidden { display: none; }

.lot-description-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.lot-description-text {
  padding: 14px 16px;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.7;
  white-space: pre-wrap;
  color: var(--text);
  margin: 0;
}

/* ── Modal extras ────────────────────────────────────────────────────────── */

.modal-box--wide {
  width: 480px;
}

.modal-box--narrow {
  width: 320px;
}

.confirm-delete-body {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.5;
}

.field-textarea {
  width: 100%;
  resize: vertical;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  line-height: 1.6;
  min-height: 180px;
}

.field-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
  line-height: 1.5;
}

.field-hint code {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 1px 4px;
  font-size: 11px;
  color: var(--accent);
}

.modal-fields-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* ── Misc ────────────────────────────────────────────────────────────────── */

.loading {
  color: var(--text-muted);
  font-size: 14px;
  padding: 40px;
}

.empty-state {
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
  padding: 60px 20px;
  grid-column: 1 / -1;
}

/* ── Auth screen ─────────────────────────────────────────────────────────── */

.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.auth-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 36px 40px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.auth-box h1 {
  font-size: 22px;
  font-weight: 700;
  text-align: center;
  color: var(--accent);
}

.auth-box .auth-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  text-align: center;
  margin-top: -12px;
}

.auth-fields {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-switch {
  font-size: 13px;
  color: var(--text-muted);
  text-align: center;
}

.auth-switch button {
  color: var(--accent);
  text-decoration: underline;
  font: inherit;
  cursor: pointer;
}
.auth-switch button:hover { color: #f5d96a; }

/* ── User menu (top-right in header) ─────────────────────────────────────── */

.user-menu {
  position: relative;
}

.user-menu-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: color 0.15s, border-color 0.15s;
  min-height: var(--touch-min);
  max-width: 200px;
}
.user-menu-btn:hover { color: var(--text); border-color: #555; }

#user-email-display {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.user-menu-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: 140px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
  z-index: 50;
  display: none;
}
.user-menu-dropdown.open { display: block; }

.user-menu-dropdown button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 16px;
  font-size: 13px;
  color: var(--text-muted);
  transition: background 0.1s, color 0.1s;
}
.user-menu-dropdown button:hover { background: var(--surface2); color: var(--text); }

/* ── Lot status badge in history ─────────────────────────────────────────── */

.lot-status {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 99px;
  font-weight: 600;
  flex-shrink: 0;
}
.lot-status--complete  { background: rgba(46,204,113,0.15); color: #2ecc71; border: 1px solid rgba(46,204,113,0.3); }
.lot-status--processing { background: rgba(232,200,74,0.15); color: var(--accent); border: 1px solid rgba(232,200,74,0.3); }
.lot-status--error     { background: rgba(192,57,43,0.15);  color: #e74c3c; border: 1px solid rgba(192,57,43,0.3); }

/* ── Inventory view ──────────────────────────────────────────────────────── */

.inventory-mode-toggle {
  display: flex;
  gap: 4px;
  margin-left: auto;
  flex-shrink: 0;
}

.inventory-mode-btn {
  padding: 5px 14px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  min-height: var(--touch-min);
}
.inventory-mode-btn:hover { color: var(--text); border-color: #555; }
.inventory-mode-btn.active {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
  font-weight: 600;
}

.inventory-content { padding: 20px; }

.inventory-flat-grid {
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
}

.card-tile {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s, opacity 0.15s;
  background: var(--surface);
}
.card-tile.selected { border-color: var(--accent); }
.card-tile.card-used { opacity: 0.4; cursor: not-allowed; }

.card-tile .card-img {
  display: block;
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
}

.card-tile .card-bottom-bar {
  padding: 4px 6px;
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Tablet (641px – 1024px) ─────────────────────────────────────────────── */

@media (max-width: 1024px) {
  /* Sidebar narrower on tablet */
  :root { --sidebar-w: 220px; }

  /* Card grid: fewer columns on mid-size screens */
  .card-grid {
    grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  }

  /* Results grid: 2 cols max on tablet */
  .results-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}

/* ── Narrow tablet / large phone (641px – 768px) ─────────────────────────── */

@media (max-width: 768px) {
  /* Stack the sidebar below the card grid — not enough horizontal room */
  .selector-layout {
    flex-direction: column;
    align-items: stretch;
  }
  .selector-sidebar {
    width: 100%;
    min-width: 0;
    position: static;
    max-height: none;
    border-left: none;
    border-top: 1px solid var(--border);
    padding: 16px;
  }
}

/* ── Mobile (≤ 640px) ────────────────────────────────────────────────────── */

@media (max-width: 640px) {
  /* ── Header ── */

  /* Compact mobile header into a toolbar-like row */
  header {
    padding: 8px 12px;
    gap: 6px;
  }
  header h1 {
    font-size: 16px;
  }
  .header-actions {
    gap: 6px;
  }

  /* The inventory header has many items; let the mode toggle drop to its own
     row so the title + user menu stay on row 1. */
  #view-inventory header .inventory-mode-toggle {
    order: 3;
    width: 100%;
    margin-left: 0;
    justify-content: flex-start;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }

  /* Hide the email text on small screens — only show the user icon */
  #user-email-display {
    display: none;
  }

  /* Sticky toolbar top offset: when the inventory header wraps it is taller.
     Use a larger offset that comfortably clears a two-row header. */
  .selector-toolbar {
    top: 0; /* no longer sticky — rely on natural scroll since header wraps */
    position: static;
  }

  /* ── Page padding ── */
  .player-grid,
  .card-grid,
  .results-grid,
  .history-list,
  .inventory-content { padding: 12px; }

  .player-grid.list-view { padding: 8px 12px; }

  /* ── Card grids ── */

  /* Inventory flat tiles: 2 columns on phones (tiles are small enough) */
  .card-grid.inventory-flat-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Keep inventory card grids at two columns on phones */
  .card-grid:not(.inventory-flat-grid):not(.list-view) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  /* Result images: single column, images constrained to viewport */
  .results-grid {
    grid-template-columns: 1fr;
  }
  .result-canvas {
    max-width: 100%;
    height: auto;
  }

  /* Player / team grids — two columns minimum even at 320px */
  .player-grid {
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 12px;
  }

  /* ── Selector layout ── */

  /* Sidebar — stack below grid instead of side-by-side */
  .selector-layout {
    flex-direction: column;
    align-items: stretch; /* children fill full width, not just their natural width */
  }
  .selector-sidebar {
    width: 100%;
    min-width: 0;
    position: static;
    max-height: none;
    border-left: none;
    border-top: 1px solid var(--border);
    padding: 16px 12px;
  }

  /* Toolbar — allow wrapping */
  .selector-toolbar {
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 12px;
  }

  /* ── Modals ── */

  .modal-box,
  .modal-box--wide,
  .modal-box--narrow {
    width: calc(100vw - 32px);
    padding: 20px;
  }

  /* Modal two-column field rows → single column */
  .modal-fields-row {
    grid-template-columns: 1fr;
  }

  /* ── Overlay & auth ── */

  .overlay-box {
    padding: 24px 20px;
    width: calc(100vw - 32px);
  }

  .auth-box {
    width: calc(100vw - 32px);
    padding: 24px 20px;
  }

  /* ── History ── */

  /* History items — allow name to wrap, date on new line */
  .history-item {
    flex-wrap: wrap;
    gap: 8px;
  }
  .history-name { width: 100%; }
  .history-date { font-size: 11px; }

  /* ── Misc ── */

  /* Drop zone */
  .drop-zone { margin: 8px 12px 0; }

  /* Lot description panel */
  .lot-description-panel { margin: 0 12px 12px; }

  /* Copy stepper — slightly larger hit area on touch */
  .copy-stepper {
    bottom: 40px;
    padding: 6px 8px;
    gap: 8px;
  }
}
