/* =========================
   MOBILE-FIRST RESPONSIVE DESIGN v2.0
   Completely rewritten for optimal mobile playability
   ========================= */

/* =========================
   BASE MOBILE STYLES (0-600px)
   Design for mobile first, then scale up
   ========================= */

/* Prevent zoom and enable smooth scrolling */
html {
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
}

/* Mobile viewport height fix for iOS */
:root {
  --vh: 1vh;
  --app-height: 100vh;
}

@supports (-webkit-touch-callout: none) {
  :root {
    --app-height: -webkit-fill-available;
  }
}

/* =========================
   MOBILE-OPTIMIZED LAYOUT (< 600px)
   ========================= */
@media (max-width: 600px) {
  /* Container optimization */
  #game-container {
    width: 100vw;
    min-height: var(--app-height);
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  /* =========================
     CARDS - Larger and touch-friendly
     ========================= */
  .card {
    width: 70px !important;
    height: 105px !important;
    min-width: 70px;
    min-height: 105px;
    font-size: 1.1rem;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
  }

  /* Card hover effect for mobile */
  .card:active {
    transform: scale(0.98);
    box-shadow: 0 2px 8px rgba(0,0,0,0.6);
  }

  /* Card suit icons - larger and centered */
  .card .suit {
    font-size: 2.2rem !important;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
  }

  /* Card corner numbers - proportional */
  .card::before,
  .card::after {
    font-size: 1rem !important;
    font-weight: bold;
    line-height: 1;
  }

  .card::before {
    top: 4px;
    left: 4px;
  }

  .card::after {
    bottom: 4px;
    right: 4px;
  }

  /* Hand layout - better spacing */
  .hand {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    align-items: center;
    min-height: 110px;
    padding: 8px 4px;
  }

  /* =========================
     GAME TABLE LAYOUT
     ========================= */
  .game-table {
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 100%;
    padding: 12px 8px;
    background: rgba(0, 50, 0, 0.3);
    border-radius: 16px;
    flex: 1;
    min-height: 0;
  }

  /* Dealer area */
  .dealer-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 12px 8px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 12px;
  }

  #dealer-hand {
    min-height: 110px;
  }

  #dealer-value {
    font-size: 1.2rem;
    font-weight: bold;
    color: #ffd700;
    text-shadow: 0 2px 4px rgba(0,0,0,0.8);
  }

  /* Deck */
  .deck {
    width: 70px;
    height: 105px;
    min-width: 70px;
    min-height: 105px;
    border-radius: 8px;
  }

  /* Players area */
  .players-area {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    max-height: 40vh;
  }

  .player-spot {
    background: rgba(0, 0, 0, 0.4);
    border: 2px solid #444;
    border-radius: 12px;
    padding: 12px;
    min-height: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .player-spot.active-player {
    border-color: #ffd700;
    border-width: 3px;
    box-shadow: 0 0 20px rgba(255, 215, 0, 0.4);
    transform: none;
  }

  .player-name {
    font-size: 1.1rem;
    font-weight: bold;
    color: #fff;
    text-shadow: 0 2px 4px rgba(0,0,0,0.8);
  }

  .player-chips {
    font-size: 0.95rem;
    color: #ffd700;
  }

  /* =========================
     ACTION BAR - Touch-optimized
     ========================= */
  .action-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(to top, rgba(10,10,10,0.98), rgba(10,10,10,0.95));
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 2px solid rgba(255, 215, 0, 0.3);
    padding: 16px 12px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
    align-items: center;
    z-index: 9999;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.8);
  }

  /* Touch-friendly buttons */
  .action-bar button {
    min-height: 52px;
    min-width: 52px;
    font-size: 1.05rem;
    font-weight: bold;
    padding: 14px 18px;
    border-radius: 12px;
    border: 2px solid rgba(255, 255, 255, 0.2);
    background: linear-gradient(135deg, #4a4a4a, #2a2a2a);
    color: #fff;
    text-shadow: 0 2px 4px rgba(0,0,0,0.6);
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: all 0.2s ease;
  }

  .action-bar button:active {
    transform: scale(0.95);
    box-shadow: 0 2px 6px rgba(0,0,0,0.6);
  }

  .action-bar button:not(.hidden) {
    flex: 1 1 calc(50% - 8px);
    max-width: 160px;
  }

  /* Primary action button (HIT) */
  #hit-btn {
    background: linear-gradient(135deg, #4CAF50, #2d7a30);
    border-color: #4CAF50;
  }

  /* Secondary action button (STAND) */
  #stand-btn {
    background: linear-gradient(135deg, #f44336, #c62828);
    border-color: #f44336;
  }

  /* Tertiary action buttons */
  #double-btn,
  #split-btn {
    background: linear-gradient(135deg, #2196F3, #1565C0);
    border-color: #2196F3;
  }

  #insurance-btn,
  #surrender-btn {
    background: linear-gradient(135deg, #FF9800, #E65100);
    border-color: #FF9800;
  }

  /* =========================
     BETTING CONTROLS - Redesigned
     ========================= */
  #betting-controls,
  .betting-controls {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(to top, rgba(15,15,15,0.98), rgba(15,15,15,0.95));
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 2px solid rgba(255, 215, 0, 0.4);
    padding: 20px 16px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
    z-index: 9999;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.8);
    display: flex !important;
    flex-direction: column;
    gap: 12px;
    visibility: visible !important;
    opacity: 1 !important;
  }

  .betting-controls.hidden {
    display: none !important;
  }

  .bet-label {
    font-size: 1rem;
    color: #fff;
    text-align: center;
    margin-bottom: 8px;
    text-shadow: 0 2px 4px rgba(0,0,0,0.8);
  }

  .bet-input-row {
    display: flex;
    gap: 12px;
    align-items: stretch;
  }

  #bet-amount {
    flex: 1;
    min-height: 52px;
    font-size: 1.2rem;
    padding: 14px 16px;
    border-radius: 12px;
    border: 2px solid rgba(255, 215, 0, 0.4);
    background: rgba(30, 30, 30, 0.9);
    color: #ffd700;
    text-align: center;
    font-weight: bold;
  }

  #place-bet {
    min-height: 52px;
    min-width: 120px;
    font-size: 1.15rem;
    font-weight: bold;
    padding: 14px 24px;
    border-radius: 12px;
    border: 2px solid #ffd700;
    background: linear-gradient(135deg, #ffd700, #f9a825);
    color: #000;
    text-shadow: none;
    box-shadow: 0 4px 16px rgba(255, 215, 0, 0.4);
    touch-action: manipulation;
    transition: all 0.2s ease;
  }

  #place-bet:active {
    transform: scale(0.95);
    box-shadow: 0 2px 8px rgba(255, 215, 0, 0.6);
  }

  /* =========================
     HUD - Simplified for mobile
     ========================= */
  .game-hud {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-radius: 12px;
    font-size: 0.95rem;
  }

  .chips-count,
  .key-count {
    font-size: 1rem;
    font-weight: bold;
    padding: 6px 12px;
    background: rgba(0, 0, 0, 0.4);
    border-radius: 8px;
  }

  /* Hide desktop navigation on mobile */
  .desktop-nav {
    display: none !important;
  }

  /* =========================
     MOBILE NAVIGATION BAR
     ========================= */
  .mobile-nav {
    display: flex !important;
    gap: 8px;
    padding: 12px;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-radius: 12px;
    justify-content: space-between;
  }

  .mobile-nav button {
    flex: 1;
    min-height: 48px;
    font-size: 1rem;
    font-weight: bold;
    padding: 12px 16px;
    border-radius: 10px;
    border: 2px solid rgba(255, 255, 255, 0.2);
    background: linear-gradient(135deg, #4a4a4a, #2a2a2a);
    color: #fff;
    touch-action: manipulation;
    transition: all 0.2s ease;
  }

  .mobile-nav button:active {
    transform: scale(0.95);
  }

  #shop-btn-mobile {
    background: linear-gradient(135deg, #9C27B0, #6A1B9A);
    border-color: #9C27B0;
  }

  /* =========================
     CHAT - Mobile optimized
     ========================= */
  #chat-panel {
    position: fixed;
    bottom: -100%;
    left: 0;
    right: 0;
    height: 50vh;
    max-height: 50vh;
    background: rgba(10, 10, 10, 0.98);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-radius: 20px 20px 0 0;
    border-top: 2px solid rgba(255, 215, 0, 0.3);
    box-shadow: 0 -4px 30px rgba(0,0,0,0.8);
    z-index: 10000;
    transition: bottom 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
    display: flex;
    flex-direction: column;
  }

  #chat-panel.open {
    bottom: 0 !important;
  }

  .chat-header {
    padding: 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .chat-header h3 {
    font-size: 1.2rem;
    margin: 0;
  }

  .chat-close-btn {
    min-width: 44px;
    min-height: 44px;
    font-size: 1.5rem;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    border-radius: 50%;
    color: #fff;
    cursor: pointer;
    touch-action: manipulation;
  }

  #chat-messages {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px;
    font-size: 0.95rem;
  }

  #chat-form {
    padding: 12px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    gap: 8px;
  }

  #chat-input {
    flex: 1;
    min-height: 44px;
    font-size: 1rem;
    padding: 12px;
    border-radius: 22px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    background: rgba(30, 30, 30, 0.9);
    color: #fff;
  }

  #chat-form button {
    min-width: 44px;
    min-height: 44px;
    padding: 12px 20px;
    border-radius: 22px;
    background: linear-gradient(135deg, #4CAF50, #2d7a30);
    border: none;
    color: #fff;
    font-weight: bold;
    touch-action: manipulation;
  }

  /* Chat toggle button */
  .bj-chat-toggle {
    position: fixed;
    bottom: calc(80px + env(safe-area-inset-bottom));
    right: 16px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2196F3, #1565C0);
    border: 3px solid rgba(255, 255, 255, 0.3);
    color: #fff;
    font-size: 1.5rem;
    box-shadow: 0 4px 16px rgba(0,0,0,0.4);
    z-index: 9998;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    touch-action: manipulation;
    transition: all 0.3s ease;
  }

  .bj-chat-toggle:active {
    transform: scale(0.9);
  }

  .bj-chat-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #f44336;
    color: #fff;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: bold;
  }

  .bj-chat-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 9999;
    display: none;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
  }

  .bj-chat-overlay.active {
    display: block;
  }

  /* =========================
     MODALS - Mobile optimized
     ========================= */
  .modal-content {
    width: 92vw;
    max-width: 420px;
    max-height: 85vh;
    margin: 20px auto;
    padding: 24px;
    border-radius: 20px;
    background: linear-gradient(135deg, #1e1e1e, #2a2a2a);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .modal-content h2 {
    font-size: 1.5rem;
    margin-bottom: 20px;
  }

  .modal-content input {
    min-height: 48px;
    font-size: 1.1rem;
    padding: 14px;
    border-radius: 12px;
    margin-bottom: 16px;
    width: 100%;
  }

  .modal-content button {
    min-height: 52px;
    font-size: 1.15rem;
    font-weight: bold;
    padding: 16px;
    border-radius: 12px;
    width: 100%;
    touch-action: manipulation;
  }

  /* =========================
     LEADERBOARD & WAITING LIST
     ========================= */
  .leaderboard {
    position: static;
    width: 100%;
    max-height: 200px;
    border-radius: 12px;
    margin: 0;
  }

  .waiting-list {
    width: 100%;
    border-radius: 12px;
    margin: 0;
  }

  /* =========================
     SAFE AREA SUPPORT
     ========================= */
  @supports (padding: max(0px)) {
    .action-bar {
      padding-bottom: max(16px, env(safe-area-inset-bottom) + 8px);
    }

    #betting-controls,
    .betting-controls {
      padding-bottom: max(20px, env(safe-area-inset-bottom) + 12px);
    }

    #chat-form {
      padding-bottom: max(12px, env(safe-area-inset-bottom) + 4px);
    }

    .bj-chat-toggle {
      bottom: max(80px, env(safe-area-inset-bottom) + 72px);
    }
  }

  /* =========================
     KEYBOARD OPEN STATE
     ========================= */
  body.kb-open .action-bar,
  body.kb-open #betting-controls {
    transform: translateY(100%);
    pointer-events: none;
  }
}

/* =========================
   TABLET (601px - 900px)
   ========================= */
@media (min-width: 601px) and (max-width: 900px) {
  .card {
    width: 80px;
    height: 120px;
  }

  .action-bar button {
    min-width: 100px;
  }

  .mobile-nav {
    display: none !important;
  }

  .desktop-nav {
    display: flex !important;
  }
}

/* =========================
   DESKTOP (> 900px)
   ========================= */
@media (min-width: 901px) {
  .mobile-nav {
    display: none !important;
  }

  .desktop-nav {
    display: flex !important;
  }

  .bj-chat-toggle {
    display: none;
  }

  #chat-panel {
    position: static;
    height: auto;
    max-height: none;
    border-radius: 12px;
  }
}

/* =========================
   LANDSCAPE MODE (mobile)
   ========================= */
@media (max-width: 900px) and (orientation: landscape) {
  .game-table {
    flex-direction: row;
    gap: 12px;
  }

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

  .players-area {
    flex: 2;
    flex-direction: row;
    flex-wrap: wrap;
    max-height: none;
  }

  .player-spot {
    flex: 1 1 calc(50% - 8px);
    min-width: 180px;
  }
}

/* =========================
   PREVENT TEXT SELECTION
   ========================= */
@media (max-width: 600px) {
  .card,
  .action-bar button,
  .mobile-nav button,
  .betting-controls button {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }
}
