* { box-sizing: border-box; }
:root {
  color-scheme: dark;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #000;
  color: #f7f7f7;
  -webkit-font-smoothing: antialiased;
}
body { margin: 0; overscroll-behavior: none; }
.app {
  height: 100vh;
  height: 100svh;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  background: #000;
}
.stage {
  display: grid;
  min-height: 0;
  padding: calc(20px + env(safe-area-inset-top)) max(24px, env(safe-area-inset-left)) 38px max(24px, env(safe-area-inset-right));
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  cursor: grab;
  outline: none;
}
.app[data-input="keyboard"] .stage:focus-visible { outline: 1px solid #777; outline-offset: -10px; }
.app[data-state="tracking"] .stage { cursor: grabbing; }
.app[data-state="rolling"] .stage { cursor: default; }
.dice-area {
  --dice-size: 144px;
  display: grid;
  grid-template-columns: var(--dice-size);
  grid-template-rows: var(--dice-size);
  align-content: center;
  justify-content: center;
  align-items: center;
  justify-items: center;
  gap: 48px;
  min-width: 0;
  pointer-events: none;
}
.dice-area[data-count="2"] { --dice-size: clamp(86px, 30vw, 128px); grid-template-rows: repeat(2, var(--dice-size)); }
.dice-area[data-count="3"] { --dice-size: clamp(74px, 24vw, 104px); grid-template-columns: repeat(2, var(--dice-size)); grid-template-rows: repeat(2, var(--dice-size)); gap: 56px; }
.dice-area[data-count="3"] .die:first-child { grid-column: 1 / -1; }
.die { width: var(--dice-size); height: var(--dice-size); perspective: 900px; }
.cube {
  --half: calc(var(--dice-size) / 2);
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);
  /* Keep the 3D layer stable when a gesture starts or a roll finishes. */
  will-change: transform;
}
.face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: calc(var(--dice-size) * .18);
  border-radius: 0;
  border: 1px solid #ddd;
  color: #111;
  background: #f9f9f9;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.front { transform: translateZ(var(--half)); }
.back { transform: rotateY(180deg) translateZ(var(--half)); background: #eee; }
.right { transform: rotateY(90deg) translateZ(var(--half)); background: #d5d5d5; border-color: #c3c3c3; }
.left { transform: rotateY(-90deg) translateZ(var(--half)); background: #e1e1e1; }
.top { transform: rotateX(90deg) translateZ(var(--half)); background: #ededed; }
.bottom { transform: rotateX(-90deg) translateZ(var(--half)); background: #c9c9c9; }
.pips { width: 100%; height: 100%; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); gap: 12%; }
.pip { width: 100%; aspect-ratio: 1; background: #111; border-radius: 50%; }
.face-value { font-size: calc(var(--dice-size) * .47); font-weight: 500; line-height: 1; font-variant-numeric: tabular-nums; }
.settings {
  width: min(calc(100% - 32px), 358px);
  margin-inline: auto;
  padding: 26px 0 calc(24px + env(safe-area-inset-bottom));
  border-top: 1px solid #222;
  display: grid;
  grid-template-columns: minmax(0, 3fr) 1px minmax(0, 4fr);
  align-items: center;
  gap: 10px;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.setting { display: grid; grid-template-columns: 20px minmax(0, 1fr); align-items: center; gap: 6px; }
.setting-icon { display: block; width: 20px; height: 20px; color: #aaa; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.settings-divider { width: 1px; height: 24px; background: #333; }
.segments { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px; padding: 3px; border-radius: 13px; background: #151515; }
.segments button {
  appearance: none;
  -webkit-appearance: none;
  min-width: 0;
  min-height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: #aaa;
  font: 500 16px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.segments button[aria-pressed="true"] { background: #f9f9f9; color: #111; }
.app[data-input="keyboard"] .segments button:focus-visible { outline: 2px solid #aaa; outline-offset: 2px; }
.segments button:disabled { cursor: default; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
.error { position: fixed; top: max(20px, env(safe-area-inset-top)); left: 24px; right: 24px; margin: 0 auto; max-width: 420px; padding: 16px; background: #151515; color: #eee; border-radius: 10px; text-align: center; font-size: 14px; line-height: 1.5; }
[hidden] { display: none !important; }
@media (max-width: 360px) {
  .settings { width: calc(100% - 24px); gap: 8px; }
  .setting { grid-template-columns: 18px minmax(0, 1fr); gap: 4px; }
  .setting-icon { width: 18px; height: 18px; }
  .segments button { font-size: 15px; }
  .dice-area[data-count="2"] { row-gap: 36px; }
  .dice-area[data-count="3"] { gap: 46px; }
}
@media (max-height: 580px) {
  .stage { padding-top: max(12px, env(safe-area-inset-top)); padding-bottom: 16px; }
  .dice-area { --dice-size: 104px; }
  .dice-area[data-count="2"] { --dice-size: clamp(72px, 24vw, 96px); row-gap: 40px; }
  .dice-area[data-count="3"] { --dice-size: clamp(62px, 20vw, 78px); gap: 44px; }
  .settings { padding-top: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
}
