/* Breakpoints: phone and tablet widths, then landscape (side HUD, nav in the top bar).
   Loaded last, after every component stylesheet, so its overrides win. */

@media (max-width: 640px) {
  body {
    background:
      radial-gradient(circle at 50% -10%, rgba(201,162,39,.22), transparent 34%),
      linear-gradient(180deg, var(--page-bg-1) 0%, var(--page-bg-2) 100%);
  }

  .game-layout { justify-content: center; gap: 8px; padding: 8px calc(8px + env(safe-area-inset-right)) 8px calc(8px + env(safe-area-inset-left)); }
  .mat-frame-wrap { order: 1; width: 100%; border-radius: 18px; overflow: hidden; box-shadow: 0 14px 35px rgba(0,0,0,.45); }
  .mat-border-frame { display: none; }
  .embroidered-mat { border-width: 9px; border-radius: 18px; box-shadow: inset 0 0 16px rgba(68,42,22,.22); }
  .embroidered-mat::before { inset: 3px; border-width: 1px; }
  .corner-ornament { transform: scale(.72); }
  .team-label-top, .team-label-bottom { padding: 5px 0; gap: 2px; }
  .board-team-name { font-size: clamp(.62rem, 2.6vw, .82rem); }

  .top-controls-row {
    order: 2; display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px;
    width: 100%; padding: 8px;
    background: linear-gradient(180deg, var(--surface-1), var(--surface-2));
    border: 1px solid var(--border-gold); border-radius: 20px;
    box-shadow: 0 16px 42px rgba(0,0,0,.62), inset 0 1px rgba(255,255,255,.06);
    backdrop-filter: blur(14px);
  }
  .top-controls-row .hud-panel { padding: 0; background: transparent; border: 0; box-shadow: none; }
  .top-controls-row .hud-panel:first-child { order: 2; }
  .top-controls-row .hud-panel:last-child { order: 1; }
  #unlock-note { display: none; }
  .dice-row { gap: 8px; }
  .cowries-box { justify-content: flex-start; gap: 6px; min-width: 0; }
  .cowrie { width: 24px; height: 34px; border-width: 2px; }
  .roll-btn, .pass-btn {
    min-width: 116px; min-height: 52px; padding: 10px 18px; border-radius: 16px;
    font-size: .95rem; letter-spacing: .5px;
    box-shadow: 0 7px 15px var(--shadow-soft), inset 0 1px rgba(255,255,255,.35);
    transition: transform .16s ease-out, filter .16s ease-out, box-shadow .16s ease-out;
  }
  .roll-btn:not(:disabled) { background: linear-gradient(180deg, #f2d465 0%, #c69a2e 48%, #946b13 100%); }
  .roll-btn:not(:disabled):active, .pass-btn:active { transform: translateY(2px) scale(.98); box-shadow: 0 3px 8px var(--shadow-soft); }
  .moves-queue-row { min-height: 30px; flex-wrap: nowrap; overflow: hidden; }
  .queue-label { flex: 0 0 auto; font-size: .62rem; }
  .moves-queue { flex-wrap: nowrap; overflow-x: auto; min-height: 30px; padding: 2px; }
  .move-pill { min-width: 38px; min-height: 30px; display: grid; place-items: center; border-radius: 12px; }
  .status-text { min-height: 0; margin-top: 2px; padding: 2px 0; background: transparent; border: 0; color: var(--text-strong); font-size: .72rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

  .player-badge.active-turn { animation: mobileTurnPulse 1.25s ease-in-out infinite; }
  @keyframes mobileTurnPulse {
    0%,100% { transform: scale(1.08); box-shadow: 0 0 0 2px #fff, 0 0 12px 4px rgba(245,207,84,.72); }
    50% { transform: scale(1.2); box-shadow: 0 0 0 3px #fff, 0 0 22px 8px rgba(245,207,84,.9); }
  }
  .token { box-shadow: inset 0 0 0 2px rgba(0,0,0,.22), 0 5px 9px rgba(0,0,0,.48); }

  .lobby-overlay { padding: 14px 12px; }
  .lobby-input-row input, #saved-room-select { min-height: 48px; font-size: 1rem; }
  .play-btn { min-height: 48px; }
  .modal-card { width: calc(100% - 16px); max-height: 88dvh; border-radius: 22px; }
}

@media (min-width: 641px) and (max-width: 1024px) {
  .game-layout { padding-inline: 14px; }
  .mat-frame-wrap { border-radius: 18px; overflow: hidden; }
  .top-controls-row { gap: 10px; }
}

/* ------------------------------------------------------------------
   Landscape (phones turned sideways, tablets, desktop browsers): the
   board is the main thing. The bottom nav moves up into the top bar --
   inline tabs where they fit, a ☰ dropdown menu where they don't -- so
   every screen gets the full height, and the HUD moves into a column
   beside the board instead of stacking above it. fitBoard()
   (board-render.js) reads --side-hud to budget the board's space the
   same way.
   ------------------------------------------------------------------ */
@media (orientation: landscape) and (min-width: 640px) {
  :root { --navbar-h: 0px; }

  .shell-topbar-inner { max-width: none; }

  /* Nav tabs as pills in the top bar's row, right-aligned just left of the
     sound button (the nav can't live inside the header element itself: its
     backdrop-filter would become the containing block for the portrait
     layout's fixed bottom bar). */
  .app-tabbar {
    top: env(safe-area-inset-top); bottom: auto; left: auto;
    right: calc(14px + 36px + 10px + env(safe-area-inset-right));
    width: auto; height: var(--topbar-h); padding: 0; z-index: 171;
    align-items: center; background: transparent; border: 0; box-shadow: none;
  }
  .app-tabbar::after { display: none; }
  .app-tabbar-inner { display: flex; align-items: center; gap: 4px; width: auto; max-width: none; }
  .app-tabbar button, .app-tabbar a, .app-tabbar a.tab-home {
    flex-direction: row; gap: 6px; min-height: 36px; padding: 0 12px;
    border-radius: 999px; font-size: .78rem; white-space: nowrap;
  }
  .app-tabbar button .tab-icon, .app-tabbar a .tab-icon { position: relative; font-size: 1rem; }
  .tab-home-circle { display: contents; }
  .tab-dot { top: -3px; right: -6px; }
  /* The inline tabs sit over the right of the top bar (~500px with the
     sound button): the screen title must truncate before reaching them. */
  .hdr-title, .hdr-matchup { max-width: calc(100vw - 720px); }

  /* Lobby screens (Home, Rules, Results, ...) use the width instead of a
     phone-width column. */
  .lobby-overlay {
    padding: 16px calc(20px + env(safe-area-inset-right)) 16px calc(20px + env(safe-area-inset-left));
  }
  .shell-stack { max-width: 1100px; }
  /* Hug the rules text instead of stretching to the bottom; still scrolls
     internally once the text is taller than the screen. */
  .rules-card { flex: 0 1 auto; }
  .daily-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
  .store-grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
  .results-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .achv-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }

  .game-layout {
    --side-hud: 1;
    --game-side-w: clamp(230px, 24vw, 340px);
    display: grid; grid-template-columns: minmax(0, 1fr) var(--game-side-w); grid-template-rows: minmax(0, 1fr);
    align-items: center; justify-items: center; column-gap: 16px;
    width: auto; max-width: none; margin: 0; overflow: hidden;
    padding: 12px calc(12px + env(safe-area-inset-right)) 12px 12px;
  }
  .mat-frame-wrap { grid-column: 1; grid-row: 1; width: auto; max-width: none; }
  .game-side {
    grid-column: 2; grid-row: 1; align-self: stretch; width: 100%; min-height: 0;
    display: flex; flex-direction: column; justify-content: center; gap: 12px; overflow-y: auto;
  }
  .game-side .top-controls-row { flex-direction: column; gap: 12px; }
  .game-side .top-controls-row .hud-panel { flex: 0 0 auto; }
  /* The column is narrower than the old full-width row: cowries on one
     line, THROW / Pass / Nudge sharing the line below. */
  .dice-row { flex-wrap: wrap; }
  .cowries-box { flex: 1 0 100%; }
  .roll-btn, .pass-btn { flex: 1 1 0; }

  /* Team name and badges side by side, so the labels above and below the
     board cost one row each instead of two. */
  /* The gap scales with the board: the active player's badge pulses to
     ~1.4x its size and must not cover the team name next to it. */
  .team-label-top, .team-label-bottom {
    flex-direction: row; gap: max(14px, calc(24px * var(--board-scale, 1)));
    padding: max(6px, calc(10px * var(--board-scale, 1))) 12px;
  }
  .team-label-bottom { flex-direction: row-reverse; }
  .board-team-name { max-width: 40%; font-size: max(.66rem, calc(.85rem * var(--board-scale, 1))); }
}

/* Landscape but too narrow for the inline tabs: they go behind the ☰
   button as a dropdown under the top bar. */
@media (orientation: landscape) and (min-width: 640px) and (max-width: 899px) {
  .hdr-nav-btn { display: block; }
  .hdr-title, .hdr-matchup { max-width: none; }
  .app-tabbar {
    display: none; top: calc(var(--topbar-h) + env(safe-area-inset-top) + 6px);
    right: calc(8px + env(safe-area-inset-right)); height: auto; min-width: 200px;
    max-height: calc(100% - var(--topbar-h) - env(safe-area-inset-top) - 16px); overflow-y: auto;
    padding: 6px; border-radius: 14px;
    background: var(--surface-solid); border: 1px solid var(--border-gold); box-shadow: 0 12px 28px var(--shadow-strong);
  }
  .app-tabbar.open { display: flex; }
  .app-tabbar-inner { flex-direction: column; align-items: stretch; gap: 2px; width: 100%; }
  .app-tabbar button, .app-tabbar a, .app-tabbar a.tab-home {
    justify-content: flex-start; gap: 10px; min-height: 42px; padding: 0 14px; border-radius: 10px; font-size: .85rem;
  }
}

/* Phone landscape: only ~320-430px of height, so trim every bit of chrome. */
@media (orientation: landscape) and (min-width: 640px) and (max-height: 500px) {
  :root { --topbar-h: 44px; }

  .shell-topbar { padding-top: calc(3px + env(safe-area-inset-top)); padding-bottom: 3px; }
  .hdr-avatar { width: 34px; height: 34px; }
  .hdr-menu-btn { width: 32px; height: 32px; }
  .lobby-overlay { padding: 8px 12px; }

  .game-layout { --game-side-w: clamp(196px, 28vw, 250px); column-gap: 10px; padding: 6px calc(8px + env(safe-area-inset-right)) 6px 8px; }
  .game-side, .game-side .top-controls-row { gap: 8px; }
  .top-controls-row .hud-panel { padding: 8px 10px; }
  #unlock-note.invisible { display: none; }
  .cowrie { width: 20px; height: 28px; }
  .roll-btn, .pass-btn { padding: 8px 10px; }
  .status-text { min-height: 0; }

  .mat-frame-wrap { border-radius: 14px; overflow: hidden; }
  .mat-border-frame { display: none; }
  .embroidered-mat { border-width: 9px; border-radius: 14px; box-shadow: inset 0 0 16px rgba(68,42,22,.22); }
  .embroidered-mat::before { inset: 3px; border-width: 1px; }
  .corner-ornament { transform: scale(.72); }
  .team-label-top, .team-label-bottom { padding: 3px 10px; }
}

/* Wide screens have room to spare in the side column: show the match
   stats inline there instead of behind the drawer button. */
@media (orientation: landscape) and (min-width: 1100px) and (min-height: 620px) {
  .game-side .team-stats-card {
    position: static; transform: none; opacity: 1; visibility: visible; pointer-events: auto;
    width: 100%; max-height: none; transition: none;
  }
  .stats-drawer-btn, .game-drawer-close { display: none; }
}
