:root {
  /* Card size scales with the viewport instead of staying fixed. The
     height term is DERIVED from the actual layout geometry (header ~60px,
     table padding 22+40px, free-cell row + margin, then a 7-card tableau
     column with 6 card-overlaps) so a 7-card column fills the available
     vertical space almost exactly, rather than guessing a multiplier —
     a guessed 14vh was calibrated against a fullscreen 1080px-tall
     viewport and came out far too small for a normal windowed (non-
     maximized) browser, which has noticeably less effective height. */
  --card-w: clamp(90px, min(8vw, calc(20.8vh - 29px)), 210px);
  --card-h: calc(var(--card-w) * 1.405);
  --card-overlap: calc(var(--card-w) * 0.333);
}

/* Tablets */
@media (max-width: 900px) {
  :root { --card-w: 72px; --card-h: 100px; --card-overlap: 24px; --board-gap: 20px; }
  .site-header { gap: 12px; }
}

/* Phones */
@media (max-width: 600px) {
  :root { --card-w: 40px; --card-h: 56px; --card-overlap: 16px; --board-gap: 16px; }

  .site-header {
    flex-wrap: wrap;
    padding: 8px 10px;
    gap: 8px;
  }
  .brand-logo { height: 24px; }
  .brand-name { font-size: 1.15rem; }
  /* The status bar now holds Time, Pause, Moves, New Game, and Save Game —
     considerably more than it did originally. It's no longer safe to force
     all of that into one non-wrapping row on a narrow phone, so it wraps. */
  .status-bar { gap: 8px; margin-left: 0; order: 3; width: 100%; justify-content: center; flex-wrap: wrap; row-gap: 6px; }
  .btn-bookmark { padding: 7px 9px; font-size: 0.7rem; }
  .new-game-group .btn { padding: 8px 10px; font-size: 0.76rem; }
  .header-actions { gap: 6px; margin-left: auto; flex-wrap: wrap; justify-content: flex-end; row-gap: 6px; }
  .btn { padding: 10px 10px; font-size: 0.78rem; min-height: 40px; }
  .btn-icon { padding: 10px; min-width: 40px; }
  .btn-labeled { min-width: 0; padding: 9px 11px; }
  .new-game-group .btn:first-child { padding-left: 12px; padding-right: 8px; }

  .table { padding: 14px 8px 30px; }
  .board-top { margin-bottom: 10px; }
  .column { min-height: 130px; padding-bottom: 90px; }

  .foundation::before { font-size: 1.2rem; }

  .overlay-card { padding: 24px 20px; max-height: 92vh; }
  .win-image { max-width: 140px; margin-bottom: 12px; }
  .win-image-daily { max-width: 110px; }

  .modal { padding: 16px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }

  .seo-content { padding: 34px 16px 20px; }
  .seo-content h1 { font-size: 1.4rem; }

  .achv-panel { position: fixed; top: 64px; right: 8px; left: 8px; width: auto; max-height: 60vh; }
  .achv-toast-stack { right: 8px; bottom: 8px; left: 8px; }
  .achv-toast { width: 100%; }
}

@media (max-width: 380px) {
  /* A straight 4x here (16px, inherited from the phone breakpoint) would
     overflow at this width since cards are already down to 34px — this is
     the largest gap that still fits 8 columns without overflowing. */
  :root { --card-w: 34px; --card-h: 48px; --card-overlap: 17px; --board-gap: 14px; }
  .status-value { font-size: 0.85rem; }
  .btn-bookmark .bookmark-label { display: none; }
  .btn-bookmark { padding: 8px; }
  .btn-labeled .btn-icon-label { display: none; }
  .btn-labeled { padding: 9px; min-width: 0; }
  .new-game-group .btn:first-child { padding-left: 10px; padding-right: 6px; font-size: 0.72rem; }
}

/* ---------- Mobile landscape: collapsible header ---------- */
/* On a phone turned sideways, vertical space is the scarce resource — the
   header alone can eat a third of the screen height, leaving little room
   for the board. Show a collapse toggle only in this situation (landscape
   + short viewport, i.e. an actual phone-in-landscape, not a short desktop
   window) and let the header shrink to almost nothing when collapsed. */
.btn-header-collapse { display: none; }

@media (orientation: landscape) and (max-height: 500px) {
  .btn-header-collapse {
    display: flex;
    position: fixed;
    top: 4px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 210;
    padding: 6px 12px;
    font-size: 0.7rem;
    opacity: 0.9;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  }
  /* .btn:active alone sets transform:scale() with no translate, which would
     silently override the centering above during a press (transforms don't
     merge across separate rules) — combine both so it stays centered. */
  .btn-header-collapse:active {
    transform: translateX(-50%) scale(0.97);
  }
  /* A landscape phone is wide enough to miss the (max-width: 600px) phone
     breakpoint entirely, but still too short to comfortably fit full-size
     desktop buttons — this is the actual gap that caused the crowded/
     overflowing header. Compact everything based on HEIGHT here, not width. */
  .site-header {
    padding: 6px 10px;
    gap: 10px;
    flex-wrap: wrap;
    row-gap: 4px;
    max-height: 200px;
    overflow: hidden;
    transition: max-height 200ms var(--ease), padding 200ms var(--ease);
  }
  .brand-name { display: none; }
  .brand-logo { height: 22px; }
  .status-bar { gap: 6px; margin-left: 0; }
  .status-item { line-height: 1; }
  .status-label { font-size: 0.55rem; }
  .status-value { font-size: 0.82rem; }
  .btn { padding: 6px 8px; font-size: 0.7rem; min-height: 30px; }
  .btn-icon { padding: 6px; min-width: 30px; }
  .btn-labeled .btn-icon-label { display: none; }
  .btn-labeled { padding: 6px; min-width: 0; }
  .new-game-group .btn:first-child { padding-left: 8px; padding-right: 6px; }
  .btn-bookmark .bookmark-label { display: none; }
  .btn-bookmark { padding: 6px; }
  .header-actions { gap: 4px; }
  .btn-pause-inline { padding: 6px; }

  body.header-collapsed .site-header {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    border-bottom: none;
  }
  body.header-collapsed .site-header * { visibility: hidden; }
  body.header-collapsed .table { padding-top: 8px; }
}
