:root {
    --bg-parchment: #e8e0cc;
    --ink-black: #1a1512;
    --blood-red: #8a1c1c;
    --magic-blue: #2c3e50;
    --gold: #c5a059;
    --wood: #5d4037;
    --sheet-paper: #f4f1ea;
    
    --font-header: 'Cinzel', 'Georgia', serif;
    --font-body: 'Lato', 'Helvetica Neue', 'Arial', sans-serif;
}

#aidm-stage {
    background: var(--bg-parchment);
    color: var(--ink-black);
    font-family: var(--font-body);
    border: 5px double var(--wood);
    max-width: 1000px;
    margin: 20px auto;
    position: relative;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
    overflow: hidden; 
}

#aidm-ui-layer {
    height: 100%;
    width: 100%;
    overflow-y: auto;
    position: relative;
}

.aidm-title { 
    font-family: var(--font-header); 
    color: var(--blood-red); 
    text-align: center; 
    border-bottom: 2px solid var(--ink-black); 
    padding-bottom: 10px; 
}

.aidm-btn {
    background: var(--ink-black); 
    color: var(--bg-parchment);
    border: 2px solid var(--gold); 
    padding: 10px 20px;
    font-family: var(--font-header); 
    cursor: pointer; 
    text-transform: uppercase;
    transition: 0.2s; 
    touch-action: manipulation;
}
.aidm-btn:hover { background: var(--blood-red); }
.aidm-btn.small { padding: 5px 10px; font-size: 0.8em; }

.aidm-game-layout { display: flex; height: 100%; position: relative; }

.aidm-sidebar {
    width: 280px; 
    background: #dcd3b5; 
    border-right: 2px solid var(--wood);
    padding: 20px; 
    overflow-y: auto; 
    height: 100%;
}
.aidm-sidebar-close { display: none; } 

.aidm-main {
    flex: 1; 
    display: flex; 
    flex-direction: column; 
    height: 100%; 
    position: relative;
}

.aidm-log {
    flex: 1; 
    overflow-y: auto; 
    padding: 20px; 
    padding-bottom: 20px;
    background: rgba(255,255,255,0.3);
    scroll-behavior: smooth;
}

.aidm-footer {
    border-top: 1px solid rgba(0,0,0,0.1);
    background: rgba(232, 224, 204, 0.95);
    padding: 15px;
}

.aidm-controls { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.aidm-opt-btn { 
    background: var(--sheet-paper); 
    color: var(--ink-black);
    border: 1px solid var(--wood); 
    padding: 10px 15px; 
    cursor: pointer; 
    font-size: 0.9em; 
    flex: 1; 
    min-width: 120px;
}
.aidm-opt-btn:hover { background: var(--wood); color: #fff; }

.aidm-input-area { display: flex; gap: 10px; }
#player-input { flex: 1; padding: 12px; font-size: 16px; border: 1px solid var(--wood); }
#btn-act { padding: 0 20px; font-weight: bold; }

.aidm-mobile-header {
    display: none; 
    justify-content: space-between; 
    align-items: center;
    padding: 10px 15px; 
    background: var(--wood); 
    color: #fff;
    border-bottom: 2px solid var(--gold);
    z-index: 50;
}
.mob-stats { font-family: var(--font-header); font-size: 0.9em; }
.mob-name { font-weight: bold; margin-right: 10px; color: var(--gold); }

@media (max-width: 768px) {
    #aidm-stage {
        margin: 0; 
        border: none; 
        height: 100dvh; 
        max-width: 100%;
        display: flex; 
        flex-direction: column;
    }

    #aidm-ui-layer {
        flex: 1;
        overflow-y: auto; 
        -webkit-overflow-scrolling: touch;
        height: 100%;
        display: flex; 
        flex-direction: column;
    }

    .aidm-menu, .aidm-creator {
        padding-bottom: 80px; 
        margin: auto 0;
    }
    
    .aidm-game-layout { 
        height: calc(100dvh - 55px); 
        flex: 1;
    } 
    
    .aidm-mobile-header { display: flex; flex-shrink: 0; height: 55px; }
    
    .aidm-sidebar {
        position: absolute; 
        top: 0; 
        left: -100%;
        width: 85%; 
        height: 100%;
        z-index: 1000;
        box-shadow: 2px 0 10px rgba(0,0,0,0.5);
        transition: left 0.3s ease-in-out;
    }
    .aidm-sidebar.open { left: 0; }
    
    .aidm-sidebar-close {
        display: block; 
        padding: 10px; 
        background: var(--blood-red); 
        color: #fff;
        text-align: center; 
        cursor: pointer; 
        margin-bottom: 15px;
        font-family: var(--font-header); 
        text-transform: uppercase;
    }
    
    .aidm-main { width: 100%; }
    .aidm-footer { padding: 10px; padding-bottom: 20px; }
    
    .aidm-controls { flex-direction: column; gap: 8px; }
    .aidm-opt-btn { width: 100%; padding: 12px; text-align: center; background: rgba(255,255,255,0.8); }

    .dice-scene { bottom: 180px !important; }
}

.aidm-menu, .aidm-creator { padding: 40px; text-align: center; }
.aidm-cols { display: flex; gap: 20px; text-align: left; margin-top: 20px; }
.aidm-col { flex: 1; padding: 20px; background: rgba(0,0,0,0.05); border-radius: 4px; }
.aidm-field, .aidm-col label { display: block; margin-bottom: 10px; font-weight: bold; font-family: var(--font-header); }
select, input[type="text"] { width: 100%; padding: 8px; background: #fffdf5; border: 1px solid var(--wood); font-family: var(--font-body); }
.aidm-stat-box { margin-top: 20px; border: 1px dashed var(--wood); padding: 10px; text-align: center; font-weight: bold; font-size: 1.1em; line-height: 1.8; }
.aidm-entry { margin-bottom: 15px; line-height: 1.5; }
.aidm-entry.player { color: var(--wood); font-style: italic; }
.aidm-entry.dm { color: var(--ink-black); }
.aidm-entry .dmg { color: var(--blood-red); font-weight: bold; }
.aidm-sheet-compact h3 { margin: 0; font-family: var(--font-header); border-bottom: 1px solid var(--wood); }
.aidm-sub { font-size: 0.85em; font-style: italic; margin-bottom: 15px; }
.aidm-vitals { display: flex; justify-content: space-between; margin-bottom: 15px; background: #cfc5a5; padding: 5px; border-radius: 4px; }
.vital-box { text-align: center; }
.vital-box label { display: block; font-size: 0.7em; text-transform: uppercase; }
.vital-box span { font-weight: bold; font-size: 1.2em; }
.critical { color: var(--blood-red); animation: pulse 1s infinite; }
.aidm-stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; font-size: 0.9em; margin-bottom: 20px; }
.aidm-inv-section ul { list-style: square; padding-left: 20px; font-size: 0.9em; }
#aidm-save-status { font-family: var(--font-body); opacity: 0.7; transition: opacity 0.3s; }
#aidm-loader { position: absolute; top:0; left:0; width:100%; height:100%; background: rgba(232, 224, 204, 0.8); display: none; justify-content: center; align-items: center; z-index: 10; }
.d20-spinner { width: 40px; height: 40px; background: var(--blood-red); clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%); animation: spin 1s linear infinite; }

@keyframes spin { 100% { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: 0.5; } }

.aidm-modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.85); z-index: 9999; display: none; align-items: center; justify-content: center; }
.aidm-modal-content { background: var(--sheet-paper); width: 900px; max-width: 95%; height: 90%; border: 3px solid var(--wood); padding: 30px; position: relative; overflow-y: auto; box-shadow: 0 0 30px #000; -webkit-overflow-scrolling: touch; }
.aidm-close { position: absolute; top: 10px; right: 15px; font-size: 2em; cursor: pointer; color: var(--wood); z-index: 100; }
.aidm-sheet-header { border-bottom: 2px solid var(--wood); padding-bottom: 15px; margin-bottom: 15px; display: flex; justify-content: space-between; align-items: flex-end; }
.sheet-identity h1 { margin: 0; font-family: var(--font-header); color: var(--blood-red); font-size: 2em; line-height: 1.2; }
.sheet-sub { font-size: 1.1em; color: var(--wood); font-weight: bold; text-transform: uppercase; }
.sheet-meta { display: flex; gap: 20px; text-align: center; }
.meta-field label { display: block; font-size: 0.7em; text-transform: uppercase; color: #666; }
.meta-field span { font-weight: bold; font-family: var(--font-header); }
.aidm-sheet-tabs { display: flex; gap: 5px; border-bottom: 1px solid #ccc; margin-bottom: 20px; overflow-x: auto; white-space: nowrap; padding-bottom: 2px; }
.sheet-tab { background: #e0d8c0; border: 1px solid #ccc; border-bottom: none; padding: 12px 20px; cursor: pointer; font-family: var(--font-header); flex-shrink: 0; }
.sheet-tab.active { background: var(--sheet-paper); font-weight: bold; border-top: 2px solid var(--blood-red); }
.sheet-pane { display: none; padding-bottom: 60px; }
.sheet-pane.active { display: block; animation: fadeIn 0.3s; }
.ability-scores-row { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; margin-bottom: 30px; }
.ability-card { border: 1px solid var(--wood); padding: 10px; text-align: center; background: #fff; }
.ability-card label { display: block; font-weight: bold; margin-bottom: 5px; }
.ability-card .score { font-size: 1.5em; font-family: var(--font-header); }
.ability-card .mod { font-size: 0.8em; color: #666; }
.vitals-row { display: flex; gap: 20px; justify-content: center; }
.vital-big { text-align: center; border: 2px solid var(--wood); padding: 15px; min-width: 100px; border-radius: 8px; flex: 1; }
.vital-big .value { font-size: 1.8em; font-weight: bold; font-family: var(--font-header); }
.saves-grid { margin-bottom: 30px; border: 1px solid #ccc; padding: 15px; background: #fff; }
.save-row { display: flex; justify-content: space-between; border-bottom: 1px solid #eee; padding: 8px 0; }
.table-responsive { overflow-x: auto; margin-bottom: 20px; }
.wpn-table { width: 100%; border-collapse: collapse; min-width: 400px; }
.wpn-table th { text-align: left; border-bottom: 2px solid var(--wood); padding: 8px; }
.wpn-table td { padding: 12px 8px; border-bottom: 1px solid #eee; }
.money-pouch { display: flex; gap: 20px; margin-bottom: 20px; font-weight: bold; justify-content: center; }
.detailed-inv { columns: 2; list-style-position: inside; }
.aidm-mobile-close-btn { display: none; width: 100%; padding: 15px; background: var(--wood); color: #fff; text-align: center; font-family: var(--font-header); text-transform: uppercase; margin-top: 20px; border-radius: 4px; cursor: pointer; }
.aidm-modal-content.small-modal { max-width: 500px; height: auto; max-height: 80vh; }
.aidm-modal-title { margin-top: 0; text-align: center; color: var(--blood-red); font-family: var(--font-header); border-bottom: 2px solid var(--wood); padding-bottom: 10px; }
.aidm-sl-tabs { display: flex; gap: 5px; margin-bottom: 15px; border-bottom: 1px solid #ccc; }
.sl-tab { flex: 1; background: #e0d8c0; border: 1px solid #ccc; border-bottom: none; padding: 10px; cursor: pointer; font-family: var(--font-header); font-weight: bold; color: #666; }
.sl-tab.active { background: var(--sheet-paper); color: var(--ink-black); border-top: 3px solid var(--blood-red); }
.sl-pane { display: none; }
.sl-pane.active { display: block; animation: fadeIn 0.3s; }
.aidm-field-group { margin-bottom: 15px; }
.aidm-field-group label { display: block; font-weight: bold; margin-bottom: 5px; }
.sl-hint { font-style: italic; font-size: 0.9em; color: #666; margin-bottom: 15px; }
.aidm-btn.full-width { width: 100%; margin-top: 10px; }
.aidm-btn.danger { border-color: #8a1c1c; color: #8a1c1c; background: transparent; }
.aidm-btn.danger:hover { background: #8a1c1c; color: #fff; }
.save-list-container { height: 300px; overflow-y: auto; border: 1px solid #ccc; background: #fff; margin-bottom: 15px; padding: 5px; }
.save-list-placeholder { padding: 20px; text-align: center; color: #999; font-style: italic; }
.save-item { padding: 10px; border-bottom: 1px solid #eee; cursor: pointer; transition: background 0.2s; }
.save-item:hover { background: #f9f9f9; }
.save-item.active { background: #e8e0cc; border-left: 4px solid var(--blood-red); }
.save-name { font-weight: bold; font-family: var(--font-header); font-size: 1.1em; }
.save-meta { font-size: 0.85em; color: #666; margin-top: 3px; }
.sl-actions { display: flex; gap: 10px; align-items: center; }
.aidm-sidebar-actions { display: flex; gap: 5px; margin-top: 15px; }
.aidm-sidebar-actions button { flex: 1; }

.dice-overlay {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.6);
    z-index: 20000;
    display: none;
    pointer-events: none;
}

.dice-scene {
    position: absolute;
    bottom: 120px;
    left: 50%;
    transform: translateX(-50%);
    width: 100px;
    height: 100px;
    perspective: 400px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.die-cube {
    width: 50px;
    height: 50px;
    position: relative;
    transform-style: preserve-3d;
    transform: rotateX(-30deg) rotateY(-45deg);
    transition: transform 1s;
}

.die-cube.rolling {
    animation: rollDice 2s ease-out forwards;
}

@keyframes rollDice {
    0% { transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg); }
    25% { transform: rotateX(180deg) rotateY(90deg) rotateZ(45deg); }
    50% { transform: rotateX(360deg) rotateY(180deg) rotateZ(90deg); }
    75% { transform: rotateX(540deg) rotateY(270deg) rotateZ(135deg); }
    100% { transform: rotateX(720deg) rotateY(360deg) rotateZ(360deg); }
}

.die-cube .face {
    position: absolute;
    width: 50px;
    height: 50px;
    background: radial-gradient(circle at center, #8a1c1c 0%, #500 100%);
    border: 1px solid #c5a059;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2em;
    color: white;
    box-shadow: inset 0 0 10px rgba(0,0,0,0.8);
    opacity: 0.95;
}

.face.front  { transform: rotateY(0deg) translateZ(25px); }
.face.back   { transform: rotateY(180deg) translateZ(25px); }
.face.right  { transform: rotateY(90deg) translateZ(25px); }
.face.left   { transform: rotateY(-90deg) translateZ(25px); }
.face.top    { transform: rotateX(90deg) translateZ(25px); }
.face.bottom { transform: rotateX(-90deg) translateZ(25px); }

.pip {
    display: block;
    width: 8px;
    height: 8px;
    background-color: #c5a059;
    border-radius: 50%;
    box-shadow: 0 0 2px #000;
}

.face.front { justify-content: center; } 
.face.back { flex-wrap: wrap; align-content: space-around; padding: 8px; gap: 8px; } 
.face.right { transform: rotateY(90deg) translateZ(25px); display: flex; justify-content: space-between; padding: 10px; } 
.face.right .pip:nth-child(2) { align-self: center; }
.face.right .pip:nth-child(3) { align-self: flex-end; }
.face.left { flex-wrap: wrap; padding: 10px; justify-content: space-between; align-content: space-between; } 
.face.top { flex-direction: column; justify-content: space-between; padding: 10px; } 
.face.bottom { justify-content: center; align-items: center; font-family: 'Cinzel', serif; font-size: 0.6em; font-weight: bold; }

.dice-text {
    margin-top: 20px;
    font-family: var(--font-header);
    color: var(--gold);
    text-shadow: 0 2px 4px #000;
    text-align: center;
    font-size: 1.1em;
    animation: pulseText 1s infinite alternate;
}

@keyframes pulseText { from { opacity: 0.7; } to { opacity: 1; } }

.aidm-modal-content.medium-modal {
    max-width: 700px;
    height: auto;
    max-height: 85vh;
}

.aidm-modal-subtitle {
    text-align: center;
    color: #666;
    margin-bottom: 20px;
    font-style: italic;
}

.campaign-list-container {
    height: 400px;
    overflow-y: auto;
    border: 1px solid var(--wood);
    background: #fff;
    padding: 10px;
    margin-top: 15px;
}

.campaign-item {
    background: #fdfbf7;
    border: 1px solid #ddd;
    padding: 15px;
    margin-bottom: 10px;
    border-left: 5px solid var(--wood);
    transition: transform 0.2s;
}
.campaign-item:hover {
    transform: translateX(5px);
    background: #fff;
    box-shadow: 2px 2px 8px rgba(0,0,0,0.1);
}

.camp-title {
    font-family: var(--font-header);
    font-weight: bold;
    font-size: 1.2em;
    color: var(--ink-black);
}

.camp-meta {
    font-size: 0.8em;
    color: #888;
    margin-bottom: 5px;
    text-transform: uppercase;
}

.camp-excerpt {
    font-size: 0.95em;
    color: #444;
    margin-bottom: 10px;
    font-style: italic;
}

.btn-join-camp {
    width: auto;
    float: right;
    background: var(--wood);
    border-color: var(--wood);
}
.btn-join-camp:hover {
    background: var(--blood-red);
}

.lobby-controls {
    text-align: right;
    border-bottom: 1px solid #ccc;
    padding-bottom: 10px;
}