/**
 * Table Header Styling - Modern CSS with Design System
 *
 * This file provides consistent, maintainable styling for table headers
 * using CSS custom properties and modern layout techniques.
 */

:root {
    /* Design System Colors */
    --color-primary: #25af88;
    --color-primary-hover: #1e9a73;
    --color-primary-focus: rgba(37, 175, 136, 0.1);
    --color-gray-50: #f9fafb;
    --color-gray-100: #f3f4f6;
    --color-gray-200: #e5e7eb;
    --color-gray-300: #d1d5db;
    --color-gray-600: #4b5563;
    --color-gray-700: #374151;
    --color-gray-900: #111827;
    --color-white: #ffffff;
    --color-red-500: #ef4444;

    /* Spacing System */
    --space-xs: 0.25rem; /* 4px */
    --space-sm: 0.5rem; /* 8px */
    --space-md: 0.75rem; /* 12px */
    --space-lg: 1rem; /* 16px */
    --space-xl: 1.25rem; /* 20px */
    --space-2xl: 1.5rem; /* 24px */

    /* Border Radius */
    --radius-sm: 6px;
    --radius-md: 12px;

    /* Shadows */
    --shadow-sm: 0 1px 0.5px 0.05px rgba(29, 41, 61, 0.02);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-focus: 0 0 0 3px rgba(37, 175, 136, 0.1);

    /* Transitions */
    --transition-fast: 0.15s ease;
    --transition-normal: 0.2s ease;

    /* Typography */
    --font-size-xs: 12px;
    --font-size-sm: 14px;
    --font-weight-medium: 500;
}

/* Table Header Container.
   ``flex-wrap: nowrap`` keeps the top pagination on the same row as the
   search/filters form regardless of how the rails are toggled or the
   viewport is resized — the previous ``wrap`` would drop pagination
   under the search whenever the parent narrowed past the combined
   intrinsic width. Mobile fallback to column layout still happens via
   the 768px media query below. */
.table-header {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: var(--space-lg);
    padding: var(--space-xl);
    border-radius: var(--radius-md) var(--radius-md) 0 0;
    background: #fff;
    flex-wrap: nowrap;
}

/* Form Container - all elements on same line */
.table-search-form {
    display: flex;
    align-items: flex-end;
    gap: 12px;
}

/* Search Section - Removed redundant wrapper */

.search-input-group {
    display: flex;
    /* Was 420px — reduced ~14% so the search-form + top pagination fit
       on one row across the rail-toggle / viewport-resize matrix. The
       table-header is now nowrap (see above), so the search shouldn't
       grow back via flex-grow either. */
    width: 360px;
    height: 36px;
    border-radius: var(--radius-md);
    border: 1px solid #e5e7eb;
    box-shadow: 0 1px 0.5px 0.05px rgba(29, 41, 61, 0.02);
    overflow: hidden;
    transition:
        border-color var(--transition-fast),
        box-shadow var(--transition-fast);
    color: #1f2937;
}

.search-input-group:focus-within {
    border-color: var(--color-gray-200);
    box-shadow: var(--shadow-focus);
}

.search-input-field {
    flex: 1;
    border: none;
    padding: var(--space-sm) var(--space-md);
    font-size: var(--font-size-sm);
    color: var(--color-gray-900);
    outline: none;
}

.search-input-field::placeholder {
    color: var(--color-gray-600);
}

/* Suppress the browser-native ``<input type="search">`` clear control,
   but ONLY in groups that render our custom ``.search-clear-button``.
   The native control mutates the input value without dispatching
   ``submit``/``change``, so HTMX-driven tables would stay in their
   filtered state on clear. Where no custom button is rendered (e.g.
   the tenant list, which uses client-side reactive filtering), the
   native ``×`` is left intact as a useful affordance.

   ``:has()`` is supported in all evergreen browsers (Chrome/Edge 105+,
   Safari 15.4+, Firefox 121+); the platform baseline for this app
   already requires those. WebKit/Chromium/Safari use ``::-webkit-search-
   cancel-button``; Firefox's equivalent was removed in Fx >= 71 and
   is now a no-op. */
.search-input-group:has(.search-clear-button)
.search-input-field::-webkit-search-cancel-button {
    appearance: none;
    -webkit-appearance: none;
}

.search-clear-button {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 28px;
    height: 100%;
    padding: 0;
    margin-right: 4px;
    background: transparent;
    border: none;
    color: var(--color-gray-600);
    cursor: pointer;
    font-size: 12px;
    border-radius: var(--radius-sm);
    transition:
        color var(--transition-fast),
        background-color var(--transition-fast);
}

.search-clear-button:hover {
    color: var(--color-gray-900);
    background-color: var(--color-gray-100);
}

.search-clear-button:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 1px;
    color: var(--color-gray-900);
}

.search-clear-button[hidden] {
    display: none;
}

.search-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-xs);
    padding: var(--space-sm) var(--space-md);
    background: var(--color-primary);
    color: #ffffff !important; /* Light text for better contrast */
    border: none;
    border-radius: 8px;
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    cursor: pointer;
    transition: background-color var(--transition-normal);
    min-width: 80px;
}

.search-button:hover {
    background: var(--color-primary-hover);
}

.search-button:focus {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

.filter-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-items: flex-start;
}

.filter-field__label {
    font-size: 12px;
    font-weight: 500;
    color: #6b7280;
    margin-bottom: 0;
}

.filter-dropdown {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 8px 12px;
    border-radius: 12px;
    border: 1px solid #e5e7eb;
    background: #f9fafb;
    box-shadow: 0 1px 0.5px 0.05px rgba(29, 41, 61, 0.02);
    font-size: 14px;
    color: #1f2937;
    cursor: pointer;
    min-width: 140px;
    outline: none;
    transition:
        border-color 0.2s ease,
        background-color 0.2s ease;
    position: relative;
    appearance: none;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e");
    background-position: right 8px center;
    background-repeat: no-repeat;
    background-size: 16px;
    padding-right: 32px;
}

.filter-dropdown:hover {
    border-color: #d1d5db;
    background: #f3f4f6;
}

.filter-dropdown:focus {
    border-color: #25af88;
    box-shadow: 0 0 0 3px rgba(37, 175, 136, 0.1);
}

/* Remove default dropdown arrow for select elements */
.filter-dropdown::-ms-expand {
    display: none;
}

/* Extra right-aligned header content (e.g. filter pill buttons) */
.table-header-extra {
    display: flex;
    align-items: center;
    margin-left: auto;
}

/* Top Pagination Controls.
   ``flex-shrink: 0`` keeps the pagination row at its intrinsic width
   when the parent narrows; the search/filter form gives way instead
   (its inner ``.search-input-group`` already has an explicit width and
   the form is ``flex: 0 1 auto``). Stops the First/Prev/Next/Last
   buttons from getting squeezed unreadable at mid-narrow viewports. */
.pagination-controls-top {
    display: flex;
    align-items: center;
    margin-left: auto;
    flex-shrink: 0;
}

/* Ensure form doesn't take full width when pagination is present */
.table-search-form {
    flex: 0 1 auto;
}

/* Responsive Design */
@media (max-width: 768px) {
    .table-header {
        flex-direction: column;
        align-items: stretch;
        gap: var(--space-lg);
    }

    .table-search-form {
        flex-direction: column;
        align-items: stretch;
        gap: var(--space-md);
    }

    .search-input-group {
        width: 100%;
        max-width: none;
    }

    .filter-dropdown {
        flex: 1;
        min-width: auto;
    }

    .pagination-controls-top,
    .table-header-extra {
        margin-left: 0;
        justify-content: center;
    }
}

@media (max-width: 480px) {
    .table-header {
        padding: var(--space-lg);
    }

    .table-search-form {
        flex-direction: column;
        gap: var(--space-sm);
    }

    .filter-dropdown {
        min-width: auto;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .search-input-group,
    .filter-dropdown {
        border-width: 2px;
    }

    .search-button {
        border: 2px solid var(--color-primary);
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    .search-input-group,
    .filter-dropdown,
    .search-button {
        transition: none;
    }
}

/* Dark mode support (future enhancement) */
@media (prefers-color-scheme: dark) {
    :root {
        --color-gray-50: #1f2937;
        --color-gray-100: #374151;
        --color-gray-200: #4b5563;
        --color-gray-300: #6b7280;
        --color-gray-600: #9ca3af;
        --color-gray-700: #d1d5db;
        --color-gray-900: #f9fafb;
        --color-white: #111827;
    }
}
