/* Falling Dream: dream-indigo UI theme, portrait-first */

:root {
  --ink: #1b1546;
  --ink-deep: #100b2c;
  --cream: #fdf4ff;
  --pink: #ff8fd6;
  --lilac: #9b86ff;
  --gold: #ffd47c;
  --cyan: #8ff7ff;
  --rose: #ff6f91;
  /* Safe areas (notch, home indicator); 0 where the browser has none. */
  --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);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--ink);
  font-family: "Segoe UI", "Trebuchet MS", Verdana, system-ui, sans-serif;
  color: var(--cream);
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: fixed;
  inset: 0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

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

.hidden {
  display: none !important;
}

/* Device-specific wording: html[data-input] is set by src/device.js. */
html[data-input="keyboard"] .only-touch,
html[data-input="touch"] .only-keyboard {
  display: none !important;
}

/* ---------- Full-screen effects ---------- */

/* Slow motion: desaturate and cool the whole frame. */
#slowmo-veil {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.45s ease;
  background: radial-gradient(ellipse at 50% 50%, rgba(120, 150, 255, 0.05) 0%, rgba(40, 30, 90, 0.35) 100%);
  backdrop-filter: saturate(0.35) contrast(1.05);
  -webkit-backdrop-filter: saturate(0.35) contrast(1.05);
}

#slowmo-veil.on {
  opacity: 1;
}

#flash {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  opacity: 0;
}

#flash.near {
  background: radial-gradient(ellipse at 50% 50%, transparent 45%, rgba(255, 255, 255, 0.55) 100%);
  animation: flash-fade 0.5s ease-out;
}

#flash.hit {
  background: radial-gradient(ellipse at 50% 50%, rgba(255, 111, 145, 0.35) 0%, rgba(255, 111, 145, 0.75) 100%);
  animation: flash-fade 0.9s ease-out;
}

@keyframes flash-fade {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

/* ---------- Touch layer ---------- */

#touch-layer {
  position: absolute;
  inset: 0;
  z-index: 4; /* under the HUD (5) so the pause button stays tappable */
  display: none;
  touch-action: none;
}

html[data-input="touch"] #touch-layer.active {
  display: block;
}

/* ---------- HUD ---------- */

#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

#hud-top-left {
  position: absolute;
  top: calc(clamp(12px, 2.5vh, 28px) + var(--safe-top));
  left: calc(clamp(12px, 3vw, 28px) + var(--safe-left));
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}

#depth-box {
  display: flex;
  align-items: baseline;
  gap: 4px;
  padding: 6px 18px 6px 16px;
  font-size: clamp(26px, 4vw, 40px);
  font-weight: 800;
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
  color: var(--cream);
  background: rgba(27, 21, 70, 0.5);
  border: 2px solid rgba(255, 143, 214, 0.4);
  border-radius: 16px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

#depth-box .unit {
  font-size: 0.5em;
  font-weight: 700;
  color: var(--pink);
}

#star-box,
#shield-box {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 14px 5px 10px;
  font-size: clamp(15px, 2vw, 20px);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  background: rgba(27, 21, 70, 0.5);
  border: 2px solid rgba(255, 212, 124, 0.4);
  border-radius: 999px;
  backdrop-filter: blur(4px);
}

#shield-box {
  border-color: rgba(143, 247, 255, 0.5);
  color: var(--cyan);
  font-size: clamp(13px, 1.6vw, 16px);
  animation: pop-in 0.3s ease-out;
}

.star-icon {
  display: inline-block;
  width: 18px;
  height: 18px;
  background: var(--gold);
  clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
  filter: drop-shadow(0 0 6px rgba(255, 212, 124, 0.9));
}

.star-icon.small {
  width: 15px;
  height: 15px;
  margin-right: 4px;
}

.shield-icon {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, #e5ffff, var(--cyan) 55%, rgba(143, 247, 255, 0.2));
  box-shadow: 0 0 8px rgba(143, 247, 255, 0.8);
}

.shield-icon.small {
  margin-right: 4px;
}

.hourglass-icon {
  display: inline-block;
  width: 12px;
  height: 17px;
  margin: 0 6px 0 2px;
  background: linear-gradient(180deg, var(--cyan) 0%, #52c8ff 100%);
  clip-path: polygon(0 0, 100% 0, 100% 10%, 60% 50%, 100% 90%, 100% 100%, 0 100%, 0 90%, 40% 50%, 0 10%);
  filter: drop-shadow(0 0 5px rgba(143, 247, 255, 0.8));
}

.ctl-icon {
  display: inline-block;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  flex: none;
  margin-right: 4px;
}

.ctl-icon.drag {
  border-radius: 8px;
  background: rgba(253, 244, 255, 0.16);
  border: 2px solid rgba(253, 244, 255, 0.55);
  position: relative;
}

.ctl-icon.drag::before {
  content: "\2194"; /* ↔ */
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 900;
  color: var(--cream);
  line-height: 1;
}

#hud-top-right {
  position: absolute;
  top: calc(clamp(12px, 2.5vh, 28px) + var(--safe-top));
  right: calc(clamp(12px, 3vw, 28px) + var(--safe-right));
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-btn {
  pointer-events: auto;
  cursor: pointer;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 2px solid rgba(255, 143, 214, 0.4);
  background: rgba(27, 21, 70, 0.55);
  color: var(--cream);
  display: grid;
  place-items: center;
  backdrop-filter: blur(4px);
  transition: transform 0.12s ease, background 0.12s ease;
}

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

.icon-btn:hover,
.icon-btn:focus-visible {
  background: rgba(27, 21, 70, 0.85);
  transform: scale(1.06);
  outline: none;
}

#mute-button .ic-off {
  display: none;
}

#mute-button.muted .ic-on {
  display: none;
}

#mute-button.muted .ic-off {
  display: block;
}

#mute-button.muted {
  color: rgba(253, 244, 255, 0.55);
}

html[data-input="touch"] .icon-btn {
  width: 56px;
  height: 56px;
}

/* Zone announcement: soft, centred, fades in and out. */
#zone-banner {
  position: absolute;
  top: calc(11% + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  width: min(90vw, 560px);
  text-align: center;
  pointer-events: none;
  animation: banner 3.8s ease-in-out forwards;
}

#zone-name {
  font-size: clamp(26px, 5vw, 48px);
  font-weight: 300;
  letter-spacing: 0.32em;
  text-transform: uppercase;
  color: var(--cream);
  text-shadow: 0 0 24px rgba(255, 255, 255, 0.55), 0 2px 12px rgba(0, 0, 0, 0.45);
}

#zone-line {
  margin-top: 8px;
  font-size: clamp(14px, 2vw, 19px);
  font-style: italic;
  letter-spacing: 0.06em;
  color: rgba(253, 244, 255, 0.9);
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

@keyframes banner {
  0% {
    opacity: 0;
    transform: translateX(-50%) translateY(10px);
    letter-spacing: 0.5em;
  }
  18% {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
  78% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translateX(-50%) translateY(-14px);
  }
}

#toast {
  position: absolute;
  top: calc(24% + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  max-width: min(600px, 86vw);
  padding: 8px 22px;
  font-size: clamp(16px, 2.2vw, 24px);
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--ink-deep);
  background: linear-gradient(135deg, var(--gold) 0%, var(--pink) 100%);
  border-radius: 999px;
  box-shadow: 0 6px 30px rgba(255, 143, 214, 0.45);
  text-align: center;
  animation: toast-pop 0.3s ease-out;
  pointer-events: none;
}

#toast.soft {
  color: var(--cream);
  background: rgba(27, 21, 70, 0.7);
  border: 2px solid rgba(143, 247, 255, 0.55);
  box-shadow: 0 6px 30px rgba(143, 247, 255, 0.25);
  font-weight: 600;
  font-style: italic;
}

#toast.near {
  color: var(--ink-deep);
  background: linear-gradient(135deg, #ffffff 0%, var(--cyan) 100%);
  box-shadow: 0 6px 30px rgba(143, 247, 255, 0.6);
  font-size: clamp(18px, 2.6vw, 28px);
  letter-spacing: 0.12em;
}

@keyframes toast-pop {
  from {
    transform: translateX(-50%) scale(0.6);
    opacity: 0;
  }
  to {
    transform: translateX(-50%) scale(1);
    opacity: 1;
  }
}

#controls-hint {
  position: absolute;
  bottom: calc(clamp(10px, 2vh, 22px) + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(12px, 1.4vw, 15px);
  letter-spacing: 0.08em;
  color: rgba(253, 244, 255, 0.85);
  background: rgba(27, 21, 70, 0.5);
  padding: 7px 16px;
  border-radius: 999px;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.8);
  white-space: nowrap;
  animation: hint-fade 0.8s ease 9s forwards;
}

html[data-input="touch"] #controls-hint {
  bottom: calc(22px + var(--safe-bottom));
  max-width: 82vw;
  white-space: normal;
  text-align: center;
  line-height: 1.35;
  font-size: 13px;
  letter-spacing: 0.05em;
  animation: hint-fade 0.8s ease 6s forwards;
}

@keyframes hint-fade {
  to {
    opacity: 0;
  }
}

/* ---------- Overlay screens ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  background:
    radial-gradient(ellipse at 50% -10%, rgba(255, 143, 214, 0.28), transparent 55%),
    radial-gradient(ellipse at 50% 110%, rgba(155, 134, 255, 0.3), transparent 60%),
    linear-gradient(180deg, rgba(16, 11, 44, 0.78) 0%, rgba(27, 21, 70, 0.7) 100%);
  padding: calc(16px + var(--safe-top)) calc(16px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
}

.panel {
  max-width: 600px;
  width: 100%;
  margin: auto;
  text-align: center;
  padding: clamp(24px, 5vh, 52px) clamp(20px, 4vw, 48px);
  background: rgba(16, 11, 44, 0.55);
  border: 1px solid rgba(253, 244, 255, 0.18);
  border-radius: 28px;
  box-shadow: 0 20px 80px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(253, 244, 255, 0.14), 0 0 0 1px rgba(255, 143, 214, 0.12);
  backdrop-filter: blur(10px);
}

.title {
  font-size: clamp(36px, 7vw, 68px);
  font-weight: 200;
  letter-spacing: 0.28em;
  line-height: 1.05;
  background: linear-gradient(180deg, #ffffff 10%, var(--pink) 55%, var(--lilac) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 6px 30px rgba(255, 143, 214, 0.25);
  margin-bottom: 14px;
}

.title.over {
  background-image: linear-gradient(180deg, #ffffff 10%, var(--gold) 55%, var(--rose) 100%);
  text-shadow: 0 6px 30px rgba(255, 111, 145, 0.3);
}

.tagline {
  font-size: clamp(15px, 1.8vw, 19px);
  line-height: 1.55;
  color: rgba(253, 244, 255, 0.9);
  margin-bottom: 22px;
}

.best {
  font-size: clamp(14px, 1.6vw, 17px);
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--gold);
  margin: -8px 0 22px;
}

.controls-list {
  list-style: none;
  text-align: left;
  display: inline-block;
  margin: 0 auto 26px;
  font-size: clamp(14px, 1.7vw, 17px);
  line-height: 2.2;
  color: rgba(253, 244, 255, 0.95);
}

.controls-list li {
  display: flex;
  align-items: center;
  gap: 8px;
}

.controls-list.compact {
  margin-bottom: 14px;
  line-height: 2;
}

kbd {
  display: inline-block;
  min-width: 30px;
  padding: 3px 8px;
  text-align: center;
  font-family: inherit;
  font-weight: 700;
  font-size: 0.85em;
  color: var(--ink-deep);
  background: linear-gradient(180deg, var(--cream), #d9c9e6);
  border-radius: 6px;
  box-shadow: 0 3px 0 #8f7aa8;
}

.stats {
  font-size: clamp(15px, 1.8vw, 19px);
  color: var(--gold);
  font-weight: 700;
  letter-spacing: 0.05em;
  margin-bottom: 18px;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 4px auto 18px;
  max-width: 420px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px 10px;
  background: rgba(253, 244, 255, 0.06);
  border: 1px solid rgba(253, 244, 255, 0.14);
  border-radius: 16px;
}

.stat-label {
  font-size: clamp(11px, 1.3vw, 13px);
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: rgba(253, 244, 255, 0.6);
}

.stat-value {
  font-size: clamp(22px, 3vw, 32px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--cream);
}

.new-best {
  font-size: clamp(16px, 2vw, 22px);
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold);
  text-shadow: 0 0 18px rgba(255, 212, 124, 0.7);
  margin-bottom: 18px;
  animation: pop-in 0.4s ease-out;
}

@keyframes pop-in {
  from {
    transform: scale(0.6);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.note {
  font-size: clamp(13px, 1.5vw, 15px);
  color: rgba(253, 244, 255, 0.7);
  margin-bottom: 20px;
}

.btn-row {
  display: flex;
  gap: 14px;
  justify-content: center;
  flex-wrap: wrap;
}

.btn {
  pointer-events: auto;
  cursor: pointer;
  font-family: inherit;
  font-size: clamp(17px, 2vw, 22px);
  font-weight: 800;
  letter-spacing: 0.08em;
  color: var(--ink-deep);
  padding: 15px 44px;
  border: none;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--gold) 0%, var(--pink) 60%, var(--lilac) 100%);
  box-shadow: 0 8px 30px rgba(255, 143, 214, 0.45), inset 0 2px 0 rgba(255, 255, 255, 0.45);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  touch-action: manipulation;
}

.btn:hover,
.btn:focus-visible {
  transform: translateY(-2px) scale(1.03);
  box-shadow: 0 12px 40px rgba(255, 143, 214, 0.65), inset 0 2px 0 rgba(255, 255, 255, 0.45);
  outline: none;
}

.btn:active {
  transform: translateY(1px) scale(0.99);
}

.btn-secondary {
  color: var(--cream);
  background: rgba(27, 21, 70, 0.55);
  border: 2px solid rgba(255, 143, 214, 0.5);
  box-shadow: none;
}

.btn-secondary:hover,
.btn-secondary:focus-visible {
  box-shadow: 0 8px 30px rgba(255, 143, 214, 0.3);
}

/* Quality selector (start + pause screens). */
.quality-row {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px 14px;
  margin-bottom: 22px;
}

.quality-label {
  font-size: clamp(13px, 1.5vw, 15px);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(253, 244, 255, 0.7);
}

.seg {
  display: inline-flex;
  padding: 3px;
  border-radius: 999px;
  background: rgba(16, 11, 44, 0.6);
  border: 2px solid rgba(255, 143, 214, 0.3);
}

.seg-btn {
  cursor: pointer;
  pointer-events: auto;
  min-width: 72px;
  min-height: 40px;
  padding: 6px 14px;
  border: none;
  border-radius: 999px;
  font-family: inherit;
  font-size: clamp(13px, 1.5vw, 15px);
  font-weight: 700;
  letter-spacing: 0.04em;
  color: rgba(253, 244, 255, 0.75);
  background: transparent;
  transition: background 0.15s ease, color 0.15s ease;
  touch-action: manipulation;
}

.seg-btn:hover,
.seg-btn:focus-visible {
  color: var(--cream);
  outline: none;
}

.seg-btn.active {
  color: var(--ink-deep);
  background: linear-gradient(135deg, var(--gold) 0%, var(--pink) 100%);
  box-shadow: 0 4px 14px rgba(255, 143, 214, 0.45);
}

html[data-input="touch"] .seg-btn {
  min-height: 48px;
}

/* ---------- FPS readout (?fps=1) ---------- */

#fps-meter {
  position: absolute;
  top: calc(8px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  padding: 4px 12px;
  border-radius: 999px;
  font-family: Consolas, "SF Mono", Menlo, monospace;
  font-size: 12px;
  color: var(--cream);
  background: rgba(16, 11, 44, 0.75);
  border: 1px solid rgba(255, 143, 214, 0.4);
  pointer-events: none;
  white-space: nowrap;
}

/* ---------- Small / short screens ---------- */

@media (max-width: 560px) {
  .controls-list {
    line-height: 2;
  }
}

/* Landscape phones: the panel would overflow a ~390px-tall viewport, so the
   story and the actions sit side by side. */
@media (max-height: 520px) and (min-width: 640px) {
  #start-screen .panel,
  #pause-screen .panel {
    max-width: 820px;
    display: grid;
    grid-template-columns: 1.15fr 1fr;
    column-gap: 24px;
    align-items: center;
    padding: 16px 22px;
    border-radius: 18px;
  }

  #start-screen .panel-story {
    text-align: left;
  }

  #start-screen .title,
  #pause-screen .title {
    font-size: clamp(28px, 9vh, 44px);
    margin-bottom: 6px;
  }

  .tagline {
    font-size: 12.5px;
    line-height: 1.4;
    margin-bottom: 8px;
  }

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

  .controls-list {
    font-size: 12.5px;
    line-height: 1.8;
    margin-bottom: 10px;
  }

  .quality-row {
    margin-bottom: 10px;
  }

  .note {
    margin-bottom: 10px;
  }

  .btn {
    padding: 10px 28px;
    font-size: 15px;
  }

  #over-screen .panel {
    padding: 14px 22px;
  }

  #over-screen .title {
    font-size: clamp(24px, 8vh, 38px);
    margin-bottom: 6px;
  }

  .stat-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    margin-bottom: 10px;
    max-width: none;
  }

  .stat {
    padding: 8px 6px;
  }

  .new-best {
    margin-bottom: 10px;
  }
}

/* Portrait phones: tighter vertical rhythm so the start panel fits. */
@media (max-width: 560px) and (orientation: portrait) {
  .panel {
    padding: 22px 16px;
  }

  .title {
    font-size: clamp(30px, 10vw, 44px);
    letter-spacing: 0.2em;
    margin-bottom: 10px;
  }

  .tagline {
    font-size: 14px;
    margin-bottom: 14px;
  }

  .controls-list {
    font-size: 13.5px;
    line-height: 1.9;
    margin-bottom: 16px;
  }

  .quality-row {
    margin-bottom: 16px;
  }

  #depth-box {
    font-size: 24px;
    padding: 4px 14px;
  }

  /* The dreamer sits around 40–45% down the screen in portrait: the banner
     goes above the head, toasts below the feet, clear of the HUD stack. */
  #zone-banner {
    top: calc(21% + var(--safe-top));
  }

  #toast {
    top: calc(60% + var(--safe-top));
  }
}
