@font-face { font-family: "Cinzel"; font-weight: 700; font-style: normal; font-display: swap; src: url("fonts/cinzel-latin-700-normal.woff2") format("woff2"); }
@font-face { font-family: "Alegreya Sans"; font-weight: 400; font-style: normal; font-display: swap; src: url("fonts/alegreya-sans-latin-400-normal.woff2") format("woff2"); }
@font-face { font-family: "Alegreya Sans"; font-weight: 700; font-style: normal; font-display: swap; src: url("fonts/alegreya-sans-latin-700-normal.woff2") format("woff2"); }

/* Layout: un tavolo a tutto schermo con il vassoio al centro; intestazione e risultato sopra, dock con i dadi sotto. */
:root {
  --wood-0: #0e0906;
  --wood-1: #1c120b;
  --leather: #2a1b12;
  --leather-2: #36241a;
  --brass: #d2a64a;
  --brass-d: #8c6a22;
  --ink: #efe4c9;
  --muted: #a99b7f;
  --ember: #e8602c;
  --set: #ff6a5a;
  --line: rgba(210, 166, 74, 0.35);
  --display: "Cinzel", "Times New Roman", serif;
  --body: "Alegreya Sans", "Trebuchet MS", system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; overflow: hidden; background: var(--wood-0); color: var(--ink); font-family: var(--body); font-size: 16px; overscroll-behavior: none; }
body { position: fixed; inset: 0; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; touch-action: manipulation; }
button:focus-visible, input:focus-visible + i { outline: 2px solid var(--brass); outline-offset: 2px; }

#gl { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

#top {
  position: fixed; top: 0; left: 0; right: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) max(16px, env(safe-area-inset-right, 0px)) 6px max(16px, env(safe-area-inset-left, 0px));
  background: linear-gradient(to bottom, rgba(8, 5, 3, 0.82), rgba(8, 5, 3, 0));
  pointer-events: none;
}
#top > * { pointer-events: auto; }
.brand { display: flex; flex-direction: column; line-height: 1; font-family: var(--display); text-transform: uppercase; }
.brand span { font-size: 10px; letter-spacing: 0.3em; color: var(--muted); }
.brand b { font-size: 22px; letter-spacing: 0.22em; color: var(--brass); margin-top: 3px; }
.tools { display: flex; gap: 6px; }
.icon { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; color: var(--ink); background: rgba(28, 18, 11, 0.55); border: 1px solid var(--line); }
.icon:active { background: rgba(210, 166, 74, 0.2); }

#result {
  position: fixed; z-index: 4; left: 0; right: 0;
  top: calc(env(safe-area-inset-top, 0px) + 58px);
  height: 108px; padding: 0 20px;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  text-align: center; pointer-events: none;
}
#total {
  font-family: var(--display); font-weight: 700; font-size: 62px; line-height: 1.05; letter-spacing: 0.02em;
  color: var(--ink); text-shadow: 0 0 22px rgba(210, 166, 74, 0.35), 0 2px 0 rgba(0, 0, 0, 0.7);
  font-variant-numeric: lining-nums tabular-nums;
}
#total.pop { animation: pop 0.45s cubic-bezier(0.2, 1.4, 0.4, 1); }
#total.rolling { opacity: 0.55; letter-spacing: 0.3em; }
#detail { margin-top: 2px; font-size: 15px; color: var(--muted); max-width: 92vw; line-height: 1.25; font-variant-numeric: tabular-nums; text-shadow: 0 1px 2px #000; }
#detail b { color: var(--ink); font-weight: 700; }
#detail s { opacity: 0.55; }
@keyframes pop { 0% { transform: scale(0.7); opacity: 0.2; } 100% { transform: scale(1); opacity: 1; } }

#flash {
  position: fixed; z-index: 6; left: 0; right: 0; top: 42%;
  text-align: center; pointer-events: none; opacity: 0;
  font-family: var(--display); font-weight: 700; font-size: 34px; letter-spacing: 0.14em; text-transform: uppercase;
}
#flash.good { color: #ffe08a; text-shadow: 0 0 24px rgba(255, 200, 80, 0.9), 0 3px 0 #5a3c07; }
#flash.bad { color: #ff7a5c; text-shadow: 0 0 24px rgba(232, 96, 44, 0.8), 0 3px 0 #3a0d05; }
#flash.show { animation: flash 1.8s ease-out; }
@keyframes flash { 0% { opacity: 0; transform: translateY(14px) scale(0.9); } 12% { opacity: 1; transform: translateY(0) scale(1.05); } 70% { opacity: 1; } 100% { opacity: 0; transform: translateY(-20px) scale(1); } }

#toast {
  position: fixed; z-index: 7; left: 50%; transform: translate(-50%, 10px); opacity: 0; pointer-events: none;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 214px);
  background: var(--leather); border: 1px solid var(--line); padding: 8px 14px; border-radius: 10px; font-size: 15px; transition: opacity 0.2s, transform 0.2s; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

#dock {
  position: fixed; z-index: 5; left: 0; right: 0; bottom: 0;
  padding: 12px max(14px, env(safe-area-inset-right, 0px)) calc(env(safe-area-inset-bottom, 0px) + 12px) max(14px, env(safe-area-inset-left, 0px));
  background:
    linear-gradient(to bottom, rgba(255, 255, 255, 0.05), rgba(0, 0, 0, 0.22)),
    var(--leather);
  border-top: 1px solid #000;
  box-shadow: 0 -10px 24px rgba(0, 0, 0, 0.55);
  display: flex; flex-direction: column; gap: 10px;
}
#dock::before { content: ""; position: absolute; inset: 5px 8px auto 8px; height: 0; border-top: 1px dashed var(--line); pointer-events: none; }
.handrow { display: flex; justify-content: space-between; align-items: baseline; min-height: 22px; padding: 0 2px; }
#hand { font-family: var(--display); font-size: 15px; letter-spacing: 0.08em; color: var(--ink); }
.link { color: var(--brass); font-size: 15px; padding: 4px 2px; }
.link[hidden] { display: none; }

.chips { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.chip { position: relative; height: 62px; border-radius: 12px; border: 1px solid rgba(255, 255, 255, 0.08); background: rgba(0, 0, 0, 0.28); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--muted); transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.1s; }
.chip svg { width: 28px; height: 28px; fill: rgba(255, 255, 255, 0.04); stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }
.chip span { font-family: var(--display); font-size: 12px; letter-spacing: 0.04em; }
.chip:active { transform: scale(0.94); }
.chip.on { color: var(--set); border-color: var(--set); background: rgba(255, 255, 255, 0.07); }
.chip.on svg { fill: color-mix(in srgb, var(--set) 22%, transparent); }
.badge { position: absolute; top: -8px; right: -5px; min-width: 24px; height: 24px; padding: 0 6px; border-radius: 12px; background: var(--brass); color: #1b1208; font-family: var(--display); font-size: 13px; display: none; align-items: center; justify-content: center; border: 2px solid var(--leather); }
.chip.on .badge { display: flex; }

.actions { display: grid; grid-template-columns: 112px minmax(0, 1fr) 100px; gap: 8px; align-items: stretch; height: 56px; }
.mod { display: grid; grid-template-columns: 34px 1fr 34px; align-items: center; border-radius: 12px; background: rgba(0, 0, 0, 0.32); border: 1px solid rgba(255, 255, 255, 0.08); }
.mod button { height: 100%; font-size: 22px; color: var(--brass); }
.mod button:active { background: rgba(210, 166, 74, 0.18); }
.mod output { text-align: center; font-family: var(--display); font-size: 17px; font-variant-numeric: tabular-nums; }
#roll {
  border-radius: 14px; font-family: var(--display); font-size: 21px; letter-spacing: 0.16em; text-transform: uppercase; color: #20150a;
  background: linear-gradient(to bottom, #f0cd7c, #c8963a 55%, #a87a28);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.55), inset 0 -2px 0 rgba(90, 58, 10, 0.55), 0 4px 14px rgba(0, 0, 0, 0.55);
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
}
#roll:active { transform: translateY(2px); box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 6px rgba(0, 0, 0, 0.6); }
#mode { border-radius: 12px; border: 1px solid var(--line); background: rgba(0, 0, 0, 0.32); color: var(--muted); font-size: 14px; line-height: 1.1; padding: 0 4px; }
#mode.adv { color: #8ff0b8; border-color: #4fbf86; background: rgba(79, 191, 134, 0.14); }
#mode.dis { color: #ff9c82; border-color: #e0673f; background: rgba(224, 103, 63, 0.14); }

/* Fogli a scomparsa */
.sheet-wrap { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet-wrap[hidden] { display: none; }
.backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.6); animation: fade 0.2s; }
.sheet {
  position: relative; width: min(100%, 520px); max-height: 82dvh; overflow-y: auto; overscroll-behavior: contain;
  background: linear-gradient(to bottom, var(--leather-2), var(--leather));
  border: 1px solid var(--line); border-bottom: 0; border-radius: 22px 22px 0 0;
  padding: 10px max(18px, env(safe-area-inset-right, 0px)) calc(env(safe-area-inset-bottom, 0px) + 22px) max(18px, env(safe-area-inset-left, 0px));
  animation: rise 0.28s cubic-bezier(0.2, 0.9, 0.3, 1);
}
.grip { width: 44px; height: 4px; border-radius: 2px; background: rgba(255, 255, 255, 0.2); margin: 0 auto 10px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet h2 { margin: 6px 0 10px; font-family: var(--display); font-size: 20px; letter-spacing: 0.1em; color: var(--brass); font-weight: 700; }
.sheet h3 { margin: 22px 0 6px; font-family: var(--display); font-size: 13px; letter-spacing: 0.2em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.sets { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.set { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 14px; border: 1px solid rgba(255, 255, 255, 0.1); background: rgba(0, 0, 0, 0.25); text-align: left; min-width: 0; }
.set i { flex: none; width: 34px; height: 34px; border-radius: 9px; transform: rotate(45deg) scale(0.82); box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.18), 0 2px 6px rgba(0, 0, 0, 0.5); }
.set span { font-size: 15px; line-height: 1.15; }
.set[aria-pressed="true"] { border-color: var(--brass); background: rgba(210, 166, 74, 0.12); }

.toggle { display: flex; justify-content: space-between; align-items: center; gap: 14px; padding: 12px 2px; border-bottom: 1px solid rgba(255, 255, 255, 0.06); font-size: 17px; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle i { flex: none; position: relative; width: 48px; height: 28px; border-radius: 14px; background: rgba(0, 0, 0, 0.45); border: 1px solid rgba(255, 255, 255, 0.15); transition: background 0.2s; }
.toggle i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: var(--muted); transition: transform 0.2s, background 0.2s; }
.toggle input:checked + i { background: rgba(210, 166, 74, 0.3); border-color: var(--brass); }
.toggle input:checked + i::after { transform: translateX(20px); background: var(--brass); }
.hint { margin: 16px 0 0; color: var(--muted); font-size: 15px; line-height: 1.4; }

#history-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
#history-list li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 12px; padding: 11px 0; border-bottom: 1px solid rgba(255, 255, 255, 0.07); }
#history-list .ex { font-family: var(--display); font-size: 14px; letter-spacing: 0.04em; }
#history-list .dt { grid-column: 1; color: var(--muted); font-size: 14px; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
#history-list .tt { grid-row: 1 / span 2; grid-column: 2; align-self: center; font-family: var(--display); font-size: 28px; color: var(--brass); font-variant-numeric: tabular-nums; }
#history-list .empty { color: var(--muted); padding: 18px 0; display: block; }

@media (max-height: 640px) {
  #total { font-size: 48px; }
  #result { height: 92px; }
  .chip { height: 54px; }
  .actions { height: 50px; }
}
@media (prefers-reduced-motion: reduce) {
  #total.pop, #flash.show, .sheet, .backdrop { animation: none; }
  #flash.show { opacity: 1; }
}
