/* Base: reset, fluid type, shared components (buttons, fields, dialogs, dice, toasts). */

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

html {
  /* Fluid root size: grows with the screen but always starts from the user's own
     font-size setting (100%) and is expressed in rem everywhere else. */
  font-size: clamp(100%, calc(0.25rem + 0.5vmin + 0.32vmax), 180%);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  height: 100%;
  background: var(--bg);
  color-scheme: light;
}
html.theme-retro { color-scheme: dark; }

body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  font-family: var(--font-body);
  line-height: 1.4;
  color: var(--on-bg);
  background: var(--bg-image), var(--bg);
  background-attachment: fixed;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app { min-height: 100dvh; }

h1, h2, h3, .display {
  font-family: var(--font-display);
  font-weight: var(--display-weight);
  text-transform: var(--display-transform);
  letter-spacing: var(--display-tracking);
  line-height: 1.15;
  margin: 0;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
img, svg { display: block; }
p { margin: 0; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  border-radius: var(--radius);
}

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

[hidden] { display: none !important; }

/* ---------- Buttons ---------- */
.btn {
  --_bg: var(--btn-bg);
  --_fg: var(--btn-text);
  --_bd: var(--btn-border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  min-height: 2.75rem;
  min-width: 2.75rem;
  padding: 0.55em 1.1em;
  border: 2px solid var(--_bd);
  border-radius: var(--radius);
  background: var(--_bg);
  color: var(--_fg);
  font-weight: 700;
  line-height: 1.1;
  text-align: center;
  box-shadow: var(--shadow);
  transition: transform 0.12s var(--ease-pop), background-color 0.15s, box-shadow 0.15s;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.btn:active:not(:disabled) { transform: translateY(1px) scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none; }
.btn-primary { --_bg: var(--accent); --_fg: var(--accent-text); --_bd: var(--accent-2); }
.btn-ghost { --_bg: transparent; --_fg: currentColor; --_bd: transparent; box-shadow: none; }
.btn-block { width: 100%; }
.btn-lg { font-size: 1.15rem; min-height: 3.25rem; padding-inline: 1.4em; }
.btn .icon { width: 1.25em; height: 1.25em; }

.theme-retro .btn { font-family: var(--font-display); letter-spacing: 0.05em; text-transform: uppercase; }
.theme-retro .btn-primary { box-shadow: 0 0 0.8rem rgba(255, 62, 165, 0.6); }
.theme-playful .btn { border-width: 3px; }

@media (hover: hover) {
  .btn:hover:not(:disabled) { filter: brightness(1.06); box-shadow: var(--shadow-lg); }
  .theme-retro .btn:hover:not(:disabled) { background: var(--btn-text); color: var(--btn-bg); }
  .theme-retro .btn-primary:hover:not(:disabled) { background: var(--accent-text); color: var(--accent); }
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 2.75rem;
  height: 2.75rem;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: var(--chip-bg);
  color: var(--chip-text);
  position: relative;
  touch-action: manipulation;
}
.icon-btn .icon { width: 1.4rem; height: 1.4rem; }
@media (hover: hover) { .icon-btn:hover { filter: brightness(1.15); } }

.icon {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.badge {
  position: absolute;
  top: -0.2rem; right: -0.2rem;
  min-width: 1.2rem; height: 1.2rem;
  padding: 0 0.3rem;
  border-radius: var(--radius-pill);
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.75rem;
  font-weight: 800;
  line-height: 1.2rem;
  text-align: center;
}

/* ---------- Cards / panels ---------- */
.panel {
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: clamp(1rem, 3vmin, 2rem);
}
.theme-retro .panel { border: 2px solid var(--border); }
.theme-playful .panel { border: 3px solid var(--btn-border); }

/* ---------- Fields ---------- */
.field { display: grid; gap: 0.35rem; }
.field > label, .field-label { font-weight: 700; font-size: 0.95rem; }
.input, .select {
  min-height: 2.75rem;
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
.input:focus-visible, .select:focus-visible { outline-offset: 0; }
.input-code { text-transform: uppercase; letter-spacing: 0.3em; font-family: var(--font-num); font-size: 1.3rem; text-align: center; }

/* Segmented control (radio group) */
.seg {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0;
  margin: 0;
  border: 0;
}
.seg legend { font-weight: 700; font-size: 0.95rem; margin-bottom: 0.35rem; padding: 0; }
.seg label {
  position: relative;
  flex: 1 1 auto;
  display: grid;
  place-items: center;
  min-height: 2.75rem;
  min-width: 2.75rem;
  padding: 0.35rem 0.8rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-weight: 700;
  text-align: center;
  cursor: pointer;
}
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg label:has(input:checked) { background: var(--accent); color: var(--accent-text); border-color: var(--accent-2); }
.seg label:has(input:focus-visible) { outline: 3px solid var(--focus); outline-offset: 2px; }
.seg label:has(input:disabled) { opacity: 0.55; cursor: default; }

.switch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 2.75rem;
  font-weight: 700;
  cursor: pointer;
}
.switch input {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  width: 3.2rem;
  height: 1.9rem;
  margin: 0;
  border-radius: var(--radius-pill);
  background: var(--surface-line);
  border: 2px solid var(--border);
  position: relative;
  cursor: pointer;
  transition: background 0.15s;
}
.switch input::after {
  content: '';
  position: absolute;
  top: 0.15rem; left: 0.15rem;
  width: 1.35rem; height: 1.35rem;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
  transition: transform 0.18s var(--ease-pop);
}
.switch input:checked { background: var(--accent); border-color: var(--accent-2); }
.switch input:checked::after { transform: translateX(1.3rem); }

/* Color swatches */
.swatches { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.swatch {
  width: 2.75rem; height: 2.75rem;
  min-width: 44px; min-height: 44px;
  border-radius: 50%;
  border: 3px solid var(--surface);
  background: var(--c);
  box-shadow: 0 0 0 2px var(--border);
  padding: 0;
}
.swatch[aria-checked='true'] { box-shadow: 0 0 0 3px var(--text); }
.swatch[aria-checked='true']::after { content: '✓'; color: #fff; font-weight: 900; text-shadow: 0 1px 2px #000; }
.swatch:disabled { opacity: 0.25; }

/* ---------- Dialogs ---------- */
dialog.sheet {
  border: 0;
  padding: 0;
  max-width: min(34rem, calc(100vw - 1.5rem));
  width: 100%;
  max-height: calc(100dvh - 1.5rem - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  background: transparent;
  color: var(--text);
  overflow: visible;
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog.sheet > .panel {
  max-height: inherit;
  overflow: auto;
  overscroll-behavior: contain;
  display: grid;
  gap: 1rem;
}
dialog.sheet[open] { animation: pop-in calc(0.22s * var(--anim-scale)) var(--ease-pop); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.dialog-head h2 { font-size: 1.4rem; }
.dialog-head .icon-btn { background: var(--surface-2); color: var(--text); }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; justify-content: flex-end; }

.rules-text { display: grid; gap: 0.6rem; font-size: 0.95rem; }
.rules-text h3 { font-size: 1.05rem; margin-top: 0.4rem; }
.rules-text ul { margin: 0; padding-left: 1.2rem; display: grid; gap: 0.25rem; }

/* ---------- Dice ---------- */
.die {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 1;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: 0;
  border-radius: 18%;
  background: none;
  color: inherit;
  touch-action: manipulation;
  transition: transform 0.18s var(--ease-pop);
  -webkit-user-select: none;
  user-select: none;
}
.die-svg { width: 100%; height: 100%; overflow: visible; filter: drop-shadow(var(--die-shadow)); }
.die-body { fill: var(--die-bg); stroke: var(--die-edge); stroke-width: 2.5; }
.die-shine { fill: var(--die-shine); }
.pip { fill: var(--die-pip); }
.die[data-value='1'] .pip { fill: var(--die-pip-1); }

.die.held { transform: translateY(-6%); }
.die.held .die-body { stroke: var(--held-ring); stroke-width: 7; }
.die .held-tag {
  position: absolute;
  bottom: -0.15rem;
  left: 50%;
  transform: translate(-50%, 50%);
  padding: 0.05rem 0.4rem;
  border-radius: var(--radius-pill);
  background: var(--held-label-bg);
  color: var(--held-label-text);
  font-size: clamp(0.6rem, 18cqi, 0.8rem);
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
}
.theme-retro .die .held-tag { font-family: var(--font-display); }
.die.held .held-tag { opacity: 1; }
.die.unrolled .die-svg { opacity: 0.45; filter: grayscale(0.6); }
.die:disabled { cursor: default; }

.die.rolling .die-svg { animation: tumble calc(0.55s * var(--anim-scale)) var(--ease-pop); }
.die.rolling:nth-child(2) .die-svg { animation-delay: 0.03s; }
.die.rolling:nth-child(3) .die-svg { animation-delay: 0.06s; }
.die.rolling:nth-child(4) .die-svg { animation-delay: 0.02s; }
.die.rolling:nth-child(5) .die-svg { animation-delay: 0.05s; }
.die.rattling .die-svg { animation: rattle 0.12s linear infinite; }
.theme-playful .die.rolling .die-svg { animation-name: tumble-bouncy; }

@keyframes tumble {
  0% { transform: translateY(-18%) rotate(-160deg) scale(0.85); }
  60% { transform: translateY(4%) rotate(12deg) scale(1.04); }
  100% { transform: none; }
}
@keyframes tumble-bouncy {
  0% { transform: translateY(-40%) rotate(-220deg) scale(0.7); }
  50% { transform: translateY(8%) rotate(20deg) scale(1.12, 0.9); }
  75% { transform: translateY(-6%) rotate(-6deg) scale(0.97, 1.04); }
  100% { transform: none; }
}
@keyframes rattle {
  0%, 100% { transform: translate(0, 0) rotate(0); }
  25% { transform: translate(-4%, 2%) rotate(-7deg); }
  75% { transform: translate(4%, -2%) rotate(7deg); }
}
@keyframes pop-in {
  from { transform: scale(0.94); opacity: 0; }
  to { transform: none; opacity: 1; }
}

/* ---------- Toasts & effects ---------- */
.toasts {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 0.75rem);
  transform: translateX(-50%);
  z-index: 100;
  display: grid;
  gap: 0.5rem;
  width: max-content;
  max-width: calc(100vw - 2rem);
  pointer-events: none;
}
.toast {
  padding: 0.6rem 1rem;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  font-weight: 700;
  text-align: center;
  animation: pop-in 0.2s var(--ease-pop);
  transition: opacity 0.35s, transform 0.35s;
}
.toast.out { opacity: 0; transform: translateY(-0.5rem); }

.fx-layer { position: fixed; inset: 0; pointer-events: none; z-index: 90; overflow: hidden; }
.celebrate {
  position: absolute;
  left: 50%; top: 40%;
  transform: translate(-50%, -50%);
  padding: 0.6em 1.1em;
  border-radius: var(--radius-lg);
  background: var(--accent);
  color: var(--accent-text);
  font-family: var(--font-display);
  font-weight: var(--display-weight);
  text-transform: var(--display-transform);
  font-size: clamp(1.6rem, 6vmin, 4rem);
  white-space: nowrap;
  box-shadow: var(--shadow-lg);
  animation: celebrate 2.2s var(--ease-pop) forwards;
}
.celebrate.small { font-size: clamp(1.2rem, 4vmin, 2.6rem); background: var(--good); color: #fff; }
.theme-retro .celebrate { text-shadow: 0 0 0.4em currentColor; }
@keyframes celebrate {
  0% { transform: translate(-50%, -50%) scale(0.3) rotate(-8deg); opacity: 0; }
  15% { transform: translate(-50%, -50%) scale(1.1) rotate(2deg); opacity: 1; }
  25% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { transform: translate(-50%, -70%) scale(1); opacity: 0; }
}
.confetti {
  position: absolute;
  top: -5vh;
  width: 0.7rem; height: 1rem;
  background: var(--c);
  border-radius: 2px;
  animation: fall var(--d, 2.4s) linear forwards;
}
.theme-retro .confetti { border-radius: 0; width: 0.6rem; height: 0.6rem; }
@keyframes fall {
  to { transform: translate(var(--x, 0), 110vh) rotate(var(--r, 540deg)); }
}

.boot { display: grid; place-items: center; min-height: 100dvh; }
.boot-title { font-family: var(--font-display); font-size: 3rem; opacity: 0.6; }

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