/* Chat Application Styles */

html,
body {
    height: 100%;
    height: calc(var(--vh, 1vh) * 100);
    /* iOS viewport fix */
    margin: 0;
    padding: 0;
    overflow: hidden;
    /* Prevent body scroll on mobile */
    -webkit-overflow-scrolling: touch;
    /* Fix for iOS Safari */
}

body {
    font-family: Arial, sans-serif;
    background: var(--bg-tertiary);
    margin: 0;
    padding: 0;
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
    /* iOS viewport fix */
    height: -webkit-fill-available;
    /* iOS viewport fix */
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
    /* iOS Safari fixes */
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
}

.chat-container {
    position: fixed;
    top: 56px;
    /* Height of the nav bar */
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-primary);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Enable touch interactions */
    touch-action: manipulation;
}

/* Mobile adjustments */
@media (max-width: 768px) {
    .chat-container {
        top: 50px;
        /* Smaller nav on mobile */
        left: 0;
        right: 0;
        border-radius: 0;
        box-shadow: none;
    }
}

.chat-header {
    position: sticky;
    top: 0;
    z-index: 2;
    padding: 12px 16px;
    background: var(--accent-color);
    color: var(--white);
    border-radius: 0;
    flex-shrink: 0;
}

.chat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    padding-bottom: 100px;
    /* Space for fixed input area */
    -webkit-overflow-scrolling: touch;
}

.chat-message {
    margin-bottom: 12px;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.chat-message .user {
    color: var(--danger-color);
    font-weight: bold;
    margin-right: 8px;
}

.chat-message .robot {
    color: var(--accent-color);
    font-weight: bold;
    margin-right: 8px;
}

.chat-input-area {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-primary);
    padding: 12px 16px;
    border-top: 1px solid var(--border-color);
    box-shadow: var(--shadow-md);
    z-index: 1000;
    /* iOS Safari fixes */
    -webkit-touch-callout: default;
    -webkit-user-select: text;
    -khtml-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    user-select: text;
    /* Support iOS safe areas */
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
}

/* Chat input styling improvements */
.chat-input-container {
    display: flex;
    gap: 8px;
    align-items: center;
    width: 100%;
}

.chat-input-container textarea {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid var(--input-border);
    border-radius: 20px;
    resize: none;
    min-height: 44px;
    /* iOS minimum touch target */
    max-height: 120px;
    font-size: 16px;
    /* Prevent zoom on iOS */
    line-height: 1.4;
    outline: none;
    font-family: inherit;
    background: var(--input-bg);
    color: var(--text-primary);
    -webkit-appearance: none;
    appearance: none;
    /* iOS specific fixes */
    -webkit-touch-callout: default;
    -webkit-user-select: text;
    -khtml-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    user-select: text;
    -webkit-tap-highlight-color: transparent;
    /* Enable text selection and input */
    pointer-events: auto;
    touch-action: manipulation;
    /* Force hardware acceleration */
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    /* Fix iOS Safari rendering */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.chat-input-container textarea::placeholder {
    color: var(--input-placeholder);
}

.chat-input-container textarea:focus {
    border-color: var(--input-focus);
    box-shadow: 0 0 0 3px var(--accent-color-alpha-25, rgba(0, 123, 255, 0.25));
}

.chat-input-container button {
    padding: 16px 28px;
    background: var(--accent-color);
    color: var(--white);
    border: none;
    border-radius: 20px;
    cursor: pointer;
    font-size: 18px;
    min-width: 80px;
    min-height: 52px;
    /* iOS minimum touch target */
    height: 52px;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: var(--accent-color-alpha-30, rgba(0, 123, 255, 0.3));
    touch-action: manipulation;
    /* Ensure button is tappable */
    pointer-events: auto;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

.chat-input-container button:hover {
    background: var(--accent-hover-color);
}

.chat-input-container button:disabled {
    background: var(--text-muted);
    cursor: not-allowed;
}

/* Header Button Styles */
.chat-header-btn {
    background: var(--bg-primary);
    color: var(--accent-color);
    border: none;
    border-radius: 4px;
    padding: 8px 12px;
    font-weight: bold;
    cursor: pointer;
    -webkit-tap-highlight-color: var(--accent-color-alpha-30);
    touch-action: manipulation;
    pointer-events: auto;
    min-height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s ease;
}

.chat-header-btn:hover {
    background: var(--bg-secondary);
}

.chat-header-btn.clear-btn {
    color: var(--danger-color);
}

.chat-header-btn.clear-btn:hover {
    background: var(--bg-secondary);
}

.chat-header-btn img {
    width: 18px;
    height: 18px;
    display: block;
    pointer-events: none;
}

/* Icon filters for dark mode */
[data-theme="dark"] .chat-header-btn img {
    filter: brightness(0) saturate(100%) invert(74%) sepia(63%) saturate(5837%) hue-rotate(190deg) brightness(101%) contrast(101%);
}

[data-theme="dark"] .chat-header-btn.clear-btn img {
    filter: brightness(0) saturate(100%) invert(47%) sepia(65%) saturate(4898%) hue-rotate(333deg) brightness(105%) contrast(101%);
}

/* Settings Modal Styles */
.settings-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.settings-modal {
    background: var(--bg-primary);
    border-radius: 12px;
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 500px;
    max-height: 80vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px 16px;
    border-bottom: 1px solid var(--border-color);
}

.settings-header h2 {
    margin: 0;
    color: var(--text-primary);
    font-size: 1.5rem;
}

.close-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s ease;
}

.close-btn:hover {
    background: var(--bg-secondary);
    color: var(--danger-color);
}

.settings-content {
    padding: 24px;
    overflow-y: auto;
}

.settings-section {
    margin-bottom: 24px;
}

.settings-section:last-child {
    margin-bottom: 0;
}

.settings-section h3 {
    margin: 0 0 12px 0;
    color: var(--text-primary);
    font-size: 1.1rem;
    font-weight: 600;
}

.theme-controls {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
}

.theme-btn {
    padding: 8px 16px;
    border: 2px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-secondary);
    color: var(--text-primary);
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s ease;
}

.theme-btn:hover {
    border-color: var(--accent-color);
    background: var(--accent-color);
    color: var(--white);
}

.theme-btn.active {
    border-color: var(--accent-color);
    background: var(--accent-color);
    color: var(--white);
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.input-group input,
.input-group textarea {
    padding: 12px;
    border: 2px solid var(--border-color);
    border-radius: 6px;
    background: var(--input-bg);
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s ease;
}

.input-group input:focus,
.input-group textarea:focus {
    border-color: var(--accent-color);
}

.input-group input::placeholder,
.input-group textarea::placeholder {
    color: var(--input-placeholder);
}

.button-group {
    display: flex;
    gap: 8px;
}

.btn-primary {
    padding: 8px 16px;
    background: var(--accent-color);
    color: var(--white);
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: background-color 0.2s ease;
}

.btn-primary:hover {
    background: var(--accent-hover-color);
}

.btn-secondary {
    padding: 8px 16px;
    background: transparent;
    color: var(--text-secondary);
    border: 2px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s ease;
}

.btn-secondary:hover {
    border-color: var(--danger-color);
    color: var(--danger-color);
}

.setting-status {
    font-size: 12px;
    font-weight: 500;
    padding: 4px 8px;
    border-radius: 4px;
    display: none;
    margin-top: 8px;
}

.setting-status.success {
    color: var(--success-color);
    background: var(--success-bg, rgba(40, 167, 69, 0.1));
}

.setting-status.error {
    color: var(--danger-color);
    background: var(--danger-bg, rgba(220, 53, 69, 0.1));
}

/* Mobile responsive adjustments */
@media (max-width: 768px) {
    .settings-overlay {
        padding: 10px;
    }

    .settings-modal {
        max-height: 90vh;
    }

    .settings-header {
        padding: 16px 20px 12px;
    }

    .settings-header h2 {
        font-size: 1.3rem;
    }

    .settings-content {
        padding: 20px;
    }

    .theme-controls {
        flex-direction: column;
    }

    .button-group {
        flex-direction: column;
    }
}

/* Chat input styling improvements */
.chat-input-container {
    display: flex;
    gap: 8px;
    align-items: center;
    width: 100%;
}

/* Mobile specific adjustments */
@media (max-width: 768px) {
    .chat-container {
        top: 50px;
        /* Smaller nav on mobile */
    }

    .chat-header {
        padding: 8px 12px;
    }

    .chat-header h2 {
        font-size: 1.2em;
    }

    .chat-messages {
        padding: 12px;
        padding-bottom: 120px;
        /* More space for input area */
    }

    .chat-input-area {
        padding: 8px 12px;
        padding-bottom: calc(8px + env(safe-area-inset-bottom));
    }

    .chat-input-area textarea {
        font-size: 16px;
        padding: 12px 16px;
        min-height: 44px;
    }

    .chat-input-area button {
        padding: 14px 20px;
        min-width: 75px;
        min-height: 50px;
    }
}

/* iOS Safari specific fixes */
@supports (-webkit-appearance: none) {
    .chat-input-container textarea {
        /* Force iOS to respect background colors in dark mode */
        -webkit-appearance: none;
        appearance: none;
        -webkit-background-clip: padding-box;
        background-clip: padding-box;
    }
}

/* iOS Dark Mode Specific Fixes */
@media (prefers-color-scheme: dark) {

    /* Only apply if system is in dark mode */
    [data-theme="dark"] .chat-input-container textarea {
        background: var(--input-bg) !important;
        color: var(--text-primary) !important;
        border-color: var(--input-border) !important;
    }

    [data-theme="dark"] .chat-input-container textarea::placeholder {
        color: var(--input-placeholder) !important;
        opacity: 1;
    }
}

/* Force override for iOS WebKit */
@media screen and (-webkit-min-device-pixel-ratio: 1) {
    [data-theme="dark"] .chat-input-container textarea {
        background: var(--input-bg) !important;
        color: var(--text-primary) !important;
        -webkit-text-fill-color: var(--text-primary) !important;
    }
}

/* Ensure all interactive elements are properly clickable on touch devices */
button,
input,
textarea,
select,
a,
[onclick],
[role="button"] {
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
    pointer-events: auto;
    /* Ensure minimum touch target size for iOS */
    min-height: 44px;
}

/* Exception for small elements that don't need large touch targets */
.chat-header-btn img,
svg,
span:not([onclick]):not([role="button"]) {
    min-height: auto;
    pointer-events: none;
}

@supports (-webkit-touch-callout: none) {
    .chat-container {
        height: -webkit-fill-available;
    }

    .chat-input-area {
        /* Handle iOS keyboard better */
        position: fixed;
        bottom: 0;
        bottom: env(safe-area-inset-bottom);
    }

    .chat-input-area textarea {
        /* Prevent iOS zoom */
        font-size: 16px;
        -webkit-text-size-adjust: 100%;
    }
}

/* Handle virtual keyboard on mobile */
@media (max-height: 500px) {
    .chat-messages {
        padding-bottom: 100px;
    }

    .chat-input-area {
        padding: 6px 12px;
    }
}