/* ===================================================================
   WISHING WELL — 16-bit RPG Dialog Box UI
   Follows DESIGN.md: pixel art diorama, stepped motion, no anti-patterns.
   Canvas agent draws the well — this file styles UI chrome only.
   =================================================================== */

/* --- DESIGN.md Color Tokens (Section 4) --- */
:root {
  /* Stone — well walls, rim */
  --stone-1: #2b2440;
  --stone-2: #4a3f6b;
  --stone-3: #6f6291;
  --stone-4: #9b8fb8;
  --stone-5: #c7bfe0;

  /* Water — well interior */
  --water-1: #0c2d48;
  --water-2: #17587a;
  --water-3: #2f9bb5;
  --water-4: #6fd6d6;

  /* Gold — coins, magic sparkle */
  --gold-1: #6b4a1a;
  --gold-2: #b3811f;
  --gold-3: #e8b33d;
  --gold-4: #ffe27a;

  /* Foliage — moss, grass, vines */
  --moss-1: #1e3b24;
  --moss-2: #356b3a;
  --moss-3: #5c9c4f;
  --moss-4: #a3d66b;

  /* Sky / atmosphere */
  --sky-1: #1a1a3a;
  --sky-2: #3a3a6b;
  --sky-3: #7a5a9b;
  --sky-4: #e8a87c;

  /* UI ink */
  --ink-1: #1a1428;
  --ink-2: #4a3f6b;
  --paper: #f4ecd8;

  /* Typography (Section 5) */
  --font-pixel-display: 'Press Start 2P', monospace;
  --font-pixel-body: 'VT323', monospace;

  /* Scroll progress — driven by js/scroll.js */
  --scroll-progress: 0;
}

/* --- Reset --- */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

[hidden] { display: none !important; }

/* --- Base (Section 5: pixel font everywhere, no sans-serif) --- */
body {
  font-family: var(--font-pixel-body);
  font-size: 16px;
  color: var(--paper);
  background: var(--sky-1);
  overflow-x: hidden;
  image-rendering: pixelated;
  -webkit-font-smoothing: none;
  -moz-osx-font-smoothing: unset;
}

.app {
  position: relative;
}

/* --- Sky Layer --- */
.sky-layer {
  position: fixed;
  inset: 0;
  background: var(--sky-1);
  z-index: -1;
}

/* --- Top UI (Section 5: pixel font, fixed grid sizes) --- */
.top-ui {
  position: absolute;
  z-index: 20;
  width: 100%;
  text-align: center;
  padding: 16px 8px 0;
  pointer-events: none;
}

.top-ui h1 {
  margin: 0;
  font-family: var(--font-pixel-display);
  font-size: 24px;
  letter-spacing: 0.05em;
  color: var(--gold-3);
  text-shadow: 2px 2px 0 var(--ink-1);
}

.tagline {
  margin: 8px 0 0;
  font-family: var(--font-pixel-body);
  font-size: 16px;
  color: var(--stone-4);
  letter-spacing: 0.05em;
}

/* --- Diorama Container (canvas agent draws the well) --- */
.diorama {
  position: relative;
  width: min(90vw, 560px);
  aspect-ratio: 1 / 1;
  image-rendering: pixelated;
  margin: 0 auto;
}

/* --- Well Canvas — hero sprite (full-size) --- */
.well-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

/* --- Overlay Canvases — effects on top of well --- */
.overlay-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  pointer-events: none;
}

.overlay-water {
  z-index: 1;
  pointer-events: auto;
}

.overlay-dust {
  opacity: 0.6;
  mix-blend-mode: screen;
  z-index: 2;
}

.overlay-sparkle {
  mix-blend-mode: screen;
  z-index: 3;
}

/* --- Scroll-driven layer parallax --- */
.diorama {
  transform: translateY(calc(var(--scroll-progress) * -16px));
  transition: transform 0.1s linear;
}

/* --- Foreground Props (Section 10: lanterns, grass, signpost) --- */
.foreground-props {
  position: absolute;
  bottom: 10%;
  left: 0;
  right: 0;
  height: 80px;
  pointer-events: none;
  z-index: 5;
}

.prop {
  position: absolute;
  image-rendering: pixelated;
}

/* Lantern — pixel art via box-shadow */
.prop-lantern {
  width: 8px;
  height: 16px;
  background: var(--gold-2);
  box-shadow:
    -4px 0 0 var(--gold-1),
    4px 0 0 var(--gold-1),
    0 -4px 0 var(--stone-3),
    0 4px 0 var(--stone-2);
  bottom: 0;
  left: 15%;
  animation: lanternGlow 2s steps(3) infinite;
}

@keyframes lanternGlow {
  0% { background: var(--gold-3); }
  33% { background: var(--gold-4); }
  66% { background: var(--gold-2); }
  100% { background: var(--gold-3); }
}

/* Grass tufts */
.prop-grass {
  width: 12px;
  height: 8px;
  background: var(--moss-3);
  box-shadow:
    -4px -4px 0 var(--moss-2),
    4px -2px 0 var(--moss-4),
    0 -6px 0 var(--moss-3);
  bottom: 0;
  clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}

.prop-grass-1 { left: 25%; }
.prop-grass-2 { left: 60%; width: 8px; height: 6px; }
.prop-grass-3 { right: 20%; width: 10px; }

/* Signpost */
.prop-sign {
  width: 4px;
  height: 24px;
  background: var(--stone-2);
  bottom: 0;
  right: 18%;
}

.prop-sign::before {
  content: '';
  position: absolute;
  top: 0;
  left: -8px;
  width: 20px;
  height: 10px;
  background: var(--stone-4);
  box-shadow: 2px 2px 0 var(--stone-1);
}

/* --- FAB Button (Section 8: pixel-bordered rectangle) --- */
.wish-fab {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 30;
  border: 4px solid var(--ink-1);
  background: var(--paper);
  color: var(--ink-1);
  font-family: var(--font-pixel-display);
  font-size: 8px;
  padding: 8px 16px;
  cursor: pointer;
  box-shadow: 4px 4px 0 var(--ink-1);
  image-rendering: pixelated;
  transition: none;
  letter-spacing: 0.05em;
}

.wish-fab:hover {
  background: var(--gold-3);
  color: var(--ink-1);
}

.wish-fab:active {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 var(--ink-1);
}

.wish-fab:focus-visible {
  outline: 4px solid var(--gold-4);
  outline-offset: 4px;
}

/* --- Modal Backdrop (Section 8: no backdrop-filter blur) --- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: rgba(26, 20, 40, 0.85);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.2s steps(4), visibility 0.2s steps(4);
}

.modal-backdrop.backdrop-visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

/* --- Modal / RPG Dialog Box (Section 8) --- */
.modal,
.rpg-dialog {
  position: relative;
  width: min(90vw, 480px);
  padding: 16px;
  background: var(--paper);
  color: var(--ink-1);
  border: 4px solid var(--ink-1);
  box-shadow: 8px 8px 0 var(--ink-1);
  image-rendering: pixelated;
  transform: scale(0.8);
  opacity: 0;
  transition: transform 0.15s steps(4), opacity 0.15s steps(4);
}

.backdrop-visible .modal,
.backdrop-visible .rpg-dialog {
  transform: scale(1);
  opacity: 1;
}

.modal h2,
.dialog-title {
  margin: 0 0 16px;
  font-family: var(--font-pixel-display);
  font-size: 16px;
  color: var(--ink-1);
  letter-spacing: 0.05em;
}

.modal-close,
.dialog-close {
  position: absolute;
  right: 8px;
  top: 8px;
  width: 24px;
  height: 24px;
  border: 4px solid var(--ink-1);
  background: var(--stone-4);
  color: var(--ink-1);
  font-family: var(--font-pixel-display);
  font-size: 8px;
  cursor: pointer;
  box-shadow: 2px 2px 0 var(--ink-1);
}

.modal-close:hover,
.dialog-close:hover {
  background: var(--gold-3);
}

.modal-close:active,
.dialog-close:active {
  transform: translate(1px, 1px);
  box-shadow: 1px 1px 0 var(--ink-1);
}

.modal-close:focus-visible,
.dialog-close:focus-visible {
  outline: 4px solid var(--gold-4);
  outline-offset: 2px;
}

/* --- Textarea (Section 8: parchment inset, block cursor) --- */
textarea {
  width: 100%;
  min-height: 128px;
  resize: vertical;
  border: 4px solid var(--ink-1);
  background: var(--paper);
  color: var(--ink-1);
  font-family: var(--font-pixel-body);
  font-size: 16px;
  padding: 8px;
  box-shadow: inset 4px 4px 0 var(--stone-4);
  image-rendering: pixelated;
  caret-color: var(--ink-1);
}

textarea:focus {
  outline: 4px solid var(--gold-4);
  outline-offset: 4px;
}

/* --- Dialog body/footer/input (RPG dialog structure) --- */
.dialog-body {
  margin: 0;
}

.dialog-footer {
  margin-top: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.dialog-input {
  width: 100%;
  min-height: 128px;
  resize: vertical;
  border: 4px solid var(--ink-1);
  background: var(--paper);
  color: var(--ink-1);
  font-family: var(--font-pixel-body);
  font-size: 16px;
  padding: 8px;
  box-shadow: inset 4px 4px 0 var(--stone-4);
  image-rendering: pixelated;
  caret-color: var(--ink-1);
}

.dialog-input:focus {
  outline: 4px solid var(--gold-4);
  outline-offset: 4px;
}

.dialog-wish-text {
  font-family: var(--font-pixel-body);
  font-size: 16px;
  line-height: 1.5;
  color: var(--ink-1);
  text-align: center;
}

.rpg-dialog--discover {
  text-align: center;
}

/* --- Form Row --- */
.form-row {
  margin-top: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.char-count {
  font-family: var(--font-pixel-body);
  font-size: 16px;
  color: var(--stone-4);
}

/* --- Primary Button (Section 8: pixel-bordered rectangle) --- */
.btn-primary {
  border: 4px solid var(--ink-1);
  background: var(--gold-3);
  color: var(--ink-1);
  font-family: var(--font-pixel-display);
  font-size: 8px;
  padding: 12px 20px;
  cursor: pointer;
  box-shadow: 4px 4px 0 var(--ink-1);
  image-rendering: pixelated;
  letter-spacing: 0.05em;
  transition: none;
}

.btn-primary:hover {
  background: var(--gold-4);
}

.btn-primary:active {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 var(--ink-1);
}

.btn-primary:focus-visible {
  outline: 4px solid var(--gold-4);
  outline-offset: 4px;
}

/* --- Toast (Section 8: pixel-bordered, hard shadow) --- */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--paper);
  color: var(--ink-1);
  border: 4px solid var(--ink-1);
  box-shadow: 4px 4px 0 var(--ink-1);
  padding: 12px 20px;
  font-family: var(--font-pixel-body);
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 45;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.15s steps(4), transform 0.15s steps(4), visibility 0.15s steps(4);
}

.toast.show {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) translateY(0);
}

.toast-icon {
  font-size: 16px;
}

/* --- Coin Toss Animation (Section 9: stepped) --- */
.toss-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 35;
}

.toss-coin {
  position: absolute;
  width: 16px;
  height: 16px;
  background: var(--gold-3);
  border: 2px solid var(--gold-1);
  box-shadow: 2px 2px 0 var(--gold-1);
  opacity: 0;
  transform: translate(-50%, -50%) scale(0);
}

.toss-coin.active {
  animation: tossDrop 0.8s steps(6) forwards;
}

@keyframes tossDrop {
  0% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  16% { opacity: 1; transform: translate(-50%, 5vh) scale(1); }
  33% { opacity: 1; transform: translate(-50%, 12vh) scale(0.9); }
  50% { opacity: 1; transform: translate(-50%, 20vh) scale(0.8); }
  66% { opacity: 1; transform: translate(-50%, 30vh) scale(0.7); }
  83% { opacity: 0.5; transform: translate(-50%, 40vh) scale(0.5); }
  100% { opacity: 0; transform: translate(-50%, 50vh) scale(0.3); }
}

.toss-ripple {
  position: absolute;
  width: 48px;
  height: 48px;
  border: 4px solid var(--water-4);
  opacity: 0;
  transform: translate(-50%, -50%) scale(0);
}

.toss-ripple.active {
  animation: rippleExpand 0.6s steps(4) forwards;
}

@keyframes rippleExpand {
  0% { opacity: 1; transform: translate(-50%, -50%) scale(0); }
  33% { opacity: 0.8; transform: translate(-50%, -50%) scale(1.5); }
  66% { opacity: 0.4; transform: translate(-50%, -50%) scale(2.5); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(3); }
}

/* --- Discover Modal --- */
.modal-discover {
  text-align: center;
}

.modal-discover p {
  font-family: var(--font-pixel-body);
  font-size: 16px;
  line-height: 1.5;
  color: var(--ink-1);
}

/* --- Visually Hidden (accessibility) --- */
.visually-hidden {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

/* --- Mobile (Section 10: crop sky first, never crop well) --- */
@media (max-width: 700px) {
  .top-ui {
    padding: 12px 8px 0;
  }

  .top-ui h1 {
    font-size: 16px;
  }

  .tagline {
    font-size: 16px;
  }

  .diorama {
    width: min(95vw, 400px);
  }

  .wish-fab {
    right: 8px;
    bottom: 8px;
    padding: 8px 12px;
    font-size: 8px;
  }

  .modal {
    width: min(95vw, 400px);
    padding: 12px;
  }

  .modal h2 {
    font-size: 16px;
  }

  textarea {
    min-height: 96px;
    font-size: 16px;
  }

  .btn-primary {
    padding: 8px 16px;
    font-size: 8px;
  }

  .toast {
    font-size: 16px;
    padding: 8px 16px;
  }
}

/* --- Reduced Motion (Section 9, 12) --- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .prop-lantern {
    animation: none;
    box-shadow: 0 0 8px var(--gold-4);
  }

  .toss-coin.active {
    animation: none;
    opacity: 0;
  }

  .toss-ripple.active {
    animation: none;
    opacity: 0;
  }
}
