/* Мобильная адаптация */

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) {
  body, #app, .stat-item, .win-btn, .intro-music-btn, .level-btn, h1, p {
    -webkit-font-smoothing: antialiased;
    text-rendering: geometricPrecision;
  }

  @keyframes jump-mobile {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-5px); }
  }

  .cell[data-mood="-6"] img.cat,
  .cell[data-mood="-5"] img.cat,
  .cell[data-mood="-4"] img.cat,
  .cell[data-mood="-3"] img.cat,
  .cell[data-mood="-2"] img.cat,
  .cell[data-mood="-1"] img.cat,
  .cell[data-mood="0"] img.cat {
    animation-name: jump-mobile;
  }


  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-row {
    display: flex;
    flex-direction: column;
    gap: 4px;
    justify-content: center;
    width: 100%;
  }

  .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-subtitle {
    font-size: 12px;
  }

  .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: rgba(30, 30, 30, 0.92);
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    border: 1px solid rgba(255, 255, 255, 0.15);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    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 {
    overflow-y: auto;
    padding: 16px;
    padding-top: max(16px, env(safe-area-inset-top));
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  #win-screen img {
    width: 50vw;
    max-width: 200px;
    max-height: 30vh;
  }

  #win-screen .win-title {
    font-size: 18px;
    line-height: 1.3;
    padding-right: 0;
  }

  #win-screen .win-stats {
    padding-right: 0;
    max-width: 90vw;
    display: flex;
    flex-direction: column;
  }

  #win-screen .win-stat-value {
    font-size: 24px;
  }

  #win-screen .win-btn {
    width: 100%;
  }

  #win-screen > div:last-child {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
    max-width: 90vw;
  }

  #impeachment-screen {
    overflow-y: auto;
  }

  #impeachment-screen .win-stats {
    padding-right: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    max-width: 90vw;
    border-radius: 12px;
  }

  .impeachment-title {
    font-size: 18px;
    line-height: 1.3;
  }

  .impeachment-video {
    max-height: min(40vh, 280px);
  }

  .impeachment-btns {
    flex-direction: column;
    width: 100%;
    max-width: 90vw;
  }

  .impeachment-btns .win-btn {
    width: 100%;
    margin-left: 0 !important;
  }

  #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);
    gap: 8px;
  }

  .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;
  }

  .rocket-btn-disabled { opacity: 0.5; cursor: default; }

  .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: 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;
  }

  .result-collapse-btn {
    position: static;
    order: -1;
    align-self: flex-end;
    width: 44px;
    height: 44px;
    padding: 0;
    font-size: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .result-collapse-btn::before {
    content: attr(data-compact);
    font-size: 20px;
  }
}

@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;
  }
}

/* Version label for mobile */
@media (max-width: 768px) {
  .version-label {
    bottom: max(6px, env(safe-area-inset-bottom));
    right: max(8px, env(safe-area-inset-right));
    /* If mobile audio bar is present, move to left to avoid overlap */
    left: auto;
  }
  
  body:has(.mobile-audio-bar) .version-label {
    right: auto;
    left: max(8px, env(safe-area-inset-left));
  }
}

/* King rain animation for mobile */
@media (max-width: 768px) {
  .king-rain-item {
    font-size: 20px;
  }
}

/* Stats screen mobile adjustments */
@media (max-width: 768px) {
  .stats-grid {
    margin: 16px auto;
  }

  .stats-card {
    padding: 12px 8px;
    min-height: 70px;
    font-size: 12px;
  }

  .stats-card > div:last-child {
    font-size: 1.3em;
  }
}

@media (max-width: 400px) {
  .stats-card {
    padding: 10px 6px;
    min-height: 60px;
    font-size: 11px;
  }

  .stats-card > div:last-child {
    font-size: 1.2em;
  }

  .kings-total-banner {
    font-size: 15px;
    padding: 8px 12px;
  }
}
