:root {
  color-scheme: dark;
  --bg: oklch(0.055 0 0);
  --surface: oklch(0.095 0 0);
  --surface-raised: oklch(0.145 0 0);
  --line: oklch(0.25 0 0);
  --line-strong: oklch(0.42 0 0);
  --ink: oklch(0.96 0 0);
  --muted: oklch(0.68 0.018 200);
  --primary: oklch(0.72 0.12 198);
  --primary-dark: oklch(0.44 0.11 200);
  --pip: oklch(0.075 0 0);
  --die-edge: oklch(0.82 0.01 200);
  --danger: oklch(0.67 0.14 25);
  --radius: 16px;
  --radius-tight: 10px;
  --die-size: clamp(74px, 16vw, 128px);
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-in-out: cubic-bezier(0.77, 0, 0.175, 1);
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100dvh;
  margin: 0;
  background:
    linear-gradient(180deg, oklch(0.09 0 0), transparent 38%),
    var(--bg);
  color: var(--ink);
}

button,
input {
  font: inherit;
}

button {
  border: 0;
}

.app-shell {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: clamp(18px, 4vw, 48px);
}

.hero-panel {
  width: min(1120px, 100%);
  min-height: min(780px, calc(100dvh - 48px));
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  gap: clamp(18px, 3vw, 32px);
  padding: clamp(18px, 4vw, 42px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background:
    linear-gradient(145deg, oklch(0.135 0 0), oklch(0.065 0 0) 62%),
    var(--surface);
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
}

.kicker,
.history-label,
.result-label,
.control-note {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
}

.kicker {
  letter-spacing: 0;
}

h1 {
  margin: 4px 0 0;
  font-size: clamp(2.15rem, 5vw, 4.8rem);
  line-height: 0.98;
  letter-spacing: -0.03em;
  text-wrap: balance;
}

.rng-state {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: oklch(0.075 0 0);
  white-space: nowrap;
}

.rng-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--primary);
  box-shadow: 0 0 0 4px oklch(0.72 0.12 198 / 0.13);
}

.is-rolling .rng-dot {
  animation: pulse-dot 680ms var(--ease-out) infinite;
}

.dice-stage {
  min-height: 290px;
  display: grid;
  place-items: center;
  perspective: 900px;
  overflow: hidden;
  border-radius: var(--radius);
}

.dice-field {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  align-content: center;
  flex-wrap: wrap;
  gap: clamp(18px, 3vw, 34px);
  padding: 18px 0;
}

.die-wrap {
  --dx: 0px;
  --dy: 0px;
  --tilt: 0deg;
  width: var(--die-size);
  height: var(--die-size);
  display: grid;
  place-items: center;
  transform-style: preserve-3d;
  will-change: transform;
}

.die-wrap.is-throwing {
  animation: die-lift var(--throw-duration, 980ms) var(--ease-in-out);
}

.die {
  --rx: 0deg;
  --ry: 0deg;
  --rz: 0deg;
  position: relative;
  width: var(--die-size);
  height: var(--die-size);
  transform-style: preserve-3d;
  transform: rotateX(var(--rx)) rotateY(var(--ry)) rotateZ(var(--rz));
  transition: transform var(--throw-duration, 980ms) var(--ease-in-out);
  will-change: transform;
}

.die-face {
  position: absolute;
  inset: 0;
  display: grid;
  border: 1px solid oklch(0.78 0.01 200);
  border-radius: 18%;
  background:
    radial-gradient(circle at 30% 24%, oklch(1 0 0), oklch(0.88 0.008 220) 68%),
    oklch(0.96 0 0);
  box-shadow:
    inset 0 1px 0 oklch(1 0 0 / 0.82),
    inset 0 -10px 22px oklch(0.54 0.015 210 / 0.18);
  backface-visibility: hidden;
}

.face-1 {
  transform: translateZ(calc(var(--die-size) / 2));
}

.face-6 {
  transform: rotateY(180deg) translateZ(calc(var(--die-size) / 2));
}

.face-3 {
  transform: rotateY(90deg) translateZ(calc(var(--die-size) / 2));
}

.face-4 {
  transform: rotateY(-90deg) translateZ(calc(var(--die-size) / 2));
}

.face-2 {
  transform: rotateX(90deg) translateZ(calc(var(--die-size) / 2));
}

.face-5 {
  transform: rotateX(-90deg) translateZ(calc(var(--die-size) / 2));
}

.pip {
  position: absolute;
  width: 16%;
  aspect-ratio: 1;
  border-radius: 999px;
  background: var(--pip);
  box-shadow:
    inset 0 2px 3px oklch(0 0 0 / 0.8),
    0 1px 0 oklch(1 0 0 / 0.26);
  transform: translate(-50%, -50%);
}

.pip[data-pos="tl"] {
  left: 27%;
  top: 27%;
}

.pip[data-pos="tc"] {
  left: 50%;
  top: 27%;
}

.pip[data-pos="tr"] {
  left: 73%;
  top: 27%;
}

.pip[data-pos="cl"] {
  left: 27%;
  top: 50%;
}

.pip[data-pos="cc"] {
  left: 50%;
  top: 50%;
}

.pip[data-pos="cr"] {
  left: 73%;
  top: 50%;
}

.pip[data-pos="bl"] {
  left: 27%;
  top: 73%;
}

.pip[data-pos="bc"] {
  left: 50%;
  top: 73%;
}

.pip[data-pos="br"] {
  left: 73%;
  top: 73%;
}

.control-deck {
  display: grid;
  grid-template-columns: minmax(220px, 0.9fr) minmax(180px, 0.7fr) minmax(250px, 1fr);
  align-items: end;
  gap: 14px;
}

.count-control {
  display: grid;
  gap: 9px;
}

.count-control label {
  color: var(--ink);
  font-size: 0.92rem;
  font-weight: 700;
}

.stepper {
  display: grid;
  grid-template-columns: 44px minmax(74px, 1fr) 44px;
  min-height: 52px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius-tight);
  background: oklch(0.075 0 0);
}

.step-button,
.count-input {
  min-width: 0;
  color: var(--ink);
  background: transparent;
}

.step-button {
  cursor: pointer;
  font-size: 1.28rem;
  transition:
    background 160ms var(--ease-out),
    transform 120ms var(--ease-out);
}

.step-button:hover {
  background: oklch(0.17 0 0);
}

.step-button:active,
.roll-button:active {
  transform: scale(0.97);
}

.count-input {
  width: 100%;
  border: 0;
  border-inline: 1px solid var(--line);
  text-align: center;
  font-size: 1.05rem;
  font-weight: 750;
  outline: none;
  appearance: textfield;
}

.count-input::-webkit-outer-spin-button,
.count-input::-webkit-inner-spin-button {
  margin: 0;
  appearance: none;
}

.count-input:focus-visible,
.roll-button:focus-visible,
.step-button:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 3px;
}

.roll-button {
  min-height: 52px;
  cursor: pointer;
  border-radius: var(--radius-tight);
  color: oklch(0.05 0 0);
  background: linear-gradient(180deg, oklch(0.98 0 0), oklch(0.78 0.012 205));
  font-size: 1rem;
  font-weight: 850;
  transition:
    transform 120ms var(--ease-out),
    filter 160ms var(--ease-out);
}

.roll-button:hover {
  filter: brightness(1.06);
}

.roll-button:disabled {
  cursor: wait;
  filter: brightness(0.72);
}

.result-panel {
  min-height: 52px;
  display: grid;
  grid-template-columns: 0.7fr 1fr;
  border: 1px solid var(--line);
  border-radius: var(--radius-tight);
  overflow: hidden;
  background: oklch(0.075 0 0);
}

.result-panel > div {
  display: grid;
  align-content: center;
  gap: 2px;
  padding: 10px 14px;
}

.result-panel > div + div {
  border-left: 1px solid var(--line);
}

.result-panel strong {
  overflow: hidden;
  color: var(--ink);
  font-size: 1.05rem;
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-strip {
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 14px;
  overflow: hidden;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.history-strip ol {
  display: flex;
  flex: 1;
  gap: 8px;
  min-width: 0;
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-x: auto;
  scrollbar-width: thin;
}

.history-strip li {
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 11px;
  color: var(--ink);
  background: oklch(0.075 0 0);
  font-size: 0.85rem;
}

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

@keyframes die-lift {
  0% {
    transform: translate3d(0, 0, 0) rotate(0deg);
  }
  30% {
    transform: translate3d(var(--dx), calc(var(--dy) - 34px), 0) rotate(var(--tilt));
  }
  68% {
    transform: translate3d(calc(var(--dx) * -0.35), calc(var(--dy) * 0.35), 0) rotate(calc(var(--tilt) * -0.35));
  }
  100% {
    transform: translate3d(0, 0, 0) rotate(0deg);
  }
}

@keyframes pulse-dot {
  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.52;
    transform: scale(0.82);
  }
}

@media (max-width: 820px) {
  .app-shell {
    place-items: stretch;
    padding: 12px;
  }

  .hero-panel {
    min-height: calc(100dvh - 24px);
  }

  .topbar {
    align-items: flex-start;
  }

  .control-deck {
    grid-template-columns: 1fr;
  }

  .result-panel {
    grid-template-columns: 1fr 1.15fr;
  }
}

@media (max-width: 520px) {
  :root {
    --die-size: clamp(62px, 27vw, 96px);
  }

  .hero-panel {
    padding: 16px;
    gap: 18px;
  }

  .topbar {
    flex-direction: column;
  }

  h1 {
    font-size: 2.4rem;
  }

  .dice-stage {
    min-height: 260px;
  }

  .history-strip {
    align-items: flex-start;
    flex-direction: column;
  }

  .history-strip ol {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
