:root {
    --bg-color: #0d1117;
    --text-color: #c9d1d9;
    --accent-color: #58a6ff;
    --secondary-bg: #161b22;
    --border-color: #30363d;
    --success-color: #2ea043;
    --error-color: #da3633;
    --font-family: 'Inter', system-ui, -apple-system, sans-serif;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-family);
    margin: 0;
    padding: 0;
    line-height: 1.6;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 1rem;
    box-sizing: border-box;
    width: 100%;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 1rem;
    flex-shrink: 0;
    /* Header shouldn't shrink */
}

h1 {
    font-size: 1.5rem;
    margin: 0;
    background: linear-gradient(45deg, var(--accent-color), #bc8cff);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

#connection-status {
    font-size: 0.875rem;
    color: var(--text-color);
    opacity: 0.7;
}

#connection-status.connected {
    color: var(--success-color);
    opacity: 1;
}

#connection-status.disconnected {
    color: var(--error-color);
    opacity: 1;
}

main {
    display: flex;
    flex: 1;
    gap: 0.75rem;
    overflow: hidden;
    min-height: 0;
    /* Crucial for nested flex scrolling */
}

#game-log {
    flex: 3;
    background-color: var(--secondary-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    font-family: 'JetBrains Mono', monospace;
}

.log-entry {
    padding: 0.5rem 0.5rem 0.5rem 0.75rem;
    margin: 0.25rem 0;
    border-left: 3px solid var(--accent-color);
    background: rgba(88, 166, 255, 0.05);
    animation: fadeIn 0.3s ease-in;
    word-wrap: break-word;
    /* Prevent overflow */
}

.log-entry.user {
    border-left-color: #6b8e23;
    background: rgba(107, 142, 35, 0.05);
}

.log-entry.system {
    border-left-color: #4a90e2;
    background: rgba(74, 144, 226, 0.05);
}

.log-entry.narrative,
.log-entry.room {
    border-left-color: #20b2aa;
    background: rgba(32, 178, 170, 0.05);
}

.log-entry.gm-message {
    border-left-color: #9b59b6;
    background: rgba(155, 89, 182, 0.05);
}

.log-entry.dm-message {
    border-left-color: #27ae60;
    background: rgba(39, 174, 96, 0.05);
}

.log-entry.error-message {
    border-left-color: #e74c3c;
    background: rgba(231, 76, 60, 0.05);
}

.log-entry.system-message {
    border-left-color: #95a5a6;
    background: rgba(149, 165, 166, 0.05);
}

#sidebar {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    min-width: 250px;
    overflow-y: auto;
    /* Allow sidebar scrolling */
}

.panel {
    background-color: var(--secondary-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.panel h2 {
    font-size: 1rem;
    margin: 0 0 0.5rem 0;
    color: var(--accent-color);
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.25rem;
}

#stats-list,
#inventory-list,
#objectives-list {
    list-style: none;
    padding: 0;
    margin: 0;
    font-size: 0.9rem;
}

#stats-list li,
#inventory-list li,
#objectives-list li {
    display: flex;
    justify-content: space-between;
    padding: 0.25rem 0;
    border-bottom: 1px solid rgba(48, 54, 61, 0.5);
}

/* Objectives Styling */
#objectives-list li.objective {
    flex-direction: column;
    gap: 0.125rem;
}

#objectives-list li.empty {
    color: var(--text-color);
    opacity: 0.5;
    font-style: italic;
}

.objective-name {
    color: #f0c674;
    font-weight: 500;
}

.objective-deadline {
    font-size: 0.8rem;
    color: var(--error-color);
    opacity: 0.8;
}

#input-area {
    margin-top: 1rem;
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
    /* Input shouldn't shrink */
}

#command-input {
    flex: 1;
    background-color: var(--secondary-bg);
    border: 1px solid var(--border-color);
    color: var(--text-color);
    padding: 0.75rem;
    border-radius: 6px;
    font-family: inherit;
    font-size: 1rem;
    transition: border-color 0.2s;
}

#command-input:focus {
    outline: none;
    border-color: var(--accent-color);
}

button {
    background-color: var(--accent-color);
    color: #fff;
    border: none;
    padding: 0 1.5rem;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
}

button:hover {
    opacity: 0.9;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-color);
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #484f58;
}

/* Character Info Panel */
#character-info {
    padding: 0.5rem 0;
}

#character-name {
    font-weight: 600;
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
    color: var(--accent-color);
}

#character-description {
    font-size: 0.9rem;
    color: var(--text-color);
    opacity: 0.8;
    line-height: 1.4;
    font-style: italic;
}

/* Mobile Responsiveness */
@media (max-width: 850px) {
    #app {
        padding: 0.5rem;
    }

    header {
        margin-bottom: 0.5rem;
        padding-bottom: 0.5rem;
    }

    h1 {
        font-size: 1.25rem;
    }

    main {
        flex-direction: column;
        /* Stack vertically */
    }

    #game-log {
        flex: 1.5;
        /* Reduce height relative to sidebar */
        min-height: 150px;
    }

    #sidebar {
        flex: 1;
        height: auto;
        max-height: none;
        min-width: 100%;
        display: flex;
        flex-direction: column;
        /* Vertical stack */
        overflow-x: hidden;
        padding-bottom: 0.5rem;
        gap: 0.5rem;
    }

    .panel {
        min-width: 0;
        height: auto;
        max-height: none;
        /* Remove max-height for scrolling text */
        overflow-y: visible;
        /* Let it expand */
    }

    /* Collapsible Logic */
    .panel h2 {
        cursor: pointer;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .panel h2::after {
        content: '▼';
        font-size: 0.8em;
        transition: transform 0.2s;
    }

    .panel.collapsed h2::after {
        transform: rotate(-90deg);
    }

    .panel.collapsed>*:not(h2) {
        display: none;
    }

    #input-area {
        margin-top: 0.5rem;
        /* Keep as row by default */
    }

    #command-input {
        padding: 0.8rem;
        /* Larger touch target */
    }

    button {
        padding: 0.8rem;
    }
}

/* Very small screens: stack input and button */
@media (max-width: 300px) {
    #input-area {
        flex-direction: column;
    }

    button {
        width: 100%;
    }
}