/* Moonleaf Café — main styles
   Canvas owns the world; DOM owns all text/UI (doc 08 §3.1).
   Colors mirror src/render/palette.ts. */

:root {
  --color-bg: #1a1620;        /* Palette.bgDeep */
  --color-text: #f5eede;      /* Palette.textPrimary */
  --color-text-muted: #a89fab;/* Palette.textMuted */
  --color-accent: #d4a574;    /* Palette.accentWarm */
  --color-panel-border: #3d344a; /* Palette.bgPanelBorder */
  --color-star: #ffd43b;      /* Palette.accentStar */
  --font-ui: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-heading: Georgia, 'Times New Roman', serif;
}

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

html,
body {
  height: 100%;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-ui);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* The game fills the viewport; no page scroll. */
body {
  overflow: hidden;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px;
}

/* ---- Game canvas (backbuffer 480×270, integer-scaled via transform) ---- */

#game-wrapper {
  position: relative;
  flex: none;
  box-shadow: 0 0 0 1px var(--color-panel-border), 0 10px 40px rgba(0, 0, 0, 0.45);
  border-radius: 2px;
}

#game-canvas {
  display: block;
  image-rendering: pixelated;
  transform-origin: top left;
  background: var(--color-bg);
}

/* ---- Title screen (doc 05 §1) ---- */

#title-screen {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2rem;
  background: var(--color-bg);
  cursor: pointer;
}

#title-screen.hidden {
  display: none;
}

/* Snowfall canvas sits BEHIND the DOM title content (juice item 9). */
#title-snow {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  pointer-events: none;
  image-rendering: pixelated;
  opacity: 0.85;
}

/* Title content needs its own stacking context so z-index:-1 snow stays put. */
#title-screen > *:not(#title-snow) {
  position: relative;
  z-index: 1;
}

#title-screen.title-fade {
  opacity: 0;
  transition: opacity 0.35s ease;
}

.title-logo {
  font-family: var(--font-heading);
  font-size: clamp(2.5rem, 8vw, 4.5rem);
  font-weight: 600;
  letter-spacing: 0.04em;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

.title-prompt {
  font-size: clamp(0.9rem, 2vw, 1.05rem);
  color: var(--color-text-muted);
  animation: title-pulse 2.5s ease-in-out infinite;
}

@keyframes title-pulse {
  0%,
  100% {
    opacity: 0.45;
  }
  50% {
    opacity: 1;
  }
}

/* ---- HUD top bar (doc 05 §2) ---- */

#hud {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 6px 10px;
  background: rgba(26, 22, 32, 0.92);
  border: 1px solid var(--color-panel-border);
  border-radius: 4px;
  font-size: 0.85rem;
}

#hud.visible {
  display: flex;
}

.hud-left {
  display: flex;
  align-items: center;
  gap: 1.1rem;
  min-width: 0;
}

.hud-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

#hud-day {
  font-weight: 600;
  white-space: nowrap;
}

#hud-coins {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#hud-stars {
  color: var(--color-star);
  letter-spacing: 2px;
  white-space: nowrap;
}

.hud-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 28px;
  background: transparent;
  border: 1px solid var(--color-panel-border);
  border-radius: 3px;
  color: var(--color-text-muted);
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease;
}

.hud-btn:hover {
  border-color: var(--color-accent);
  color: var(--color-accent);
}

.hud-btn svg {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---- Accessibility ---- */

/*
 * Focus ring is ALWAYS visible (doc 05 §6). The base rule uses :focus-visible;
 * the fallback below guarantees a ring even for browsers/heuristics that never
 * match :focus-visible — `outline: none` appears nowhere in this stylesheet.
 */
:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

:focus {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

/* Text size (doc 05 §6): the setting writes font-size on <html>; rem-based
   panel typography scales with it. Canvas world unaffected. */
html {
  font-size: 100%; /* 100% | 125% | 150% via settings */
}

.settings-version {
  opacity: 0.75;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Reduced motion: kill the pulse and the title fade (doc 05 §6). */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .title-prompt {
    animation: none;
    opacity: 0.75;
  }

  /* The in-game reduced-motion toggle must match the OS behavior — the class
     is applied on <html> by the settings/boot code (doc 05 §6). */
  html.reduced-motion *,
  html.reduced-motion *::before,
  html.reduced-motion *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  #title-snow {
    display: none;
  }
}

/* In-game reduced-motion toggle (works regardless of OS preference). */
html.reduced-motion .title-prompt {
  animation: none;
  opacity: 0.75;
}

html.reduced-motion .wiggle {
  animation: none;
}

html.reduced-motion .page-turn {
  animation: none;
}

/* ---- Shared buttons / rows (doc 05 §4: hover + pressed + focus states) ---- */

.btn {
  font-family: var(--font-ui);
  font-size: 0.9rem;
  padding: 7px 14px;
  border-radius: 4px;
  border: 1px solid var(--color-panel-border);
  background: #241f30;
  color: var(--color-text);
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, transform 0.06s ease;
}

.btn:hover:not(:disabled) {
  border-color: var(--color-accent);
  color: var(--color-accent);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  border-color: var(--color-accent);
}

.btn-secondary {
  color: var(--color-text-muted);
}

.btn-danger {
  border-color: #a05656;
  color: #d98c8c;
}

.btn-row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

/* ---- Overlays / panels (modal layer per doc 05 §1) ---- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 8, 14, 0.72);
  padding: 16px;
}

/* The evening-market shop opens FROM the recap (doc 05 §3.3) and must always
   paint in front of it. Both share .overlay's z-index:40, but the recap node is
   recreated and re-appended to #app every day while the shop node persists from
   its first open — so from day 2 the younger recap node wins the paint order.
   Give the shop its own higher layer so it sits above the recap everywhere. */
#shop-overlay {
  z-index: 60;
}

/*
 * Cozy overlay enter (docs/11): soft backdrop fade + a gentle panel settle,
 * never a hard cut. Applied via the `.overlay-anim` class (toggled by
 * playOverlayEnter() in ui/overlay-anim.ts) so it replays reliably even for
 * overlays whose node is reused (kettle/journal/shop). Recreated overlays
 * (mailbox/letter/scene/recap/ending/settings) also call it on show. Reduced
 * motion collapses every animation to instant via the rules at the bottom.
 */
.overlay-anim {
  animation: overlay-in 0.22s ease-out both;
}

.overlay-anim .panel {
  animation: panel-in 0.26s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes overlay-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

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

.overlay.hidden,
.hidden {
  display: none !important;
}

.panel {
  position: relative;
  width: min(560px, 92vw);
  max-height: 86vh;
  overflow-y: auto;
  background: var(--color-bg);
  border: 1px solid var(--color-panel-border);
  border-radius: 6px;
  padding: 22px 24px;
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.55);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel h2 {
  font-family: var(--font-heading);
  font-weight: 600;
  letter-spacing: 0.02em;
}

.panel h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  margin-top: 4px;
}

.panel-close {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 28px;
  height: 28px;
  border: 1px solid transparent;
  border-radius: 3px;
  background: transparent;
  color: var(--color-text-muted);
  font-size: 1.15rem;
  cursor: pointer;
}

.panel-close:hover {
  border-color: var(--color-accent);
  color: var(--color-accent);
}

.note {
  font-size: 0.82rem;
  color: var(--color-text-muted);
}

.note-warn {
  color: #d9b98c;
}

.note-inline {
  min-height: 1.2em;
}

/* ---- Letter overlay (tutorial step 1) ---- */

.letter-panel .letter-body {
  white-space: pre-wrap;
  line-height: 1.65;
  font-family: var(--font-heading);
  font-size: 0.95rem;
}

/* ---- Morning mailbox (Batch 3) — calm, cozy, reuses panel language ---- */

.mailbox-panel {
  gap: 10px;
}

.mailbox-content {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.mailbox-letter {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.mailbox-sender {
  font-family: var(--font-heading);
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  margin: 0;
}

.mailbox-day {
  margin: 0;
}

.mailbox-subject {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  margin: 2px 0 0;
}

.mailbox-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.mailbox-list-item {
  width: 100%;
  text-align: left;
  justify-content: flex-start;
}

.mailbox-list-item:disabled {
  opacity: 0.5;
  cursor: default;
}

/* ---- Morning banner (prep phase, doc 02 §1) ---- */

.banner {
  position: fixed;
  left: 50%;
  top: 14%;
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 18px 26px;
  background: rgba(26, 22, 32, 0.94);
  border: 1px solid var(--color-panel-border);
  border-radius: 6px;
  text-align: center;
  max-width: min(520px, 92vw);
}

.banner-title {
  font-family: var(--font-heading);
  font-size: 1.25rem;
  font-weight: 600;
}

.shelf-line {
  font-variant-numeric: tabular-nums;
}

/* ---- Action bar (kettle button etc., bottom strip) ---- */

.action-bar {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  gap: 10px;
}

.action-bar.hidden {
  display: none;
}

/* ---- Toast (calm inline feedback, doc 05 §5) ---- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 74px;
  transform: translateX(-50%);
  z-index: 35;
  max-width: min(480px, 90vw);
  padding: 9px 16px;
  background: rgba(26, 22, 32, 0.95);
  border: 1px solid var(--color-panel-border);
  border-radius: 4px;
  font-size: 0.88rem;
  text-align: center;
  /* Calm entrance (docs/11): ease up + in instead of popping. Reduced motion
     collapses to an instant appear; the `.hidden` toggle still controls removal. */
  animation: toast-in 0.24s ease-out both;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* ---- Kettle panel chips ---- */

.option-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.chip {
  font-family: var(--font-ui);
  font-size: 0.85rem;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--color-panel-border);
  background: #241f30;
  color: var(--color-text-muted);
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease;
}

.chip:hover:not(:disabled) {
  border-color: var(--color-accent);
  color: var(--color-accent);
}

.chip-selected {
  border-color: var(--color-accent);
  color: var(--color-bg);
  background: var(--color-accent);
}

.chip:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Known-recipe chips in the kettle: a quiet reference list, visually
   distinct from the active toggle chips (no selected-state fill). */
.chip-recipe {
  background: #1e1a28;
  color: var(--color-text-soft);
  border-style: dashed;
}

/* ---- Recap modal (count-up coins) ---- */

.recap-coins-label {
  font-size: 0.85rem;
  color: var(--color-text-muted);
}

.recap-coins {
  font-family: var(--font-heading);
  font-size: 2.2rem;
  font-weight: 600;
  color: var(--color-star);
  font-variant-numeric: tabular-nums;
}

.recap-discoveries {
  margin-left: 1.1em;
}

.recap-saved::before {
  content: '✒ ';
}

/* ---- Settings: save export/import (doc 05 §3.4) ---- */

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
}

.code-box {
  width: 100%;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.78rem;
  line-height: 1.45;
  word-break: break-all; /* long codes wrap, never truncate visually */
  background: #16121e;
  color: var(--color-text);
  border: 1px solid var(--color-panel-border);
  border-radius: 4px;
  padding: 8px;
}

.confirm-wrap {
  border-top: 1px solid var(--color-panel-border);
  padding-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ---- Journal (doc 02 §6: four tabs) ---- */

.journal-panel {
  width: min(640px, 94vw);
}

.journal-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 12px;
  border: 1px solid var(--color-panel-border);
  border-radius: 4px;
  background: #201b2c;
}

.journal-card-title {
  font-weight: 600;
  font-size: 0.95rem;
}

.journal-regular {
  display: grid;
  grid-template-columns: 56px 1fr;
  gap: 12px;
  align-items: start;
}

.journal-portrait {
  width: 48px;
  height: 48px;
  image-rendering: pixelated;
  border: 1px solid var(--color-panel-border);
  border-radius: 3px;
  overflow: hidden;
}

.journal-portrait img {
  width: 100%;
  height: 100%;
  display: block;
}

.journal-hearts {
  color: var(--color-star);
  letter-spacing: 2px;
  font-size: 0.9rem;
}

/* New-discovery card: highlight ring + page-turn entrance (juice item 5).
   Reduced motion keeps the static highlight, drops the animation. */
.journal-card-new {
  border-color: var(--color-accent);
  box-shadow: inset 0 0 0 1px var(--color-accent);
}

@keyframes page-turn {
  0% {
    transform: perspective(600px) rotateY(-14deg);
    opacity: 0.4;
  }
  100% {
    transform: perspective(600px) rotateY(0deg);
    opacity: 1;
  }
}

.page-turn {
  animation: page-turn 0.45s ease-out;
}

/* ---- Shop (doc 05 §3.3 recap shop button → evening market) ---- */

.shop-section-heading {
  margin-top: 6px;
}

.shop-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid #2a2438;
}

.shop-row-text {
  flex: 1;
  min-width: 0;
}

.shop-price {
  font-variant-numeric: tabular-nums;
  color: var(--color-star);
  white-space: nowrap;
}

/* Insufficient funds: price reads muted, button wiggles (doc 05 §5). */
.price-muted {
  color: var(--color-text-muted);
}

.shop-owned-badge {
  color: var(--color-accent);
  font-size: 0.82rem;
  white-space: nowrap;
}

.shop-row-note {
  min-width: 110px;
  max-width: 170px;
  text-align: right;
}

.shop-close-btn {
  align-self: flex-end;
  margin-top: 8px;
}

@keyframes shop-wiggle {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-3px);
  }
  75% {
    transform: translateX(3px);
  }
}

.wiggle {
  animation: shop-wiggle 0.18s ease-in-out 2;
}

@media (prefers-reduced-motion: reduce) {
  .wiggle {
    animation: none;
  }
}
