:root {
    --bg: #0b0c10; /* Dark background */
    --fg: #f5f6fa; /* Main text color */
    --muted: #8d99ae; /* Subtle text */
    --accent: #3b82f6; /* Primary blue */
    --accent-2: #2563eb; /* Darker blue for hover */
    --accent-glow: rgba(59, 130, 246, 0.7); /* Blue glow effect */
    --danger: #ef4444;
    --card: #141820;
    --border: #1f2937; /* Subtle dark border */
}

html, body {
    height: 100dvh;
    min-height: 100dvh;
    background: var(--bg);
    color: var(--fg);
    font-family: "Segoe UI", Inter, ui-sans-serif, system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
}

h1, h2, h3, h4 {
    margin: 0;
}

.page {
    min-height: 100vh;
}

.sidebar {
    background: #121317;
    border-right: 1px solid var(--border);
    padding: 1rem;
}

main .top-row {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
}

.content {
    /*padding: 1rem 1.5rem 4.5rem;*/
}

.content-wrapper{
    padding: 1.5rem;
    box-sizing: border-box;
}

.input {
    background: #0e1014;
    color: var(--fg);
    border: 1px solid var(--border);
    border-radius: .5rem;
    padding: .5rem .75rem;
    outline: none;
    width: 100%;
    box-sizing: border-box;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 6px var(--accent-glow);
}

.btn {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 3px;
    padding: 6px 10px;
    cursor: pointer;
    transition: background 0.2s ease;
}

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

.btn.secondary {
    background: transparent;
    border: 1px solid var(--accent);
    color: 1px solid var(--accent);
}

.btn.danger {
    border: 1px solid var(--danger);
    background: transparent;
    border-color: var(--danger);
    color: var(--danger);
}

.btn.danger.primary {
    background: var(--danger);
    color: white;
    border: none;
    border-radius: 3px;
    padding: 6px 10px;
    cursor: pointer;
    transition: background 0.2s ease;
}

.icon-btn {
    background: transparent;
    color: var(--fg);
    border: 1px solid var(--border);
    border-radius: .4rem;
    padding: .25rem .5rem;
    cursor: pointer;
}

.badge-btn {
    background: #20242c;
    color: var(--fg);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: .25rem .75rem;
    cursor: pointer;
}

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

.blocks {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
    gap: 1rem;
}

.block-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: .75rem;
    padding: 0 1rem 0 1rem;
}

.event-composer h4{
    padding: 0 0 0 1rem;
}

.new-block, .new-ref {
    display: flex;
    gap: .5rem;
    margin-bottom: 1rem;
}

.new-ref .input {
    width: auto;
}

.new-ref select.input {
    width: 220px;
}

.new-ref input.input {
    flex: 1;
}

.event-composer .actions {
    margin-top: .5rem;
    text-align: right;
}

.event-list {
    margin-top: 1rem;
    margin-bottom: 1rem;
    display: flex;
    flex-direction: column;
    gap: .75rem;
    max-height: 78vh;
    padding: 0.5rem 0.5rem 1.5rem 0.5rem;
    overflow: auto;
    position: relative;
    z-index: 0; /* ensure baseline behind fixed modals */
    --fade-size: 24px;
    
    -webkit-mask-image:
            linear-gradient(to bottom, transparent 0, black var(--fade-size), black calc(100% - var(--fade-size)), transparent 100%);
    mask-image:
            linear-gradient(to bottom, transparent 0, black var(--fade-size), black calc(100% - var(--fade-size)), transparent 100%);
}

/* When any modal is open inside event-composer, disable the fade mask to avoid overlap */
.event-composer:has(.modal-backdrop) .event-list {
    -webkit-mask-image: none;
    mask-image: none;
}

.event-list::before,
.event-list::after {
    display: none;
}

.event-card {
    background: var(--card);
    border: 1px solid var(--accent);
    border-radius: 0.75rem;
    padding: 0.85rem;
    box-shadow: inset 0 0 8px var(--accent-glow);
    transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

.event-card:hover {
    box-shadow: inset 0 0 14px var(--accent-glow);
    border-color: var(--accent);
}

.event-content {
    font-size: 1rem;
    margin-bottom: 0.25rem;
    white-space: pre-wrap;
    color: var(--fg);
}

.event-user {
    font-size: 0.9rem;

    color: var(--muted);
}

.meta {
    font-size: 0.8rem;
    color: var(--muted);
    display: flex;
    gap: 0.4rem;
}

.ref-admin-list {
    list-style: none;
    padding: 0.5rem;
    margin: 0;
    display: grid;
    gap: .5rem;
    max-height: 68vh;
    overflow-y: scroll;
}

.ref-admin-item {
    display: flex;
    align-items: center;
    gap: .5rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: .5rem;
    padding: .5rem .75rem;
}

.quickplay-item {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: .5rem;
    padding: .5rem .75rem;
}

.badge {
    font-size: .8rem;
    padding: .1rem .4rem;
    border-radius: .35rem;
    border: 1px solid var(--border);
}

.badge.pc {
    background: #1f2430;
    color: #a0aec0;
}

.badge.other {
    background: #1c222a;
    color: #9ae6b4;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    display: grid;
    place-items: center;
    z-index: 2000;
}

.modal {
    width: min(800px, 90vw);
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: .75rem;
    box-shadow: 0 10px 30px rgba(0,0,0,.6);
    position: relative; /* ensure it stacks above backdrop */
    z-index: 2100; /* higher than .modal-backdrop (2000) */
}

.modal-header, .modal-footer {
    padding: .75rem 1rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-footer {
    border-bottom: none;
    border-top: 1px solid var(--border);
}

.modal-body {
    padding: 1rem;
}

.ref-list {
    display: grid;
    gap: .5rem;
    max-height: 70vh;
    margin-top: .5rem;
    overflow-y: scroll;
}

.ref-item {
    display: flex;
    align-items: center;
    gap: .5rem;
}

/* Separator between reference items in scrollable lists */
.ref-list .ref-item + .ref-item {
    border-top: 1px solid var(--border);
}

.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--card);
    border-top: 1px solid var(--border);
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    z-index: 1000;
}

.bottom-nav .nav-item {
    padding: .75rem 1rem;
    text-align: center;
    color: var(--fg);
    text-decoration: none;
    border-right: 1px solid var(--border);
}

.bottom-nav .nav-item:last-child {
    border-right: none;
}

.bottom-nav .nav-item.active {
    background: rgba(255,255,255,.04);
    color: #fff;
    font-weight: 600;
}

.block-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .5rem;
    padding: 0.5rem 1rem calc(.75rem + env(safe-area-inset-bottom));
}

.ref-form {
    display: grid;
    grid-template-columns: 220px 1fr auto;
    grid-template-areas:
        "type name add"
        "desc desc add";
    gap: .5rem;
    margin-bottom: 1rem;
    align-items: end;
}

.ref-form .field {
    display: flex;
    flex-direction: column;
    gap: .25rem;
}

.ref-form .field label {
    font-size: .8rem;
    color: var(--muted);
}

.ref-form .field.type {
    grid-area: type;
}

.ref-form .field.name {
    grid-area: name;
}

.ref-form .field.desc {
    grid-area: desc;
}

.ref-form .actions {
    grid-area: add;
    align-self: end;
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
}

.ref-form textarea.input {
    min-height: 96px;
    resize: vertical;
}

.retry-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--muted);
    margin-top: 0.25rem;
}

.spinner {
    width: 14px;
    height: 14px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    display: inline-block;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

* {
    -webkit-tap-highlight-color: rgba(255,255,255,0.08);
}

.modal-body > .input:first-child {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--card);
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}

.modal,
.ref-list {
    overscroll-behavior: contain;
}

.ref-item input[type="checkbox"] {
    width: 22px;
    height: 22px;
    accent-color: var(--accent);
}

.ref-type-bar{
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    margin: 0.5rem 0 0 0;
    gap: 0.5rem;
    border: 1px solid var(--border);
    border-radius: .5rem;
    padding: .5rem .75rem;
}

.btn.memorize {
    background: #1f2937;
    color: #a78bfa;
    border: 1px solid #6d28d9;
    transition: all 0.2s ease;
}

.btn.memorize:hover:not(:disabled) {
    background: #374151;
    color: #c4b5fd;
    border-color: #7c3aed;
    box-shadow: 0 0 8px rgba(167, 139, 250, 0.3);
}

.btn.memorize:disabled {
    background: #111827;
    color: #6b7280;
    border-color: #374151;
    cursor: not-allowed;
    opacity: 0.6;
}

hr {
    display: block;
    height: 1px;
    border: 0;
    border-top: 1px solid var(--border);
    margin: 1em 0;
    padding: 0;
}

.roll-manager {
    margin: .5rem 0 .5rem 0;
}
.roll-form {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: .5rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: .5rem;
    padding: .5rem;
    margin-bottom: .75rem;
}
.modal-body .field { display: flex; flex-direction: column; gap: .25rem; }
.modal-body .field label { font-size: .8rem; color: var(--muted); }
.modal-body .dcvs { display: flex; align-items: center; gap: .5rem; }
.modal-body .dcvs .radio { display: flex; align-items: center; gap: .25rem; }
.modal-body .dcvs .input.small { width: 90px; }

.roll-list { display: grid; gap: .5rem; }
.roll-row {
    background: #374045;
    border: 1px solid var(--border);
    border-radius: .5rem;
    padding: .5rem .75rem;
}
.roll-row-header { font-weight: 600; margin-bottom: .15rem; }
.roll-row-body { display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
.roll-row-body .label { padding: .05rem .35rem; border-radius: .25rem; border: 1px solid transparent; }
.roll-row-body .label.blue { color: #4b8ed7; border-color: #4b8ed7; }
.roll-row-body .label.red { color: #fe4736; border-color: #fe4736; }
.roll-row-body .label.purple { color: #c364e7; border-color: #c364e7; }
.roll-row-body .label.green { color: #00d093; border-color: #00d093; }
.roll-row-body .label.yellow { color: #f5be2c; border-color: #f5be2c; }
.roll-row-body .divider { opacity: .6; }
.roll-row-body .value { font-weight: 700; }
.roll-row-actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: .35rem; }
.success { color: #00d093; font-weight: 700; }
.fail { color: #fe4736; font-weight: 700; }
.desc { opacity: .9; }

@media (max-width: 640px) {
    .modal-backdrop {
        place-items: stretch;
    }

    .modal {
        width: 100vw;
        height: 100dvh;
        max-height: none;
        border-radius: 0;
        border: none;
        display: grid;
        grid-template-rows: auto 1fr auto;
        box-shadow: none;
    }

    .modal-header {
        top: 0;
        position: sticky;
        z-index: 3;
        background: var(--card);
        padding-top: calc(.75rem + env(safe-area-inset-top));
        padding-bottom: .75rem;
    }

    .modal-footer {
        bottom: 0;
        padding-bottom: calc(.75rem + env(safe-area-inset-bottom));
        border-top: 1px solid var(--border);
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: .5rem;
        position: sticky;
        z-index: 3;
        background: var(--card);
        padding-top: calc(.75rem + env(safe-area-inset-top));
    }

    .modal-body {
        padding: .75rem;
        overflow: auto;
        -webkit-overflow-scrolling: touch;
    }

    .ref-item {
        gap: .75rem;
        min-height: 48px;
        border-radius: .5rem;
    }

    .badge {
        font-size: .9rem;
        padding: .2rem .5rem;
    }

    .modal-footer .btn {
        width: 100%;
    }

    .ref-form {
        grid-template-columns: 1fr;
        grid-template-areas:
            "type"
            "name"
            "desc"
            "add";
        align-items: stretch;
    }

    .ref-form .actions {
        justify-self: end;
        gap: 0.5rem;
    }
}
/* Hide bottom nav when any modal is open to avoid overlap regardless of nesting */
body:has(.modal-backdrop) .bottom-nav {
    display: none !important;
}
