/* Variables & Reset */
:root {
    /* Core Colors */
    --primary-color: #3b82f6; /* Bright Blue */
    --primary-hover: #2563eb;
    --secondary-color: #10b981; /* Green */
    --accent-color: #8b5cf6; /* Purple */
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    
    /* Light Theme Defaults */
    --bg-body: #f8fafc;
    --bg-card: #ffffff;
    --bg-sidebar: #f1f5f9;
    --bg-input: #ffffff;
    --bg-hover: #e2e8f0;
    --bg-active: #dbeafe;
    
    --bg-message: #ffffff;
    --bg-message-hover: #f1f5f9;

    --border-color: #e2e8f0;
    --border-focus: #3b82f6;
    
    --text-main: #0f172a;
    --text-muted: #64748b;
    --text-light: #94a3b8;
    
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
    
    --radius-sm: 0.375rem;
    --radius-md: 0.5rem;
    --radius-lg: 0.75rem;

    --font-sans: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

/* Dark Theme Overrides */
body:not(.theme-light) { /* Default to Dark if no class or explicitly dark */
    --bg-body: #0f172a; /* Slate 900 */
    --bg-card: #1e293b; /* Slate 800 */
    --bg-sidebar: #1e293b;
    --bg-input: #0f172a;
    --bg-hover: #334155;
    --bg-active: #1e40af; /* Blue 800 */
    
    --bg-message: rgba(30, 41, 59, 0.4);
    --bg-message-hover: rgba(30, 41, 59, 0.7);

    --border-color: #334155;
    --border-focus: #60a5fa;
    
    --text-main: #f1f5f9;
    --text-muted: #94a3b8;
    --text-light: #64748b;

    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.3);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.3);
}

/* Explicit Dark Theme Class */
body.theme-dark {
    /* Same as body:not(.theme-light) */
    --bg-body: #0f172a;
    --bg-card: #1e293b;
    --bg-sidebar: #1e293b;
    --bg-input: #0f172a;
    --bg-hover: #334155;
    --bg-active: #1e40af;
    --border-color: #334155;
    --border-focus: #60a5fa;
    --text-main: #f1f5f9;
    --text-muted: #94a3b8;
    --text-light: #64748b;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.3);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.3);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    min-height: 100%;
    width: 100%;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-body);
    color: var(--text-main);
    line-height: 1.5;
    font-size: 15px;
    -webkit-font-smoothing: antialiased;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* Layout Containers */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
}

.container.center {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    width: 100%;
    max-width: none;
    background: radial-gradient(ellipse at top right, #1e293b 0%, var(--bg-body) 50%);
}

body.theme-light .container.center {
    background: radial-gradient(circle at top right, #e2e8f0 0%, var(--bg-body) 60%);
}

/* Cards (Welcome, Settings, etc) */
.welcome-box {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 2.5rem;
    box-shadow: var(--shadow-lg);
    max-width: 480px;
    width: 100%;
}

.welcome-box h1 {
    color: var(--text-main);
    text-align: center;
    margin-bottom: 0.5rem;
    font-size: 1.875rem;
    font-weight: 700;
    letter-spacing: -0.025em;
}

.subtitle {
    text-align: center;
    color: var(--text-muted);
    margin-bottom: 1.5rem;
    font-size: 1rem;
}

.security-notice {
    text-align: center;
    color: var(--secondary-color);
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 2rem;
    padding: 0.75rem;
    background: rgba(16, 185, 129, 0.1);
    border-radius: var(--radius-md);
    border: 1px solid rgba(16, 185, 129, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

/* Forms */
.form-group {
    margin-bottom: 1.25rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    color: var(--text-main);
    font-weight: 500;
    font-size: 0.9rem;
}

input[type="text"],
input[type="password"] {
    width: 100%;
    padding: 0.75rem 1rem;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    font-family: inherit;
    font-size: 1rem;
    border-radius: var(--radius-md);
    transition: all 0.2s;
}

input[type="text"]:focus,
input[type="password"]:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

select {
    width: 100%;
    padding: 0.75rem 1rem;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    font-family: inherit;
    font-size: 1rem;
    border-radius: var(--radius-md);
    transition: all 0.2s;
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2394a3b8' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 1rem center;
    padding-right: 2.5rem;
}

select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

select option {
    background: var(--bg-card);
    color: var(--text-main);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.75rem 1.5rem;
    font-weight: 600;
    font-size: 1rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
    border: none;
    text-decoration: none;
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
    width: 100%;
}

.btn-primary:hover {
    background-color: var(--primary-hover);
    transform: translateY(-1px);
}

.btn-secondary {
    background-color: var(--bg-hover);
    color: var(--text-main);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background-color: var(--border-color);
}

.btn-send {
    background-color: var(--primary-color);
    color: white;
    padding: 0 1.25rem;
}
.btn-send:hover {
    background-color: var(--primary-hover);
}

/* Helper sections */
.last-channel {
    margin-top: 1.5rem;
    text-align: center;
    padding: 1.5rem;
    background: var(--bg-sidebar);
    border-radius: var(--radius-md);
}

.last-channel p {
    color: var(--text-muted);
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

.last-channel strong {
    color: var(--text-main);
}

.info {
    margin-top: 2rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border-color);
}

.info h2, .info h3 {
    color: var(--text-main);
    font-size: 1.1rem;
    margin-bottom: 1rem;
    font-weight: 600;
}

.info ul {
    list-style: none;
}

.info li {
    margin-bottom: 0.75rem;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.info code {
    font-family: var(--font-mono);
    background: var(--bg-sidebar);
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
    color: var(--primary-color);
    font-size: 0.85em;
    border: 1px solid var(--border-color);
}

/* Chat Interface - Layout */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--bg-body);
}

.iframe-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
}

.messages-iframe {
    flex: 1;
    width: 100%;
    border: none;
    margin: 0;
    padding: 0;
    background: var(--bg-body);
}

/* Messages Iframe Layout */
body.messages-frame {
    height: 100vh;
    margin: 0;
    overflow: hidden;
}

.messages-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--bg-body);
}

/* Topbar */
.topbar {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border-color);
    padding: 0.75rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    height: 64px;
}

.channel-info {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.channel-name {
    color: var(--text-main);
    font-weight: 700;
    font-size: 1.25rem;
}

.user-count {
    color: var(--text-muted);
    font-size: 0.9rem;
    background: var(--bg-hover);
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
}

.status-info {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.status {
    padding: 0.35rem 0.75rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.status-connected {
    background: rgba(16, 185, 129, 0.15);
    color: var(--secondary-color);
}

.status-reconnecting {
    background: rgba(245, 158, 11, 0.15);
    color: var(--warning-color);
}

.status-failed, .status-disconnected {
    background: rgba(239, 68, 68, 0.15);
    color: var(--danger-color);
}

.nick {
    font-weight: 600;
    color: var(--text-main);
}

/* Main Content (Sidebar + Chat) */
.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 240px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    padding: 1.25rem;
    overflow-y: auto;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}

.sidebar h3 {
    color: var(--text-muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 1rem;
    font-weight: 700;
}

.channel-list, .user-list {
    list-style: none;
    margin-bottom: 1.5rem;
}

.channel-item {
    margin-bottom: 2px;
}

.channel-item a {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem 0.75rem;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: var(--radius-md);
    font-size: 0.95rem;
    transition: all 0.15s;
}

.channel-item a:hover {
    background: var(--bg-hover);
    color: var(--text-main);
}

.channel-item.active a {
    background: var(--bg-active);
    color: var(--primary-color);
    font-weight: 600;
}

.dm-icon {
    color: var(--accent-color);
}

.unread-badge {
    background: var(--danger-color);
    color: white;
    padding: 0.15rem 0.4rem;
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 700;
    margin-left: auto;
}

/* Compact Popular Channels */
.popular-channels {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.popular-channels .channel-item {
    margin-bottom: 0;
    flex: 0 1 auto;
}

.popular-channels .channel-item a {
    padding: 4px 8px;
    font-size: 0.8rem;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    justify-content: center;
}

.popular-channels .channel-item a:hover {
    background: var(--bg-hover);
    border-color: var(--primary-color);
    color: var(--primary-color);
    text-decoration: none;
}

/* Sidebar Footer / User List */
.sidebar-footer {
    margin-top: 1.5rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border-color);
}

.sidebar-footer details {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.sidebar-footer summary {
    cursor: pointer;
    padding: 0.5rem;
    border-radius: var(--radius-md);
    font-weight: 600;
    color: var(--text-main);
    user-select: none;
    transition: background-color 0.2s;
}

.sidebar-footer summary:hover {
    background: var(--bg-hover);
}

/* User toggle link */
.user-toggle {
    display: block;
    padding: 0.5rem 0.75rem;
    border-radius: var(--radius-md);
    font-weight: 500;
    font-size: 0.9rem;
    color: var(--text-muted);
    text-decoration: none;
    transition: background-color 0.2s, color 0.2s;
}

.user-toggle:hover {
    background: var(--bg-hover);
    color: var(--text-main);
}

.user-toggle .icon {
    display: inline-block;
    width: 1rem;
    font-size: 0.7rem;
    color: var(--primary-color);
}

.user-list-compact {
    list-style: none;
    margin-top: 0.5rem;
    max-height: 200px;
    overflow-y: auto;
    padding-left: 0.5rem;
}

.user-list-compact li {
    padding: 0.25rem 0.5rem;
    color: var(--text-muted);
    font-size: 0.85rem;
    border-radius: var(--radius-sm);
}

.user-list-compact li:hover {
    background: var(--bg-hover);
    color: var(--text-main);
}

.user-list-compact .current-user {
    color: var(--primary-color);
    font-weight: 600;
}

.settings-link {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
}

.settings-link a {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    transition: all 0.2s;
}

.settings-link a:hover {
    background: var(--bg-hover);
    color: var(--text-main);
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-body);
    position: relative;
}

.messages {
    flex: 1;
    overflow-y: scroll;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
}

/* Wrapper pushes messages to bottom when content is short */
.messages-wrapper {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-top: auto; /* Pushes content to bottom */
}

#bottom {
    height: 1px;
}

.message {
    padding: 0.5rem 1rem;
    margin-bottom: 0.5rem;
    border-radius: var(--radius-md);
    line-height: 1.5;
    word-wrap: break-word;
    display: flex;
    gap: 0.75rem;
    align-items: baseline;
    background: var(--bg-message);
    border: 1px solid var(--border-color);
    transition: all 0.2s;
}

.message:hover {
    background: var(--bg-message-hover);
    box-shadow: var(--shadow-sm);
}

.timestamp {
    color: var(--text-light);
    font-size: 0.75rem;
    font-family: var(--font-mono);
    min-width: 45px;
    flex-shrink: 0;
    opacity: 0.7;
}

.message:hover .timestamp {
    opacity: 1;
}

.message .nick {
    color: var(--text-main);
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}

.message .text {
    color: var(--text-muted);
}

/* Message Types */
.msg-privmsg .nick { color: var(--primary-color); }
.msg-action .action { color: var(--accent-color); font-style: italic; }
.msg-join .event, .msg-part .event, .msg-quit .event { 
    color: var(--text-light); 
    font-size: 0.9rem; 
    font-style: italic; 
    padding: 0.25rem 1rem;
    background: transparent;
    border: none;
}
.msg-notice .notice { color: var(--warning-color); }
.msg-system .system { color: var(--text-light); font-style: italic; }

/* Channel Welcome / Empty State */
.channel-welcome {
    padding: 2rem;
    margin-bottom: 1rem;
    text-align: center;
    color: var(--text-muted);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1; /* Take up available space if empty */
}

.welcome-icon {
    width: 64px;
    height: 64px;
    background: var(--bg-hover);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    font-weight: bold;
    color: var(--text-main);
    margin-bottom: 1rem;
}

.channel-welcome h2 {
    color: var(--text-main);
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
}

.channel-welcome .highlight {
    color: var(--primary-color);
    font-weight: 600;
}

.channel-welcome .sub-text {
    font-size: 0.9rem;
    color: var(--text-light);
}

/* Input Area */
.input-area-fixed, .input-area {
    background: var(--bg-card);
    border-top: 1px solid var(--border-color);
    padding: 1rem 1.5rem;
    flex-shrink: 0;
}

.input-area-fixed form, .input-area form {
    display: flex;
    gap: 1rem;
    width: 100%;
}

.input-area-fixed input[type="text"], .input-area input[type="text"] {
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.75rem 1rem;
    font-family: var(--font-sans);
    color: var(--text-main);
}

/* Settings Panel (Overlay) */
.settings-panel {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    background: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(4px);
    align-items: center;
    justify-content: center;
}

.settings-panel:target {
    display: flex;
}

.settings-content {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    padding: 2rem;
}

.settings-content h2 {
    color: var(--text-main);
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
}

.setting-item {
    background: var(--bg-sidebar);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1rem;
    margin-bottom: 1rem;
}

/* Scrollbars */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-light);
}

/* Status Message (Connecting/Error) */
.status-message {
    background: var(--bg-sidebar);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1.5rem;
    margin: 1.5rem 0;
    text-align: center;
}

.status-message h2 {
    color: var(--primary-color);
    font-size: 1.25rem;
    margin-bottom: 0.75rem;
}

.status-message p {
    color: var(--text-main);
}

.status-message.error {
    background: rgba(239, 68, 68, 0.05);
    border-color: rgba(239, 68, 68, 0.2);
}

.status-message.error h2 {
    color: var(--danger-color);
}

.status-detail {
    color: var(--text-light) !important;
    font-size: 0.85rem;
    margin-top: 0.5rem;
}

.progress-indicator {
    background: var(--bg-sidebar);
    padding: 1rem;
    border-radius: var(--radius-md);
    margin: 1.5rem 0;
    font-family: var(--font-mono);
    font-size: 0.9rem;
    color: var(--text-muted);
    border: 1px solid var(--border-color);
    text-align: center;
}

/* Error Page */
.error-message {
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.2);
    border-radius: var(--radius-md);
    padding: 1.5rem;
    margin: 1.5rem 0;
}

.error-message h2 {
    color: var(--danger-color);
    font-size: 1.25rem;
    margin-bottom: 0.5rem;
}

.error-message p {
    color: var(--text-muted);
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar {
        width: 60px;
        padding: 0.75rem;
    }
    .sidebar h3, .sidebar-footer summary span {
        display: none;
    }
    .channel-item a span {
        display: none;
    }
    .channel-item a {
        justify-content: center;
        padding: 0.5rem;
    }
    .channel-name-text {
        display: none;
    }
    .channel-item.active a {
        border-left: none;
    }
    .unread-badge {
        position: absolute;
        top: 0;
        right: 0;
    }
    .sidebar-footer summary {
        font-size: 0;
    }
    .sidebar-footer summary::after {
        content: "👥";
        font-size: 1rem;
    }
}
