:root {
  --paper: #ffffff;
  --ink: #202020;
  --muted: #737373;
  --line: rgba(0, 0, 0, 0.12);
  --panel: rgba(255, 255, 255, 0.92);
  --hat-width: 92px;
  --hat-visual-width: calc(var(--hat-width) + 30px);
  --row-height: 28px;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.16);
  font-family: "Noto Serif SC", "Source Han Serif SC", "Songti SC", "SimSun", serif;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--paper);
  color: var(--ink);
  scroll-behavior: smooth;
}

body {
  min-width: 320px;
  margin: 0;
  overflow-x: hidden;
  background: var(--paper);
}

button {
  font: inherit;
  cursor: pointer;
}

[role="button"] {
  cursor: pointer;
}

.app {
  min-height: 100vh;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.018), transparent 18%, transparent 82%, rgba(0, 0, 0, 0.018)),
    #fff;
}

.brand-panel {
  position: fixed;
  top: 18px;
  left: 18px;
  z-index: 35;
  width: min(230px, calc(100vw - 36px));
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
}

.brand-panel strong {
  display: block;
  font-size: 1.05rem;
  letter-spacing: 0.08em;
}

.status-text {
  margin: 8px 0 0;
  color: #b42318;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  font-size: 0.82rem;
  line-height: 1.45;
}

.status-text.is-ok {
  color: #176b35;
}

.stage {
  position: relative;
  z-index: 1;
  width: 100%;
  min-height: 100vh;
  padding: 72px 8px 92px;
}

.tower {
  position: relative;
  z-index: 8;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.tower-row {
  position: relative;
  z-index: var(--stack-z, 1);
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--hat-width) minmax(0, 1fr);
  align-items: center;
  width: min(720px, calc(100vw - 20px));
  min-height: var(--row-height);
  margin-top: -1px;
  isolation: isolate;
}

.tower-row:hover,
.tower-row:focus-within,
.tower-row.is-hover-row,
.tower-row.is-active-row,
.tower-row.is-selected-row {
  z-index: 999 !important;
}

.tower-row:first-child {
  margin-top: 0;
}

.tower-row.is-left .hat-choice {
  grid-column: 1 / -1;
}

.tower-row.is-right .hat-choice {
  grid-column: 1 / -1;
}

.hat-choice {
  --direction: 1;
  position: relative;
  z-index: 3;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--hat-width) minmax(0, 1fr);
  align-items: center;
  justify-items: center;
  overflow: visible;
  width: 100%;
  min-width: 0;
  min-height: var(--row-height);
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ink);
  pointer-events: none;
  transition: transform 0.16s ease, filter 0.16s ease, opacity 0.16s ease;
}

.tower-row.is-right .hat-choice {
  --direction: -1;
}

.hat-label {
  position: relative;
  z-index: 5;
  display: block;
  max-width: min(300px, calc(50vw - 56px));
  padding: 0;
  font-size: clamp(1rem, 1.55vw, 1.36rem);
  line-height: 1.08;
  text-align: right;
  white-space: nowrap;
  pointer-events: auto;
}

.tower-row.is-left .hat-label {
  grid-column: 1 / 2;
  grid-row: 1 / 2;
  justify-self: end;
  margin-right: var(--text-gap, 30px);
}

.tower-row.is-right .hat-label {
  grid-column: 3 / 4;
  grid-row: 1 / 2;
  justify-self: start;
  margin-left: var(--text-gap, 30px);
  text-align: left;
}

.hat-stack {
  position: relative;
  z-index: 2;
  display: block;
  grid-column: 2 / 3;
  grid-row: 1 / 2;
  justify-self: center;
  width: var(--hat-width);
  height: var(--row-height);
  pointer-events: auto;
}

.hat-stack img {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 2;
  width: var(--hat-visual-width);
  max-width: none;
  height: auto;
  transform: translate(-50%, -50%) rotate(var(--tilt, 0deg));
  transform-origin: center;
  filter: contrast(1.08) saturate(0.9) drop-shadow(0 2px 3px rgba(0, 0, 0, 0.38));
  transition: transform 0.16s ease, filter 0.16s ease, opacity 0.16s ease;
  user-select: none;
  pointer-events: none;
}

.tower-row:nth-child(4n + 1) .hat-stack img {
  --tilt: -0.7deg;
}

.tower-row:nth-child(4n + 2) .hat-stack img {
  --tilt: 0.45deg;
}

.tower-row:nth-child(4n + 3) .hat-stack img {
  --tilt: -0.25deg;
}

.hat-choice.is-hovering,
.hat-choice.is-active,
.hat-choice.is-selected {
  z-index: 15;
  outline: none;
  transform: translateX(calc(var(--direction) * -4px)) scale(1.045);
  filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.18));
}

.hat-choice.is-hovering .hat-stack img,
.hat-choice.is-active .hat-stack img,
.hat-choice.is-selected .hat-stack img {
  transform: translate(-50%, -50%) rotate(var(--tilt, 0deg)) scale(1.16);
  filter: contrast(1.18) saturate(1.06) drop-shadow(0 5px 8px rgba(0, 0, 0, 0.5));
}

.hat-choice.is-hovering .hat-label,
.hat-choice.is-active .hat-label,
.hat-choice.is-selected .hat-label {
  font-weight: 700;
  color: #000;
  text-shadow: 0 0 0.45px #000, 0 0 12px rgba(0, 0, 0, 0.13);
}

.hat-stack:focus-visible,
.hat-label:focus-visible {
  outline: 2px solid #111;
  outline-offset: 4px;
}

.person-wrap {
  position: relative;
  z-index: 1;
  width: min(92px, 22vw);
  margin: 4px auto 0;
  padding: 0;
}

.person-wrap img {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  height: auto;
  object-fit: contain;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.18));
}

.confirm-dialog {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(255, 255, 255, 0.74);
  backdrop-filter: blur(10px);
}

.dialog-card {
  width: min(460px, calc(100vw - 48px));
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
  text-align: center;
}

.dialog-card h2 {
  margin: 0 0 10px;
  font-size: clamp(1.35rem, 4vw, 2.2rem);
  font-weight: 500;
}

.dialog-card p {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  font-size: 1rem;
  line-height: 1.55;
}

.dialog-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 20px;
}

.primary-button,
.ghost-button {
  border-radius: 999px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  font-weight: 800;
  letter-spacing: 0.04em;
  transition: transform 0.16s ease, opacity 0.16s ease;
}

.primary-button {
  padding: 10px 18px;
  border: 1px solid #111;
  background: #111;
  color: #fff;
}

.ghost-button {
  padding: 10px 16px;
  border: 1px solid rgba(0, 0, 0, 0.22);
  background: rgba(255, 255, 255, 0.9);
  color: #111;
}

.primary-button:not(:disabled):hover,
.ghost-button:not(:disabled):hover {
  transform: translateY(-1px);
}

.primary-button:disabled,
.ghost-button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.ritual-scene {
  position: fixed;
  inset: 0;
  z-index: 45;
  display: grid;
  place-items: center;
  padding: 28px;
  overflow: hidden;
  background: radial-gradient(circle at center, #fff 0%, #fff 48%, rgba(245, 245, 245, 0.96) 100%);
}

.ritual-focus {
  display: grid;
  grid-template-columns: minmax(230px, 380px) minmax(230px, 460px);
  align-items: center;
  gap: clamp(20px, 5vw, 70px);
  transform: scale(0.8);
  animation: focusZoom 780ms cubic-bezier(0.17, 0.84, 0.25, 1.18) forwards;
}

.ritual-visual {
  position: relative;
  isolation: isolate;
  width: min(380px, 46vw);
  aspect-ratio: 1 / 1.06;
}

.ritual-hat {
  position: absolute;
  left: 50%;
  top: 37.5%;
  z-index: 8;
  width: 50%;
  max-width: none;
  transform: translateX(-50%) scale(0.9);
  transform-origin: bottom center;
  filter: contrast(1.16) saturate(1.04) drop-shadow(0 9px 8px rgba(0, 0, 0, 0.34));
  animation: giantHat 680ms 120ms cubic-bezier(0.2, 0.85, 0.24, 1.24) forwards;
}

.ritual-person {
  position: absolute;
  left: 50%;
  bottom: 5%;
  z-index: 5;
  width: 38%;
  transform: translateX(-50%);
  filter: drop-shadow(0 8px 10px rgba(0, 0, 0, 0.2));
}

.result-copy-wrap {
  max-width: 460px;
  opacity: 0;
  transform: translateY(18px);
  animation: textFloat 760ms 880ms ease forwards;
}

.result-copy-wrap h1 {
  margin: 0 0 14px;
  font-size: clamp(2.2rem, 6vw, 4.6rem);
  line-height: 0.98;
  word-break: break-word;
}

.result-copy {
  margin: 0 0 22px;
  color: #252525;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  font-size: clamp(1.05rem, 2.3vw, 1.55rem);
  line-height: 1.55;
}

.result-copy strong {
  font-size: 1.35em;
}

.leaderboard-section {
  position: relative;
  z-index: 50;
  width: min(960px, calc(100vw - 28px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 70px 0 90px;
}

.leaderboard-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}

.leaderboard-head h2 {
  margin: 0;
  font-size: clamp(1.8rem, 4.8vw, 3.4rem);
  font-weight: 500;
}

.leaderboard-title-block {
  display: grid;
  gap: 8px;
}

.leaderboard-total {
  margin: 0;
  color: var(--muted);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  font-size: clamp(1rem, 2vw, 1.22rem);
  font-weight: 800;
}

.leaderboard-total strong {
  color: #111;
  font-size: 1.34em;
}

.leaderboard-list {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.leaderboard-list li {
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 12px;
  min-height: 58px;
  padding: 11px 16px;
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.06);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
}

.leaderboard-list li.top-1 {
  background: linear-gradient(90deg, rgba(255, 215, 0, 0.3), #fff);
}

.leaderboard-list li.top-2 {
  background: linear-gradient(90deg, rgba(192, 192, 192, 0.32), #fff);
}

.leaderboard-list li.top-3 {
  background: linear-gradient(90deg, rgba(205, 127, 50, 0.3), #fff);
}

.leaderboard-list li.is-user {
  position: relative;
  border-color: #111;
  border-width: 2px;
  background: linear-gradient(90deg, rgba(255, 243, 133, 0.82), rgba(255, 255, 255, 0.96));
  box-shadow: 0 16px 38px rgba(0, 0, 0, 0.16), 0 0 0 4px rgba(255, 214, 0, 0.24);
}

.leaderboard-list li.is-user::before {
  content: "我的帽子";
  position: absolute;
  top: -10px;
  right: 18px;
  padding: 3px 9px;
  border: 1px solid #111;
  border-radius: 999px;
  background: #111;
  color: #fff;
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.04em;
}

.leaderboard-list .rank {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: #111;
  color: #fff;
  font-weight: 900;
}

.leaderboard-list .rank .crown {
  font-size: 1.28rem;
  line-height: 1;
}

.leaderboard-list .name {
  overflow: hidden;
  font-size: 1.08rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-list .user-marker {
  justify-self: end;
  padding: 5px 10px;
  border-radius: 999px;
  background: #111;
  color: #fff;
  font-size: 0.78rem;
  font-weight: 900;
  white-space: nowrap;
}

.leaderboard-list .count {
  color: #111;
  font-size: 1.02rem;
  font-weight: 900;
  white-space: nowrap;
}

.leaderboard-list .empty {
  display: block;
  color: var(--muted);
  text-align: center;
}

.app.is-scattering .hat-choice:not(.is-selected) {
  animation: flyAway 620ms cubic-bezier(0.17, 0.84, 0.25, 1) forwards;
}

.app.is-scattering .hat-choice.is-selected {
  z-index: 30;
  animation: chosenPulse 620ms ease forwards;
}

.app.result-mode .stage,
.app.result-mode .brand-panel {
  display: none;
}

[hidden] {
  display: none !important;
}

@keyframes flyAway {
  to {
    opacity: 0;
    transform: translate(calc(var(--fly-x, 0) * 1px), calc(var(--fly-y, -260) * 1px)) rotate(calc(var(--fly-r, 90) * 1deg)) scale(0.28);
    filter: blur(3px);
  }
}

@keyframes chosenPulse {
  50% {
    transform: translateX(calc(var(--direction) * -4px)) scale(1.26);
  }

  100% {
    opacity: 0;
    transform: translateX(calc(var(--direction) * -4px)) scale(1.52);
  }
}

@keyframes focusZoom {
  to {
    transform: scale(1);
  }
}

@keyframes giantHat {
  to {
    transform: translateX(-50%) scale(1);
  }
}

@keyframes textFloat {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 820px) {
  :root {
    --hat-width: 68px;
    --hat-visual-width: calc(var(--hat-width) + 24px);
    --row-height: 25px;
  }

  .brand-panel {
    top: 10px;
    left: 10px;
    width: auto;
    padding: 9px 11px;
  }

  .brand-panel strong {
    font-size: 0.94rem;
  }

  .stage {
    padding-top: 58px;
  }

  .tower-row {
    width: 100%;
  }

  .hat-label {
    max-width: calc(50vw - 34px);
    font-size: clamp(0.78rem, 3.4vw, 0.96rem);
    line-height: 1.06;
    white-space: normal;
    word-break: break-word;
  }

  .person-wrap {
    width: 68px;
    margin-top: 2px;
  }

  .ritual-focus {
    grid-template-columns: 1fr;
    gap: 8px;
    text-align: center;
  }

  .ritual-visual {
    width: min(300px, 78vw);
    aspect-ratio: 1 / 1.08;
    margin: 0 auto;
  }

  .ritual-hat {
    top: 32%;
    width: 54%;
  }

  .ritual-person {
    bottom: 5%;
    width: 42%;
  }

  .result-copy-wrap {
    margin: 0 auto;
  }

  .leaderboard-list li {
    grid-template-columns: 44px minmax(0, 1fr);
  }

  .leaderboard-list .rank {
    grid-row: 1 / span 3;
  }

  .leaderboard-list .name,
  .leaderboard-list .user-marker,
  .leaderboard-list .count {
    grid-column: 2 / 3;
  }

  .leaderboard-list .user-marker,
  .leaderboard-list .count {
    justify-self: start;
  }

  .leaderboard-list .count {
    margin-top: -4px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}