:root {
  /* 제주 바다 & 현무암 & 테왁 테마 */
  --bg-outer: #03111c;
  --bg-deep: #072235;
  --sea-surface: #1f8ea8;
  --sea-mid: #0c4d6e;
  --sea-abyss: #041524;

  --panel-bg: rgba(7, 27, 42, 0.88);
  --panel-solid: #0a2438;
  --panel-border: rgba(132, 224, 240, 0.22);
  --card-bg: linear-gradient(180deg, rgba(12, 41, 61, 0.96) 0%, rgba(6, 22, 36, 0.98) 100%);

  --text-main: #eef9fc;
  --text-muted: #92bccb;
  --text-dim: #6894a6;

  --accent-tewak: #ff6b35;
  --accent-tewak-hi: #ff8659;
  --accent-tewak-lo: #d94e1f;
  --accent-pearl: #7ef2ff;
  --accent-gold: #ffd166;

  --o2-safe: #38e4c1;
  --o2-warn: #ffbe3b;
  --o2-danger: #ff4d5a;

  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 20px;

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);

  --col-max: 480px;
  --shadow-card: 0 20px 48px rgba(0, 0, 0, 0.55);
}

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: radial-gradient(circle at 50% 0%, #0d3a54 0%, var(--bg-outer) 72%);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif;
  line-height: 1.4;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* 세로 우선 320px~데스크톱 중앙 480px 컬럼 */
.app {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: var(--col-max);
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  background: linear-gradient(180deg, var(--sea-surface) 0%, var(--sea-mid) 36%, var(--sea-abyss) 100%);
  box-shadow: 0 0 0 1px rgba(132, 224, 240, 0.14), 0 24px 64px rgba(0, 0, 0, 0.6);
  overflow: hidden;
}

/* 상단 HUD */
.hud {
  flex: 0 0 auto;
  z-index: 5;
  padding: calc(8px + var(--safe-top)) calc(10px + var(--safe-right)) 8px calc(10px + var(--safe-left));
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-border);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.hud-top {
  display: flex;
  align-items: center;
  gap: 6px;
}

.hud-stat {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.hud-label {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.hud-value {
  font-size: 15px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-value.accent {
  color: var(--accent-gold);
}

.icon-btn {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-main);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.15s ease, transform 0.1s ease;
}

.icon-btn:active {
  transform: scale(0.94);
  background: rgba(255, 255, 255, 0.14);
}

.icon-svg {
  width: 22px;
  height: 22px;
}

.icon-btn .sound-mute {
  display: none;
}

.icon-btn.is-muted .sound-wave {
  display: none;
}

.icon-btn.is-muted .sound-mute {
  display: block;
}

.icon-btn.is-muted {
  color: var(--text-dim);
}

/* 산소 게이지 */
.o2-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 7px;
}

.o2-tag {
  font-size: 11px;
  font-weight: 800;
  color: var(--accent-pearl);
  letter-spacing: 0.04em;
  min-width: 16px;
}

.o2-bar {
  position: relative;
  flex: 1 1 auto;
  height: 12px;
  border-radius: 999px;
  background: rgba(2, 14, 24, 0.8);
  border: 1px solid rgba(132, 224, 240, 0.28);
  overflow: hidden;
}

.o2-fill {
  height: 100%;
  width: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #1cb8d8 0%, var(--o2-safe) 100%);
  box-shadow: 0 0 10px rgba(56, 228, 193, 0.45);
  transition: width 0.08s linear, background-color 0.2s ease;
}

.o2-bar.warn .o2-fill {
  background: linear-gradient(90deg, #f39c12 0%, var(--o2-warn) 100%);
  box-shadow: 0 0 10px rgba(255, 190, 59, 0.5);
}

.o2-bar.danger {
  border-color: rgba(255, 77, 90, 0.7);
  animation: o2Pulse 0.65s ease-in-out infinite;
}

.o2-bar.danger .o2-fill {
  background: linear-gradient(90deg, #d92638 0%, var(--o2-danger) 100%);
  box-shadow: 0 0 12px rgba(255, 77, 90, 0.7);
}

.o2-value {
  min-width: 38px;
  text-align: right;
  font-size: 12px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text-main);
}

@keyframes o2Pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 77, 90, 0.45); }
  50% { box-shadow: 0 0 0 4px rgba(255, 77, 90, 0); }
}

/* 중앙 플레이 영역 */
.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  overflow: hidden;
  touch-action: none;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.toast {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(5, 22, 36, 0.88);
  border: 1px solid rgba(126, 242, 255, 0.4);
  color: var(--text-main);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 4;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.danger {
  border-color: rgba(255, 77, 90, 0.65);
  color: #ffd3d7;
}

.toast.gold {
  border-color: rgba(255, 209, 102, 0.65);
  color: var(--accent-gold);
}

/* 하단 조작부 */
.controls {
  flex: 0 0 auto;
  z-index: 5;
  display: flex;
  align-items: stretch;
  gap: 8px;
  padding: 10px calc(10px + var(--safe-right)) calc(12px + var(--safe-bottom)) calc(10px + var(--safe-left));
  background: var(--panel-bg);
  border-top: 1px solid var(--panel-border);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  touch-action: none;
}

.ctrl-btn {
  appearance: none;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-md);
  color: var(--text-main);
  font-family: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 58px;
  min-width: 56px;
  touch-action: none;
  transition: transform 0.08s ease, background-color 0.12s ease, box-shadow 0.12s ease;
}

.ctrl-dir {
  flex: 0 0 64px;
  background: linear-gradient(180deg, rgba(28, 86, 114, 0.6) 0%, rgba(12, 42, 62, 0.85) 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12);
}

.ctrl-dir:active,
.ctrl-dir.is-pressed {
  transform: scale(0.95);
  background: linear-gradient(180deg, rgba(44, 128, 165, 0.8) 0%, rgba(18, 63, 92, 0.95) 100%);
  border-color: var(--accent-pearl);
}

.ctrl-icon {
  width: 26px;
  height: 26px;
}

.ctrl-dive {
  flex: 1 1 auto;
  padding: 8px 12px;
  background: linear-gradient(180deg, var(--accent-tewak-hi) 0%, var(--accent-tewak) 55%, var(--accent-tewak-lo) 100%);
  border-color: rgba(255, 210, 185, 0.45);
  box-shadow: 0 6px 18px rgba(255, 107, 53, 0.32), inset 0 1px 0 rgba(255, 255, 255, 0.35);
}

.ctrl-dive:active,
.ctrl-dive.is-pressed {
  transform: scale(0.97);
  background: linear-gradient(180deg, #21a6c2 0%, #0f6f8e 100%);
  border-color: var(--accent-pearl);
  box-shadow: 0 0 0 2px rgba(126, 242, 255, 0.35), inset 0 2px 6px rgba(0, 0, 0, 0.3);
}

.dive-main {
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: #fff;
}

.dive-sub {
  margin-top: 2px;
  font-size: 10px;
  font-weight: 600;
  color: rgba(255, 246, 240, 0.9);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

/* 오버레이 공통 (.open으로 표시/숨김) */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(16px + var(--safe-top)) calc(16px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
  background: rgba(3, 14, 24, 0.78);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.22s ease, visibility 0.22s ease;
}

.overlay.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.card {
  width: 100%;
  max-width: 360px;
  max-height: 100%;
  overflow-y: auto;
  padding: 22px 20px 20px;
  border-radius: var(--radius-lg);
  background: var(--card-bg);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow-card);
  text-align: center;
}

.emblem {
  width: 88px;
  height: 88px;
  margin: 0 auto 10px;
}

.emblem-svg {
  width: 100%;
  height: 100%;
  display: block;
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.4));
}

.eyebrow {
  margin: 0;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--accent-pearl);
}

.game-title {
  margin: 4px 0 6px;
  font-size: 38px;
  font-weight: 900;
  letter-spacing: 0.08em;
  line-height: 1.1;
  color: #ffffff;
  text-shadow: 0 4px 18px rgba(56, 228, 193, 0.3);
}

.tagline {
  margin: 0 0 14px;
  font-size: 13px;
  color: var(--text-muted);
}

.records-strip {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 14px;
}

.record-pill {
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(132, 224, 240, 0.15);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.record-label {
  font-size: 11px;
  color: var(--text-muted);
}

.record-pill strong {
  font-size: 17px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--accent-gold);
}

.guide-list {
  list-style: none;
  margin: 0 0 18px;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  background: rgba(3, 15, 26, 0.55);
  border: 1px solid rgba(132, 224, 240, 0.14);
  text-align: left;
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.guide-list li {
  position: relative;
  padding-left: 14px;
}

.guide-list li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 6px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-tewak);
}

.guide-list strong {
  color: var(--text-main);
}

/* 주요 버튼 (44px+ 터치 타겟, 1탭 시작/재시작) */
.primary-btn {
  width: 100%;
  min-height: 52px;
  padding: 12px 18px;
  border: 1px solid rgba(255, 214, 190, 0.45);
  border-radius: var(--radius-md);
  background: linear-gradient(180deg, var(--accent-tewak-hi) 0%, var(--accent-tewak) 55%, var(--accent-tewak-lo) 100%);
  color: #ffffff;
  font-family: inherit;
  font-size: 17px;
  font-weight: 800;
  letter-spacing: 0.03em;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: 0 8px 22px rgba(255, 107, 53, 0.36), inset 0 1px 0 rgba(255, 255, 255, 0.35);
  transition: transform 0.1s ease, filter 0.15s ease;
}

.primary-btn:hover {
  filter: brightness(1.06);
}

.primary-btn:active {
  transform: scale(0.97);
}

/* 결과 화면 */
.result-heading {
  margin: 6px 0 14px;
  font-size: 22px;
  font-weight: 900;
  color: #ffffff;
}

.result-score-box {
  position: relative;
  padding: 14px 12px;
  margin-bottom: 12px;
  border-radius: var(--radius-md);
  background: rgba(3, 15, 26, 0.65);
  border: 1px solid rgba(126, 242, 255, 0.25);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.result-score-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
}

.result-score {
  font-size: 38px;
  font-weight: 900;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  color: var(--accent-gold);
}

.badge-new {
  margin-top: 4px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--accent-tewak);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.result-stat {
  padding: 8px 6px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.result-stat-label {
  font-size: 10px;
  color: var(--text-muted);
}

.result-stat strong {
  font-size: 15px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text-main);
}

.dive-log {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
}

.dive-log-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(132, 224, 240, 0.12);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.dive-log-row.is-lost {
  border-color: rgba(255, 77, 90, 0.35);
  color: #ffb8be;
}

.dive-log-row .dive-pts {
  font-weight: 800;
  color: var(--accent-pearl);
}

.dive-log-row.is-lost .dive-pts {
  color: var(--o2-danger);
}

/* 320px 소형 폭 및 짧은 세로 화면 대응 */
@media (max-width: 360px) {
  .hud-value {
    font-size: 13px;
  }

  .ctrl-dir {
    flex: 0 0 54px;
  }

  .dive-main {
    font-size: 14px;
  }

  .dive-sub {
    font-size: 9px;
  }

  .card {
    padding: 18px 14px 16px;
  }

  .game-title {
    font-size: 32px;
  }
}

@media (max-height: 680px) {
  .emblem {
    width: 68px;
    height: 68px;
    margin-bottom: 6px;
  }

  .game-title {
    font-size: 32px;
  }

  .ctrl-btn {
    min-height: 50px;
  }
}

/* 데스크톱 여백 */
@media (min-width: 520px) {
  body {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .app {
    height: min(100dvh, 880px);
    border-radius: 22px;
  }
}
