@import url("https://fonts.googleapis.com/css2?family=Fredoka:wght@400;600;700&display=swap");

:root {
  --bg: #fff7e9;
  --bg-2: #f3fff2;
  --accent: #ff8a5b;
  --accent-2: #59c173;
  --ink: #1c1c1c;
  --ui-gap: 14px;
  --fab-size: 96px;
  --fab-radius: 26px;
  --fab-inset: 12px;
  --overlay-top-reserve: 76px;
  --overlay-bottom-reserve: var(--fab-inset);
  --overlay-side-reserve: calc(var(--fab-size) + var(--fab-inset));
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  font-family: "Fredoka", "Segoe UI", sans-serif;
  background: radial-gradient(circle at top left, #fff1db, transparent 55%),
    radial-gradient(circle at 20% 60%, #eaffef, transparent 55%),
    linear-gradient(135deg, var(--bg), var(--bg-2));
}

#game {
  display: block;
  width: 100vw;
  height: 100vh;
  touch-action: manipulation;
}

.hud {
  position: fixed;
  top: var(--ui-gap);
  left: var(--ui-gap);
  color: var(--ink);
  z-index: 6;
  display: grid;
  gap: 6px;
}

.score {
  font-size: 1.6rem;
  font-weight: 700;
}

.hint {
  font-size: 0.9rem;
  opacity: 0.7;
}

.leaderboard-widget {
  position: fixed;
  top: calc(var(--ui-gap) + env(safe-area-inset-top, 0px));
  right: calc(var(--ui-gap) + env(safe-area-inset-right, 0px));
  width: min(320px, calc(100vw - 36px));
  z-index: 3;
  pointer-events: none;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: blur(8px);
  padding: 10px 12px 12px;
  color: var(--ink);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.1);
  display: grid;
  gap: 10px;
}

.leaderboard-brand {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.podium {
  width: 100%;
}

.overlay-panel-visible .leaderboard-widget {
  opacity: 0;
  pointer-events: none;
}

.podium-title {
  display: none;
}

.podium-body {
  display: grid;
  gap: 8px;
}

.podium-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  line-height: 1.1;
}

.podium-icon {
  font-size: 1.2rem;
}

.podium-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 700;
}

.podium-score {
  font-weight: 600;
}

.podium-row.rank-1 {
  font-size: 1.2rem;
}

.podium-row.rank-2 {
  font-size: 1rem;
}

.podium-row.rank-3 {
  font-size: 0.9rem;
}

.podium-empty {
  opacity: 0.7;
  font-size: 0.86rem;
}

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.78);
  backdrop-filter: blur(8px);
  z-index: 2;
}

.overlay.overlay-active {
  z-index: 4;
  place-items: start center;
  padding-top: calc(var(--overlay-top-reserve) + env(safe-area-inset-top, 0px));
  padding-bottom: calc(var(--overlay-bottom-reserve) + env(safe-area-inset-bottom, 0px));
  padding-left: calc(var(--ui-gap) + env(safe-area-inset-left, 0px));
  padding-right: calc(var(--ui-gap) + var(--overlay-side-reserve) + env(safe-area-inset-right, 0px));
}

.panel {
  background: #fff;
  padding: 26px 32px;
  border-radius: var(--fab-radius);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
  text-align: center;
  width: min(500px, calc(100vw - 24px));
}

.panel-brand {
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: var(--ink);
  margin-bottom: 6px;
}

.overlay.overlay-active .panel {
  max-height: calc(100dvh - var(--overlay-top-reserve) - var(--overlay-bottom-reserve) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  overflow: auto;
}


.title {
  font-size: 1.6rem;
  font-weight: 700;
}

.subtitle {
  margin-top: 6px;
  opacity: 0.7;
}

.final-score {
  margin-top: 10px;
  font-size: 2.1rem;
  font-weight: 700;
  color: var(--ink);
}

.btn {
  margin-top: 14px;
  border: none;
  border-radius: 999px;
  padding: 10px 20px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
}

.start-fab {
  position: fixed;
  right: calc(var(--fab-inset) + env(safe-area-inset-right, 0px));
  bottom: calc(var(--fab-inset) + env(safe-area-inset-bottom, 0px));
  width: var(--fab-size);
  height: var(--fab-size);
  border: none;
  border-radius: var(--fab-radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: linear-gradient(160deg, #ffb46a 0%, #ff8a3d 50%, #f05a24 100%);
  color: #fff;
  font-family: inherit;
  cursor: pointer;
  z-index: 5;
  box-shadow: none;
  text-shadow: none;
  transition: transform 0.08s ease, filter 0.08s ease;
}

.start-fab::before {
  content: "";
  position: absolute;
  top: 10px;
  left: 12px;
  right: 12px;
  height: 32%;
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.55), rgba(255, 255, 255, 0));
  pointer-events: none;
  opacity: 0.6;
}

.start-fab::after {
  content: none;
}

.start-fab:active {
  transform: scale(0.985);
  filter: brightness(0.9);
}

.start-icon {
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.start-icon svg {
  width: 100%;
  height: 100%;
  fill: currentColor;
}

.icon-replay {
  display: none;
}

.start-fab.mode-restart .icon-play {
  display: none;
}

.start-fab.mode-restart .icon-replay {
  display: block;
}

.icon-save,
.icon-check,
.icon-spinner {
  display: none;
}

.save-fab .icon-save {
  display: block;
}

.save-fab.is-saving .icon-save,
.save-fab.is-success .icon-save {
  display: none;
}

.save-fab.is-saving .icon-spinner {
  display: block;
}

.save-fab.is-success .icon-check {
  display: block;
}

.icon-spinner {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-dasharray: 40 14;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); transform-origin: 12px 12px; }
  100% { transform: rotate(360deg); transform-origin: 12px 12px; }
}

.start-label {
  font-size: 0.84rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.save-fab {
  position: fixed;
  right: calc(var(--fab-inset) + env(safe-area-inset-right, 0px));
  bottom: calc(var(--fab-inset) + var(--fab-size) + 12px + env(safe-area-inset-bottom, 0px));
  width: var(--fab-size);
  height: var(--fab-size);
  border: none;
  border-radius: var(--fab-radius);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: linear-gradient(160deg, #86f0a7 0%, #30c071 52%, #1a8e55 100%);
  color: #fff;
  font-family: inherit;
  cursor: pointer;
  z-index: 5;
  box-shadow: none;
  text-shadow: none;
  transition: transform 0.08s ease, filter 0.08s ease;
}

.save-fab:active {
  transform: scale(0.985);
  filter: brightness(0.9);
}

.save-fab::before {
  content: "";
  position: absolute;
  top: 10px;
  left: 12px;
  right: 12px;
  height: 32%;
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.55), rgba(255, 255, 255, 0));
  pointer-events: none;
  opacity: 0.6;
}

.overlay-panel-visible .save-fab {
  display: flex;
}

.submit-wrap {
  margin-top: 16px;
  text-align: left;
}

.submit-wrap label {
  display: block;
  font-size: 0.86rem;
  margin-bottom: 6px;
}

.submit-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

#playerName {
  width: 100%;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 16px;
  padding: 10px 14px;
  height: 44px;
  font-family: inherit;
  font-size: 0.98rem;
}

.name-confirm-btn {
  height: 44px;
  width: 44px;
  border: none;
  border-radius: 14px;
  background: #f2f2f2;
  color: #1c1c1c;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.08s ease, filter 0.08s ease;
}

.name-confirm-btn:active {
  transform: scale(0.985);
  filter: brightness(0.9);
}

.name-confirm-icon {
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.name-confirm-icon svg {
  width: 100%;
  height: 100%;
  fill: currentColor;
}


.submit-msg {
  margin-top: 8px;
  min-height: 1.1em;
  font-size: 0.84rem;
  opacity: 0.8;
}

.submit-msg.error {
  color: #c62828;
  opacity: 1;
}

.overlay-leaderboard {
  margin-top: 18px;
  text-align: left;
}

.lb-title {
  font-weight: 700;
  letter-spacing: 0.02em;
  margin-bottom: 6px;
}

.lb-head,
.lb-row {
  display: grid;
  grid-template-columns: 1.15fr 0.55fr 1fr;
  gap: 8px;
  align-items: center;
}

.lb-head {
  font-size: 0.78rem;
  opacity: 0.75;
  padding-bottom: 4px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.lb-body {
  display: grid;
  gap: 2px;
  margin-top: 4px;
  max-height: 210px;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

.lb-row {
  font-size: 0.82rem;
  padding: 4px 0;
}

.lb-empty {
  font-size: 0.82rem;
  opacity: 0.7;
  padding: 8px 0 4px;
}

.lb-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-score {
  font-weight: 600;
}

.lb-date {
  opacity: 0.8;
}

.hidden {
  display: none;
}

@media (max-width: 720px) {
  :root {
    --ui-gap: 10px;
    --fab-size: 88px;
    --fab-radius: 24px;
    --fab-inset: 10px;
    --overlay-top-reserve: 68px;
    --overlay-bottom-reserve: var(--fab-inset);
  }

  html, body {
    touch-action: manipulation;
  }

  .leaderboard-widget {
    width: min(180px, calc(100vw - 16px - env(safe-area-inset-right, 0px)));
    padding: 8px 8px 10px;
    overflow: hidden;
    gap: 6px;
  }

  .podium-row.rank-1 {
    font-size: 0.92rem;
  }

  .podium-row.rank-2 {
    font-size: 0.82rem;
  }

  .podium-row.rank-3 {
    font-size: 0.76rem;
  }

  .podium-body {
    gap: 5px;
  }

  .podium-row {
    gap: 7px;
  }

  .score {
    font-size: 1.2rem;
  }

  .panel {
    padding: 20px;
    width: 100%;
  }

  .overlay.overlay-active .panel {
    height: calc(100dvh - var(--overlay-top-reserve) - var(--overlay-bottom-reserve) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  }

  .final-score {
    font-size: 1.7rem;
  }

  .start-label {
    font-size: 0.78rem;
  }

  .leaderboard-brand {
    font-size: 0.92rem;
  }
}

@media (max-width: 900px) and (orientation: landscape), (max-height: 520px) {
  :root {
    --ui-gap: 8px;
    --fab-size: 76px;
    --fab-radius: 22px;
    --fab-inset: 8px;
    --overlay-top-reserve: 58px;
    --overlay-bottom-reserve: var(--fab-inset);
    --overlay-side-reserve: calc(var(--fab-size) + var(--fab-inset));
  }

  .overlay {
    padding:
      calc(8px + env(safe-area-inset-top, 0px))
      calc(8px + env(safe-area-inset-right, 0px))
      calc(8px + env(safe-area-inset-bottom, 0px))
      calc(8px + env(safe-area-inset-left, 0px));
  }

  .panel {
    width: 100%;
    padding: 16px 18px;
  }

  .overlay.overlay-active .panel {
    height: calc(100dvh - var(--overlay-top-reserve) - var(--overlay-bottom-reserve) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  }

  .overlay-leaderboard {
    margin-top: 10px;
  }

  .lb-body {
    max-height: min(34dvh, 170px);
  }

  .leaderboard-widget {
    width: auto;
    max-width: calc(100vw - 16px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 6px 10px;
  }

  .podium-body {
    display: flex;
    align-items: center;
    gap: 0;
  }

  .podium-row {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    line-height: 1.05;
    padding-right: 10px;
    margin-right: 10px;
  }

  .podium-row:not(:last-child) {
    border-right: 1px solid rgba(0, 0, 0, 0.2);
  }

  .podium-row:last-child {
    padding-right: 0;
    margin-right: 0;
    border-right: none;
  }

  .podium-name {
    max-width: 7ch;
  }

  .podium-score {
    margin-left: 1px;
  }

  .podium-row.rank-1 {
    font-size: 0.82rem;
  }

  .podium-row.rank-2 {
    font-size: 0.78rem;
  }

  .podium-row.rank-3 {
    font-size: 0.74rem;
  }

  .start-label {
    font-size: 0.64rem;
  }

}

@media (min-width: 901px) and (min-height: 600px) {
  .overlay.overlay-active {
    place-items: center;
    padding-top: 0;
    padding-bottom: 0;
    padding-left: 0;
    padding-right: 0;
  }

  .overlay.overlay-active .panel {
    max-height: calc(100dvh - 48px);
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
