/* Мобильная адаптация */

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  padding:
    max(4px, env(safe-area-inset-top))
    max(2px, env(safe-area-inset-right))
    max(4px, env(safe-area-inset-bottom))
    max(2px, env(safe-area-inset-left));
  overflow-x: hidden;
  touch-action: manipulation;
}

#app {
  min-height: auto;
  width: 100%;
}

button,
.level-btn,
.cell {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button {
  min-height: 44px;
}

@media (max-width: 768px) {
  h1 {
    font-size: 1.35rem;
    padding: 0 8px;
  }

  .menu-buttons {
    flex-wrap: wrap;
    gap: 4px;
    padding: 0 2px;
    width: 100%;
    max-width: 420px;
    margin-inline: auto;
  }

  .menu-buttons button {
    flex: 1 1 calc(50% - 4px);
    min-width: 140px;
  }

  #level-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 3px;
    width: 100%;
    max-width: 100%;
    padding: 0 2px;
    box-sizing: border-box;
  }

  .level-btn {
    width: 100%;
    height: 50px;
    font-size: 14px;
  }

  .game-screen {
    display: flex;
    flex-direction: column;
    min-height: calc(100dvh - 24px);
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .topbar {
    display: flex;
    flex-direction: column;
    gap: 4px;
    justify-content: center;
    width: 100%;
    max-width: 100%;
    padding: 0 2px;
    flex-shrink: 0;
  }

  .topbar-buttons {
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: 100%;
  }

  .topbar-nav-buttons {
    display: flex;
    gap: 4px;
    justify-content: center;
    width: 100%;
  }

  .topbar-leave {
    width: 100%;
  }

  .topbar-title {
    width: 100%;
    font-size: 14px !important;
    line-height: 1.35;
    padding: 0 3px;
    text-align: center;
  }

  .topbar-prev,
  .topbar-next {
    flex: 1;
  }

  .topbar-prev,
  .topbar-next,
  .topbar-leave {
    width: 100%;
    font-size: 13px;
    padding: 6px 4px;
  }

  .board-area {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 0;
    padding: 2px 0;
  }

  .board-scroll-wrap {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    justify-content: center;
    padding: 0 2px;
    box-sizing: border-box;
  }

  .game-play-area {
    min-height: 0;
  }

  .board-area {
    min-width: 0;
  }

  .game-hud {
    flex-shrink: 0;
    width: 100%;
    padding: 4px 2px 2px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  body:has(.mobile-audio-bar) #level-grid {
    margin-bottom: 0;
  }

  body:has(.mobile-audio-bar) .victory-sound-page,
  body:has(.mobile-audio-bar) #intro-screen {
    padding-bottom: 0;
  }

  .stats {
    margin-top: 0;
    font-size: 12px;
  }

  .stat-item {
    padding: 4px 3px;
    font-size: 11px;
    line-height: 1.25;
    min-height: 30px;
    min-width: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    text-align: left;
    box-sizing: border-box;
  }

  #message {
    font-size: 15px;
    margin-top: 4px;
    padding: 0 3px;
  }

  .mobile-audio-bar {
    position: relative;
    left: auto;
    right: auto;
    bottom: auto;
    z-index: 1;
    display: flex;
    flex-direction: row;
    gap: 4px;
    padding: 4px 6px;
    border-radius: 14px;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.16) 0%, rgba(255, 255, 255, 0.06) 100%);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.28);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    margin-top: 4px;
    justify-content: center;
    flex-wrap: wrap;
  }

  .mobile-audio-row {
    display: flex;
    align-items: center;
    gap: 4px;
    min-width: 0;
    flex: 1;
    min-width: 100px;
  }

  .mobile-audio-btn {
    width: 44px;
    height: 44px;
    min-height: 44px;
    padding: 0;
    border-radius: 10px;
    font-size: 18px;
    flex-shrink: 0;
  }

  .mobile-audio-slider {
    flex: 1 1 auto;
    min-width: 40px;
    max-width: 100%;
    height: 28px;
  }

  .floating-audio-controls {
    top: max(12px, env(safe-area-inset-top)) !important;
    bottom: auto !important;
    right: max(12px, env(safe-area-inset-right)) !important;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .audio-volume-popup {
    bottom: 100% !important;
    top: auto !important;
    right: 0 !important;
    margin-right: 0 !important;
    margin-bottom: 6px !important;
  }

  .audio-volume-slider {
    width: 120px !important;
  }

  #win-screen {
    padding: 8px;
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  #win-screen .win-title {
    font-size: 18px;
    line-height: 1.3;
  }

  #win-screen .win-stat-value {
    font-size: 24px;
  }

  .impeachment-title {
    font-size: 18px;
    line-height: 1.3;
  }

  #intro-screen {
    min-height: 100dvh;
    padding:
      max(6px, env(safe-area-inset-top))
      6px
      max(6px, env(safe-area-inset-bottom));
  }

  .intro-hero {
    max-width: min(360px, 92vw);
  }

  .intro-image {
    max-height: 42dvh;
  }

  .intro-start-btn,
  .intro-music-btn {
    min-height: 48px;
  }

  #skin-grid,
  #theme-grid {
    grid-template-columns: minmax(0, 1fr);
    max-width: 300px;
    width: 100%;
    padding: 0 8px;
    box-sizing: border-box;
  }

  .victory-sound-page {
    min-height: calc(100dvh - 80px);
  }

  .records-table-container {
    padding: 0 4px;
    width: 100%;
    box-sizing: border-box;
  }

  .records-table th,
  .records-table td {
    padding: 8px 6px;
    font-size: 12px;
  }

  #footer {
    font-size: 11px;
    line-height: 1.4;
    padding: 4px;
    margin-top: 4px;
  }

  body:has(#board) #footer {
    display: block;
    margin-top: 4px;
  }

  .cell .label {
    font-size: calc(var(--cell-font-size) - 1px);
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 0 2px;
    box-sizing: border-box;
  }

  /* Reduce spacing in game stage for mobile */
  .game-stage {
    gap: 6px;
  }

  .game-stage .stats {
    margin-top: 6px;
    padding: 0 6px;
    box-sizing: border-box;
    width: 100%;
    position: static !important;
    left: auto !important;
    top: auto !important;
    right: auto !important;
    transform: none !important;
    /* FIX: выключаем CSS Grid на мобиле — раскладку держит Bootstrap (.row > .col-6) */
    display: block;
    grid-template-columns: none;
  }

  .game-stage .stats .stat-item {
    padding: 6px 8px;
    font-size: 11px;
    line-height: 1.3;
    min-height: 32px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    text-align: left;
    box-sizing: border-box;
    white-space: normal;
    overflow: hidden;
    text-overflow: ellipsis;
    word-wrap: break-word;
    word-break: break-word;
  }

  /* Reduce spacing in victory sound page */
  .victory-sound-page {
    gap: 6px;
    padding: 10px 8px;
  }

  #victory-sound-grid {
    gap: 10px;
    margin: 6px auto;
  }

  .victory-sound-card {
    padding: 12px 10px;
  }

  .victory-sound-icon {
    font-size: 32px;
    margin-bottom: 6px;
  }

  .victory-sound-name {
    font-size: 14px;
    margin-bottom: 4px;
  }

  .victory-sound-desc {
    font-size: 12px;
    margin-bottom: 8px;
    min-height: 32px;
  }

  .victory-sound-actions {
    gap: 4px;
  }

  /* Reduce spacing in skin and theme grids */
  #skin-grid,
  #theme-grid {
    gap: 10px;
    margin: 10px auto;
  }

  .skin-card,
  .theme-card {
    padding: 10px;
  }

  .skin-preview {
    width: 60px;
    height: 60px;
    margin-bottom: 6px;
  }

  .skin-name,
  .theme-name {
    font-size: 14px;
    margin-bottom: 4px;
  }

  .skin-desc,
  .theme-desc {
    font-size: 12px;
    margin-bottom: 8px;
    min-height: 32px;
  }

  /* Reduce button margins */
  .skin-btn,
  .back-btn {
    margin: 6px auto;
    padding: 8px 16px;
    font-size: 14px;
  }

  .menu-buttons {
    gap: 6px;
    margin: 6px auto;
  }

  .theme-btn,
  .victory-sound-btn {
    padding: 8px 16px;
    font-size: 14px;
  }

  /* Reduce intro controls spacing */
  .intro-controls {
    gap: 8px;
  }

  .intro-title {
    font-size: 24px;
    margin: 12px 0;
  }

  .intro-title-image {
    margin: 4px 0;
  }

  /* Reduce records table spacing */
  .records-table-container {
    margin: 10px auto;
  }

  .records-table th,
  .records-table td {
    padding: 6px 4px;
    font-size: 11px;
  }

  /* Reduce win stat row spacing */
  .win-stat-row {
    padding: 10px 12px;
  }

  .win-stat-header {
    gap: 8px;
    margin-bottom: 2px;
  }

  .win-stat-detail {
    margin-top: 4px;
    font-size: 12px;
  }
}

@media (max-width: 400px) {
  #level-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .topbar-prev,
  .topbar-next,
  .topbar-leave {
    font-size: 12px;
    padding: 6px 4px;
  }

  .mobile-audio-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .mobile-audio-row {
    min-width: 100%;
    flex: none;
  }

  .mobile-audio-slider {
    min-width: 50px;
    max-width: 100%;
  }

  .mobile-audio-btn {
    width: 36px;
    height: 36px;
    min-height: 36px;
    font-size: 16px;
  }

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

.mobile-audio-bar {
  display: none;
}

@media (max-width: 768px) {
  .mobile-audio-bar {
    display: flex;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  .game-screen {
    min-height: auto;
  }

  .game-hud {
    padding-bottom: calc(72px + env(safe-area-inset-bottom));
  }

  .stats {
    font-size: 11px;
  }

  .stat-item {
    min-height: 32px;
    padding: 4px 6px;
    min-width: 0;
    font-size: 10px;
  }

  .topbar-title {
    font-size: 12px !important;
  }
}
