:root {
  --bg-top: #d8f7ff;
  --bg-bottom: #9ed7ff;
  --card: rgba(255, 255, 255, 0.88);
  --text: #1d2d3b;
  --muted: #3d6278;
  --accent: #ffb43a;
  --accent-dark: #e58e15;
  --shadow: 0 14px 30px rgba(9, 42, 68, 0.2);
  --radius: 20px;
  --mobile-pull-zone: 0px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  max-width: 100%;
  min-height: 100%;
  height: 100%;
  overflow-x: clip;
  overflow-y: auto;
  overscroll-behavior-x: none;
  overscroll-behavior-y: auto;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  color: var(--text);
  font-family: "Trebuchet MS", "Avenir Next", "Segoe UI", sans-serif;
}

#app {
  position: relative;
  width: 100%;
  max-width: 100%;
  min-height: 100dvh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: visible;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 8px calc(env(safe-area-inset-bottom, 0px) + 8px) 8px;
}

#pullRefreshZone {
  width: 100%;
  flex: 0 0 var(--mobile-pull-zone);
  touch-action: pan-y;
}

#game-shell {
  position: relative;
  margin: 0;
  width: 100%;
  max-width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  border-radius: 24px;
  overflow: hidden;
  box-shadow: var(--shadow);
  touch-action: none;
  background: linear-gradient(180deg, #89d1ff 0%, #79c8f4 100%);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
}

#game-shell,
#gameCanvas,
#hud,
#overlay {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  -webkit-tap-highlight-color: transparent;
}

#hud {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 22px);
  left: 22px;
  width: max-content;
  z-index: 5;
  pointer-events: none;
}

#hud.hidden {
  display: none;
}

.hud-card {
  display: inline-flex;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(14, 55, 79, 0.12);
  padding: 8px 12px;
}

.hud-card p {
  margin: 0;
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

.hud-card .label {
  color: var(--muted);
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.hud-card span:last-child {
  font-size: 1rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  min-width: 3.5ch;
  text-align: right;
  white-space: nowrap;
}

#overlay {
  position: absolute;
  top: var(--mobile-pull-zone);
  right: 0;
  bottom: 0;
  left: 0;
  display: grid;
  place-items: center;
  z-index: 8;
  padding: 16px;
  background: linear-gradient(180deg, rgba(12, 78, 115, 0.42), rgba(14, 107, 147, 0.28));
}

#overlay.hidden {
  display: none;
}

.overlay-card {
  width: min(560px, 100%);
  padding: 20px;
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow);
  text-align: center;
  max-height: calc(100dvh - 80px);
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

#leaderboardToggle { position: absolute; right: 24px; bottom: max(22px, env(safe-area-inset-bottom, 0px)); border: none; border-bottom: 1px solid rgba(255,255,255,.35); padding: 8px 2px 4px; background: transparent; color: rgba(255,255,255,.8); font: inherit; font-size: .8rem; cursor: pointer; }
#leaderboardToggle:hover { color: #fff; border-bottom-color: #fff; }
#leaderboardToggle:focus-visible { outline: 2px solid #fff; outline-offset: 5px; }
#scoreDialog { width: min(480px, calc(100% - 32px)); max-height: calc(100dvh - 48px); margin: auto; padding: 24px; border: 0; border-radius: 20px; background: #f5fbff; color: var(--text); box-shadow: 0 20px 60px rgba(0,42,62,.3); overflow-y: auto; overscroll-behavior: contain; touch-action: pan-y; }
#scoreDialog::backdrop { background: rgba(10,49,67,.46); backdrop-filter: blur(3px); }
#scorePanel { text-align: left; }
.score-name { margin: 22px auto 0; text-align: left; }
.score-name[hidden] { display: none; }
.player-name-label { display: block; font-size: .75rem; font-weight: 600; color: #527486; margin-bottom: 6px; }
.score-name-fields { display: flex; align-items: center; gap: 8px; }
#playerName { width: 100%; min-width: 0; flex: 1; min-height: 50px; border: 1px solid #7aadc5; border-radius: 10px; padding: 10px; font: inherit; font-size: 1rem; color: #17354a; background: #fff; }
#playerName:focus { outline: 2px solid #4296bd; outline-offset: 2px; }
#nameSaveStatus, #scoreStatus { font-size: 1rem; line-height: 1.4; color: #8a342d; margin: 12px 0 0; }
#nameSaveStatus:empty, #scoreStatus:empty { display: none; }
#highScoreNotice[hidden] { display: none; }
#highScoreNotice { border-radius: 999px; padding: 6px 12px; background: #dff3e5; color: #20633b; font-size: .85rem; font-weight: 800; white-space: nowrap; }
#claimHighScore { flex: 0 0 auto; min-height: 36px; border: 1px solid #267749; border-radius: 8px; padding: 7px 12px; background: #267749; color: #fff; font: inherit; font-size: .8rem; font-weight: 700; cursor: pointer; }
#claimHighScore:hover { background: #1c6039; }
#claimHighScore:disabled { opacity: .6; cursor: wait; }
#claimHighScore:focus-visible { outline: 2px solid #267749; outline-offset: 3px; }
.leaderboard-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.leaderboard-heading h2 { font-size: 1.4rem; line-height: 1.3; margin: 0; }
#closeLeaderboard { flex: 0 0 auto; display: grid; place-items: center; width: 36px; height: 36px; margin: -4px -8px 0 0; border: 0; border-radius: 50%; background: transparent; color: #527486; font: inherit; font-size: 1.5rem; cursor: pointer; }
#closeLeaderboard:hover { background: #e4f2f9; }
#defaultSettings, #retryScore { border: 1px solid #7aadc5; background: #eefaff; color: #194e69; border-radius: 7px; padding: 10px 12px; cursor: pointer; font: inherit; font-size: 1rem; }
#leaderboardList { padding-left: 26px; margin: 20px 0 0; font-size: 1.125rem; line-height: 1.4; }
#leaderboardList li { padding: 12px 0; border-bottom: 1px solid rgba(25,78,105,.09); }
#leaderboardList li span { display: inline-block; max-width: calc(100% - 64px); overflow-wrap: anywhere; }
#leaderboardList li strong { float: right; font-variant-numeric: tabular-nums; }

#tuningPanel[hidden] { display: none !important; }
.config-view #game-shell, .config-view #hud, .config-view #overlay, .config-view #pullRefreshZone, .config-view #scoreDialog { display: none !important; }
.config-view #tuningPanel { position: fixed; inset: 0; width: 100%; height: 100%; max-width: none; padding: 24px; display: grid; place-items: center; pointer-events: auto; background: #edf9ff; z-index: 40; }
.config-view #tuningDetails { position: static; inset: auto; width: min(720px, 100%); height: auto; max-height: calc(100dvh - 48px); border-radius: 14px; display: grid; grid-template-rows: auto minmax(0, 1fr); }
.config-view .tuning-body { height: min(720px, calc(100dvh - 112px)); max-height: none; }
.config-actions { display: flex; align-items: center; gap: 8px; }
.config-view #tuningCollapseButton { display: inline-flex; align-items: center; justify-content: center; width: auto; height: auto; min-height: 36px; padding: 8px 12px; font-size: .8rem; text-decoration: none; white-space: nowrap; }
@media (max-width: 900px), (pointer: coarse) {
  .config-view #tuningPanel { padding: 0; }
  .config-view #tuningDetails { width: 100%; height: 100%; max-height: none; border-radius: 0; }
  .config-view .tuning-body { height: 100%; }
}

#overlayTitle {
  margin: 0;
  font-size: clamp(2rem, 6vw, 3rem);
  line-height: 1.05;
}

#gameTitle { padding: 0; border: 0; background: transparent; color: inherit; font: inherit; }
#gameTitle[hidden], #endTitle[hidden], #actionButton[hidden] { display: none; }

#overlaySubtitle {
  margin: 12px 0 8px;
  color: #2c5c79;
  font-size: clamp(1.02rem, 3.3vw, 1.2rem);
}

#overlayReason {
  margin: 6px auto 16px;
  max-width: 46ch;
  color: #154861;
  font-size: 1rem;
}

#actionButton {
  border: none;
  border-radius: 14px;
  padding: 14px 24px;
  min-width: 220px;
  min-height: 52px;
  background: linear-gradient(180deg, var(--accent), var(--accent-dark));
  color: #1f2833;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  cursor: pointer;
  box-shadow: 0 9px 16px rgba(133, 80, 0, 0.34);
  margin-top: 10px;
}

#actionButton:active {
  transform: translateY(1px);
}

#tinyNote {
  margin: 12px 0 0;
  color: #355d73;
  font-size: 0.9rem;
}

#overlaySubtitle:empty,
#overlayReason:empty,
#tinyNote:empty {
  display: none;
}

#overlay.start-mode #actionButton {
  margin-top: 28px;
}

@media (max-width: 900px), (pointer: coarse) {
  :root {
    --mobile-pull-zone: calc(env(safe-area-inset-top, 0px) + 34px);
  }

  #app {
    width: 100%;
    max-width: 100%;
    min-height: 100svh;
    height: 100svh;
    overflow: hidden;
    padding: 0 0 env(safe-area-inset-bottom, 0px) 0;
  }

  #game-shell {
    border-radius: 0;
  }

  #hud {
    top: calc(var(--mobile-pull-zone) + 10px);
    left: 12px;
  }

  #overlay {
    left: 0;
    right: 0;
    padding-left: 10px;
    padding-right: 10px;
  }

  .overlay-card {
    width: 100%;
    max-width: 100%;
    padding: 18px 16px;
    border-radius: 18px;
  }
}

#overlaySubtitle { white-space: pre-line; }
#overlay.end-mode #overlayTitle {
  font-family: inherit;
  font-size: clamp(1.25rem, 3.6vw, 1.5rem);
  font-weight: 800;
  line-height: 1.35;
  color: #154861;
}
.end-score-row { display: flex; align-items: center; justify-content: center; gap: 12px; margin: 16px 0 0; }
#overlay.start-mode .end-score-row { display: none; }
#overlay.end-mode #overlayReason { margin: 0; font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }
#overlay.end-mode #actionButton { margin-top: 24px; }
