:root {
  color-scheme: dark;
  --bg: #18100f;
  --bg-secondary: #211310;
  --surface: rgba(48, 25, 20, 0.82);
  --surface-strong: #321a16;
  --surface-muted: rgba(255, 244, 221, 0.06);
  --text: #fff8e8;
  --text-muted: #cbbdaf;
  --line: rgba(255, 228, 185, 0.14);
  --accent: #d74b37;
  --accent-hover: #e25a43;
  --accent-dark: #9f2f24;
  --gold: #f3ca74;
  --gold-soft: #f9e3ae;
  --danger: #ff8f7f;
  --shadow: rgba(60, 9, 4, 0.42);
  --radius: 20px;
  --font-sans: "PingFang SC", "Noto Sans CJK SC", "Microsoft YaHei", system-ui, sans-serif;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #fff8eb;
  --bg-secondary: #f7e8ce;
  --surface: rgba(255, 253, 247, 0.9);
  --surface-strong: #fffdf7;
  --surface-muted: rgba(133, 44, 33, 0.06);
  --text: #311d18;
  --text-muted: #735f55;
  --line: rgba(116, 58, 42, 0.15);
  --accent: #c73f2f;
  --accent-hover: #ae3327;
  --accent-dark: #84281f;
  --gold: #9a681d;
  --gold-soft: #6d4813;
  --danger: #a3241b;
  --shadow: rgba(119, 60, 35, 0.17);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
  scroll-behavior: smooth;
}

body {
  min-width: 320px;
  min-height: 100dvh;
  margin: 0;
  overflow-x: hidden;
  color: var(--text);
  background:
    radial-gradient(circle at 16% 0%, rgba(184, 48, 34, 0.18), transparent 31rem),
    radial-gradient(circle at 85% 90%, rgba(219, 153, 54, 0.08), transparent 28rem),
    var(--bg);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
}

button:focus-visible,
input:focus-visible,
summary:focus-visible,
a:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 3px;
}

button:active {
  transform: translateY(1px);
}

#confetti {
  position: fixed;
  inset: 0;
  z-index: 4;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.app-shell {
  position: relative;
  z-index: 1;
  width: min(1480px, 100%);
  min-height: 100dvh;
  margin: 0 auto;
  padding: 0 clamp(20px, 4vw, 72px);
}

.topbar {
  display: flex;
  min-height: 72px;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: inherit;
  text-decoration: none;
}

.brand-mark,
.empty-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  border: 1px solid color-mix(in srgb, var(--gold) 50%, transparent);
  border-radius: 50%;
  color: var(--gold-soft);
  background: color-mix(in srgb, var(--accent) 24%, transparent);
  font-weight: 800;
}

.brand strong,
.brand small {
  display: block;
}

.brand strong {
  font-size: 15px;
  letter-spacing: 0.02em;
}

.brand small {
  margin-top: 2px;
  color: var(--text-muted);
  font-size: 11px;
}

.topbar-actions {
  display: flex;
  gap: 8px;
}

.admin-notice {
  min-height: 44px;
  margin-top: 14px;
  padding: 12px 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1.5;
}

.admin-notice.is-active {
  border-color: color-mix(in srgb, var(--gold) 42%, transparent);
  color: var(--gold-soft);
  background: color-mix(in srgb, var(--gold) 8%, transparent);
}

.admin-notice.is-error {
  border-color: color-mix(in srgb, var(--danger) 40%, transparent);
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 7%, transparent);
}

.quiet-button,
.text-button,
.reset-button,
.dialog-close {
  border: 0;
  cursor: pointer;
}

.quiet-button {
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--text-muted);
  background: var(--surface-muted);
  font-size: 13px;
  transition: border-color 180ms ease, color 180ms ease, background 180ms ease;
}

.quiet-button:hover {
  border-color: color-mix(in srgb, var(--gold) 40%, transparent);
  color: var(--text);
  background: color-mix(in srgb, var(--surface-muted) 70%, var(--gold) 6%);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(330px, 0.75fr);
  gap: clamp(30px, 5vw, 76px);
  min-height: calc(100dvh - 130px);
  padding: clamp(36px, 6vh, 72px) 0 42px;
}

.draw-stage {
  display: flex;
  min-width: 0;
  flex-direction: column;
}

.stage-copy {
  max-width: 720px;
}

.eyebrow {
  margin: 0 0 12px;
  color: var(--gold-soft);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.18em;
}

.stage-copy h1 {
  max-width: none;
  margin: 0;
  font-size: clamp(42px, 4.7vw, 68px);
  font-weight: 850;
  letter-spacing: -0.055em;
  line-height: 1.04;
}

.stage-copy h1 span {
  display: block;
  white-space: nowrap;
}

.stage-copy > p:last-child {
  max-width: 590px;
  margin: 20px 0 0;
  color: var(--text-muted);
  font-size: clamp(15px, 1.4vw, 18px);
  line-height: 1.8;
}

.draw-surface {
  display: grid;
  grid-template-columns: minmax(240px, 0.7fr) minmax(320px, 1.3fr);
  gap: clamp(28px, 4vw, 58px);
  align-items: end;
  margin-top: clamp(24px, 4vh, 44px);
  padding: clamp(22px, 3.5vw, 40px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background:
    linear-gradient(145deg, color-mix(in srgb, var(--surface) 94%, var(--gold) 4%), var(--surface)),
    var(--surface);
  box-shadow: 0 28px 90px var(--shadow);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.envelope-wrap {
  position: relative;
  display: grid;
  min-height: 300px;
  place-items: center;
  perspective: 1200px;
}

.halo {
  position: absolute;
  border: 1px solid color-mix(in srgb, var(--gold) 22%, transparent);
  border-radius: 50%;
  opacity: 0.65;
}

.halo-one {
  width: 260px;
  height: 260px;
}

.halo-two {
  width: 205px;
  height: 205px;
}

.envelope {
  position: relative;
  width: 180px;
  height: 242px;
  filter: drop-shadow(0 26px 22px rgba(84, 13, 7, 0.34));
  transform-style: preserve-3d;
}

.envelope-back,
.envelope-front {
  position: absolute;
  inset: 0;
  border-radius: 18px;
  background: linear-gradient(145deg, #db4c36, #a52820);
}

.envelope-back {
  box-shadow: inset 0 1px 0 rgba(255, 232, 194, 0.3);
}

.envelope-front {
  z-index: 3;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 26%, rgba(255, 216, 131, 0.12), transparent 32%),
    linear-gradient(145deg, #d94834, #a62720);
}

.envelope-front::after {
  position: absolute;
  right: -42px;
  bottom: -48px;
  width: 130px;
  height: 130px;
  border: 1px solid rgba(255, 220, 153, 0.16);
  border-radius: 50%;
  content: "";
}

.envelope-flap {
  position: absolute;
  z-index: 4;
  top: 0;
  left: 0;
  width: 100%;
  height: 43%;
  border-radius: 18px 18px 52% 52%;
  background: linear-gradient(160deg, #ef684b, #c43a2b);
  box-shadow: inset 0 1px 0 rgba(255, 236, 207, 0.34), 0 5px 12px rgba(92, 12, 7, 0.18);
  transform-origin: top;
  transition: transform 700ms cubic-bezier(0.16, 1, 0.3, 1);
}

.envelope-seal {
  position: relative;
  z-index: 5;
  display: grid;
  width: 78px;
  height: 78px;
  place-items: center;
  border: 1px solid rgba(109, 67, 17, 0.24);
  border-radius: 50%;
  color: #8a321f;
  background: radial-gradient(circle at 34% 28%, #ffe4a0, #e8b74f 72%);
  box-shadow: 0 10px 20px rgba(81, 22, 8, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.5);
  font-size: 35px;
  font-weight: 900;
}

.envelope-front small {
  position: absolute;
  bottom: 23px;
  color: rgba(255, 240, 216, 0.78);
  font-size: 11px;
  letter-spacing: 0.1em;
}

.envelope-card {
  position: absolute;
  z-index: 2;
  top: 15px;
  left: 16px;
  display: flex;
  width: calc(100% - 32px);
  height: 178px;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  border-radius: 14px;
  color: #5d341b;
  background: linear-gradient(155deg, #fff8dc, #edc875);
  box-shadow: 0 6px 20px rgba(67, 18, 6, 0.18);
  transform: translateY(22px);
  transition: transform 760ms cubic-bezier(0.16, 1, 0.3, 1);
}

.envelope-card .card-kicker {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.16em;
}

.envelope-card strong {
  min-width: 2ch;
  margin: 4px 0 -2px;
  font-size: 58px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.06em;
  line-height: 1;
}

.envelope-card > span:last-child {
  font-size: 13px;
  font-weight: 700;
}

.envelope.is-drawing {
  animation: envelopeShake 660ms ease-in-out infinite;
}

.envelope.is-opening .envelope-flap,
.envelope.is-drawing .envelope-flap {
  transform: rotateX(172deg);
}

.envelope.is-opening .envelope-card,
.envelope.is-drawing .envelope-card {
  transform: translateY(-76px);
}

.draw-form {
  align-self: end;
  padding-bottom: 4px;
}

.field-label {
  display: block;
  margin-top: 0;
  margin-bottom: 10px;
  color: var(--text);
  font-size: 14px;
  font-weight: 750;
}

.input-stack {
  display: grid;
  gap: 10px;
}

.input-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
}

input {
  width: 100%;
  min-height: 56px;
  padding: 0 17px;
  border: 1px solid var(--line);
  border-radius: 16px;
  color: var(--text);
  background: var(--surface-muted);
  transition: border-color 180ms ease, background 180ms ease;
}

input::placeholder {
  color: color-mix(in srgb, var(--text-muted) 86%, transparent);
}

input:hover,
input:focus {
  border-color: color-mix(in srgb, var(--gold) 52%, transparent);
  background: color-mix(in srgb, var(--surface-muted) 90%, var(--gold) 4%);
}

input[aria-invalid="true"] {
  border-color: var(--danger);
}

.primary-button {
  min-width: 132px;
  min-height: 56px;
  padding: 0 24px;
  border: 1px solid color-mix(in srgb, var(--accent) 88%, white 12%);
  border-radius: 16px;
  color: #fff9ef;
  background: var(--accent);
  box-shadow: 0 13px 30px color-mix(in srgb, var(--accent-dark) 34%, transparent);
  cursor: pointer;
  font-weight: 800;
  white-space: nowrap;
  transition: transform 180ms ease, background 180ms ease, box-shadow 180ms ease;
}

.primary-button:hover:not(:disabled) {
  background: var(--accent-hover);
  box-shadow: 0 15px 34px color-mix(in srgb, var(--accent-dark) 46%, transparent);
  transform: translateY(-1px);
}

.primary-button:disabled {
  color: #fff9ef;
  background: var(--accent);
  box-shadow: 0 13px 30px color-mix(in srgb, var(--accent-dark) 34%, transparent);
  cursor: not-allowed;
}

.field-help,
.field-error {
  min-height: 20px;
  margin: 9px 2px 0;
  font-size: 12px;
  line-height: 1.55;
}

.field-help {
  color: var(--text-muted);
}

.field-error {
  color: var(--danger);
}

.pool-strip {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin-top: -2px;
  padding-top: 22px;
  border-top: 1px solid var(--line);
}

.pool-strip > div {
  padding: 0 22px;
  border-right: 1px solid var(--line);
}

.pool-strip > div:first-child {
  padding-left: 0;
}

.pool-strip > div:last-child {
  border-right: 0;
}

.pool-strip span,
.pool-strip strong {
  display: block;
}

.pool-strip > div > span {
  margin-bottom: 5px;
  color: var(--text-muted);
  font-size: 12px;
}

.pool-strip strong {
  color: var(--gold-soft);
  font-size: 19px;
  font-variant-numeric: tabular-nums;
}

.pool-strip strong span {
  display: inline;
}

.side-panel {
  min-width: 0;
  align-self: stretch;
  padding-left: clamp(24px, 4vw, 54px);
  border-left: 1px solid var(--line);
}

.history-section {
  min-height: 390px;
}

.section-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 22px;
}

.section-heading h2 {
  margin: 0;
  font-size: 22px;
  letter-spacing: -0.03em;
}

.section-heading p {
  margin: 6px 0 0;
  color: var(--text-muted);
  font-size: 13px;
}

.text-button {
  flex: 0 0 auto;
  padding: 5px 0;
  color: var(--gold-soft);
  background: transparent;
  font-size: 13px;
  font-weight: 700;
}

.text-button:disabled {
  color: var(--text-muted);
  cursor: not-allowed;
  opacity: 0.58;
}

.empty-state {
  display: flex;
  min-height: 250px;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--text-muted);
}

.empty-state .empty-mark {
  width: 58px;
  height: 58px;
  font-size: 22px;
  opacity: 0.75;
}

.empty-state p {
  margin: 16px 0 0;
  font-size: 13px;
}

.history-list {
  max-height: 382px;
  margin: 0;
  padding: 0 6px 0 0;
  overflow-y: auto;
  list-style: none;
  scrollbar-color: var(--line) transparent;
}

.history-item {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  gap: 13px;
  align-items: center;
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
}

.history-item:first-child {
  padding-top: 2px;
}

.history-rank {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border-radius: 50%;
  color: var(--gold-soft);
  background: var(--surface-muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  font-weight: 750;
}

.history-person strong,
.history-person time {
  display: block;
}

.history-person strong {
  overflow: hidden;
  font-size: 14px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-person time {
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 11px;
}

.history-amount {
  color: var(--gold-soft);
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  font-weight: 850;
}

.reset-button {
  width: 100%;
  min-height: 46px;
  margin-top: 28px;
  border: 1px solid transparent;
  border-radius: 16px;
  color: var(--text-muted);
  background: transparent;
  font-size: 12px;
  transition: color 180ms ease, border-color 180ms ease, background 180ms ease;
}

.admin-actions:empty {
  display: none;
}

.reset-button:hover {
  border-color: color-mix(in srgb, var(--danger) 36%, transparent);
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 7%, transparent);
}

.footer {
  display: flex;
  min-height: 58px;
  align-items: center;
  justify-content: space-between;
  border-top: 1px solid var(--line);
  color: var(--text-muted);
  font-size: 11px;
}

.result-dialog {
  width: min(460px, calc(100% - 36px));
  padding: 0;
  overflow: visible;
  border: 1px solid color-mix(in srgb, var(--gold) 32%, transparent);
  border-radius: var(--radius);
  color: var(--text);
  background: var(--surface-strong);
  box-shadow: 0 42px 130px rgba(35, 7, 4, 0.55);
}

.result-dialog::backdrop {
  background: rgba(20, 8, 6, 0.76);
  backdrop-filter: blur(9px);
  -webkit-backdrop-filter: blur(9px);
}

.result-dialog[open] {
  animation: resultEnter 440ms cubic-bezier(0.16, 1, 0.3, 1) both;
}

.result-inner {
  position: relative;
  overflow: hidden;
  padding: 48px 38px 36px;
  border-radius: inherit;
  text-align: center;
}

.result-inner::before,
.result-inner::after {
  position: absolute;
  width: 190px;
  height: 190px;
  border: 1px solid color-mix(in srgb, var(--gold) 17%, transparent);
  border-radius: 50%;
  content: "";
  pointer-events: none;
}

.result-inner::before {
  top: -118px;
  left: -88px;
}

.result-inner::after {
  right: -108px;
  bottom: -128px;
}

.dialog-close {
  position: absolute;
  z-index: 1;
  top: 17px;
  right: 18px;
  padding: 6px;
  color: var(--text-muted);
  background: transparent;
  font-size: 12px;
}

.result-name {
  margin: 0 0 8px;
  color: var(--gold-soft);
  font-size: 14px;
  font-weight: 750;
}

.result-dialog h2 {
  margin: 0;
  font-size: 24px;
  letter-spacing: -0.03em;
}

.result-amount {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  margin: 22px 0 16px;
  color: var(--gold-soft);
}

.result-amount span {
  margin-top: 17px;
  font-size: 23px;
  font-weight: 800;
}

.result-amount strong {
  font-size: 92px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.08em;
  line-height: 0.95;
}

.result-message {
  min-height: 24px;
  margin: 0 0 25px;
  color: var(--text-muted);
  font-size: 14px;
}

.result-next {
  width: 100%;
}

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

@keyframes envelopeShake {
  0%,
  100% {
    transform: rotate(-1.5deg) translateY(0);
  }
  50% {
    transform: rotate(1.5deg) translateY(-5px);
  }
}

@keyframes resultEnter {
  from {
    opacity: 0;
    transform: translateY(22px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@media (max-width: 1040px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr) 330px;
    gap: 34px;
  }

  .draw-surface {
    grid-template-columns: 1fr;
  }

  .envelope-wrap {
    min-height: 260px;
  }

  .pool-strip {
    grid-column: auto;
  }
}

@media (max-width: 780px) {
  .app-shell {
    padding: 0 18px;
  }

  .topbar {
    min-height: 66px;
  }

  .brand small,
  #soundToggle {
    display: none;
  }

  .quiet-button {
    min-height: 38px;
    padding: 0 11px;
  }

  .workspace {
    display: block;
    min-height: 0;
    padding: 36px 0 28px;
  }

  .stage-copy h1 {
    max-width: none;
    font-size: clamp(34px, 9.6vw, 44px);
  }

  .stage-copy > p:last-child {
    margin-top: 16px;
    font-size: 15px;
  }

  .draw-surface {
    gap: 16px;
    margin-top: 28px;
    padding: 20px;
  }

  .envelope-wrap {
    min-height: 224px;
    transform: scale(0.84);
  }

  .input-row {
    grid-template-columns: 1fr;
  }

  .primary-button {
    width: 100%;
  }

  .pool-strip > div {
    padding: 0 12px;
  }

  .pool-strip > div:first-child {
    padding-left: 0;
  }

  .pool-strip > div:last-child {
    padding-right: 0;
  }

  .pool-strip strong {
    font-size: 16px;
  }

  .side-panel {
    margin-top: 46px;
    padding: 36px 0 0;
    border-top: 1px solid var(--line);
    border-left: 0;
  }

  .history-section {
    min-height: 0;
  }

  .footer {
    gap: 10px;
    align-items: flex-start;
    flex-direction: column;
    justify-content: center;
    padding: 16px 0;
  }
}

@media (max-width: 420px) {
  .brand-mark {
    width: 38px;
    height: 38px;
  }

  .brand strong {
    font-size: 13px;
  }

  .pool-strip > div > span {
    font-size: 10px;
  }

  .pool-strip strong {
    font-size: 14px;
  }

  .result-inner {
    padding: 48px 24px 28px;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

@media (prefers-reduced-transparency: reduce) {
  .draw-surface,
  .result-dialog,
  .result-dialog::backdrop {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }

  .draw-surface {
    background: var(--surface-strong);
  }
}
