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

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  color: #fff;
  overflow: hidden;
  height: 100dvh;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

:root {
  --primary: #E9D19C;
  --primary-second: #ffe9ba;
}

#screen-select::before, #screen-roll::before,
#screen-select::after, #screen-roll::after {
  content: ' ';
  display: block;
  position: absolute;
  width: 100%;
  height: 400px;
  opacity: 0.2;
  z-index: -1;
}

#screen-select::before {
  background: url('../assets/images/omg.png');
  background-position: top;
  background-size: 95%;
  bottom: 0;
}

#screen-roll::after {
  background: url('../assets/images/background-frame.png');
  background-position: center;
  background-size: contain;
  height: 100%;
  background-repeat: no-repeat;
  inset: 0;
  margin: auto;
}

#app {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: hidden;
  z-index: 15;
}

.screen {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.screen.active {
  display: flex;
}

#screen-select {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  z-index: 20;
  justify-content: space-between;
  padding: 20px 16px calc(env(safe-area-inset-bottom) + 16px);
}

.select-header {
  text-align: center;
  padding-top: 10px;
}

.select-header h1 {
  font-size: 1.8rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary), var(--primary-second));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: 2px;
}

.select-header p {
  color: #aaa;
  margin-top: 2px;
  font-size: 0.85rem;
}

.select-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
}

.select-summary {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  min-height: 32px;
  width: 100%;
  max-width: 400px;
}

.select-summary .summary-chip {
  display: flex;
  align-items: center;
  gap: 4px;
  background: rgb(0 0 0 / 14%);
  border: 3px solid #dbb2855c;
  padding: 4px 6px 4px 10px;
  font-size: 0.85rem;
  font-weight: 600;
}

.select-summary .summary-chip .chip-qty {
  color: var(--primary);
}

.select-summary .summary-chip .chip-del {
  background: none;
  border: none;
  color: rgba(255,255,255,0.4);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 2px;
  transition: color 0.15s;
}

.select-summary .summary-chip .chip-del:hover {
  color: var(--primary);
}

.dice-list {
  display: flex;
  gap: 16px;
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.15) transparent;
  overscroll-behavior-x: contain;
  justify-content: center;
}

.dice-list::-webkit-scrollbar {
  height: 4px;
}

.dice-list::-webkit-scrollbar-track {
  background: transparent;
}

.dice-list::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.15);
}

.dice-item {
  width: 108px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px 0 0;
}

.dice-item .dice-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  max-width: 35px;
  height: 65px;
  justify-content: flex-end;
}

.dice-item .dice-label svg {
  fill: #fff;
  width: 35px;
  height: auto;
}

.dice-item .dice-label span {
  font-size: 0.9rem;
  font-weight: 600;
  color: #aaa;
}

.dice-controls {
  display: flex;
  align-items: center;
  width: 100%;
  flex-direction: column;
  gap: 8px;
}

button.primary {
  position: relative;
  width: 240px;
  height: 64px;
  padding: 12px;
  border: 3px solid #dbb2855c;
  background: transparent;
  cursor: pointer;
  color: var(--primary);
  font-size: 1rem;
  font-weight: 700;
  z-index: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

button.primary.sm {
  width: 55px;
  height: 35px;
  border: none;
}

button.primary svg, 
button.secondary svg {
  position: absolute;
  inset: 0;
  margin: auto;
}

button.primary:hover span{
  color: #0f3460;
  z-index: 1;
}

button.secondary:hover span {
  color: #16293f;
  z-index: 1;
}

button.primary:hover svg {
  fill: var(--primary);
  z-index: -1;
}

button.secondary:hover svg {
  fill: #696666;
  z-index: -1;
}

button.primary:hover svg path, button.secondary:hover svg path {
  stroke-opacity: 1;
}

button.secondary {
  position: relative;
  width: 240px;
  height: 64px;
  padding: 12px;
  border: 3px solid #928b8429;
  background: transparent;
  cursor: pointer;
  color: #939191;
  font-size: 1rem;
  font-weight: 700;
  z-index: 0;
  display: flex;
  align-items: center;
  justify-content: center;

}

#screen-roll {
  z-index: 10;
  pointer-events: none;
}

#screen-roll button {
  pointer-events: auto;
}

/* --- Roll result overlay --- */
.roll-result {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 50;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  pointer-events: auto;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 0.3s ease;
}

.roll-result.active {
  display: flex;
}

.roll-result-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  background: rgba(26, 26, 46, 0.85);
  border: 1px solid rgba(255,255,255,0.1);
  padding: 28px 32px;
  min-width: 260px;
  max-width: 90vw;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5);
}

.roll-result-dice {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.roll-result-dice .result-die {
  background: rgba(0,0,0,0.35);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 10px;
  padding: 8px 12px;
  min-width: 56px;
  text-align: center;
}

.roll-result-dice .result-die .die-type {
  font-size: 0.65rem;
  color: #888;
  font-weight: 600;
  text-transform: uppercase;
  display: block;
}

.roll-result-dice .result-die .die-value {
  font-size: 1.4rem;
  font-weight: 800;
  color: #fff;
  display: block;
}

.roll-result-dice .result-die.max .die-value { color: #4caf50; }
.roll-result-dice .result-die.min .die-value { color: var(--primary); }

.roll-result-total {
  font-size: 1.6rem;
  font-weight: 700;
}

.roll-result-total span {
  color: #939191;
}

.roll-result-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 4px;
}

button.primary.btn-roll {
  position: fixed;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 15;
  transition: transform 0.15s, opacity 0.15s;
  touch-action: manipulation;
}

button.primary.btn-roll:active {
  transform: translateX(-50%) scale(0.95);
}

button.primary.btn-roll:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#back-btn.secondary {
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 15;
  transition: background 0.15s;
  touch-action: manipulation;
}

/* --- Waiting overlay --- */
.waiting-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 40;
  align-items: flex-start;
  justify-content: center;
  pointer-events: none;
}

.waiting-overlay.active {
  display: flex;
}

.waiting-overlay .waiting-box {
  margin-top: 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  background: rgba(0,0,0,0.45);
  padding: 14px 28px;
  backdrop-filter: blur(4px);
  border: 1px solid rgba(255,255,255,0.06);
}

.waiting-overlay p {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--primary);
  animation: pulse 1.2s ease-in-out infinite;
}

.waiting-spinner {
  width: 28px;
  height: 28px;
  border: 3px solid rgba(233,69,96,0.2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

@keyframes pulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

/* --- Result die (shared) --- */
.result-die {
  display: flex;
  flex-direction: column;
  align-items: center;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}


@media (max-width: 480px) {
  .select-header h1 { font-size: 1.4rem; }
  .dice-item { padding: 8px; min-width: 80px; }
  .btn-roll { bottom: 24px; padding: 14px 40px; font-size: 1rem; }
  .dice-list {
    justify-content: initial;
  }
}
