  /* Fonts are served from this site, latin subset only. Cinzel and
     IBM Plex Sans are variable fonts, so one file holds every weight. */
  @font-face { font-family: Cinzel; font-weight: 400 900; font-display: swap; src: url(fonts/cinzel-v26.woff2) format("woff2"); }
  @font-face { font-family: "IBM Plex Sans"; font-weight: 100 700; font-display: swap; src: url(fonts/ibm-plex-sans-v23.woff2) format("woff2"); }
  @font-face { font-family: "IBM Plex Mono"; font-weight: 400; font-display: swap; src: url(fonts/ibm-plex-mono-400-v20.woff2) format("woff2"); }
  @font-face { font-family: "IBM Plex Mono"; font-weight: 600; font-display: swap; src: url(fonts/ibm-plex-mono-600-v20.woff2) format("woff2"); }

  :root {
    --felt:        #16302a;
    --felt-deep:   #0a1815;
    --bone:        #e9e1cf;
    --brass:       #c9a227;
    --oxblood:     #94322b;
    --chalk:       #cfd8ce;
    --chalk-dim:   #8ba093;
    --sheet:       #0f221d;
    --hair:        rgba(201, 162, 39, 0.22);
  }

  * { box-sizing: border-box; }

  body {
    margin: 0;
    height: 100dvh;
    overflow: hidden;
    background: var(--felt-deep);
    color: var(--chalk);
    font-family: "IBM Plex Sans", system-ui, -apple-system, sans-serif;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
  }

  /* A canvas takes its size from its pixel buffer, not from inset: 0.
     The buffer is up to 1.5 times the viewport on a phone, so without
     this size the tray showed only two thirds of the screen. */
  #stage {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
  }

  /* ---------- top bar ---------- */

  .bar {
    position: fixed;
    top: 0; left: 0; right: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    padding-top: calc(14px + env(safe-area-inset-top, 0px));
    pointer-events: none;
    z-index: 3;
  }

  .wordmark {
    font-family: Cinzel, Georgia, serif;
    font-weight: 700;
    font-size: 15px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--brass);
    margin: 0;
    text-shadow: 0 1px 3px rgba(0,0,0,0.8);
  }

  .spacer { flex: 1; }

  button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }

  .icon-btn {
    pointer-events: auto;
    width: 42px; height: 42px;
    flex: none;
    display: grid;
    place-items: center;
    border-radius: 50%;
    border: 1px solid var(--hair);
    background: rgba(6, 18, 15, 0.72);
    color: var(--brass);
    backdrop-filter: blur(6px);
    transition: transform 0.18s ease, background 0.18s ease;
  }
  .icon-btn:active { transform: scale(0.92); }
  .icon-btn svg { width: 21px; height: 21px; }
  .icon-btn.off { color: var(--chalk-dim); border-color: rgba(139,160,147,0.25); }

  :where(button, input, [tabindex]):focus-visible {
    outline: 2px solid var(--brass);
    outline-offset: 3px;
  }

  /* ---------- bottom readout ---------- */

  .readout {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    display: flex;
    align-items: flex-end;
    gap: 12px;
    padding: 20px 14px;
    padding-bottom: calc(18px + env(safe-area-inset-bottom, 0px));
    background: linear-gradient(to top, rgba(5, 15, 13, 0.96) 62%, rgba(5, 15, 13, 0));
    pointer-events: none;
    z-index: 3;
  }

  .total { display: flex; flex-direction: column; line-height: 1; flex: none; }
  .readout { --total-size: clamp(52px, 15vw, 84px); }
  .total-label {
    font-size: 10px;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--chalk-dim);
    margin-bottom: 7px;
  }
  .total-value {
    font-family: "IBM Plex Mono", ui-monospace, monospace;
    font-weight: 600;
    font-size: var(--total-size);
    font-variant-numeric: tabular-nums;
    color: var(--bone);
    text-shadow: 0 2px 10px rgba(0,0,0,0.7);
    /* The readout swaps a big number for the smaller word "rolling". The
       big line box is reserved in both states, or the Roll button beside
       it moves every time a roll starts and ends. The width holds four
       tabular digits, so a tray of 24 d100 totalling 2400 does not push
       the button either. */
    display: flex;
    align-items: flex-end;
    min-height: var(--total-size);
    min-width: calc(var(--total-size) * 2.45);
  }
  .total-value.stale { color: var(--chalk-dim); }
  .total-value.word { font-size: clamp(34px, 10vw, 60px); }
  .total-value.muted { color: var(--chalk-dim); font-size: clamp(20px, 5.5vw, 28px); letter-spacing: 0.05em; }

  .breakdown {
    flex: 1;
    display: flex;
    flex-wrap: wrap-reverse;
    justify-content: flex-end;
    align-content: flex-end;
    gap: 6px;
    max-height: 116px;
    overflow: hidden;
  }

  .chip {
    display: inline-flex;
    align-items: baseline;
    gap: 5px;
    padding: 5px 10px;
    border-radius: 3px;
    border: 1px solid var(--hair);
    background: rgba(9, 26, 21, 0.9);
    font-family: "IBM Plex Mono", ui-monospace, monospace;
    font-size: 16px;
    font-variant-numeric: tabular-nums;
    color: var(--bone);
    white-space: nowrap;
  }
  .chip .die-tag { color: var(--chalk-dim); font-size: 12px; }
  .chip.crit   { border-color: var(--brass); color: var(--brass); }
  .chip.fumble { border-color: var(--oxblood); color: #e0857e; }
  .chip.heads, .chip.tails { color: #e3bb52; }

  .roll-btn {
    pointer-events: auto;
    align-self: center;
    flex: none;
    padding: 13px 24px;
    border-radius: 999px;
    border: 1px solid var(--brass);
    background: rgba(201, 162, 39, 0.14);
    color: var(--brass);
    font-family: Cinzel, Georgia, serif;
    font-weight: 700;
    font-size: 13px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    transition: transform 0.15s ease, background 0.15s ease;
  }
  .roll-btn:active { transform: scale(0.95); background: rgba(201, 162, 39, 0.28); }

  /* On a phone the three columns leave the chips about 90px of width, so a
     roll of eight dice shows two of them. Below 520px the chips take a row
     of their own above the total. */
  @media (max-width: 520px) {
    .readout { flex-wrap: wrap; align-items: center; }
    .breakdown {
      order: -1;
      flex: 0 0 100%;
      justify-content: flex-start;
      align-content: flex-start;
      flex-wrap: wrap;
      max-height: 104px;
      overflow-y: auto;
      pointer-events: auto;
      scrollbar-width: none;
    }
    .breakdown::-webkit-scrollbar { display: none; }
    /* One row of chips is kept free while the dice roll, so the readout
       does not grow when the result comes and cover the tray. */
    .breakdown { min-height: 33px; }
    .total { flex: 1; }
  }

  /* A phone on its side has about 400 px of height. The full-size bar
     and readout took two thirds of it and left the tray a thin strip. */
  @media (max-height: 520px) and (orientation: landscape) {
    .bar { padding-top: calc(6px + env(safe-area-inset-top, 0px)); padding-bottom: 6px; }
    .icon-btn { width: 36px; height: 36px; }
    .readout {
      --total-size: clamp(34px, 11vh, 52px);
      padding-top: 8px;
      padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
      align-items: center;
    }
    .total-label { margin-bottom: 3px; }
    .roll-btn { padding: 9px 20px; }
    .breakdown { max-height: 70px; align-content: center; }
  }

  [hidden] { display: none !important; }

  /* The one control that unlocks shake and tilt. It sits on the felt,
     not in a settings sheet, because nothing else about the app works
     the way it promises until this is granted. */
  .motion-prompt {
    position: fixed;
    left: 50%;
    bottom: calc(var(--readout-h, 146px) + 12px);
    transform: translateX(-50%);
    z-index: 4;
    width: min(88vw, 430px);
    min-height: 66px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0;
    padding: 0;
    border-radius: 18px;
    border: 2px solid var(--brass);
    background: rgba(30, 24, 6, 0.92);
    backdrop-filter: blur(8px);
    color: var(--brass);
    font-family: Cinzel, Georgia, serif;
    font-weight: 700;
    font-size: clamp(15px, 4.4vw, 19px);
    line-height: 1.25;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    text-wrap: balance;
    box-shadow: 0 10px 34px rgba(0, 0, 0, 0.6), 0 0 0 6px rgba(201, 162, 39, 0.09);
  }
  .motion-prompt svg { width: 30px; height: 30px; flex: none; }
  .motion-prompt:active { background: rgba(201, 162, 39, 0.3); }
  .motion-prompt.settled {
    border-color: var(--chalk-dim);
    color: var(--chalk-dim);
    box-shadow: 0 10px 34px rgba(0, 0, 0, 0.6);
  }
  .motion-prompt.settled .shake-icon { animation: none; }

  .shake-icon { animation: shakeit 2.4s ease-in-out infinite; transform-origin: 50% 50%; }
  @keyframes shakeit {
    0%, 72%, 100% { transform: rotate(0deg); }
    78% { transform: rotate(-13deg); }
    84% { transform: rotate(13deg); }
    90% { transform: rotate(-8deg); }
  }
  @media (prefers-reduced-motion: reduce) { .shake-icon { animation: none; } }

  .mp-main {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
    min-height: 62px;
    padding: 16px 6px 16px 22px;
  }
  /* Its own target, well clear of the main one, so a thumb aiming to
     grant motion does not dismiss the prompt instead. */
  .mp-x {
    align-self: stretch;
    display: flex;
    align-items: center;
    padding: 0 18px 0 12px;
    opacity: 0.55;
  }
  .mp-x svg { width: 19px; height: 19px; }
  .mp-x:active { opacity: 1; }

  .hint {
    position: fixed;
    left: 50%;
    top: 62%;
    transform: translateX(-50%);
    text-align: center;
    font-size: 12.5px;
    letter-spacing: 0.05em;
    color: rgba(207, 216, 206, 0.4);
    pointer-events: none;
    transition: opacity 0.6s ease;
    z-index: 2;
  }
  .hint.gone { opacity: 0; }

  /* ---------- settings sheet ---------- */

  .scrim {
    position: fixed;
    inset: 0;
    background: rgba(3, 10, 8, 0.66);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
    z-index: 4;
  }
  .scrim.open { opacity: 1; pointer-events: auto; }

  .sheet {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    max-height: 86dvh;
    overflow-y: auto;
    background: var(--sheet);
    border-top: 1px solid var(--hair);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -18px 50px rgba(0,0,0,0.6);
    transform: translateY(102%);
    transition: transform 0.32s cubic-bezier(0.22, 0.8, 0.3, 1);
    padding: 0 16px;
    padding-bottom: calc(22px + env(safe-area-inset-bottom, 0px));
    z-index: 5;
  }
  .sheet.open { transform: translateY(0); }

  .sheet-head {
    position: sticky;
    top: 0;
    background: var(--sheet);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 0 12px;
    border-bottom: 1px solid var(--hair);
    margin-bottom: 14px;
  }
  .sheet-head h2 {
    margin: 0;
    font-family: Cinzel, Georgia, serif;
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--brass);
  }
  .sheet-head .count {
    font-family: "IBM Plex Mono", monospace;
    font-size: 11px;
    color: var(--chalk-dim);
  }

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

  .row {
    display: grid;
    grid-template-columns: 42px 1fr auto;
    align-items: center;
    gap: 12px;
    padding: 8px 10px 8px 6px;
    border-radius: 8px;
    background: rgba(255,255,255,0.03);
  }
  .row.active { background: rgba(201, 162, 39, 0.1); }
  .row canvas { width: 42px; height: 42px; }

  .row .name {
    font-family: "IBM Plex Mono", monospace;
    font-size: 15px;
    font-weight: 600;
    color: var(--bone);
  }
  .row .range {
    display: block;
    font-family: "IBM Plex Sans", sans-serif;
    font-size: 11px;
    font-weight: 400;
    color: var(--chalk-dim);
    margin-top: 2px;
  }

  .stepper {
    display: flex;
    align-items: center;
    border: 1px solid var(--hair);
    border-radius: 7px;
    overflow: hidden;
  }
  .stepper button {
    width: 38px; height: 38px;
    font-size: 19px;
    line-height: 1;
    color: var(--brass);
    background: rgba(201, 162, 39, 0.07);
  }
  .stepper button:active { background: rgba(201, 162, 39, 0.24); }
  .stepper button[disabled] { color: rgba(139, 160, 147, 0.32); background: none; }
  .stepper .n {
    width: 34px;
    text-align: center;
    font-family: "IBM Plex Mono", monospace;
    font-variant-numeric: tabular-nums;
    font-size: 15px;
    color: var(--bone);
  }
  .stepper .n.zero { color: var(--chalk-dim); }

  .opts {
    margin-top: 18px;
    padding-top: 14px;
    border-top: 1px solid var(--hair);
    display: grid;
    gap: 15px;
  }
  .opt { display: flex; align-items: center; gap: 12px; }
  .opt label { font-size: 13.5px; flex: 1; }
  .opt .sub { display: block; font-size: 11px; color: var(--chalk-dim); margin-top: 2px; }
  .seg {
    display: flex;
    flex: none;
    border: 1px solid var(--hair);
    border-radius: 999px;
    overflow: hidden;
  }
  .seg button {
    padding: 7px 13px;
    font-size: 12px;
    letter-spacing: 0.06em;
    color: var(--chalk-dim);
    background: none;
  }
  .seg button + button { border-left: 1px solid var(--hair); }
  .seg button[aria-checked="true"] {
    background: rgba(201, 162, 39, 0.18);
    color: var(--brass);
  }

  .opt .val {
    font-family: "IBM Plex Mono", ui-monospace, monospace;
    font-variant-numeric: tabular-nums;
    color: var(--brass);
  }

  input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 122px;
    height: 22px;
    flex: none;
    background: none;
  }
  input[type="range"]::-webkit-slider-runnable-track { height: 3px; border-radius: 2px; background: rgba(201,162,39,0.3); }
  input[type="range"]::-moz-range-track { height: 3px; border-radius: 2px; background: rgba(201,162,39,0.3); }
  input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px; height: 18px; margin-top: -7.5px;
    border-radius: 50%; background: var(--brass); border: 0;
  }
  input[type="range"]::-moz-range-thumb { width: 18px; height: 18px; border: 0; border-radius: 50%; background: var(--brass); }

  .switch {
    width: 46px; height: 27px;
    border-radius: 999px;
    border: 1px solid var(--hair);
    background: rgba(0,0,0,0.35);
    position: relative;
    flex: none;
  }
  .switch::after {
    content: "";
    position: absolute;
    top: 3px; left: 3px;
    width: 19px; height: 19px;
    border-radius: 50%;
    background: var(--chalk-dim);
    transition: transform 0.2s ease, background 0.2s ease;
  }
  .switch[aria-checked="true"] { background: rgba(201, 162, 39, 0.32); }
  .switch[aria-checked="true"]::after { transform: translateX(19px); background: var(--brass); }

  .note { margin: 16px 0 0; font-size: 11.5px; line-height: 1.65; color: var(--chalk-dim); }

  .diag {
    margin: 10px 0 0;
    padding: 9px 10px;
    border-radius: 6px;
    border: 1px solid rgba(139, 160, 147, 0.22);
    background: rgba(0, 0, 0, 0.28);
    font-family: "IBM Plex Mono", ui-monospace, monospace;
    font-size: 10.5px;
    line-height: 1.6;
    color: var(--chalk-dim);
    white-space: pre-line;
    word-break: break-word;
  }
  .diag b { color: var(--bone); font-weight: 600; }
  .diag .bad { color: #e0857e; }
  .diag .good { color: var(--brass); }

  .motion-cta {
    display: none;
    width: 100%;
    margin-top: 14px;
    padding: 12px;
    border-radius: 8px;
    border: 1px solid var(--brass);
    background: rgba(201, 162, 39, 0.12);
    color: var(--brass);
    font-size: 13px;
    font-weight: 600;
  }
  .motion-cta.show { display: block; }

  .fatal {
    position: fixed;
    inset: 0;
    display: none;
    place-items: center;
    padding: 32px;
    text-align: center;
    font-size: 14px;
    line-height: 1.6;
    background: var(--felt-deep);
    z-index: 9;
  }
  .fatal.show { display: grid; }

  @media (prefers-reduced-motion: reduce) {
    .sheet, .scrim, .icon-btn, .roll-btn { transition-duration: 0.01ms; }
  }
