:root {
    --bg-image: url('../images/bg-light.jpg');
    --bg-body: rgba(240, 242, 245, 0.4);
    --bg-panel: rgba(255, 255, 255, 0.85);
    /* Slightly translucent to show map background */
    --bg-hover: rgba(238, 242, 247, 0.9);
    --bg-input: rgba(255, 255, 255, 0.9);
    --text-main: #333333;
    --text-muted: #7f8c8d;
    --text-heading: #2c3e50;
    --border-color: #eee;
    --accent-primary: #3498db;
    --accent-hover: #2980b9;
    --border-radius-main: 12px;
    --font-primary: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    --font-heading: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

[data-theme="dungeon"] {
    --bg-image: url('../images/bg-dungeon.jpg');
    --bg-body: #0a0a0c;
    --bg-panel: #1a1a1d;
    --bg-hover: #252529;
    --bg-input: #121214;
    --text-main: #d4c4a8;
    --text-muted: #8b8b93;
    --text-heading: #d4af37;
    --border-color: #3f3f46;
    --accent-primary: #8a1c1c;
    --accent-hover: #a82222;
    --border-radius-main: 2px;
    --font-primary: 'Merriweather', serif;
    --font-heading: 'Cinzel', serif;
}

[data-theme="desert"] {
    --bg-image: url('../images/bg-desert.jpg');
    --bg-body: #1c140d;
    --bg-panel: #291e14;
    --bg-hover: #382a1d;
    --bg-input: #140e09;
    --text-main: #dcbfa3;
    --text-muted: #a3886f;
    --text-heading: #f2c46d;
    --border-color: #523a24;
    --accent-primary: #d96c21;
    --accent-hover: #f2853a;
    --border-radius-main: 0px;
    --font-primary: 'Merriweather', serif;
    --font-heading: 'Cinzel', serif;
}

[data-theme="chimera"] {
    --bg-image: url('../images/bg-chimera.jpg');
    --bg-body: rgba(5, 7, 10, 0.6);
    --bg-panel: rgba(18, 22, 30, 0.75);
    /* Slightly translucent to show background */
    --bg-hover: #1c2533;
    --bg-input: rgba(10, 14, 20, 0.85);
    --text-main: #d1d9e1;
    --text-muted: #8b9bb0;
    --text-heading: #7adcfb;
    /* Lightning Blue */
    --border-color: #2d3a4f;
    --accent-primary: #9b59b6;
    /* Arcane Purple */
    --accent-hover: #7adcfb;
    /* Lightning Blue */
    --border-radius-main: 6px;
    --font-primary: 'Merriweather', serif;
    --font-heading: 'Cinzel', serif;
}

[data-theme="chimera"] button {
    border: 1px solid #eec373;
    /* Runic Gold */
    box-shadow: 0 0 5px rgba(238, 195, 115, 0.3);
}

[data-theme="chimera"] button.icon-btn {
    border: none;
    box-shadow: none;
}

[data-theme="chimera"] .tag-locked {
    color: #ff6b6b;
}

[data-theme="chimera"] .tag-exit {
    color: #7adcfb;
}

body {
    font-family: var(--font-primary);
    background-color: var(--bg-body);
    background-image: var(--bg-image);
    background-size: cover;
    background-attachment: fixed;
    background-position: center;
    background-blend-mode: multiply;
    transition: background-color 0.3s, background-image 0.3s, color 0.3s;
    color: var(--text-main);
    margin: 0;
    padding: 20px;
}

/* Menu View Styles */
#menu-view {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-height: 90vh;
}

.auth-container {
    margin-bottom: 20px;
    background: var(--bg-panel);
    padding: 15px 30px;
    border-radius: var(--border-radius-main);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    gap: 15px;
}

#user-email {
    font-weight: 600;
    color: var(--text-heading);
    margin-right: 10px;
}

.menu-container {
    background: var(--bg-panel);
    padding: 40px;
    border-radius: var(--border-radius-main);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    width: 400px;
    text-align: center;
}

.menu-container h1 {
    color: var(--text-heading);
    margin-bottom: 20px;
    font-size: 2.5em;
    letter-spacing: 2px;
}

.flavor-text {
    color: var(--text-muted);
    font-size: 0.95em;
    line-height: 1.6;
    margin-bottom: 30px;
    text-align: left;
    font-style: italic;
}

.form-group {
    margin-bottom: 30px;
    text-align: left;
}

.form-group label {
    display: block;
    margin-bottom: 5px;
    font-size: 0.9em;
    font-weight: 600;
    color: var(--text-heading);
}

.form-group h3 {
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 10px;
    margin-bottom: 15px;
    color: var(--text-muted);
}

.character-select-row {
    display: flex;
    gap: 10px;
    margin-bottom: 25px;
}
.character-select-row select {
    margin-bottom: 0;
    flex-grow: 1;
}
.character-select-row button {
    white-space: nowrap;
}

input[type="text"],
select,
textarea {
    width: 100%;
    padding: 12px;
    margin-bottom: 10px;
    border: 1px solid var(--border-color);
    border-radius: calc(var(--border-radius-main) / 2);
    box-sizing: border-box;
    background-color: var(--bg-input);
    color: var(--text-main);
}

textarea {
    resize: none;
    height: 80px;
    overflow-y: auto;
    font-family: inherit;
    line-height: 1.4;
}

.load-item {
    padding: 10px;
    background: var(--bg-body);
    border: 1px solid var(--border-color);
    border-radius: calc(var(--border-radius-main) / 2);
    margin-bottom: 5px;
    cursor: pointer;
    transition: background 0.2s;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.load-item:hover {
    background: var(--bg-hover);
}

.delete-game-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s;
}

.delete-game-btn:hover {
    color: #e74c3c;
    background: transparent;
}

/* Game View Styles */
#game-view {
    display: none;
}

.main-container {
    display: flex;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: flex-start;
    gap: 20px;
    max-width: 1700px;
    margin: 0 auto;
}

.sidebar {
    background: var(--bg-panel);
    width: 280px;
    padding: 20px;
    border-radius: var(--border-radius-main);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    position: sticky;
    top: 20px;
    text-align: left;
    flex-shrink: 0;
    max-height: 90vh;
    overflow-y: auto;
    transition: background-color 0.3s, color 0.3s;
}

h1,
h2,
h3,
.section-header,
#room-name {
    font-family: var(--font-heading);
}

.sidebar h2 {
    margin-top: 10px;
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 10px;
    font-size: 1.5em;
    color: var(--text-heading);
    text-align: center;
}

.player-portrait {
    width: 100%;
    height: auto;
    border-radius: calc(var(--border-radius-main) / 1.5);
    border: 1px solid var(--border-color);
    display: block;
    margin-bottom: 15px;
}

.stat-block {
    margin-bottom: 15px;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 5px;
    font-size: 0.95em;
}

.stat-label {
    font-weight: 600;
    color: var(--text-muted);
    position: relative;
    cursor: help;
}

.section-header {
    font-size: 1.1em;
    font-weight: bold;
    color: var(--text-heading);
    margin-top: 20px;
    margin-bottom: 10px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 5px;
    position: relative;
    cursor: help;
}

.tools-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.tools-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 5px;
    font-size: 0.95em;
    padding: 8px;
    background: var(--bg-body);
    border-radius: calc(var(--border-radius-main) / 3);
    cursor: help;
    position: relative;
    transition: background 0.2s;
}

.tools-list li:hover {
    background: var(--bg-hover);
}

.tools-list li[data-tooltip]:hover::after,
.section-header[data-tooltip]:hover::after,
.stat-label[data-tooltip]:hover::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--text-main);
    color: var(--bg-panel);
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 0.85em;
    white-space: normal;
    width: 200px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    z-index: 1000;
    pointer-events: none;
    text-align: center;
}

.tools-list li[data-tooltip]:hover::before,
.section-header[data-tooltip]:hover::before,
.stat-label[data-tooltip]:hover::before {
    content: '';
    position: absolute;
    bottom: 100%;
    left: 50%;
    margin-bottom: -5px;
    transform: translateX(-50%);
    border-width: 5px;
    border-style: solid;
    border-color: var(--text-main) transparent transparent transparent;
    z-index: 1000;
}

.tool-name {
    font-weight: 600;
    color: var(--text-heading);
}

.book-page {
    background: var(--bg-panel);
    width: 100%;
    max-width: 1000px;
    padding: 0;
    padding-bottom: 40px;
    border-radius: var(--border-radius-main);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    flex-grow: 1;
    flex-shrink: 1;
    transition: background-color 0.3s, color 0.3s;
}

.book-content {
    padding: 0 40px;
}

#story-image {
    width: 100%;
    border-radius: var(--border-radius-main) var(--border-radius-main) 0 0;
    object-fit: contain;
    background-color: var(--bg-input);
    max-height: 60vh;
    margin-bottom: 25px;
    display: block;
}

/* Action Toolbar Styles */
.action-toolbar {
    display: flex;
    justify-content: flex-end;
    gap: 15px;
    margin-bottom: 10px;
    margin-top: -15px; /* Pull it slightly closer to the image */
}

.action-toolbar .icon-btn {
    border: 1px solid var(--border-color);
    background-color: var(--bg-input);
    padding: 8px;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    box-sizing: border-box;
    flex-shrink: 0;
    transition: background-color 0.2s, color 0.2s, stroke 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.action-toolbar .btn-regen {
    color: var(--accent-primary);
}

.action-toolbar .btn-regen:hover {
    background-color: var(--accent-primary);
    color: var(--bg-panel);
}

.action-toolbar .btn-undo {
    color: #e74c3c;
}

.action-toolbar .btn-undo:hover {
    background-color: #e74c3c;
    color: var(--bg-panel);
}

.narrative-collapse summary {
    cursor: pointer;
    color: var(--text-muted);
    font-family: var(--font-heading);
    font-size: 0.9em;
    font-weight: bold;
    user-select: none;
    margin-bottom: 10px;
}

.narrative {
    font-size: 18px;
    margin: 10px 0 25px 0;
    line-height: 1.6;
    text-align: left;
}

.input-area {
    margin-top: 30px;
    display: flex;
    gap: 10px;
}

#game-input {
    flex-grow: 1;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: calc(var(--border-radius-main) / 2);
    font-size: 16px;
    background-color: var(--bg-input);
    color: var(--text-main);
}

.controls {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    gap: 5px;
    align-items: center;
}

button {
    padding: 10px 16px;
    font-size: 14px;
    cursor: pointer;
    background-color: var(--accent-primary);
    color: white;
    border: 1px solid var(--border-color);
    border-radius: calc(var(--border-radius-main) / 2);
    transition: background-color 0.2s, border-color 0.2s;
}

button:hover {
    background-color: var(--accent-hover);
}

button:disabled {
    background-color: #bdc3c7;
    cursor: not-allowed;
}

.system-output {
    margin-top: 20px;
    padding: 15px;
    background-color: var(--bg-input);
    color: var(--text-main);
    border-left: 4px solid #b58900;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.9em;
}

#page-num {
    margin: 0 10px;
    font-weight: bold;
    color: var(--text-muted);
}

.meta-info {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 25px;
    border-top: 1px solid var(--border-color);
    padding-top: 15px;
    text-align: center;
}

.npc-item {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
    padding: 8px;
    background: var(--bg-body);
    border-radius: calc(var(--border-radius-main) / 1.5);
    border: 1px solid var(--border-color);
}

.npc-portrait-wrapper {
    position: relative;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
    background: var(--bg-hover);
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border-color);
}

.npc-portrait {
    width: 100%;
    height: 100%;
    object-fit: cover;
    z-index: 2;
    position: absolute;
    top: 0;
    left: 0;
}

.npc-initials {
    font-size: 1.1em;
    font-weight: bold;
    color: var(--text-muted);
    z-index: 1;
    text-transform: uppercase;
}

.npc-name {
    font-weight: 600;
    color: var(--text-heading);
    font-size: 0.95em;
}

@media (max-width: 1650px) {
    .main-container {
        flex-direction: row;
        justify-content: flex-start !important;
        overflow-x: auto;
        scroll-snap-type: x mandatory;
        scroll-behavior: smooth;
        gap: 0;
        scrollbar-width: none;
    }
    
    .main-container::-webkit-scrollbar { 
        display: none; 
    }

    .sidebar,
    #right-sidebar,
    .book-page {
        flex: 0 0 100vw;
        max-width: 100vw;
        scroll-snap-align: start;
        height: 100vh;
        overflow-y: auto;
        box-sizing: border-box;
    }
}

@media (max-width: 1024px) {
    body {
        padding: 0;
    }
    .menu-container {
        width: 95%;
        padding: 20px;
        box-sizing: border-box;
    }
    #game-input, input[type="text"], select, textarea, button {
        font-size: 16px;
    }
    #game-input, #btn-submit {
        padding: 15px;
    }
    .action-toolbar {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        align-items: center;
        gap: 10px;
        margin: 10px 0;
    }
    .custom-audio-player {
        border: none;
        background: transparent;
        padding: 0;
        margin: 0;
        flex-grow: 1;
    }
    #story-image {
        width: 100%;
        max-height: 50vh;
        border-radius: 0;
        object-fit: contain !important;
    }
}

/* Tag Styles */
.tag-locked {
    color: #e74c3c;
    /* Red */
    font-size: 0.85em;
    margin-left: 5px;
}

.tag-cover {
    color: #7f8c8d;
    /* Slate Grey */
    font-size: 0.85em;
    margin-left: 5px;
}

.tag-state {
    color: #e67e22;
    /* Orange */
    font-size: 0.85em;
    margin-left: 5px;
}

.tag-exit {
    color: #27ae60;
    /* Green */
    font-size: 0.85em;
    margin-left: 5px;
}

.tag-available {
    color: #27ae60;
    /* Green */
    font-size: 0.85em;
    margin-left: 5px;
}

.tag-inaccessible {
    color: #e67e22;
    /* Orange */
    font-size: 0.85em;
    margin-left: 5px;
}

.tag-container {
    color: var(--accent-primary);
    /* Blue */
    font-size: 0.85em;
    margin-left: 5px;
}

/* New classes for semantic JS rendering */
.text-accent {
    color: var(--accent-primary);
    font-family: monospace;
}

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

.npc-stats {
    font-size: 0.8em;
    color: var(--text-muted);
}

.tool-value {
    color: var(--accent-hover);
    font-weight: bold;
}

.empty-state {
    color: var(--text-muted);
    font-style: italic;
}

/* Restoring visual properties for elements with removed inline styles */
#room-name {
    font-weight: bold;
    color: var(--text-heading);
    font-size: 1.1em;
    margin-bottom: 5px;
}

#room-desc {
    font-size: 0.9em;
    color: var(--text-main);
    line-height: 1.4;
    white-space: pre-wrap;
}

#sys-details summary {
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.9em;
    user-select: none;
}

#sys-details {
    margin-top: 20px;
}

.top-bar {
    display: flex;
    justify-content: flex-end;
    gap: 15px;
    max-width: 1700px;
    margin: 0 auto 10px auto;
    padding: 0 20px;
}

.icon-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    transition: color 0.2s;
}

.icon-btn:hover {
    color: var(--text-heading);
    background: transparent;
}

.theme-selector {
    margin-top: 15px;
    font-family: var(--font-primary);
    width: 100%;
    padding: 8px;
    border-radius: calc(var(--border-radius-main) / 2);
    border: 1px solid var(--border-color);
    background-color: var(--bg-input);
    color: var(--text-main);
}

.custom-audio-player {
    display: flex;
    align-items: center;
    gap: 15px;
    background-color: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-main);
    padding: 0 15px;
    height: 40px;
    box-sizing: border-box;
    margin: 0;
    flex-grow: 1;
}

#audio-play-btn {
    min-width: 70px;
    padding: 6px 10px;
    font-size: 13px;
    background-color: var(--accent-primary);
}

#audio-progress {
    flex-grow: 1;
    cursor: pointer;
    accent-color: var(--accent-primary);
}

#audio-time {
    font-family: monospace;
    font-size: 0.85em;
    color: var(--text-muted);
    min-width: 85px;
    text-align: right;
}

.audio-toggle-label {
    display: flex;
    align-items: center;
    gap: 5px;
    color: var(--text-main);
    font-size: 0.85em;
    cursor: pointer;
}

#audio-autoplay-toggle {
    margin: 0;
    cursor: pointer;
}

#p-equipment {
    padding-left: 20px;
    font-size: 0.9em;
    margin-top: 5px;
}

.npc-container {
    margin-bottom: 20px;
}

/* Modal Styles */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 2000;
    justify-content: center;
    align-items: center;
}

.modal-content {
    background-color: var(--bg-panel);
    color: var(--text-main);
    padding: 30px;
    border-radius: var(--border-radius-main);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    width: 90%;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-content h2 {
    margin-top: 0;
    margin-bottom: 20px;
    text-align: center;
}

.flag-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    gap: 15px;
}

.flag-row label {
    font-size: 0.9em;
    font-weight: 600;
    color: var(--text-muted);
    word-break: break-all;
    flex: 1;
}

.flag-input {
    width: 200px;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: calc(var(--border-radius-main) / 2);
    background-color: var(--bg-input);
    color: var(--text-main);
    font-family: inherit;
    font-size: 0.9em;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 30px;
    border-top: 1px solid var(--border-color);
    padding-top: 20px;
}

/* Spinner Styles */
.loading-overlay {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 30px;
    text-align: center;
}

.spinner {
    border: 4px solid var(--border-color);
    border-top: 4px solid var(--accent-primary);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 1s linear infinite;
    margin-bottom: 20px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}