@font-face {
  font-family: 'Fredoka';
  src: url('/fonts/fredoka.woff2') format('woff2');
  font-weight: 400 700;
  font-display: swap;
}

:root {
  --rail-green: #1e6b4f;
  --rail-green-deep: #164f3b;
  --loco-yellow: #ffd23f;
  --wagon-red: #e8483b;
  --night: #252b61;
  --night-deep: #1a1f4a;
  --moonlight: #f7e6a6;
  --cream: #fff7e4;
  --ease: cubic-bezier(.65, 0, .35, 1);
}

[hidden] { display: none !important; }

* { box-sizing: border-box; }

html {
  font-family: 'Fredoka', system-ui, sans-serif;
  font-synthesis: none;
  color: var(--cream);
  background: var(--night-deep);
}

body { margin: 0; min-height: 100vh; }

button, a { font: inherit; color: inherit; }

#app {
  --bg: var(--rail-green);
  --bg-deep: var(--rail-green-deep);
  --accent: var(--loco-yellow);
  min-height: 100vh;
  background: radial-gradient(ellipse at 50% -20%, color-mix(in srgb, var(--bg) 70%, white) 0%, var(--bg) 45%, var(--bg-deep) 100%);
  background-color: var(--bg);
}

#app[data-phase="2"] {
  --bg: var(--night);
  --bg-deep: var(--night-deep);
  --accent: var(--moonlight);
}

/* El fondo pasa de verde a índigo en el cambio de fase: se anima con una capa encima. */
#app::before {
  content: '';
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at 50% -20%, #3a4a9a 0%, var(--night) 45%, var(--night-deep) 100%);
  opacity: 0;
  pointer-events: none;
  transition: opacity 1.4s var(--ease);
  z-index: 0;
}
#app[data-phase="2"]::before { opacity: 1; }

.screen { position: relative; z-index: 1; }

:focus-visible { outline: 4px solid var(--accent); outline-offset: 4px; }

/* ---------- Grilla ---------- */

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 28px 64px 8px;
}

.emblem { position: relative; width: 240px; height: 150px; }
.emblem svg { position: absolute; inset: 0; width: 100%; height: 100%; transition: transform 1.2s var(--ease), opacity .9s var(--ease); }

.rail { fill: var(--rail-green-deep); }
.train-red { fill: var(--wagon-red); }
.train-yellow { fill: var(--loco-yellow); }
.train-window { fill: #bfe6f2; }
.wheel { fill: #2b2b2b; }
.hub { fill: var(--loco-yellow); }
.puff { fill: var(--cream); opacity: .9; }

.moon { fill: var(--moonlight); }
.star { fill: var(--moonlight); }

.emblem-moon { opacity: 0; transform: translateY(60px); }
#app[data-phase="2"] .emblem-train { opacity: 0; transform: translateX(-420px); }
#app[data-phase="2"] .emblem-moon { opacity: 1; transform: none; }

.top-actions { display: flex; gap: 14px; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  border: 0;
  border-radius: 50%;
  background: color-mix(in srgb, var(--cream) 10%, transparent);
  cursor: pointer;
  opacity: .55;
  transition: opacity .2s;
}
.icon-btn:hover { opacity: 1; }
.icon-btn svg, .bar-btn svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 44px 40px;
  padding: 24px 64px 80px;
}

.tile {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}

.tile-thumb {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: 28px;
  overflow: hidden;
  background: var(--bg-deep);
  box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 0%, transparent), 0 14px 0 -4px rgb(0 0 0 / .22);
  transition: transform .18s var(--ease), box-shadow .18s var(--ease);
}
.tile-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile:hover .tile-thumb, .tile:focus-visible .tile-thumb {
  transform: translateY(-4px);
  box-shadow: 0 0 0 6px var(--accent), 0 18px 0 -4px rgb(0 0 0 / .25);
}
.tile:focus-visible { outline: none; }

.tile-meta { display: flex; gap: 12px; align-items: baseline; padding: 0 8px; font-size: 17px; line-height: 1.3; }
.tile-title {
  flex: 1;
  opacity: .82;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.tile-duration { font-weight: 600; color: var(--accent); font-variant-numeric: tabular-nums; }

.grid-end { height: 1px; }

.empty { max-width: 34em; margin: 80px auto; padding: 0 32px; font-size: 22px; text-align: center; opacity: .8; }

/* ---------- Reproductor ---------- */

#player-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: #000;
  z-index: 5;
}

.player-bar {
  display: flex;
  align-items: center;
  gap: 20px;
  height: 72px;
  padding: 0 20px;
  background: var(--bg-deep);
}
#app[data-phase="2"] .player-bar { background: var(--night-deep); }

.bar-btn {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  height: 52px;
  padding: 0 22px;
  border: 0;
  border-radius: 26px;
  background: color-mix(in srgb, var(--cream) 12%, transparent);
  font-size: 20px;
  font-weight: 500;
  cursor: pointer;
}
.bar-btn:hover { background: color-mix(in srgb, var(--cream) 22%, transparent); }
.bar-btn:disabled { opacity: .35; cursor: default; background: color-mix(in srgb, var(--cream) 12%, transparent); }
.bar-btn.fs-toggle { padding: 0 14px; }

.now-title { flex: 1; font-size: 18px; opacity: .7; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.player-frame { flex: 1; position: relative; }
.player-frame iframe, .player-frame #player { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }

/* ---------- Siguiente ---------- */

#next-screen {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: 1fr auto auto;
  justify-items: center;
  gap: 28px;
  padding: 48px 64px 40px;
  z-index: 4;
  background: radial-gradient(ellipse at 50% -20%, #3a4a9a 0%, var(--night) 45%, var(--night-deep) 100%);
}

.next-main { display: flex; flex-direction: column; align-items: center; gap: 18px; min-height: 0; }

.next-card {
  position: relative;
  height: min(54vh, 560px);
  aspect-ratio: 16 / 9;
  padding: 0;
  border: 0;
  border-radius: 36px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: 0 0 0 8px var(--moonlight), 0 22px 0 -4px rgb(0 0 0 / .3);
  background: var(--night-deep);
}
.next-thumb { position: absolute; inset: 0; background-size: cover; background-position: center; }

.next-play {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 168px;
  height: 168px;
  transform: translate(-50%, -50%);
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgb(26 31 74 / .72);
}
.next-play .play { width: 64px; height: 64px; fill: var(--moonlight); margin-left: 6px; }
.next-play .ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-track { fill: none; stroke: rgb(247 230 166 / .2); stroke-width: 8; }
.ring-progress { fill: none; stroke: var(--moonlight); stroke-width: 8; stroke-linecap: round; stroke-dasharray: 326.7; stroke-dashoffset: 0; }
.countdown { position: absolute; bottom: 22px; font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }

.next-title { margin: 0; max-width: 40em; font-size: 22px; text-align: center; opacity: .85; }

.alternatives { display: grid; grid-template-columns: repeat(4, 300px); gap: 28px; }
.alternatives .tile-thumb { border-radius: 20px; }
.alternatives .tile-meta { font-size: 15px; }

.cancel { justify-self: center; }

@media (max-width: 1200px) {
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); padding-inline: 32px; }
  .top { padding-inline: 32px; }
  .alternatives { grid-template-columns: repeat(2, 260px); }
}
@media (max-width: 640px) {
  .grid { grid-template-columns: 1fr; }
  .emblem { width: 160px; height: 100px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before { transition-duration: 0s !important; animation-duration: 0s !important; }
}
