/* Dice roller (spec 069): floating button, panel (card on wide screens, bottom sheet on phones),
   tray with CSS 3D cubes and flat shapes. Above the Planner modal (2400), below the cookie banner
   and the demo notice (2500). */

.dice-fab {
    position: fixed;
    right: 18px;
    bottom: 60px;
    z-index: 2450;
    width: 48px;
    height: 48px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--tt-accent);
    border-radius: 0;
    background: linear-gradient(135deg, var(--tt-accent-from), var(--tt-accent-to));
    color: var(--tt-on-accent);
    box-shadow: none;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.dice-fab:hover {
    transform: translateY(-2px);
    box-shadow: none;
}

.dice-fab:focus-visible,
.dice-panel button:focus-visible,
.dice-panel-title:focus-visible,
.dice-form-input:focus-visible {
    outline: 3px solid var(--tt-accent);
    outline-offset: 2px;
}

.dice-fab[aria-expanded="true"] {
    background: linear-gradient(135deg, var(--tt-accent-from), var(--tt-accent-to));
}

.page.has-timer-chip .dice-fab {
    bottom: 84px;
}

.page.dice-viewer .dice-fab {
    right: auto;
    left: calc(260px + 18px);
    bottom: 18px;
}

.page.dice-viewer.sidebar-collapsed .dice-fab {
    left: calc(76px + 18px);
}

/* Spec 111 rule 2: from 769px the viewer opens the dice from its tool row, so nothing floats over the PDF. */
@media (min-width: 769px) {
    .page.dice-viewer .dice-fab {
        display: none;
    }
}

.dice-panel {
    position: fixed;
    right: 18px;
    bottom: 120px;
    z-index: 2460;
    display: none;
    flex-direction: column;
    width: 360px;
    max-width: calc(100vw - 36px);
    max-height: calc(100vh - 140px);
    border: 1px solid;
    overflow: hidden;
}

.dice-panel[data-open] {
    display: flex;
}

.page.has-timer-chip .dice-panel {
    bottom: 144px;
}

.page.dice-viewer .dice-panel {
    right: auto;
    left: calc(260px + 18px);
    bottom: 78px;
}

.page.dice-viewer.sidebar-collapsed .dice-panel {
    left: calc(76px + 18px);
}

.dice-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.7rem 0.9rem;
    border-bottom: 1px solid var(--tt-line);
}

.dice-panel-title {
    margin: 0;
    font-size: 1.05rem;
    font-weight: 700;
}

.dice-panel-close {
    border: 0;
    background: transparent;
    color: inherit;
    font-size: 1.5rem;
    line-height: 1;
    padding: 0 0.3rem;
    cursor: pointer;
}

.dice-panel-body {
    padding: 0.8rem 0.9rem 1rem;
    overflow-y: auto;
}

.dice-tray {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 14px;
    min-height: 64px;
    padding: 8px 4px;
    perspective: 600px;
}

.dice-result {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.4rem 1rem;
    /* One line of the result, so the first roll does not move the panel (spec 134 rule 6). */
    min-height: 1.5em;
    margin: 0.2rem 0 0.6rem;
    font-weight: 700;
    text-align: center;
}

.dice-actions,
.dice-form-actions {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

.dice-roll-btn,
.dice-clear-btn,
.dice-new-btn,
.dice-pick-btn,
.dice-pick-edit {
    border: 1px solid;
    color: inherit;
    font: inherit;
    cursor: pointer;
}

.dice-roll-btn {
    flex: 1;
    padding: 0.5rem 0.9rem;
    font-weight: 700;
}

.dice-clear-btn,
.dice-new-btn {
    padding: 0.4rem 0.7rem;
    font-size: 0.9rem;
}

.dice-roll-btn:disabled,
.dice-pick-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.dice-limit {
    display: none;
    margin: 0 0 0.5rem;
    font-size: 0.85rem;
    color: var(--tt-accent);
}

.dice-limit[data-show] {
    display: block;
}

.dice-pick-list {
    list-style: none;
    margin: 0 0 0.6rem;
    padding: 0;
}

.dice-pick {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.25rem 0;
}

.dice-pick-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
}

.dice-pick-controls {
    display: inline-flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    align-items: center;
    gap: 0.35rem;
    flex-shrink: 0;
}

.dice-pick-btn {
    width: 2rem;
    height: 2rem;
    font-weight: 700;
}

.dice-pick-count {
    min-width: 1.5rem;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

.dice-pick-edit {
    padding: 0.2rem 0.5rem;
    font-size: 0.8rem;
}

.dice-pick-danger {
    border-color: var(--tt-danger);
    color: var(--tt-danger);
}

.dice-confirm-text {
    font-size: 0.85rem;
}

.dice-section-title {
    margin: 0.6rem 0 0.3rem;
    font-size: 0.9rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    opacity: 0.8;
}

.dice-form {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    margin-bottom: 0.6rem;
}

.dice-form-label {
    font-size: 0.85rem;
    font-weight: 600;
}

.dice-form-input {
    width: 100%;
    padding: 0.4rem 0.5rem;
    border: 1px solid var(--tt-line);
    border-radius: 0;
    background: var(--tt-field);
    color: inherit;
    font: inherit;
}

.dice-form-faces {
    resize: vertical;
}

.dice-form-error {
    margin: 0;
    font-size: 0.85rem;
    color: var(--tt-danger);
}

.dice-history-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    min-height: 2.25rem;
}

/* Spec 134 rule 6: the history is folded behind its title; an open list scrolls inside a fixed height. */
.dice-history-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    min-height: 2rem;
    padding: 0;
    border: 0;
    background: transparent;
    color: inherit;
    font: inherit;
    letter-spacing: inherit;
    text-transform: inherit;
    cursor: pointer;
}

.dice-history-toggle::after {
    content: "";
    width: 0.45em;
    height: 0.45em;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: translateY(-0.15em) rotate(45deg);
    transition: transform 0.15s ease;
}

.dice-roller[data-history-open] .dice-history-toggle::after {
    transform: translateY(0.1em) rotate(-135deg);
}

.dice-history-count {
    font-variant-numeric: tabular-nums;
}

.dice-roller:not([data-history-open]) :is(.dice-history, [data-dice-history-clear]) {
    display: none;
}

.dice-history {
    list-style: none;
    max-height: 12rem;
    margin: 0.3rem 0 0;
    padding: 0;
    overflow-y: auto;
    font-size: 0.85rem;
}

.dice-history-item {
    display: flex;
    flex-wrap: wrap;
    gap: 0.2rem 0.6rem;
    padding: 0.3rem 0;
    border-top: 1px solid var(--tt-line);
}

.dice-history-time {
    font-variant-numeric: tabular-nums;
    opacity: 0.7;
}

.dice-history-label {
    font-weight: 600;
}

.dice-history-total {
    font-weight: 700;
}

.dice-history-empty {
    opacity: 0.7;
}

/* --- Dice in the tray --- */

.dice-die {
    position: relative;
    width: 48px;
    height: 48px;
}

.dice-cube {
    position: absolute;
    inset: 0;
    transform-style: preserve-3d;
}

.dice-cube.is-rolling {
    transition: transform 0.9s cubic-bezier(0.2, 0.7, 0.25, 1);
}

.dice-face {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #d8d0c2;
    border-radius: 8px;
    background: #fbf4ea;
    color: #2a1c13;
    font-weight: 800;
    font-size: 0.95rem;
    backface-visibility: hidden;
    box-shadow: inset 0 0 6px rgba(42, 28, 19, 0.18);
    overflow: hidden;
    text-align: center;
    word-break: break-word;
}

.dice-face-front { transform: rotateY(0deg) translateZ(24px); }
.dice-face-back { transform: rotateY(180deg) translateZ(24px); }
.dice-face-right { transform: rotateY(90deg) translateZ(24px); }
.dice-face-left { transform: rotateY(-90deg) translateZ(24px); }
.dice-face-top { transform: rotateX(90deg) translateZ(24px); }
.dice-face-bottom { transform: rotateX(-90deg) translateZ(24px); }

.dice-face-pips {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    padding: 6px;
    place-items: center;
}

.dice-pip {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #2a1c13;
}

.dice-pip-empty {
    visibility: hidden;
}

.dice-face.dice-text-long,
.dice-flat-text.dice-text-long {
    font-size: 0.62rem;
    line-height: 1.1;
}

.dice-die-flat {
    display: flex;
    align-items: center;
    justify-content: center;
    filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.25));
}

.dice-die-flat.is-rolling {
    animation: diceSpin 0.9s cubic-bezier(0.2, 0.7, 0.25, 1);
}

.dice-flat-shape {
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, var(--tt-accent-from), var(--tt-accent-to));
}

.dice-flat-text {
    position: relative;
    max-width: 44px;
    color: #fff;
    font-weight: 800;
    font-size: 1rem;
    text-align: center;
    overflow: hidden;
    overflow-wrap: anywhere;
    text-shadow: none;
}

.dice-shape-d4 .dice-flat-shape { clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.dice-shape-d4 .dice-flat-text { top: 8px; }
.dice-shape-d8 .dice-flat-shape { clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%); }
.dice-shape-d10 .dice-flat-shape { clip-path: polygon(50% 0, 100% 42%, 50% 100%, 0 42%); }
.dice-shape-d12 .dice-flat-shape { clip-path: polygon(50% 0, 100% 38%, 82% 100%, 18% 100%, 0 38%); }
.dice-shape-d20 .dice-flat-shape { clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%); }
.dice-shape-token .dice-flat-shape { border-radius: 0; background: var(--tt-area); }

@keyframes diceSpin {
    0% { transform: rotate(0deg) scale(0.7); }
    60% { transform: rotate(600deg) scale(1.1); }
    100% { transform: rotate(720deg) scale(1); }
}

.page.theme-dark .dice-panel-header {
    border-bottom-color: var(--tt-line);
}

.page.theme-dark .dice-clear-btn,
.page.theme-dark .dice-new-btn,
.page.theme-dark .dice-pick-btn,
.page.theme-dark .dice-pick-edit,
.page.theme-dark .dice-form-input {
    border-color: var(--tt-line);
    background: var(--tt-field);
    color: var(--tt-ink);
}

.page.theme-dark .dice-limit {
    color: var(--tt-accent);
}

.page.theme-dark .dice-form-error,
.page.theme-dark .dice-pick-danger {
    color: var(--tt-muted);
    border-color: var(--tt-line);
}

.page.theme-dark .dice-history-item {
    border-top-color: var(--tt-line);
}

/* --- Phones: bottom sheet --- */

@media (max-width: 768px) {
    /* Spec 096 statement 5: steps aside while the page scrolls down (diceRoller.js sets the class). */
    .dice-fab.dice-fab-away {
        transform: translateY(calc(100% + 2rem));
        opacity: 0;
        pointer-events: none;
    }

    .dice-fab {
        transition: transform 0.2s ease, opacity 0.2s ease, box-shadow 0.15s ease;
        right: 14px;
        bottom: calc(16px + env(safe-area-inset-bottom) + var(--app-viewport-bottom-inset, 0px));
    }

    .page.has-timer-chip .dice-fab {
        right: 14px;
        bottom: calc(84px + env(safe-area-inset-bottom) + var(--app-viewport-bottom-inset, 0px));
    }

    /* Spec 095 statement 5: in the slim top bar, left of the menu button, off the chat and the rulebook. */
    .page.dice-viewer .dice-fab,
    .page.dice-viewer.sidebar-collapsed .dice-fab {
        top: calc(0.375rem + env(safe-area-inset-top));
        right: 3.5rem;
        bottom: auto;
        left: auto;
        width: 2.5rem;
        height: 2.5rem;
    }

    /* Spec 094 statement 7: still above a Planner modal, but in the free top corner, off its footer buttons. */
    html:has(.planner-modal-backdrop:not([hidden])) .dice-fab,
    html:has(.planner-modal-backdrop:not([hidden])) .page.has-timer-chip .dice-fab {
        top: calc(8px + env(safe-area-inset-top));
        right: 8px;
        bottom: auto;
        left: auto;
        width: 40px;
        height: 40px;
    }

    .dice-panel,
    .page.has-timer-chip .dice-panel,
    .page.dice-viewer .dice-panel,
    .page.dice-viewer.sidebar-collapsed .dice-panel {
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        max-width: none;
        max-height: 80vh;
        border-radius: 0;
        padding-bottom: env(safe-area-inset-bottom);
    }

    body.nav-menu-open .dice-roller {
        display: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    .dice-fab,
    .dice-cube.is-rolling {
        transition: none;
    }

    .dice-die-flat.is-rolling {
        animation: none;
    }
}
