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

:root {
  --bg:             #0d0b0b;
  --surface:        #1a1414;
  --surface-2:      #221a1a;
  --border:         rgba(180, 70, 50, 0.18);
  --border-bright:  rgba(200, 90, 60, 0.38);
  --accent:         #c0392b;
  --accent-bright:  #e74c3c;
  --gold:           #c8a850;
  --gold-dim:       rgba(200, 168, 80, 0.12);
  --text:           #e8ddd5;
  --muted:          #8a7870;
  --glow-red:       rgba(192, 57, 43, 0.12);
  --glow-gold:      rgba(200, 168, 80, 0.18);
}

html {
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

body {
  min-height: 100vh;
  background:
    radial-gradient(ellipse at 50% -10%, rgba(192, 57, 43, 0.07) 0%, transparent 55%),
    var(--bg);
}

/* ---- Back link ---- */
.back-link {
  position: fixed;
  top: 1.25rem;
  left: 1.25rem;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--muted);
  text-decoration: none;
  letter-spacing: 0.04em;
  transition: color 0.2s;
  z-index: 10;
}
.back-link:hover { color: var(--text); }

/* ---- App shell ---- */
.app {
  max-width: 540px;
  margin: 0 auto;
  padding: 5.5rem 1.5rem 4rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2rem;
}

/* ---- Header ---- */
.app-header { text-align: center; }

.title-emblem {
  font-size: 2.2rem;
  color: var(--accent);
  display: block;
  margin-bottom: 0.4rem;
  filter: drop-shadow(0 0 14px rgba(192, 57, 43, 0.65));
  line-height: 1;
}

.app-title {
  font-family: 'Cinzel', serif;
  font-size: clamp(3.2rem, 11vw, 5.5rem);
  font-weight: 900;
  letter-spacing: 0.14em;
  color: var(--text);
  text-shadow: 0 0 50px rgba(192, 57, 43, 0.25);
  line-height: 1;
  margin-bottom: 0.6rem;
}

.app-sub {
  font-size: 0.72rem;
  color: var(--muted);
  letter-spacing: 0.22em;
  text-transform: uppercase;
  font-weight: 500;
}

.app-version {
  font-family: 'Cinzel', serif;
  font-size: 0.65rem;
  color: var(--muted);
  opacity: 0.5;
  letter-spacing: 0.15em;
  margin-top: 0.4rem;
}

/* ---- Dice row ---- */
.dice-row {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
  justify-content: center;
}

.die-btn {
  font-family: 'Cinzel', serif;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  padding: 0.55rem 0.95rem;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  cursor: pointer;
  min-width: 3.2rem;
  transition: background 0.15s, border-color 0.15s, color 0.15s, box-shadow 0.15s;
}

.die-btn:hover {
  border-color: var(--border-bright);
  color: var(--text);
  background: var(--surface-2);
}

.die-btn.selected {
  background: var(--accent);
  border-color: var(--accent-bright);
  color: #fff;
  box-shadow: 0 0 18px rgba(192, 57, 43, 0.45);
}

/* ---- Controls ---- */
.controls-row {
  display: flex;
  align-items: center;
  gap: 2.5rem;
}

.qty-control {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.qty-btn {
  width: 30px;
  height: 30px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
  line-height: 1;
}

.qty-btn:hover {
  border-color: var(--border-bright);
  background: var(--surface-2);
}

.qty-val {
  font-family: 'Cinzel', serif;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
  min-width: 1.8rem;
  text-align: center;
  line-height: 1;
}

.qty-label {
  font-size: 0.7rem;
  color: var(--muted);
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.mod-control {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.ctrl-label {
  font-size: 0.7rem;
  color: var(--muted);
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.mod-input {
  width: 62px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: 'Cinzel', serif;
  font-size: 1rem;
  font-weight: 700;
  text-align: center;
  padding: 0.38rem 0.5rem;
  border-radius: 6px;
  outline: none;
  transition: border-color 0.2s;
  -moz-appearance: textfield;
}
.mod-input::-webkit-outer-spin-button,
.mod-input::-webkit-inner-spin-button { -webkit-appearance: none; }
.mod-input:focus { border-color: var(--border-bright); }

/* ---- Advantage row ---- */
.adv-row {
  display: flex;
  gap: 0.75rem;
}

.adv-btn {
  font-family: 'Cinzel', serif;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  padding: 0.45rem 1.1rem;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.adv-btn:hover {
  border-color: var(--border-bright);
  color: var(--text);
}

.adv-btn.active {
  background: var(--gold-dim);
  border-color: var(--gold);
  color: var(--gold);
}

/* ---- Result ---- */
.result-wrap {
  width: 100%;
  min-height: 148px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 2rem;
  transition: border-color 0.35s, box-shadow 0.35s;
}

.result-wrap.rolled {
  border-color: rgba(192, 57, 43, 0.35);
  box-shadow: 0 0 50px rgba(192, 57, 43, 0.08), inset 0 0 30px rgba(192, 57, 43, 0.04);
}

.result-wrap.crit {
  border-color: rgba(200, 168, 80, 0.5);
  box-shadow: 0 0 60px var(--glow-gold), inset 0 0 40px rgba(200, 168, 80, 0.05);
}

.result-wrap.fumble {
  border-color: rgba(192, 57, 43, 0.6);
  box-shadow: 0 0 60px rgba(192, 57, 43, 0.2), inset 0 0 40px rgba(192, 57, 43, 0.07);
}

.result-idle {
  font-family: 'Cinzel', serif;
  font-size: 4rem;
  color: var(--muted);
  opacity: 0.25;
  line-height: 1;
}

.result-total {
  font-family: 'Cinzel', serif;
  font-size: clamp(3.2rem, 14vw, 6rem);
  font-weight: 900;
  color: var(--text);
  line-height: 1;
  letter-spacing: -0.02em;
}

.result-total.crit   { color: var(--gold);         text-shadow: 0 0 32px rgba(200, 168, 80, 0.55); }
.result-total.fumble { color: var(--accent-bright); text-shadow: 0 0 32px rgba(231, 76, 60, 0.55); }

@keyframes rollIn {
  0%   { transform: scale(0.65) rotate(-6deg); opacity: 0; }
  65%  { transform: scale(1.1)  rotate(1.5deg); }
  100% { transform: scale(1)    rotate(0deg);   opacity: 1; }
}

.result-total.pop {
  animation: rollIn 0.32s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

.result-breakdown {
  font-size: 0.78rem;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-align: center;
  min-height: 1.1em;
}

/* ---- Roll button ---- */
.roll-btn {
  font-family: 'Cinzel', serif;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  padding: 1rem 0;
  width: 100%;
  max-width: 300px;
  background: var(--accent);
  border: none;
  color: #fff;
  border-radius: 10px;
  cursor: pointer;
  box-shadow: 0 4px 28px rgba(192, 57, 43, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.1);
  transition: background 0.2s, transform 0.15s, box-shadow 0.2s;
}

.roll-btn:hover {
  background: var(--accent-bright);
  transform: translateY(-2px);
  box-shadow: 0 8px 36px rgba(192, 57, 43, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

.roll-btn:active {
  transform: translateY(1px);
  box-shadow: 0 2px 14px rgba(192, 57, 43, 0.35);
}

/* ---- History ---- */
.history-wrap {
  width: 100%;
  border-top: 1px solid var(--border);
  padding-top: 1.5rem;
  display: none;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.8rem;
}

.history-title {
  font-family: 'Cinzel', serif;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}

.clear-btn {
  font-size: 0.7rem;
  color: var(--muted);
  background: none;
  border: none;
  cursor: pointer;
  letter-spacing: 0.05em;
  transition: color 0.2s;
  padding: 0;
}
.clear-btn:hover { color: var(--accent-bright); }

.history-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  max-height: 220px;
  overflow-y: auto;
  padding-right: 2px;
}

.history-list::-webkit-scrollbar { width: 3px; }
.history-list::-webkit-scrollbar-track { background: transparent; }
.history-list::-webkit-scrollbar-thumb { background: var(--border-bright); border-radius: 2px; }

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.45rem 0.75rem;
  background: var(--surface);
  border: 1px solid transparent;
  border-radius: 6px;
  animation: slideIn 0.18s ease both;
}

.history-item.crit   { border-color: rgba(200, 168, 80, 0.22); }
.history-item.fumble { border-color: rgba(192, 57, 43, 0.22); }

@keyframes slideIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.history-notation {
  font-family: 'Cinzel', serif;
  font-size: 0.76rem;
  color: var(--muted);
}

.history-result {
  font-family: 'Cinzel', serif;
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--text);
}

.history-result.crit   { color: var(--gold); }
.history-result.fumble { color: var(--accent-bright); }

/* ---- Dice canvas (dice-box container) ---- */
#dice-box-wrap,
.dice-box-canvas {
  position: fixed;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
  pointer-events: none;
  z-index: 50;
}

/* ---- Mute button ---- */
.mute-btn {
  position: fixed;
  top: 1.25rem;
  right: 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.2s, color 0.2s;
  z-index: 10;
  padding: 0;
  line-height: 1;
}
.mute-btn:hover { border-color: var(--border-bright); color: var(--text); }
.mute-btn.muted { color: var(--muted); opacity: 0.5; }

/* ---- Colour swatches ---- */
.colour-row {
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
  justify-content: center;
}

.colour-swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--c);
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s, box-shadow 0.15s;
  padding: 0;
  outline: none;
}

.colour-swatch:hover {
  transform: scale(1.18);
  border-color: rgba(255,255,255,0.3);
}

.colour-swatch.selected {
  border-color: rgba(255,255,255,0.7);
  box-shadow: 0 0 10px var(--c);
  transform: scale(1.12);
}

/* ---- Controls locked during roll ---- */
.roll-locked {
  pointer-events: none;
  opacity: 0.45;
  transition: opacity 0.25s;
}

/* ---- Responsive ---- */
@media (max-width: 420px) {
  .controls-row { gap: 1.5rem; }
  .die-btn { padding: 0.5rem 0.7rem; min-width: 2.8rem; font-size: 0.78rem; }
}
