:root {
  --bg: #161a22;
  --panel: #1e232d;
  --panel-2: #262c38;
  --line: #333b4a;
  --ink: #eef1f6;
  --muted: #9aa3b4;
  --accent: #f0a63a;
  --accent-ink: #1b1306;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Inter", "Segoe UI", system-ui, sans-serif;
  font-size: 15px;
  overflow: hidden;
}

.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "top top"
    "palette stage"
    "palette bar";
}

/* ---------- Top bar ---------- */

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 18px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 17px;
  letter-spacing: 0.01em;
}

.brand svg { width: 26px; height: 26px; }

.topbar-right { display: flex; align-items: center; gap: 16px; }

.back, .link-btn {
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  text-decoration: none;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.back:hover, .link-btn:hover { color: var(--ink); text-decoration: underline; }

/* ---------- Dice palette ---------- */

.palette-wrap {
  grid-area: palette;
  width: 128px;
  padding: 16px 14px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
}

.palette-title {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.palette { display: grid; gap: 8px; }

.die-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.1s, border-color 0.15s;
}

.die-btn:hover { border-color: #4b5568; }
.die-btn:active { transform: scale(0.97); cursor: grabbing; }
.die-btn:focus-visible, .btn:focus-visible, .link-btn:focus-visible, .back:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.die-btn svg { width: 30px; height: 30px; flex: none; }

.palette-help {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.45;
}

.ghost {
  position: fixed;
  left: 0;
  top: 0;
  width: 46px;
  height: 46px;
  margin: -23px 0 0 -23px;
  pointer-events: none;
  z-index: 50;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.45));
}

.ghost svg { width: 100%; height: 100%; }

/* ---------- 3D stage ---------- */

.stage {
  grid-area: stage;
  position: relative;
  min-height: 0;
  overflow: hidden;
}

.stage canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.hint {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translate(-50%, -8px);
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(20, 24, 32, 0.88);
  border: 1px solid var(--line);
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
  max-width: calc(100% - 32px);
  overflow: hidden;
  text-overflow: ellipsis;
}

.hint.show { opacity: 1; transform: translate(-50%, 0); }

.pause {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(10, 12, 18, 0.55);
  cursor: pointer;
}

.pause-card {
  padding: 18px 28px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  text-align: center;
}

.pause-card p { margin: 0; color: var(--muted); }
.pause-card .pause-title { margin-bottom: 4px; color: var(--ink); font-size: 22px; font-weight: 700; }

/* ---------- Bottom bar ---------- */

.bar {
  grid-area: bar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px 18px;
  min-height: 78px;
  padding: 12px 18px calc(12px + env(safe-area-inset-bottom, 0px));
  background: var(--panel);
  border-top: 1px solid var(--line);
}

.bar-info { min-width: 0; flex: 1 1 320px; }

.results {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 40px;
}

.results .idle, .results .rolling { color: var(--muted); }
.results .rolling { font-weight: 600; color: var(--ink); }

.results .expr {
  margin-right: 6px;
  color: var(--muted);
  font-weight: 600;
}

.chip svg { display: block; width: 40px; height: 40px; }
.results.many .chip svg { width: 30px; height: 30px; }

.total {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  margin-left: 10px;
  padding: 4px 14px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 24px;
  font-weight: 700;
}

.total small { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; }

.loaded {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.bar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  padding: 11px 18px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.btn:hover:not(:disabled) { border-color: #4b5568; }

.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.noscript { padding: 24px; }

@media (max-width: 480px) {
  .back .long { display: none; }
}

/* ---------- Narrow screens: palette becomes a row under the top bar ---------- */

@media (max-width: 720px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas: "top" "palette" "stage" "bar";
  }

  .topbar { padding-inline: 16px; }
  .brand { font-size: 16px; }
  .topbar-right { gap: 12px; }

  .palette-wrap {
    width: auto;
    padding: 10px 16px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    overflow: visible;
  }

  .palette-title, .palette-help { display: none; }

  .palette { grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 5px; }

  .die-btn {
    flex-direction: column;
    gap: 2px;
    padding: 6px 2px;
    font-size: 12px;
  }

  .die-btn svg { width: 26px; height: 26px; }

  .bar { padding-inline: 16px; }
  .bar-actions { width: 100%; }
  .bar-actions .btn { flex: 1; }
  .chip svg { width: 34px; height: 34px; }
  .total { font-size: 20px; }
}
