:root {
  --bg: #141210;
  --ivory: #f3e9d2;
  --ivory-deep: #e3d3b0;
  --red: #b5271d;
  --red-bright: #d33a2c;
  --red-light: #ff7a6e;
  --red-dark: #7e1712;
  --gold: #e8c877;
  --muted: #a99c86;
  --line: rgba(243, 233, 210, 0.14);
  --radius: 16px;
  --die-feat: clamp(92px, 27vw, 120px);
  --die-success: clamp(74px, 21vw, 96px);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background:
    radial-gradient(120% 80% at 50% 0%, #27211b 0%, #17130f 55%, #0d0b09 100%);
  color: var(--ivory);
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  line-height: 1.4;
}

body.is-modal-open {
  overflow: hidden;
}

::selection {
  background: rgba(211, 58, 44, 0.4);
}

[hidden] {
  display: none !important;
}

.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;
}

.svg-sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
  pointer-events: none;
}

.app {
  width: min(100% - 32px, 500px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 0 max(28px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.app__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.app__title {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 600;
  letter-spacing: 0.03em;
}

.app__subtitle {
  margin: 2px 0 0;
  font-size: 0.64rem;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  color: var(--muted);
}

.app__main {
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.app__footer {
  margin-top: auto;
  padding-top: 8px;
  text-align: center;
  font-size: 0.68rem;
  color: var(--muted);
}

.app__footer p {
  margin: 0;
}

.app__footer p + p {
  margin-top: 4px;
}

.app__byline a {
  color: var(--ivory);
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-color: rgba(243, 233, 210, 0.5);
}

.app__byline a:hover {
  color: var(--red-light);
}

.app__byline a:focus-visible {
  outline: 2px solid var(--ivory);
  outline-offset: 2px;
  border-radius: 3px;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(243, 233, 210, 0.04);
  color: var(--ivory);
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.icon-button svg {
  width: 22px;
  height: 22px;
}

.icon-button:hover {
  background: rgba(243, 233, 210, 0.09);
  border-color: rgba(243, 233, 210, 0.3);
}

.icon-button:focus-visible {
  outline: 2px solid var(--ivory);
  outline-offset: 2px;
}

.icon-button--small {
  width: 44px;
  height: 44px;
  border-radius: 10px;
}

.dice-section__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.dice-section__title {
  margin: 0;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--muted);
}

.dice-section__hint {
  color: var(--muted);
  letter-spacing: 0.1em;
}

.dice-section__count {
  font-size: 0.7rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.dice-row {
  display: grid;
  justify-content: center;
  gap: 14px;
}

.dice-row--feat {
  grid-template-columns: repeat(2, var(--die-feat));
}

.dice-row--success {
  grid-template-columns: repeat(3, var(--die-success));
}

.die {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  background: none;
  color: var(--red);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s ease, filter 0.15s ease;
}

.die:disabled {
  cursor: default;
}

.die:focus-visible {
  outline: 2px solid var(--ivory);
  outline-offset: 4px;
  border-radius: 16px;
}

.die__inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  background: linear-gradient(145deg, var(--red-bright), var(--red-dark));
  box-shadow: 0 7px 16px rgba(0, 0, 0, 0.45);
  transition: transform 0.18s ease, opacity 0.18s ease, filter 0.18s ease;
}

.die--d6 .die__inner {
  border-radius: 16%;
}

.die__art {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  transition: transform 0.18s ease, opacity 0.18s ease, filter 0.18s ease;
}

.die__face {
  position: absolute;
  inset: 5px;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(circle at 32% 22%, rgba(255, 255, 255, 0.95), transparent 58%),
    linear-gradient(150deg, #fbf4e3, #eadcc0 58%, #d8c5a3);
  border-radius: 11%;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8);
}

.die__face::after {
  content: "";
  position: absolute;
  inset: 7%;
  border: 1.5px solid var(--red-dark);
  border-radius: 9%;
  opacity: 0.55;
  pointer-events: none;
}

.die__value {
  font-size: clamp(1.6rem, 7.2vw, 2.25rem);
  font-weight: 700;
  line-height: 1;
  color: var(--red);
  font-variant-numeric: lining-nums tabular-nums;
}

.die__glyph {
  width: 46%;
  height: 46%;
  color: var(--red);
}

.die[aria-pressed="false"] .die__inner,
.die--feat[aria-pressed="false"] .die__art {
  opacity: 0.48;
  filter: saturate(0.7) brightness(0.95);
}

.die[aria-pressed="true"] .die__inner,
.die--feat[aria-pressed="true"] .die__art {
  transform: translateY(-3px);
}

.die[aria-pressed="true"] {
  filter: drop-shadow(0 0 10px rgba(211, 58, 44, 0.4));
}

.die.is-discarded {
  opacity: 0.42;
}

.die.is-discarded .die__inner,
.die--feat.is-discarded .die__art {
  transform: none;
}

.die.is-negated .die__value {
  text-decoration: line-through;
  opacity: 0.55;
}

.die.is-rolling .die__inner,
.die--feat.is-rolling .die__art {
  animation: die-tumble 0.42s ease-in-out;
}

@keyframes die-tumble {
  0%,
  100% {
    transform: rotate(0deg) scale(1);
  }
  20% {
    transform: rotate(-9deg) scale(1.04);
  }
  45% {
    transform: rotate(7deg) scale(0.97);
  }
  70% {
    transform: rotate(-4deg) scale(1.02);
  }
}

@media (hover: hover) {
  .die[aria-pressed="false"]:hover .die__inner,
  .die--feat[aria-pressed="false"]:hover .die__art {
    opacity: 0.78;
    filter: saturate(0.9);
  }
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.conditions {
  display: flex;
  gap: 10px;
  margin: 0;
  padding: 0;
  border: 0;
}

.chip {
  position: relative;
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(243, 233, 210, 0.035);
  color: var(--muted);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.chip input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.chip span {
  font-size: 0.9rem;
}

.chip:has(input:checked) {
  border-color: var(--red-bright);
  background: rgba(181, 39, 29, 0.18);
  color: var(--ivory);
}

.chip:has(input:focus-visible) {
  outline: 2px solid var(--ivory);
  outline-offset: 2px;
}

.mode {
  margin: 0;
  padding: 0;
  border: 0;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(243, 233, 210, 0.03);
}

.segment {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border-radius: 9px;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.segment input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.segment span {
  font-size: 0.82rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.segment:has(input:checked) {
  background: linear-gradient(180deg, var(--red-bright), var(--red-dark));
  color: var(--ivory);
  box-shadow: 0 4px 12px rgba(126, 23, 18, 0.35);
}

.segment:has(input:focus-visible) {
  outline: 2px solid var(--ivory);
  outline-offset: 2px;
}

.roll-button {
  width: 100%;
  min-height: 56px;
  border: 0;
  border-radius: 14px;
  background: linear-gradient(180deg, var(--red-bright), var(--red-dark));
  color: var(--ivory);
  font: inherit;
  font-size: 1.05rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow:
    0 8px 20px rgba(126, 23, 18, 0.35),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
  transition: filter 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}

.roll-button:not(:disabled):hover {
  filter: brightness(1.06);
}

.roll-button:not(:disabled):active {
  transform: translateY(1px);
}

.roll-button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  box-shadow: none;
}

.roll-button:focus-visible {
  outline: 2px solid var(--ivory);
  outline-offset: 2px;
}

.result {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 132px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(243, 233, 210, 0.055), rgba(243, 233, 210, 0.025));
}

.result__placeholder {
  margin: auto 0;
  text-align: center;
  font-size: 0.95rem;
  color: var(--muted);
}

.result__placeholder--rolling {
  animation: pulse 0.9s ease-in-out infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.45;
  }
}

.result__head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
}

.result__total {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.result__total-label {
  font-size: 0.62rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--muted);
}

.result__total-value {
  font-size: clamp(2.6rem, 13vw, 3.4rem);
  font-weight: 700;
  line-height: 0.9;
  color: var(--ivory);
  font-variant-numeric: lining-nums tabular-nums;
}

.result__outcome {
  max-width: 60%;
  margin: 0;
  font-size: 0.85rem;
  text-align: right;
}

.result__outcome--success {
  color: var(--gold);
}

.result__outcome--failure {
  color: var(--red-light);
}

.result__outcome--neutral {
  color: var(--muted);
}

.result__dice {
  display: grid;
  gap: 10px;
}

.result__group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.result__group-label {
  flex: none;
  width: 56px;
  font-size: 0.62rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}

.result__chips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.result__math {
  margin: 0;
  font-size: 0.82rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.result__math strong {
  color: var(--ivory);
}

.result__badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.result__none {
  font-size: 0.82rem;
  color: var(--muted);
}

.mini-die {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1.5px solid var(--red-dark);
  border-radius: 9px;
  background: linear-gradient(150deg, #fbf4e3, #e6d6b8);
  color: var(--red);
  font-size: 0.92rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.mini-die--feat {
  border-radius: 11px;
  box-shadow: inset 0 0 0 2px rgba(243, 233, 210, 0.65);
}

.mini-die .die__value {
  font-size: 0.95rem;
}

.mini-die .die__glyph {
  width: 20px;
  height: 20px;
}

.mini-die.is-kept {
  outline: 2px solid var(--red-bright);
  outline-offset: 2px;
}

.mini-die.is-discarded {
  opacity: 0.35;
}

.mini-die.is-zeroed {
  text-decoration: line-through;
  opacity: 0.5;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.55rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 0.72rem;
  color: var(--muted);
}

.badge--success {
  border-color: rgba(232, 200, 119, 0.6);
  color: var(--gold);
}

.badge--failure {
  border-color: rgba(211, 58, 44, 0.6);
  color: var(--red-bright);
}

.history {
  width: min(560px, calc(100vw - 32px));
  max-height: min(82vh, 760px);
  margin: auto;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #1a1612;
  color: var(--ivory);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
}

.history[open] {
  display: flex;
  flex-direction: column;
}

.history::backdrop {
  background: rgba(5, 4, 3, 0.72);
  backdrop-filter: blur(4px);
}

.history__header {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}

.history__title {
  margin: 0;
  font-size: 1rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ivory);
}

.history__body {
  flex: 1 1 auto;
  min-height: 0;
  padding: 4px 18px 14px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(243, 233, 210, 0.25) transparent;
}

.history__body:focus-visible {
  outline: 2px solid var(--ivory);
  outline-offset: -2px;
}

.history__footer {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 18px;
  border-top: 1px solid var(--line);
}

.history__count {
  font-size: 0.78rem;
  color: var(--muted);
}

.history__empty {
  margin: 0;
  padding: 28px 8px;
  text-align: center;
  font-size: 0.9rem;
  color: var(--muted);
}

.history__item {
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
}

.history__item:last-child {
  border-bottom: 0;
}

.history__item-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.history__time {
  font-size: 0.72rem;
  color: var(--muted);
}

.history__total {
  font-size: 1.25rem;
  color: var(--ivory);
  font-variant-numeric: tabular-nums;
}

.history__dice {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}

.history__group {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.history__plus {
  font-size: 0.8rem;
  color: var(--muted);
}

.history__tags {
  margin: 6px 0 0;
  font-size: 0.7rem;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.history__none {
  color: var(--muted);
}

.text-button {
  min-height: 44px;
  padding: 0 4px;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.8rem;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}

.text-button:hover {
  color: var(--ivory);
}

.text-button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.text-button:focus-visible {
  outline: 2px solid var(--ivory);
  outline-offset: 2px;
  border-radius: 4px;
}

@media (max-width: 600px) {
  .history {
    width: calc(100vw - 16px);
    max-width: none;
    height: calc(100dvh - 16px);
    max-height: none;
    margin: 8px auto;
    border-radius: 16px;
  }

}

@media (max-width: 440px) {
  .result__outcome {
    max-width: 100%;
    text-align: left;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
