:root {
  --sky-a: #87d3ff;
  --sky-b: #c9f0ff;
  --ocean-a: #1497db;
  --ocean-b: #056fa8;
  --panel: #ffffffeb;
  --ink: #173048;
  --sunny: #ffd65c;
  --mint: #76dfb0;
  --peach: #ffc69a;
  --coral: #ff7f7d;
  --shadow: 0 10px 24px rgba(8, 44, 74, 0.16);
}

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

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  min-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: "Fredoka", "Nunito", sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, var(--sky-b), var(--sky-a) 42%, #64c7f4 65%, #2a9fd8 100%);
}

button {
  font: inherit;
  touch-action: manipulation;
  min-height: 44px;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 100dvh;
}

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

.hud-top {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: calc(env(safe-area-inset-top, 0) + 10px) 12px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  z-index: 3;
}

.title-block,
.stats,
.quest,
.controls {
  background: var(--panel);
  border: 3px solid #ffffff;
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.title-block {
  padding: 8px 12px 9px;
}

.title-block .tiny {
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: #2e5672;
}

.title-block h1 {
  margin: 0;
  font-family: "Nunito", sans-serif;
  font-size: clamp(20px, 5vw, 30px);
  line-height: 1;
}

.stats {
  min-width: 180px;
  padding: 8px 10px;
}

.stats p {
  margin: 0 0 2px;
  font-size: clamp(10px, 2.6vw, 14px);
  line-height: 1.2;
}

.stats p:last-child {
  margin-bottom: 0;
}

#mood-text,
#combo-text,
#level-text,
#weather-text {
  border-radius: 999px;
  padding: 2px 7px;
  display: inline-block;
  margin-top: 1px;
}

#mood-text {
  background: #fff3bd;
}

#combo-text {
  background: #ffe0ea;
}

#level-text {
  background: #e8ebff;
}

#weather-text {
  background: #dbf4ff;
}

.quest {
  position: absolute;
  left: 12px;
  right: 12px;
  top: calc(env(safe-area-inset-top, 0) + 86px);
  z-index: 3;
  padding: 8px 12px 10px;
}

.quest-label {
  margin: 0 0 2px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #335977;
}

#quest-text {
  margin: 0 0 3px;
  font-size: clamp(14px, 3.9vw, 18px);
}

.chapter-text {
  margin: 0 0 2px;
  font-size: clamp(11px, 2.8vw, 14px);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #2f5f82;
}

.story-text {
  margin: 0;
  font-size: clamp(12px, 3vw, 15px);
  line-height: 1.25;
  color: #2d4f69;
}

.stage-goals {
  margin-top: 7px;
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(225, 244, 255, 0.78);
  border: 1px solid rgba(132, 182, 212, 0.5);
}

.stage-goals-title {
  margin: 0 0 3px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #2e5878;
}

.stage-goals-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 2px;
}

.stage-goals-list li {
  font-size: clamp(11px, 2.8vw, 13px);
  line-height: 1.2;
  color: #2c4f68;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stage-goals-list li.done {
  color: #2e7a49;
}

.controls {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: calc(env(safe-area-inset-bottom, 0) + 8px);
  z-index: 3;
  padding: 8px;
}

.tool-text {
  margin: 0 0 6px;
  font-size: 13px;
}

.button-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
}

.tool-btn {
  border: 0;
  border-radius: 13px;
  padding: 11px 8px;
  font-size: clamp(12px, 3.2vw, 15px);
  font-weight: 700;
  color: #10314b;
  background: #dff4ff;
  box-shadow: inset 0 -3px 0 rgba(13, 57, 94, 0.2);
  transition: transform 120ms ease, filter 120ms ease;
}

.tool-btn:active {
  transform: translateY(1px) scale(0.98);
  filter: brightness(0.98);
}

.tool-btn.active {
  background: linear-gradient(180deg, #ffe889, #ffd65c);
}

.collect-btn {
  background: linear-gradient(180deg, #a5f0c9, #76dfb0);
}

.event-log {
  list-style: none;
  margin: 7px 2px 0;
  padding: 0;
  min-height: 22px;
  max-height: 52px;
  overflow: hidden;
}

.event-log li {
  font-size: 12px;
  line-height: 1.2;
  color: #365672;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: linear-gradient(180deg, rgba(16, 60, 97, 0.38), rgba(8, 37, 62, 0.5));
  padding: 18px;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  width: min(460px, 100%);
  max-height: min(88dvh, 760px);
  overflow-y: auto;
  border-radius: 22px;
  border: 4px solid #fff;
  background: linear-gradient(180deg, #fff8d7, #ffffff);
  padding: 18px 16px;
  text-align: center;
  box-shadow: 0 18px 35px rgba(5, 34, 56, 0.35);
}

.stage-complete-card {
  text-align: left;
}

.stage-score-list {
  list-style: none;
  margin: 10px 0 8px;
  padding: 0;
  display: grid;
  gap: 3px;
}

.stage-score-list li {
  font-size: clamp(13px, 3.4vw, 16px);
  line-height: 1.22;
  color: #274d67;
}

.stage-medal-text {
  margin: 8px 0 2px;
  font-size: clamp(16px, 4.5vw, 22px);
  font-weight: 700;
}

.stage-score-total {
  margin: 0;
  font-size: clamp(16px, 4.5vw, 22px);
  font-weight: 700;
  color: #1e4f73;
}

.stage-best-text {
  margin: 4px 0 0;
  font-size: clamp(13px, 3.4vw, 17px);
  color: #2f5a79;
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-family: "Nunito", sans-serif;
  font-size: clamp(26px, 7vw, 36px);
  line-height: 1.04;
}

.overlay-card p {
  margin: 6px 0;
  font-size: clamp(15px, 4.2vw, 20px);
}

.start-btn {
  margin-top: 12px;
  border: 0;
  border-radius: 999px;
  padding: 12px 24px;
  font-size: 20px;
  font-weight: 800;
  color: #15324b;
  background: linear-gradient(180deg, #ffe889, #ffcb4a);
  box-shadow: inset 0 -4px 0 rgba(70, 49, 7, 0.18), 0 6px 10px rgba(5, 34, 56, 0.22);
}

.start-btn:disabled {
  opacity: 0.82;
  filter: saturate(0.78);
}

.boot-loader {
  margin-top: 10px;
  padding: 9px 10px 8px;
  border-radius: 12px;
  background: rgba(226, 246, 255, 0.75);
  border: 1px solid rgba(119, 174, 209, 0.48);
}

.boot-loader[hidden] {
  display: none;
}

.boot-status {
  margin: 0 0 6px;
  font-size: clamp(12px, 3.1vw, 15px);
  color: #2d5879;
}

.boot-track {
  width: 100%;
  height: 14px;
  border-radius: 999px;
  background: linear-gradient(180deg, #d9eaf5, #c8dce9);
  overflow: hidden;
  box-shadow: inset 0 2px 3px rgba(18, 49, 73, 0.2);
}

.boot-bar {
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, #76dfb0, #45c5ff);
  transition: width 90ms linear;
}

.boot-percent {
  margin: 6px 0 0;
  font-size: clamp(12px, 3.1vw, 15px);
  font-weight: 700;
  color: #1f4d6f;
}

.reset-btn {
  margin-top: 8px;
  border: 0;
  border-radius: 999px;
  padding: 9px 18px;
  font-size: 14px;
  font-weight: 700;
  color: #214663;
  background: linear-gradient(180deg, #e8f4ff, #d6e9fb);
  box-shadow: inset 0 -2px 0 rgba(33, 70, 99, 0.2);
}

.reset-btn.confirm {
  color: #5a2f16;
  background: linear-gradient(180deg, #ffe2ba, #ffcb8f);
}

@media (max-width: 560px) {
  .hud-top {
    padding: calc(env(safe-area-inset-top, 0) + 6px) 9px 6px;
    gap: 8px;
  }

  .title-block {
    padding: 7px 10px 8px;
  }

  .stats {
    min-width: 150px;
    padding: 6px 8px;
  }

  .quest {
    left: 8px;
    right: 8px;
    top: calc(env(safe-area-inset-top, 0) + 78px);
    padding: 7px 9px 8px;
    max-height: min(39dvh, 268px);
    overflow-y: auto;
  }

  .stage-goals {
    padding: 5px 6px;
  }

  .stage-goals-list {
    max-height: 86px;
    overflow-y: auto;
  }

  .controls {
    left: 6px;
    right: 6px;
    bottom: calc(env(safe-area-inset-bottom, 0) + 6px);
    padding: 7px;
  }

  .button-row {
    gap: 6px;
  }

  .tool-btn {
    padding: 9px 5px;
    font-size: 12px;
  }

  .event-log {
    min-height: 18px;
    max-height: 36px;
  }

  .overlay {
    padding: 10px;
  }

  .overlay-card {
    padding: 14px 12px;
  }

  .start-btn {
    width: 100%;
    font-size: 18px;
  }

  .reset-btn {
    width: 100%;
  }
}

@media (max-height: 520px) and (orientation: landscape) {
  .quest {
    max-height: min(48dvh, 210px);
    overflow-y: auto;
  }

  .controls {
    max-width: 760px;
    bottom: calc(env(safe-area-inset-bottom, 0) + 4px);
    padding: 6px;
  }

  .tool-btn {
    padding: 8px 6px;
  }
}

@media (min-width: 760px) {
  .quest {
    left: 18px;
    right: auto;
    width: min(420px, 50vw);
  }

  .controls {
    left: 16px;
    right: 16px;
    max-width: 640px;
    margin: 0 auto;
  }

  .button-row {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
}
