:root {
  --bg: #1F2023;
  --raised: #2A2B2F;
  --line: #3A3B40;
  --text: #E8E6E1;
  --muted: #8E8D89;
  --blue: #4BA3E3;
  --green: #8FD694;
  --yellow: #F5C66B;
  --red: #FF6B6B;
  --magenta: #D78BE5;
  --cyan: #5FD3D9;
  --felt: #22302A;
  --felt-edge: #2E3F36;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 var(--mono);
  overflow: hidden;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus { outline: none; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- layout ---------- */
.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
}
.main {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.panel {
  flex: 0 0 auto;
  height: 31%;
  min-height: 100px;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--raised);
  overflow: hidden;
}
@media (min-width: 660px) {
  .app { flex-direction: row; }
  .panel { height: auto; width: 38%; max-width: 320px; min-width: 220px; }
}

/* ---------- shelf ---------- */
.shelf {
  display: flex;
  justify-content: space-between;
  gap: 4px;
}
.shelf-die {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 4px 0 2px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  transition: background-color .15s, border-color .15s, transform .15s;
}
.shelf-die .die-svg { width: 34px; height: 34px; }
.shelf-label { font-size: 11px; color: var(--muted); }
.shelf-die:hover { background: var(--raised); border-color: var(--line); }
.shelf-die:hover .shelf-label { color: var(--text); }
.shelf-die:active { transform: translateY(1px) scale(.96); }

/* ---------- dice ---------- */
.die-svg { display: block; overflow: visible; }
.die-body > * { fill: #26272B; stroke: var(--c); stroke-width: 4; stroke-linejoin: round; }
.die-facets > * { fill: none; stroke: var(--c); stroke-width: 1.5; opacity: .35; stroke-linejoin: round; }
.die-svg text {
  fill: var(--c);
  font-family: var(--mono);
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: central;
  paint-order: stroke;
  stroke: #26272B;
  stroke-width: 6px;
}

/* ---------- tray ---------- */
.tray {
  position: relative;
  flex: 1 1 auto;
  min-height: 92px;
  display: flex;
  flex-direction: column;
  border-radius: 12px;
  background:
    radial-gradient(ellipse at 50% 40%, rgba(255,255,255,.035), transparent 70%),
    var(--felt);
  box-shadow: inset 0 0 0 1px var(--felt-edge), inset 0 6px 18px rgba(0,0,0,.35);
  overflow: hidden;
}
.tray-clear {
  position: absolute; top: 6px; right: 6px; z-index: 2;
  width: 26px; height: 26px; line-height: 1;
  border-radius: 6px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
}
.tray-clear[hidden] { display: none; }
.tray-clear:hover { color: var(--text); border-color: var(--felt-edge); background: rgba(0,0,0,.18); }

.tray-dice {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  flex-wrap: wrap;
  align-content: safe center;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 8px 34px 2px;
  overflow-x: hidden;
  overflow-y: auto;
  scrollbar-width: thin;
  --size: 54px;
}
.die { position: relative; width: var(--size); height: var(--size); flex: 0 0 auto; }
.die .die-svg { width: 100%; height: 100%; filter: drop-shadow(0 3px 2px rgba(0,0,0,.4)); }
.die.preview .die-svg { opacity: .55; filter: none; }
.die.preview .die-body > * { stroke-dasharray: 6 5; }
.die.neg::before {
  content: "−"; position: absolute; left: -9px; top: 50%; transform: translateY(-50%);
  color: var(--muted); font-weight: 700;
}
.die.dropped .die-svg { opacity: .35; filter: grayscale(.6); }
.die.dropped::after {
  content: ""; position: absolute; left: 12%; right: 12%; top: 50%;
  border-top: 2px solid var(--muted); transform: rotate(-20deg);
}
.tray-empty {
  margin: 0; text-align: center; color: var(--text); padding: 0 8px;
}
.tray-empty span { color: var(--muted); font-size: 12px; }

.tray-foot {
  display: flex;
  align-items: flex-end;
  gap: 12px;
  padding: 0 12px 7px;
  min-height: 30px;
}
.total {
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
  color: var(--text);
  flex: 0 0 auto;
  font-variant-numeric: tabular-nums;
}
.total:empty { display: none; }
.tray-meta { min-width: 0; flex: 1 1 auto; }
.breakdown {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.flair { font-size: 12px; font-weight: 700; min-height: 0; }
.flair:empty { display: none; }
.flair.crit { color: var(--yellow); }
.flair.fumble { color: #E59A9A; }
.tray.crit { box-shadow: inset 0 0 0 1px rgba(245,198,107,.45), inset 0 6px 18px rgba(0,0,0,.35), inset 0 0 30px rgba(245,198,107,.08); }
.tray.crit .total { color: var(--yellow); }

/* tumble */
.die.tumbling { animation: tumble .6s cubic-bezier(.2,.7,.3,1) both; }
@keyframes tumble {
  0%   { transform: translate(calc(var(--dx) * -3), -26px) rotate(var(--spin)) scale(.8); opacity: 0; }
  25%  { opacity: 1; }
  60%  { transform: translate(var(--dx), var(--dy)) rotate(calc(var(--r) * -2)) scale(1.04); }
  100% { transform: translate(0, 0) rotate(var(--r)) scale(1); }
}
.die:not(.preview) { transform: rotate(var(--r)); }
.die.dropped { transform: rotate(var(--r)) scale(.86); }

/* natural 20: a warm glow and a few sparks, once */
.die.crit .die-svg { filter: drop-shadow(0 0 6px rgba(245,198,107,.55)) drop-shadow(0 3px 2px rgba(0,0,0,.4)); }
.die.crit { animation: crit-pop .5s ease-out both; }
@keyframes crit-pop {
  0% { transform: rotate(var(--r)) scale(1); }
  45% { transform: rotate(0deg) scale(1.16); }
  100% { transform: rotate(0deg) scale(1.08); }
}
.spark { position: absolute; inset: 0; pointer-events: none; }
.spark i {
  position: absolute; left: 50%; top: 50%; width: 5px; height: 5px; margin: -2.5px;
  border-radius: 50%; background: var(--yellow); opacity: 0;
  animation: spark .8s ease-out .05s both;
}
.spark i:nth-child(1) { --a: 0deg; }
.spark i:nth-child(2) { --a: 60deg; background: var(--text); }
.spark i:nth-child(3) { --a: 120deg; }
.spark i:nth-child(4) { --a: 180deg; background: var(--text); }
.spark i:nth-child(5) { --a: 240deg; }
.spark i:nth-child(6) { --a: 300deg; background: var(--text); }
@keyframes spark {
  0% { transform: rotate(var(--a)) translateY(0) scale(.6); opacity: 0; }
  20% { opacity: 1; }
  100% { transform: rotate(var(--a)) translateY(calc(var(--size) * -.85)) scale(.3); opacity: 0; }
}

/* natural 1: a sympathetic wobble */
.die.fumble { animation: wobble .7s ease-in-out both; }
.die.fumble .die-body > * { fill: #2E2526; }
@keyframes wobble {
  0% { transform: rotate(var(--r)); }
  20% { transform: rotate(-9deg) translateY(1px); }
  40% { transform: rotate(7deg); }
  60% { transform: rotate(-4deg); }
  80% { transform: rotate(2deg); }
  100% { transform: rotate(0deg) translateY(2px); }
}

/* ---------- controls ---------- */
.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.btn {
  height: 30px;
  padding: 0 12px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--raised);
  color: var(--text);
}
.btn:hover { border-color: #55565c; background: #303136; }
.btn.sq { width: 30px; padding: 0; font-size: 16px; }
.btn.roll {
  margin-left: auto;
  padding: 0 18px;
  background: var(--blue);
  border-color: var(--blue);
  color: #10161c;
  font-weight: 700;
}
.btn.roll:hover { background: #63b2ea; }
.btn.roll:active { transform: translateY(1px); }

.mod { display: flex; align-items: center; gap: 4px; }
.mod-val {
  min-width: 40px;
  text-align: center;
  font-weight: 700;
  color: var(--green);
}

.seg {
  display: inline-flex;
  margin: 0;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--bg);
}
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; margin: 0; }
.seg label {
  padding: 3px 9px;
  border-radius: 5px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.seg label:hover { color: var(--text); }
.seg input:checked + label { background: var(--raised); color: var(--yellow); box-shadow: inset 0 0 0 1px var(--line); }
.seg input:focus-visible + label { outline: 2px solid var(--blue); outline-offset: 1px; }

.notation {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #18191B;
}
.notation:focus-within { border-color: var(--blue); }
.notation .prompt { color: var(--green); font-weight: 700; }
.notation input {
  flex: 1 1 auto;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 0;
  height: 100%;
}
.notation input:focus-visible { outline: none; }
.notation input::placeholder { color: #6c6b68; }
.notation:has(input[aria-invalid="true"]) { border-color: var(--red); }

.msg { font-size: 12px; min-height: 0; color: var(--muted); }
.msg:empty { display: none; }
.msg.error { color: var(--red); }
.msg.ok { color: var(--green); }

/* ---------- panel ---------- */
.tabs {
  display: flex;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.tabs [role="tab"] {
  flex: 1 1 0;
  padding: 6px 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tabs [role="tab"]:hover { color: var(--text); }
.tabs [role="tab"][aria-selected="true"] { color: var(--yellow); border-bottom-color: var(--yellow); }
.tabs [role="tab"]:focus-visible { outline-offset: -2px; }

.pane {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.pane[hidden] { display: none; }
.list {
  list-style: none;
  margin: 0;
  padding: 4px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}
.list:empty { display: none; }
.empty { margin: 0; padding: 12px; color: var(--muted); flex: 1 1 auto; }
.empty[hidden] { display: none; }

.hist-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  text-align: left;
}
.hist-item:hover { background: rgba(255,255,255,.04); }
.h-total { min-width: 2.6em; text-align: right; font-weight: 700; color: var(--text); }
.hist-item.crit .h-total { color: var(--yellow); }
.hist-item.fumble .h-total { color: #E59A9A; }
.h-main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.h-expr, .h-detail { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.h-expr { color: var(--green); }
.h-detail { color: var(--muted); font-size: 11px; }
.h-time { color: var(--muted); font-size: 11px; flex: 0 0 auto; }

.pane-foot {
  flex: 0 0 auto;
  display: flex;
  justify-content: flex-end;
  padding: 4px 8px 6px;
  border-top: 1px solid var(--line);
  font-size: 12px;
}
.pane-foot[hidden] { display: none; }
.confirm { display: flex; gap: 10px; align-items: center; }
.confirm[hidden] { display: none; }
.link {
  border: 0; background: transparent; padding: 2px 4px; border-radius: 4px;
  color: var(--muted); text-decoration: underline; text-underline-offset: 3px;
}
.link:hover { color: var(--text); }
.link.danger { color: var(--red); }
.link[hidden] { display: none; }

.quick-row { display: flex; align-items: stretch; gap: 2px; }
.quick-run {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 5px 8px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  text-align: left;
}
.quick-run:hover { background: rgba(255,255,255,.04); }
.q-name { color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.q-expr { color: var(--green); flex: 0 0 auto; }
.q-arrow { color: var(--blue); flex: 0 0 auto; }
.q-expr { margin-left: auto; }
.quick-del {
  flex: 0 0 auto;
  width: 26px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  font-size: 15px;
}
.quick-del:hover { color: var(--red); background: rgba(255,107,107,.08); }

.quick-form {
  flex: 0 0 auto;
  display: flex;
  gap: 4px;
  padding: 6px;
  border-top: 1px solid var(--line);
}
.quick-form input {
  min-width: 0;
  height: 28px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #18191B;
}
.quick-form input:focus-visible { outline: none; border-color: var(--blue); }
#qName { flex: 1.3 1 0; }
#qExpr { flex: 1 1 0; }
.quick-form .btn { height: 28px; padding: 0 10px; }
#quickMsg { padding: 0 8px 6px; }

/* ---------- small heights ---------- */
@media (max-height: 400px) {
  .app { padding: 8px; gap: 6px; }
  .main { gap: 6px; }
  .shelf-die .die-svg { width: 28px; height: 28px; }
  .shelf-label { font-size: 10px; }
  .panel { min-height: 96px; }
}
@media (max-width: 380px) {
  .seg label { padding: 3px 6px; }
  .btn.roll { padding: 0 12px; }
  .tray-dice { padding: 10px 28px 4px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .spark { display: none; }
}
