*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --text: #1c1c1c;
  --text-muted: #6b6b6b;
  --accent: #2e5c8a;
  --border: #e2ded5;
  --die-body: #1c1c1c;
  --die-face: #ffffff;
  --die-border: rgba(0, 0, 0, 0.12);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --flicker-duration: 600ms;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
}

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ===== Picker ===== */

.picker {
  flex: 1;
  padding: var(--space-6) var(--space-4);
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.picker__header {
  margin-bottom: var(--space-6);
}

.picker__title {
  font-size: 2rem;
  margin: 0 0 var(--space-2);
  letter-spacing: -0.01em;
}

.picker__subtitle {
  margin: 0;
  color: var(--text-muted);
  font-size: 1rem;
}

.picker__grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: var(--space-4);
}

.game-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-3);
  text-align: left;
  padding: var(--space-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  transition: transform 120ms ease, box-shadow 120ms ease;
  min-height: 160px;
  width: 100%;
}

.game-card:hover,
.game-card:focus-visible {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.game-card__preview {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.game-card__preview-die {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  position: relative;
  box-shadow: inset 0 0 0 1px var(--die-border);
}

.game-card__preview-die::after {
  content: '';
  position: absolute;
  inset: 0;
  background-image: radial-gradient(circle, var(--preview-face) 22%, transparent 24%);
  background-size: 50% 50%;
  background-position: center;
  background-repeat: no-repeat;
}

.game-card__name {
  font-size: 1.25rem;
  margin: 0;
  font-weight: 600;
}

.game-card__desc {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

/* ===== Game view ===== */

.game {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: var(--space-4) var(--space-4) calc(var(--space-6) + 80px);
  gap: var(--space-4);
}

.game__header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.game__back {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.9rem;
  border: 1px solid transparent;
}

.game__back:hover,
.game__back:focus-visible {
  color: var(--text);
  border-color: var(--border);
}

.game__title {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  flex: 1;
  min-width: 0;
}

.counter {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: var(--space-1);
}

.counter__btn {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  font-size: 1.2rem;
  color: var(--text);
  line-height: 1;
}

.counter__btn:hover:not(:disabled),
.counter__btn:focus-visible:not(:disabled) {
  background: var(--border);
}

.counter__btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.counter__value {
  min-width: 2ch;
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.counter__label {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* ===== Dice grid ===== */

.dice-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(72px, 1fr));
  gap: var(--space-3);
  margin-top: var(--space-2);
}

@media (min-width: 480px) {
  .dice-grid {
    grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
  }
}

.die {
  aspect-ratio: 1 / 1;
  min-width: 64px;
  min-height: 64px;
  background: var(--die-body);
  color: var(--die-face);
  border-radius: 14px;
  position: relative;
  box-shadow: var(--shadow-sm), inset 0 0 0 1px var(--die-border);
  transition: transform 160ms ease, opacity 160ms ease;
  will-change: transform;
  display: block;
  padding: 0;
  overflow: hidden;
}

.die:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.die__numeral {
  position: absolute;
  top: 8%;
  left: 10%;
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  opacity: 0.85;
}

.die__pips {
  position: absolute;
  inset: 18%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 4%;
}

.die__pip {
  background: var(--die-face);
  border-radius: 50%;
  width: 100%;
  height: 100%;
  place-self: center;
  aspect-ratio: 1 / 1;
  max-width: 70%;
  max-height: 70%;
}

.die__empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 1.8rem;
  font-weight: 600;
  opacity: 0.55;
}

.die--rolling {
  animation: die-shake var(--flicker-duration) ease-out;
}

.die--used {
  opacity: 0.4;
}

.die--used::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    135deg,
    transparent 46%,
    rgba(255, 255, 255, 0.55) 48%,
    rgba(255, 255, 255, 0.55) 52%,
    transparent 54%
  );
  pointer-events: none;
}

.die--used .die__check {
  position: absolute;
  bottom: 6%;
  right: 8%;
  font-size: 1rem;
  line-height: 1;
  font-weight: 700;
  opacity: 0.9;
}

.die__check {
  display: none;
}

@keyframes die-shake {
  0% {
    transform: scale(1) rotate(0);
  }
  15% {
    transform: scale(1.05) rotate(-2deg);
  }
  30% {
    transform: scale(1.05) rotate(2deg);
  }
  50% {
    transform: scale(1.04) rotate(-1.5deg);
  }
  70% {
    transform: scale(1.03) rotate(1deg);
  }
  100% {
    transform: scale(1) rotate(0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .die--rolling {
    animation: die-fade 150ms ease-out;
  }

  @keyframes die-fade {
    0% { opacity: 0.4; }
    100% { opacity: 1; }
  }
}

/* ===== Actions ===== */

.actions {
  position: sticky;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4) calc(var(--space-3) + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 75%, transparent);
  margin: 0 calc(var(--space-4) * -1);
}

.btn {
  flex: 1;
  padding: var(--space-4) var(--space-4);
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 1rem;
  min-height: 52px;
  transition: transform 100ms ease, background 160ms ease, opacity 160ms ease;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn--primary {
  background: var(--accent);
  color: var(--surface);
}

.btn--primary:hover:not(:disabled),
.btn--primary:focus-visible:not(:disabled) {
  filter: brightness(1.06);
}

.btn--secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn--secondary:hover:not(:disabled),
.btn--secondary:focus-visible:not(:disabled) {
  background: var(--border);
}

.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;
}
