/* ==========================================================================
   1. Global Resets & Base Styles
   ========================================================================== */
html,
body {
    height: 100%;
    margin: 0;
    padding: 0;
    padding-top: 0 !important;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
    background-color: #111010;
    color: #e0e0e0;
    font-size: 16px;
    line-height: 1.6;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100%;
    /* For JS --vh fix: height: calc(var(--vh, 1vh) * 100); */
    /* overflow-x: hidden; /* Prevent horizontal scroll on body */
}

/* Links */
a {
    color: #e74c3c; /* Default link color */
    text-decoration: none;
    transition: color 0.2s ease-in-out;
}
a:hover {
    color: #c0392b;
}

/* Headings */
h1, h3 { /* Added h3 as it's used in sidebars */
    color: #e74c3c;
    margin-top: 0;
    letter-spacing: 0.5px;
}

/* Custom Scrollbars (shared where applied) */
.custom-scrollbar::-webkit-scrollbar { width: 8px; }
.custom-scrollbar::-webkit-scrollbar-track { background: #2c2c2c; border-radius: 10px; }
.custom-scrollbar::-webkit-scrollbar-thumb { background-color: #e74c3c; border-radius: 10px; border: 2px solid #2c2c2c; }
.custom-scrollbar::-webkit-scrollbar-thumb:hover { background-color: #c0392b; }
.custom-scrollbar { scrollbar-width: thin; scrollbar-color: #e74c3c #2c2c2c; }


/* ==========================================================================
   2. Main Site Layout Structure
   ========================================================================== */
.site-wrapper {
    display: flex;
    width: 100%;
    max-width: 1600px;
    margin: 0 auto;
    padding: 20px 15px;
    flex-grow: 1;
    overflow: hidden; /* Prevent site-wrapper from causing scrollbars */
}

.sidebar {
    width: 220px;
    flex-shrink: 0;
    padding-top: 12px;
    background-color: #1c1c1c;
    border: 1px solid #383838;
    border-radius: 10px;
    margin: 0 15px;
    display: none; /* Shown via media query */
    overflow-y: auto;
    max-height: calc(100vh - 40px); /* Viewport height minus site-wrapper padding */
    position: sticky;
    top: 20px; /* Match site-wrapper top padding */
}

.sidebar h3 {
    text-align: center;
    margin-bottom: 20px;
    font-size: 1.2em;
    border-bottom: 1px solid #4a4a4a;
    padding-bottom: 12px;
}

.sidebar .ad-container {
    margin-bottom: 20px;
}

.ad-placeholder-sidebar {
    width: 100%;
    min-height: 500px; /* Default minimum, adjust per ad unit */
    background-color: #282828;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 6px;
    overflow: hidden;
    margin: 10px auto;
}
.ad-placeholder-sidebar ins {
    display: block !important;
    margin: 0 auto !important;
    max-width: 100%;
}

.ad-placeholder-sidebar[style*="height:600px"] { /* For specific tall ad units */
    min-height: 600px;
}

.main-content {
    flex-grow: 1;
    display: flex;
    justify-content: center;
    min-width: 0; /* Critical for flex children sizing */
    height: 100%; /* Fill .site-wrapper */
    overflow: hidden;
}

/* ==========================================================================
   3. App Container (Base for Character Selection & Chat)
   ========================================================================== */
.app-container {
    width: 100%;
    max-width: 700px;
    background-color: #222222;
    border-radius: 12px;
    box-shadow: 0 5px 30px rgba(231, 76, 60, 0.3);
    border: 2px solid #e74c3c;
    display: flex;
    flex-direction: column;
    /* height: 100%;  Fill .main-content */
    overflow: hidden; /* Children will scroll internally */
}

/* ==========================================================================
   4. Character Selection Page (#character-selection-app)
   ========================================================================== */
#character-selection-app {
    /* It inherits display:flex, flex-direction:column from .app-container */
    /* We want this container to be as tall as its content, unless content overflows max-height */
    /* No explicit height: 100% here. */
    /* Set a max-height to enable scrolling if content is very tall */
    max-height: calc(100vh - 20px - 20px); /* Example: Viewport height - site-wrapper padding - some buffer */
    /* Or if using --vh: max-height: calc(var(--vh, 1vh) * 100 - 60px); */
    overflow: hidden; /* Parent will clip, children will scroll */
}

#character-selection-app h1 {
    text-align: center;
    padding: 25px 20px 20px;
    margin: 0; /* Reset margin from generic h1 */
    font-size: 2em;
    border-bottom: 1px solid #4a4a4a;
    font-weight: bold;
    flex-shrink: 0;
}

.character-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 20px;
    padding: 30px;
    justify-items: center;
    /* flex-grow: 1; Takes available space in #character-selection-app */
    overflow-y: auto; /* Enables scrolling for character cards */
    /* Add .custom-scrollbar class in HTML to apply shared scrollbar styles */
}

a.character-card-link {
    display: block;
    transition: transform 0.2s ease-out;
}
a.character-card-link:hover {
    transform: scale(1.03);
}

.character-card {
    background-color: #303030;
    border-radius: 10px;
    padding: 20px;
    text-align: center;
    border: 1px solid #444;
    width: 100%;
    transition: border-color 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    cursor: pointer;
}
.character-card:hover {
    border-color: #e74c3c;
    box-shadow: 0 4px 15px rgba(231, 76, 60, 0.25);
}

.character-card img {
    width: 90px;
    height: 90px;
    border-radius: 50%;
    margin-bottom: 15px;
    object-fit: cover;
    border: 3px solid #555;
    transition: border-color 0.2s;
}
.character-card:hover img {
    border-color: #e74c3c;
}

.character-card span {
    display: block;
    color: #f0f0f0;
    font-size: 1em;
    font-weight: 600;
}

/* ==========================================================================
   5. Chat Page (#chat-app-container)
   ========================================================================== */
#chat-app-container.app-container {
    /* FOR CHAT, WE *DO* WANT IT TO TRY AND FILL THE HEIGHT */
    height: 100%; /* Tries to fill .main-content height */
    /* max-height: calc(100vh - 40px); /* Or your --vh calculation */
    /* overflow: hidden; /* Already on .app-container */
}

.chat-header {
    display: flex;
    align-items: center;
    padding: 12px 20px;
    background-color: #2a2a2a;
    border-bottom: 1px solid #4a4a4a;
    flex-shrink: 0;
}

.chat-header-info {
    display: flex;
    align-items: center;
    margin-right: auto; /* Pushes this group left */
    order: 1;
    min-width: 0; /* Prevents long names from breaking layout */
}

#chat-header-image {
    width: 45px;
    height: 45px;
    border-radius: 50%;
    margin-right: 12px;
    object-fit: cover;
    border: 2px solid #555;
}

#chat-header-name {
    color: #f5f5f5;
    font-weight: bold;
    font-size: 1.2em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#back-button-link { /* This is an <a> tag styled as a button */
    font-size: 1.1em;
    font-weight: bold;
    cursor: pointer;
    padding: 5px 0 5px 15px; /* Add padding to its left */
    order: 2;
    white-space: nowrap;
    margin-left: auto; /* Push to right if .chat-header-info doesn't fill space */
}

#chat-box {
    flex-grow: 1; /* Takes available vertical space */
    overflow-y: auto; /* Enables internal scrolling */
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    border-bottom: 1px solid #4a4a4a;
    background-color: #252525;
    min-height: 150px; /* Minimum visible area */
    position: relative; /* For z-index context if needed for ads */
    /* Add .custom-scrollbar class in HTML to apply shared scrollbar styles */
}

/* Message Styling */
.message {
    padding: 12px 18px;
    border-radius: 20px;
    max-width: 80%;
    word-wrap: break-word;
    line-height: 1.5;
    opacity: 0;
    transform: translateY(15px);
    animation: fadeInMessage 0.35s ease-out forwards;
    font-size: 0.95em;
}
@keyframes fadeInMessage { to { opacity: 1; transform: translateY(0); } }

.message.user {
    background-color: #3498db;
    color: #fff;
    align-self: flex-end;
    border-bottom-right-radius: 6px;
}

.message.ai {
    background-color: #484848;
    color: #e8e8e8;
    align-self: flex-start;
    border-bottom-left-radius: 6px;
}
.message.ai strong { /* Character Name in AI message */
    color: #e74c3c;
    display: block;
    margin-bottom: 6px;
    font-size: 0.9em; /* Relative to .message font-size */
    font-weight: bold;
}

.message.system {
    background-color: transparent;
    color: #999;
    font-style: italic;
    text-align: center;
    font-size: 0.85em; /* Relative to body font-size */
    align-self: center;
    animation: none; opacity: 1; transform: none;
    padding: 5px 0;
}

/* Typing Indicator */
.message.ai.typing-indicator {
    /* Inherits .message.ai styles */
    display: flex;
    align-items: center;
    gap: 8px;
}
.message.ai.typing-indicator strong { margin-bottom: 0; } /* Override if needed */
.typing-indicator div { display: flex; } /* Wrapper for dots */
.typing-indicator span {
    height: 8px; width: 8px; background-color: #b0b0b0;
    border-radius: 50%; display: inline-block; margin: 0 2.5px;
    animation: dot-pulse 1.4s infinite ease-in-out both;
}
.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }
@keyframes dot-pulse {
  0%, 80%, 100% { transform: scale(0); opacity: 0.5; }
  40% { transform: scale(1.0); opacity: 1; }
}

/* Input Area */
#input-area {
    display: flex;
    padding: 15px 20px;
    background-color: #1e1e1e;
    flex-shrink: 0;
    border-top: 1px solid #4a4a4a;
    position: relative; /* For z-index if needed */
}

#chat-input-form { /* The form wrapping the textarea and button */
    display: flex;
    flex-grow: 1;
    align-items: center; /* Vertically align textarea and button */
}

#user-input { /* Textarea */
    flex-grow: 1;
    padding: 10px 18px;
    border: 1px solid #555;
    border-radius: 25px 0 0 25px;
    background-color: #333;
    color: #f0f0f0;
    font-size: 1em;
    font-family: inherit;
    line-height: 1.4;
    outline: none;
    transition: border-color 0.2s, background-color 0.2s;
    resize: none;
    overflow-y: hidden; /* JS will change to 'auto' when needed */
    min-height: calc(1em * 1.4 + 20px); /* Approx 1 line text + padding */
    max-height: 120px; /* Approx 5 lines before internal scroll */
}
#user-input:focus {
    border-color: #e74c3c;
    background-color: #383838;
    /* overflow-y: auto; /* JS handles this based on content */
}
#user-input:disabled {
    background-color: #404040;
    cursor: not-allowed;
    border-color: #505050;
}

#send-button {
    padding: 0 25px; /* Adjust padding if height is fixed */
    height: calc(1em * 1.4 + 20px); /* Match textarea's initial height (text + padding + border) */
    box-sizing: border-box; /* Include padding and border in height */
    border: none;
    background-color: #e74c3c;
    flex-shrink: 0;
    color: #fff;
    font-size: 1em;
    font-weight: bold;
    cursor: pointer;
    border-radius: 0 25px 25px 0;
    transition: background-color 0.2s ease;
    display: flex; /* To center text if needed */
    align-items: center;
    justify-content: center;
}
#send-button:hover:not(:disabled) { background-color: #c0392b; }
#send-button:disabled { background-color: #a33a2f; cursor: not-allowed; opacity: 0.7; }

/* ==========================================================================
   6. Disclaimer
   ========================================================================== */
.disclaimer {
    font-size: 0.8em;
    text-align: center;
    color: #888;
    padding: 10px 15px;
    background-color: #222222;
    flex-shrink: 0;
    border-top: 1px solid #4a4a4a;
}


/* ==========================================================================
   7. Responsive Styles
   ========================================================================== */
@media (min-width: 992px) { /* Desktop */
    .sidebar {
        display: block;
    }
}

@media (min-width: 1200px) { /* Large Desktop */
    .sidebar {
        width: 250px;
    }
    /* .app-container { max-width: 750px; } /* Optional wider app */
}

@media (max-width: 991px) { /* Tablet - Sidebars hidden */
    .site-wrapper {
        padding: 15px 10px; /* Less horizontal padding */
    }
    
    #character-selection-app {
        height: max-content;
    }
    .character-grid {
        /* For larger tablets, auto-fit might be okay, or specify columns */
        grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); /* Slightly smaller min */
        /* OR fixed columns: */
        /* grid-template-columns: repeat(4, 1fr); */
        gap: 20px;
        padding: 25px;
        justify-content: flex-start; /* Align grid items to the left if row isn't full */
    }
    
}

@media (max-width: 768px) { /* Small Tablet / Large Phone */
    #character-selection-app h1 { font-size: 1.7em; padding: 20px 15px 15px; }
    .character-grid { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 20px; padding: 20px; }
    .character-card img { width: 75px; height: 75px; }

    #chat-app-container.app-container {
        min-height: 88vh; /* Ensure good height on mobile, adjust as needed with ads */
        max-height: calc(100vh - 30px);
    }
    .chat-header { padding: 10px 15px; }
    #chat-header-image { width: 40px; height: 40px; }
    #chat-header-name, #back-button-link { font-size: 1.05em; }

    #chat-box { padding: 15px; gap: 12px; }
    .message { max-width: 85%; font-size: 0.9em; }

    #input-area { padding: 12px 15px; }
    #user-input {
        padding: 8px 15px; /* Adjust padding for smaller font */
        font-size: 0.95em;
        min-height: calc(0.95em * 1.4 + 16px);
    }
    #send-button {
        font-size: 0.95em;
        padding: 0 20px; /* Adjust padding */
        height: calc(0.95em * 1.4 + 16px); /*  Match smaller textarea */
    }
}

@media (max-width: 480px) { /* Small Phone */
    .site-wrapper { padding: 10px 5px; }
    .app-container, #character-selection-app, #chat-app-container.app-container {
        border-radius: 8px;
    }

    #character-selection-app h1 { font-size: 1.5em; }
    .character-grid { grid-template-columns: repeat(2, 1fr); gap: 20px; padding: 15px; }
    .character-card { min-width: 140px; }
    .character-card img { width: 70px; height: 70px; margin-bottom: 10px; }
    .character-card span { font-size: 0.9em; }

    #chat-header-image { width: 35px; height: 35px; margin-right: 8px; }
    #chat-header-name, #back-button-link { font-size: 1em; }
    #back-button-link { padding: 5px 0 5px 10px; }


    #input-area { padding: 10px; }
    #user-input {
        padding: 8px 12px;
        min-height: calc(1em * 1.4 + 16px); /* Base on 1em font for consistency */
    }
    #send-button {
        padding: 0 15px; /* Adjust padding */
        height: calc(1em * 1.4 + 16px); /* Match textarea */
    }

    .message { max-width: 90%; }
    .disclaimer { font-size: 0.75em; padding: 8px 10px;}
}