:root {
    color-scheme: light;
    font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --accent-soft: rgba(37, 99, 235, 0.1);
    --danger: #dc2626;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --border: #e2e8f0;
    --surface: rgba(255, 255, 255, 0.92);
    --surface-solid: #ffffff;
    --background: linear-gradient(160deg, #f8fafc 0%, #e2e8f0 45%, #f8fafc 100%);
    --shadow: 0 20px 40px rgba(15, 23, 42, 0.1);
    --bar: #2563eb;
    --bar-latest: #1e3a8a;
    --theory: #e2583e;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: var(--background);
    color: var(--text-primary);
}

button,
input,
select,
textarea {
    font: inherit;
    color: inherit;
}

[hidden] {
    display: none !important;
}

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

/* ---------- Page ---------- */

.page {
    width: min(1120px, 100%);
    padding: clamp(1.5rem, 4vw, 3rem) clamp(1rem, 4vw, 2rem) 3rem;
    display: grid;
    gap: 1.5rem;
}

.page__header {
    text-align: center;
    display: grid;
    gap: 0.5rem;
}

.page__header h1 {
    margin: 0;
    font-size: clamp(2rem, 5vw, 3rem);
    letter-spacing: -0.02em;
}

.page__header p {
    margin: 0;
    color: var(--text-secondary);
}

/* ---------- Buttons ---------- */

.btn {
    border: 1px solid transparent;
    border-radius: 999px;
    padding: 0.6rem 1.1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 140ms ease, transform 100ms ease, box-shadow 140ms ease;
}

.btn:active {
    transform: scale(0.97);
}

.btn:focus-visible,
.tabs button:focus-visible,
.link-button:focus-visible,
.attendance button:focus-visible {
    outline: 3px solid rgba(37, 99, 235, 0.45);
    outline-offset: 2px;
}

.btn--primary {
    background: var(--accent);
    color: #fff;
    box-shadow: 0 10px 24px rgba(37, 99, 235, 0.28);
}

.btn--primary:hover {
    background: var(--accent-hover);
}

.btn--primary:disabled {
    background: var(--text-muted);
    box-shadow: none;
    cursor: not-allowed;
}

.btn--ghost {
    background: var(--surface-solid);
    border-color: var(--border);
    color: var(--text-secondary);
}

.btn--ghost:hover {
    border-color: #cbd5e1;
    color: var(--text-primary);
}

.btn--danger:hover {
    color: var(--danger);
    border-color: #fecaca;
}

.btn--big {
    font-size: 1.25rem;
    padding: 0.85rem 2.4rem;
}

.link-button {
    border: none;
    background: none;
    padding: 0.2rem 0.3rem;
    color: var(--accent);
    font-weight: 600;
    cursor: pointer;
    border-radius: 6px;
}

.link-button:hover {
    text-decoration: underline;
}


.btn--ghost:disabled,
.btn--primary:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.btn.is-active {
    background: var(--text-primary);
    border-color: var(--text-primary);
    color: #fff;
}

/* ---------- Settings ---------- */

.settings {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.75rem 1.5rem;
}

.setting {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.segmented {
    display: inline-flex;
    padding: 4px;
    border-radius: 999px;
    background: rgba(15, 23, 42, 0.06);
}

.segmented label {
    cursor: pointer;
}

.segmented input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.segmented span {
    display: block;
    min-width: 2.4rem;
    text-align: center;
    padding: 0.4rem 0.9rem;
    border-radius: 999px;
    font-weight: 600;
    color: var(--text-secondary);
}

.segmented input:checked + span {
    background: var(--surface-solid);
    color: var(--text-primary);
    box-shadow: 0 2px 8px rgba(15, 23, 42, 0.12);
}

.segmented input:focus-visible + span {
    outline: 3px solid rgba(37, 99, 235, 0.45);
}

.setting select {
    padding: 0.5rem 0.8rem;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface-solid);
    font-weight: 600;
    color: var(--text-primary);
}

/* ---------- Layout ---------- */

.workspace {
    display: grid;
    grid-template-columns: minmax(260px, 340px) minmax(0, 1fr);
    gap: 1.25rem;
    align-items: start;
}

.card {
    position: relative;
    background: var(--surface);
    border-radius: 24px;
    box-shadow: var(--shadow);
    padding: clamp(1.1rem, 3vw, 1.75rem);
}

.card h2 {
    margin: 0;
    font-size: 1.1rem;
}

/* ---------- Tray ---------- */

.tray {
    display: grid;
    gap: 1.1rem;
    justify-items: center;
    text-align: center;
}

.dice {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 0.9rem;
    min-height: 92px;
}

.die {
    width: 84px;
    height: 84px;
}

.dice[data-count="3"] .die {
    width: 72px;
    height: 72px;
}

.die.is-rolling {
    animation: shake 90ms linear infinite alternate;
}

@keyframes shake {
    from {
        transform: rotate(-8deg) translateY(-2px);
    }
    to {
        transform: rotate(8deg) translateY(2px);
    }
}

.result-line {
    min-height: 2.4rem;
    font-size: clamp(1.4rem, 3vw, 1.9rem);
    font-weight: 800;
    letter-spacing: -0.02em;
}

.result-line small {
    display: block;
    font-size: 0.9rem;
    font-weight: 600;
    letter-spacing: 0;
    color: var(--text-secondary);
}

.roll-main {
    width: 100%;
}

.roll-batch {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
    width: 100%;
}

.roll-batch .btn {
    padding-inline: 0.5rem;
}

.tray__footer {
    display: flex;
    gap: 0.5rem;
    width: 100%;
}

.tray__footer .btn {
    flex: 1;
    padding-inline: 0.6rem;
    white-space: nowrap;
}

.hint {
    margin: 0;
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* ---------- Stats ---------- */

.stats {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.stat {
    border-radius: 16px;
    background: var(--surface-solid);
    border: 1px solid var(--border);
    padding: 0.7rem 0.9rem;
    display: grid;
    gap: 0.1rem;
}

.stat__label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.stat__value {
    font-size: 1.6rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
}

.stat__sub {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

/* ---------- Chart ---------- */

.chart-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.6rem 1rem;
    margin-bottom: 0.5rem;
}

.chart-options {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.6rem 1rem;
}

.chart-options .segmented span {
    padding: 0.3rem 0.8rem;
    font-size: 0.9rem;
}

.toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text-secondary);
    cursor: pointer;
}

.toggle input {
    width: 1.05rem;
    height: 1.05rem;
    accent-color: var(--accent);
}

.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem 1.1rem;
    margin: 0 0 0.4rem;
    padding: 0;
    list-style: none;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.legend li {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.legend__swatch {
    width: 12px;
    height: 12px;
    border-radius: 3px;
    background: var(--bar);
}

.legend__line {
    width: 16px;
    height: 0;
    border-top: 2px solid var(--theory);
    position: relative;
}

.legend__line::after {
    content: "";
    position: absolute;
    left: 50%;
    top: -5px;
    width: 8px;
    height: 8px;
    margin-left: -4px;
    border-radius: 50%;
    background: var(--theory);
}

.chart {
    position: relative;
    width: 100%;
    height: clamp(260px, 42vh, 420px);
}

.chart svg {
    display: block;
    width: 100%;
    height: 100%;
    overflow: visible;
}

.chart .grid line {
    stroke: #e2e8f0;
    stroke-width: 1;
}

.chart .baseline {
    stroke: #94a3b8;
    stroke-width: 1;
}

.chart .axis text {
    fill: #64748b;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
}

.chart .axis-title {
    fill: #64748b;
    font-size: 12px;
    font-weight: 600;
}

.chart .bar {
    fill: var(--bar);
    transition: opacity 120ms ease;
}

.chart .bar.is-latest {
    fill: var(--bar-latest);
}

.chart .col:hover .bar,
.chart .col.is-hover .bar {
    opacity: 0.8;
}

.chart .theory line {
    stroke: var(--theory);
    stroke-width: 2;
    stroke-linecap: round;
}

.chart .theory circle {
    fill: var(--theory);
    stroke: var(--surface-solid);
    stroke-width: 2;
}

.chart .hit {
    fill: transparent;
    cursor: default;
}

.chart__empty {
    position: absolute;
    inset: 0 0 32px 44px;
    display: grid;
    place-items: center;
    color: var(--text-muted);
    font-weight: 600;
    pointer-events: none;
    text-align: center;
    padding: 1rem;
}

.tooltip {
    position: absolute;
    z-index: 5;
    pointer-events: none;
    min-width: 170px;
    padding: 0.55rem 0.7rem;
    border-radius: 10px;
    background: var(--surface-solid);
    border: 1px solid var(--border);
    box-shadow: 0 10px 28px rgba(15, 23, 42, 0.16);
    font-size: 0.85rem;
    line-height: 1.45;
    transform: translate(-50%, calc(-100% - 10px));
}

.tooltip strong {
    display: block;
    margin-bottom: 0.15rem;
}

.tooltip__row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    color: var(--text-secondary);
}

.tooltip__row b {
    margin-left: auto;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
}

.tooltip__key {
    width: 10px;
    height: 10px;
    border-radius: 2px;
    background: var(--bar);
}

.tooltip__key--theory {
    border-radius: 50%;
    background: var(--theory);
}

/* ---------- Table ---------- */

.table-toggle {
    margin-top: 1rem;
}

.table-toggle summary {
    cursor: pointer;
    font-weight: 600;
    color: var(--accent);
    width: fit-content;
    border-radius: 6px;
}

.table-toggle summary:focus-visible {
    outline: 3px solid rgba(37, 99, 235, 0.45);
}

.table-wrap {
    margin-top: 0.75rem;
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
    font-size: 0.92rem;
}

th,
td {
    padding: 0.45rem 0.6rem;
    text-align: right;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

th:first-child,
td:first-child {
    text-align: left;
}

th {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

/* ---------- Tips ---------- */

.notes {
    color: var(--text-secondary);
    line-height: 1.55;
}

.notes h2 {
    margin: 0 0 0.4rem;
    font-size: 1.1rem;
    color: var(--text-primary);
}

.notes ul {
    margin: 0;
    padding-left: 1.2rem;
}

/* ---------- Fullscreen ---------- */

.fullscreen-btn {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 10px;
    background: rgba(15, 23, 42, 0.05);
    color: var(--text-secondary);
    display: grid;
    place-items: center;
    cursor: pointer;
}

.fullscreen-btn:hover {
    background: rgba(15, 23, 42, 0.1);
    color: var(--text-primary);
}

.fullscreen-btn svg {
    width: 20px;
    height: 20px;
}

.workspace:fullscreen {
    background: #f8fafc;
    padding: 3vh 3vw;
    align-content: center;
    overflow: auto;
}

.workspace:fullscreen .chart {
    height: 62vh;
}

@media (max-width: 860px) {
    .workspace {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 480px) {
    .stats {
        grid-template-columns: 1fr 1fr;
    }

    .stats .stat:last-child {
        grid-column: span 2;
    }

    .die {
        width: 68px;
        height: 68px;
    }

    .dice[data-count="3"] .die {
        width: 60px;
        height: 60px;
    }
}

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