/* --- Design Tokens --- */
:root {
    /* Dark Theme (Default) */
    --bg-main: #09090b;
    --bg-secondary: #18181b;
    --glass-bg: rgba(24, 24, 27, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
    --text-primary: #f4f4f5;
    --text-secondary: #a1a1aa;
    --accent-primary: #8b5cf6; /* Purple */
    --accent-secondary: #38bdf8; /* Light Blue */
    --accent-gradient: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    --system-message-bg: rgba(255, 255, 255, 0.05);
    --highlight-bg: var(--accent-primary);
    --highlight-text: #ffffff;
    --chat-bg: var(--bg-main);
    --ui-scale: 1.0;
    --chat-font-scale: 1.0;
}

body.light-mode {
    --bg-main: #f4f4f5;
    --bg-secondary: #ffffff;
    --glass-bg: rgba(255, 255, 255, 0.6);
    --glass-border: rgba(0, 0, 0, 0.08);
    --text-primary: #18181b;
    --text-secondary: #52525b;
    --system-message-bg: rgba(0, 0, 0, 0.05);
    --highlight-bg: var(--accent-primary);
    --highlight-text: #ffffff;
    --chat-bg: #f9fafb;
}

/* --- Global Reset & Base --- */
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-main);
    color: var(--text-primary);
    min-height: 100vh;
    padding: 2rem 1rem;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* --- Background Effects --- */
.background-overlay {
    position: fixed; inset: 0; z-index: -2;
    background: radial-gradient(circle at 15% 15%, rgba(139, 92, 246, 0.15), transparent 40%),
                radial-gradient(circle at 85% 85%, rgba(56, 189, 248, 0.15), transparent 40%);
}

/* --- Layout & Cards --- */
.app-container {
    max-width: 900px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.glass-card {
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border);
    border-radius: 24px;
    padding: calc(1.5rem * var(--ui-scale));
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
    transition: background-color 0.3s, border-color 0.3s;
}

/* --- Typography --- */
h1 {
    font-weight: 800;
    font-size: calc(2.5rem * var(--ui-scale));
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 0.5rem;
}
h2 { font-size: calc(1.5rem * var(--ui-scale)); margin-bottom: 1.5rem; font-weight: 600; }
h3 { font-size: calc(1.1rem * var(--ui-scale)); margin-bottom: 1rem; font-weight: 600; border-bottom: 1px solid var(--glass-border); padding-bottom: 0.5rem;}
p, li, label, span { color: var(--text-secondary); font-size: calc(0.95rem * var(--ui-scale)); line-height: 1.5; }
.hero-card { text-align: center; padding: 2rem 1rem; }

/* --- Buttons & Inputs --- */
.btn {
    padding: calc(0.6rem * var(--ui-scale)) calc(1.2rem * var(--ui-scale));
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s ease, background-color 0.2s ease, opacity 0.2s ease;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: calc(0.9rem * var(--ui-scale));
    white-space: nowrap;
}
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: translateY(0); }
.btn.btn-primary { background: var(--accent-gradient); color: white; border: none; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.bmc-button { background: #FFDD00; color: #000; border: none; }

input[type="search"] {
    width: 100%;
    padding: calc(0.7rem * var(--ui-scale));
    border-radius: 12px;
    border: 1px solid var(--glass-border);
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: calc(0.9rem * var(--ui-scale));
}
input:focus { outline: 2px solid var(--accent-primary); }
input[type="file"] { display: none; }

/* --- Controls Section --- */
.controls-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
.upload-section, .search-section, .view-options { display: flex; flex-direction: column; gap: 0.75rem; }
.view-options { grid-column: 1 / -1; flex-direction: row; align-items: center; flex-wrap: wrap;}
.upload-section { align-items: flex-start; }
#fileName { font-style: italic; word-break: break-all; }
.search-nav { display: flex; align-items: center; gap: 0.5rem; }
.search-nav .btn { padding: calc(0.5rem * var(--ui-scale)); line-height: 1; }
#searchResultCount { margin-left: 0.5rem; }
.size-controls { display: flex; align-items: center; gap: 0.5rem; margin-right: auto; }
.size-controls .btn { width: calc(28px * var(--ui-scale)); height: calc(28px * var(--ui-scale)); padding: 0; text-align: center; line-height: 1; border-radius: 50%; }

/* --- Chat Area --- */
#chat-display-area { padding: 1rem; }
#chat-container {
    height: 60vh;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    background-color: var(--chat-bg);
    border-radius: 18px;
}
.placeholder { text-align: center; margin: auto; font-size: calc(1.1em * var(--ui-scale)); }
.placeholder.error { color: #ef4444; }

/* --- Chat Bubbles --- */
.message-bubble {
    max-width: 75%;
    padding: calc(8px * var(--chat-font-scale)) calc(12px * var(--chat-font-scale));
    border-radius: 16px;
    margin-bottom: calc(8px * var(--chat-font-scale));
    word-wrap: break-word;
    display: flex;
    flex-direction: column;
    font-size: calc(1em * var(--chat-font-scale));
    transition: background-color 0.3s ease;
    border: 1px solid var(--glass-border);
}
.message-bubble.highlight-bubble { box-shadow: 0 0 0 2px var(--accent-primary); }
.message-bubble .sender { font-weight: bold; font-size: 0.9em; margin-bottom: 0.2em; transition: color 0.3s ease; }
.message-bubble .content { font-size: 0.95em; line-height: 1.4; white-space: pre-wrap; margin-bottom: 0.3em; transition: color 0.3s ease; }
.message-bubble .content mark { background-color: var(--highlight-bg); color: var(--highlight-text); border-radius: 3px; padding: 1px 2px;}
.message-bubble .content.placeholder-content { font-style: italic; opacity: 0.8; }
.message-bubble .message-meta { font-size: 0.75em; color: var(--text-secondary); text-align: right; line-height: 1.2; }
.message-bubble .message-meta .time-ago { font-style: italic; margin-left: 0.5em; }

.system-message {
    background-color: var(--system-message-bg);
    color: var(--text-secondary);
    font-style: italic;
    font-size: calc(0.85em * var(--chat-font-scale));
    padding: calc(6px * var(--chat-font-scale)) calc(12px * var(--chat-font-scale));
    border-radius: 20px;
    margin: calc(10px * var(--ui-scale)) auto;
    max-width: 80%;
    text-align: center;
    align-self: center;
}

/* --- Details Section (Legend & Info) --- */
.details-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 2rem; }
.legend-item { display: flex; align-items: center; margin-bottom: 0.5rem; gap: 0.75rem; }
.legend-item .color-box { width: 15px; height: 15px; border-radius: 4px; border: 1px solid var(--glass-border); flex-shrink: 0; }
.legend-item .sender-name { flex-grow: 1; }
.legend-item .swap-button { margin-left: auto; padding: calc(0.4rem * var(--ui-scale)) calc(0.8rem * var(--ui-scale)); }
.info-section ol, .info-section ul { padding-left: 1.5rem; }
.info-section strong { color: var(--text-primary); }

/* --- Scrollbar --- */
#chat-container::-webkit-scrollbar { width: 8px; }
#chat-container::-webkit-scrollbar-track { background: transparent; }
#chat-container::-webkit-scrollbar-thumb { background: var(--glass-border); border-radius: 4px; }
#chat-container::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }

/* --- Mobile Responsiveness --- */
@media (max-width: 768px) {
    body { padding: 1rem 0.5rem; }
    .controls-grid { grid-template-columns: 1fr; }
    .view-options { flex-direction: column; align-items: stretch; gap: 0.75rem; }
    .size-controls { margin-right: 0; justify-content: space-between;}
    .details-grid { grid-template-columns: 1fr; }
}