/* Dice Fairness — modern UI (light + dark themes) */

*,
*::before,
*::after {
    box-sizing: border-box;
}

* {
    margin: 0;
    padding: 0;
}

/* ---------- Theme tokens ---------- */

:root {
    color-scheme: light dark;

    /* Surfaces */
    --bg: light-dark(#f2f4f9, #0a0c10);
    --surface: light-dark(#ffffff, #14171e);
    --surface-2: light-dark(#f7f8fb, #1a1e27);
    --surface-inset: light-dark(#eef1f7, #0d1015);
    --panel-sheen: light-dark(rgba(255, 255, 255, 0.65), rgba(255, 255, 255, 0.035));
    --row-hover: light-dark(rgba(15, 23, 42, 0.035), rgba(255, 255, 255, 0.035));

    /* Lines */
    --border: light-dark(rgba(15, 23, 42, 0.10), rgba(255, 255, 255, 0.08));
    --border-strong: light-dark(rgba(15, 23, 42, 0.20), rgba(255, 255, 255, 0.18));
    --divider: light-dark(rgba(15, 23, 42, 0.07), rgba(255, 255, 255, 0.06));

    /* Text */
    --text: light-dark(#1b2230, #e9ecf3);
    --muted: light-dark(#596274, #98a1b3);

    /* Accents */
    --accent: light-dark(#4a63e7, #6c8cff);
    --accent-2: light-dark(#7c5cf0, #8b5cf6);
    --on-accent: #ffffff;
    --success: light-dark(#15803d, #4ade80);
    --success-tint: light-dark(rgba(21, 128, 61, 0.10), rgba(74, 222, 128, 0.13));
    --fail: light-dark(#b91c1c, #ff6b6b);
    --fail-tint: light-dark(rgba(185, 28, 28, 0.09), rgba(255, 107, 107, 0.13));

    /* Effects */
    --ring: 0 0 0 3px light-dark(rgba(74, 99, 231, 0.32), rgba(108, 140, 255, 0.38));
    --shadow-panel:
        0 1px 2px light-dark(rgba(15, 23, 42, 0.05), rgba(0, 0, 0, 0.35)),
        0 18px 40px -26px light-dark(rgba(15, 23, 42, 0.35), rgba(0, 0, 0, 0.8));
    --shadow-accent: 0 10px 24px -12px light-dark(rgba(74, 99, 231, 0.55), rgba(108, 140, 255, 0.55));

    /* Form chrome */
    --select-chevron: light-dark(
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23596274' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m4 6 4 4 4-4'/%3E%3C/svg%3E"),
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%2398a1b3' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m4 6 4 4 4-4'/%3E%3C/svg%3E")
    );

    /* Radii + fonts */
    --radius: 16px;
    --radius-md: 12px;
    --radius-sm: 10px;
    --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, "SFMono-Regular", "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

:root[data-theme="dark"] {
    color-scheme: dark;
}

:root[data-theme="light"] {
    color-scheme: light;
}

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

body {
    font-family: var(--font-sans);
    background-color: var(--bg);
    color: var(--text);
    min-height: 100vh;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

::selection {
    background: var(--accent);
    color: var(--on-accent);
}

a,
button,
select,
input {
    font: inherit;
}

:focus-visible {
    outline: none;
    box-shadow: var(--ring);
}

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

.layout {
    max-width: 960px;
    margin: 0 auto;
    padding: 1.5rem 1.25rem 2.5rem;
}

/* ---------- Header ---------- */

.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.9rem;
    min-width: 0;
}

.brand-mark {
    flex: none;
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    border-radius: var(--radius-md);
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    color: var(--on-accent);
    box-shadow: var(--shadow-accent);
}

.brand-text {
    min-width: 0;
}

.header h1 {
    font-size: clamp(1.2rem, 3vw, 1.45rem);
    font-weight: 700;
    letter-spacing: -0.02em;
    line-height: 1.25;
}

.tagline {
    color: var(--muted);
    font-size: 0.88rem;
}

.theme-toggle {
    flex: none;
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
    color: var(--muted);
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s, background-color 0.15s, transform 0.15s;
}

.theme-toggle:hover {
    color: var(--text);
    border-color: var(--border-strong);
    transform: translateY(-1px);
}

.theme-toggle:active {
    transform: translateY(0);
}

.theme-toggle .icon-sun {
    display: block;
}

.theme-toggle .icon-moon {
    display: none;
}

:root[data-theme="light"] .theme-toggle .icon-sun {
    display: none;
}

:root[data-theme="light"] .theme-toggle .icon-moon {
    display: block;
}

/* ---------- Panels ---------- */

.main {
    display: grid;
    gap: 1.25rem;
    grid-template-columns: minmax(0, 1fr);
    align-content: start;
}

.panel {
    position: relative;
    background-color: var(--surface);
    background-image: linear-gradient(180deg, var(--panel-sheen), transparent 45%);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-panel);
    padding: 1.15rem 1.2rem;
}

.panel h2 {
    font-size: 0.76rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--muted);
    margin-bottom: 1rem;
}

@media (min-width: 720px) {
    .main {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    }

    .config-panel { grid-column: 1; grid-row: 1; }
    .dice-panel   { grid-column: 2; grid-row: 1; }
    .log-panel    { grid-column: 1; grid-row: 2; }
    .stats-panel  { grid-column: 2; grid-row: 2; }

    .panel {
        padding: 1.3rem 1.35rem;
    }
}

/* ---------- Fields ---------- */

.field {
    margin-bottom: 1.1rem;
}

.field:last-child {
    margin-bottom: 0;
}

.field .label,
.field label[for] {
    display: block;
    margin-bottom: 0.45rem;
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 600;
}

.field .label strong,
.field label[for] strong {
    color: var(--text);
    font-weight: 700;
}

.field input[type="number"],
.field select {
    font-size: 0.95rem;
    color: var(--text);
    background-color: var(--surface-inset);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.55rem 0.7rem;
    transition: border-color 0.15s, box-shadow 0.15s, background-color 0.15s;
}

.field input[type="number"]:focus,
.field select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: var(--ring);
}

.field select {
    appearance: none;
    width: 100%;
    max-width: 22rem;
    padding-right: 2.25rem;
    cursor: pointer;
    background-image: var(--select-chevron);
    background-repeat: no-repeat;
    background-position: right 0.7rem center;
    background-size: 1rem 1rem;
}

.field input[type="number"] {
    width: 6ch;
    max-width: none;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

.field-dice-setup .dice-setup-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.9rem 1.25rem;
    align-items: flex-end;
}

.field-dice-setup .dice-setup-cell label {
    display: block;
    margin-bottom: 0.45rem;
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 600;
}

.field-dice-setup .dice-setup-count input[type="number"] {
    display: block;
}

.field-hint {
    margin-top: 0.5rem;
    font-size: 0.78rem;
    color: var(--muted);
    line-height: 1.5;
}

.field-hint code {
    font-family: var(--font-mono);
    font-size: 0.85em;
    padding: 0.08em 0.35em;
    background: var(--surface-inset);
    border-radius: 5px;
    border: 1px solid var(--border);
}

/* ---------- Face value chips ---------- */

.checkbox-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.checkbox-row label {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.6rem;
    padding: 0.4rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface-2);
    color: var(--muted);
    font-size: 0.9rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s, background-color 0.15s, transform 0.12s;
}

.checkbox-row label:hover {
    color: var(--text);
    border-color: var(--border-strong);
    transform: translateY(-1px);
}

.checkbox-row input[type="checkbox"] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    margin: 0;
    cursor: pointer;
}

.checkbox-row label:has(input:focus-visible) {
    box-shadow: var(--ring);
}

#success-checks label:has(input:checked) {
    color: var(--success);
    background: var(--success-tint);
    border-color: var(--success);
}

#subtract-checks label:has(input:checked) {
    color: var(--fail);
    background: var(--fail-tint);
    border-color: var(--fail);
}

/* ---------- Actions ---------- */

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-top: 1.3rem;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.62rem 1.1rem;
    font-size: 0.95rem;
    font-weight: 600;
    line-height: 1.25;
    text-align: center;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s, box-shadow 0.15s,
        transform 0.12s, filter 0.15s, opacity 0.15s;
}

.btn:active {
    transform: translateY(1px);
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    color: var(--on-accent);
    box-shadow: var(--shadow-accent);
}

.btn-primary:hover {
    filter: brightness(1.07);
    transform: translateY(-1px);
}

.btn-primary:active {
    transform: translateY(1px);
    filter: brightness(0.98);
}

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

.btn-secondary:hover {
    background: var(--surface-inset);
    border-color: var(--accent);
}

.btn:disabled {
    opacity: 0.55;
    cursor: not-allowed;
    transform: none;
    filter: none;
    box-shadow: none;
}

@media (max-width: 479px) {
    .actions .btn {
        flex: 1 1 100%;
    }
}

/* ---------- Dice stage ---------- */

.dice-panel {
    display: flex;
    flex-direction: column;
    gap: 0.875rem;
    justify-content: center;
}

.dice-container {
    width: 100%;
    height: clamp(260px, 34vw, 320px);
    position: relative;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface-inset);
    box-shadow: inset 0 1px 10px light-dark(rgba(15, 23, 42, 0.06), rgba(0, 0, 0, 0.5));
}

.dice-container canvas {
    display: block;
}

.dice-result {
    align-self: center;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 2.4rem;
    max-width: 100%;
    padding: 0.5rem 0.95rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface-2);
    color: var(--text);
    font-family: var(--font-mono);
    font-size: 0.85rem;
    font-variant-numeric: tabular-nums;
    text-align: center;
}

.dice-result:empty::before {
    content: "Roll the dice to see the result";
    color: var(--muted);
    font-family: var(--font-sans);
}

/* ---------- Roll log ---------- */

.roll-log {
    max-height: 300px;
    overflow-y: auto;
    overscroll-behavior: contain;
    background: var(--surface-inset);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 0.35rem 0.5rem;
    font-family: var(--font-mono);
    font-size: 0.84rem;
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

.roll-log::-webkit-scrollbar {
    width: 8px;
}

.roll-log::-webkit-scrollbar-track {
    background: transparent;
}

.roll-log::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: 999px;
}

.roll-log:empty::before {
    content: "No rolls yet — use the buttons above to start the log.";
    display: block;
    padding: 0.75rem 0.6rem;
    color: var(--muted);
    font-family: var(--font-sans);
    font-size: 0.85rem;
}

.roll-log-entry {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.15rem 0.6rem;
    padding: 0.45rem 0.6rem;
    border-bottom: 1px solid var(--divider);
    border-left: 2px solid transparent;
    border-radius: 6px;
    transition: background-color 0.12s;
}

.roll-log-entry:last-child {
    border-bottom: none;
}

.roll-log-entry:hover {
    background: var(--row-hover);
}

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

.roll-values {
    flex: 1 1 auto;
    min-width: 0;
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

.roll-net {
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.roll-outcome {
    font-family: var(--font-sans);
    font-size: 0.66rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 0.16rem 0.5rem;
    border-radius: 999px;
}

.roll-log-entry.outcome-success {
    border-left-color: var(--success);
}

.roll-log-entry.outcome-success .roll-net,
.roll-log-entry.outcome-success .roll-outcome {
    color: var(--success);
}

.roll-log-entry.outcome-success .roll-outcome {
    background: var(--success-tint);
}

.roll-log-entry.outcome-fail {
    border-left-color: var(--fail);
}

.roll-log-entry.outcome-fail .roll-net,
.roll-log-entry.outcome-fail .roll-outcome {
    color: var(--fail);
}

.roll-log-entry.outcome-fail .roll-outcome {
    background: var(--fail-tint);
}

/* ---------- Statistics ---------- */

.summary {
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
}

#summary:empty::before {
    content: "Stats appear after your first roll.";
    color: var(--muted);
    font-size: 0.85rem;
}

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(6.75rem, 1fr));
    gap: 0.75rem;
}

.stat-card {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    padding: 0.75rem 0.9rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
}

.stat-label {
    font-size: 0.66rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--muted);
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    line-height: 1.2;
    font-variant-numeric: tabular-nums;
    color: var(--text);
}

.stat-pct {
    margin-left: 0.35rem;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--muted);
}

.text-success {
    color: var(--success);
}

.text-fail {
    color: var(--fail);
}

.stat-bar {
    display: flex;
    height: 10px;
    border-radius: 999px;
    background: var(--fail-tint);
    border: 1px solid var(--border);
    overflow: hidden;
}

.stat-bar-fill {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: linear-gradient(90deg, var(--success), light-dark(#1a7f43, #86efac));
    transition: width 0.4s ease;
}

/* ---------- Footer ---------- */

.footer {
    margin-top: 1.75rem;
    padding-top: 1.25rem;
    border-top: 1px solid var(--border);
    text-align: center;
    font-size: 0.85rem;
    color: var(--muted);
}

.footer a {
    color: var(--accent);
    font-weight: 600;
    text-decoration: none;
}

.footer a:hover {
    text-decoration: underline;
}

.footer a:focus-visible {
    border-radius: 4px;
}

/* ---------- Preferences ---------- */

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