/* Ending cinematic overlay (world-ending.js): letterbox bars, title card,
   project-logo montage, stats card and credits over Unity's orbiting camera. */

.world-ending {
  --bar: clamp(44px, 11vh, 112px);
  position: absolute;
  inset: 0;
  z-index: 8;
  color: #fffaf0;
  font-family: Inter, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", sans-serif;
  cursor: default;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}
.world-ending[hidden] {
  display: none;
}
.world-ending.can-advance {
  cursor: pointer;
}

/* Hide the game chrome while the cinematic runs. */
#game.in-ending .world-toolbar,
#game.in-ending .world-hud,
#game.in-ending .touch-controls,
#game.in-ending .minimap,
#game.in-ending #chat-bubbles,
#game.in-ending #world-toast,
#game.in-ending .pointer-hint,
#game.in-ending #damage-layer {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition:
    opacity 0.6s ease,
    visibility 0s linear 0.6s;
}

/* ---------- letterbox ---------- */
.ending-bar {
  position: absolute;
  left: 0;
  right: 0;
  height: var(--bar);
  background: #040a07;
  transition: transform 0.95s cubic-bezier(0.6, 0, 0.2, 1);
}
.ending-bar-top {
  top: 0;
  transform: translateY(-100%);
  box-shadow: 0 1px 0 #f3dc9b33;
}
.ending-bar-bottom {
  bottom: 0;
  transform: translateY(100%);
  box-shadow: 0 -1px 0 #f3dc9b33;
}
.world-ending.show .ending-bar {
  transform: none;
}
.world-ending.leaving .ending-bar {
  transition-duration: 0.8s;
}
.world-ending.leaving .ending-bar-top {
  transform: translateY(-100%);
}
.world-ending.leaving .ending-bar-bottom {
  transform: translateY(100%);
}

/* ---------- stage ---------- */
.ending-stage {
  position: absolute;
  inset: var(--bar) 0;
  display: grid;
  place-items: center;
  padding: 12px 16px;
  overflow: hidden;
}
/* Soft vignette behind the panels (not while Kapi talks: let the world show). */
.ending-stage::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at center, #06120dcc 0%, #06120d88 45%, #06120d00 78%);
  opacity: 0;
  transition: opacity 0.9s ease;
}
.world-ending[data-stage="title"] .ending-stage::before,
.world-ending[data-stage="montage"] .ending-stage::before,
.world-ending[data-stage="stats"] .ending-stage::before,
.world-ending[data-stage="credits"] .ending-stage::before {
  opacity: 1;
}
.world-ending.leaving .ending-stage::before {
  opacity: 0;
}
.ending-panel {
  grid-area: 1 / 1;
  position: relative;
  display: grid;
  justify-items: center;
  max-width: min(960px, 100%);
  text-align: center;
  opacity: 0;
  visibility: hidden;
  transform: translateY(10px);
  transition:
    opacity 0.8s ease,
    transform 0.8s ease,
    visibility 0s linear 0.8s;
}
.ending-panel.active {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition:
    opacity 0.9s ease 0.15s,
    transform 0.9s ease 0.15s,
    visibility 0s;
}
.world-ending.leaving .ending-panel.active {
  opacity: 0;
}
.ending-kicker {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #f3dc9b;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.32em;
  text-transform: uppercase;
  text-shadow: 0 1px 4px #000a;
}
.ending-kicker::before,
.ending-kicker::after {
  content: "";
  width: 6px;
  height: 6px;
  background: #f3dc9b;
  transform: rotate(45deg);
}

/* ---------- title card ---------- */
.ending-title {
  gap: 14px;
}
.ending-rule {
  position: relative;
  width: min(420px, 70vw);
  height: 1px;
  background: linear-gradient(90deg, #f3dc9b00, #f3dc9bcc 30%, #fff 50%, #f3dc9bcc 70%, #f3dc9b00);
  transform: scaleX(0);
}
.ending-rule::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 7px;
  height: 7px;
  background: #f3dc9b;
  transform: translate(-50%, -50%) rotate(45deg);
}
.ending-title.active .ending-rule {
  animation: ending-rule 1.4s cubic-bezier(0.3, 0.7, 0.2, 1) 0.3s forwards;
}
.ending-heading {
  display: grid;
  justify-items: center;
  gap: 6px;
  margin: 0;
  font-weight: 600;
}
.ending-heading-main {
  font: 600 clamp(28px, 5.6vw, 58px)/1.12 Georgia, "Yu Mincho", "Hiragino Mincho ProN", serif;
  letter-spacing: 0.06em;
  background: linear-gradient(180deg, #fffdf4 10%, #f3dc9b 60%, #c99a3c);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 2px 10px #000b);
}
.ending-heading-tail {
  position: relative;
  padding: 0 34px;
  font: italic 500 clamp(16px, 2.4vw, 24px)/1.4 Georgia, "Yu Mincho", serif;
  letter-spacing: 0.4em;
  color: #fff6dc;
  text-shadow: 0 0 14px #f3dc9b99;
}
.ending-heading-tail::before,
.ending-heading-tail::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 24px;
  height: 1px;
  background: #f3dc9bb3;
}
.ending-heading-tail::before {
  left: 0;
}
.ending-heading-tail::after {
  right: 0;
}
.ending-title.active .ending-heading-main {
  animation: ending-reveal 2.2s cubic-bezier(0.2, 0.7, 0.2, 1) 0.2s both;
}
.ending-title.active .ending-heading-tail {
  animation: ending-fade-up 1.2s ease 1.5s both;
}
.ending-title.active .ending-kicker {
  animation: ending-fade-up 1s ease 0.1s both;
}
.ending-sub {
  margin: 0;
  color: #e9e2cd;
  font-size: clamp(13px, 1.6vw, 16px);
  letter-spacing: 0.08em;
  text-shadow: 0 1px 4px #000c;
}
.ending-title.active .ending-sub {
  animation: ending-fade-up 1.2s ease 2.1s both;
}

/* ---------- logo montage ---------- */
.ending-montage {
  gap: clamp(14px, 3vh, 26px);
}
.ending-logos {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: clamp(14px, 2.6vw, 28px);
  max-width: min(720px, 92vw);
  margin: 0;
  padding: 0;
  list-style: none;
}
.ending-logos li {
  display: grid;
  justify-items: center;
  gap: 8px;
  width: clamp(70px, 12vw, 104px);
  opacity: 0;
}
.ending-montage.active .ending-logos li {
  animation: ending-logo-in 1s cubic-bezier(0.2, 1.3, 0.4, 1) calc(0.5s + var(--i) * 0.42s) both;
}
.ending-logo {
  position: relative;
  display: grid;
  place-items: center;
  width: clamp(56px, 9vw, 80px);
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 18px;
  background: #fffaf0;
  color: #10261e;
  font: 700 28px Georgia, serif;
  box-shadow:
    0 0 0 2px #10261e,
    0 0 0 3px #f3dc9bcc,
    0 8px 26px #000a,
    0 0 26px #f3dc9b44;
}
.ending-logo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
/* Gold glint sweeping across each logo once it has landed. */
.ending-logo::after {
  content: "";
  position: absolute;
  inset: -40%;
  background: linear-gradient(115deg, transparent 40%, #fff8 48%, #f3dc9bcc 50%, #fff8 52%, transparent 60%);
  transform: translateX(-120%);
}
.ending-montage.active .ending-logo::after {
  animation: ending-glint 1.3s ease calc(1.1s + var(--i) * 0.42s) both;
}
.ending-logo-name {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.03em;
  color: #fff6dc;
  text-shadow: 0 1px 4px #000c;
}

/* ---------- stats card ---------- */
.ending-stats,
.ending-credits {
  gap: 14px;
  min-width: min(420px, 92vw);
  padding: 26px clamp(22px, 4vw, 40px) 24px;
  border: 1px solid #f3dc9b55;
  border-radius: 18px;
  background: linear-gradient(180deg, #10261ef2, #0a1b14f5);
  box-shadow:
    inset 0 0 0 4px #10261e,
    inset 0 0 0 5px #f3dc9b26,
    0 22px 60px #000a;
}
.ending-stats::before,
.ending-credits::before {
  content: "";
  position: absolute;
  top: -5px;
  left: 50%;
  width: 9px;
  height: 9px;
  border: 1px solid #f3dc9b;
  background: #10261e;
  transform: translateX(-50%) rotate(45deg);
}
.ending-stats-name {
  margin: 0;
  font: 600 clamp(20px, 3vw, 28px)/1.2 Georgia, "Yu Mincho", serif;
  letter-spacing: 0.04em;
  color: #fffaf0;
}
.ending-stat-list,
.ending-credit-list {
  display: grid;
  width: 100%;
  margin: 0;
}
.ending-stat-list div {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 24px;
  padding: 9px 2px;
  border-bottom: 1px solid #f3dc9b22;
}
.ending-stat-list div:last-child {
  border-bottom: 0;
}
.ending-stat-list dt {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #d9e3d4;
  font-size: 14px;
}
.ending-stat-list dt::before {
  content: "";
  width: 5px;
  height: 5px;
  background: #f3dc9b;
  transform: rotate(45deg);
}
.ending-stat-list dd {
  margin: 0;
  color: #f3dc9b;
  font-size: 17px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}
.ending-stats.active .ending-stat-list div,
.ending-credits.active .ending-credit-list div {
  animation: ending-fade-up 0.8s ease calc(0.5s + var(--i) * 0.22s) both;
}

/* ---------- credits ---------- */
.ending-credit-list {
  gap: 14px;
}
.ending-credit-list dt {
  color: #f3dc9b;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.24em;
  text-transform: uppercase;
}
.ending-credit-list dd {
  margin: 3px 0 0;
  color: #fffaf0;
  font-size: 14px;
  line-height: 1.45;
}
.ending-thanks {
  margin: 4px 0 0;
  font: italic 500 16px/1.4 Georgia, "Yu Mincho", serif;
  color: #fff6dc;
}
.ending-credits.active .ending-thanks {
  animation: ending-fade-up 1s ease 1.6s both;
}

/* ---------- skip + hint ---------- */
.ending-skip {
  position: absolute;
  top: calc(var(--bar) / 2);
  right: max(18px, env(safe-area-inset-right));
  transform: translateY(-50%);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 38px;
  padding: 0 12px 0 16px;
  border: 1px solid #f3dc9b55;
  border-radius: 999px;
  background: #10261ecc;
  color: #f4efe0;
  font: 650 13px/1 inherit;
  letter-spacing: 0.06em;
  cursor: pointer;
  opacity: 0;
  transition:
    opacity 0.5s ease 0.6s,
    background 0.18s,
    border-color 0.18s;
  touch-action: manipulation;
}
.ending-skip .wi {
  width: 16px;
  height: 16px;
}
.world-ending.show .ending-skip {
  opacity: 1;
}
.world-ending.leaving .ending-skip {
  opacity: 0;
  transition-delay: 0s;
}
.ending-skip:hover {
  background: #315c40;
  border-color: #f3dc9b99;
}
.ending-skip:focus-visible {
  outline: 2px solid #f3dc9b;
  outline-offset: 2px;
}
.ending-hint {
  position: absolute;
  left: 50%;
  bottom: calc(var(--bar) / 2);
  margin: 0;
  transform: translate(-50%, 50%);
  color: #e9e2cd99;
  font-size: 11.5px;
  letter-spacing: 0.18em;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.6s ease;
  pointer-events: none;
}
.world-ending.can-advance .ending-hint {
  opacity: 1;
}

/* The dialogue box sits above the bottom bar during the ending. */
#game.in-ending .world-dialogue {
  z-index: 10;
}

/* ---------- keyframes ---------- */
@keyframes ending-rule {
  to {
    transform: scaleX(1);
  }
}
@keyframes ending-reveal {
  0% {
    opacity: 0;
    letter-spacing: 0.4em;
    filter: blur(10px) drop-shadow(0 2px 10px #000b);
  }
  60% {
    opacity: 1;
  }
  100% {
    opacity: 1;
    letter-spacing: 0.06em;
    filter: blur(0) drop-shadow(0 2px 10px #000b);
  }
}
@keyframes ending-fade-up {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}
@keyframes ending-logo-in {
  0% {
    opacity: 0;
    transform: scale(0.6) translateY(14px);
  }
  100% {
    opacity: 1;
    transform: none;
  }
}
@keyframes ending-glint {
  to {
    transform: translateX(120%);
  }
}

/* ---------- small / short screens ---------- */
@media (max-width: 520px) {
  .ending-stats,
  .ending-credits {
    padding: 20px 18px 18px;
  }
  .ending-credit-list dd {
    font-size: 12.5px;
  }
  .ending-kicker {
    font-size: 10.5px;
    letter-spacing: 0.24em;
  }
  .ending-skip {
    min-height: 34px;
    font-size: 12px;
  }
}
@media (max-height: 520px) {
  .world-ending {
    --bar: 40px;
  }
  .ending-stats,
  .ending-credits {
    gap: 8px;
    padding: 14px 22px 12px;
  }
  .ending-stat-list div {
    padding: 4px 2px;
  }
  .ending-stat-list dd {
    font-size: 15px;
  }
  .ending-credit-list {
    grid-template-columns: repeat(3, auto);
    gap: 4px 24px;
    text-align: start;
  }
  .ending-credit-list div:last-child {
    grid-column: 1 / -1;
  }
  .ending-credit-list dd {
    font-size: 12px;
    margin-top: 1px;
  }
  .ending-stats-name {
    font-size: 18px;
  }
  .ending-thanks {
    display: none;
  }
  .ending-logo {
    width: 52px;
    border-radius: 14px;
  }
  .ending-logos {
    max-width: 96vw;
  }
  .ending-logos li {
    width: 84px;
  }
  .ending-skip {
    min-height: 30px;
    padding: 0 10px 0 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ending-bar,
  .ending-panel,
  .ending-panel.active,
  .ending-stage::before,
  .ending-skip {
    transition-duration: 0.01s !important;
    transition-delay: 0s !important;
  }
  .ending-panel *,
  .ending-panel *::after {
    animation: none !important;
  }
  .ending-rule {
    transform: none;
  }
  .ending-logos li {
    opacity: 1;
  }
  .ending-logo::after {
    display: none;
  }
}
