/* ═══════════════════════════════════════════════════════════════════════
   EGW Semantic Search — Stylesheet
   ═══════════════════════════════════════════════════════════════════════ */

:root {
    --bg: #faf8f5;
    --surface: #ffffff;
    --surface-alt: #f5f0eb;
    --text: #2c2418;
    --text-secondary: #6b5d4d;
    --text-muted: #9e8e7a;
    --accent: #7c4d1e;
    --accent-light: #a06830;
    --accent-bg: #f5ebe0;
    --border: #e5ddd3;
    --border-light: #ede7df;
    --shadow-sm: 0 1px 3px rgba(44, 36, 24, 0.06);
    --shadow-md: 0 4px 12px rgba(44, 36, 24, 0.08);
    --shadow-lg: 0 8px 32px rgba(44, 36, 24, 0.12);
    --radius: 8px;
    --radius-lg: 12px;
    --font-body: 'Merriweather', Georgia, serif;
    --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --max-width: 900px;
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-body);
    background: var(--bg);
    color: var(--text);
    line-height: 1.7;
    -webkit-font-smoothing: antialiased;
}

/* ── Header ─────────────────────────────────────────────────────────── */

.header {
    background: linear-gradient(135deg, #3a2614 0%, #5a3a20 50%, #7c4d1e 100%);
    color: white;
    padding: 2rem 1.5rem 1.5rem;
    text-align: center;
    overflow: hidden;
    transition: max-height 0.3s ease-out, padding 0.3s ease-out, opacity 0.25s ease-out;
}

.header.collapsed {
    max-height: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    opacity: 0;
    pointer-events: none;
}

.header-inner {
    max-width: var(--max-width);
    margin: 0 auto;
}

.logo {
    font-family: var(--font-ui);
    font-size: 1.8rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin-bottom: 0.25rem;
}

.logo-icon {
    margin-right: 0.3rem;
}

.subtitle {
    font-size: 0.85rem;
    opacity: 0.8;
    font-family: var(--font-ui);
    font-weight: 400;
}

/* ── Search Section ─────────────────────────────────────────────────── */

.search-section {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 1.5rem;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}

.search-container {
    max-width: var(--max-width);
    margin: 0 auto;
}

.search-bar {
    display: flex;
    gap: 0.5rem;
}

.search-bar input {
    flex: 1;
    padding: 0.75rem 1rem;
    font-family: var(--font-ui);
    font-size: 0.95rem;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.search-bar input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(124, 77, 30, 0.12);
}

.search-bar input::placeholder {
    color: var(--text-muted);
    font-size: 0.85rem;
}

.search-bar button {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.75rem 1.25rem;
    font-family: var(--font-ui);
    font-size: 0.9rem;
    font-weight: 600;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
    white-space: nowrap;
}

.search-bar button:hover {
    background: var(--accent-light);
}

.search-bar button:active {
    transform: scale(0.97);
}

/* ── Filters ────────────────────────────────────────────────────────── */

.filters {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-top: 0.75rem;
    align-items: flex-end;
}

.filter-group {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.filter-group label {
    font-family: var(--font-ui);
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.filter-group select,
.filter-group input[type="number"] {
    padding: 0.4rem 0.6rem;
    font-family: var(--font-ui);
    font-size: 0.8rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: white;
    color: var(--text);
}

.year-range {
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.year-range input {
    width: 70px;
}

.year-range span {
    color: var(--text-muted);
}

.filter-toggles {
    flex-direction: row;
    gap: 0.75rem;
    align-items: center;
    margin-left: auto;
}

.filter-toggles label {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.75rem;
    text-transform: none;
    letter-spacing: normal;
    cursor: pointer;
    color: var(--text-secondary);
}

.filter-toggles input[type="checkbox"] {
    accent-color: var(--accent);
}

/* ── Results ────────────────────────────────────────────────────────── */

.results-section {
    max-width: var(--max-width);
    margin: 0 auto;
    padding: 1rem 1.5rem 3rem;
}

.status-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    font-family: var(--font-ui);
    font-size: 0.8rem;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border-light);
    margin-bottom: 1rem;
}

.latency {
    color: var(--text-muted);
    font-size: 0.75rem;
}

.facets {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}

.facet-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.25rem 0.6rem;
    font-family: var(--font-ui);
    font-size: 0.7rem;
    font-weight: 500;
    background: var(--accent-bg);
    color: var(--accent);
    border-radius: 20px;
    border: 1px solid rgba(124, 77, 30, 0.15);
}

.facet-badge .count {
    font-weight: 700;
}

.expanded-queries {
    font-family: var(--font-ui);
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
    padding: 0.4rem 0;
}

.expanded-queries span {
    color: var(--accent);
    font-weight: 500;
}

/* ── Result Card ────────────────────────────────────────────────────── */

.result-card {
    background: var(--surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-lg);
    padding: 1.25rem 1.5rem;
    margin-bottom: 0.75rem;
    transition: box-shadow 0.2s, border-color 0.2s;
}

.result-card:hover {
    border-color: var(--border);
    box-shadow: var(--shadow-md);
}

.result-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 0.5rem;
    gap: 0.75rem;
}

.result-meta {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
}

.badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    font-family: var(--font-ui);
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    border-radius: 4px;
}

.badge-book { background: #e8f0e3; color: #3a6b25; }
.badge-periodical { background: #e3eaf0; color: #25536b; }
.badge-manuscript { background: #f0e8e3; color: #6b4025; }
.badge-pamphlet { background: #f0e3ef; color: #6b2560; }

.result-title-block {
    flex: 1;
    min-width: 0;
}

.result-title {
    font-family: var(--font-ui);
    font-size: 0.9rem;
    color: var(--text);
    margin-bottom: 0.15rem;
    line-height: 1.4;
}

.result-title .book-name {
    font-weight: 600;
    color: var(--text);
}

.result-title .author-name {
    font-weight: 400;
    color: var(--text-secondary);
    font-size: 0.8rem;
}

.result-ref {
    font-family: var(--font-ui);
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 0.5rem;
}

.result-ref a {
    color: var(--accent);
    text-decoration: none;
}

.result-ref a:hover {
    text-decoration: underline;
}

.result-text {
    font-size: 0.92rem;
    line-height: 1.75;
    color: var(--text);
    margin-bottom: 0.75rem;
}

/* Sentence-level semantic highlighting (from server) */
.result-text mark {
    background: linear-gradient(to bottom, rgba(255, 213, 79, 0.15), rgba(255, 213, 79, 0.35));
    color: inherit;
    padding: 0.1em 0.2em;
    border-radius: 3px;
    border-bottom: 2px solid rgba(196, 160, 40, 0.4);
    transition: background 0.2s;
}

.result-text mark:hover {
    background: rgba(255, 213, 79, 0.5);
}

/* Word-level bold: primary query terms */
.result-text .kw-primary {
    color: var(--accent);
    font-weight: 700;
    background: rgba(124, 77, 30, 0.08);
    padding: 0 0.1em;
    border-radius: 2px;
}

/* Word-level bold: expansion/neural terms */
.result-text .kw-expansion {
    color: #5a7a3a;
    font-weight: 600;
    background: rgba(90, 122, 58, 0.08);
    padding: 0 0.1em;
    border-radius: 2px;
    font-style: italic;
}

.result-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.action-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.3rem 0.6rem;
    font-family: var(--font-ui);
    font-size: 0.7rem;
    font-weight: 500;
    color: var(--text-muted);
    background: transparent;
    border: 1px solid var(--border-light);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s;
    text-decoration: none;
}

.action-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: var(--accent-bg);
}

.result-scores {
    font-family: var(--font-ui);
    font-size: 0.65rem;
    color: var(--text-muted);
    margin-top: 0.3rem;
}

/* ── Result Groups ─────────────────────────────────────────────────── */

.results-group {
    margin-bottom: 1.5rem;
}

.group-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.6rem 0;
    border-bottom: 2px solid var(--accent);
    margin-bottom: 0.75rem;
}

.group-header.clickable {
    cursor: pointer;
    border-bottom-color: var(--border);
    transition: border-color 0.2s;
}

.group-header.clickable:hover {
    border-bottom-color: var(--accent);
}

.group-title {
    font-family: var(--font-ui);
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text);
    margin: 0;
}

.group-title.estate-title {
    color: var(--text-muted);
    font-weight: 600;
}

.group-count {
    font-family: var(--font-ui);
    font-size: 0.75rem;
    color: var(--text-muted);
}

.toggle-arrow {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-left: auto;
}

.estate-group {
    opacity: 0.8;
}

/* ── Collapsed Duplicate Cards ─────────────────────────────────────── */

.result-card-collapsed {
    padding: 0.6rem 1rem;
    border-left: 3px solid var(--border);
    opacity: 0.85;
    background: var(--surface-alt);
}

.result-card-collapsed:hover {
    opacity: 1;
}

.collapsed-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    cursor: pointer;
}

.dup-indicator {
    font-family: var(--font-ui);
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-right: 0.3rem;
}

.dup-expand-arrow {
    font-size: 0.7rem;
    color: var(--text-muted);
    flex-shrink: 0;
    margin-left: 0.5rem;
}

.dup-expanded-content {
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border-light);
}

/* ── Load More ─────────────────────────────────────────────────────── */

.load-more-container {
    text-align: center;
    padding: 1.5rem 0;
}

.load-more-btn {
    display: inline-block;
    padding: 0.6rem 1.5rem;
    font-family: var(--font-ui);
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--accent);
    background: transparent;
    border: 2px solid var(--accent);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s;
}

.load-more-btn:hover {
    background: var(--accent);
    color: white;
}

/* ── Loading ────────────────────────────────────────────────────────── */

.loading {
    text-align: center;
    padding: 3rem;
    color: var(--text-muted);
    font-family: var(--font-ui);
    font-size: 0.9rem;
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    margin: 0 auto 1rem;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ── Empty State ────────────────────────────────────────────────────── */

.empty-state {
    text-align: center;
    padding: 3rem 1rem;
}

.empty-icon {
    font-size: 3rem;
    margin-bottom: 0.5rem;
}

.empty-state h2 {
    font-family: var(--font-ui);
    font-size: 1.3rem;
    font-weight: 600;
    margin-bottom: 0.3rem;
}

.empty-state p {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 1.5rem;
}

.suggestions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
}

.suggestion {
    padding: 0.4rem 0.8rem;
    font-family: var(--font-ui);
    font-size: 0.8rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 20px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.15s;
}

.suggestion:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-bg);
}

/* ── Modal ──────────────────────────────────────────────────────────── */

.modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
}

.modal-content {
    position: relative;
    width: 90%;
    max-width: 700px;
    max-height: 85vh;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-light);
}

.modal-header h3 {
    font-family: var(--font-ui);
    font-size: 1rem;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0.25rem;
    line-height: 1;
}

.modal-close:hover {
    color: var(--text);
}

.modal-body {
    padding: 1.5rem;
    overflow-y: auto;
    line-height: 1.8;
}

.modal-body .context-chunk {
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border-light);
}

.modal-body .context-chunk:last-child {
    border-bottom: none;
}

.modal-body .context-chunk.highlight {
    background: var(--accent-bg);
    margin: 0 -1.5rem;
    padding: 0.75rem 1.5rem;
    border-radius: 4px;
}

/* Semantic highlighting inside collapsed cards and modal */
.result-card-collapsed mark,
.modal-body mark {
    background: linear-gradient(to bottom, rgba(255, 213, 79, 0.15), rgba(255, 213, 79, 0.35));
    color: inherit;
    padding: 0.1em 0.2em;
    border-radius: 3px;
    border-bottom: 2px solid rgba(196, 160, 40, 0.4);
}

.result-card-collapsed .kw-primary,
.modal-body .kw-primary {
    color: var(--accent);
    font-weight: 700;
    background: rgba(124, 77, 30, 0.08);
    padding: 0 0.1em;
    border-radius: 2px;
}

.result-card-collapsed .kw-expansion,
.modal-body .kw-expansion {
    color: #5a7a3a;
    font-weight: 600;
    background: rgba(90, 122, 58, 0.08);
    padding: 0 0.1em;
    border-radius: 2px;
    font-style: italic;
}

.context-ref {
    font-family: var(--font-ui);
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 0.25rem;
}

/* ── Collapsing Header (mobile: banner + search; desktop: banner only) */

@media (max-width: 768px) {
    .search-section {
        overflow: hidden;
        transition: max-height 0.3s ease-out, padding 0.3s ease-out,
                    opacity 0.25s ease-out, border-width 0.3s ease-out,
                    box-shadow 0.25s ease-out;
    }

    .search-section.collapsed {
        max-height: 0 !important;
        padding-top: 0 !important;
        padding-bottom: 0 !important;
        border-bottom-width: 0 !important;
        box-shadow: none !important;
        opacity: 0;
        pointer-events: none;
    }
}

/* ── Responsive ─────────────────────────────────────────────────────── */

@media (max-width: 640px) {
    .header {
        padding: 1.5rem 1rem 1rem;
    }

    .logo {
        font-size: 1.4rem;
    }

    .search-section {
        padding: 1rem;
    }

    .search-bar {
        flex-direction: column;
    }

    .search-bar button {
        width: 100%;
        justify-content: center;
    }

    .filters {
        flex-direction: column;
        gap: 0.5rem;
    }

    .filter-toggles {
        margin-left: 0;
    }

    .results-section {
        padding: 0.75rem 1rem 2rem;
    }

    .result-card {
        padding: 1rem;
    }

    .result-header {
        flex-direction: column;
    }
}
