/* ============================================
   TACTICAL WATERS — Battleship Reimagined
   ============================================ */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
    --bg: #0a1428;
    --bg-deep: #050a18;
    --panel: #122039;
    --panel-light: #1a2c4d;
    --grid-bg: #0d1a2d;
    --grid-line: #1e3358;
    --water: #133252;
    --water-hover: #1c4a78;
    --ship: #6b7d8c;
    --ship-hit: #d83333;
    --miss: #5a7794;
    --burning: #ff5733;
    --warm: #ff9933;
    --cold: #66ccff;
    --freezing: #aaccff;
    --text: #e6efff;
    --text-dim: #8aa3c7;
    --accent: #00d4ff;
    --accent-dark: #008cb0;
    --gold: #ffcc44;
    --danger: #ff4466;
    --bombed: #4a1a1a;
}

body {
    background: linear-gradient(180deg, var(--bg-deep) 0%, var(--bg) 100%);
    color: var(--text);
    font-family: 'Segoe UI', Tahoma, sans-serif;
    min-height: 100vh;
    overflow-x: hidden;
}

/* ============================================ SCREENS ============================================ */
.screen {
    display: none;
    min-height: 100vh;
    padding: 20px;
}
.screen.active { display: flex; align-items: center; justify-content: center; flex-direction: column; }

/* ============================================ LOBBY ============================================ */
.lobby-card {
    background: var(--panel);
    padding: 40px;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.5);
    max-width: 480px;
    width: 100%;
    text-align: center;
}
.title {
    font-size: 2.5em;
    font-weight: 800;
    letter-spacing: 4px;
    background: linear-gradient(135deg, var(--accent), var(--gold));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 8px;
}
.subtitle { color: var(--text-dim); margin-bottom: 32px; }

.input-label {
    display: block;
    text-align: left;
    color: var(--text-dim);
    font-size: 0.85em;
    margin-bottom: 6px;
}
input[type="text"] {
    width: 100%;
    padding: 12px 14px;
    background: var(--bg);
    border: 1px solid var(--grid-line);
    border-radius: 6px;
    color: var(--text);
    font-size: 1em;
    margin-bottom: 16px;
}
input[type="text"]:focus {
    outline: none;
    border-color: var(--accent);
}

.lobby-actions { display: flex; flex-direction: column; gap: 12px; margin-top: 20px; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; margin-bottom: 0; text-transform: uppercase; letter-spacing: 4px; text-align: center; font-weight: bold; }

.btn {
    padding: 12px 20px;
    background: var(--panel-light);
    border: 1px solid var(--grid-line);
    border-radius: 6px;
    color: var(--text);
    font-size: 1em;
    cursor: pointer;
    transition: all 0.15s;
    font-weight: 600;
}
.btn:hover:not(:disabled) { background: var(--water); border-color: var(--accent); transform: translateY(-1px); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.primary { background: linear-gradient(135deg, var(--accent-dark), var(--accent)); border-color: var(--accent); }
.btn.primary:hover:not(:disabled) { box-shadow: 0 0 16px var(--accent); }
.btn.ghost { background: transparent; }

.error-msg {
    color: var(--danger);
    margin-top: 12px;
    padding: 8px;
    background: rgba(255,68,102,0.1);
    border-radius: 4px;
    font-size: 0.9em;
}
.hidden { display: none !important; }

.how-to-play {
    margin-top: 30px;
    text-align: left;
    color: var(--text-dim);
    font-size: 0.85em;
}
.how-to-play summary {
    cursor: pointer;
    color: var(--accent);
    margin-bottom: 10px;
}
.how-to-play ul { margin-left: 16px; }
.how-to-play li { margin: 6px 0; }

/* ============================================ ROOM CODE ============================================ */
.room-code-display {
    font-size: 3em;
    font-weight: 800;
    letter-spacing: 12px;
    color: var(--gold);
    margin: 20px 0;
    text-shadow: 0 0 20px rgba(255,204,68,0.3);
}

/* ============================================ PLACEMENT ============================================ */
.placement-layout {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 24px;
    max-width: 1100px;
    width: 100%;
}
.panel {
    background: var(--panel);
    padding: 20px;
    border-radius: 8px;
}
.panel h2 { margin-bottom: 8px; }
.hint { color: var(--text-dim); font-size: 0.85em; margin-bottom: 16px; }

.fleet-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 20px; }
.ship-card {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    background: var(--bg);
    border: 2px solid var(--grid-line);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s;
}
.ship-card:hover { border-color: var(--accent); }
.ship-card.selected { border-color: var(--gold); background: rgba(255,204,68,0.1); }
.ship-card.placed { opacity: 0.5; }
.ship-card.placed.selected { opacity: 1; }
.ship-preview {
    display: flex;
    gap: 2px;
}
.ship-preview .ship-cell {
    width: 16px; height: 16px;
    background: var(--ship);
    border-radius: 2px;
}
.ship-info { flex: 1; }
.ship-info b { display: block; }
.ship-info small { color: var(--text-dim); }

.placement-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.placement-buttons .btn { flex: 1; padding: 10px; min-height: 44px; }
.rotate-btn { background: var(--panel-light); border-color: var(--accent); color: var(--accent); font-weight: 700; }
.rotate-btn:hover:not(:disabled) { background: var(--water); box-shadow: 0 0 12px rgba(0,212,255,0.25); }

.status-line {
    margin-top: 16px;
    padding: 10px;
    background: var(--bg);
    border-radius: 4px;
    font-size: 0.9em;
    text-align: center;
    color: var(--text-dim);
}
.status-line.ready { color: var(--gold); }

/* ============================================ GRID ============================================ */
.grid-wrap {
    display: flex;
    justify-content: center;
    padding: 16px;
    background: var(--bg);
    border-radius: 8px;
}
.grid {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    grid-template-rows: repeat(12, 1fr);
    gap: 1px;
    background: var(--grid-line);
    border: 2px solid var(--grid-line);
    border-radius: 4px;
    aspect-ratio: 1;
    max-width: 480px;
    width: 100%;
    user-select: none;
    position: relative; /* anchor for .ship-overlay */
    container-type: inline-size; /* lets cell pseudo-elements use cqi sized to the grid */
}

/* ============================================ SHIP SPRITE OVERLAY ============================================ */
.ship-overlay {
    /* Fills the grid's padding-box (which is already inside the 2px border with box-sizing:border-box).
       Cells share this exact coordinate space — DO NOT inset further. */
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 5;
}
.ship-img {
    position: absolute;
    pointer-events: none;
    user-select: none;
    -webkit-user-drag: none;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.45));
    will-change: transform;
}
.ship-img.sunk {
    filter: grayscale(1) brightness(0.42) drop-shadow(0 2px 4px rgba(0,0,0,0.4));
    opacity: 0.92;
}

/* Cells where a sprite paints the ship: drop the gray placeholder so the sprite is visible. */
.placement-grid .cell.ship,
.my-grid .cell.ship { background: var(--water); }

/* Damage / reveal markers must paint ABOVE the sprite layer (z-index 5). */
.my-grid .cell.ship.hit-part { background: rgba(216, 51, 51, 0.62); z-index: 10; }
.my-grid .cell.ship.sunk     { background: rgba(0, 0, 0, 0.30); z-index: 10; }
.my-grid .cell.opp-ping      { z-index: 10; }
.enemy-grid .cell.hit,
.enemy-grid .cell.miss,
.enemy-grid .cell.recon-empty,
.enemy-grid .cell.recon-occ,
.enemy-grid .cell.sonar      { z-index: 10; }
/* Sunk enemy ship cells: drop the gray fill so the sunk-sprite shows through; X mark still renders on top. */
.enemy-grid .cell.hit.ship.sunk { background: transparent; }
.cell {
    background: var(--water);
    cursor: pointer;
    transition: background 0.1s;
    position: relative;
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7em;
    font-weight: bold;
}
.cell:hover { background: var(--water-hover); }
.cell.ship { background: var(--ship); }
.cell.ship.sunk { background: #444; }
.cell.ship.hit-part { background: var(--ship-hit); }

.cell.preview-ok { background: rgba(0, 212, 255, 0.4); }
.cell.preview-bad { background: rgba(255, 68, 102, 0.4); }

.cell.bombed-zone {
    background: var(--bombed);
    background-image: repeating-linear-gradient(
        45deg,
        var(--bombed),
        var(--bombed) 4px,
        #2a0a0a 4px,
        #2a0a0a 8px
    );
}
.cell.bombed-zone .bomb-counter {
    position: absolute;
    top: 1px; right: 2px;
    font-size: 0.6em;
    color: #ff8866;
}

.cell.hit {
    background: var(--ship-hit);
}
/* Flickering 🔥 sits on every hit cell (enemy.hit and my-fleet.hit-part both).
   font-size in cqi scales the emoji to the actual grid width, so it stays
   readable on phones where cells are ~28px and unreadable in em units. */
.cell.hit::after,
.cell.hit-part::after {
    content: '🔥';
    font-size: 6.2cqi;
    line-height: 1;
    animation: bs-fire-flicker 0.45s ease-in-out infinite;
    filter: drop-shadow(0 0 5px rgba(255,120,0,0.75));
    z-index: 12;
    position: relative;
    will-change: transform;
}

/* One-shot expanding fireball when a cell is freshly hit. Added via JS as `.hit-burst`,
   removed after the animation completes so it doesn't replay on later renders. */
.cell.hit-burst::before {
    content: '';
    position: absolute;
    inset: -50%;
    background: radial-gradient(circle,
        rgba(255,245,180,1)  0%,
        rgba(255,190,60,0.95) 18%,
        rgba(255,100,20,0.85) 42%,
        rgba(180,30,0,0.5)    68%,
        transparent           88%);
    pointer-events: none;
    z-index: 15;
    animation: bs-fire-burst 0.7s ease-out forwards;
    mix-blend-mode: screen;
    border-radius: 50%;
}

@keyframes bs-fire-burst {
    0%   { transform: scale(0.3); opacity: 1; }
    35%  { transform: scale(1.4); opacity: 1; }
    100% { transform: scale(2.4); opacity: 0; }
}
@keyframes bs-fire-flicker {
    0%, 100% { transform: scale(1)    translateY(0);     filter: brightness(1.00) drop-shadow(0 0 4px rgba(255,100,0,0.7)); }
    25%      { transform: scale(1.10) translateY(-0.5px);filter: brightness(1.15) drop-shadow(0 0 7px rgba(255,160,0,0.95)); }
    50%      { transform: scale(0.94) translateY(0.5px); filter: brightness(0.90) drop-shadow(0 0 3px rgba(255,80,0,0.6)); }
    75%      { transform: scale(1.06) translateY(-0.5px);filter: brightness(1.08) drop-shadow(0 0 5px rgba(255,130,0,0.85)); }
}
.cell.miss { background: var(--miss); }
.cell.miss::after {
    content: '•';
    color: #aaa;
    font-size: 2em;
    line-height: 0;
}

.cell.miss.burning { background: var(--burning); }
.cell.miss.warm { background: var(--warm); }
.cell.miss.cold { background: var(--cold); }
.cell.miss.freezing { background: var(--freezing); color: #134; }

.cell.recon-empty { background: rgba(150,150,150,0.4); }
.cell.recon-empty::after { content: '○'; color: #aaa; font-size: 1em; line-height: 0.6; }
.cell.recon-occ { background: rgba(255,204,68,0.3); }
.cell.recon-occ::after { content: '?'; color: var(--gold); font-size: 1.2em; }

.cell.sonar { background: rgba(102,204,255,0.3); border: 1px dashed var(--cold); }
.cell.sonar .sonar-arrow { color: var(--cold); font-size: 1.2em; font-weight: bold; }

.cell.opp-ping {
    background: rgba(255,140,0,0.2);
    border: 1px dashed var(--warm);
}
.cell.opp-ping::before {
    content: '👁';
    position: absolute;
    font-size: 0.7em;
    top: 1px; left: 1px;
}

.cell.attack-target { outline: 2px solid var(--accent); outline-offset: -2px; }
.cell.move-from { outline: 2px solid var(--gold); outline-offset: -2px; }
.cell.move-target { outline: 2px solid var(--gold); outline-offset: -2px; background: rgba(255,204,68,0.2); }

/* ============================================ BATTLE ============================================ */
.battle-layout {
    width: 100%;
    max-width: 1200px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}
.battle-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    background: var(--panel);
    border-radius: 8px;
}
.turn-indicator {
    font-size: 1.2em;
    font-weight: bold;
    padding: 6px 14px;
    border-radius: 6px;
    background: var(--bg);
}
.turn-indicator.my-turn { background: var(--accent-dark); color: white; box-shadow: 0 0 20px rgba(0,212,255,0.3); }
.turn-indicator.their-turn { background: var(--bg); color: var(--text-dim); }
.player-tags { display: flex; align-items: center; gap: 12px; }
.tag {
    padding: 4px 10px;
    background: var(--bg);
    border-radius: 4px;
    font-weight: 600;
}
.tag.active { background: var(--accent-dark); color: white; }
.vs { color: var(--text-dim); }

.boards {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}
.board-section {
    background: var(--panel);
    padding: 16px;
    border-radius: 8px;
}
.board-section h3 { margin-bottom: 12px; text-align: center; }
.ship-counter {
    margin-top: 10px;
    text-align: center;
    color: var(--text-dim);
    font-size: 0.9em;
}

.attack-bar {
    display: grid;
    grid-template-columns: repeat(5, 1fr) auto auto;
    gap: 8px;
    background: var(--panel);
    padding: 10px;
    border-radius: 8px;
}
.attack-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px 6px;
    background: var(--bg);
    border: 2px solid var(--grid-line);
    border-radius: 6px;
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s;
    position: relative;
}
.attack-btn:hover:not(:disabled) { border-color: var(--accent); }
.attack-btn.selected { border-color: var(--gold); background: rgba(255,204,68,0.1); box-shadow: 0 0 12px rgba(255,204,68,0.3); }
.attack-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.atk-name { font-weight: bold; font-size: 0.9em; }
.atk-desc { font-size: 0.7em; color: var(--text-dim); margin-top: 2px; text-align: center; }
.atk-cd {
    position: absolute;
    top: 4px; right: 6px;
    color: var(--gold);
    font-size: 0.7em;
    font-weight: bold;
}

.action-btn {
    padding: 8px 18px;
    background: var(--bg);
    border: 2px solid var(--grid-line);
    border-radius: 6px;
    color: var(--text);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
}
.action-btn:hover:not(:disabled) { border-color: var(--accent); }
.action-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.action-btn.primary { background: linear-gradient(135deg, var(--accent-dark), var(--accent)); border-color: var(--accent); }
.action-btn.primary:hover:not(:disabled) { box-shadow: 0 0 16px var(--accent); }
.action-btn.active { border-color: var(--gold); background: rgba(255,204,68,0.15); }

.info-row {
    display: grid;
    grid-template-columns: 1fr 2fr;
    gap: 16px;
}
.hint-box {
    background: var(--panel);
    padding: 14px 16px;
    border-radius: 8px;
    font-size: 0.95em;
    border-left: 3px solid var(--accent);
}
.log-box {
    background: var(--panel);
    padding: 12px 16px;
    border-radius: 8px;
    max-height: 140px;
    overflow-y: auto;
    font-size: 0.85em;
}
.log-line { margin: 3px 0; }
.log-line.system { color: var(--gold); font-style: italic; }
.log-line.attack { color: var(--accent); }
.log-line.move { color: var(--text-dim); }
.log-line.chat { color: var(--text); }

/* ============================================ END SCREEN ============================================ */
#end-title {
    font-size: 2em;
    margin-bottom: 16px;
}
#end-title.victory { color: var(--gold); }
#end-title.defeat { color: var(--danger); }

/* ============================================ PICKER ============================================ */
.picker-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
.picker-overlay.hidden { display: none; }
.picker-card {
    background: var(--panel);
    padding: 24px;
    border-radius: 8px;
    min-width: 280px;
}
.picker-card h3 { margin-bottom: 16px; text-align: center; }
#picker-options { display: grid; gap: 8px; margin-bottom: 16px; }
.picker-opt {
    padding: 10px 14px;
    background: var(--bg);
    border: 1px solid var(--grid-line);
    border-radius: 6px;
    color: var(--text);
    cursor: pointer;
    text-align: center;
}
.picker-opt:hover { border-color: var(--accent); background: var(--water); }

/* ============================================ MOBILE ============================================ */
@media (max-width: 900px) {
    .placement-layout { grid-template-columns: 1fr; gap: 12px; }
    .boards { grid-template-columns: 1fr; }
    .info-row { grid-template-columns: 1fr; }
    .attack-bar { grid-template-columns: repeat(3, 1fr); }
    .attack-bar .action-btn { grid-column: span 1; }
    .title { font-size: 1.8em; }
    .rotate-btn { flex-basis: 100%; order: -1; min-height: 52px; font-size: 1.05em; }
}
@media (max-width: 600px) {
    .screen { padding: 10px; }
    .panel { padding: 12px; }
    .grid-wrap { padding: 6px; }
    .grid { max-width: 100%; }
    .cell { font-size: 0.6em; }
    .battle-header { flex-direction: column; gap: 8px; padding: 10px; }
    .turn-indicator { font-size: 1em; }
    .ship-card { padding: 8px 10px; }
    .ship-preview .ship-cell { width: 12px; height: 12px; }
    .attack-bar { grid-template-columns: repeat(2, 1fr); gap: 6px; padding: 8px; }
    .attack-btn { padding: 8px 4px; min-height: 56px; }
    .atk-name { font-size: 0.85em; }
    .atk-desc { font-size: 0.65em; }
    .action-btn { min-height: 48px; padding: 10px 14px; }
    #btn-end-turn, #btn-move { grid-column: span 2; }
    .picker-card { min-width: 0; width: calc(100% - 32px); max-width: 360px; }
    .picker-opt { min-height: 48px; }

    /* Mobile fire tuning:
       - Slightly bigger emoji so it's still visible at ~28px cells.
       - Skip the GPU-heavy drop-shadow filter (per-cell composite layer).
       - Cheaper flicker with no filter transitions. */
    .cell.hit::after,
    .cell.hit-part::after {
        font-size: 7.2cqi;
        filter: none;
        animation: bs-fire-flicker-light 0.5s ease-in-out infinite;
    }
    .cell.hit-burst::before {
        inset: -30%;
        animation-duration: 0.55s;
    }
}

/* Cheap flicker for mobile: transform only, no filter recalc. */
@keyframes bs-fire-flicker-light {
    0%, 100% { transform: scale(1); }
    50%      { transform: scale(1.10); }
}

/* Accessibility: users who prefer reduced motion get a static, glowing 🔥 with
   no burst and no flicker. The cell still reads as "on fire" but stops moving. */
@media (prefers-reduced-motion: reduce) {
    .cell.hit::after,
    .cell.hit-part::after {
        animation: none;
        filter: drop-shadow(0 0 3px rgba(255,120,0,0.55));
    }
    .cell.hit-burst::before { animation: none; opacity: 0; }
}
