/* --- CSS Variables --- */
:root {
    --chat-font-size: 16px;
}

/* --- Foundational Layout (Mobile-First & iOS Fixes) --- */
html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    overflow: hidden; /* Prevents accidental scrolling of the body */
    position: fixed; /* Critical fix for iOS viewport issues */
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: #121212;
    color: #e0e0e0;
    display: flex;
    justify-content: center;
    align-items: center;
}

.chat-container {
    width: 100%;
    max-width: 600px;
    /* Use fixed height instead of dynamic viewport height */
    height: 100vh; /* Use standard viewport height */
    /* Remove the dynamic --vh variable to prevent keyboard-induced resizing */
    background-color: #1e1e1e;
    border-radius: 0;
    box-shadow: none;
    display: flex;
    flex-direction: column;
    overflow: hidden; /* This is important */
    /* Add this to prevent the container from resizing when keyboard appears */
    position: relative;
}

/* Add keyboard-specific styles for mobile */
@media (max-width: 768px) {
    .chat-container {
        /* Ensure the container uses the initial viewport height, not the dynamic one */
        height: 100vh;
        /* Prevent any automatic adjustments */
        min-height: 100vh;
        max-height: 100vh;
    }
    
    /* When keyboard is active, prevent viewport adjustments */
    .keyboard-active .chat-container {
        height: 100vh;
        transform: translateY(0);
    }
}

.chat-window {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    padding: 15px;
    display: flex;
    flex-direction: column;
    min-height: 0;
    font-size: var(--chat-font-size, 16px);
}

.message {
    margin-bottom: 15px;
    padding: 10px 12px;
    border-radius: 8px;
    line-height: 1.45;
    word-wrap: break-word;
    font-size: var(--chat-font-size, 16px);
}

/* Ensure all HTML content within messages respects the font size */
.message * {
    font-size: inherit !important;
}

/* Make all headers one font size larger than standard text */
.message h1,
.message h2,
.message h3,
.message h4,
.message h5,
.message h6 {
    font-size: calc(var(--chat-font-size, 16px) + 2px) !important;
    font-weight: bold;
    margin: 0.5em 0;
}

/* Cleaner markdown formatting styles */
.message strong,
.message b {
    font-weight: bold;
}

.message em,
.message i {
    font-style: italic;
}

.message code {
    background-color: rgba(255, 255, 255, 0.1);
    padding: 2px 4px;
    border-radius: 3px;
    font-family: 'Courier New', 'Menlo', 'Monaco', monospace;
    font-size: 0.9em;
}

.message pre {
    background-color: rgba(255, 255, 255, 0.05);
    padding: 10px;
    border-radius: 5px;
    overflow-x: auto;
    margin: 10px 0;
    white-space: pre;
}

.message pre code {
    background-color: transparent;
    padding: 0;
    border-radius: 0;
}

.message ul,
.message ol {
    margin: 10px 0;
    padding-left: 20px;
}

.message li {
    margin: 3px 0;
}

.message hr {
    border: none;
    border-top: 1px solid rgba(255, 255, 255, 0.2);
    margin: 15px 0;
}

.message blockquote {
    border-left: 4px solid rgba(255, 255, 255, 0.3);
    padding-left: 10px;
    margin: 10px 0;
    font-style: italic;
}

.message p {
    margin: 10px 0;
}

.message table {
    border-collapse: collapse;
    width: 100%;
    margin: 10px 0;
}

.message th,
.message td {
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 8px;
    text-align: left;
}

.message th {
    background-color: rgba(255, 255, 255, 0.1);
    font-weight: bold;
}

.user-message {
    background-color: #007bff;
    color: white;
    align-self: flex-end;
    margin-left: auto;
    max-width: 80%;
    text-align: right;
}

.llm-message {
    background-color: #333;
    color: #e0e0e0;
    align-self: flex-start;
    max-width: 80%;
}

.message img {
    max-width: 100%;
    border-radius: 5px;
    margin-top: 10px;
    cursor: pointer; /* Add this to indicate images are clickable */
    flex-shrink: 0; /* Prevent images from affecting other elements' flex calculations */
    display: block; /* Ensure images don't interfere with inline layout */
}

/* --- Fullscreen Image Viewer --- */
#fullscreen-image-viewer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.9);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    cursor: pointer;
    padding: 20px;
    box-sizing: border-box;
}

#fullscreen-image {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    object-fit: contain;
    border-radius: 8px;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.7);
}

/* Mobile optimizations for fullscreen viewer */
@media (max-width: 768px) {
    #fullscreen-image-viewer {
        padding: 10px;
    }
    
    #fullscreen-image {
        max-width: calc(100vw - 20px);
        max-height: calc(100vh - 20px);
    }
}

/* --- Input Area (Robust Flexbox) --- */
.input-area {
    display: flex;
    align-items: flex-end; /* Align items to the bottom */
    flex-shrink: 0;
    padding: 8px;
    background-color: #1e1e1e;
    min-height: 60px;
    box-sizing: border-box;
    transition: all 0.3s ease;
}

#user-input {
    flex: 1 1 auto; /* Allow it to grow and shrink */
    min-width: 0; /* Prevent it from overflowing */
    min-height: 44px;
    height: 44px;
    max-height: 200px;
    padding: 0 10px;           /* No vertical padding */
    border: none;
    box-sizing: border-box;
    line-height: 44px;         /* Vertically center text for single line */
    font-size: 16px;
    resize: none;
    background-color: #333;
    color: #e0e0e0;
    border-radius: 4px;
    font-family: inherit;
    transition: all 0.3s ease;
    overflow-y: hidden;
    outline: none;
}

#user-input:focus {
    outline: none;
    /* Optionally add a custom focus style for accessibility, e.g.: */
    /* box-shadow: 0 0 0 2px #007bff; */
}

.input-area.expanded #user-input {
    /* No change needed, flexbox handles it */
}

/* --- Button Row & Individual Buttons --- */
.button-row {
    display: flex;
    align-items: flex-end;
    flex-shrink: 0; /* Prevent this container from shrinking */
    margin-left: 6px;
    gap: 5px;
}

#attach-button, #mic-button, #settings-button, #send-button {
    background-color: #333;
    color: #e0e0e0;
    border: none;
    cursor: pointer;
    font-size: 16px;
    border-radius: 4px;
    height: 44px;
    width: 44px;
    padding: 0;
    flex-shrink: 0; /* Prevent buttons from shrinking */
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    box-sizing: border-box;
    font-family: inherit;
    line-height: 1;
    outline: none; /* Remove focus outline */
    -webkit-tap-highlight-color: transparent; /* Remove mobile tap highlight */
}

#send-button {
    background-color: #007bff;
    color: white;
}

#send-button:hover {
    background-color: #0056b3;
}

#mic-button.recording {
    background-color: #28a745;
    box-shadow: 0 0 10px rgba(40, 167, 69, 0.5);
    animation: pulse-green 1.5s infinite;
}

@keyframes pulse-green {
    0% {
        box-shadow: 0 0 10px rgba(40, 167, 69, 0.5);
    }
    50% {
        box-shadow: 0 0 20px rgba(40, 167, 69, 0.8);
    }
    100% {
        box-shadow: 0 0 10px rgba(40, 167, 69, 0.5);
    }
}

.input-area.expanded #attach-button,
.input-area.expanded #mic-button {
    opacity: 0;
    visibility: hidden;
    width: 0;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

/* Basic Toggle Switch CSS */
.switch {
    position: relative;
    display: inline-block;
    width: 50px; /* Adjusted width */
    height: 28px; /* Adjusted height */
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
    border-radius: 28px; /* Adjusted for height */
}

.slider:before {
    position: absolute;
    content: "";
    height: 20px; /* Adjusted size */
    width: 20px;  /* Adjusted size */
    left: 4px;    /* Adjusted position */
    bottom: 4px;  /* Adjusted position */
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: #007bff;
}

input:focus + .slider {
    box-shadow: 0 0 1px #007bff;
}

input:checked + .slider:before {
    transform: translateX(22px); /* Adjusted translation */
}

/* Basic Slider CSS */
.setting-item input[type="range"] {
    flex-grow: 1; /* Corrected from flex-shrink to allow the slider to fill available space */
    margin: 0 10px;
    cursor: pointer;
    min-width: 0; /* Allow the slider to shrink below its default minimum width */
}
.setting-item input[type="number"] {
    max-width: 70px; /* Use max-width to allow shrinking */
    min-width: 60px; /* Prevent it from becoming too small */
    flex-shrink: 1;  /* Explicitly allow shrinking */
    padding: 5px;
    background-color: #444;
    color: #e0e0e0;
    border: 1px solid #555;
    border-radius: 3px;
}
/* Welcome message styles - centered in chat */
.welcome-message {
    background: linear-gradient(135deg, #000000 0%, #000000 100%);
    color: white;
    text-align: center;
    font-size: 28px;
    font-weight: bold;
    padding: 8px 20px;
    margin: 10px auto;
    border-radius: 15px;
    box-shadow: 
        0 20px 40px rgba(0, 0, 0, 0.4),
        0 10px 20px rgba(0, 0, 0, 0.3),
        0 5px 10px rgba(0, 0, 0, 0.2);
    max-width: 50%;
    min-width: 200px; /* Prevent compression below this width */
    width: fit-content; /* Use content-based sizing */
    align-self: center;
    position: relative;
    overflow: hidden;
    transform: translateY(-5px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    flex-shrink: 0; /* Prevent flex container from shrinking this element */
}

/* Add hover effect for more floating feel */
.welcome-message:hover {
    transform: translateY(-8px);
    box-shadow: 
        0 25px 50px rgba(0, 0, 0, 0.5),
        0 15px 30px rgba(0, 0, 0, 0.4),
        0 8px 15px rgba(0, 0, 0, 0.3);
    transition: all 0.3s ease;
}

/* Add dark glow effect behind the box */
.welcome-message::after {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    background: linear-gradient(135deg, #333333, #111111);
    border-radius: 17px;
    z-index: -1;
    filter: blur(8px);
    opacity: 0.6;
}

/* Animated letter styles for the colorful effect */
.animated-letter {
    display: inline-block;
    animation: colorWave 3s ease-in-out infinite;
    animation-fill-mode: both;
}

/* When a panel is open, pause the expensive background animation to prevent rendering glitches. */
body.panel-open .animated-letter {
    animation: none;
}

@keyframes colorWave {
    0%, 100% {
        color: #ffffff;
        transform: translateY(0px) scale(1);
        text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
    }
    25% {
        color: #ff6b6b;
        transform: translateY(-3px) scale(1.05);
        text-shadow: 0 0 15px rgba(255, 107, 107, 0.8);
    }
    50% {
        color: #4ecdc4;
        transform: translateY(-5px) scale(1.1);
        text-shadow: 0 0 20px rgba(78, 205, 196, 0.9);
    }
    75% {
        color: #45b7d1;
        transform: translateY(-3px) scale(1.05);
        text-shadow: 0 0 15px rgba(69, 183, 209, 0.8);
    }
}

/* Stagger the animation for each letter */
.animated-letter:nth-child(1) { animation-delay: 0.1s; }
.animated-letter:nth-child(2) { animation-delay: 0.2s; }
.animated-letter:nth-child(3) { animation-delay: 0.3s; }
.animated-letter:nth-child(4) { animation-delay: 0.4s; }
.animated-letter:nth-child(5) { animation-delay: 0.5s; }
.animated-letter:nth-child(6) { animation-delay: 0.6s; }
.animated-letter:nth-child(7) { animation-delay: 0.7s; }
.animated-letter:nth-child(8) { animation-delay: 0.8s; }
.animated-letter:nth-child(9) { animation-delay: 0.9s; }
.animated-letter:nth-child(10) { animation-delay: 1.0s; }
.animated-letter:nth-child(11) { animation-delay: 1.1s; }
.animated-letter:nth-child(12) { animation-delay: 1.2s; }
.animated-letter:nth-child(13) { animation-delay: 1.3s; }
.animated-letter:nth-child(14) { animation-delay: 1.4s; }
.animated-letter:nth-child(15) { animation-delay: 1.5s; }

#attached-docs-container {
    background: none;
    border: none;
    box-shadow: none;
    margin: 0 0 8px 0;
    padding: 0;
    display: flex;
    flex-direction: row;
    align-items: flex-end;
}

.attached-docs-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.clear-docs-btn {
    background: none;
    border: none;
    color: #ff6b6b;
    cursor: pointer;
    font-size: 14px;
    padding: 2px 8px;
    border-radius: 4px;
    transition: background-color 0.2s, color 0.2s;
}

.clear-docs-btn:hover {
    background-color: #ff6b6b;
    color: white;
}

.attached-docs-list {
    display: flex;
    flex-wrap: wrap;
    padding-left: 10px;
    gap: 8px;
}

.attached-doc-item {
    background-color: #222;
    border-radius: 4px;
    padding: 4px 8px;
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 13px;
}

.attached-doc-item:hover {
    background-color: #333;
}

.doc-name {
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.remove-doc-btn {
    background: none;
    border: none;
    color: #ff6b6b;
    cursor: pointer;
    font-size: 14px;
    padding: 2px 6px;
    border-radius: 50%;
    transition: background-color 0.3s ease, color 0.3s ease;
    align-items: center;
    justify-content: center;
}

.remove-doc-btn:hover {
    background-color: #ff6b6b;
    color: white;
}

/* --- Overlay for modal panels --- */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 10, 20, 0.55);
    z-index: 1000;
    transition: opacity 0.3s ease;
    backdrop-filter: blur(2px);
}
.overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

/* --- Panel Containers (Common Styles) --- */
#settings-panel-container,
#persona-panel-container {
    position: fixed;
    left: 0;
    right: 0;
    z-index: 1100;
    pointer-events: none;
    display: flex;
    justify-content: center;
}

#settings-panel-container {
    bottom: 0;
    align-items: flex-end;
}

#persona-panel-container {
    top: 0;
    align-items: flex-start;
}

/* --- Panel Base Styles (The Animated Container) --- */
.settings-panel,
.persona-panel {
    position: relative;
    width: 100%;
    max-width: 600px;
    background: linear-gradient(135deg, #232323 80%, #181818 100%);
    box-shadow: 0 0 40px rgba(0,0,0,0.5);
    border: 1px solid #292929;
    transition: transform 0.35s cubic-bezier(.2, .8, .2, 1), opacity 0.3s ease;
    opacity: 0;
    pointer-events: auto;
    contain: layout style; /* Optimize rendering */
    transform: translateZ(0); /* Forces hardware acceleration and creates a new rendering layer */
    /* This outer panel has NO padding, overflow, or height. It only animates. */
}

.settings-panel {
    border-radius: 18px 18px 0 0;
    transform: translateY(100%);
}
.persona-panel {
    border-radius: 0 0 18px 18px;
    transform: translateY(-100%);
}

.settings-panel.open,
.persona-panel.open {
    transform: translateY(0);
    opacity: 1;
}

/* --- Panel Content (The Scrollable Inner Wrapper) --- */
.panel-content {
    padding: 24px 20px;
    overflow-y: scroll; /* Changed to 'scroll' to permanently reserve space for the scrollbar */
    max-height: 480px; /* Fixed pixel height instead of vh */
    min-height: 200px; /* Prevent collapse */
    box-sizing: border-box;
    /* The 'will-change' property has been removed as it was likely causing issues. */
}

/* --- Panel Form Elements Styling --- */
.settings-panel h2,
.persona-panel h2 {
    font-size: 1.4em;
    font-weight: bold;
    margin-top: 0;
    margin-bottom: 24px;
    color: #fff;
    text-align: center;
}

.settings-panel .setting-item,
.persona-panel .persona-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    gap: 15px;
}

.settings-panel label,
.persona-panel label {
    color: #b0b0b0;
    font-size: 1em;
    white-space: nowrap; /* Prevent label text from wrapping to a new line */
}

.settings-panel input,
.settings-panel select,
.settings-panel textarea,
.persona-panel input,
.persona-panel select,
.persona-panel textarea {
    background: #181818;
    color: #e0e0e0;
    border: 1px solid #333;
    border-radius: 8px;
    padding: 12px;
    font-size: 1em;
    flex-grow: 1; /* Allow them to grow to fill space */
    max-width: 250px; /* But not get too wide */
    min-width: 0; /* CRITICAL FIX: Allows elements to shrink below their intrinsic minimum width */
}

.settings-panel button,
.persona-panel button {
    background: #007bff;
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 12px 18px;
    font-size: 1em;
    margin-top: 12px;
    cursor: pointer;
    transition: background 0.2s, box-shadow 0.2s, transform 0.2s ease;
    flex: 1 1 auto; /* Allow buttons to grow and shrink as needed */
}

/* Add this new class for button containers */
.panel-button-group {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    margin-bottom: 20px; /* Add this to create space below the buttons */
}

.settings-panel button.secondary-button,
.persona-panel button.secondary-button {
    background: #444;
}
.settings-panel button.cancel-button,
.persona-panel button.cancel-button {
    background: #555;
}
.settings-panel button:hover,
.persona-panel button:hover {
    background: #0056b3;
    transform: translateY(-2px);
}

/* --- Persona Panel Specific Styles --- */
.persona-panel textarea {
    min-height: 150px;
    resize: vertical;
    width: 100%;
    max-width: none; /* This removes the 250px limit from the general rule */
    box-sizing: border-box;
    margin-bottom: 16px;
}

.panel-description {
    text-align: center;
    color: #b0b0b0;
    margin-top: -16px;
    margin-bottom: 24px;
}

#create-persona-button {
    background-color: #4a934a; /* Muted Green */
    box-shadow: 0 4px 15px -5px rgba(74, 147, 74, 0.6);
}
#create-persona-button:hover {
    background-color: #3a7d3a;
    box-shadow: 0 6px 20px -5px rgba(74, 147, 74, 0.8);
}

#use-random-persona-button {
    background-color: #8867a5; /* Muted Purple */
    box-shadow: 0 4px 15px -5px rgba(136, 103, 165, 0.6);
}
#use-random-persona-button:hover {
    background-color: #6f5287;
    box-shadow: 0 6px 20px -5px rgba(136, 103, 165, 0.8);
}

/* --- Collapsible Settings Group --- */
.settings-group {
    border: 1px solid #333;
    border-radius: 8px;
    margin-top: 16px;
    margin-bottom: 16px;
    padding: 0 15px;
}

.settings-group[open] {
    padding-bottom: 10px;
}

.settings-group-header {
    display: block;
    padding: 12px 0;
    cursor: pointer;
    font-weight: bold;
    color: #e0e0e0;
    list-style: none; /* Removes the default marker in Firefox */
}

.settings-group-header::-webkit-details-marker {
    display: none; /* Removes the default marker in Chrome/Safari */
}

.settings-group-header:hover {
    color: #fff;
}

.settings-group-header::before {
    content: '▶';
    display: inline-block;
    margin-right: 10px;
    font-size: 0.8em;
    transition: transform 0.2s ease-in-out;
}

.settings-group[open] > .settings-group-header::before {
    transform: rotate(90deg);
}

/* Custom dark scrollbar styling */
.chat-window::-webkit-scrollbar,
.panel-content::-webkit-scrollbar {
    width: 6px;
}

.chat-window::-webkit-scrollbar-track,
.panel-content::-webkit-scrollbar-track {
    background: #1e1e1e;
    border-radius: 3px;
}

.chat-window::-webkit-scrollbar-thumb,
.panel-content::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 3px;
}

.chat-window::-webkit-scrollbar-thumb:hover,
.panel-content::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* --- Responsive for small screens --- */
@media (max-width: 600px) {
    .panel-content {
        padding: 20px 15px;
        max-height: 400px; /* Fixed pixel height for mobile too */
        min-height: 300px;
    }
    .settings-panel, .persona-panel {
        border-radius: 0; /* Edge-to-edge on mobile */
    }
}

