* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  background: linear-gradient(180deg, #06304d 0%, #0a4a72 40%, #0d5f8f 100%);
  color: #eaf4fb;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
}

#app {
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.app-header {
  padding: 14px 0 6px;
  width: 100%;
  max-width: 480px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.app-header h1 {
  font-size: 1.3rem;
  margin: 0;
  letter-spacing: 0.5px;
  text-align: center;
}
.mute-btn {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 50%;
  width: 34px;
  height: 34px;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  color: inherit;
}
.mute-btn:active { transform: translateY(-50%) scale(0.92); }

.screen {
  display: none;
  width: 100%;
  max-width: 480px;
  padding: 8px 14px 24px;
  flex: 1;
}
.screen.active { display: block; }

.card {
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 16px;
  padding: 20px;
  backdrop-filter: blur(4px);
}
.card.center { text-align: center; }

label {
  display: block;
  font-size: 0.85rem;
  opacity: 0.85;
  margin: 12px 0 6px;
}
label:first-child { margin-top: 0; }

input[type="text"] {
  width: 100%;
  font-size: 16px;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.25);
  background: rgba(255,255,255,0.9);
  color: #0a2a43;
  outline: none;
}
input[type="text"]:focus { border-color: #4fc3f7; }

.btn-row {
  display: flex;
  gap: 10px;
  margin-top: 16px;
  flex-wrap: wrap;
}
.btn-row.small { margin-top: 12px; }
.btn-row button { flex: 1; min-width: 120px; }

.btn {
  appearance: none;
  border: none;
  border-radius: 10px;
  padding: 13px 16px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.08s ease, opacity 0.15s;
}
.btn:active { transform: scale(0.96); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.orientation-indicator {
  display: block;
  font-size: 0.7rem;
  font-weight: 500;
  opacity: 0.75;
  margin-top: 2px;
}

.btn-primary { background: #ffb84d; color: #3a2400; }
.btn-secondary { background: rgba(255,255,255,0.14); color: #eaf4fb; border: 1px solid rgba(255,255,255,0.25); }
.btn-danger-outline { background: transparent; color: #ff8a80; border: 1px solid #ff8a80; margin-top: 18px; width: 100%; }
.btn-wide { width: 100%; margin-top: 18px; }

.hidden { display: none !important; }

.error {
  color: #ff8a80;
  font-size: 0.85rem;
  margin-top: 10px;
}

.code-display {
  font-size: 2.4rem;
  font-weight: 700;
  letter-spacing: 6px;
  background: rgba(255,255,255,0.1);
  border-radius: 12px;
  padding: 16px;
  margin: 14px 0;
}

.hint { opacity: 0.8; font-size: 0.9rem; margin-top: 14px; }
.dots span { animation: blink 1.4s infinite; opacity: 0; }
.dots span:nth-child(2) { animation-delay: 0.2s; }
.dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%,80%,100% { opacity:0; } 40% { opacity:1; } }

.subtitle { text-align: center; opacity: 0.85; margin: 0 0 12px; }

.ship-tray {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 6px;
  margin-bottom: 4px;
}
.ship-chip {
  flex: 0 0 auto;
  background: rgba(255,255,255,0.1);
  border: 2px solid rgba(255,255,255,0.25);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.8rem;
  text-align: center;
  cursor: pointer;
  min-width: 70px;
}
.ship-chip.selected { border-color: #ffb84d; background: rgba(255,184,77,0.2); }
.ship-chip.placed { opacity: 0.4; }
.ship-chip .blocks {
  display: flex;
  gap: 2px;
  justify-content: center;
  margin-top: 4px;
}
.ship-chip .blocks i {
  width: 8px; height: 8px;
  background: #bcd7e8;
  display: inline-block;
  border-radius: 2px;
}

.board-wrap {
  display: grid;
  grid-template-columns: 18px 1fr;
  grid-template-rows: 18px 1fr;
  grid-template-areas: "corner col" "row board";
  gap: 3px;
  width: 100%;
  max-width: 380px;
  margin: 10px auto;
}
.board-wrap.small {
  max-width: 220px;
  grid-template-columns: 14px 1fr;
  grid-template-rows: 14px 1fr;
}
.board-corner { grid-area: corner; position: relative; }
.board-corner::after {
  content: "⚓";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.6rem;
  opacity: 0.4;
}
.board-colheaders, .board-rowheaders {
  opacity: 0.65;
  font-size: 0.65rem;
  font-weight: 600;
}
.board-colheaders { grid-area: col; display: grid; grid-template-columns: repeat(10, 1fr); }
.board-rowheaders { grid-area: row; display: grid; grid-template-rows: repeat(10, 1fr); }
.board-colheaders span, .board-rowheaders span { display: flex; align-items: center; justify-content: center; }
.board-wrap.small .board-colheaders, .board-wrap.small .board-rowheaders { font-size: 0.55rem; }

.board-wrap .board { grid-area: board; margin: 0; max-width: none; width: 100%; }

.board {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(10, 1fr);
  gap: 3px;
  width: 100%;
  max-width: 380px;
  aspect-ratio: 1 / 1;
  margin: 10px auto;
  user-select: none;
  position: relative;
  padding: 3px;
  border-radius: 8px;
  overflow: hidden;
  background: linear-gradient(135deg, #0a4a72, #0d5f8f, #0a4a72);
  background-size: 300% 300%;
  animation: waterFlow 10s ease-in-out infinite;
}
@keyframes waterFlow {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

.ship-layer { position: absolute; inset: 0; pointer-events: none; z-index: 1; }
.ship-overlay {
  position: absolute;
  transform: rotate(var(--base-rot, 0deg));
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.4));
}
.ship-overlay svg { display: block; width: 100%; height: 100%; }
.ship-overlay.wreck {
  animation: sinkShake 0.5s ease-in-out 1;
  filter: grayscale(0.5) brightness(0.55) sepia(1) hue-rotate(-50deg) saturate(4) drop-shadow(0 1px 1px rgba(0,0,0,0.4));
}
@keyframes sinkShake {
  0% { transform: rotate(var(--base-rot, 0deg)); }
  30% { transform: rotate(calc(var(--base-rot, 0deg) + 6deg)); }
  60% { transform: rotate(calc(var(--base-rot, 0deg) - 4deg)); }
  100% { transform: rotate(var(--base-rot, 0deg)); }
}

.cell {
  background: radial-gradient(circle at 30% 25%, rgba(255,255,255,0.18), rgba(255,255,255,0.04) 65%), rgba(15,80,125,0.35);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
  z-index: 2;
}
.cell.ship { background: rgba(138,169,189,0.25); box-shadow: inset 0 0 0 1px rgba(255,255,255,0.15); }
.cell.hit { background: rgba(214,69,69,0.4); }
.cell.hit::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,140,80,0.9), rgba(255,80,40,0.4) 60%, transparent 70%);
  animation: hitBurst 0.5s ease-out forwards;
}
.cell.hit::after { content: "✕"; color: #fff; font-size: 0.7rem; font-weight: 700; position: relative; z-index: 1; }
@keyframes hitBurst {
  0% { transform: scale(0.2); opacity: 1; }
  100% { transform: scale(1.5); opacity: 0; }
}
.cell.miss { background: rgba(255,255,255,0.06); }
.cell.miss::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid rgba(188,215,232,0.8);
  animation: missRipple 0.5s ease-out forwards;
}
.cell.miss::after { content: "•"; color: #bcd7e8; font-size: 1rem; position: relative; z-index: 1; }
@keyframes missRipple {
  0% { transform: scale(0.2); opacity: 1; }
  100% { transform: scale(1.3); opacity: 0; }
}
.cell.sunk { background: rgba(110,31,31,0.55); }

.board-enemy .cell { cursor: pointer; }
.board-enemy .cell:active { background: rgba(255,255,255,0.25); }
.board-enemy.disabled .cell { pointer-events: none; opacity: 0.7; }

.board-label {
  text-align: center;
  font-size: 0.85rem;
  opacity: 0.85;
  margin: 4px 0 0;
}

.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 4px 4px;
  font-size: 0.9rem;
}
.turn-indicator {
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 20px;
  background: rgba(255,255,255,0.12);
}
.turn-indicator.my-turn { background: #ffb84d; color: #3a2400; }
.turn-indicator:not(.my-turn) { animation: radarPulse 2s ease-in-out infinite; }
@keyframes radarPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(79,195,247,0.5); }
  50% { box-shadow: 0 0 0 8px rgba(79,195,247,0); }
}

.toast {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(10,42,67,0.95);
  border: 1px solid rgba(255,255,255,0.2);
  padding: 10px 18px;
  border-radius: 30px;
  font-size: 0.9rem;
  z-index: 50;
  transition: opacity 0.25s;
}
.toast.hidden { opacity: 0; pointer-events: none; }

h2 { margin-top: 0; }

/* ---------- Screen shake / hit flash ---------- */

#flash-overlay {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  z-index: 60;
}
#flash-overlay.flashing { animation: flashPulse 0.4s ease-out; }
@keyframes flashPulse {
  0% { opacity: 0.6; }
  100% { opacity: 0; }
}

.board.shake { animation: boardShake 0.35s ease-in-out; }
.board.shake-big { animation: boardShakeBig 0.55s ease-in-out; }
@keyframes boardShake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}
@keyframes boardShakeBig {
  10%, 90% { transform: translate(-2px, 1px); }
  20%, 80% { transform: translate(4px, -2px); }
  30%, 50%, 70% { transform: translate(-8px, 2px); }
  40%, 60% { transform: translate(8px, -3px); }
}

/* ---------- Reconnect banner ---------- */

.reconnect-banner {
  position: fixed;
  top: env(safe-area-inset-top);
  left: 0;
  right: 0;
  z-index: 70;
  background: #ffb84d;
  color: #3a2400;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 8px 12px;
  transition: transform 0.25s ease;
}
.reconnect-banner.hidden { transform: translateY(-120%); }

/* ---------- Emoji reaction bar ---------- */

.emoji-bar {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  justify-content: flex-start;
  padding: 6px 2px 2px;
  margin-top: 6px;
}
.emoji-btn {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.2);
  background: rgba(255,255,255,0.08);
  font-size: 1.15rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}
.emoji-btn:active { transform: scale(0.88); background: rgba(255,255,255,0.2); }

#emoji-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 65;
}
.emoji-bubble {
  position: absolute;
  bottom: 60px;
  font-size: 1.8rem;
  animation: emojiFloat 2.2s ease-out forwards;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,0.35));
}
@keyframes emojiFloat {
  0% { transform: translateY(0) scale(0.6) rotate(0deg); opacity: 0; }
  15% { opacity: 1; transform: translateY(-20px) scale(1.1) rotate(-4deg); }
  100% { transform: translateY(-320px) scale(1) rotate(6deg); opacity: 0; }
}

/* ---------- Game-over sheet (overlays battle screen, boards stay visible) ---------- */

.gameover-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 55;
  max-width: 480px;
  margin: 0 auto;
  background: rgba(8,36,58,0.97);
  border: 1px solid rgba(255,255,255,0.15);
  border-bottom: none;
  border-radius: 20px 20px 0 0;
  padding: 18px 20px calc(18px + env(safe-area-inset-bottom));
  text-align: center;
  box-shadow: 0 -8px 30px rgba(0,0,0,0.4);
  transition: transform 0.3s ease;
  max-height: 60vh;
  overflow-y: auto;
}
.gameover-sheet.hidden { transform: translateY(105%); }
.sheet-toggle {
  position: absolute;
  top: 8px;
  right: 12px;
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 50%;
  width: 30px;
  height: 30px;
  color: inherit;
  cursor: pointer;
}
.sheet-reopen {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 55;
  background: #ffb84d;
  color: #3a2400;
  border: none;
  border-radius: 30px;
  padding: 10px 18px;
  font-weight: 700;
  font-size: 0.9rem;
  box-shadow: 0 4px 14px rgba(0,0,0,0.35);
  cursor: pointer;
}
.sheet-reopen.hidden { display: none; }
