/* Dice — 3D dice + spinner + random picker
 * Palette: ash-rose + cream + emerald accents
 * © אופיר ברנס (Ofir Baranes) 2026
 */

* { margin:0; padding:0; box-sizing:border-box; -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; }

:root {
  --bg-1: #181014;
  --bg-2: #211519;
  --bg-3: #2c1c22;
  --rose-200: #e8d2d6;
  --rose-300: #d4b3b9;
  --rose-400: #c4a5a8;
  --rose-500: #a88489;
  --rose-600: #8c6970;
  --rose-700: #5d4248;
  --cream: #fef3c7;
  --cream-light: #fffbeb;
  --gold: #fcd34d;
  --emerald: #34d399;
  --emerald-deep: #059669;
  --text-primary: #fef3c7;
  --text-secondary: rgba(254,243,199,0.72);
  --text-muted: rgba(254,243,199,0.42);
  --surface: rgba(212,179,185,0.05);
  --surface-strong: rgba(212,179,185,0.10);
  --border: rgba(196,165,168,0.16);
  --border-strong: rgba(196,165,168,0.28);
  --shadow-glass: 0 30px 60px -25px rgba(24,16,20,0.7);
  --bezier: cubic-bezier(.34,1.56,.64,1);
  --bezier-soft: cubic-bezier(.4,0,.2,1);
}

html, body {
  background: var(--bg-1);
  color: var(--text-primary);
  font-family: 'Heebo', system-ui, sans-serif;
  font-weight: 400;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

body {
  background:
    radial-gradient(ellipse 700px 800px at 80% -10%, rgba(196,165,168,0.18), transparent 60%),
    radial-gradient(ellipse 500px 600px at -10% 60%, rgba(52,211,153,0.10), transparent 65%),
    radial-gradient(ellipse 800px 1000px at 50% 110%, rgba(93,66,72,0.30), transparent 70%),
    linear-gradient(180deg, #181014 0%, #211519 60%, #2c1c22 110%);
  background-attachment: fixed;
  position: relative;
  padding-bottom: 30px;
}

.ambient-bg {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}
.orb-blur {
  position: absolute;
  border-radius: 50%;
  filter: blur(85px);
  opacity: 0.45;
}
.orb-blur.o1 { width: 420px; height: 420px; top: -120px; right: -120px; background: radial-gradient(circle, #c4a5a8 0%, transparent 70%); animation: drift1 26s ease-in-out infinite; }
.orb-blur.o2 { width: 340px; height: 340px; bottom: 5%; left: -100px; background: radial-gradient(circle, #34d399 0%, transparent 70%); opacity: 0.18; animation: drift2 32s ease-in-out infinite; }
.orb-blur.o3 { width: 260px; height: 260px; top: 40%; right: 30%; background: radial-gradient(circle, #5d4248 0%, transparent 70%); opacity: 0.32; animation: drift3 38s ease-in-out infinite; }
@keyframes drift1 { 0%,100% { transform: translate(0,0) scale(1); } 50% { transform: translate(-30px,40px) scale(1.1); } }
@keyframes drift2 { 0%,100% { transform: translate(0,0) scale(1); } 50% { transform: translate(40px,-30px) scale(1.08); } }
@keyframes drift3 { 0%,100% { transform: translate(0,0) scale(1); } 50% { transform: translate(-25px,-35px) scale(1.12); } }

/* ===== Topbar ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: linear-gradient(180deg, rgba(24,16,20,0.85) 0%, rgba(24,16,20,0.55) 100%);
  backdrop-filter: blur(20px) saturate(150%);
  -webkit-backdrop-filter: blur(20px) saturate(150%);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 700;
  color: var(--cream-light);
  letter-spacing: -0.01em;
}
.mark {
  width: 32px; height: 32px;
  display: grid; place-items: center;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--rose-300), var(--rose-700));
  color: var(--bg-1);
  box-shadow: 0 0 18px rgba(196,165,168,0.45);
}
.mark svg { width: 18px; height: 18px; }

.header-actions { display: flex; gap: 8px; }
.icon-btn, .install-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  border-radius: 12px;
  padding: 7px 12px;
  cursor: pointer;
  transition: all 0.2s var(--bezier-soft);
  backdrop-filter: blur(10px);
}
.icon-btn { padding: 7px 9px; font-size: 16px; }
.icon-btn:hover, .install-btn:hover {
  background: var(--surface-strong);
  border-color: var(--border-strong);
  transform: translateY(-1px);
}
.install-btn {
  background: linear-gradient(135deg, var(--rose-300), var(--rose-700));
  border: none;
  color: var(--bg-1);
  font-weight: 700;
}

main {
  position: relative;
  z-index: 1;
  padding: 18px 18px;
  max-width: 540px;
  margin: 0 auto;
}
.hidden { display: none !important; }

/* ===== Mode tabs ===== */
.mode-tabs {
  display: flex;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 4px;
  margin-bottom: 18px;
}
.mode-tab {
  flex: 1;
  padding: 10px 6px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 10px;
  transition: all 0.25s var(--bezier);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.mode-tab:hover { color: var(--cream-light); }
.mode-tab.active {
  background: linear-gradient(135deg, var(--rose-400), var(--rose-700));
  color: var(--cream-light);
  font-weight: 700;
  box-shadow: 0 6px 14px rgba(168,132,137,0.30);
}
.mt-icon { width: 20px; height: 20px; }

/* ===== Result card (dice) ===== */
.result-card {
  text-align: center;
  margin-bottom: 22px;
  padding: 18px;
  background: linear-gradient(140deg, var(--surface-strong) 0%, var(--surface) 100%);
  backdrop-filter: blur(18px) saturate(160%);
  border: 1px solid var(--border-strong);
  border-radius: 22px;
}
.result-label {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--rose-300);
  margin-bottom: 6px;
}
.result-value {
  font-family: 'IBM Plex Mono', monospace;
  font-size: clamp(56px, 14vw, 84px);
  font-weight: 300;
  color: var(--cream-light);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
  line-height: 1;
  text-shadow: 0 0 40px rgba(254,243,199,0.20);
  margin-bottom: 8px;
  direction: ltr;
}
.result-value.changing {
  animation: result-pulse 0.4s var(--bezier);
}
@keyframes result-pulse {
  0% { transform: scale(0.8); opacity: 0.4; }
  60% { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(1); }
}
.result-detail {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  color: var(--text-muted);
  letter-spacing: 0.05em;
  font-variant-numeric: tabular-nums;
  direction: ltr;
}

/* ===== 3D Dice stage ===== */
.dice-stage {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  justify-content: center;
  align-items: center;
  min-height: 120px;
  margin-bottom: 24px;
  perspective: 600px;
  perspective-origin: 50% 50%;
}

.dice {
  width: 80px;
  height: 80px;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 1.6s cubic-bezier(.2,.7,.4,1.1);
}
.dice.rolling {
  animation: dice-roll 1.4s cubic-bezier(.4,1.6,.5,1) forwards;
}
@keyframes dice-roll {
  0%   { transform: rotateX(0) rotateY(0) translateY(0); }
  20%  { transform: rotateX(360deg) rotateY(180deg) translateY(-40px); }
  40%  { transform: rotateX(720deg) rotateY(360deg) translateY(-60px); }
  60%  { transform: rotateX(1080deg) rotateY(540deg) translateY(-30px); }
  80%  { transform: rotateX(1440deg) rotateY(720deg) translateY(-10px); }
  100% { transform: rotateX(var(--rx, 0)) rotateY(var(--ry, 0)) translateY(0); }
}

.dice-face {
  position: absolute;
  width: 80px;
  height: 80px;
  border-radius: 14px;
  background: linear-gradient(140deg, var(--cream-light) 0%, var(--cream) 50%, #d4c896 100%);
  display: grid;
  place-items: center;
  border: 1px solid rgba(0,0,0,0.15);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: inset -4px -4px 12px rgba(120,80,80,0.18), inset 2px 2px 6px rgba(255,255,255,0.5);
}

/* 6 faces of a cube positioned in 3D */
.dice-face.front  { transform: translateZ(40px); }
.dice-face.back   { transform: rotateY(180deg) translateZ(40px); }
.dice-face.right  { transform: rotateY(90deg) translateZ(40px); }
.dice-face.left   { transform: rotateY(-90deg) translateZ(40px); }
.dice-face.top    { transform: rotateX(90deg) translateZ(40px); }
.dice-face.bottom { transform: rotateX(-90deg) translateZ(40px); }

/* Dot grid for D6 faces */
.face-dots {
  width: 60px;
  height: 60px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
}
.face-dots .dot {
  width: 12px;
  height: 12px;
  background: var(--bg-1);
  border-radius: 50%;
  align-self: center;
  justify-self: center;
  box-shadow: inset -1px -1px 2px rgba(255,255,255,0.2), inset 1px 1px 1px rgba(0,0,0,0.4);
}

/* Dot position helpers (1-9 grid; we use 1, 3, 5, 7, 9 + center for various counts) */
.face-dots[data-pip="1"] .dot:nth-child(5) { background: var(--bg-1); }
/* Hidden dots */
.face-dots .dot.hidden { background: transparent; box-shadow: none; }

/* Numeric face fallback (D4, D8, D10, D12, D20, D100) */
.face-num {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 32px;
  font-weight: 700;
  color: var(--bg-1);
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 2px rgba(255,255,255,0.5);
}

.dice.small { width: 56px; height: 56px; }
.dice.small .dice-face { width: 56px; height: 56px; }
.dice.small .dice-face.front  { transform: translateZ(28px); }
.dice.small .dice-face.back   { transform: rotateY(180deg) translateZ(28px); }
.dice.small .dice-face.right  { transform: rotateY(90deg) translateZ(28px); }
.dice.small .dice-face.left   { transform: rotateY(-90deg) translateZ(28px); }
.dice.small .dice-face.top    { transform: rotateX(90deg) translateZ(28px); }
.dice.small .dice-face.bottom { transform: rotateX(-90deg) translateZ(28px); }
.dice.small .face-dots { width: 42px; height: 42px; gap: 1px; }
.dice.small .face-dots .dot { width: 8px; height: 8px; }
.dice.small .face-num { font-size: 22px; }

/* ===== Roll button ===== */
.roll-btn {
  width: 100%;
  padding: 18px;
  background: linear-gradient(135deg, var(--rose-400), var(--rose-700));
  border: none;
  border-radius: 18px;
  color: var(--cream-light);
  font-family: inherit;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  transition: all 0.25s var(--bezier);
  box-shadow: 0 12px 28px rgba(168,132,137,0.40), inset 0 1px 0 rgba(254,243,199,0.18);
  margin-bottom: 22px;
  letter-spacing: 0.02em;
}
.roll-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 16px 36px rgba(168,132,137,0.55), inset 0 1px 0 rgba(254,243,199,0.22);
}
.roll-btn:active { transform: scale(0.98); }
.roll-btn.rolling { pointer-events: none; opacity: 0.7; }
.rb-icon { font-size: 22px; }
.rb-label { font-size: 17px; }

/* ===== Dice config ===== */
.dice-config {
  background: var(--surface);
  backdrop-filter: blur(14px);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.config-group { display: flex; flex-direction: column; gap: 8px; }
.cg-header { display: flex; justify-content: space-between; align-items: baseline; }
.cg-meta {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.cg-link {
  background: transparent;
  border: none;
  color: var(--rose-300);
  font-family: inherit;
  font-size: 11px;
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 6px;
}
.cg-link:hover { color: var(--cream-light); background: rgba(196,165,168,0.10); }

.config-label {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--rose-300);
}

.dice-types {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.dt-btn {
  padding: 8px 14px;
  background: rgba(212,179,185,0.04);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-secondary);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.dt-btn:hover { background: var(--surface-strong); color: var(--cream-light); }
.dt-btn.active {
  background: linear-gradient(135deg, var(--rose-400), var(--rose-700));
  color: var(--cream-light);
  border-color: transparent;
  box-shadow: 0 4px 10px rgba(168,132,137,0.35);
}

.qty-control {
  display: flex;
  align-items: center;
  gap: 14px;
  background: rgba(212,179,185,0.04);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  width: fit-content;
}
.qty-btn {
  width: 36px; height: 36px;
  border-radius: 10px;
  background: var(--surface-strong);
  border: 1px solid var(--border);
  color: var(--cream-light);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 22px;
  font-weight: 700;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: all 0.15s;
}
.qty-btn:hover { background: var(--rose-700); color: var(--cream-light); border-color: var(--rose-400); }
.qty-btn:active { transform: scale(0.92); }
.qty-value {
  min-width: 40px;
  text-align: center;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 22px;
  font-weight: 600;
  color: var(--cream-light);
  font-variant-numeric: tabular-nums;
}

/* ===== Spinner ===== */
.spinner-stage {
  position: relative;
  width: 280px;
  height: 280px;
  margin: 0 auto 18px;
}
#spinnerSvg {
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 18px 30px rgba(24,16,20,0.5));
  transition: transform 3.5s cubic-bezier(.18,.7,.2,1);
}
.spinner-arrow {
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 28px;
  height: 36px;
  color: var(--cream);
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.5));
  z-index: 2;
}
.spinner-arrow svg { width: 100%; height: 100%; }
.spinner-result {
  font-family: 'Frank Ruhl Libre', 'Heebo', serif;
  font-size: 28px;
  font-weight: 700;
  color: var(--cream-light);
  text-align: center;
  margin-bottom: 18px;
  letter-spacing: -0.015em;
  min-height: 36px;
  font-variant-numeric: normal;
}
.spinner-result.empty {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 400;
  letter-spacing: 0.05em;
}
.config-textarea {
  width: 100%;
  padding: 14px;
  background: rgba(212,179,185,0.04);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--cream-light);
  font-family: 'Heebo', sans-serif;
  font-size: 14px;
  resize: vertical;
  min-height: 100px;
  text-align: right;
  direction: rtl;
  line-height: 1.55;
  transition: all 0.2s;
}
.config-textarea:focus {
  outline: none;
  border-color: var(--rose-400);
  background: rgba(212,179,185,0.08);
}

/* ===== Picker ===== */
.picker-result-card {
  background: linear-gradient(140deg, var(--surface-strong) 0%, var(--surface) 100%);
  backdrop-filter: blur(18px) saturate(160%);
  border: 1px solid var(--border-strong);
  border-radius: 22px;
  padding: 24px 18px;
  text-align: center;
  margin-bottom: 18px;
}
.picker-name {
  font-family: 'Frank Ruhl Libre', 'Heebo', serif;
  font-size: clamp(36px, 8vw, 48px);
  font-weight: 700;
  color: var(--cream-light);
  letter-spacing: -0.02em;
  line-height: 1.1;
  margin-bottom: 8px;
  text-shadow: 0 0 40px rgba(254,243,199,0.18);
  min-height: 40px;
  word-break: break-word;
}
.picker-name.changing {
  animation: result-pulse 0.4s var(--bezier);
}
.picker-meta {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}

.picker-config {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 18px;
}
.picker-options {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.option-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: rgba(212,179,185,0.03);
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s;
}
.option-row:hover { background: rgba(212,179,185,0.06); }
.option-row input[type="checkbox"] {
  width: 18px; height: 18px;
  accent-color: var(--rose-400);
  cursor: pointer;
}
.opt-text { flex: 1; min-width: 0; }
.opt-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--cream-light);
  margin-bottom: 2px;
}
.opt-sub {
  font-size: 11px;
  color: var(--text-muted);
  font-family: 'IBM Plex Mono', monospace;
}

/* ===== Modal ===== */
.modal {
  position: fixed; inset: 0;
  background: rgba(15,10,12,0.7);
  backdrop-filter: blur(8px);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: modal-fade 0.25s var(--bezier-soft);
}
@keyframes modal-fade { from { opacity: 0; } to { opacity: 1; } }
.modal-card {
  width: 100%;
  max-width: 540px;
  background: linear-gradient(180deg, #2c1c22 0%, #181014 100%);
  border: 1px solid var(--border-strong);
  border-bottom: none;
  border-radius: 28px 28px 0 0;
  padding: 20px 22px 32px;
  padding-bottom: max(32px, env(safe-area-inset-bottom));
  animation: modal-slide 0.4s var(--bezier);
  max-height: 88vh;
  overflow-y: auto;
}
@keyframes modal-slide { from { transform: translateY(100%); } to { transform: translateY(0); } }
.handle {
  width: 38px; height: 4px;
  background: var(--border-strong);
  border-radius: 4px;
  margin: 0 auto 16px;
}
.modal-card h3 {
  font-family: 'Frank Ruhl Libre', 'Heebo', serif;
  color: var(--cream-light);
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 14px;
}
.modal-card p {
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 1.65;
  margin-bottom: 14px;
}
.mark-big {
  width: 64px; height: 64px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--rose-300), var(--rose-700));
  border-radius: 18px;
  color: var(--bg-1);
  margin: 4px auto 14px;
  box-shadow: 0 0 30px rgba(196,165,168,0.45);
}
.mark-big svg { width: 32px; height: 32px; }

/* History */
.hh-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 14px;
}
.hh-header h3 { margin-bottom: 0; }
.hist-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 60vh;
  overflow-y: auto;
}
.hist-list::-webkit-scrollbar { width: 4px; }
.hist-list::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }

.hist-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  background: rgba(212,179,185,0.04);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.hr-mode {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--rose-300);
  padding: 2px 8px;
  background: rgba(196,165,168,0.10);
  border-radius: 100px;
}
.hr-result {
  flex: 1;
  font-family: 'Heebo', sans-serif;
  font-size: 14px;
  color: var(--cream-light);
  font-weight: 500;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hr-time {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}
.hist-empty {
  text-align: center;
  padding: 30px 20px;
  color: var(--text-muted);
  font-size: 13px;
  font-family: 'IBM Plex Mono', monospace;
}

.btn-primary, .btn-secondary, .btn-mini {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 13px 18px;
  border-radius: 14px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.25s var(--bezier);
  border: none;
}
.btn-primary {
  background: linear-gradient(135deg, var(--rose-400), var(--rose-700));
  color: var(--cream-light);
  font-weight: 700;
  box-shadow: 0 8px 24px rgba(168,132,137,0.40);
}
.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 12px 30px rgba(168,132,137,0.55); }
.btn-secondary {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--cream-light);
}
.btn-mini {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  flex: 0 1 auto;
  padding: 9px 14px;
  font-size: 12px;
}
.btn-mini:hover { background: var(--surface-strong); color: var(--cream-light); }
