/* ─── Palette (light theme) ────────────────────────────────────────────── */
:root {
  --bg: #f3efe4;
  --card-bg: #ffffff;
  --text: #23232b;
  --text-muted: #6f6f7a;
  --text-faint: #9a9aa3;
  --accent: #2c5f8a;
  --accent-soft: #eaf1f7;
  --shadow: 0 14px 34px rgba(30, 28, 20, 0.14), 0 2px 8px rgba(30, 28, 20, 0.08);
}

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

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: none; /* managed by app.js */
}

/* ─── Place chooser: shown before any place has been picked ─────────────── */
#place-chooser {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: max(24px, env(safe-area-inset-top));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
}

#place-chooser[hidden] {
  display: none;
}

#chooser-content {
  width: 100%;
  max-width: 360px;
  text-align: center;
}

#chooser-title {
  font-size: clamp(22px, 6vw, 28px);
  font-weight: 700;
}

#chooser-sub {
  margin-top: 8px;
  color: var(--text-muted);
  font-size: 15px;
}

#chooser-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 28px;
}

.chooser-btn {
  background: var(--card-bg);
  color: var(--text);
  border: none;
  border-radius: 16px;
  padding: 16px;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background 0.15s, color 0.15s, transform 0.1s;
}

.chooser-btn:hover,
.chooser-btn:focus-visible {
  background: var(--accent-soft);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.chooser-btn:active {
  transform: scale(0.98);
}

/* ─── Stage: the light backdrop the card floats on ─────────────────────── */
#stage {
  position: fixed;
  inset: 0;
  padding: 14px;
  padding-top: max(14px, env(safe-area-inset-top));
  padding-bottom: max(14px, env(safe-area-inset-bottom));
  display: flex;
}

#stage[hidden] {
  display: none;
}

#card-wrap {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ─── Main card ─────────────────────────────────────────────────────────── */
#card {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  background: var(--card-bg);
  border-radius: 24px;
  box-shadow: var(--shadow);
  overflow: hidden;
  user-select: none;
  transition: transform 0.24s cubic-bezier(.22,.61,.36,1), opacity 0.24s ease;
}

#card.shift-left  { transform: translateX(-28px) scale(0.97); opacity: 0; }
#card.shift-right { transform: translateX(28px) scale(0.97); opacity: 0; }
#card.no-anim     { transition: none; }

/* ─── Image container ───────────────────────────────────────────────────── */
#img-container {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--accent-soft);
}

#bird-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  transition: opacity 0.2s ease;
}

#bird-img.loading {
  opacity: 0;
}

.img-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(44, 95, 138, 0.15);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

#img-loading {
  opacity: 0;
  transition: opacity 0.15s;
}

#img-loading.visible {
  opacity: 1;
}

/* ─── Bird info (below the image, part of the card body) ───────────────── */
#bird-info {
  padding: 14px 18px 18px;
  background: var(--card-bg);
}

#name-latin {
  font-size: clamp(12px, 3vw, 14px);
  font-style: italic;
  color: var(--text-faint);
  margin-bottom: 4px;
}

#names-row {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  column-gap: 10px;
  row-gap: 2px;
}

#name-primary, #name-secondary {
  font-size: clamp(19px, 5vw, 25px);
  line-height: 1.15;
}

#name-primary {
  font-weight: 700;
  color: var(--text);
}

#name-secondary {
  font-weight: 400;
  font-style: italic;
  color: var(--text-muted);
}

/* ─── Navigation buttons (prev/next bird, over the image) ───────────────── */
#nav-controls {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.nav-btn {
  position: absolute;
  background: rgba(255,255,255,0.85);
  color: var(--text);
  border: none;
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  width: 42px;
  height: 42px;
  box-shadow: 0 2px 8px rgba(30,28,20,0.18);
  transition: background 0.15s, transform 0.1s;
  backdrop-filter: blur(4px);
}

.nav-btn:hover,
.nav-btn:focus-visible {
  background: #fff;
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.nav-btn:active {
  transform: scale(0.92);
}

#btn-prev {
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
}

#btn-next {
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
}

.nav-btn.hidden {
  display: none;
}

/* ─── More-photos button (inside the card, over the image) ─────────────── */
.photos-btn {
  position: absolute;
  right: 10px;
  bottom: 10px;
  background: rgba(255,255,255,0.9);
  color: var(--text);
  border: none;
  border-radius: 999px;
  padding: 7px 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(30,28,20,0.18);
  transition: background 0.15s, transform 0.1s;
}

.photos-btn:hover,
.photos-btn:focus-visible {
  background: #fff;
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.photos-btn:active {
  transform: scale(0.95);
}

.photos-btn[hidden] {
  display: none;
}

.photos-icon {
  font-size: 15px;
}

/* ─── Song button (inside the card, over the image, mirrors .photos-btn) ── */
.song-btn {
  position: absolute;
  left: 10px;
  bottom: 10px;
  background: rgba(255,255,255,0.9);
  color: var(--text);
  border: none;
  border-radius: 50%;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(30,28,20,0.18);
  transition: background 0.15s, transform 0.1s;
}

.song-btn:hover,
.song-btn:focus-visible {
  background: #fff;
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.song-btn:active {
  transform: scale(0.92);
}

.song-btn[hidden] {
  display: none;
}

.song-btn.playing {
  background: var(--accent);
  color: #fff;
}

.song-icon {
  line-height: 1;
}

/* ─── Image dots ────────────────────────────────────────────────────────── */
#img-dots {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: row;
  gap: 5px;
}

.img-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgba(255,255,255,0.55);
  box-shadow: 0 1px 2px rgba(0,0,0,0.2);
  transition: background 0.15s;
}

.img-dot.active {
  background: #fff;
}

/* ─── Bird counter ──────────────────────────────────────────────────────── */
#bird-counter {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  left: 14px;
  z-index: 100;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--card-bg);
  padding: 5px 11px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  pointer-events: none;
}

/* ─── Menu button ───────────────────────────────────────────────────────── */
#menu-btn {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: 14px;
  z-index: 100;
  background: var(--card-bg);
  color: var(--text);
  border: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  transition: background 0.15s;
}

#menu-btn:hover,
#menu-btn:focus-visible {
  background: var(--accent-soft);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ─── View toggle (Ficha / Póster, promoted as equal options) ───────────── */
#view-toggle {
  position: fixed;
  top: calc(max(14px, env(safe-area-inset-top)) + 46px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  gap: 2px;
  background: var(--card-bg);
  border-radius: 999px;
  padding: 3px;
  box-shadow: var(--shadow);
}

.view-toggle-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}

.view-toggle-btn:hover:not(.active),
.view-toggle-btn:focus-visible {
  background: var(--accent-soft);
  outline: none;
}

.view-toggle-btn.active {
  background: var(--accent);
  color: #fff;
}

/* ─── Menu overlay ──────────────────────────────────────────────────────── */
#menu-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

#menu-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

/* ─── Menu panel ────────────────────────────────────────────────────────── */
#menu-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: min(340px, 90vw);
  height: 100%;
  background: var(--card-bg);
  z-index: 300;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.25s ease;
  padding: 20px;
  overscroll-behavior: contain;
  touch-action: pan-y;
  box-shadow: -8px 0 30px rgba(30,28,20,0.14);
}

#menu-panel:not([hidden]) {
  transform: translateX(0);
}

/* Override hidden to allow transition */
#menu-panel[hidden] {
  display: block !important;
  transform: translateX(100%);
  pointer-events: none;
}

#menu-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-faint);
  font-size: 20px;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
}

#menu-close:hover,
#menu-close:focus-visible {
  color: var(--text);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#menu-content h1 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 24px;
  color: var(--text);
}

#menu-content h2 {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-faint);
  margin-bottom: 10px;
  margin-top: 28px;
}

/* ─── Place buttons ─────────────────────────────────────────────────────── */
#place-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.place-btn {
  background: var(--accent-soft);
  color: var(--accent);
  border: 2px solid transparent;
  border-radius: 8px;
  padding: 12px 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, border-color 0.15s;
}

.place-btn:hover,
.place-btn:focus-visible {
  background: #dfeaf3;
  outline: none;
  border-color: var(--accent);
}

.place-btn.active {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

/* ─── Language selectors ───────────────────────────────────────────────── */
.language-selectors {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 10px 12px;
}

.language-selectors label {
  color: var(--text-muted);
  font-size: 14px;
}

.language-selectors select {
  width: 100%;
  border: 1px solid #ccd6df;
  border-radius: 8px;
  background: var(--card-bg);
  color: var(--text);
  padding: 10px 12px;
  font: inherit;
}

.language-selectors select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ─── Chooser language section (main window, alongside place buttons) ──── */
#chooser-language-section {
  margin-top: 28px;
  padding-top: 24px;
  border-top: 1px solid rgba(30, 28, 20, 0.1);
  text-align: left;
}

#chooser-language-heading {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-faint);
  margin-bottom: 12px;
  text-align: center;
}

/* ─── Help list ─────────────────────────────────────────────────────────── */
.help-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.help-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: var(--text-muted);
}

.gesture {
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 6px;
  padding: 4px 8px;
  font-family: monospace;
  font-size: 13px;
  min-width: 100px;
  text-align: center;
}

.help-note {
  font-size: 12px;
  color: var(--text-faint);
  margin-top: 10px;
  line-height: 1.5;
}

/* ─── Print button ──────────────────────────────────────────────────────── */
.print-btn {
  background: var(--accent);
  color: #fff;
  border: 2px solid transparent;
  border-radius: 8px;
  padding: 12px 16px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  transition: background 0.15s, border-color 0.15s;
}

.print-btn:hover,
.print-btn:focus-visible {
  background: #234c70;
  outline: none;
  border-color: var(--text);
}

/* ─── Print sheet (hidden on screen, shown by @media print below) ──────── */
#print-sheet {
  display: none;
}

/* Grid cell sizes, from each bird's aspect ratio (see gridSpanClass in app.js).
   Shared by the print collage and the on-screen poster mode grid. */
.span-normal     { grid-column: span 2; grid-row: span 2; }
.span-normal-big { grid-column: span 3; grid-row: span 3; }
.span-wide       { grid-column: span 3; grid-row: span 2; }
.span-wide-big   { grid-column: span 4; grid-row: span 3; }
.span-tall       { grid-column: span 2; grid-row: span 3; }
.span-tall-big   { grid-column: span 3; grid-row: span 4; }

/* ─── Poster mode (fullscreen bin-packed collage, quiz-friendly) ────────── */
#poster-view {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  padding: 14px;
  padding-top: max(14px, env(safe-area-inset-top));
  padding-bottom: max(14px, env(safe-area-inset-bottom));
}

#poster-view[hidden] {
  display: none;
}

#poster-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

#poster-place-name {
  font-weight: 700;
  font-size: clamp(14px, 2.2vw, 18px);
  color: var(--text);
  text-align: center;
  flex: 1;
}

#poster-topbar-actions {
  display: flex;
  gap: 8px;
}

.poster-ctrl-btn {
  background: var(--card-bg);
  color: var(--text);
  border: none;
  border-radius: 999px;
  padding: 9px 14px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background 0.15s, color 0.15s, transform 0.1s;
  white-space: nowrap;
}

.poster-ctrl-btn:hover,
.poster-ctrl-btn:focus-visible {
  background: var(--accent-soft);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.poster-ctrl-btn:active {
  transform: scale(0.95);
}

.poster-ctrl-btn.quiz-active {
  background: var(--accent);
  color: #fff;
}

.poster-ctrl-btn-round {
  border-radius: 50%;
  width: 42px;
  height: 42px;
  padding: 0;
  justify-content: center;
  font-size: 20px;
}

#poster-grid-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
}

#poster-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-auto-rows: 9vh;
  grid-auto-flow: dense;
  align-content: start;
  gap: 1.4vh 1vw;
  overflow-y: auto;
}

#poster-bottombar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  margin-top: 10px;
}

#poster-page-indicator {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  min-width: 56px;
  text-align: center;
}

.poster-bird-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  min-width: 0;
  min-height: 0;
  cursor: pointer;
}

.poster-bird-img-wrap {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.poster-bird-img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
  filter: drop-shadow(0 3px 6px rgba(30, 28, 20, 0.18));
}

/* Boxed fallback photo (only used if a species has no poster-cutout.png yet) */
.poster-bird-img:not(.poster-bird-img-cutout) {
  border-radius: 12px;
  background: var(--card-bg);
  padding: 6px;
  box-shadow: var(--shadow);
  filter: none;
}

.poster-bird-label {
  margin-top: 3px;
  text-align: center;
  line-height: 1.15;
  transition: opacity 0.15s;
}

.poster-name-primary,
.poster-name-secondary,
.poster-name-latin {
  display: block;
}

.poster-name-primary {
  font-weight: 700;
  font-size: clamp(10px, 1.3vw, 13px);
  color: var(--text);
}

.poster-name-secondary {
  font-style: italic;
  font-size: clamp(9px, 1.1vw, 11px);
  color: var(--text-muted);
}

.poster-name-latin {
  font-size: clamp(8px, 0.9vw, 10px);
  font-style: italic;
  color: var(--text-faint);
}

/* Quiz mode: names hidden by default, revealed per-bird on tap */
#poster-grid:not(.show-names) .poster-bird-cell:not(.revealed) .poster-bird-label {
  opacity: 0;
  pointer-events: none;
}

/* ─── Print (A4 guide, replaces the old Puppeteer-rendered PDF) ─────────── */
@media print {
  @page {
    size: A4 portrait;
    margin: 15mm 12mm 12mm 12mm;
  }

  html, body {
    height: auto;
    overflow: visible;
    background: #fff;
    color: #111;
  }

  #stage, #menu-overlay, #menu-panel, #poster-view {
    display: none !important;
  }

  #print-sheet {
    display: block;
  }

  .print-page {
    page-break-after: always;
  }

  .print-page:last-child {
    page-break-after: auto;
  }

  .print-page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 2px solid #2c5f8a;
    padding-bottom: 4mm;
    margin-bottom: 5mm;
  }

  .print-place-name {
    font-size: 20pt;
    font-weight: 700;
    color: #2c5f8a;
  }

  .print-page-num {
    font-size: 11pt;
    color: #777;
  }

  /* Collage grid: dense flow packs the varying cell sizes without gaps left
     by strict row/column order — this *is* the bin-packing, done in CSS. */
  .print-poster-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-auto-rows: 21mm;
    grid-auto-flow: dense;
    column-gap: 3mm;
    row-gap: 1mm;
    /* No fixed height/overflow:hidden here on purpose — if a page's mix of
       cell sizes ever needs more room than fits, it should visibly spill
       onto an extra printed page, never silently clip a bird off. */
  }

  .print-bird-cell {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    min-width: 0;
    min-height: 0;
  }

  .print-bird-img-wrap {
    flex: 1;
    min-height: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: visible;
  }

  .print-bird-img {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    object-fit: contain;
    display: block;
  }

  /* Boxed fallback photo (only used if a species has no poster-cutout.png yet) */
  .print-bird-img:not(.print-bird-img-cutout) {
    border-radius: 3mm;
    background: #f5f5f5;
    padding: 2mm;
  }

  .print-bird-label {
    margin-top: 1mm;
    text-align: center;
    line-height: 1.15;
  }

  .print-name-primary, .print-name-secondary {
    font-size: 9pt;
  }

  .print-name-primary {
    font-weight: 700;
  }

  .print-name-secondary {
    color: #444;
    font-style: italic;
  }

  .print-name-latin {
    display: block;
    font-size: 6.5pt;
    color: #888;
    font-style: italic;
  }
}

/* ─── Reduced motion ────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ─── Landscape adjustments ─────────────────────────────────────────────── */
@media (orientation: landscape) and (max-height: 500px) {
  #bird-info {
    padding: 8px 16px 10px;
  }

  #name-primary, #name-secondary {
    font-size: clamp(16px, 5vh, 22px);
  }
}
