:root {
  --bg-start: #05070f;
  --bg-mid: #0a1020;
  --bg-end: #090814;
  --panel: rgba(12, 18, 35, 0.72);
  --text: #f4f8ff;
  --muted: #aeb9d4;
  --glow-color: #20ff8a;
  --accent-color: #62ffb2;
  --bg-gradient: linear-gradient(135deg, #06140c, #0e2438 48%, #19102d);
  --border-glow: rgba(32, 255, 138, 0.58);
  --dice-face: #0e1b2a;
  --text-glow: rgba(98, 255, 178, 0.75);
  --danger: #ff4d82;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.46);
  --radius: 8px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html {
  min-height: 100%;
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  margin: 0;
  background: radial-gradient(circle at 18% 12%, rgba(32, 255, 138, 0.14), transparent 28rem), radial-gradient(circle at 82% 24%, rgba(0, 210, 255, 0.12), transparent 24rem), linear-gradient(145deg, var(--bg-start), var(--bg-mid) 48%, var(--bg-end));
  color: var(--text);
  font-family: "Trebuchet MS", Arial, sans-serif;
  line-height: 1.5;
  overflow-x: hidden;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -3;
  background: var(--bg-gradient);
  opacity: 0.5;
  transition: background 240ms ease;
}

button { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

:focus-visible {
  outline: 3px solid var(--accent-color);
  outline-offset: 4px;
  box-shadow: 0 0 0 7px rgba(255, 255, 255, 0.16);
}

.skip-link {
  position: fixed;
  top: 1rem;
  left: 1rem;
  z-index: 20;
  transform: translateY(-180%);
  background: #ffffff;
  color: #06101f;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  font-weight: 800;
}

.skip-link:focus { transform: translateY(0); }

.starfield,
.starfield::before,
.starfield::after {
  position: fixed;
  inset: 0;
  z-index: -2;
  pointer-events: none;
  background-image: radial-gradient(circle, rgba(255, 255, 255, 0.85) 0 1px, transparent 1.5px), radial-gradient(circle, rgba(98, 255, 178, 0.65) 0 1px, transparent 1.5px);
  background-position: 0 0, 48px 70px;
  background-size: 120px 120px, 170px 170px;
  opacity: 0.32;
}

.starfield::before {
  content: "";
  opacity: 0.24;
  transform: translateY(20px) scale(1.15);
  animation: drift-stars 22s linear infinite;
}

.starfield::after {
  content: "";
  opacity: 0.18;
  transform: scale(1.4);
  animation: drift-stars 34s linear infinite reverse;
}

.site-header { padding: clamp(3rem, 9vw, 5.5rem) 1rem 1rem; }

.header-shell,
.app-shell,
.site-footer {
  width: min(1160px, calc(100% - 2rem));
  margin: 0 auto;
}

.header-shell {
  display: grid;
  gap: 0.65rem;
}

.kicker,
.panel-label {
  margin: 0;
  color: var(--accent-color);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  text-shadow: 0 0 18px var(--text-glow);
}

h1, h2, p { margin-top: 0; }

h1 {
  max-width: 920px;
  margin-bottom: 0;
  font-size: clamp(2.55rem, 8vw, 6.35rem);
  line-height: 0.95;
  letter-spacing: 0;
  text-shadow: 0 0 14px var(--text-glow), 0 0 42px rgba(255, 255, 255, 0.14);
}

h2 {
  margin-bottom: 0;
  font-size: clamp(1.25rem, 3vw, 1.8rem);
  line-height: 1.1;
}

.tagline {
  max-width: 700px;
  margin-bottom: 0;
  color: var(--muted);
  font-size: clamp(1rem, 2vw, 1.22rem);
}

.app-shell {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  padding: 1.25rem 0 2rem;
}

.glass-panel {
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: var(--radius);
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.08), transparent), var(--panel);
  box-shadow: var(--shadow), inset 0 0 28px rgba(255, 255, 255, 0.04), 0 0 30px rgba(32, 255, 138, 0.08);
  backdrop-filter: blur(20px);
}

.glass-panel::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  box-shadow: inset 0 0 0 1px var(--border-glow), inset 0 0 50px rgba(255, 255, 255, 0.035);
  opacity: 0.65;
}

.control-panel,
.history-panel,
.roll-stage { padding: clamp(1rem, 3vw, 1.5rem); }

.section-heading,
.history-header,
.stage-topline {
  position: relative;
  z-index: 1;
}

.section-heading {
  display: grid;
  gap: 0.3rem;
  margin-bottom: 1.1rem;
}

.dice-picker {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.65rem;
}

.die-option,
.clear-button,
.roll-button {
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  font-weight: 900;
  transition: transform 160ms ease, border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

.die-option {
  min-height: 54px;
  background: rgba(255, 255, 255, 0.06);
}

.die-option:hover,
.die-option.is-selected {
  border-color: var(--accent-color);
  background: rgba(255, 255, 255, 0.12);
  box-shadow: 0 0 22px rgba(32, 255, 138, 0.18), inset 0 0 16px rgba(255, 255, 255, 0.06);
  transform: translateY(-2px);
}

.selected-readout {
  position: relative;
  z-index: 1;
  margin-top: 1rem;
  color: var(--muted);
}

.selected-readout strong {
  color: var(--text);
  text-shadow: 0 0 16px var(--text-glow);
}

.roll-stage {
  display: grid;
  gap: 1.1rem;
}

.stage-topline,
.history-header {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
  justify-content: space-between;
}

.theme-chip {
  flex: 0 0 auto;
  max-width: 48%;
  padding: 0.42rem 0.65rem;
  border: 1px solid var(--border-glow);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 900;
  text-align: right;
  text-shadow: 0 0 16px var(--text-glow);
  background: rgba(0, 0, 0, 0.22);
}

.dice-arena {
  position: relative;
  display: grid;
  min-height: clamp(260px, 42vw, 390px);
  place-items: center;
  perspective: 1000px;
}

.burst-ring {
  position: absolute;
  width: min(72vw, 380px);
  aspect-ratio: 1;
  border: 2px solid var(--border-glow);
  border-radius: 50%;
  filter: blur(0.2px);
  opacity: 0.4;
  transform: scale(0.72);
  box-shadow: 0 0 28px var(--glow-color), inset 0 0 36px var(--glow-color);
}

.dice-card {
  position: relative;
  display: grid;
  width: min(72vw, 280px);
  aspect-ratio: 1;
  place-items: center;
  padding: 1rem;
  border: 2px solid var(--border-glow);
  border-radius: 18%;
  background: radial-gradient(circle at 28% 22%, rgba(255, 255, 255, 0.22), transparent 24%), linear-gradient(145deg, rgba(255, 255, 255, 0.12), transparent 38%), var(--dice-face);
  box-shadow: 0 0 28px var(--glow-color), 0 32px 52px rgba(0, 0, 0, 0.52), inset -16px -18px 28px rgba(0, 0, 0, 0.34), inset 12px 12px 30px rgba(255, 255, 255, 0.08);
  transform: rotateX(12deg) rotateY(-16deg);
  transform-style: preserve-3d;
}

.dice-card::before,
.dice-card::after {
  content: "";
  position: absolute;
  border-radius: inherit;
  pointer-events: none;
}

.dice-card::before {
  inset: 12%;
  border: 1px solid rgba(255, 255, 255, 0.22);
}

.dice-card::after {
  inset: -10px;
  background: radial-gradient(circle, var(--glow-color), transparent 62%);
  filter: blur(20px);
  opacity: 0.18;
  z-index: -1;
}

.die-type,
.die-status {
  position: absolute;
  color: var(--muted);
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.die-type {
  top: 1rem;
  left: 1rem;
  font-size: 0.86rem;
}

.die-status {
  right: 1rem;
  bottom: 1rem;
  font-size: 0.68rem;
}

.die-result-visual {
  color: var(--text);
  font-size: clamp(4.8rem, 20vw, 8.7rem);
  font-weight: 900;
  line-height: 1;
  text-shadow: 0 0 12px var(--text-glow), 0 0 44px var(--glow-color);
}

.dice-card.is-rolling { animation: roll-die 880ms cubic-bezier(0.18, 0.84, 0.32, 1.12); }
.dice-card.is-snapping .die-result-visual { animation: snap-result 280ms ease-out; }
.dice-card.is-critical { animation: critical-pulse 900ms ease-out; }
.dice-arena.has-burst .burst-ring { animation: burst-ring 800ms ease-out; }

.result-panel {
  position: relative;
  z-index: 1;
  min-height: 112px;
  padding: 1rem;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
  background: rgba(0, 0, 0, 0.24);
}

.result-label {
  margin-bottom: 0.15rem;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.result-value {
  margin-bottom: 0.25rem;
  color: var(--text);
  font-size: clamp(1.55rem, 4vw, 2.45rem);
  font-weight: 900;
  line-height: 1.1;
  text-shadow: 0 0 18px var(--text-glow);
}

.result-message {
  margin-bottom: 0;
  color: var(--muted);
}

.roll-button {
  position: relative;
  z-index: 1;
  min-height: 62px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.24), rgba(255, 255, 255, 0)), var(--accent-color);
  color: #04101a;
  font-size: 1.12rem;
  text-transform: uppercase;
  box-shadow: 0 0 24px var(--glow-color), inset 0 0 14px rgba(255, 255, 255, 0.35);
}

.roll-button:hover:not(:disabled) { transform: translateY(-2px) scale(1.01); }
.roll-button:disabled { cursor: wait; opacity: 0.68; }
.history-panel { align-self: start; }

.clear-button {
  min-height: 42px;
  padding: 0 0.8rem;
  background: rgba(255, 255, 255, 0.06);
}

.clear-button:hover {
  border-color: var(--danger);
  box-shadow: 0 0 18px rgba(255, 77, 130, 0.22);
}

.history-list {
  position: relative;
  z-index: 1;
  display: grid;
  gap: 0.65rem;
  margin: 1rem 0 0;
  padding: 0;
  list-style: none;
}

.history-list li {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 0.75rem;
  align-items: center;
  padding: 0.78rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.055);
}

.history-list .empty-history {
  display: block;
  grid-template-columns: none;
  color: var(--muted);
}

.history-die {
  color: var(--accent-color);
  font-weight: 900;
}

.history-result {
  color: var(--text);
  font-size: 1.18rem;
  font-weight: 900;
}

.history-note {
  color: var(--muted);
  font-size: 0.86rem;
  text-align: right;
}

.site-footer {
  padding: 1rem 0 2.25rem;
  color: var(--muted);
  font-size: 0.94rem;
}

.site-footer p { margin-bottom: 0; }

@keyframes drift-stars {
  from { background-position: 0 0, 48px 70px; }
  to { background-position: 240px 360px, 388px 410px; }
}

@keyframes roll-die {
  0% { transform: rotateX(12deg) rotateY(-16deg) scale(1); }
  35% { transform: rotateX(210deg) rotateY(160deg) rotateZ(18deg) scale(1.08); }
  70% { transform: rotateX(420deg) rotateY(330deg) rotateZ(-14deg) scale(0.96); }
  100% { transform: rotateX(732deg) rotateY(704deg) rotateZ(0deg) scale(1); }
}

@keyframes snap-result {
  0% { opacity: 0; transform: scale(0.55); }
  80% { transform: scale(1.18); }
  100% { opacity: 1; transform: scale(1); }
}

@keyframes critical-pulse {
  0%, 100% { box-shadow: 0 0 28px var(--glow-color), 0 32px 52px rgba(0, 0, 0, 0.52), inset -16px -18px 28px rgba(0, 0, 0, 0.34), inset 12px 12px 30px rgba(255, 255, 255, 0.08); }
  45% { box-shadow: 0 0 42px #ffffff, 0 0 86px var(--glow-color), 0 32px 52px rgba(0, 0, 0, 0.52), inset 0 0 34px rgba(255, 255, 255, 0.25); }
}

@keyframes burst-ring {
  0% { opacity: 0.8; transform: scale(0.36); }
  100% { opacity: 0; transform: scale(1.28); }
}

@media (min-width: 760px) {
  .app-shell {
    grid-template-columns: minmax(220px, 0.75fr) minmax(420px, 1.4fr);
    align-items: start;
  }

  .roll-stage { grid-row: span 2; }
}

@media (min-width: 1040px) {
  .app-shell { grid-template-columns: minmax(230px, 0.72fr) minmax(470px, 1.45fr) minmax(260px, 0.85fr); }
  .roll-stage { grid-row: auto; }
}

@media (max-width: 460px) {
  .dice-picker { grid-template-columns: repeat(2, 1fr); }
  .stage-topline, .history-header { display: grid; }
  .theme-chip { max-width: 100%; text-align: left; }
  .history-list li { grid-template-columns: 1fr auto; }
  .history-note { grid-column: 1 / -1; text-align: left; }
}

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

  .dice-card.is-rolling { animation: none; }
}