:root {
  --bg: #f4f0e6;
  --panel: #ffffff;
  --border: #a8907a;
  --ink: #333333;
  --brand: #7a5e9a;
  --brand-strong: #614a7e;
  --muted: #c9bcdc;
  --muted-bg: #e6e0f4;
  --radius-lg: 15px;
  --radius-md: 10px;
  --shadow-lg: 0 10px 20px rgba(0, 0, 0, 0.2);
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-7: 32px;
  --tap-min: 44px;
  --die-size: 48px;
  --die-icon: 30px;

  --support-offset: 12px;
  --support-minh: 44px;
  --support-gap-top: 10px;
}

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

html,
body {
  height: 100%;
}

body {
  font-family: "Georgia", serif;
  background-color: var(--bg);
  color: var(--ink);
  margin: 0;
  padding: var(--space-4);
}

#app {
  background-color: var(--panel);
  border: 2px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-6);
  padding-bottom: calc(
    var(--space-6) + var(--support-offset) + var(--support-minh) +
      var(--support-gap-top)
  );
  width: 100%;
  max-width: 560px;
  box-shadow: var(--shadow-lg);
  margin: 0 auto var(--space-7) auto;
  position: relative;
}

h1 {
  color: var(--brand);
  text-align: center;
  margin: 0 0 var(--space-5) 0;
  line-height: 1.2;
  font-size: 1.5rem;
}

#status-box {
  background-color: var(--muted-bg);
  border: 1px solid var(--muted);
  padding: var(--space-3);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-5);
  font-size: 1rem;
  text-align: center;
}

#dice-container {
  min-height: 120px;
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  gap: var(--space-2);
}

@keyframes scatter-animation {
  0% {
    opacity: 1;
    transform: translateY(0) rotate(0deg) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(var(--dx, 100px), var(--dy, 200px))
      rotate(var(--rot, 360deg)) scale(var(--s, 0.5));
  }
}

.falling {
  position: absolute;
  animation-name: scatter-animation;
  animation-timing-function: ease-out;
  animation-fill-mode: forwards;
}

.die {
  width: var(--die-size);
  height: var(--die-size);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  transition: transform 0.12s ease-out, box-shadow 0.12s ease-out;
  will-change: transform, opacity;
}

.die::before {
  content: "";
  position: absolute;
  inset: 6px 10px 16px;
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.7),
    rgba(255, 255, 255, 0)
  );
  border-radius: 10px;
  pointer-events: none;
  z-index: 0;
}

.die .fa-solid {
  font-size: var(--die-icon);
  color: #2f2650;
  filter: drop-shadow(0 2px 4px rgba(38, 22, 68, 0.35));
  position: relative;
  z-index: 1;
}

.die.shake {
  transform: translateY(-2px) rotate(-1deg);
}

#controls {
  margin-top: var(--space-5);
  display: grid;
  gap: var(--space-4);
}

.control-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: var(--space-3);
}

.control-label {
  font-weight: 700;
}

input[type="number"] {
  padding: var(--space-3);
  min-height: var(--tap-min);
  width: 100%;
  max-width: 120px;
  border: 1px solid #ccc;
  border-radius: 8px;
  text-align: center;
  font-size: 1rem;
}

.control-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-3);
}

button {
  padding: var(--space-3) var(--space-4);
  min-height: var(--tap-min);
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: background-color 0.2s, opacity 0.2s, filter 0.2s, transform 0.02s;
  font-weight: 700;
  font-size: 1rem;
  touch-action: manipulation;
}

button:active {
  transform: translateY(1px);
}

#stack-btn {
  background-color: var(--brand);
  color: #ffffff;
}

#stack-btn:hover:not(:disabled):not(.visually-disabled) {
  background-color: var(--brand-strong);
}

#reset-btn {
  background-color: var(--muted);
  color: var(--ink);
}

#reset-btn:hover:not(:disabled) {
  background-color: #b7a9ce;
}

button:disabled {
  background-color: #e0e0e0;
  cursor: not-allowed;
  color: #666;
}

#stack-btn.visually-disabled {
  background-color: #e0ddea;
  color: #b9b2ca;
  cursor: not-allowed;
  filter: grayscale(25%);
  opacity: 0.7;
  pointer-events: none;
}

#message-area {
  margin-top: var(--space-4);
  padding: var(--space-3);
  border-radius: 8px;
  font-weight: 700;
  text-align: center;
  font-size: 0.95rem;
}

.message-success {
  background-color: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.message-fail {
  background-color: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}

.fallen-wrapper {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space-3);
  margin-top: var(--space-2);
}

.fallen-group {
  border: 1px dashed var(--muted);
  background: #f6f3ff;
  border-radius: 10px;
  padding: var(--space-3);
}

.group-title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: bold;
  color: #4d3a75;
  margin-bottom: var(--space-2);
}

.group-title .fa-solid {
  font-size: 22px;
  filter: none;
  color: #4d3a75;
}

.group-dice {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  justify-content: flex-start;
}

.support-row {
  position: absolute;
  right: var(--support-offset);
  bottom: var(--support-offset);
  display: block;
}

.bmc-inline {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  text-decoration: none;
  background: #ffffff;
  color: #2f2650;
  border: 1px solid #e4e0f0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1;
  opacity: 0.9;
  min-height: var(--support-minh);
}

.bmc-inline:hover,
.bmc-inline:focus-visible {
  opacity: 1;
  border-color: #d5cee9;
  outline: none;
}

.bmc-icon {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
  fill: currentColor;
}

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

@media (min-width: 480px) {
  :root {
    --die-size: 52px;
    --die-icon: 34px;
  }
  h1 {
    font-size: 1.75rem;
  }
  .control-grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 768px) {
  :root {
    --die-size: 54px;
    --die-icon: 36px;
  }
  #app {
    padding: var(--space-7);
    padding-bottom: calc(
      var(--space-7) + var(--support-offset) + var(--support-minh) +
        var(--support-gap-top)
    );
  }
  h1 {
    font-size: 2rem;
  }
  #controls {
    grid-template-columns: 1fr;
    gap: var(--space-5);
  }
  .control-row {
    grid-template-columns: auto 140px;
    justify-content: center;
  }
  .control-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: var(--space-4);
  }
}

@media (max-width: 380px) {
  .bmc-text {
    display: none;
  }
  .bmc-inline {
    padding: 10px;
  }
}
