/* === Phase 2: Foundation === */
html {
  touch-action: manipulation;
}

body {
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  padding-top: env(safe-area-inset-top, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
}

body.drawer-open {
  overflow: hidden;
}

/* Touch device detection: larger tap targets */
@media (hover: none) and (pointer: coarse) {
  .keyboard button {
    min-width: 44px;
    min-height: 44px;
  }

  .modal-btn {
    min-height: 48px;
  }
}

/* Mobile layout overrides (shared) */
@media (max-width: 640px) {
  main {
    justify-content: space-between;
    padding-bottom: 0.5rem;
  }

  header {
    padding: 0.25rem 0.5rem;
    padding-top: calc(0.25rem + env(safe-area-inset-top, 0px));
    padding-left: calc(0.5rem + env(safe-area-inset-left, 0px));
    padding-right: calc(0.5rem + env(safe-area-inset-right, 0px));
  }

  main {
    padding: 0.5rem;
    padding-bottom: calc(0.5rem + env(safe-area-inset-bottom, 0px));
  }
}

/* === Phase 3: Hamburger Drawer === */
.hamburger-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 60;
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
}
.hamburger-overlay.open { opacity: 1; pointer-events: auto; }

.hamburger-drawer {
  position: fixed; top: 0; left: 0; bottom: 0; width: 280px;
  max-width: 80vw; background: var(--color-surface); z-index: 61;
  transform: translateX(-100%); transition: transform 0.25s ease;
  overflow-y: auto;
  padding-top: env(safe-area-inset-top, 0px);
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  padding: 1rem;
  padding-top: calc(1rem + env(safe-area-inset-top, 0px));
  box-shadow: 8px 0 0 rgba(0, 0, 0, 0.12);
}
.hamburger-drawer.open { transform: translateX(0); }

.hamburger-drawer-section {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.hamburger-drawer-section-title {
  font-size: var(--font-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.hamburger-drawer .radio-pills {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
}

.hamburger-drawer .radio-pills .radio-pill {
  flex: 1;
  text-align: center;
  min-width: 60px;
}

.hamburger-drawer .drawer-btn {
  display: block;
  width: 100%;
  padding: 0.6rem 0.8rem;
  background: none;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  color: var(--color-text-muted);
  font-size: var(--font-sm);
  font-family: inherit;
  cursor: pointer;
  text-align: left;
  transition: background 0.1s, color 0.1s, border-color 0.1s;
}

.hamburger-drawer .drawer-btn:hover {
  background: rgba(255,255,255,0.04);
  color: var(--color-text);
  border-color: var(--color-border-hover);
}

.hamburger-drawer .drawer-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.3rem;
}

.hamburger-drawer .drawer-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  padding: 0.4rem;
  background: rgba(255,255,255,0.03);
  border-radius: 4px;
}

.hamburger-drawer .drawer-stat-value {
  font-size: var(--font-lg);
  font-weight: 700;
  color: var(--color-text);
}

.hamburger-drawer .drawer-stat-label {
  font-size: var(--font-xs);
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Header mobile layout */
@media (max-width: 640px) {
  .header-row {
    grid-template-columns: auto 1fr auto !important;
  }

  .header-row > h1 {
    grid-column: 2;
    order: 0;
    font-size: var(--font-title);
  }

  .header-left,
  .game-controls,
  .header-right {
    grid-column: auto;
    justify-self: auto;
  }

  .game-controls {
    display: none;
  }

  .header-left {
    display: flex;
    align-items: center;
  }

  .header-left .game-btn,
  .header-left .theme-selector {
    display: none;
  }

  .header-left .game-btn,
  .header-left .theme-selector {
    display: none;
  }

  #hamburger-btn-mobile {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .header-right .phase-label,
  .header-right .hamburger-menu {
    display: none;
  }
}

@media (min-width: 641px) {
  .hamburger-overlay,
  .hamburger-drawer {
    display: none !important;
  }

  #hamburger-btn-mobile {
    display: none !important;
  }
}

/* === Phase 4: Collapse Stats Bar === */
@media (max-width: 640px) {
  .stats-bar {
    display: none;
  }
  .stats-bar.visible {
    display: flex;
  }
}

/* === Phase 6: Keyboard Spacers === */
@media (max-width: 640px) {
  .keyboard .row[data-row="1"]::before,
  .keyboard .row[data-row="2"]::before {
    display: none;
  }

  .keyboard .row {
    gap: clamp(3px, 1.5vw, 6px);
  }

  .keyboard {
    overflow: hidden;
    width: 100%;
    max-width: 100%;
  }

  .keyboard button {
    min-width: 0;
    min-height: 44px;
  }
}

/* === Phase 7: MP Sidebar Removal + Compact Score === */
@media (max-width: 640px) {
  .sidebar-left,
  .sidebar-right {
    display: none !important;
  }

  .mp-compact-score {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    font-size: var(--font-sm);
    padding: 0.25rem 0;
  }

  .mp-compact-score .vs {
    color: var(--color-yellow);
    font-weight: 700;
  }
}

@media (min-width: 641px) {
  .mp-compact-score {
    display: none !important;
  }
}

/* === Phase 8: MP Mini Opponent Board === */
.opponent-board-toggle {
  width: 100%;
  text-align: center;
  padding: 0.3rem;
  font-size: var(--font-xs);
  color: var(--color-text-muted);
  cursor: pointer;
  background: none;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  font-family: inherit;
  transition: border-color 0.15s, color 0.15s;
}

.opponent-board-toggle:hover {
  border-color: var(--color-border-hover);
  color: var(--color-text);
}

.opponent-board-compact {
  transition: max-height 0.3s ease;
  overflow: hidden;
  max-height: 0;
}

.opponent-board-compact.expanded {
  max-height: 300px;
}

.opponent-board-compact .board .tile {
  width: 32px;
  height: 32px;
  font-size: 0.9rem;
  border-width: 2px;
}

/* === Phase 9: Bottom Sheet Modals === */
@media (max-width: 640px) {
  .result-overlay {
    align-items: flex-end;
  }

  .result-modal {
    width: 100%;
    max-width: 100%;
    border-radius: 16px 16px 0 0;
    padding: 2rem 1.5rem;
    padding-bottom: calc(2rem + env(safe-area-inset-bottom, 0px));
    transform: translateY(100%);
    transition: transform 0.3s ease;
    margin-top: auto;
  }

  .result-overlay:not(.hidden) .result-modal {
    transform: translateY(0);
  }

  .result-stats {
    justify-content: space-around;
    width: 100%;
  }

  .modal-btn {
    width: 100%;
    text-align: center;
  }

  .result-overlay.hidden .result-modal {
    transform: translateY(100%);
  }
}

/* === Phase 10: Touch Polish === */
.game-container {
  overscroll-behavior: none;
  touch-action: pan-y;
}

@media (prefers-reduced-motion: reduce) {
  .tile, .modal-btn, .keyboard button {
    animation: none !important;
    transition: none !important;
  }

  .hamburger-drawer,
  .hamburger-overlay,
  .result-modal {
    transition: none !important;
  }
}

/* === Phase 11: Landscape === */
@media (orientation: landscape) and (max-height: 500px) and (max-width: 900px) {
  main {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .board {
    flex: 0 0 auto;
  }

  .keyboard {
    flex: 1;
    min-width: 200px;
  }

  .keyboard .row {
    gap: 2px;
  }

  header {
    display: none;
  }

  :root {
    --tile-size: 48px;
    --tile-font: 1.3rem;
  }
}

@media (orientation: landscape) and (min-width: 640px) {
  .game-area {
    flex-direction: row;
  }
}

/* Final responsive pass. Keep safe-area handling on the page edges only; the
   header and main add their own spacing so notched phones do not get doubled
   top/bottom padding. */
body {
  padding-top: 0;
  padding-bottom: 0;
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}

@media (max-width: 640px) {
  header {
    min-height: 3.25rem;
  }

  .header-row {
    min-height: 2.75rem;
    width: 100%;
  }

  .header-row > h1 {
    max-width: 100%;
    letter-spacing: 0.12em;
  }

  #hamburger-btn-mobile {
    z-index: 21;
  }

  .stats-bar.visible {
    max-width: 100%;
    padding-inline: 0.25rem;
  }

  .keyboard .row {
    width: 100%;
    gap: clamp(3px, 1.5vw, 6px);
  }

  .keyboard button {
    min-width: 0;
    min-height: 44px;
    height: max(44px, var(--key-size));
    flex: 1 1 0;
    max-width: none;
  }

  .keyboard button.wide {
    flex: 1.5 1 0;
    max-width: none;
  }

  .keyboard button[data-key="ENTER"] {
    flex: 2 1 0;
    max-width: none;
  }

  .keyboard {
    margin-bottom: clamp(2.5rem, 8vh, 4rem);
  }

  #timer-strip .timer:not(.hidden) {
    position: absolute;
    top: 0.35rem;
    left: max(0.5rem, env(safe-area-inset-left, 0px));
    transform: none;
    margin: 0;
    padding: 0.2rem 0.8rem;
    background: color-mix(in srgb, var(--color-surface), transparent 12%);
    border: 1px solid var(--color-border);
    border-radius: 999px;
    font-size: 1.25rem;
    line-height: 1.2;
  }

  .keyboard button.wide {
    max-width: var(--key-wide-size);
    white-space: nowrap;
    letter-spacing: -0.04em;
    overflow: hidden;
  }

  .keyboard button[data-key="ENTER"] {
    max-width: var(--key-enter-size);
  }

  .result-modal {
    max-height: min(88dvh, 640px);
    overflow-y: auto;
  }

  .hamburger-drawer {
    width: min(320px, 88vw);
    padding-left: max(1rem, env(safe-area-inset-left, 0px));
    padding-right: 1rem;
  }

  /* Theme names vary in length between languages. A bounded grid with
     wrapping labels is safer than flex pills that can shrink below the text
     width. */
  #drawer-theme-selector {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.4rem;
    width: 100%;
  }

  #drawer-theme-selector .radio-pill {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    width: 100%;
    min-height: 2.25rem;
    padding: 0.45rem 0.35rem;
    text-align: center;
    line-height: 1.15;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  /* Match the desktop controls' semantic accents inside the drawer. */
  .hamburger-drawer .radio-pill.active {
    border-color: var(--color-green);
    color: var(--color-text);
    background: color-mix(in srgb, var(--color-green), transparent 84%);
  }

  #drawer-mode-selector .radio-pill:has(input[value="rush"]).active,
  #drawer-difficulty-selector .radio-pill[data-difficulty="hard"].active {
    border-color: var(--color-red);
    background: color-mix(in srgb, var(--color-red), transparent 84%);
  }

  #drawer-difficulty-selector .radio-pill[data-difficulty="normal"].active {
    border-color: var(--color-yellow);
    background: color-mix(in srgb, var(--color-yellow), transparent 84%);
  }

  /* Keep the multiplayer game phase tall enough to anchor the keyboard in
     the lower thumb zone, just like the single-player screen. */
  #game-phase {
    width: 100%;
    height: 100%;
    min-height: 0;
    overflow: hidden;
    justify-content: space-between;
  }

  #menu-phase,
  #queue-phase {
    flex: 1;
    justify-content: center;
  }

  html.multi-puzzle {
    overflow-y: auto;
  }

  body.multi-puzzle {
    height: auto;
    min-height: 100dvh;
    overflow-y: auto;
  }

  body.multi-puzzle main {
    overflow: visible;
    justify-content: flex-start;
  }

  body.multi-puzzle .board.board-layout {
    grid-template-columns: repeat(var(--board-columns, 2), minmax(0, 1fr));
    max-width: 100%;
    gap: 0.5rem;
  }

  body.multi-puzzle .board.board-layout.variant-double {
    max-width: min(100%, 520px);
  }

  body.multi-puzzle .target-board {
    width: 100%;
    justify-self: center;
  }

  body.multi-puzzle .target-board-grid {
    padding: 0.1rem;
  }

  body.multi-puzzle .target-board-grid .tile {
    border-width: 2px;
  }
}

@media (orientation: landscape) and (max-height: 500px) and (max-width: 900px) {
  main {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    justify-content: center;
    gap: 0.5rem 1rem;
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
  }

  .keyboard {
    margin-bottom: 0;
  }

  .board {
    flex: 0 0 auto;
  }

  .keyboard {
    flex: 1 1 280px;
    width: min(100%, 520px);
    min-width: 280px;
  }

  header {
    display: none;
  }
}

/* Keep the on-screen keyboard inside the viewport after all narrow-screen
   overrides. Grid columns are deterministic, including the weighted action
   keys, so puzzle layouts cannot make the keyboard wider than the page. */
@media (max-width: 640px) {
  .keyboard {
    width: 100%;
    max-width: 100%;
  }

  .keyboard .row {
    display: grid;
    width: 100%;
    min-width: 0;
  }

  .keyboard .row[data-row="0"] {
    grid-template-columns: repeat(10, minmax(0, 1fr));
  }

  .keyboard .row[data-row="1"] {
    grid-template-columns: repeat(9, minmax(0, 1fr));
  }

  .keyboard .row[data-row="2"] {
    grid-template-columns: 2fr repeat(7, minmax(0, 1fr)) 1.5fr;
  }

  .keyboard button,
  .keyboard button.wide,
  .keyboard button[data-key="ENTER"] {
    width: auto;
    min-width: 0;
    max-width: none !important;
    flex: none !important;
  }
}
