/* Mobile CSS for Bond Bot System - iOS Optimized */
/* Phase 1: Foundation - Safe mobile enhancements without breaking desktop */

/* Ensure all sections are visible on desktop */
@media screen and (min-width: 1025px) {
    .category-filter,
    .topic-filter,
    .sources-section {
        display: block !important;
    }
}

/* ==========================================================================
   MOBILE-FIRST RESPONSIVE FRAMEWORK
   ========================================================================== */

/* Base mobile improvements (apply to all mobile) */
@media screen and (max-width: 1024px) {
    /* Ensure all sections are visible on mobile */
    .category-filter,
    .topic-filter,
    .sources-section {
        display: block !important;
    }
    /* Improve touch targets */
    button, .btn, input[type="submit"], input[type="button"] {
        min-height: 44px;
        min-width: 44px;
        padding: 12px 16px;
    }
    
    /* Better typography for mobile reading */
    body {
        font-size: 16px; /* Prevent iOS zoom on input focus */
        line-height: 1.5;
        -webkit-text-size-adjust: none;
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }
    
    /* Improved form inputs */
    input, textarea, select {
        font-size: 16px; /* Prevent iOS zoom */
        padding: 12px;
        border-radius: 8px;
    }
    
    /* Welcome message - green background for all mobile layouts */
    .welcome-message {
        background: var(--primary-light, #e8f5f0) !important;
        border-radius: 12px !important;
        padding: 20px !important;
        margin-bottom: 20px !important;
        text-align: left;
        max-width: 85% !important;
    }
    
    .welcome-message p {
        margin: 0 0 8px 0 !important;
        font-size: 16px;
        line-height: 1.5;
    }
    
    .welcome-message p:last-child {
        margin-bottom: 0 !important;
    }
    
    .welcome-message strong {
        color: var(--primary-color, #006341);
        font-weight: 600;
    }
}

/* ==========================================================================
   IPHONE PORTRAIT LAYOUT (≤480px) - Stacked Single Column
   ========================================================================== */
@media screen and (max-width: 480px) {
    
    /* Header adjustments for small screens */
    header {
        padding: 0.75rem 1rem;
        position: relative;
        z-index: 100;
    }
    
    .header-content {
        flex-direction: column;
        gap: 0.75rem;
        align-items: stretch;
    }
    
    .header-content h1 {
        font-size: 1.1rem;
        text-align: left;
        width: 100%;
        margin: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    
    .header-controls {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 1rem;
    }
    
    /* User menu improvements for touch */
    .user-menu {
        position: relative;
    }
    
    .user-menu-button {
        height: 36px !important; /* Exact match to mode-indicator */
        min-height: unset !important;
        min-width: unset !important;
        padding: 0.5rem 0.75rem !important; /* Match mode-indicator padding style */
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 0.3rem;
        background: var(--primary-color, #006341);
        color: white;
        border: none;
        border-radius: 8px;
        font-size: 0.8rem; /* Match mode-indicator font-size */
        font-weight: 600; /* Match mode-indicator weight */
        -webkit-tap-highlight-color: rgba(255, 255, 255, 0.2);
        cursor: pointer;
    }
    
    .new-chat-button {
        height: 36px !important; /* Match user-menu-button and mode-indicator */
        min-height: unset !important;
        min-width: unset !important;
        padding: 0.5rem 0.75rem !important; /* Match other buttons */
        font-size: 0.8rem !important; /* Match mobile font size */
        font-weight: 600 !important;
        gap: 4px !important; /* Smaller gap on mobile */
    }
    
    .user-menu-dropdown {
        position: fixed !important;
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        max-height: 70vh !important;
        background: white;
        border-radius: 16px 16px 0 0 !important;
        box-shadow: 0 -4px 20px rgba(0,0,0,0.15) !important;
        transform: translateY(100%) !important;
        transition: transform 0.3s ease-out;
        z-index: 1000 !important;
        padding: 1rem !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .user-menu-dropdown.show {
        transform: translateY(0) !important;
    }
    
    .menu-item {
        display: flex !important;
        align-items: center !important;
        gap: 1rem !important;
        padding: 16px 20px !important;
        min-height: 56px !important;
        border-radius: 12px !important;
        margin-bottom: 8px !important;
        font-size: 16px !important;
        color: var(--text-color, #333) !important;
        text-decoration: none !important;
        background: var(--light-gray, #f8f9fa);
        -webkit-tap-highlight-color: rgba(0, 99, 65, 0.1);
        transition: background-color 0.2s ease;
    }
    
    .menu-item:active {
        background: rgba(0, 99, 65, 0.1) !important;
        transform: scale(0.98);
    }
    
    .menu-item svg {
        width: 20px !important;
        height: 20px !important;
        flex-shrink: 0;
    }
    
    .menu-divider {
        height: 1px !important;
        background: var(--medium-gray, #e9ecef) !important;
        margin: 16px 0 !important;
        border: none !important;
    }
    
    /* Main layout - vertical stacking */
    main {
        flex-direction: column !important;
        padding: 0.5rem !important;
        gap: 0.5rem !important; /* Restored original gap */
    }
    
    /* Chat container - full width stacked */
    .chat-container {
        flex-direction: column !important;
        width: 100% !important;
        order: 2;
    }
    
    /* Chat messages area */
    .chat-messages {
        flex: 1 !important; /* Fill all available space */
        min-height: 350px;
        overflow-y: auto !important;
        margin-bottom: 0 !important; /* Remove margin to eliminate gap */
    }
    
    /* Chat input area - enhanced for iOS Safari */
    .chat-input {
        order: 3;
        width: 100%;
        position: sticky;
        bottom: 0;
        background: white;
        padding: 8px 12px; /* Reduced padding */
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -2px 20px rgba(0,0,0,0.1);
        z-index: 50;
        border-top: 1px solid var(--medium-gray, #e9ecef);
    }
    
    /* Text input area - full width */
    #query-input {
        width: 100%;
        min-height: 40px !important; /* Slightly smaller */
        max-height: 100px; /* Reduced max height */
        padding: 8px 12px !important; /* Reduced padding */
        border: 2px solid var(--medium-gray, #e9ecef) !important;
        border-radius: 12px !important;
        font-size: 16px !important;
        font-family: inherit;
        resize: none;
        background: white;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        -webkit-tap-highlight-color: transparent;
        transition: border-color 0.2s ease, box-shadow 0.2s ease;
        margin-bottom: 6px; /* Reduced margin */
    }
    
    #query-input:focus {
        outline: none !important;
        border-color: var(--primary-color, #006341) !important;
        box-shadow: 0 0 0 3px rgba(0, 99, 65, 0.1) !important;
    }
    
    /* Restructure input area layout */
    .input-group {
        display: block !important;
        margin-bottom: 6px; /* Reduced margin */
    }
    
    /* Send button - aligned with prompt selector */
    #send-button {
        min-width: 80px !important;
        min-height: 40px !important; /* Match prompt selector height */
        padding: 8px 14px !important;
        background: var(--primary-color, #006341) !important;
        color: white !important;
        border: none !important;
        border-radius: 12px !important;
        font-size: 16px !important;
        font-weight: 600 !important;
        -webkit-tap-highlight-color: rgba(255, 255, 255, 0.2);
        cursor: pointer;
        transition: background-color 0.2s ease, transform 0.1s ease;
        flex-shrink: 0; /* Prevent button from shrinking */
    }
    
    #send-button:active {
        transform: scale(0.95);
        background: var(--primary-hover, #004d33) !important;
    }
    
    /* Prompt selector row - horizontal layout with send button */
    .prompt-selector-row {
        display: flex !important;
        align-items: center !important;
        gap: 8px !important; /* Gap between elements */
        margin-bottom: 4px !important; /* Reduced margin */
    }
    
    /* Prompt selector group takes available space */
    .prompt-selector-group {
        display: flex !important;
        flex: 1 !important;
        align-items: center !important;
        gap: 8px !important;
    }
    
    /* Hide prompt selector for council users */
    body.council-user .prompt-selector-group {
        display: none !important;
    }

    /* Adjust prompt selector row layout for council users */
    body.council-user .prompt-selector-row {
        justify-content: flex-end !important;
    }

    /* Hide admin menu items for council users in mobile */
    body.council-user .menu-item[href*="admin"],
    body.council-user .menu-item[href*="users"],
    body.council-user #prompt-management-link,
    body.council-user .menu-item[href*="upload_media"] {
        display: none !important;
    }

    /* Header council notice mobile styling */
    .header-council-notice {
        background: #fff3cd !important; /* Original yellow background */
        color: #856404 !important;
        border: 2px solid var(--warning-color) !important; /* Restore border */
        font-size: 0.75rem !important;
        padding: 0.25rem 0.5rem !important;
        height: auto !important; /* Let it size naturally */
        margin: 0 !important;
        width: auto !important; /* Only as wide as content */
        display: inline-flex !important; /* Inline flex to fit content */
        align-items: center !important;
        gap: 0.25rem !important;
        flex-shrink: 0 !important;
        white-space: nowrap !important; /* Prevent text wrapping */
    }
    
    .header-council-notice .council-notice-icon {
        font-size: 0.9rem !important;
    }
    
    
    /* Hide the "Response Style:" label completely */
    .prompt-selector-row label {
        display: none !important;
    }
    
    #prompt-selector {
        flex: 1 !important;
        min-height: 40px !important; /* Slightly smaller */
        padding: 6px 10px !important; /* Reduced padding */
        border: 2px solid var(--medium-gray, #e9ecef) !important;
        border-radius: 12px !important;
        font-size: 16px !important;
        background: white;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        background-image: url('data:image/svg+xml;charset=US-ASCII,<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12"><path fill="%236c757d" d="M6 8L2 4h8L6 8z"/></svg>');
        background-repeat: no-repeat;
        background-position: right 12px center;
        padding-right: 40px !important;
        margin: 0 !important;
    }
    
    .input-status {
        font-size: 11px; /* Slightly smaller */
        color: var(--dark-gray, #6c757d);
        text-align: left;
        min-height: 14px; /* Reduced height */
    }
    
    /* Sources panel - collapsible and touch-friendly */
    .sources-panel {
        order: 1 !important;
        width: 100% !important;
        background: white;
        border-radius: 12px;
        margin-bottom: 0.5rem; /* Reduced margin */
        overflow: visible; /* Allow expansion beyond container */
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        transition: all 0.3s ease;
        min-height: auto;
    }
    
    /* Dynamic container height based on expanded sections */
    @media screen and (max-width: 480px) {
        .sources-panel {
            height: auto !important;
            max-height: none !important;
        }
        
        /* When any section is expanded, allow dynamic height */
        .sources-panel:has(.expanded) {
            min-height: 400px !important; /* Space for any expanded section */
        }
    }
    
    .category-filter,
    .topic-filter,
    .sources-section {
        padding: 12px 16px 4px 16px;
        border-bottom: 1px solid var(--medium-gray, #e9ecef);
        transition: all 0.3s ease;
    }
    
    /* Collapsible header with toggle button for all sections */
    .category-filter h4,
    .topic-filter h4,
    .sources-section h3 {
        margin: 0;
        font-size: 16px;
        font-weight: 600;
        color: var(--text-color, #333);
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        cursor: pointer !important;
        padding: 8px 0 !important;
        min-height: 44px !important;
        -webkit-tap-highlight-color: rgba(0, 99, 65, 0.1);
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }
    
    /* Toggle icon */
    .category-filter h4::after,
    .topic-filter h4::after,
    .sources-section h3::after {
        content: '▼' !important;
        font-size: 12px !important;
        color: var(--primary-color, #006341) !important;
        transition: transform 0.2s ease !important;
        margin-left: 8px !important;
    }
    
    /* Collapsed state icon */
    .category-filter.collapsed h4::after,
    .topic-filter.collapsed h4::after,
    .sources-section.collapsed h3::after {
        transform: rotate(-90deg) !important;
    }
    
    .category-options, 
    .topic-filter > div:not(h4), 
    .sources-content {
        max-height: 120px;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        transition: max-height 0.3s ease, opacity 0.2s ease;
        opacity: 1;
    }
    
    /* Collapsed state */
    .category-filter.collapsed .category-options,
    .topic-filter.collapsed > div:not(h4),
    .sources-section.collapsed .sources-content {
        max-height: 0 !important;
        opacity: 0 !important;
        overflow: hidden !important;
    }
    
    /* Force collapsed state initially on mobile */
    @media screen and (max-width: 480px) {
        .category-options, 
        .topic-filter > div:not(h4), 
        .sources-content {
            max-height: 0 !important;
            opacity: 0 !important;
            overflow: hidden !important;
        }
        
        .category-filter h4::after,
        .topic-filter h4::after,
        .sources-section h3::after {
            transform: rotate(-90deg) !important;
        }
        
        /* When expanded - overlay part of chat window */
        .category-filter.expanded,
        .topic-filter.expanded,
        .sources-section.expanded {
            position: relative !important;
            z-index: 200 !important;
            background: white !important;
            border-radius: 12px !important;
            box-shadow: 0 8px 32px rgba(0,0,0,0.2) !important;
            border: 1px solid var(--medium-gray, #e9ecef) !important;
            margin: 4px 0 !important;
        }
        
        .category-filter.expanded .category-options,
        .topic-filter.expanded > div:not(h4),
        .sources-section.expanded .sources-content {
            max-height: 300px !important; /* Much larger when expanded */
            opacity: 1 !important;
            overflow-y: auto !important;
            padding-bottom: 12px !important;
            border-radius: 0 0 12px 12px !important;
        }
        
        .category-filter.expanded h4::after,
        .topic-filter.expanded h4::after,
        .sources-section.expanded h3::after {
            transform: rotate(0deg) !important;
        }
    }
    
    .category-checkbox {
        display: flex !important;
        align-items: center !important;
        gap: 12px !important;
        padding: 8px 0 !important;
        min-height: 44px !important;
        font-size: 14px !important;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }
    
    .category-checkbox input[type="checkbox"] {
        width: 20px !important;
        height: 20px !important;
        margin: 0 !important;
        -webkit-appearance: none;
        appearance: none;
        border: 2px solid var(--medium-gray, #e9ecef);
        border-radius: 4px;
        position: relative;
        background: white;
    }
    
    .category-checkbox input[type="checkbox"]:checked {
        background: var(--primary-color, #006341);
        border-color: var(--primary-color, #006341);
    }
    
    .category-checkbox input[type="checkbox"]:checked::after {
        content: '✓';
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        color: white;
        font-size: 12px;
        font-weight: bold;
    }
    
    .sources-section {
        padding: 12px 16px 4px 16px;
        transition: all 0.3s ease;
    }
    
    /* Collapsible Sources header with toggle button */
    .sources-section h3 {
        margin: 0;
        font-size: 16px;
        font-weight: 600;
        color: var(--text-color, #333);
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        cursor: pointer !important;
        padding: 8px 0 !important;
        min-height: 44px !important;
        -webkit-tap-highlight-color: rgba(0, 99, 65, 0.1);
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }
    
    /* Toggle icon for Sources */
    .sources-section h3::after {
        content: '▼' !important;
        font-size: 12px !important;
        color: var(--primary-color, #006341) !important;
        transition: transform 0.2s ease !important;
        margin-left: 8px !important;
    }
    
    /* Collapsed state icon for Sources */
    .sources-section.collapsed h3::after {
        transform: rotate(-90deg) !important;
    }
    
    .sources-content {
        max-height: 200px;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        transition: max-height 0.3s ease, opacity 0.2s ease;
        opacity: 1;
    }
    
    /* Collapsed state for Sources content */
    .sources-section.collapsed .sources-content {
        max-height: 0 !important;
        opacity: 0 !important;
        overflow: hidden !important;
    }
    
    /* Force Sources section collapsed initially on mobile */
    @media screen and (max-width: 480px) {
        .sources-content {
            max-height: 0 !important;
            opacity: 0 !important;
            overflow: hidden !important;
        }
        
        .sources-section h3::after {
            transform: rotate(-90deg) !important;
        }
        
        /* When Sources expanded - overlay part of chat window */
        .sources-section.expanded {
            position: relative !important;
            z-index: 200 !important;
            background: white !important;
            border-radius: 12px !important;
            box-shadow: 0 8px 32px rgba(0,0,0,0.2) !important;
            border: 1px solid var(--medium-gray, #e9ecef) !important;
            margin: 4px 0 !important;
        }
        
        .sources-section.expanded .sources-content {
            max-height: 350px !important; /* Much larger for sources */
            opacity: 1 !important;
            overflow-y: auto !important;
            padding-bottom: 12px !important;
            border-radius: 0 0 12px 12px !important;
        }
        
        .sources-section.expanded h3::after {
            transform: rotate(0deg) !important;
        }
    }
    
    .no-sources {
        font-size: 14px;
        color: var(--dark-gray, #6c757d);
        text-align: left;
        padding: 20px 0;
        margin: 0;
    }
    
    /* Chat messages - enhanced for mobile reading */
    .chat-messages {
        padding: 16px !important;
        padding-bottom: 20px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        scroll-behavior: smooth;
    }
    
    .welcome-message {
        background: var(--primary-light, #e8f5f0) !important;
        border-radius: 12px !important;
        padding: 20px !important;
        margin-bottom: 20px !important;
        text-align: left;
    }
    
    .welcome-message p {
        margin: 0 0 8px 0 !important;
        font-size: 16px;
        line-height: 1.5;
    }
    
    .welcome-message p:last-child {
        margin-bottom: 0 !important;
    }
    
    .welcome-message strong {
        color: var(--primary-color, #006341);
        font-weight: 600;
    }
    
    /* Message bubbles - iOS-style design */
    .message {
        margin-bottom: 16px !important;
        padding: 16px 20px !important;
        border-radius: 20px !important;
        max-width: 85% !important;
        font-size: 16px !important;
        line-height: 1.4 !important;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }
    
    .message.user {
        background: var(--primary-color, #006341) !important;
        color: white !important;
        border-radius: 20px 6px 20px 20px !important;
        margin-left: auto !important;
        margin-right: 0 !important;
    }
    
    .message.bot {
        background: var(--light-gray, #f8f9fa) !important;
        color: var(--text-color, #333) !important;
        border-radius: 6px 20px 20px 20px !important;
        border: 1px solid var(--medium-gray, #e9ecef);
        margin-left: 0 !important;
        margin-right: auto !important;
    }
    
    .message a {
        color: inherit !important;
        text-decoration: underline !important;
        -webkit-tap-highlight-color: rgba(255, 255, 255, 0.2);
    }
    
    .message.user a {
        color: rgba(255, 255, 255, 0.9) !important;
    }
    
    /* Message content formatting */
    .message p {
        margin: 0 0 8px 0 !important;
    }
    
    .message p:last-child {
        margin-bottom: 0 !important;
    }
    
    .message ul, .message ol {
        margin: 8px 0 !important;
        padding-left: 20px !important;
    }
    
    .message li {
        margin-bottom: 4px !important;
    }
    
    .message code {
        background: rgba(0, 0, 0, 0.1) !important;
        padding: 2px 6px !important;
        border-radius: 4px !important;
        font-family: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, 'Courier New', monospace !important;
        font-size: 14px !important;
    }
    
    .message.user code {
        background: rgba(255, 255, 255, 0.2) !important;
    }
    
    /* Form elements - full width */
    #user-input {
        width: 100%;
        min-height: 44px;
        padding: 12px 16px;
        border-radius: 22px;
        border: 2px solid var(--medium-gray, #e9ecef);
        font-size: 16px;
        resize: none;
    }
    
    /* Buttons - touch friendly */
    .send-button, .btn {
        min-height: 44px;
        padding: 12px 24px;
        font-size: 16px;
        border-radius: 22px;
        margin-top: 0.5rem;
        width: 100%;
    }
    
    /* Admin forms - stack vertically */
    .form-group {
        margin-bottom: 1rem;
    }
    
    .form-group label {
        display: block;
        margin-bottom: 0.5rem;
        font-weight: 500;
    }
    
    /* Tables - horizontal scroll */
    .table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    table {
        min-width: 600px;
    }
    
    /* Users table container - ensure proper scrolling */
    .users-table-container {
        overflow-x: auto !important;
        overflow-y: auto !important;
        max-height: 400px !important; /* Smaller on mobile */
        -webkit-overflow-scrolling: touch;
    }
    
    /* Convert users table to cards on mobile */
    .admin-table:not(.prompts-table) {
        display: block !important;
        min-width: unset !important;
    }
    
    .admin-table:not(.prompts-table) thead {
        display: none !important;
    }
    
    .admin-table:not(.prompts-table) tbody {
        display: block !important;
    }
    
    .admin-table:not(.prompts-table) tr {
        display: block !important;
        background: white;
        border: 1px solid #ddd;
        border-radius: 8px;
        margin-bottom: 12px;
        padding: 12px 12px 60px 12px; /* Add bottom padding for buttons */
        position: relative;
        min-height: 80px; /* Make cards taller */
    }
    
    .admin-table:not(.prompts-table) td {
        display: block !important;
        border: none !important;
        padding: 0 !important;
        text-align: left !important;
        position: static;
    }
    
    /* Username - prominent at top left (users table only) */
    .admin-table:not(.prompts-table) td:nth-child(1) {
        font-size: 16px;
        margin-bottom: 8px;
        padding-right: 100px !important; /* Space for buttons */
    }
    
    /* Admin badge styling on mobile (users table only) */
    .admin-table:not(.prompts-table) .admin-badge {
        display: inline-block !important;
        font-size: 10px !important;
        padding: 2px 6px !important;
        margin-left: 8px !important;
        vertical-align: middle;
    }
    
    /* Role - small text below username (users table only) */
    .admin-table:not(.prompts-table) td:nth-child(2) {
        font-size: 12px;
        color: #666;
        margin-bottom: 4px;
    }
    
    .admin-table:not(.prompts-table) td:nth-child(2)::before {
        content: "Role: ";
        font-weight: 600;
    }
    
    /* Created date - small text below role (users table only) */
    .admin-table:not(.prompts-table) td:nth-child(3) {
        font-size: 12px;
        color: #666;
        margin-bottom: 4px;
    }
    
    .admin-table:not(.prompts-table) td:nth-child(3)::before {
        content: "Created: ";
        font-weight: 600;
    }
    
    /* Last login - small text below created date (users table only) */
    .admin-table:not(.prompts-table) td:nth-child(4) {
        font-size: 12px;
        color: #666;
        margin-bottom: 8px;
    }
    
    .admin-table:not(.prompts-table) td:nth-child(4)::before {
        content: "Last Login: ";
        font-weight: 600;
    }
    
    /* Action buttons - positioned at bottom as a row (users table only) */
    .admin-table:not(.prompts-table) td:nth-child(5) {
        position: absolute !important;
        bottom: 12px !important;
        left: 12px !important;
        right: 12px !important;
        display: flex !important;
        flex-direction: row !important;
        gap: 4px !important;
        justify-content: space-between !important;
    }
    
    /* Make buttons smaller and arranged in a row on mobile (users table only) */
    .admin-table:not(.prompts-table) .action-button.small,
    .admin-table:not(.prompts-table) .danger-button.small {
        font-size: 10px !important;
        padding: 4px 8px !important; /* Reduced height, slightly more width for row layout */
        min-width: unset !important;
        width: auto !important;
        flex: 1 !important; /* Equal width distribution */
        margin: 0 !important;
        text-transform: none !important;
        white-space: nowrap !important; /* Prevent text wrapping in row layout */
        line-height: 1.1 !important; /* Very tight line spacing */
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: none !important; /* Remove max-width for flex sizing */
        min-height: 24px !important; /* Reduced height */
        height: 24px !important; /* Fixed height for consistency */
    }
    
    /* Prompts table mobile styling */
    .prompts-table {
        display: block !important;
        min-width: unset !important;
    }
    
    .prompts-table thead {
        display: none !important;
    }
    
    .prompts-table tbody {
        display: block !important;
    }
    
    .prompts-table tr {
        display: block !important;
        background: white;
        border: 1px solid #ddd;
        border-radius: 8px;
        margin-bottom: 12px;
        padding: 12px 12px 40px 12px; /* Add bottom padding for button */
        position: relative;
    }
    
    .prompts-table td {
        display: block !important;
        border: none !important;
        padding: 0 !important;
        text-align: left !important;
        position: static;
    }
    
    /* Prompt name - prominent at top */
    .prompts-table td:nth-child(1) {
        font-size: 16px;
        font-weight: 600;
        margin-bottom: 8px;
        padding-right: 80px !important; /* Space for actions */
    }
    
    /* Description - normal text below name */
    .prompts-table td:nth-child(2) {
        font-size: 14px;
        color: #333;
        margin-bottom: 8px;
        line-height: 1.4;
    }
    
    /* Default indicator - inline with metadata, only show if present */
    .prompts-table td:nth-child(3) {
        font-size: 12px;
        margin-bottom: 4px;
        display: inline-block !important;
        margin-right: 12px;
    }
    
    .prompts-table td:nth-child(3):empty {
        display: none !important;
    }
    
    .prompts-table .status-active {
        color: var(--success-color, #27ae60);
        font-weight: 600;
    }
    
    /* Bypass indicator - inline with metadata (4th column in 6-column layout) */
    .prompts-table td:nth-child(4) {
        font-size: 12px;
        margin-bottom: 4px;
        display: inline-block !important;
    }
    
    .prompts-table .protected-label {
        font-size: 11px;
        color: #999;
        font-weight: normal;
    }
    
    /* Actions - positioned at bottom right */
    .prompts-table td:nth-child(5) {
        position: absolute !important;
        bottom: 12px !important;
        right: 12px !important;
        display: flex !important;
        gap: 6px !important;
    }
    
    .prompts-table .action-buttons {
        display: flex !important;
        gap: 6px;
        flex-direction: row;
    }

    .prompts-table .action-buttons button {
        font-size: 13px;
        padding: 6px 10px;
        min-height: 32px;
    }
    
    /* Upload section - ensure enough height on mobile */
    .collapsible-section:not(.collapsed) .collapsible-content {
        max-height: 3000px !important; /* Extra height for mobile forms */
    }
    
    .upload-section:not(.collapsed) .collapsible-content {
        padding-bottom: 30px; /* Extra padding to ensure button is visible */
    }
    
    /* When collapsed, ensure no padding or overflow */
    .collapsible-section.collapsed .collapsible-content {
        padding-bottom: 0 !important;
        max-height: 0 !important;
        overflow: hidden !important;
    }
    
    /* Documents Summary - mobile styling */
    .documents-summary {
        padding: 12px;
        margin-bottom: 12px;
        display: block;
    }
    
    .documents-summary .summary-stat {
        display: block;
        font-size: 11px;
        color: #666;
        line-height: 1.6;
        white-space: normal;
    }
    
    .documents-summary .summary-stat:not(:last-child)::after {
        display: none;
    }
    
    /* Document table - convert to mobile cards */
    .documents-table {
        display: block !important;
        min-width: unset !important;
    }
    
    .documents-table thead {
        display: none !important;
    }
    
    .documents-table tbody {
        display: block !important;
    }
    
    .documents-table tr, .document-row {
        display: block !important;
        background: white;
        border: 1px solid #ddd;
        border-radius: 8px;
        margin-bottom: 12px;
        padding: 16px;
        padding-top: 60px;
        position: relative;
    }

    /* Hidden rows should still be hidden */
    .documents-table tr[style*="display: none"], .document-row[style*="display: none"] {
        display: none !important;
    }
    
    .documents-table td {
        display: block !important;
        border: none !important;
        padding: 4px 0 !important;
        text-align: left !important;
        position: static;
    }
    
    /* Show checkbox column on mobile - positioned at top left */
    .documents-table td:nth-child(1) {
        display: block !important;
        position: absolute;
        top: 16px;
        left: 16px;
        padding: 0 !important;
    }
    
    .documents-table td:nth-child(1) input[type="checkbox"] {
        width: 20px;
        height: 20px;
        cursor: pointer;
    }
    
    /* Title - prominent at top with space for checkbox */
    .documents-table td:nth-child(2) {
        margin-bottom: 4px;
        padding-bottom: 4px;
        border-bottom: 1px solid #eee;
        padding-left: 16px;
        padding-right: 16px;
        margin-top: 2px; /* Minimal top margin to bring title very close to category */
        width: 100% !important;
        box-sizing: border-box !important;
    }
    
    /* Ensure title text and metadata use full width */
    .documents-table td:nth-child(2) strong,
    .documents-table td:nth-child(2) small {
        width: 100% !important;
        display: block !important;
        box-sizing: border-box !important;
        word-wrap: break-word !important;
    }
    
    /* Category badge - top left corner, after checkbox */
    .documents-table td:nth-child(3) {
        position: absolute;
        top: 16px;
        left: 45px; /* Space for checkbox */
        right: 16px; /* Ensure space from right edge */
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }
    
    /* Category badge styling on mobile */
    .documents-table td:nth-child(3) .category-badge {
        font-size: 10px !important;
        padding: 2px 6px !important;
        white-space: nowrap !important;
        display: inline-block !important;
        max-width: 100% !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }
    
    /* Action buttons - bottom right corner */
    .documents-table td:nth-child(6) {
        position: absolute;
        bottom: 10px;
        right: 16px;
    }
    
    /* Date - inline, no wrapping */
    .documents-table td:nth-child(4) {
        display: inline-block !important;
        font-size: 12px;
        color: #666;
        margin-right: 12px;
        margin-bottom: 40px; /* Space for bottom-right buttons */
        white-space: nowrap;
        vertical-align: top;
    }
    
    /* Topics - allow wrapping, use full width */
    .documents-table td:nth-child(5) {
        display: block !important;
        font-size: 12px;
        color: #666;
        margin-bottom: 40px; /* Space for bottom-right buttons */
        margin-top: -20px; /* Move topics very close to date */
        width: 100% !important;
        word-wrap: break-word !important;
        white-space: normal !important;
        clear: both;
    }
    
    .documents-table td:nth-child(4)::before {
        content: "📅 ";
        font-size: 12px;
    }
    
    .documents-table td:nth-child(5)::before {
        content: "🏷️ ";
        font-size: 12px;
    }
    
    /* Action buttons mobile sizing */
    .documents-table .action-buttons {
        display: flex !important;
        gap: 6px;
        flex-direction: row;
    }
    
    .documents-table .action-buttons button {
        font-size: 13px;
        padding: 6px 10px;
        min-height: 32px;
    }
    
    /* Rebuild Search Index button - smaller on mobile */
    #rebuild-index {
        font-size: 13px;
        padding: 8px 12px;
        white-space: nowrap;
    }
    
    /* Bulk topic button - smaller on mobile */
    #bulk-topic-btn {
        font-size: 13px;
        padding: 8px 12px;
        white-space: nowrap;
    }
    
    .section-header {
        flex-direction: column;
        gap: 12px;
        align-items: flex-start;
    }
    
    .section-header h2 {
        margin-bottom: 0;
    }
    
    /* Document filters - mobile responsive */
    .document-filters {
        margin: 0.5rem 0;
        padding: 0.75rem;
    }
    
    .filter-row {
        flex-direction: column;
        gap: 0.75rem;
        align-items: stretch;
    }
    
    .filter-group {
        width: 100%;
    }
    
    .filter-select {
        width: 100%;
        min-width: unset;
    }
    
    /* Ensure word count font size matches the filename/link text */
    .documents-table td:nth-child(2) small {
        font-size: 12px;
    }
    
    /* Navigation - hide complex nav, show mobile alternative */
    .desktop-nav {
        display: none;
    }
    
    /* Status indicators */
    .mode-indicator {
        font-size: 0.8rem;
        padding: 0.5rem;
        height: 36px;
    }

    /* =============================================
       COUNCIL MEETING PROCESSING - Mobile Layout
       ============================================= */

    /* Step 1: Fix container height - council page scrolls vertically, not chat-style */
    body:has(.council-page-layout) .container {
        height: auto !important;
        min-height: 100vh;
    }

    /* Tighten up the two-column stacking for phone screens */
    .council-page-layout {
        padding: 0 !important;
        gap: 1rem !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    .council-left-column,
    .council-right-column {
        width: 100% !important;
    }

    /* Push history below the form content on mobile */
    .council-right-column {
        order: 2;
    }

    /* Step 2: Tab bar - horizontal scroll on small screens */
    .tab-bar {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        gap: 0 !important;
        padding-bottom: 0.25rem;
        scrollbar-width: none; /* Firefox */
    }

    .tab-bar::-webkit-scrollbar {
        display: none; /* Chrome/Safari */
    }

    .tab-button {
        flex: 1 !important;
        padding: 0.6rem 0.5rem !important;
        font-size: 0.75rem !important;
        min-height: 44px;
        white-space: normal !important;
        text-align: center !important;
        line-height: 1.3;
    }

    /* Step 3: Council form inputs - prevent iOS zoom & proper touch targets */
    .upload-section input[type="file"] {
        min-height: 44px !important;
        font-size: 16px !important;
        padding: 10px !important;
    }

    .upload-section .recap-textarea {
        min-height: 150px !important;
        font-size: 16px !important;
        padding: 12px !important;
    }

    /* Upload section card - reduce padding on mobile */
    .upload-section {
        padding: 1rem !important;
    }

    /* Process buttons - full width on mobile */
    .process-button {
        width: 100% !important;
        min-height: 48px !important;
        font-size: 16px !important;
        padding: 12px !important;
        border-radius: 8px !important;
    }

    /* Step 4: Council history table - card layout on mobile */
    .history-table {
        display: block !important;
        min-width: unset !important;
    }

    .history-table thead {
        display: none !important;
    }

    .history-table tbody {
        display: block !important;
    }

    .history-table tr {
        display: block !important;
        background: white;
        border: 1px solid var(--medium-gray, #e9ecef);
        border-radius: 8px;
        margin-bottom: 10px;
        padding: 12px;
    }

    .history-table td {
        display: block !important;
        border: none !important;
        padding: 0 !important;
    }

    .filename-cell {
        gap: 0.25rem !important;
    }

    .filename-cell .download-link {
        font-size: 14px;
        font-weight: 600;
        word-break: break-word;
    }

    .file-metadata {
        font-size: 0.75rem !important;
    }

    .output-type-badge {
        font-size: 0.6em !important;
    }

    .history-section {
        padding: 1rem !important;
    }

    .history-section h2 {
        font-size: 1rem !important;
    }

    /* Step 5: Collapsible sections - mobile touch & text enhancements */
    .council-collapsible-header {
        -webkit-tap-highlight-color: rgba(0, 99, 65, 0.1);
        min-height: 44px;
    }

    .council-collapsible-content ul {
        font-size: 0.8rem !important;
        font-style: italic !important;
        color: var(--dark-gray, #6c757d) !important;
    }

    /* Step 6: Council status messages - mobile polish */
    .upload-section .status-message {
        padding: 10px 12px !important;
        font-size: 13px !important;
        border-radius: 8px !important;
        line-height: 1.4 !important;
        word-break: break-word;
    }

    .upload-section .spinner {
        width: 14px !important;
        height: 14px !important;
        margin-right: 6px !important;
        vertical-align: middle;
    }

    /* =============================================
       MODAL BOTTOM SHEET - Mobile Layout
       ============================================= */

    /* All modals → bottom sheet on mobile */
    .modal {
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        top: auto !important;
        transform: none !important;
        max-height: 85vh !important;
        width: 100% !important;
        border-radius: 16px 16px 0 0 !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .modal-content,
    .modal-content.small,
    .modal-content.large {
        width: 100% !important;
        max-width: 100% !important;
        border-radius: 16px 16px 0 0 !important;
    }

    .modal-header {
        position: sticky;
        top: 0;
        z-index: 10;
        border-radius: 16px 16px 0 0 !important;
    }

    .modal-footer {
        position: sticky;
        bottom: 0;
        z-index: 10;
    }

    /* =============================================
       FORM FIELD CONSISTENCY - Site-Wide
       ============================================= */

    /* Universal form field consistency - prevent iOS zoom everywhere */
    .form-group input[type="text"],
    .form-group input[type="password"],
    .form-group input[type="email"],
    .form-group input[type="url"],
    .form-group input[type="number"],
    .form-group input[type="date"],
    .form-group select,
    .form-group textarea,
    .modal input[type="text"],
    .modal input[type="password"],
    .modal select,
    .modal textarea {
        font-size: 16px !important;
        min-height: 44px !important;
        padding: 10px 12px !important;
        border-radius: 8px !important;
        box-sizing: border-box !important;
        width: 100% !important;
    }

    /* =============================================
       UPLOAD MEDIA PAGE - Mobile Layout
       ============================================= */

    .upload-container {
        padding: 1rem !important;
        margin: 0.5rem !important;
        border-radius: 8px !important;
    }

    .upload-main {
        padding: 0.5rem 0 !important;
    }

    .progress-bar {
        height: 24px !important;
        border-radius: 12px !important;
    }

}

/* ==========================================================================
   IPAD LAYOUT (768px - 1024px) - Optimized Two-Column
   ========================================================================== */
@media screen and (min-width: 768px) and (max-width: 1024px) {
    
    /* Container adjustments */
    .container {
        max-width: 100%;
        padding: 0 1rem;
    }
    
    /* Header - better spacing */
    header {
        padding: 1rem 2rem;
    }
    
    .header-content h1 {
        font-size: 1.4rem;
    }
    
    /* Main layout - optimized two-column */
    main {
        flex-direction: row;
        gap: 1.5rem;
        padding: 1rem;
    }
    
    /* Chat container - primary content area */
    .chat-container {
        flex: 1;
        min-width: 0; /* Allow flex shrinking */
    }
    
    /* Chat messages - better height for tablet */
    .chat-messages {
        flex: 1; /* Use flex instead of fixed height */
        min-height: 450px;
        overflow-y: auto;
    }
    
    /* Welcome message - green background like iPhone */
    .welcome-message {
        background: var(--primary-light, #e8f5f0) !important;
        border-radius: 12px !important;
        padding: 20px !important;
        margin-bottom: 20px !important;
        text-align: left;
        max-width: 85% !important;
    }
    
    .welcome-message strong {
        color: var(--primary-color, #006341);
        font-weight: 600;
    }
    
    /* Sources panel - sticky sidebar */
    .sources-panel {
        width: 320px !important;
        position: sticky;
        top: 1rem;
        height: fit-content;
        max-height: calc(100vh - 150px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        flex-shrink: 0;
    }
    
    /* Input area - proper sizing */
    .input-area, .chat-form {
        padding: 1.5rem;
    }
    
    #user-input {
        min-height: 44px;
        padding: 12px 16px;
        font-size: 16px;
    }
    
    /* Buttons - appropriate sizing */
    .send-button, .btn {
        min-height: 44px;
        padding: 12px 20px;
        font-size: 16px;
    }
    
    /* Admin interface - two-column forms */
    .admin-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 2rem;
    }
    
    
    /* Modal dialogs - centered and sized */
    .modal {
        max-width: 600px;
        margin: 2rem auto;
    }
}

/* ==========================================================================
   iOS SAFARI SPECIFIC OPTIMIZATIONS
   ========================================================================== */
@media screen and (max-width: 1024px) {
    
    /* iOS Safari viewport fixes */
    html {
        height: 100%;
    }
    
    body {
        min-height: 100vh;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
    
    .container {
        min-height: 100vh;
    }
    
    /* For supported browsers, use fill-available */
    @supports (height: 100svh) {
        html {
            height: 100svh;
            height: -webkit-fill-available;
        }
        body {
            min-height: 100svh;
            min-height: -webkit-fill-available;
        }
        .container {
            min-height: 100svh;
            min-height: -webkit-fill-available;
        }
    }
    
    /* Prevent iOS bounce/overscroll */
    .chat-messages, .sources-content, .category-options {
        -webkit-overscroll-behavior: contain;
        -ms-overscroll-behavior: contain;
        overscroll-behavior: contain;
    }
    
    /* Remove hover effects on touch devices */
    @media (hover: none) {
        .btn:hover, button:hover, .nav-link:hover, .menu-item:hover {
            background-color: initial;
            transform: none;
        }
    }
    
    /* Enhanced tap targets for touch */
    .clickable, .btn, button, a, input, select, textarea, .category-checkbox {
        -webkit-tap-highlight-color: rgba(0, 99, 65, 0.1);
        cursor: pointer;
    }
    
    /* iOS momentum scrolling */
    * {
        -webkit-overflow-scrolling: touch;
    }
    
    /* Prevent text selection on UI elements */
    .btn, button, .header-controls, .mode-indicator, .menu-item {
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        -webkit-touch-callout: none;
    }
    
    /* Allow text selection in messages */
    .message, .welcome-message {
        -webkit-user-select: text;
        -moz-user-select: text;
        -ms-user-select: text;
        user-select: text;
    }
    
    /* iOS keyboard handling */
    .chat-input {
        /* Prevent layout shifting when keyboard appears */
        transform: translateZ(0);
        -webkit-transform: translateZ(0);
    }
    
    /* Improved focus states for accessibility */
    input:focus, textarea:focus, select:focus, button:focus {
        outline: 2px solid var(--primary-color, #006341);
        outline-offset: 2px;
    }
    
    /* iOS-style loading states */
    button:disabled, .btn:disabled {
        opacity: 0.6;
        cursor: not-allowed;
        transform: none !important;
    }
    
    /* Toast notifications - mobile positioning */
    .toast, .notification, .alert {
        position: fixed !important;
        bottom: 120px !important; /* Above mobile input area */
        left: 16px !important;
        right: 16px !important;
        width: auto !important;
        z-index: 9999 !important;
        border-radius: 12px !important;
        box-shadow: 0 4px 20px rgba(0,0,0,0.15) !important;
    }
    
    /* Safe area insets for iPhone X+ */
    @supports (padding: max(0px)) {
        .container {
            padding-left: max(16px, env(safe-area-inset-left));
            padding-right: max(16px, env(safe-area-inset-right));
        }
        
        .chat-input {
            padding-bottom: max(16px, env(safe-area-inset-bottom));
        }
        
        .user-menu-dropdown {
            padding-bottom: max(16px, env(safe-area-inset-bottom)) !important;
        }
    }
}

/* ==========================================================================
   LANDSCAPE ORIENTATION ADJUSTMENTS
   ========================================================================== */
@media screen and (max-width: 926px) and (orientation: landscape) {
    
    /* Reduce header height in landscape */
    header {
        padding: 0.75rem 1rem;
    }
    
    .header-content h1 {
        font-size: 1.2rem;
    }
    
    /* Optimize chat height for landscape */
    .chat-messages {
        height: calc(100vh - 160px) !important;
    }
    
    /* Compact input area */
    .input-area, .chat-form {
        padding: 0.75rem 1rem;
    }
    
    /* Sources panel - reduce max height */
    .sources-panel {
        max-height: 120px !important;
    }
}

/* ==========================================================================
   ACCESSIBILITY IMPROVEMENTS
   ========================================================================== */
@media screen and (max-width: 1024px) {
    
    /* Larger text for better readability */
    @media (prefers-reduced-motion: no-preference) {
        .message, .chat-messages {
            scroll-behavior: smooth;
        }
    }
    
    /* High contrast mode support removed for compatibility */
    
    /* Reduced motion preference */
    @media (prefers-reduced-motion: reduce) {
        * {
            animation-duration: 0.01ms !important;
            animation-iteration-count: 1 !important;
            transition-duration: 0.01ms !important;
        }
    }
}

/* ==========================================================================
   PRINT STYLES (Mobile-Friendly)
   ========================================================================== */
@media print {
    .sources-panel, .input-area, .chat-form, header, .header-controls {
        display: none;
    }
    
    .chat-messages {
        height: auto !important;
        overflow: visible !important;
    }
    
    .message {
        break-inside: avoid;
        margin-bottom: 1rem;
    }
}

/* Mobile - larger logo for iPhone */
@media screen and (max-width: 480px) {
    .login-container {
        margin-top: 60px !important;
    }
    
    .login-logo {
        top: -100px !important;
    }
    
    .logo-image {
        max-width: 160px !important;
        width: 160px !important;
        padding: 10px !important;
    }
}

/* iPad and tablets */
@media screen and (min-width: 481px) and (max-width: 1024px) {
    .login-container {
        margin-top: 80px !important;
    }
    
    .login-logo {
        top: -120px !important;
    }
    
    .logo-image {
        max-width: 150px !important;
        width: 150px !important;
        padding: 10px !important;
    }
}

/* iPad landscape and small laptops */
@media screen and (min-width: 1025px) and (max-height: 900px) {
    .login-container {
        margin-top: 50px !important;
    }
    
    .login-logo {
        top: -80px !important;
    }
    
    .logo-image {
        max-width: 120px !important;
        width: 120px !important;
        padding: 8px !important;
    }
}

/* Topic Filtering Mobile Styles */
@media screen and (max-width: 1024px) {
    /* Make filter tabs full width on mobile */
    .filter-tabs {
        position: sticky;
        top: 0;
        z-index: 10;
        background: white;
        padding: 0.5rem;
        margin: -0.5rem -0.5rem 1rem;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    
    /* Adjust topic list height for mobile */
    .topic-list {
        max-height: 200px;
    }
    
    /* Make topic items larger for touch */
    .topic-item {
        padding: 0.75rem;
    }
    
    /* Larger touch targets for remove buttons */
    .topic-tag .remove-topic {
        padding: 0.25rem;
        margin-left: 0.5rem;
    }
    
    /* Stack toggle controls on small screens */
    .topic-mode-toggle {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
    }
    
    .toggle-switch {
        justify-content: center;
    }
    
    /* Full width search on mobile */
    .topic-search input {
        padding: 0.75rem;
        font-size: 16px; /* Prevent zoom on iOS */
    }
}

/* iPad specific adjustments */
@media screen and (min-width: 768px) and (max-width: 1024px) {
    .topic-list {
        max-height: 400px;
    }
    
    .filter-tabs {
        margin-bottom: 1.5rem;
    }
}