:root {
    --bg-color: #0d0221;
    --glass-bg: rgba(255, 255, 255, 0.05);
    --glass-border: rgba(255, 255, 255, 0.1);
    --accent-primary: #7b2cbf;
    --accent-secondary: #3c096c;
    --accent-glow: #9d4edd;
    --text-color: #e0e1dd;
    --text-muted: #8d99ae;
    --neon-blue: #00d4ff;
    --neon-pink: #ff007f;
    --card-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.8);
    --font-main: 'Outfit', sans-serif;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font-main);
    background-color: var(--bg-color);
    background-image:
        radial-gradient(circle at 20% 30%, rgba(123, 44, 191, 0.15) 0%, transparent 40%),
        radial-gradient(circle at 80% 70%, rgba(0, 212, 255, 0.1) 0%, transparent 40%);
    color: var(--text-color);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 2rem;
    overflow-x: hidden;
}

.container {
    width: 100%;
    max-width: 1200px;
    display: grid;
    grid-template-columns: 1fr 350px;
    gap: 1.5rem;
    height: calc(100vh - 4rem);
    min-height: 600px;
}

@media (max-width: 800px) {
    body {
        padding: 1rem;
        align-items: flex-start;
    }

    .container {
        grid-template-columns: 1fr;
        height: auto;
        display: flex;
        flex-direction: column;
    }

    .sidebar {
        order: 2;
    }

    .dice-tray-container {
        order: 1;
        min-height: 300px;
    }

    .header h1 {
        font-size: 2rem;
    }
}

/* Glassmorphism utility */
.glass {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    box-shadow: var(--card-shadow);
}

/* Left Section: Dice Tray */
.dice-tray-container {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    position: relative;
}

.dice-tray {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-content: center;
    gap: 1.5rem;
    padding: 2rem;
    min-height: 400px;
    position: relative;
    transition: all 0.3s ease;
}

.dice-tray::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 20px;
    background: radial-gradient(circle, rgba(157, 78, 221, 0.05) 0%, transparent 70%);
    pointer-events: none;
}

/* Right Section: Sidebar / Controls */
.sidebar {
    display: flex;
    flex-direction: column;
    gap: 0;
    /* Managed by children */
    overflow: hidden;
    /* Prevent sidebar itself from scrolling */
    padding-right: 0;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    padding-right: 0.5rem;
    padding-bottom: 1.5rem;
}

.sidebar-content::-webkit-scrollbar {
    width: 6px;
}

.sidebar-content::-webkit-scrollbar-thumb {
    background: var(--glass-border);
    border-radius: 10px;
}

.panel {
    padding: 1.5rem;
}

.header {
    margin-bottom: 2rem;
    text-align: center;
}

.header h1 {
    font-size: 2.5rem;
    font-weight: 800;
    background: linear-gradient(45deg, var(--neon-blue), var(--accent-glow));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -1px;
}

.header p {
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* Section Titles */
h2 {
    font-size: 1.1rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.btn-icon-clear {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    transition: color 0.2s;
}

.btn-icon-clear:hover {
    color: var(--neon-pink);
}

/* Dice Selector */
.dice-color-selector {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--glass-border);
    justify-content: center;
}

.color-btn {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    transition: all 0.2s;
}

.color-btn.active {
    border-color: white;
    transform: scale(1.2);
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

.dice-options {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.8rem;
    margin-bottom: 1rem;
}

.dice-btn {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    padding: 0.8rem;
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
    font-weight: 600;
}

.dice-btn:hover {
    transform: translateY(-5px);
    border-color: var(--accent-glow);
    background: rgba(157, 78, 221, 0.1);
    box-shadow: 0 0 15px rgba(157, 78, 221, 0.3);
}

.dice-btn img,
.dice-btn svg {
    width: 24px;
    height: 24px;
    fill: var(--text-color);
}

/* Selector Shapes */
.dice-btn::before {
    content: '';
    width: 30px;
    height: 30px;
    background: var(--text-color);
    margin-bottom: 5px;
    transition: background 0.2s;
}

.dice-btn:hover::before {
    background: var(--accent-glow);
}

.dice-btn[onclick*="'d4'"]::before {
    clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}

.dice-btn[onclick*="'d6'"]::before {
    border-radius: 4px;
}

.dice-btn[onclick*="'d8'"]::before {
    clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}

.dice-btn[onclick*="'d10'"]::before {
    clip-path: polygon(50% 0%, 100% 35%, 50% 100%, 0% 35%);
}

.dice-btn[onclick*="'d12'"]::before {
    clip-path: polygon(50% 0%, 95% 35%, 80% 100%, 20% 100%, 5% 35%);
}

.dice-btn[onclick*="'d20'"]::before {
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
}

/* Current Dice Set Bar */
.current-set-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.btn-clear {
    background: transparent;
    border: 1px solid rgba(255, 0, 127, 0.3);
    color: var(--neon-pink);
    padding: 0.4rem 0.8rem;
    border-radius: 8px;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-clear:hover {
    background: rgba(255, 0, 127, 0.1);
    border-color: var(--neon-pink);
}

.stats-group {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.tray-sum {
    position: absolute;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%);
    font-weight: 800;
    color: var(--neon-blue);
    font-size: 3rem;
    opacity: 0.3;
    pointer-events: none;
    text-shadow: 0 0 20px rgba(0, 212, 255, 0.4);
    z-index: 1;
    transition: opacity 0.3s ease;
}

.dice-tray:hover .tray-sum {
    opacity: 0.5;
}

.tray-sum.corner-mode {
    bottom: 1rem;
    left: auto;
    right: 1.5rem;
    transform: none;
    font-size: 2.5rem;
    opacity: 0.15;
}

/* Roll Button */
.roll-section {
    position: sticky;
    bottom: 0;
    padding-top: 1rem;
}

.btn-roll {
    width: 100%;
    padding: 1.25rem;
    background: linear-gradient(45deg, var(--accent-primary), var(--accent-secondary));
    border: none;
    border-radius: 15px;
    color: white;
    font-size: 1.4rem;
    font-weight: 800;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 2px;
    box-shadow: 0 10px 25px rgba(123, 44, 191, 0.4);
    transition: all 0.2s;
    position: relative;
    overflow: hidden;
}

.btn-roll:hover {
    transform: scale(1.02);
    box-shadow: 0 15px 35px rgba(123, 44, 191, 0.6);
}

.btn-roll:active {
    transform: scale(0.98);
}

.btn-roll::after {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.2) 0%, transparent 70%);
    opacity: 0;
    transition: opacity 0.3s;
}

.btn-roll:hover::after {
    opacity: 1;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% {
        transform: scale(1);
        opacity: 0.5;
    }

    50% {
        transform: scale(1.2);
        opacity: 0.2;
    }

    100% {
        transform: scale(1);
        opacity: 0.5;
    }
}

/* Cubiletes / Saved Sets */
.saved-sets-list {
    display: flex;
    flex-direction: column;
    gap: 0.8rem;
    max-height: 200px;
    overflow-y: auto;
}

.saved-set-item {
    background: rgba(0, 0, 0, 0.2);
    padding: 0.8rem 1rem;
    border-radius: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all 0.2s;
}

.saved-set-item:hover {
    background: rgba(255, 255, 255, 0.08);
    border-color: var(--glass-border);
}

.saved-set-item .set-name {
    font-weight: 600;
    font-size: 0.95rem;
}

.saved-set-item .set-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Modifier Controls */
.modifier-container {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.5rem 0;
}

.btn-modifier {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    border: 1px solid var(--glass-border);
    background: var(--glass-bg);
    color: var(--text-color);
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-modifier:hover {
    background: var(--accent-primary);
    border-color: var(--accent-primary);
    transform: scale(1.1);
}

.modifier-container input {
    width: 50px;
    background: transparent;
    border: none;
    color: var(--text-color);
    text-align: center;
    font-size: 1.2rem;
    font-weight: 700;
}

.btn-modifier-reset {
    padding: 0.4rem 0.8rem;
    border-radius: 8px;
    border: 1px solid var(--glass-border);
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-muted);
    font-size: 0.8rem;
    cursor: pointer;
}

.btn-modifier-reset:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-color);
}

.btn-save-current {
    width: 100%;
    background: var(--glass-bg);
    border: 1px dashed var(--glass-border);
    color: var(--text-color);
    padding: 0.8rem;
    border-radius: 12px;
    cursor: pointer;
    margin-top: 1rem;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.btn-save-current:hover {
    background: rgba(255, 255, 255, 0.1);
    border-style: solid;
}

/* Dice visuals */
.die-container {
    position: relative;
    width: 90px;
    height: 95px;
    display: flex;
    justify-content: center;
    align-items: center;
    animation: dropIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    transition: transform 0.2s;
}

.die {
    width: 80px;
    height: 80px;
    background: white;
    border-radius: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2rem;
    font-weight: 800;
    color: var(--bg-color);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3),
        inset 0 -5px 10px rgba(0, 0, 0, 0.2),
        inset 0 5px 10px rgba(255, 255, 255, 0.4);
    user-select: none;
    position: relative;
    overflow: hidden;
}

.die::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.2) 0%, rgba(255, 255, 255, 0) 50%, rgba(0, 0, 0, 0.1) 100%);
    pointer-events: none;
}

.die.rolling {
    animation: shake 0.4s infinite linear;
}

.die-container.critical-hit {
    filter: drop-shadow(0 0 12px #ffd700);
    z-index: 10;
}

.die-container.critical-hit .die {
    animation: criticalHit 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes criticalHit {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.35);
        filter: brightness(1.2);
    }

    100% {
        transform: scale(1.15);
    }
}

/* Locking system */
.die-container.locked .die {
    opacity: 0.8;
    filter: grayscale(0.5) contrast(0.8);
    transform: scale(0.95);
    border: 2px solid var(--accent-primary);
    box-shadow: 0 0 20px rgba(0, 243, 255, 0.3);
}

/* Nav Panel & Hub Link */
.nav-panel {
    margin-top: auto;
    padding: 0;
    overflow: hidden;
    border-top: 1px solid var(--glass-border);
    background: rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.btn-hub-link {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    padding: 1rem;
    color: var(--text-main);
    text-decoration: none;
    font-weight: 700;
    font-size: 0.95rem;
    background: rgba(255, 255, 255, 0.05);
    transition: all 0.2s;
}

.btn-hub-link:hover {
    background: var(--accent-primary);
    color: #000;
}

.hub-icon {
    font-size: 1.2rem;
}

.lock-icon {
    position: absolute;
    top: -8px;
    left: -8px;
    font-size: 1.2rem;
    opacity: 0;
    transition: all 0.2s;
    cursor: pointer;
    z-index: 21;
    text-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
    pointer-events: auto;
}

.die-container.locked .lock-icon {
    opacity: 1;
}

.die-container .die {
    cursor: pointer;
}

.remove-die {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 24px;
    height: 24px;
    background: var(--neon-pink);
    color: white;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2rem;
    line-height: 1;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
    z-index: 20;
    opacity: 0;
    transition: all 0.2s;
    pointer-events: auto;
}

.die-container:hover .remove-die {
    opacity: 1;
    transform: scale(1.1);
}

.rolling-container {
    pointer-events: none;
}

.die[data-type="d8"] {
    clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
    width: 80px;
    height: 90px;
    box-shadow: none;
    background-image: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, transparent 50%, rgba(0, 0, 0, 0.1) 100%),
        linear-gradient(225deg, rgba(255, 255, 255, 0.05) 0%, transparent 50%);
}

.die[data-type="d4"] {
    clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
    width: 90px;
    height: 80px;
    padding-top: 25px;
    box-shadow: none;
    background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.1) 0%, transparent 60%),
        linear-gradient(60deg, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
}

.die[data-type="d10"] {
    clip-path: polygon(50% 0%, 100% 35%, 50% 100%, 0% 35%);
    width: 80px;
    height: 95px;
    padding-bottom: 20px;
    box-shadow: none;
    background-image: linear-gradient(180deg, rgba(255, 255, 255, 0.1) 0%, transparent 40%, rgba(0, 0, 0, 0.05) 100%);
}

.die[data-type="d12"] {
    clip-path: polygon(50% 0%, 95% 35%, 80% 100%, 20% 100%, 5% 35%);
    width: 85px;
    height: 85px;
    box-shadow: none;
    background-image: radial-gradient(circle at 50% 10%, rgba(255, 255, 255, 0.15) 0%, transparent 70%);
}

.die[data-type="d20"] {
    clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
    width: 85px;
    height: 95px;
    box-shadow: none;
    background-image: linear-gradient(135deg, rgba(255, 255, 255, 0.1) 0%, transparent 50%, rgba(0, 0, 0, 0.05) 100%),
        radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.05) 0%, transparent 80%);
}

/* Particles */
.particle {
    position: absolute;
    width: 6px;
    height: 6px;
    background: #ffd700;
    border-radius: 50%;
    pointer-events: none;
    z-index: 100;
    box-shadow: 0 0 10px #ffd700;
}

@keyframes particle-explode {
    0% {
        transform: translate(0, 0) scale(1);
        opacity: 1;
    }

    100% {
        transform: translate(var(--tx), var(--ty)) scale(0);
        opacity: 0;
    }
}

/* New: Coin (d2) */
.die[data-type="d2"] {
    border-radius: 50%;
    background: linear-gradient(135deg, #ffd700, #b8860b);
    color: #4a3c00;
    border: 4px double rgba(0, 0, 0, 0.2);
    width: 80px;
    height: 80px;
}

/* New: Fate Die */
.die[data-type="dfate"] {
    background: #004b23;
    color: white;
    font-size: 2.5rem;
}

@keyframes coinFlip {
    0% {
        transform: scale(1) rotateY(0);
    }

    50% {
        transform: scale(1.4) rotateY(720deg);
    }

    100% {
        transform: scale(1) rotateY(1440deg);
    }
}

.die[data-type="d2"].rolling {
    animation: coinFlip 0.6s infinite ease-in-out;
}


@keyframes dropIn {
    from {
        opacity: 0;
        transform: translateY(-30px) scale(0.8);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Roll History */
.roll-history-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-height: 300px;
    overflow-y: auto;
}

.history-item {
    background: rgba(255, 255, 255, 0.03);
    border-radius: 10px;
    padding: 0.6rem 0.8rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    border-left: 3px solid var(--accent-glow);
    animation: slideIn 0.3s ease-out;
}

.history-sum {
    font-weight: 800;
    font-size: 1.2rem;
    color: var(--neon-blue);
    min-width: 30px;
}

.history-details {
    display: flex;
    flex-direction: column;
}

.history-math {
    font-size: 0.7rem;
    color: var(--text-color);
}

.history-time {
    font-size: 0.6rem;
    color: var(--text-muted);
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateX(-10px);
    }

    to {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes shake {
    0% {
        transform: rotate(0deg) translate(0, 0);
    }

    25% {
        transform: rotate(5deg) translate(2px, -2px);
    }

    50% {
        transform: rotate(0deg) translate(-2px, 2px);
    }

    75% {
        transform: rotate(-5deg) translate(2px, 2px);
    }

    100% {
        transform: rotate(0deg) translate(0, 0);
    }
}

.remove-die {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 24px;
    height: 24px;
    background: var(--neon-pink);
    border-radius: 50%;
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
    border: 2px solid var(--bg-color);
}

.die:hover .remove-die {
    opacity: 1;
}

/* Modal for saving cubilete */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(5px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal {
    width: 90%;
    max-width: 400px;
    padding: 2rem;
}

.modal h3 {
    margin-bottom: 1rem;
}

.modal input {
    width: 100%;
    padding: 1rem;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--glass-border);
    border-radius: 10px;
    color: white;
    margin-bottom: 1.5rem;
    font-family: inherit;
    font-size: 1rem;
}

.modal-btns {
    display: flex;
    gap: 1rem;
}

.btn-confirm {
    flex: 1;
    padding: 0.8rem;
    background: var(--accent-primary);
    border: none;
    border-radius: 8px;
    color: white;
    font-weight: 600;
    cursor: pointer;
}

.btn-cancel {
    flex: 1;
    padding: 0.8rem;
    background: transparent;
    border: 1px solid var(--glass-border);
    color: var(--text-muted);
    border-radius: 8px;
    cursor: pointer;
}

/* Interstitial Ad Mock */

/* Settings Panel */
.settings-panel {
    margin-top: auto;
    padding-top: 1rem;
    border-top: 1px solid var(--glass-border);
}

.setting-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.8rem;
}

.setting-item label {
    font-size: 0.9rem;
    color: var(--text-color);
}

.switch {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 20px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--glass-border);
    transition: .4s;
    border-radius: 20px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked+.slider {
    background-color: var(--accent-glow);
}

input:checked+.slider:before {
    transform: translateX(20px);
}

/* Custom Scrollbar and Range Slider */
.slider-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.8rem;
}

.slider-container {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 1rem;
}

.slider-container input[type="range"] {
    flex: 1;
    -webkit-appearance: none;
    appearance: none;
    background: var(--glass-border);
    height: 6px;
    border-radius: 10px;
    outline: none;
}

.slider-container input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    background: var(--accent-glow);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 0 10px rgba(157, 78, 221, 0.5);
}

#durationValue {
    font-size: 0.8rem;
    color: var(--text-muted);
    min-width: 35px;
}