:root {
  color-scheme: dark;
  font-family: system-ui, "PingFang SC", "Microsoft YaHei", sans-serif;
  color: #fff2d5;
  background: #130c18;
  -webkit-tap-highlight-color: transparent;
}

* { box-sizing: border-box; }
body { min-width: 280px; margin: 0; }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 2px solid #ffe198; outline-offset: 4px; }
button:disabled { cursor: default; }

.game {
  width: min(100%, 520px);
  min-height: 100vh;
  min-height: 100svh;
  margin-inline: auto;
  display: grid;
  grid-template-rows: auto minmax(320px, 1fr) auto;
  padding: max(24px, calc(env(safe-area-inset-top) + 12px)) clamp(16px, 5vw, 32px)
    max(16px, calc(env(safe-area-inset-bottom) + 10px));
  background: linear-gradient(180deg, #160b205e, transparent 31%, #2312220c 56%, #140b19 100%),
    url("./assets/moonlit-tavern.webp") center 48% / cover no-repeat, #241329;
  box-shadow: 0 0 100px #0009;
  overflow: hidden;
}

.masthead { text-align: center; text-shadow: 0 2px 8px #1c0a23; }
.eyebrow { display: flex; justify-content: center; align-items: center; gap: 12px; color: #ffe0ab; font-size: .875rem; letter-spacing: .25em; }
.eyebrow i { width: 6px; height: 6px; border: 1px solid #ffcf83; transform: rotate(45deg); box-shadow: 0 0 9px #e7a562; }
h1 {
  margin: .14em -.08em .08em 0;
  color: #ffdc96;
  font-family: "Songti SC", "Noto Serif CJK SC", SimSun, serif;
  font-weight: 900;
  font-size: clamp(2.7rem, 12vw, 4.25rem);
  line-height: 1.16;
  letter-spacing: .09em;
  text-shadow: 0 2px #8a4323, 0 5px #3c1a2e, 0 7px 16px #140820;
}
.masthead p { margin: 10px 0 0; color: #f4d9c0; font-size: .9375rem; letter-spacing: .13em; }

.play { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; align-items: center; justify-items: center; min-width: 0; padding: clamp(16px, 3vh, 25px) 0 14px; }
.status {
  position: relative;
  z-index: 1;
  min-height: 31px;
  max-width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 13px;
  border: 1px solid #f1bf7a54;
  border-radius: 99px;
  background: #1d1123da;
  color: #ffe1ad;
  font-size: .875rem;
  line-height: 1.25;
  text-align: center;
}
.status::before { content: ""; flex: 0 0 auto; width: 6px; height: 6px; border-radius: 50%; background: #ffc378; box-shadow: 0 0 7px #ffc378; }
.table { position: relative; width: min(100%, 405px); height: 100%; min-height: 225px; isolation: isolate; --base: 12%; --travel: 134px; --lift: 0; }
.table[data-cover="open"] { --lift: 1; }
.tray {
  position: absolute;
  left: 4%;
  bottom: var(--base);
  width: 92%;
  height: 30%;
  min-height: 78px;
  border-radius: 50%;
  border: 5px solid #9f602e;
  background: radial-gradient(ellipse at 48% 37%, #613548, #331a2f 74%);
  box-shadow: inset 0 5px 10px #120912, inset 0 0 0 3px #211121, 0 2px #e5a559, 0 7px #54251c, 0 11px #241019, 0 16px 22px #0d070ce0;
}
.cup {
  position: absolute;
  z-index: 3;
  left: 50%;
  bottom: calc(var(--base) + 10%);
  width: min(77%, 300px);
  height: min(77%, 280px);
  padding: 0;
  border: 0;
  background: transparent;
  touch-action: none;
  transform: translate(calc(-50% + 13px * var(--lift)), calc(var(--travel) * var(--lift) * -1))
    scale(calc(1 - .25 * var(--lift))) rotate(calc(-12deg * var(--lift)));
  transform-origin: 50% 90%;
  transition: transform 250ms cubic-bezier(.2,.7,.3,1);
  user-select: none;
}
.cup img { position: absolute; inset: 0; display: block; width: 100%; height: 100%; object-fit: contain; object-position: center bottom; pointer-events: none; filter: drop-shadow(0 14px 12px #120910a0); }
.cup-shadow { position: absolute; left: 8%; width: 84%; height: 17%; bottom: -4%; border-radius: 50%; background: #130917c4; filter: blur(9px); opacity: calc(1 - var(--lift)); }
.table[data-dragging] :is(.cup, .dice) { transition: none; }
.table[data-shaking] .cup { animation: shake 1050ms ease-in-out both; }
@keyframes shake {
  0%, 100% { transform: translate(-50%, 0); }
  12% { transform: translate(-63%, -14px) perspective(420px) rotateY(-19deg) rotate(-13deg); }
  25% { transform: translate(-36%, -28px) perspective(420px) rotateY(17deg) rotate(15deg); }
  38% { transform: translate(-63%, -24px) perspective(420px) rotateY(-15deg) rotate(-13deg); }
  51% { transform: translate(-38%, -32px) perspective(420px) rotateY(16deg) rotate(12deg); }
  64% { transform: translate(-58%, -16px) perspective(420px) rotateY(-10deg) rotate(-10deg); }
  77% { transform: translate(-45%, -18px) rotate(7deg); }
  88% { transform: translate(-53%, -4px) rotate(-4deg); }
}

.dice {
  position: absolute;
  z-index: 2;
  width: 74%;
  left: 13%;
  height: 29%;
  bottom: calc(var(--base) + 7%);
  opacity: clamp(0, calc((var(--lift) - .08) * 3.7), 1);
  transition: opacity 150ms;
  pointer-events: none;
  --die-size: clamp(32px, 9vw, 47px);
}
.table[data-shaking] .dice { opacity: 1; }
.table[data-shaking] .die { animation: rattle var(--speed) ease-in-out infinite alternate; }
.die {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: var(--die-size);
  height: var(--die-size);
  transform: translate(-50%, -50%);
  perspective: 360px;
}
.die::before { content: ""; position: absolute; inset: 58% -8% -6%; border-radius: 50%; background: #150c15aa; filter: blur(4px); }
.die-camera, .cube { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; }
.die-camera { transform: rotateX(-50deg) rotateY(var(--yaw)); }
.cube { transform: var(--orientation); }
.face {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  place-items: center;
  padding: 14%;
  border: 1px solid #fff1d7;
  border-radius: 15%;
  background: linear-gradient(145deg, #eac798, #bb8b59);
  box-shadow: inset 1px 1px 3px #fff9, inset -2px -2px 4px #79491c8a;
  transform: var(--face-rotation) translateZ(calc(var(--die-size) / 2 - .5px));
  backface-visibility: hidden;
}
.face[data-top] { background: linear-gradient(145deg, #fff9e8, #efd5a5); }
.pip { width: 75%; height: 75%; border-radius: 50%; background: #214c93; box-shadow: inset 0 1px 2px #10182d99, 0 1px #fff8; }
.face:is([data-value="1"], [data-value="4"]) .pip { background: #b82931; }
.face[data-value="1"] .pip { width: 94%; height: 94%; }
.table[data-shaking] .cube { animation: tumble var(--speed) linear infinite; }
@keyframes rattle {
  35% { transform: translate(-49%, -71%); }
  68% { transform: translate(-67%, -34%); }
  100% { transform: translate(-34%, -57%); }
}
@keyframes tumble {
  from { transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg) var(--orientation); }
  to { transform: rotateX(360deg) rotateY(var(--spin)) rotateZ(360deg) var(--orientation); }
}
.gesture { display: flex; align-items: center; justify-content: center; gap: 9px; margin: 0; font-size: .9375rem; color: #f8daba; text-align: center; text-shadow: 0 2px 5px #170a18; }
.chevron { width: 8px; height: 8px; border-top: 2px solid #ffc77b; border-left: 2px solid #ffc77b; transform: translateY(2px) rotate(45deg); }
.chevron.down { transform: translateY(-2px) rotate(225deg); }

.controls { width: 100%; display: grid; justify-items: center; gap: 8px; padding-top: 10px; }
.actions { display: grid; width: 100%; grid-template-columns: 1.45fr 1fr; gap: 10px; }
.actions button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 55px;
  padding: 8px;
  border-radius: 14px;
  font-weight: 750;
}
.roll { border: 1px solid #ffde9b; background: linear-gradient(#ffd684, #ed9e49); color: #3b1e26; box-shadow: inset 0 2px 1px #fff2b9, 0 4px #98491f, 0 7px 15px #100815a0; font-size: 1.125rem; }
.lock { border: 1px solid #a36b8b; background: linear-gradient(#593753, #361f39); box-shadow: inset 0 1px #ae74928c, 0 4px #24132a; font-size: .9375rem; }
.lock[aria-pressed="true"] { border-color: #e6aa69; color: #ffe1a8; background: linear-gradient(#72463f, #472735); }
.actions button:disabled { filter: saturate(.55); opacity: .65; }
.actions button:not(:disabled):active { transform: translateY(2px); }
.die-icon { width: 17px; height: 17px; border: 2px solid currentColor; border-radius: 4px; transform: rotate(-12deg); background: radial-gradient(circle, currentColor 0 1.7px, transparent 2px); }
.lock-icon { position: relative; width: 13px; height: 10px; border: 1.5px solid currentColor; margin-top: 5px; border-radius: 2px; }
.lock-icon::before { content: ""; position: absolute; width: 6px; height: 6px; border: 1.5px solid currentColor; border-bottom: 0; border-radius: 5px 5px 0 0; left: 2px; bottom: 7px; transform-origin: right bottom; transform: rotate(19deg); }
.lock[aria-pressed="true"] .lock-icon::before { transform: none; }
.preferences { display: flex; flex-wrap: wrap; justify-content: center; column-gap: 8px; width: 100%; }
.preference { display: flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 6px; border: 0; background: transparent; color: #edcfdd; font-size: .875rem; }
.preference-icon { color: #ffc77b; font-size: 1.15rem; line-height: 1; }
.switch { width: 32px; height: 18px; padding: 2px; border: 1px solid #a77a99; border-radius: 10px; background: #432b46; }
.switch::before { content: ""; display: block; width: 12px; height: 12px; border-radius: 50%; background: #e7bfdb; transition: transform 160ms; }
.preference[aria-pressed="true"] .switch { background: #aa672f; border-color: #f4bf78; }
.preference[aria-pressed="true"] .switch::before { transform: translateX(14px); background: #fff1d1; }
.motion-note { min-height: 1.3em; margin: -5px 0 0; color: #dbc0cf; font-size: .8125rem; line-height: 1.3; text-align: center; }

@media (min-height: 700px) {
  .game { padding-top: max(35px, calc(env(safe-area-inset-top) + 16px)); padding-bottom: max(24px, calc(env(safe-area-inset-bottom) + 12px)); }
  .play { padding-bottom: 21px; }
  .table { max-height: 510px; }
}
@media (max-height: 650px) {
  .game { grid-template-rows: auto minmax(290px, 1fr) auto; padding-top: max(15px, env(safe-area-inset-top)); }
  h1 { font-size: clamp(2.7rem, 11vw, 3.5rem); }
  .play { padding: 13px 0 9px; }
  .table { --travel: 96px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .table[data-shaking] .dice { opacity: 0; }
}
