/* ============================================================
   Reset & base
   ============================================================ */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg:        #1a1a2e;
    --surface:   #16213e;
    --surface2:  #0f3460;
    --accent:    #60a5fa;
    --accent2:   #4ade80;
    --danger:    #f87171;
    --text:      #e5e7eb;
    --text-muted:#9ca3af;
    --radius:    10px;
    --shadow:    0 4px 24px rgba(0,0,0,0.4);
}

html, body {
    height: 100%;
}

body {
    background-color: var(--bg);
    color: var(--text);
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    font-size: 1rem;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* ============================================================
   Navbar
   ============================================================ */
.navbar {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    padding: 0.875rem 2rem;
    background: var(--surface);
    border-bottom: 1px solid rgba(255,255,255,0.08);
    position: sticky;
    top: 0;
    z-index: 100;
}

.nav-brand {
    font-family: 'Century Gothic', 'Avant Garde', Futura, sans-serif;
    font-size: 1.75rem;
    font-weight: 400;
    letter-spacing: 0.03em;
    margin-right: auto;
    color: var(--text);
}

.nav-links {
    display: flex;
    gap: 0.5rem;
}

.nav-link {
    color: var(--text-muted);
    text-decoration: none;
    padding: 0.4rem 0.875rem;
    border-radius: var(--radius);
    font-size: 0.95rem;
    transition: color 0.15s, background 0.15s;
}

.nav-link:hover {
    color: var(--text);
    background: rgba(255,255,255,0.07);
}

.nav-link--logout {
    color: var(--danger);
}

.nav-link--logout:hover {
    background: rgba(248,113,113,0.1);
    color: var(--danger);
}

.nav-user {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 2px solid var(--accent);
}

.nav-name {
    font-size: 0.9rem;
    color: var(--text-muted);
}

/* ============================================================
   Main content
   ============================================================ */
.main-content {
    flex: 1;
    padding: 2.5rem 2rem;
    max-width: 1100px;
    width: 100%;
    margin: 0 auto;
}

/* ============================================================
   Flash messages
   ============================================================ */
.flash {
    padding: 0.875rem 1.25rem;
    border-radius: var(--radius);
    margin-bottom: 1.5rem;
    font-size: 0.95rem;
}

.flash--error {
    background: rgba(248,113,113,0.15);
    border: 1px solid var(--danger);
    color: var(--danger);
}

.flash--success {
    background: rgba(74,222,128,0.15);
    border: 1px solid var(--accent2);
    color: var(--accent2);
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.65rem 1.4rem;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: opacity 0.15s, transform 0.1s;
}

.btn:hover { opacity: 0.88; }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.btn--primary {
    background: var(--accent);
    color: #0f172a;
}

.btn--secondary {
    background: var(--surface2);
    color: var(--text);
}

.btn--google {
    background: #fff;
    color: #3c4043;
    font-weight: 600;
    padding: 0.75rem 1.75rem;
    font-size: 1rem;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

.btn--google:hover { opacity: 0.94; }

.btn--roll {
    background: linear-gradient(135deg, var(--accent), #818cf8);
    color: #0f172a;
    font-size: 1.2rem;
    padding: 0.9rem 2.5rem;
    border-radius: 50px;
    box-shadow: 0 0 24px rgba(96,165,250,0.3);
    margin-top: 1.5rem;
    transition: box-shadow 0.2s, opacity 0.15s, transform 0.1s;
}

.btn--roll:hover {
    box-shadow: 0 0 36px rgba(96,165,250,0.5);
}

/* ============================================================
   Login page
   ============================================================ */
.login-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
    padding: 4rem 2rem;
    text-align: center;
}

.login-container h1 {
    font-size: 2.5rem;
    font-weight: 800;
}

.login-subtitle {
    color: var(--text-muted);
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
}

.google-icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

/* ============================================================
   Poll (index) page
   ============================================================ */
.page-header {
    margin-bottom: 2rem;
}

.page-header h1 {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 0.4rem;
}

.subtitle {
    color: var(--text-muted);
}

.poll-form {
    background: var(--surface);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: var(--radius);
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    max-width: 560px;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.form-group label {
    font-size: 0.9rem;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

select {
    background: var(--bg);
    color: var(--text);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: var(--radius);
    padding: 0.65rem 1rem;
    font-size: 1rem;
    appearance: none;
    cursor: pointer;
}

select:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ============================================================
   Tabulation / results page
   ============================================================ */
.results-container {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.results-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 1rem;
}

.results-header h1 {
    font-size: 2rem;
    font-weight: 700;
}

.winner-banner {
    background: linear-gradient(135deg, rgba(74,222,128,0.15), rgba(96,165,250,0.15));
    border: 1px solid var(--accent2);
    border-radius: var(--radius);
    padding: 0.65rem 1.5rem;
    font-size: 1.1rem;
    color: var(--accent2);
}

.chart-wrapper {
    background: var(--surface);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: var(--radius);
    padding: 1.75rem;
    box-shadow: var(--shadow);
}

.round-controls {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.25rem;
}

.round-label {
    font-size: 1rem;
    color: var(--text-muted);
    font-weight: 600;
    flex: 1;
    text-align: center;
}

/* Wrapper div height is set dynamically by tabulation.js (per candidate count).
   position:relative is required by Chart.js responsive mode. */
#chart-canvas-wrap {
    position: relative;
    width: 100%;
}

#irv-chart {
    width: 100% !important;
}

.results-footer {
    padding-top: 0.5rem;
}

/* ============================================================
   D20 roller page
   ============================================================ */
.d20-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2.5rem;
    align-items: start;
}

@media (max-width: 700px) {
    .d20-layout {
        grid-template-columns: 1fr;
    }
}

/* Left: dice panel */
.dice-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.25rem;
    text-align: center;
}

.dice-panel h1 {
    font-size: 1.75rem;
    font-weight: 700;
}

.dice-container {
    width: 240px;
    height: 240px;
    display: block;
    position: relative;
    transition: filter 0.4s;
}

.dice-container canvas {
    display: block;
    position: relative;
    z-index: 1;
}

/* Glow div sits behind the canvas; box-shadow is set dynamically by updateGlow() */
.dice-glow {
    position: absolute;
    inset: 12%;
    border-radius: 50%;
    pointer-events: none;
    z-index: 0;
    transition: box-shadow 0.4s;
}

/* iOS only: exponential radial gradient overlay in front of canvas; set by updateGlow() */
.dice-glow-overlay {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    pointer-events: none;
    z-index: 2;
    transition: background 0.4s;
}

.d20-canvas { }

/* Genre result display */
.genre-result {
    background: var(--surface);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: var(--radius);
    padding: 1.25rem 2rem;
    width: 100%;
    max-width: 340px;
}

.genre-result--hidden {
    display: none;
}

@keyframes genre-reveal {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

.genre-result--revealed {
    animation: genre-reveal 0.4s ease forwards;
    border-color: var(--accent);
    box-shadow: 0 0 20px rgba(96,165,250,0.2);
}

.genre-result--fumble {
    opacity: 1;
    transform: translateY(0);
    border-color: var(--danger);
    box-shadow: 0 0 20px rgba(248,113,113,0.2);
    background: rgba(248,113,113,0.07);
    border: 1px solid var(--danger);
    border-radius: var(--radius);
    padding: 1.25rem 2rem;
    width: 100%;
    max-width: 340px;
    text-align: center;
}

.fumble-msg {
    color: var(--danger);
    font-size: 0.95rem;
    margin-top: 0.4rem;
}

.genre-label {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    margin-bottom: 0.4rem;
}

.genre-name {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--accent);
}

.roll-again-msg {
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* Right: rolls table */
.table-panel {
    background: var(--surface);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: var(--radius);
    padding: 1.75rem;
    box-shadow: var(--shadow);
}

.table-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1.25rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid rgba(255,255,255,0.08);
}

.table-panel h2 {
    font-size: 1.25rem;
    font-weight: 700;
    margin: 0;
}

.btn--reset {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
    padding: 0.3rem 0.875rem;
    font-size: 0.8rem;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.btn--reset:hover {
    background: rgba(248, 113, 113, 0.12);
}

.rolls-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
}

.rolls-table th,
.rolls-table td {
    text-align: left;
    padding: 0.65rem 0.875rem;
}

.rolls-table th {
    color: var(--text-muted);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.rolls-table td {
    border-bottom: 1px solid rgba(255,255,255,0.05);
}

.rolls-table tr:last-child td {
    border-bottom: none;
}

.rolls-table tr.my-roll td {
    color: var(--accent);
    font-weight: 600;
}

.rolls-table tr:not(.my-roll):hover td {
    background: rgba(255,255,255,0.03);
}

/* Roll column: center the die thumbnail */
.rolls-table th:nth-child(2),
.rolls-table td:nth-child(2) {
    text-align: center;
    width: 72px;
}

/* Die thumbnail in Today's Rolls */
.thumb-wrap {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
}
.roll-thumb {
    width: 52px;
    height: 52px;
    display: block;
}
.roll-thumb-num {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.88rem;
    font-weight: 700;
    text-shadow: 0 1px 3px rgba(0,0,0,1), 0 0 6px rgba(0,0,0,0.9);
    pointer-events: none;
}

.no-rolls {
    color: var(--text-muted);
    font-size: 0.95rem;
    padding: 1rem 0;
}

/* ============================================================
   Roll overlay (fullscreen emphasis during animation)
   ============================================================ */
.roll-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.78);
    z-index: 299;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.4s;
}
.roll-backdrop.active {
    opacity: 1;
    pointer-events: auto;
}

.roll-overlay {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -58%);
    width: 380px;
    height: 380px;
    z-index: 300;
    border-radius: 50%;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    filter: drop-shadow(0 0 40px rgba(96, 165, 250, 0.55));
    transition: opacity 0.35s;
}
.roll-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.roll-hold-display {
    position: fixed;
    top: calc(50% + 130px);
    left: 50%;
    transform: translateX(-50%);
    z-index: 301;
    text-align: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.4s;
}
.roll-hold-display.active { opacity: 1; }

.roll-hold-number {
    display: inline-block;   /* required for transform in ticker-lock animation */
    font-size: 4rem;
    font-weight: 900;
    color: var(--accent);
    line-height: 1;
    text-shadow: 0 0 15px currentColor;
    transition: color 0.06s;
}

.roll-hold-label {
    font-size: 4rem;
    color: var(--text-muted);
    margin-top: 0.3rem;
    letter-spacing: 0.05em;
    opacity: 0;
    transition: opacity 0.4s;
}

/* Slot-machine ticker: fast-cycling phase */
.roll-hold-display.cycling .roll-hold-label {
    font-size: 3rem;
    opacity: 1;
    transition: none;
}

.roll-hold-display.cycling .roll-hold-number {
    font-size: 3rem;
    opacity: 0.65;
    text-shadow: 0 0 8px currentColor;
    transition: none; /* instant color flips during fast cycling */
}

/* Slot-machine ticker: number locks on result */
.roll-hold-display.locked .roll-hold-label {
    opacity: 1;
}

.roll-hold-display.locked .roll-hold-number {
    animation: ticker-lock 0.45s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes ticker-lock {
    0%   { transform: scale(1); }
    55%  { transform: scale(1.32); }
    100% { transform: scale(1.15); }
}

/* ============================================================
   D20 roll number display
   ============================================================ */
.roll-number-display {
    font-size: 0.95rem;
    color: var(--text-muted);
    margin-bottom: 0.4rem;
}

.roll-number-display strong {
    color: var(--accent);
    font-size: 1.1rem;
}

/* ============================================================
   Natural 20 panel
   ============================================================ */
.nat20-panel {
    background: rgba(251, 191, 36, 0.08);
    border: 1px solid rgba(251, 191, 36, 0.45);
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    width: 100%;
    max-width: 340px;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    text-align: center;
}

.nat20-title {
    font-size: 1.3rem;
    font-weight: 700;
    color: #fbbf24;
}

.nat20-sub {
    font-size: 0.9rem;
    color: var(--text-muted);
}

.nat20-choices {
    display: flex;
    gap: 0.75rem;
    justify-content: center;
}

.custom-genre-form {
    display: flex;
    gap: 0.5rem;
}

.custom-input {
    flex: 1;
    background: var(--bg);
    color: var(--text);
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: var(--radius);
    padding: 0.5rem 0.875rem;
    font-size: 0.95rem;
}

.custom-input:focus {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ============================================================
   Genre reference table
   ============================================================ */
.genres-reference {
    margin-top: 2.5rem;
    background: var(--surface);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: var(--radius);
    padding: 1.5rem;
}

.genres-ref-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1.1rem;
}

.genres-ref-header h2 {
    margin-bottom: 0;
}

/* ------------------------------------------------------------------
   Die customization panel
   ------------------------------------------------------------------ */

.die-settings {
    position: relative;
    width: 100%;
    margin-top: 0.75rem;
}

.btn--settings {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    padding: 0.3rem 0.9rem;
    font-size: 0.78rem;
    border-radius: var(--radius);
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s;
    letter-spacing: 0.04em;
}
.btn--settings:hover,
.btn--settings.active {
    color: var(--accent);
    border-color: var(--accent);
}

.settings-panel {
    position: absolute;
    top: calc(100% + 0.4rem);
    left: 0;
    right: 0;
    z-index: 20;
    padding: 0.9rem 1rem;
    background: var(--surface);
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: var(--radius);
    box-shadow: 0 8px 24px rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}
.settings-panel.hidden { display: none; }

.settings-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    overflow: visible;
}

/* Color pickers row — three evenly spaced controls */
.settings-colors {
    justify-content: space-evenly;
}

.color-control {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.3rem;
}

.settings-label {
    font-size: 0.78rem;
    color: var(--text-muted);
    letter-spacing: 0.04em;
}

.color-control input[type="color"] {
    width: 2.4rem;
    height: 2rem;
    padding: 0.1rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: transparent;
    cursor: pointer;
}

.mode-btns {
    display: flex;
    gap: 0.4rem;
    width: 100%;
}

.mode-btn {
    flex: 1;
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    padding: 0.25rem 0.65rem;
    font-size: 0.75rem;
    border-radius: var(--radius);
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s, background 0.15s;
    text-align: center;
}
.mode-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
}
.mode-btn.active {
    color: var(--accent);
    border-color: var(--accent);
    background: rgba(96, 165, 250, 0.10);
}

.settings-row--actions {
    justify-content: center;
    gap: 0.5rem;
    padding-top: 0.25rem;
    border-top: 1px solid rgba(255,255,255,0.07);
}

.btn--prefs-save,
.btn--prefs-reset {
    padding: 0.3rem 1.1rem;
    font-size: 0.8rem;
    border-radius: var(--radius);
    font-weight: 700;
    cursor: pointer;
    border: none;
    transition: opacity 0.15s;
}

.btn--prefs-save {
    background: var(--accent);
    color: #0f172a;
}
.btn--prefs-save:hover { opacity: 0.85; }
.btn--prefs-save:disabled { opacity: 0.5; cursor: not-allowed; }

.btn--prefs-reset {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid rgba(255,255,255,0.15);
}
.btn--prefs-reset:hover { color: var(--text); border-color: rgba(255,255,255,0.35); }

.btn--randomize {
    background: transparent;
    color: var(--accent);
    border: 1px solid var(--accent);
    padding: 0.3rem 0.875rem;
    font-size: 0.8rem;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.btn--randomize:hover {
    background: rgba(96, 165, 250, 0.12);
}

.btn--randomize:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.genres-reference h2 {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.07em;
}

.genres-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 0.3rem 1.5rem;
}

.genre-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.3rem 0.5rem;
    border-radius: 6px;
}

.genre-row:hover {
    background: rgba(255,255,255,0.04);
}

.genre-row--fumble .genre-num,
.genre-row--fumble .genre-name-ref {
    color: var(--danger);
}

.genre-row--special {
    background: rgba(251, 191, 36, 0.07);
    border: 1px solid rgba(251, 191, 36, 0.25);
}

.genre-row--special .genre-num {
    color: #fbbf24;
}

.genre-num {
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--accent);
    width: 1.75rem;
    text-align: right;
    flex-shrink: 0;
}

.genre-name-ref {
    font-size: 0.9rem;
    color: var(--text);
}

/* ============================================================
   Tiebreaker panel
   ============================================================ */
.tiebreaker-panel {
    margin-top: 1.25rem;
    background: rgba(251, 191, 36, 0.08);
    border: 1px solid rgba(251, 191, 36, 0.4);
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    font-size: 0.9rem;
    line-height: 1.6;
}

.tiebreaker-panel .tb-title {
    font-weight: 700;
    color: #fbbf24;
    margin-bottom: 0.5rem;
    font-size: 0.95rem;
}

.tiebreaker-panel .tb-scores {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    margin: 0.5rem 0;
}

.tiebreaker-panel .tb-score-row {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
}

.tiebreaker-panel .tb-score-row.eliminated {
    background: rgba(248, 113, 113, 0.12);
    color: var(--danger);
}

.tiebreaker-panel .tb-score-row.survived {
    background: rgba(74, 222, 128, 0.1);
    color: var(--accent2);
}

.tiebreaker-panel .tb-note {
    color: var(--text-muted);
    font-size: 0.82rem;
    margin-top: 0.4rem;
}

/* ============================================================
   Mobile
   ============================================================ */
@media (max-width: 600px) {
    .navbar {
        padding: 0.5rem 0.875rem;
        gap: 0.5rem;
    }

    .nav-brand {
        font-size: 1.05rem;
        white-space: nowrap;
    }

    .nav-name {
        display: none;
    }

    .avatar {
        width: 26px;
        height: 26px;
    }

    .nav-link {
        padding: 0.3rem 0.5rem;
        font-size: 0.82rem;
    }

    .main-content {
        padding: 1.5rem 1rem;
    }

    .d20-layout {
        gap: 1.5rem;
    }

    /* Tabulation chart — tighter padding so bars have maximum horizontal room */
    .chart-wrapper {
        padding: 1rem 0.75rem;
    }

    /* Mode buttons: scale font and padding continuously with viewport width
       so all 5 labels (incl. "Metallic") stay inside the settings panel */
    .mode-btns {
        gap: clamp(0.15rem, 0.8vw, 0.4rem);
    }
    .mode-btn {
        font-size: clamp(0.58rem, 2.5vw, 0.75rem);
        padding: 0.25rem clamp(0.1rem, 0.9vw, 0.65rem);
    }
}

/* ============================================================
   Utilities
   ============================================================ */
.hidden {
    display: none !important;
}
