:root {
  --bg: #140e0a;
  --bg-2: #1f1611;
  --card: #241a14;
  --card-2: #2d211a;
  --line: rgba(255, 228, 190, 0.12);
  --text: #f6ecdc;
  --muted: #b7a690;
  --red: #e1262a;
  --red-deep: #a8191c;
  --green: #b9dba1;
  --green-deep: #3f7a3a;
  --gold: #f2c94c;
  --ivory: #f8f1df;
  --win: #7ddc8a;
  --loss: #ff7b72;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100vh;
  background:
    radial-gradient(1200px 600px at 30% -10%, rgba(225, 38, 42, 0.18), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(185, 219, 161, 0.08), transparent 60%),
    var(--bg);
  color: var(--text);
  font: 15px/1.45 Inter, "Segoe UI", system-ui, sans-serif;
}

[lang="ne"], small[lang="ne"] { font-family: "Noto Sans Devanagari", "Nirmala UI", Inter, sans-serif; }

h1, h2, h3 { margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 15px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-bottom: 12px; }
h2 small { text-transform: none; letter-spacing: 0; font-weight: 600; font-size: 12px; opacity: 0.8; margin-left: 6px; }
h3 { font-size: 15px; margin: 18px 0 8px; }
img { display: block; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ buttons */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  padding: 10px 16px;
  border-radius: 10px;
  cursor: pointer;
  transition: transform 0.08s, background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover:not(:disabled) { border-color: rgba(255, 228, 190, 0.35); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.btn.primary {
  background: linear-gradient(180deg, #ee3a36, var(--red-deep));
  border-color: #ff6a5c;
  box-shadow: 0 6px 18px rgba(225, 38, 42, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.25);
}
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.icon { width: 40px; height: 40px; padding: 0; font-size: 20px; line-height: 1; }

/* ------------------------------------------------------------- header */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(20, 14, 10, 0.75);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; margin-right: auto; }
.brand-mark { width: 44px; height: 44px; border-radius: 10px; background: var(--green); border: 3px solid var(--red); }
.brand h1 { font-size: 22px; font-weight: 900; }
.brand p { margin: 0; font-size: 13px; color: var(--muted); }
.top-actions { display: flex; gap: 8px; }

/* ------------------------------------------------------------- layout */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 400px;
  gap: 20px;
  padding: 20px 24px 32px;
  max-width: 1600px;
  margin: 0 auto;
}
.stage { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.scene-wrap {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  background: #1b130e;
}
.scene { width: 100%; aspect-ratio: 3 / 2; max-height: calc(100vh - 230px); min-height: 300px; }
.scene canvas { display: block; width: 100% !important; height: 100% !important; touch-action: manipulation; }
.scene-fallback { display: grid; place-items: center; color: var(--muted); padding: 24px; text-align: center; }

.stage-status { position: absolute; top: 12px; left: 12px; display: flex; gap: 8px; align-items: center; pointer-events: none; }
.stage-status > * { pointer-events: auto; }
.pill {
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  background: rgba(20, 14, 10, 0.78);
  border: 1px solid var(--line);
}
.pill[data-state="ROLLING"], .pill[data-state="LOCKED"] { color: var(--gold); border-color: rgba(242, 201, 76, 0.5); }
.pill[data-state="COMPLETE"] { color: var(--win); }

/* ------------------------------------------------------- result strip */
.result-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 20px;
  padding: 12px 16px;
  min-height: 76px;
  border-radius: var(--radius);
  background: var(--card);
  border: 1px solid var(--line);
}
.dice-row { display: flex; gap: 8px; }
.die {
  width: 48px;
  height: 48px;
  border-radius: 9px;
  background: var(--ivory);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.12), 0 3px 8px rgba(0, 0, 0, 0.35);
  padding: 4px;
  animation: pop 0.35s both;
}
.die img { width: 100%; height: 100%; }
.die:nth-child(2) { animation-delay: 0.05s; }
.die:nth-child(3) { animation-delay: 0.1s; }
.die:nth-child(4) { animation-delay: 0.15s; }
.die:nth-child(5) { animation-delay: 0.2s; }
.die:nth-child(6) { animation-delay: 0.25s; }
.die.rolling { background: linear-gradient(135deg, var(--ivory), #d9cfb8); animation: wobble 0.5s infinite alternate; }
@keyframes pop { from { transform: scale(0.6); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes wobble { from { transform: rotate(-8deg); } to { transform: rotate(8deg) translateY(-3px); } }

/* -------------------------------------------------------------- panel */
.panel { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.start-bar { display: flex; gap: 10px; }
.btn.start.lift:not(.primary) { font-size: 18px; }
.btn.start:disabled { animation: none; }
.btn.start {
  flex: 1;
  height: 64px;
  font-size: 22px;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  border-radius: 16px;
}
.btn.start:not(:disabled) { animation: breathe 2.4s ease-in-out infinite; }
@keyframes breathe { 50% { box-shadow: 0 6px 28px rgba(225, 38, 42, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.25); } }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile {
  position: relative;
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 10px 4px;
  border-radius: 12px;
  background: var(--green);
  color: #151515;
  box-shadow: inset 0 0 0 3px var(--gold);
  transition: transform 0.2s, box-shadow 0.2s, opacity 0.2s;
}
.tile img { width: 46px; height: 46px; border-radius: 8px; }
.tile-name { font-weight: 800; font-size: 13px; }
.tile-name small { font-weight: 600; opacity: 0.75; }
.tile-count { font-weight: 900; font-size: 22px; font-variant-numeric: tabular-nums; }
.tile:not(.hit):not(.idle) { opacity: 0.45; }
.tile.hit { box-shadow: inset 0 0 0 3px var(--gold), 0 0 0 3px var(--gold), 0 0 22px rgba(242, 201, 76, 0.55); transform: translateY(-2px); }
.tile.hit .tile-count { color: var(--red-deep); }

.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 6px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 700; }
th:first-child, td:first-child { text-align: left; }
.note { font-size: 13px; color: var(--muted); margin: 8px 0; }
.muted { color: var(--muted); }
.scene.ready canvas { cursor: pointer; }
.skip { position: absolute; bottom: 12px; right: 12px; }
.view-ctrls { position: absolute; top: 12px; right: 12px; display: flex; gap: 6px; }
.view-ctrls .btn { background: rgba(20, 14, 10, 0.78); }
.view-ctrls .btn[aria-pressed="true"] { border-color: var(--gold); color: var(--gold); }
.scene.portrait { aspect-ratio: 4 / 5; max-height: calc(100vh - 160px); }

/* ------------------------------------------------------------ history */
.history { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; max-height: 280px; overflow: auto; }
.history li { display: grid; grid-template-columns: 40px auto 1fr; align-items: center; gap: 8px; padding: 4px 0; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.history li.muted { display: block; border: 0; }
.h-num { color: var(--muted); font-size: 12px; }
.h-dice { display: flex; gap: 3px; }
.h-dice img { width: 22px; height: 22px; border-radius: 5px; }
.h-top { text-align: right; color: var(--muted); font-size: 12px; }

/* ------------------------------------------------------------ dialogs */
.dialog {
  width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--card);
  color: var(--text);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(3px); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--card); }
.dialog-head h2 { margin: 0; color: var(--text); text-transform: none; letter-spacing: 0; font-size: 18px; }
.dialog-body { padding: 8px 20px 20px; }
.rules { padding-left: 20px; }
.rules li { margin: 6px 0; }
kbd { padding: 1px 6px; border-radius: 5px; border: 1px solid var(--line); background: var(--bg-2); font: 700 12px Inter, sans-serif; }
.settings fieldset { border: 1px solid var(--line); border-radius: 12px; margin: 12px 0; padding: 10px 14px 14px; }
.settings legend { font-weight: 800; padding: 0 6px; }
.row { display: flex; align-items: center; gap: 10px; margin: 8px 0; }
.row select, .row input[type="range"] { flex: 1; }
select {
  background: var(--bg-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 8px;
  font: inherit;
}
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--red); }
input[type="range"] { accent-color: var(--gold); }

/* ------------------------------------------------------------- toasts */
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 20; max-width: min(380px, calc(100vw - 32px)); }
.toast {
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--card-2);
  border: 1px solid var(--line);
  border-left: 4px solid var(--gold);
  box-shadow: var(--shadow);
  animation: pop 0.2s both;
  transition: opacity 0.4s, transform 0.4s;
}
.toast.error { border-left-color: var(--loss); }
.toast.warn { border-left-color: #ffb347; }
.toast.out { opacity: 0; transform: translateY(8px); }

/* --------------------------------------------------------- responsive */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
  .scene { max-height: none; }
}
@media (max-width: 640px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  .brand-mark { width: 36px; height: 36px; }
  .brand h1 { font-size: 19px; }
  .brand p { display: none; }
  .top-actions .btn { padding: 8px 10px; font-size: 13px; }
  .layout { padding: 12px 12px calc(96px + env(safe-area-inset-bottom)); gap: 12px; }
  .scene { aspect-ratio: 5 / 4; min-height: 0; }
  .scene.portrait { aspect-ratio: 4 / 5; max-height: calc(100vh - 190px); }
  .btn.start { font-size: 17px; }
  .scene-wrap { border-radius: 12px; }
  .result-strip { min-height: 60px; padding: 10px; justify-content: center; }
  .dice-row { gap: 5px; }
  .die { width: 42px; height: 42px; }
  .start-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    background: linear-gradient(180deg, rgba(20, 14, 10, 0), rgba(20, 14, 10, 0.92) 35%);
  }
  .btn.start { height: 58px; }
  .tile img { width: 40px; height: 40px; }
  .tile-count { font-size: 20px; }
  .card { padding: 14px; }
  .toasts { bottom: calc(90px + env(safe-area-inset-bottom)); }
}
@media (max-width: 360px) {
  .die { width: 36px; height: 36px; }
  .top-actions .btn { padding: 6px 8px; font-size: 12px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
