:root {
  --dice-size: 120px;
  --half: calc(var(--dice-size) / 2);
  --pip-size: 20px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: linear-gradient(135deg, #1e1b4b, #312e81 60%, #4c1d95);
  color: #f8fafc;
}

.app {
  width: 100%;
  max-width: 520px;
  padding: 2rem 1.5rem 3rem;
  text-align: center;
}

.top h1 {
  margin: 0 0 1rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

/* ---- Session bar ---- */
.session-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  margin-bottom: 2rem;
  color: #c7d2fe;
}
.session-label { opacity: 0.7; }
.session-id {
  background: rgba(255, 255, 255, 0.1);
  padding: 0.15rem 0.5rem;
  border-radius: 6px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.link-btn {
  background: transparent;
  border: 1px solid rgba(199, 210, 254, 0.5);
  color: #c7d2fe;
  border-radius: 6px;
  padding: 0.15rem 0.5rem;
  cursor: pointer;
  font-size: 0.8rem;
}
.link-btn:hover { background: rgba(255, 255, 255, 0.1); }
.presence { opacity: 0.85; }
.conn { font-size: 0.75rem; padding: 0.1rem 0.45rem; border-radius: 999px; }
.conn-connecting { background: #ca8a04; color: #1e1b4b; }
.conn-open { background: #16a34a; color: #f0fdf4; }
.conn-closed { background: #dc2626; color: #fef2f2; }

/* ---- 3D dice ---- */
.stage {
  perspective: 900px;
  width: var(--dice-size);
  height: var(--dice-size);
  margin: 0 auto 2rem;
}
.dice {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transform: rotateX(-20deg) rotateY(-20deg);
  transition: transform 1.1s cubic-bezier(0.25, 1, 0.4, 1);
}
.dice.rolling {
  animation: tumble 0.45s linear infinite;
  transition: none;
}
@keyframes tumble {
  0%   { transform: rotateX(0deg)   rotateY(0deg)   rotateZ(0deg); }
  100% { transform: rotateX(360deg) rotateY(360deg) rotateZ(180deg); }
}

.face {
  position: absolute;
  width: var(--dice-size);
  height: var(--dice-size);
  background: #f8fafc;
  border: 2px solid #cbd5e1;
  border-radius: 16px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 4px;
  padding: 14px;
  box-shadow: inset 0 0 18px rgba(0, 0, 0, 0.12);
}
.pip {
  width: var(--pip-size);
  height: var(--pip-size);
  background: #1e293b;
  border-radius: 50%;
  align-self: center;
  justify-self: center;
}

/* Place faces around the cube */
.face-1 { transform: rotateY(0deg)   translateZ(var(--half)); }
.face-2 { transform: rotateY(90deg)  translateZ(var(--half)); }
.face-3 { transform: rotateY(180deg) translateZ(var(--half)); }
.face-4 { transform: rotateY(-90deg) translateZ(var(--half)); }
.face-5 { transform: rotateX(90deg)  translateZ(var(--half)); }
.face-6 { transform: rotateX(-90deg) translateZ(var(--half)); }

/* Pip arrangements */
.face-1 { grid-template-columns: 1fr; grid-template-rows: 1fr; }
.face-1 .pip { grid-area: 1 / 1; }

.face-2 .pip:nth-child(1) { grid-area: 1 / 1; }
.face-2 .pip:nth-child(2) { grid-area: 3 / 3; }

.face-3 .pip:nth-child(1) { grid-area: 1 / 1; }
.face-3 .pip:nth-child(2) { grid-area: 2 / 2; }
.face-3 .pip:nth-child(3) { grid-area: 3 / 3; }

.face-4 .pip:nth-child(1) { grid-area: 1 / 1; }
.face-4 .pip:nth-child(2) { grid-area: 1 / 3; }
.face-4 .pip:nth-child(3) { grid-area: 3 / 1; }
.face-4 .pip:nth-child(4) { grid-area: 3 / 3; }

.face-5 .pip:nth-child(1) { grid-area: 1 / 1; }
.face-5 .pip:nth-child(2) { grid-area: 1 / 3; }
.face-5 .pip:nth-child(3) { grid-area: 2 / 2; }
.face-5 .pip:nth-child(4) { grid-area: 3 / 1; }
.face-5 .pip:nth-child(5) { grid-area: 3 / 3; }

.face-6 .pip:nth-child(1) { grid-area: 1 / 1; }
.face-6 .pip:nth-child(2) { grid-area: 1 / 3; }
.face-6 .pip:nth-child(3) { grid-area: 2 / 1; }
.face-6 .pip:nth-child(4) { grid-area: 2 / 3; }
.face-6 .pip:nth-child(5) { grid-area: 3 / 1; }
.face-6 .pip:nth-child(6) { grid-area: 3 / 3; }

/* Numeric dice tokens (mixed / non-d6 rolls) */
.tokens {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  justify-content: center;
  margin: 0 auto 1.5rem;
  max-width: 420px;
}
.token {
  width: 56px;
  height: 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #f8fafc;
  color: #1e293b;
  border: 2px solid #cbd5e1;
  border-radius: 12px;
  box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
}
.token-val { font-size: 1.5rem; font-weight: 800; line-height: 1; }
.token-type { font-size: 0.65rem; opacity: 0.6; margin-top: 2px; }
.token.shaking { animation: shake 0.4s linear infinite; }
@keyframes shake {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  25% { transform: translateY(-4px) rotate(-8deg); }
  75% { transform: translateY(4px) rotate(8deg); }
}
/* Faint per-type tinting so mixed dice are visually distinguishable */
.token.d4   { border-color: #f87171; }
.token.d6   { border-color: #fbbf24; }
.token.d8   { border-color: #34d399; }
.token.d10  { border-color: #60a5fa; }
.token.d12  { border-color: #a78bfa; }
.token.d20  { border-color: #f472b6; }
.token.d100 { border-color: #22d3ee; }

/* ---- Landing ---- */
.landing { max-width: 420px; margin: 1rem auto 0; }
.lede { color: #c7d2fe; font-size: 0.95rem; margin-bottom: 1.5rem; }
.landing-actions { display: flex; flex-direction: column; gap: 0.75rem; }
.mode-btn {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.85rem 1rem;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.06);
  color: #f8fafc;
  cursor: pointer;
  text-align: left;
  font: inherit;
}
.mode-btn:hover { background: rgba(255, 255, 255, 0.12); }
.mode-btn.primary { background: #fbbf24; color: #1e1b4b; border-color: transparent; }
.mode-btn.primary:hover { background: #fcd34d; }
.mode-title { font-weight: 700; font-size: 1.05rem; }
.mode-sub { font-size: 0.8rem; opacity: 0.75; }
.join-form { display: flex; gap: 0.5rem; }
.join-form input {
  flex: 1;
  padding: 0.6rem 0.75rem;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.08);
  color: #f8fafc;
  font-size: 0.95rem;
}
.join-form .mode-btn { flex: 0 0 auto; flex-direction: row; align-items: center; }

/* ---- D&D mode ---- */
.dnd-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: #c7d2fe;
  cursor: pointer;
}
.dnd-panel {
  max-width: 420px;
  margin: 0 auto 1.25rem;
  padding: 1rem;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.15);
}
.dice-rows { display: flex; flex-direction: column; gap: 0.5rem; }
.dice-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  justify-content: center;
}
.dice-row select {
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.08);
  color: #f8fafc;
}
.stepper { display: inline-flex; align-items: center; gap: 0.5rem; }
.stepper button {
  width: 28px; height: 28px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.08);
  color: #f8fafc;
  font-size: 1.1rem;
  cursor: pointer;
}
.stepper button:hover { background: rgba(255, 255, 255, 0.18); }
.row-count { min-width: 1.5ch; text-align: center; font-weight: 700; }
.row-remove {
  background: transparent;
  border: none;
  color: #fca5a5;
  cursor: pointer;
  font-size: 0.9rem;
}
.dnd-controls {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.85rem;
}
.add-die-btn {
  padding: 0.45rem 0.9rem;
  border-radius: 8px;
  border: 1px dashed rgba(199, 210, 254, 0.6);
  background: transparent;
  color: #c7d2fe;
  cursor: pointer;
}
.add-die-btn:hover { background: rgba(255, 255, 255, 0.08); }
.mod-field { display: flex; flex-direction: column; gap: 0.2rem; font-size: 0.8rem; color: #c7d2fe; text-align: left; }
.mod-field input {
  width: 70px;
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.08);
  color: #f8fafc;
}
.notation-preview {
  margin: 0.85rem 0 0;
  font-family: ui-monospace, Menlo, monospace;
  font-weight: 700;
  color: #fbbf24;
}

/* ---- Controls ---- */
.controls {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1.5rem;
}

.roll-btn {
  padding: 0.6rem 1.6rem;
  font-size: 1.05rem;
  font-weight: 700;
  color: #1e1b4b;
  background: #fbbf24;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.2s ease;
}
.roll-btn:hover { background: #fcd34d; }
.roll-btn:active { transform: scale(0.96); }
.roll-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---- Result + history ---- */
.result {
  font-size: 1.1rem;
  min-height: 1.6em;
  margin: 0.5rem 0 1.5rem;
}
.result .total { font-weight: 800; color: #fbbf24; }

.history {
  list-style: none;
  padding: 0;
  margin: 0 auto;
  max-width: 360px;
  text-align: left;
  font-size: 0.85rem;
  color: #c7d2fe;
}
.history li {
  padding: 0.35rem 0.6rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}
.history li .roll-detail { opacity: 0.7; font-family: ui-monospace, Menlo, monospace; }

/* ---- Ad slots (hidden via [hidden] when AdSense isn't configured) ---- */
.ad-slot {
  margin: 0 auto;
  max-width: 468px;
  min-height: 60px;
}
.ad-slot[hidden] { display: none; }
.ad-top { margin-bottom: 1.25rem; }
.ad-bottom { margin-top: 2.5rem; }
