* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  /* Confetti colors for dots */
  --dot-color-1: #4A3AC0;  /* Purple */
  --dot-color-2: #45E8A3;  /* Teal/Mint Green */
  --dot-color-3: #FFB300;  /* Orange/Gold */
  --dot-color-4: #3D6BB6;  /* Blue */
  --dot-color-5: #FF4848;  /* Red/Coral */
  --dot-color-6: #18B1FF;  /* Bright Blue */

  /* Light theme colors */
  --bg-primary: #ffffff;
  --bg-secondary: #f5f5f5;
  --border-color: #e0e0e0;
  --text-primary: #333;
  --text-secondary: rgba(0, 0, 0, 0.6);
  --panel-bg: #ffffff;
  --control-bg: rgba(255, 255, 255, 0.9);
  --control-text: #333;
  --modal-bg: #ffffff;
  --modal-overlay: rgba(0, 0, 0, 0.9);
  --sum-text: #333;
  --sum-shadow: rgba(0, 0, 0, 0.1);
}

[data-theme="dark"] {
  /* Dark theme colors */
  --bg-primary: #1a1a1a;
  --bg-secondary: #2a2a2a;
  --border-color: #3a3a3a;
  --text-primary: #e0e0e0;
  --text-secondary: rgba(255, 255, 255, 0.6);
  --panel-bg: #2a2a2a;
  --control-bg: rgba(42, 42, 42, 0.9);
  --control-text: #e0e0e0;
  --modal-bg: #2a2a2a;
  --modal-overlay: rgba(0, 0, 0, 0.95);
  --sum-text: #e0e0e0;
  --sum-shadow: rgba(0, 0, 0, 0.5);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  overflow: hidden;
  height: 100vh;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  transition: background 0.3s ease;
}

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

/* Dice Grid - Vertical Stack */
.dice-grid {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0;
  background: var(--bg-primary);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overflow: hidden;
  position: relative;
  z-index: 10;
  transition: background 0.3s ease, opacity 0.6s ease-out, visibility 0.6s ease-out, transform 0.6s ease-out;
}

/* Die Panel */
.die-panel {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  transition: filter 0.1s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  min-height: 0;
}

.die-panel:active {
  filter: brightness(0.95);
}

.die-panel.locked {
  opacity: 0.6;
}

.die-panel.locked::after {
  content: '';
  position: absolute;
  top: 12px;
  right: 12px;
  width: 24px;
  height: 24px;
  background: rgba(255,255,255,0.8);
  border-radius: 50%;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%23333' stroke-width='2.5'%3E%3Crect x='3' y='11' width='18' height='11' rx='2' ry='2'%3E%3C/rect%3E%3Cpath d='M7 11V7a5 5 0 0 1 10 0v4'%3E%3C/path%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: center;
}

/* Panel Background */
.die-panel {
  background: var(--panel-bg);
  transition: background 0.3s ease, opacity 0.5s ease-out, transform 0.5s ease-out;
}


/* Fixed size box for dots */
.dots-box {
  width: min(280px, 65vw);
  height: 100%;
  max-height: min(280px, 65vw);
  display: grid;
  position: relative;
  z-index: 1;
  margin: auto;
  padding: 16px;
}

/* Adjust dots size based on number of dice */
.dice-grid:has(.die-panel:nth-child(3)) .dots-box {
  width: min(200px, 60vw);
  max-height: min(200px, 60vw);
  padding: 12px;
}

.dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dots-box {
  width: min(240px, 62vw);
  max-height: min(240px, 62vw);
  padding: 14px;
}

/* Individual Dot - consistent size */
.dot {
  width: 100%;
  height: 100%;
  max-width: 90px;
  max-height: 90px;
  min-width: 35px;
  min-height: 35px;
  border-radius: 50%;
  justify-self: center;
  align-self: center;
}

/* Scale dots down when there are 3 dice */
.dice-grid:has(.die-panel:nth-child(3)) .dot {
  max-width: 55px;
  max-height: 55px;
  min-width: 25px;
  min-height: 25px;
}

/* Scale dots for 2 dice */
.dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dot {
  max-width: 70px;
  max-height: 70px;
  min-width: 30px;
  min-height: 30px;
}

/* Dot colors - vibrant colors for each dice value */
.die-panel[data-color="1"] .dot { background: var(--dot-color-1); }
.die-panel[data-color="2"] .dot { background: var(--dot-color-2); }
.die-panel[data-color="3"] .dot { background: var(--dot-color-3); }
.die-panel[data-color="4"] .dot { background: var(--dot-color-4); }
.die-panel[data-color="5"] .dot { background: var(--dot-color-5); }
.die-panel[data-color="6"] .dot { background: var(--dot-color-6); }

/* Grid layouts for each value - classic dice patterns */
.dots-box[data-value="1"] {
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
}

.dots-box[data-value="2"] {
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: clamp(12px, 3vw, 24px);
}
.dots-box[data-value="2"] .dot:nth-child(1) { grid-area: 1 / 1; }
.dots-box[data-value="2"] .dot:nth-child(2) { grid-area: 3 / 3; }

.dots-box[data-value="3"] {
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: clamp(12px, 3vw, 24px);
}
.dots-box[data-value="3"] .dot:nth-child(1) { grid-area: 1 / 1; }
.dots-box[data-value="3"] .dot:nth-child(2) { grid-area: 2 / 2; }
.dots-box[data-value="3"] .dot:nth-child(3) { grid-area: 3 / 3; }

.dots-box[data-value="4"] {
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: clamp(12px, 3vw, 24px);
}
.dots-box[data-value="4"] .dot:nth-child(1) { grid-area: 1 / 1; }
.dots-box[data-value="4"] .dot:nth-child(2) { grid-area: 1 / 3; }
.dots-box[data-value="4"] .dot:nth-child(3) { grid-area: 3 / 1; }
.dots-box[data-value="4"] .dot:nth-child(4) { grid-area: 3 / 3; }

.dots-box[data-value="5"] {
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: clamp(12px, 3vw, 24px);
}
.dots-box[data-value="5"] .dot:nth-child(1) { grid-area: 1 / 1; }
.dots-box[data-value="5"] .dot:nth-child(2) { grid-area: 1 / 3; }
.dots-box[data-value="5"] .dot:nth-child(3) { grid-area: 2 / 2; }
.dots-box[data-value="5"] .dot:nth-child(4) { grid-area: 3 / 1; }
.dots-box[data-value="5"] .dot:nth-child(5) { grid-area: 3 / 3; }

.dots-box[data-value="6"] {
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: clamp(12px, 3vw, 24px);
}
.dots-box[data-value="6"] .dot:nth-child(1) { grid-area: 1 / 1; }
.dots-box[data-value="6"] .dot:nth-child(2) { grid-area: 1 / 3; }
.dots-box[data-value="6"] .dot:nth-child(3) { grid-area: 2 / 1; }
.dots-box[data-value="6"] .dot:nth-child(4) { grid-area: 2 / 3; }
.dots-box[data-value="6"] .dot:nth-child(5) { grid-area: 3 / 1; }
.dots-box[data-value="6"] .dot:nth-child(6) { grid-area: 3 / 3; }

/* Sum Display - Overlay at top */
.sum-display {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
  pointer-events: none;
  z-index: 25;
  transition: opacity 0.5s ease-out, visibility 0.5s ease-out, transform 0.5s ease-out;
  transform: translateY(0);
}

.sum-display.hidden {
  opacity: 0;
  visibility: hidden;
  transform: translateY(-20px);
}

.sum-value {
  font-size: clamp(3rem, 15vw, 8rem);
  font-weight: 800;
  color: var(--sum-text);
  text-shadow: 0 2px 10px var(--sum-shadow);
  line-height: 1;
  transition: color 0.3s ease;
}

/* Controls - Bottom bar */
.controls {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  padding: 20px;
  padding-bottom: max(20px, env(safe-area-inset-bottom));
  z-index: 20;
  pointer-events: auto;
}

.control-btn {
  width: 50px;
  height: 50px;
  border: none;
  border-radius: 50%;
  background: var(--control-bg);
  color: var(--control-text);
  font-size: 1.5rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s, background 0.3s, color 0.3s;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.control-btn:active {
  transform: scale(0.92);
  opacity: 0.8;
}

.control-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  pointer-events: none;
}

.control-btn svg {
  width: 24px;
  height: 24px;
}

/* Lottie Confetti Overlay */
#confetti-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  min-width: 100vw;
  min-height: 100vh;
  min-height: 100dvh; /* Dynamic viewport height for mobile */
  pointer-events: none;
  z-index: 15;
  display: none;
  overflow: hidden;
}

#confetti-container.active {
  display: block;
}

#confetti-container svg {
  position: absolute !important;
  top: 50% !important;
  left: 50% !important;
  transform: translate(-50%, -50%) !important;
  width: 100vw !important;
  height: 100vh !important;
  height: 100dvh !important;
  min-width: 100% !important;
  min-height: 100% !important;
}

/* Hide dice when rolling */
.dice-grid.rolling {
  opacity: 0;
  visibility: hidden;
  transform: scale(0.95);
}

/* Settings Modal */
.settings-modal, .welcome-modal {
  position: fixed;
  inset: 0;
  background: var(--modal-overlay);
  display: none;
  justify-content: center;
  align-items: center;
  z-index: 100;
  pointer-events: auto;
  transition: background 0.3s ease;
}

.settings-modal.open, .welcome-modal.open {
  display: flex;
}

.modal-content {
  background: var(--modal-bg);
  padding: 30px;
  border-radius: 24px;
  min-width: 340px;
  max-width: 480px;
  width: 90%;
  color: var(--text-primary);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
  transition: background 0.3s ease, color 0.3s ease;
}

.modal-content h2 {
  margin-bottom: 24px;
  text-align: center;
  font-weight: 600;
}

.color-legend {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  transition: background 0.3s ease, border 0.3s ease;
}

.legend-swatch {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
}

/* Toggle Switch */
.toggle-setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px;
  border-radius: 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  cursor: pointer;
  transition: background 0.3s ease, border 0.3s ease;
}

.toggle-label {
  font-size: 0.95rem;
  color: var(--text-primary);
  font-weight: 500;
  transition: color 0.3s ease;
}

.toggle-switch {
  position: relative;
  width: 48px;
  height: 26px;
  background: #ccc;
  border-radius: 13px;
  cursor: pointer;
  transition: background 0.3s;
}

.toggle-switch.active {
  background: #4CAF50;
}

.toggle-slider {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  background: white;
  border-radius: 50%;
  transition: transform 0.3s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.toggle-switch.active .toggle-slider {
  transform: translateX(22px);
}

/* Theme Selector */
.theme-options {
  display: flex;
  gap: 8px;
}

.theme-option {
  flex: 1;
  padding: 10px;
  border: 2px solid var(--border-color);
  border-radius: 10px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  text-align: center;
  transition: all 0.3s;
}

.theme-option:hover {
  opacity: 0.8;
}

.theme-option.active {
  border-color: #4CAF50;
  background: rgba(76, 175, 80, 0.1);
  color: #4CAF50;
}

.setting-section {
  margin-bottom: 24px;
}

.setting-label {
  display: block;
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-bottom: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  transition: color 0.3s ease;
  font-weight: 600;
}

/* Collapsible sections */
.collapsible-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  user-select: none;
  transition: color 0.2s ease;
}

.collapsible-header:hover {
  color: var(--primary-color);
}

.chevron-icon {
  width: 16px;
  height: 16px;
  stroke: var(--text-secondary);
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: transform 0.3s ease, stroke 0.2s ease;
}

.collapsible-header:hover .chevron-icon {
  stroke: var(--primary-color);
}

.collapsible-section.collapsed .chevron-icon {
  transform: rotate(-90deg);
}

.collapsible-content {
  max-height: 500px;
  overflow: hidden;
  transition: max-height 0.3s ease, opacity 0.3s ease, margin-top 0.3s ease;
  margin-top: 12px;
}

.collapsible-section.collapsed .collapsible-content {
  max-height: 0;
  opacity: 0;
  margin-top: 0;
}

.dice-count-setting {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
}

.setting-btn {
  width: 44px;
  height: 44px;
  border: 1px solid var(--border-color);
  border-radius: 50%;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 1.5rem;
  cursor: pointer;
  transition: background 0.3s, color 0.3s, border 0.3s;
}

.setting-btn:hover {
  opacity: 0.8;
}

.setting-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  pointer-events: none;
}

.dice-count-value {
  font-size: 2rem;
  font-weight: 700;
  min-width: 60px;
  text-align: center;
  color: var(--text-primary);
  transition: color 0.3s ease;
}

.modal-close {
  margin-top: 24px;
  width: 100%;
  padding: 14px;
  border: 1px solid var(--border-color);
  border-radius: 12px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.3s, color 0.3s, border 0.3s;
}

.modal-close:hover {
  opacity: 0.8;
}

.refresh-btn {
  width: 100%;
  padding: 10px 16px;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.3s, color 0.3s, border 0.3s, transform 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.refresh-btn:hover {
  opacity: 0.8;
}

.refresh-btn:active {
  transform: scale(0.98);
}

.refresh-btn svg {
  width: 16px;
  height: 16px;
}

.refresh-btn.checking svg {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* Welcome Modal Specific Styles */
.welcome-content {
  text-align: center;
  margin: 20px 0 30px 0;
}

.welcome-text {
  font-size: 1.2rem;
  color: var(--text-primary);
  margin-bottom: 16px;
  line-height: 1.5;
  font-weight: 500;
}

.welcome-subtext {
  font-size: 1rem;
  color: var(--text-secondary);
  line-height: 1.6;
}

/* Mobile optimizations */
@media (max-width: 480px) {
  body {
    -webkit-tap-highlight-color: transparent;
  }

  .control-btn {
    width: 48px;
    height: 48px;
    font-size: 1.3rem;
  }

  .control-btn svg {
    width: 22px;
    height: 22px;
  }

  .controls {
    gap: 12px;
    padding: 16px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  .sum-display {
    padding: 16px;
    padding-top: max(16px, env(safe-area-inset-top));
  }

  .sum-value {
    font-size: clamp(2.5rem, 12vw, 4.5rem);
  }

  .dots-box {
    width: min(240px, 65vw);
    max-height: min(240px, 65vw);
    padding: 14px;
  }

  .dice-grid:has(.die-panel:nth-child(3)) .dots-box {
    width: min(170px, 58vw);
    max-height: min(170px, 58vw);
    padding: 10px;
  }

  .dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dots-box {
    width: min(210px, 62vw);
    max-height: min(210px, 62vw);
    padding: 12px;
  }

  .dot {
    max-width: 60px;
    max-height: 60px;
    min-width: 28px;
    min-height: 28px;
  }

  .dice-grid:has(.die-panel:nth-child(3)) .dot {
    max-width: 42px;
    max-height: 42px;
    min-width: 20px;
    min-height: 20px;
  }

  .dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dot {
    max-width: 50px;
    max-height: 50px;
    min-width: 24px;
    min-height: 24px;
  }

  .modal-content {
    padding: 24px;
    margin: 16px;
    max-width: calc(100vw - 32px);
  }

  .modal-content h2 {
    font-size: 1.3rem;
  }

  .setting-label {
    font-size: 0.8rem;
  }

  .dice-count-value {
    font-size: 1.6rem;
  }

  .setting-btn {
    width: 40px;
    height: 40px;
  }

  .color-legend {
    gap: 8px;
  }

  .legend-item {
    padding: 10px;
    gap: 10px;
  }

  .legend-swatch {
    width: 28px;
    height: 28px;
  }

  .welcome-text {
    font-size: 1.1rem;
  }

  .welcome-subtext {
    font-size: 0.9rem;
  }
}

/* Tablet */
@media (min-width: 481px) and (max-width: 767px) {
  .dots-box {
    width: min(270px, 60vw);
    max-height: min(270px, 60vw);
    padding: 16px;
  }

  .dice-grid:has(.die-panel:nth-child(3)) .dots-box {
    width: min(190px, 55vw);
    max-height: min(190px, 55vw);
    padding: 12px;
  }

  .dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dots-box {
    width: min(230px, 58vw);
    max-height: min(230px, 58vw);
    padding: 14px;
  }

  .dot {
    max-width: 75px;
    max-height: 75px;
    min-width: 35px;
    min-height: 35px;
  }

  .dice-grid:has(.die-panel:nth-child(3)) .dot {
    max-width: 50px;
    max-height: 50px;
    min-width: 24px;
    min-height: 24px;
  }

  .dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dot {
    max-width: 62px;
    max-height: 62px;
    min-width: 30px;
    min-height: 30px;
  }

  .control-btn {
    width: 52px;
    height: 52px;
  }
}

/* Desktop */
@media (min-width: 768px) {
  .dots-box {
    width: min(320px, 50vw);
    max-height: min(320px, 50vw);
    padding: 18px;
  }

  .dice-grid:has(.die-panel:nth-child(3)) .dots-box {
    width: min(220px, 45vw);
    max-height: min(220px, 45vw);
    padding: 14px;
  }

  .dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dots-box {
    width: min(270px, 48vw);
    max-height: min(270px, 48vw);
    padding: 16px;
  }

  .dot {
    max-width: 95px;
    max-height: 95px;
    min-width: 42px;
    min-height: 42px;
  }

  .dice-grid:has(.die-panel:nth-child(3)) .dot {
    max-width: 60px;
    max-height: 60px;
    min-width: 28px;
    min-height: 28px;
  }

  .dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dot {
    max-width: 78px;
    max-height: 78px;
    min-width: 35px;
    min-height: 35px;
  }

  .controls {
    gap: 20px;
  }
}

/* Landscape mobile */
@media (max-width: 767px) and (orientation: landscape) {
  .dots-box {
    width: min(220px, 45vh);
    max-height: min(220px, 45vh);
    padding: 12px;
  }

  .dice-grid:has(.die-panel:nth-child(3)) .dots-box {
    width: min(150px, 35vh);
    max-height: min(150px, 35vh);
    padding: 8px;
  }

  .dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dots-box {
    width: min(185px, 40vh);
    max-height: min(185px, 40vh);
    padding: 10px;
  }

  .sum-display {
    padding: 12px;
  }

  .sum-value {
    font-size: clamp(2rem, 8vh, 3.5rem);
  }

  .controls {
    padding: 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }

  .dot {
    max-width: 52px;
    max-height: 52px;
    min-width: 24px;
    min-height: 24px;
  }

  .dice-grid:has(.die-panel:nth-child(3)) .dot {
    max-width: 36px;
    max-height: 36px;
    min-width: 18px;
    min-height: 18px;
  }

  .dice-grid:has(.die-panel:nth-child(2):not(:nth-child(3))) .dot {
    max-width: 44px;
    max-height: 44px;
    min-width: 20px;
    min-height: 20px;
  }
}
