/* ==========================================================================
   Main Visual System, Stage Layout, Typography & Android Viewport Adaptation
   ========================================================================== */

:root {
  /* Starry Night Palette & Warm Paper Contrast */
  --bg-night-deep: #091022;
  --bg-night-mid: #121E3A;
  --bg-night-low: #1B2A4A;
  --starlight: #F7EFE2;
  --starlight-soft: rgba(244, 234, 216, 0.78);

  --bg-warm: #0B1328;
  --bg-cream: #F3EADB;
  --champagne: #E6D3B3;
  --champagne-deep: #D2B486;
  --mist-rose: #E9CFCB;

  --ink-primary: #3A3530;
  --ink-secondary: #6A6158;
  --ink-muted: #988E83;

  --paper-base: #FCF9F3;
  --paper-edge: #EAE0CF;
  --shadow-warm: rgba(88, 70, 48, 0.14);

  /* Typography */
  --font-serif: "Songti SC", "Noto Serif SC", "Source Han Serif SC", "STSong", "SimSun", Georgia, serif;
  --font-sans: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;

  /* Dynamic viewport height fallback (updated by device.js) */
  --app-h: 100vh;
  --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);

  /* Z-index layers */
  --z-backdrop: 1;
  --z-particles: 2;
  --z-atmosphere: 3;
  --z-mosaic: 10;
  --z-envelope: 20;
  --z-letter: 30;
  --z-origami: 40;
  --z-finale: 50;
  --z-audio: 93;
}

@supports (height: 100dvh) {
  :root {
    --app-h: 100dvh;
  }
}

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: var(--app-h);
  overflow: hidden;
  overflow-x: hidden;
  background-color: var(--bg-warm);
  color: var(--ink-primary);
  font-family: var(--font-serif);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
  touch-action: manipulation;
}

button {
  font-family: inherit;
  color: inherit;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

button:focus-visible {
  outline: 1.5px solid var(--champagne-deep);
  outline-offset: 4px;
}

/* Main full-viewport stage */
.app-stage {
  position: relative;
  width: 100vw;
  max-width: 100%;
  height: var(--app-h);
  overflow: hidden;
  background: radial-gradient(ellipse at 50% 35%, #172546 0%, #0E1830 58%, #080E1E 100%);
  user-select: none;
  -webkit-user-select: none;
}

/* Fullscreen canvases */
.stage-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}

.stage-canvas--backdrop {
  z-index: var(--z-backdrop);
}

.stage-canvas--particles {
  z-index: var(--z-particles);
}

.stage-canvas--mosaic {
  z-index: var(--z-mosaic);
  opacity: 0;
  will-change: opacity;
}

.stage-canvas--origami {
  z-index: var(--z-origami);
  opacity: 0;
  will-change: opacity;
}

/* Atmospheric fine grain & soft starry edge vignette */
.atmosphere-grain {
  position: absolute;
  inset: -40px;
  z-index: var(--z-atmosphere);
  pointer-events: none;
  background-image: url("../assets/textures/film-grain.svg");
  background-size: 180px 180px;
  opacity: 0.04;
  mix-blend-mode: screen;
}

.atmosphere-vignette {
  position: absolute;
  inset: 0;
  z-index: var(--z-atmosphere);
  pointer-events: none;
  background: radial-gradient(
    ellipse at 50% 48%,
    rgba(9, 16, 34, 0) 48%,
    rgba(4, 8, 18, 0.42) 100%
  );
  transition: opacity 1.8s ease;
}

.app-stage.is-sky-open .atmosphere-vignette {
  opacity: 0.55;
}

/* Shared scene container */
.scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  contain: layout style;
}

/* Subtle Audio Control (top right, respects safe area) */
.audio-control {
  position: fixed;
  top: calc(14px + var(--safe-top));
  right: calc(14px + var(--safe-right));
  z-index: var(--z-audio);
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
  gap: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.8s ease;
}

.audio-control.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.audio-toggle {
  width: 44px;
  height: 44px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  color: rgba(246, 236, 218, 0.88);
  background: rgba(16, 26, 52, 0.62);
  border: 1px solid rgba(230, 211, 179, 0.34);
  box-shadow: 0 4px 16px rgba(4, 8, 20, 0.38);
  transition: transform 0.25s ease, background-color 0.3s ease, color 0.3s ease;
}

.audio-toggle:active {
  transform: scale(0.94);
}

.audio-icon {
  width: 18px;
  height: 18px;
}

.audio-toggle .audio-icon--off {
  display: none;
}

.audio-toggle.is-muted .audio-icon--on {
  display: none;
}

.audio-toggle.is-muted .audio-icon--off {
  display: block;
  opacity: 0.62;
}

.audio-toggle.is-playing {
  color: #FFF7E8;
  border-color: rgba(238, 218, 184, 0.55);
}

.audio-volume-popover {
  display: flex;
  align-items: center;
  height: 38px;
  padding: 0 12px;
  border-radius: 999px;
  background: rgba(16, 26, 52, 0.84);
  border: 1px solid rgba(230, 211, 179, 0.32);
  box-shadow: 0 8px 22px rgba(4, 8, 20, 0.45);
  opacity: 0;
  transform: translateX(8px) scale(0.95);
  pointer-events: none;
  transition: opacity 0.28s ease, transform 0.28s ease;
}

.audio-control.is-volume-open .audio-volume-popover {
  opacity: 1;
  transform: translateX(0) scale(1);
  pointer-events: auto;
}

.audio-volume-slider {
  width: 82px;
  height: 4px;
  accent-color: var(--champagne-deep);
  cursor: pointer;
}

/* Full-resource preload curtain over live starry night backdrop */
.preload-overlay {
  position: fixed;
  inset: 0;
  z-index: 95;
  display: grid;
  place-items: center;
  padding: calc(24px + var(--safe-top)) calc(24px + var(--safe-right))
    calc(28px + var(--safe-bottom)) calc(24px + var(--safe-left));
  background: radial-gradient(
    ellipse at 50% 45%,
    rgba(14, 24, 50, 0.56) 0%,
    rgba(8, 14, 30, 0.82) 100%
  );
  opacity: 1;
  transition: opacity 0.85s cubic-bezier(0.22, 1, 0.36, 1), visibility 0.85s ease;
}

.preload-overlay.is-done {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.preload-card {
  width: min(84vw, 320px);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.preload-star {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-bottom: 22px;
  background: #FFF6E5;
  box-shadow:
    0 0 12px 3px rgba(252, 232, 196, 0.7),
    0 0 28px 8px rgba(176, 198, 244, 0.32);
  animation: preloadStarPulse 2.4s ease-in-out infinite;
}

@keyframes preloadStarPulse {
  0%, 100% {
    transform: scale(0.92);
    opacity: 0.72;
  }
  50% {
    transform: scale(1.15);
    opacity: 1;
  }
}

.preload-status {
  margin: 0 0 14px;
  font-size: 14px;
  letter-spacing: 0.14em;
  color: rgba(248, 240, 224, 0.92);
}

.preload-bar-track {
  width: 100%;
  height: 4px;
  border-radius: 999px;
  background: rgba(236, 224, 200, 0.16);
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.35);
}

.preload-bar-fill {
  width: 100%;
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, #D9C39E 0%, #F7E9CF 65%, #FFF9EE 100%);
  box-shadow: 0 0 10px rgba(248, 228, 192, 0.65);
  transform-origin: left center;
  transform: scaleX(0);
  transition: transform 0.22s ease-out;
  will-change: transform;
}

.preload-percent {
  margin: 12px 0 0;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.16em;
  color: rgba(238, 222, 194, 0.86);
}

.preload-tip {
  margin: 22px 0 0;
  font-size: 12px;
  line-height: 1.85;
  letter-spacing: 0.08em;
  color: rgba(214, 220, 236, 0.68);
}

/* Reduced motion accessibility */
@media (prefers-reduced-motion: reduce) {
  .atmosphere-grain {
    display: none;
  }
}
