:root {
  --bg1: #7c3aed;
  --bg2: #ec4899;
  --bg3: #f59e0b;
  --card-radius: 20px;
  --text-dark: #2d1b4e;
  color-scheme: light;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

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

body {
  font-family: -apple-system, "Segoe UI Rounded", "Segoe UI", "Comic Sans MS", ui-rounded, system-ui, sans-serif;
  color: var(--text-dark);
  background: linear-gradient(135deg, var(--bg1), var(--bg2), var(--bg3));
  background-size: 300% 300%;
  animation: bgShift 18s ease infinite;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

@keyframes bgShift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

.app-header {
  text-align: center;
  padding: 1.4rem 1rem 0.6rem;
  color: white;
  text-shadow: 0 2px 8px rgba(0,0,0,0.25);
}

.app-header h1 {
  margin: 0;
  font-size: clamp(1.8rem, 6vw, 2.6rem);
  letter-spacing: 0.02em;
}

.logo-dot {
  display: inline-block;
  animation: wiggle 3s ease-in-out infinite;
}

@keyframes wiggle {
  0%, 100% { transform: rotate(0deg); }
  10% { transform: rotate(-12deg); }
  20% { transform: rotate(10deg); }
  30% { transform: rotate(-6deg); }
  40% { transform: rotate(0deg); }
}

.subtitle {
  margin: 0.2rem 0 0;
  font-size: 0.95rem;
  opacity: 0.9;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem;
  gap: 1.2rem;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

.dice-container {
  display: flex;
  flex-wrap: wrap;
  gap: 1.1rem;
  justify-content: center;
  align-items: flex-start;
  width: 100%;
}

.die-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  animation: popIn 0.35s cubic-bezier(.34,1.56,.64,1);
}

@keyframes popIn {
  from { transform: scale(0.4) rotate(-15deg); opacity: 0; }
  to { transform: scale(1) rotate(0deg); opacity: 1; }
}

.die-card.removing {
  animation: popOut 0.25s ease forwards;
}

@keyframes popOut {
  to { transform: scale(0.3) rotate(20deg); opacity: 0; }
}

.die-face {
  --die-color: #7c3aed;
  position: relative;
  width: clamp(96px, 26vw, 140px);
  height: clamp(96px, 26vw, 140px);
  border: none;
  border-radius: 26%;
  background: #fff;
  box-shadow:
    0 10px 0 color-mix(in srgb, var(--die-color) 55%, black 15%),
    0 14px 20px rgba(0,0,0,0.28);
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.08s ease;
}

.die-face:active {
  transform: translateY(6px);
  box-shadow:
    0 4px 0 color-mix(in srgb, var(--die-color) 55%, black 15%),
    0 6px 10px rgba(0,0,0,0.25);
}

.die-face::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 26%;
  border: 6px solid var(--die-color);
  pointer-events: none;
}

.die-face-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.die-face.is-rolling {
  animation: rollShake 0.5s linear infinite;
}

@keyframes rollShake {
  0% { transform: rotate(0deg) scale(1); }
  20% { transform: rotate(-14deg) scale(1.05); }
  40% { transform: rotate(10deg) scale(0.96); }
  60% { transform: rotate(-8deg) scale(1.04); }
  80% { transform: rotate(12deg) scale(0.97); }
  100% { transform: rotate(0deg) scale(1); }
}

.die-face.just-landed {
  animation: landPop 0.4s cubic-bezier(.34,1.56,.64,1);
}

@keyframes landPop {
  0% { transform: scale(1.35) rotate(8deg); }
  60% { transform: scale(0.92) rotate(-3deg); }
  100% { transform: scale(1) rotate(0deg); }
}

/* Pip grid for d6 */
.pip-grid {
  width: 74%;
  height: 74%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 4%;
}

.pip {
  border-radius: 50%;
  background: var(--die-color);
  width: 100%;
  height: 100%;
}

.pip.empty {
  background: transparent;
}

/* Number / letter face */
.face-number {
  font-size: clamp(2.2rem, 9vw, 3.4rem);
  font-weight: 800;
  color: var(--die-color);
  line-height: 1;
  user-select: none;
}

.face-number.is-letter {
  font-size: clamp(2.6rem, 10vw, 3.8rem);
}

.die-type-label {
  font-size: 0.8rem;
  font-weight: 700;
  color: white;
  background: rgba(0,0,0,0.22);
  padding: 0.1rem 0.55rem;
  border-radius: 999px;
  text-shadow: none;
}

.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 10, 40, 0.55);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 30;
}

.settings-overlay[hidden] {
  display: none;
}

.settings-modal {
  background: #fbf9ff;
  color: var(--text-dark);
  border-radius: 24px;
  width: min(440px, 100%);
  max-height: min(85vh, 680px);
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 60px rgba(0,0,0,0.45);
  animation: popIn 0.3s cubic-bezier(.34,1.56,.64,1);
  overflow: hidden;
}

.settings-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.1rem 1.3rem;
  background: linear-gradient(135deg, var(--bg1), var(--bg2) 70%);
  color: white;
}

.settings-modal-header h2 {
  margin: 0;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  text-shadow: 0 2px 6px rgba(0,0,0,0.25);
}

.wobble-icon {
  display: inline-block;
  animation: wiggle 3s ease-in-out infinite;
}

.modal-close {
  border: none;
  background: rgba(255,255,255,0.25);
  color: white;
  min-width: 52px;
  height: 34px;
  padding: 0 0.9rem;
  border-radius: 999px;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease;
}

.modal-close:hover {
  background: rgba(255,255,255,0.4);
}

.modal-close:active {
  transform: scale(0.9);
}

.settings-list {
  overflow-y: auto;
  padding: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.settings-item {
  --item-color: #7c3aed;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.9rem 1rem;
  border-radius: 16px;
  background: color-mix(in srgb, var(--item-color) 10%, white);
  border-left: 6px solid var(--item-color);
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.settings-item-header {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.settings-item-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: 0 0 0 3px white, 0 1px 4px rgba(0,0,0,0.25);
}

.settings-item-title {
  font-weight: 800;
  color: color-mix(in srgb, var(--item-color) 70%, black 25%);
}

.settings-item-badge {
  margin-left: auto;
  font-size: 0.72rem;
  font-weight: 700;
  color: white;
  background: var(--item-color);
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
}

.settings-row {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  font-weight: 700;
}

.settings-row[hidden] {
  display: none;
}

.settings-label {
  color: color-mix(in srgb, var(--item-color) 65%, black 20%);
}

.settings-row select,
.settings-row input[type="number"],
.settings-row textarea {
  font: inherit;
  font-family: inherit;
  padding: 0.5rem 0.6rem;
  border-radius: 10px;
  border: 2px solid color-mix(in srgb, var(--item-color) 30%, white);
  background: white;
  width: 100%;
  font-weight: 600;
  color: var(--text-dark);
}

.settings-row select:focus,
.settings-row input[type="number"]:focus,
.settings-row textarea:focus {
  outline: none;
  border-color: var(--item-color);
}

.settings-row textarea {
  resize: vertical;
  font-size: 0.85rem;
}

.color-picker-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.color-presets {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.color-preset-btn {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  transition: transform 0.1s ease;
}

.color-preset-btn:active {
  transform: scale(0.88);
}

.color-preset-btn.is-active {
  border-color: white;
  box-shadow: 0 0 0 2px var(--item-color);
}

.settings-row input[type="color"] {
  width: 30px;
  height: 30px;
  border: 2px solid color-mix(in srgb, var(--item-color) 30%, white);
  border-radius: 50%;
  padding: 0;
  cursor: pointer;
  overflow: hidden;
  flex-shrink: 0;
}

.settings-row input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 0;
}

.settings-row input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 50%;
}

.toolbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.9rem;
  margin-top: 0.5rem;
  width: 100%;
}

.btn-large {
  font-size: 1.15rem;
  padding: 0.9rem 1.8rem;
}

.stepper {
  display: flex;
  align-items: center;
  background: rgba(255,255,255,0.18);
  border-radius: 999px;
  padding: 0.3rem;
  gap: 0.3rem;
}

.stepper-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.9);
  color: #7c3aed;
  font-size: 1.3rem;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
  transition: transform 0.1s ease;
}

.stepper-btn:active {
  transform: scale(0.9);
}

.stepper-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  box-shadow: none;
}

.stepper-count {
  min-width: 90px;
  text-align: center;
  color: white;
  font-weight: 700;
  font-size: 0.95rem;
}

.toolbar-utility {
  display: grid;
  grid-template-columns: repeat(2, minmax(120px, 1fr));
  gap: 0.6rem;
  width: 100%;
  max-width: 340px;
}

.toolbar-utility .btn {
  padding: 0.6rem 0.8rem;
  font-size: 0.85rem;
  white-space: nowrap;
}

.btn {
  font: inherit;
  font-weight: 700;
  border: none;
  border-radius: 999px;
  padding: 0.75rem 1.3rem;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  transition: transform 0.1s ease;
}

.btn:active {
  transform: scale(0.94);
}

.btn-primary {
  background: white;
  color: #7c3aed;
  font-size: 1.05rem;
}

.btn-secondary {
  background: rgba(255,255,255,0.85);
  color: #2d1b4e;
}

.btn-ghost {
  background: rgba(255,255,255,0.15);
  color: white;
}

select.btn {
  appearance: none;
  -webkit-appearance: none;
  text-align: center;
  text-align-last: center;
}

select.btn option {
  color: #2d1b4e;
}

.sum-display {
  background: rgba(255,255,255,0.9);
  color: var(--text-dark);
  font-weight: 800;
  font-size: 1.1rem;
  padding: 0.4rem 1.1rem;
  border-radius: 999px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.app-footer {
  text-align: center;
  color: rgba(255,255,255,0.85);
  font-size: 0.8rem;
  padding: 0.8rem;
}

.app-footer p {
  margin: 0.2rem 0;
}

.app-footer a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.2rem;
  transform: translate(-50%, 20px);
  background: rgba(0,0,0,0.8);
  color: white;
  padding: 0.6rem 1rem;
  border-radius: 999px;
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 20;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}
