/* Reset dan Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Courier New', monospace;
    background: linear-gradient(135deg, #000000 0%, #1F2937 100%);
    color: #FFFFFF;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

/* Game Container */
.game-container {
    text-align: center;
    max-width: 800px;
    width: 100%;
    display: flex;              /* baru */
    flex-direction: column;     /* baru */
    align-items: center;        /* baru */
}

/* Header */
.game-header {
    margin-bottom: 20px;
    order: 1; /* urutan global */
}

.game-header h1 {
    font-size: 2.5rem;
    color: #DC2626;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
    margin-bottom: 15px;
    letter-spacing: 2px;
}

.score-display {
    display: flex;
    justify-content: space-between;
    max-width: 600px;
    margin: 0 auto;
    font-size: 1.2rem;
    font-weight: bold;
}

.current-score, .high-score {
    background: rgba(220, 38, 38, 0.2);
    padding: 10px 20px;
    border-radius: 8px;
    border: 2px solid #991B1B;
}

/* Game Area */
.game-area {
    position: relative;
    display: inline-block;
    margin-bottom: 20px;
    order: 3; /* urutan global */
}

#gameCanvas {
    background-color: #000000;
    border: 3px solid #DC2626;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(220, 38, 38, 0.3);
}

/* Modal Styles */
.modal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 8px;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: linear-gradient(135deg, #991B1B 0%, #DC2626 100%);
    padding: 30px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    border: 2px solid #FFFFFF;
}

.modal-content h2 {
    font-size: 2rem;
    margin-bottom: 20px;
    color: #FFFFFF;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
}

.modal-content p {
    font-size: 1.2rem;
    margin-bottom: 10px;
    color: #FFFFFF;
}

/* Button Styles */
.btn {
    font-family: 'Courier New', monospace;
    font-size: 1rem;
    font-weight: bold;
    padding: 12px 24px;
    margin: 0 8px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.btn-primary {
    background: linear-gradient(135deg, #DC2626 0%, #991B1B 100%);
    color: #FFFFFF;
    border: 2px solid #991B1B;
}

.btn-primary:hover {
    background: linear-gradient(135deg, #EF4444 0%, #DC2626 100%);
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(220, 38, 38, 0.4);
}

.btn-secondary {
    background: linear-gradient(135deg, #6B7280 0%, #4B5563 100%);
    color: #FFFFFF;
    border: 2px solid #4B5563;
}

.btn-secondary:hover {
    background: linear-gradient(135deg, #9CA3AF 0%, #6B7280 100%);
    transform: translateY(-2px);
}

.btn-danger {
    background: linear-gradient(135deg, #7F1D1D 0%, #991B1B 100%);
    color: #FFFFFF;
    border: 2px solid #7F1D1D;
}

.btn-danger:hover {
    background: linear-gradient(135deg, #991B1B 0%, #7F1D1D 100%);
    transform: translateY(-2px);
}

.btn:active {
    transform: translateY(0);
}

/* Game Controls */
.game-controls {
    margin-bottom: 30px;
    order: 2; /* urutan global */
}

/* Instructions */
.instructions {
    background: rgba(220, 38, 38, 0.1);
    padding: 20px;
    border-radius: 12px;
    border: 2px solid #991B1B;
    max-width: 600px;
    margin: 0 auto;
    order: 5; /* di bawah canvas/dpad */
}

.instructions h3 {
    color: #DC2626;
    margin-bottom: 15px;
    font-size: 1.3rem;
}

.instructions p {
    margin-bottom: 15px;
    font-size: 1rem;
    line-height: 1.5;
}

.key-controls {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
}

.key-group {
    display: flex;
    gap: 8px;
}

/* Mobile Controls */
.mobile-controls {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
}

.mobile-controls.hidden {
    display: none;
}

.dpad {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    background: rgba(0, 0, 0, 0.8);
    padding: 15px;
    border-radius: 15px;
    border: 2px solid #DC2626;
    box-shadow: 0 4px 15px rgba(220, 38, 38, 0.3);
    touch-action: none; /* cegah scroll saat menahan tombol */
}

.dpad-middle {
    display: flex;
    align-items: center;
    gap: 5px;
}

.dpad-btn {
    width: 60px;
    height: 60px;
    border: none;
    border-radius: 12px;
    background: linear-gradient(135deg, #DC2626 0%, #991B1B 100%);
    color: #FFFFFF;
    font-size: 24px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s ease;
    border: 2px solid #991B1B;
    box-shadow: 0 2px 8px rgba(220, 38, 38, 0.4);
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

/* Hapus gaya bawaan tombol pada iOS/Android agar tidak tampil seperti tombol putih default */
.dpad-btn, .btn {
    -webkit-appearance: none;
    appearance: none;
    outline: none;
    -webkit-tap-highlight-color: transparent;
    font: inherit;
}

.dpad-btn:focus, .btn:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.25) inset;
}
/* Pastikan tombol DPAD terpusat dan responsif pada perangkat sentuh */
.dpad-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
}

.dpad-btn:active {
    background: linear-gradient(135deg, #EF4444 0%, #DC2626 100%);
    transform: scale(0.95);
    box-shadow: 0 1px 4px rgba(220, 38, 38, 0.6);
}

.dpad-center {
    width: 60px;
    height: 60px;
    background: rgba(220, 38, 38, 0.2);
    border: 2px solid #991B1B;
    border-radius: 12px;
}

/* Responsive Design */
@media (max-width: 768px) {
    .game-container {
        padding: 10px;
    }
    
    .game-header h1 {
        font-size: 2rem;
    }
    
    .score-display {
        font-size: 1rem;
        flex-direction: row;            /* sebelumnya column */
        justify-content: space-between; /* agar rapi satu baris */
        gap: 12px;
    }
    
    #gameCanvas {
        max-width: 100%;
        height: auto;
    }
    
    .instructions {
        font-size: 0.9rem;
    }
    
    .key-controls {
        display: none;
    }
    
    .mobile-controls {
        display: block;
        padding-bottom: calc(20px + env(safe-area-inset-bottom)); /* aman untuk notch */
    }
    
    .mobile-controls.hidden {
        display: none;
    }
}

.key {
    display: inline-block;
    background: #DC2626;
    color: #FFFFFF;
    padding: 8px 12px;
    border-radius: 6px;
    font-weight: bold;
    font-size: 0.9rem;
    min-width: 35px;
    text-align: center;
    border: 2px solid #991B1B;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.or {
    color: #9CA3AF;
    font-style: italic;
    font-size: 0.9rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    /* Urutan: 1) Judul, 2) Skor satu baris, 3) Tombol, 4) Snake Area, 5) D-pad (mobile) / Cara Bermain (desktop) */
    /* ===== Final Cascade Overrides: Urutan Layout Global ===== */
    /* 1. Judul (dan skor di dalam header)
       2. —
       3. Tombol Mulai/Pause/Reset
       4. Snake Area (Canvas)
       5. D-pad untuk Mobile, "Cara Bermain" untuk Desktop */
    .game-header { order: 1; }
    .game-controls { order: 2; margin: 16px 0; }
    .game-area { order: 3; }
    .instructions { order: 5; }
    .mobile-controls { order: 5; }
    
    /* Skor & Tertinggi selalu satu baris */
    .score-display {
      display: flex !important;
      flex-direction: row !important;
      justify-content: space-between !important;
      gap: 12px;
      width: 100%;
      max-width: 600px;
    }
    
    /* Desktop: tampilkan Cara Bermain, sembunyikan D-pad */
    @media (min-width: 769px) {
      .instructions { display: block !important; }
      .mobile-controls { display: none !important; }
    }
    
    /* Mobile: tampilkan D-pad tepat di bawah canvas (bukan fixed), sembunyikan Cara Bermain */
    @media (max-width: 768px) {
      .instructions { display: none !important; }
      .mobile-controls {
        display: block !important;
        position: static !important;  /* override dari posisi fixed */
        left: auto !important;
        bottom: auto !important;
        transform: none !important;
        margin-top: 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
      }
    }
    /* ===== End Final Cascade Overrides ===== */
}

.key {
    display: inline-block;
    background: #DC2626;
    color: #FFFFFF;
    padding: 8px 12px;
    border-radius: 6px;
    font-weight: bold;
    font-size: 0.9rem;
    min-width: 35px;
    text-align: center;
    border: 2px solid #991B1B;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.or {
    color: #9CA3AF;
    font-style: italic;
    font-size: 0.9rem;
}

/* Responsive Design */
@media (max-width: 480px) {
    .game-header h1 {
        font-size: 1.5rem;
    }
    
    .modal-content {
        padding: 20px;
        margin: 20px;
    }
    
    .instructions {
        padding: 15px;
    }

    /* Penyesuaian ukuran DPAD untuk layar kecil */
    .dpad {
        gap: 8px;
        padding: 12px;
    }
    .dpad-middle {
        gap: 8px;
    }
    .dpad-btn {
        width: 56px;
        height: 56px;
        font-size: 20px;
        border-radius: 14px;
    }
    .dpad-center {
        width: 56px;
        height: 56px;
        border-radius: 14px;
    }
}

/* Animation untuk makanan (akan digunakan di JavaScript) */
@keyframes pulse {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
    100% {
        transform: scale(1);
    }
}

.pulse {
    animation: pulse 1s infinite;
}
/* ===== Alignment & Width Sync with Canvas ===== */
/* Gunakan variabel agar semua baris (skor, tombol, instruksi/D-pad) memiliki lebar yang sama dengan canvas */
.game-container { --game-width: 600px; }
@media (max-width: 768px) {
  .game-container { --game-width: min(100%, 400px); }
}

/* Samakan lebar dan posisi dengan canvas */
.game-area { width: var(--game-width) !important; margin-left: auto; margin-right: auto; }
#gameCanvas { width: 100% !important; height: auto !important; }
.score-display, .game-controls, .instructions, .mobile-controls {
  width: var(--game-width) !important;
  margin-left: auto; margin-right: auto;
}

/* Score row: selalu satu baris, beri jarak yang pas */
.score-display {
  display: flex !important;
  flex-wrap: nowrap !important;
  justify-content: space-between !important;
  column-gap: 16px !important;
}

/* Controls row: selalu satu baris sejajar dengan canvas */
.game-controls {
  display: flex !important;
  flex-wrap: nowrap !important;
  align-items: center;
  justify-content: center;
  column-gap: 16px;
}
.btn { white-space: nowrap; }
@media (max-width: 768px) {
  .btn { font-size: 0.9rem; padding: 10px 16px; }
}
/* ===== End Alignment & Width Sync ===== */