:root {
  color-scheme: dark;
  --font-display: "Noto Serif TC", "Songti TC", "PMingLiU", "Yu Mincho", serif;
  --font-body: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --text: #f2eaf2;
  --quiet: #d6ccd9;
  --pad-top: max(18px, env(safe-area-inset-top));
  --pad-bottom: max(22px, env(safe-area-inset-bottom));
  --pad-inline: max(16px, env(safe-area-inset-left), env(safe-area-inset-right));
  --box-choice: min(15.7vw, 23svh, 184px);
  --stage-card-width: min(7.5vw, 86px, 12svh);
  --slot-width: min(17vw, 14.5svh, 105px);
  --fan-width: clamp(70px, 12vw, 124px);
  --fan-step: clamp(44px, 6.2vw, 67px);
  --button-height: 48px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  display: grid;
  isolation: isolate;
  position: relative;
  background: #1d2135;
  color: var(--text);
  font-family: var(--font-body);
  -webkit-tap-highlight-color: transparent;
}

.sky {
  position: fixed;
  inset: 0;
  z-index: -2;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 50% 45%, rgb(22 25 48 / 40%), rgb(18 19 38 / 68%) 100%),
    #3d4569 url("/assets/quiet-sky.webp") center / cover no-repeat;
}

.sky::after {
  position: absolute;
  inset: 0;
  content: "";
  background:
    radial-gradient(circle at 13% 19%, #e2deed 0 .65px, transparent 1.5px),
    radial-gradient(circle at 26% 29%, #dbd5ef 0 .65px, transparent 1.5px),
    radial-gradient(circle at 68% 14%, #eee2d9 0 .7px, transparent 1.7px),
    radial-gradient(circle at 78% 29%, #e3ddec 0 .68px, transparent 1.5px),
    radial-gradient(circle at 92% 44%, #eee4e9 0 .6px, transparent 1.5px),
    radial-gradient(circle at 19% 72%, #ded7f3 0 .8px, transparent 1.7px),
    radial-gradient(circle at 36% 82%, #cec8e4 0 .63px, transparent 1.6px),
    radial-gradient(circle at 74% 73%, #eee3de 0 .72px, transparent 1.7px),
    radial-gradient(circle at 89% 87%, #cec8e4 0 .65px, transparent 1.5px);
  opacity: .62;
}

.celestial { z-index: -1; }

#app {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

#app:focus { outline: none; }

.stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  place-items: center;
  width: 100%;
  height: 100%;
  padding: var(--pad-top) var(--pad-inline) var(--pad-bottom);
}

button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible {
  outline: 2px solid #f5e5cf;
  outline-offset: 5px;
}
button:disabled { cursor: default; }

.ritual-button {
  position: relative;
  isolation: isolate;
  display: inline-grid;
  place-content: center;
  min-width: 126px;
  height: var(--button-height);
  padding-inline: 23px 19px;
  border: 1px solid rgb(226 208 231 / 66%);
  border-radius: 100vw;
  color: #fff4ee;
  background: linear-gradient(160deg, rgb(151 129 166 / 20%), rgb(22 24 45 / 76%) 77%);
  box-shadow: inset 0 0 0 3px rgb(67 57 87 / 44%), inset 0 0 18px rgb(195 160 222 / 10%), 0 5px 20px rgb(21 19 42 / 28%);
  font-family: var(--font-display);
  font-size: .94rem;
  font-weight: 500;
  letter-spacing: .24em;
  line-height: 1;
  text-shadow: 0 1px 6px #281f35;
  cursor: pointer;
  transition: box-shadow 220ms ease, background-color 220ms ease, transform 220ms ease;
}

.ritual-button::before, .ritual-button::after {
  position: absolute;
  top: 50%;
  width: 6px;
  height: 9px;
  background: #ebdbe8;
  clip-path: polygon(50% 0, 60% 38%, 100% 50%, 60% 60%, 50% 100%, 40% 60%, 0 50%, 40% 38%);
  content: "";
  transform: translateY(-50%);
}

.ritual-button::before { left: 12px; }
.ritual-button::after { right: 12px; }
.ritual-button:hover, .ritual-button:focus-visible {
  box-shadow: inset 0 0 0 3px rgb(67 57 87 / 32%), inset 0 0 18px rgb(225 202 244 / 23%), 0 0 26px rgb(218 183 246 / 19%);
}
.ritual-button:active { transform: scale(.97); }

.invitation {
  display: grid;
  place-items: center;
  width: min(100%, 28rem);
  text-align: center;
  transform: translateY(-2.5svh);
}

.welcome-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.02rem, 1.7vw, 1.32rem);
  font-weight: 400;
  line-height: 1.7;
  letter-spacing: .18em;
  text-indent: .18em;
  text-shadow: 0 2px 16px rgb(19 16 39 / 72%);
}

.welcome-line {
  margin: 12px 0 32px;
  color: var(--quiet);
  font-size: 0.875rem;
  line-height: 1.55;
  letter-spacing: .2em;
  text-indent: .2em;
}

.deck-grid {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: min(5.35vw, 73px);
  width: 100%;
}

.deck-choice, .fan-card, .flip-trigger {
  border: 0;
  color: inherit;
  background: transparent;
  padding: 0;
  cursor: pointer;
}

.deck-choice {
  display: grid;
  position: relative;
  place-items: center;
  min-width: 0;
  width: var(--box-choice);
  border-radius: 3px;
  transition: translate 280ms ease;
}
.deck-choice:not(:disabled):is(:hover, :focus-visible) { translate: 0 -8px; }

.deck-label {
  position: absolute;
  top: calc(100% + 18px);
  color: var(--quiet);
  font-family: var(--font-display);
  font-size: clamp(.875rem, 1.7vw, 1.02rem);
  letter-spacing: .26em;
  text-indent: .26em;
  white-space: nowrap;
}

.shuffle-stage {
  grid-template-rows: minmax(0, 1fr) var(--button-height);
  gap: min(3.4svh, 28px);
  padding-bottom: max(36px, env(safe-area-inset-bottom));
}

.ritual-field {
  isolation: isolate;
  position: relative;
  width: min(99%, 800px);
  height: min(70svh, 550px);
  max-height: 100%;
  transform: translateY(calc(var(--button-height) / 2));
}

.ritual-field::before {
  position: absolute;
  inset: -4% -1%;
  border-radius: 42%;
  background: radial-gradient(ellipse, rgb(17 13 31 / 29%), rgb(18 15 36 / 16%) 58%, transparent 74%);
  content: "";
}

.opening-box {
  position: absolute;
  inset: 50% auto auto 50%;
  z-index: 2;
  width: var(--box-choice);
  transform: translate(-50%, -50%);
}

.flying-card {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 1;
  width: min(var(--stage-card-width), var(--field-card-ceiling, var(--stage-card-width)));
  aspect-ratio: .62;
  border-radius: max(2px, .35vw);
  box-shadow: 0 3px 9px rgb(0 0 0 / 31%);
  opacity: 0;
  contain: layout style;
  pointer-events: none;
}

.shuffle-stage > .ritual-button { z-index: 3; grid-row: 2; }

.spread-stage {
  grid-template-rows: minmax(0, 1fr) auto var(--button-height) minmax(0, 12svh);
  gap: min(2.5svh, 21px);
}

.fan-rail {
  position: relative;
  align-self: center;
  width: min(100%, 970px);
  max-height: 100%;
  padding: 18px 1px 20px;
  overflow: hidden;
  touch-action: pan-x;
  -webkit-mask-image: linear-gradient(to right, transparent, #000 4%, #000 96%, transparent);
  mask-image: linear-gradient(to right, transparent, #000 4%, #000 96%, transparent);
}

.fan-rail.is-pickable {
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: none;
  -webkit-user-select: none;
  user-select: none;
}
.fan-rail.is-pickable::-webkit-scrollbar { display: none; }

.fan-track {
  display: flex;
  width: max-content;
  align-items: center;
  min-height: calc(var(--fan-width) / .64 + var(--fan-rise, 35px) * 1.25 + 13px);
  padding: 0 max(30px, 4vw);
  opacity: 0;
}
.fan-track.is-arranged { opacity: 1; }

.fan-card {
  position: relative;
  display: block;
  flex: none;
  width: var(--fan-width);
  aspect-ratio: .64;
  margin-inline-end: calc(var(--fan-step) - var(--fan-width));
  border-radius: 5px;
  box-shadow: -2px 3px 10px rgb(9 5 16 / 41%);
  transform: translateY(var(--dip, 0)) rotate(var(--pitch, 0deg));
  transition: translate 220ms ease, opacity 240ms ease;
}
.fan-card:last-child { margin-inline-end: 0; }
.fan-card:focus-visible {
  z-index: 1;
  translate: 0 -9px;
}
@media (hover: hover) and (pointer: fine) {
  .is-pickable .fan-card:hover { translate: 0 -9px; }
}
.fan-card.is-taken {
  opacity: 0;
  pointer-events: none;
}
.fan-card .card-back { pointer-events: none; }

.is-preview .is-arranged .fan-card {
  cursor: default;
  animation: unfold 540ms both cubic-bezier(.22,.66,.26,1);
  animation-delay: var(--enter-delay);
}
@keyframes unfold {
  from { transform: translateX(var(--unfold-shift, 0)) translateY(35px) scale(.87); opacity: .18; }
  to { transform: translateY(var(--dip)) rotate(var(--pitch)); opacity: 1; }
}

.moon-slider {
  display: flex;
  align-items: center;
  gap: 14px;
  color: var(--quiet);
}

.moon-choice {
  font-size: .875rem;
  letter-spacing: .12em;
  white-space: nowrap;
}
.moon-choice.is-current { color: #fff4ee; }

.moon-track {
  display: grid;
  place-items: center;
  width: 88px;
  height: 44px;
}

.moon-range {
  appearance: none;
  width: 100%;
  height: 44px;
  margin: 0;
  border-radius: 2rem;
  background: transparent;
  cursor: pointer;
}

.moon-range::-webkit-slider-runnable-track {
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg, #8d7aaa, #e7d4ea, #8d7aaa);
}
.moon-range::-moz-range-track {
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg, #8d7aaa, #e7d4ea, #8d7aaa);
}
.moon-range::-webkit-slider-thumb {
  appearance: none;
  width: 21px;
  height: 21px;
  margin-top: -9.5px;
  border: 1px solid #f1ddf1;
  border-radius: 50%;
  background: radial-gradient(circle at 37% 35%, #f6e8f4, #bca4cf 57%, #5b4b72 96%);
  box-shadow: 0 0 10px rgb(226 196 246 / 48%);
}
.moon-range::-moz-range-thumb {
  width: 21px;
  height: 21px;
  border: 1px solid #f1ddf1;
  border-radius: 50%;
  background: radial-gradient(circle at 37% 35%, #f6e8f4, #bca4cf 57%, #5b4b72 96%);
  box-shadow: 0 0 10px rgb(226 196 246 / 48%);
}

.draw-stage {
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  align-items: center;
}

.draw-slots, .revealed-deck {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: clamp(10px, 3vw, 34px);
  width: 100%;
}

.draw-slot {
  width: var(--slot-width);
  aspect-ratio: .64;
  border: 1px solid rgb(227 212 235 / 38%);
  border-radius: 6px;
  background:
    radial-gradient(circle closest-side, transparent 49%, rgb(231 216 238 / 12%) 50% 51%, transparent 52%),
    rgb(28 22 49 / 16%);
}

.draw-slot.is-held { border: 0; }
.selected-flight {
  position: fixed;
  z-index: 5;
  transform-origin: center;
  pointer-events: none;
  border-radius: 5px;
}

.reveal-stage {
  grid-template-rows: minmax(0, 1fr) var(--button-height);
  gap: 22px;
  padding-bottom: max(10svh, var(--pad-bottom));
}
.reveal-stage > .ritual-button { grid-row: 2; }

.revealed-deck { --face-width: min(24vw, 26svh, 218px); }
.revealed-deck.count-1 { --face-width: min(51vw, 26svh, 240px); }

.revealed-card {
  width: var(--face-width);
  min-width: 0;
  margin: 0;
  text-align: center;
}

.flip-trigger {
  display: block;
  position: relative;
  width: 100%;
  aspect-ratio: .6;
  border-radius: 6px;
  perspective: 950px;
}

.flip-inner {
  display: block;
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  border-radius: inherit;
}
.is-revealed .flip-inner { transform: rotateY(180deg); }

.flip-back, .flip-front {
  position: absolute;
  display: grid;
  place-items: center;
  inset: 0;
  border-radius: inherit;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  box-shadow: 0 6px 28px rgb(13 8 25 / 30%);
}
.flip-front {
  padding: 1px;
  transform: rotateY(180deg);
  border: 1px solid var(--deck-trim);
  background: var(--deck-ink-shadow);
}

.flip-front img {
  display: block;
  width: 100%;
  height: 100%;
  max-height: 100%;
  min-height: 0;
  object-fit: contain;
  border-radius: 3px;
}

.card-name {
  margin-block-start: 13px;
  color: #f8eef2;
  font-family: var(--font-display);
  font-size: clamp(.86rem, 2.45vw, 1.06rem);
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (max-width: 690px) {
  :root {
    --box-choice: min(24.4vw, 22svh, 132px);
    --stage-card-width: min(11.1vw, 9.8svh, 53px);
    --fan-width: min(23vw, 14svh, 103px);
  }

  .deck-grid { gap: max(12px, 4.8vw); }
  .deck-choice .box-title, .opening-box .box-title { font-size: clamp(.44rem, 1.55vw + .12rem, .76rem); }
  .deck-label { top: calc(100% + 12px); }
  .shuffle-stage { padding-bottom: max(31px, env(safe-area-inset-bottom)); }
  .fan-track { padding-inline: 24px; }
  .reveal-stage { padding-bottom: max(7svh, var(--pad-bottom)); }
  .revealed-deck { --face-width: min(26vw, 26svh, 190px); }
}
@media (max-height: 440px) and (orientation: landscape) {
  :root {
    --pad-top: 10px;
    --pad-bottom: 12px;
    --fan-width: min(12vw, 15svh);
    --slot-width: min(11vw, 13svh);
  }
  .welcome-line { margin-block: 8px 20px; }
  .spread-stage { grid-template-rows: minmax(0, 1fr) auto var(--button-height); }
  .reveal-stage { gap: 6px; }
  .revealed-deck, .revealed-deck.count-1 { --face-width: min(24vw, 25svh, 218px); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; animation-delay: 0ms !important; }
}
