/* ============================================================================
   PANEL RAIL COMPONENT
   ============================================================================

   Vertical "binder divider" tab rail on the inline-start edge of the
   document viewer.  Each tab expands its own dedicated panel.  Only one
   panel can be open at a time.

   Layout (RTL — start = right):
   ┌────────────────┬───┬───────────┐
   │                │ ■ │           │
   │  Reading area  │ □ │  Panel    │
   │  (shrinks)     │ □ │  300px    │
   │                │   │           │
   └────────────────┴───┴───────────┘
                  Rail ─┘  └── Panel

   Sections:
   - Rail container (fixed position, flexbox)
   - Tab strip (vertical icons + labels)
   - Panel content area (0 → 300px transition)
   - Individual tab states (hover, active, connected)
   - Responsive: tablet overlay, mobile bottom bar

   ============================================================================ */


/* ============================================================================
   RAIL CONTAINER
   ============================================================================ */

.panel-rail-container {
    position: fixed;
    inset-inline-start: 0;
    top: var(--nav-height);
    bottom: var(--footer-height);
    display: flex;
    flex-direction: row-reverse;   /* tab strip at inline-start, panel extends */
    z-index: var(--z-sidebar);
    pointer-events: none;          /* pass-through when closed */
}

.panel-rail-container > * {
    pointer-events: auto;          /* children are interactive */
}


/* ============================================================================
   TAB STRIP — Vertical binder tabs
   ============================================================================ */

.panel-rail-tabs {
    width: var(--rail-width);
    background: var(--rail-bg);
    border-inline-end: 1px solid var(--rail-border);
    display: flex;
    flex-direction: column;
    align-items: stretch;
    padding: 0;
    gap: 0;
    overflow: hidden;
}

.panel-rail-tab {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-bottom: 1px solid var(--rail-border);
    cursor: pointer;
    color: var(--rail-tab-color);
    padding: 0;
    transition: background var(--duration-fast) var(--ease-out),
                color var(--duration-fast) var(--ease-out);
    position: relative;
}

.panel-rail-tab:last-child {
    border-bottom: none;
}

/* Vertical text inside divider tab */
.panel-rail-tab-text {
    writing-mode: vertical-rl;
    font-family: var(--font-ui);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-semibold);
    letter-spacing: 0.08em;
    white-space: nowrap;
    user-select: none;
}

.panel-rail-tab:hover {
    background: var(--rail-tab-hover-bg);
    color: var(--color-text-primary);
}

/* Active tab — visually "connected" to its panel */
.panel-rail-tab.active {
    background: var(--rail-tab-active-bg);
    color: var(--rail-tab-active-color);
    border-inline-end: 3px solid var(--rail-tab-active-color);
    margin-inline-end: -1px;  /* overlap the rail border to create continuity */
}

/* Keyboard focus — visible ring */
.panel-rail-tab:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: -2px;
    z-index: 1;
}


/* ============================================================================
   PANEL CONTENT — Expandable area
   ============================================================================ */

.panel-rail-content {
    width: 0;
    overflow: hidden;
    background: var(--panel-bg);
    border-inline-end: 1px solid var(--panel-border);
    box-shadow: var(--panel-shadow);
    transition: width var(--duration-base) var(--ease-out);
    display: flex;
    flex-direction: column;
}

.panel-rail-content.open {
    width: var(--panel-width);
}


/* ============================================================================
   PANEL HEADER
   ============================================================================ */

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--color-border-secondary);
    flex-shrink: 0;
    direction: rtl;
}

.panel-header-title {
    font-family: var(--font-ui);
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-semibold);
    color: var(--color-text-primary);
    margin: 0;
}

.panel-close-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: var(--space-1);
    border-radius: var(--radius-sm);
    color: var(--color-text-secondary);
    font-size: 1rem;
    line-height: 1;
    transition: background var(--duration-fast) var(--ease-out),
                color var(--duration-fast) var(--ease-out);
}

.panel-close-btn:hover {
    background: var(--color-surface-sunken);
    color: var(--color-text-primary);
}

.panel-close-btn:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: -2px;
}


/* ============================================================================
   PANEL BODY — Scrollable content region
   ============================================================================ */

.panel-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
}

/* TOC panel body — tighter padding */
.panel-body.toc-panel {
    padding: var(--space-2) var(--space-3);
    direction: rtl;
}

/* Search panel body */
.panel-body.search-panel {
    padding: 0;
}


/* ============================================================================
   TOC PANEL OVERRIDES
   ============================================================================
   Re-uses existing toc-* classes from toc-sidebar.css.
   These overrides adjust spacing for the narrower panel context.
   ============================================================================ */

.panel-body .toc-loading,
.panel-body .toc-error,
.panel-body .toc-empty {
    padding: var(--space-6) var(--space-4);
    text-align: center;
    color: var(--color-text-secondary);
    font-size: var(--font-size-sm);
    direction: rtl;
}

.panel-body .toc-error {
    color: var(--color-error);
}


/* ============================================================================
   SEARCH PANEL OVERRIDES
   ============================================================================
   The DocumentSearch component is mounted inside panel-body.search-panel.
   It carries its own .document-search / .doc-search-* styles from
   document-sidebar.css.  These overrides just ensure it fills the panel.
   ============================================================================ */

.panel-body.search-panel .document-search {
    height: 100%;
}

.panel-body.search-panel .sidebar-tab-content {
    padding: var(--space-3) var(--space-4);
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
}


/* ============================================================================
   MATCHES PANEL
   ============================================================================ */

.panel-body.matches-panel {
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100%;
    direction: rtl;
}

/* Status messages */
.panel-matches-placeholder,
.panel-matches-loading,
.panel-matches-empty,
.panel-matches-error {
    padding: var(--space-6) var(--space-4);
    text-align: center;
    color: var(--color-text-secondary);
    font-size: var(--font-size-sm);
    font-family: var(--font-ui);
}

.panel-matches-error {
    color: var(--color-error, #dc3545);
}

/* Match count header */
.panel-matches-count {
    padding: var(--space-2) var(--space-3);
    font-size: var(--font-size-xs);
    font-family: var(--font-ui);
    color: var(--color-text-secondary);
    background: var(--color-surface-sunken);
    border-bottom: 1px solid var(--color-border-secondary);
    flex-shrink: 0;
}

/* Sort control bar — sits between count strip and match list */
.panel-matches-sort-bar {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-1) var(--space-3);
    border-bottom: 1px solid var(--color-border-secondary);
    background: var(--color-surface-sunken);
    flex-shrink: 0;
    direction: rtl;
}

.panel-matches-sort-label {
    font-family: var(--font-ui);
    font-size: var(--font-size-xs);
    color: var(--color-text-secondary);
    white-space: nowrap;
    flex-shrink: 0;
}

.panel-matches-sort-select {
    flex: 1;
    min-width: 0;
    font-family: var(--font-ui);
    font-size: var(--font-size-xs);
    color: var(--color-text-primary);
    background: var(--color-surface-primary);
    border: 1px solid var(--color-border-secondary);
    border-radius: var(--radius-sm);
    padding: 2px var(--space-2);
    cursor: pointer;
    direction: inherit;
}

.panel-matches-sort-select:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: -2px;
}

/* Scrollable match list */
.panel-matches-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
}

/* Genre group wrapper — needed for sticky divider positioning */
.panel-matches-genre-group {
    position: relative;
}

/* Genre group divider — sticky inside .panel-matches-list (overflow-y: auto) */
.panel-matches-genre-divider {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-1) var(--space-3);
    background: var(--color-surface-sunken);
    border-top: 1px solid var(--color-border-secondary);
    border-bottom: 1px solid var(--color-border-secondary);
    direction: rtl;
    position: sticky;
    top: 0;
    z-index: 1;
}

.panel-matches-genre-label {
    font-family: var(--font-ui);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    color: var(--color-text-primary);
    flex: 1;
}

.panel-matches-genre-count {
    font-family: var(--font-ui);
    font-size: var(--font-size-xs);
    color: var(--color-text-secondary);
    flex-shrink: 0;
}

/* Full-view link at bottom */
.panel-matches-fullview {
    padding: var(--space-2) var(--space-3);
    text-align: center;
    border-top: 1px solid var(--color-border-secondary);
    flex-shrink: 0;
}

.panel-matches-fullview a {
    font-family: var(--font-ui);
    font-size: var(--font-size-sm);
    color: var(--color-primary);
    text-decoration: none;
}

.panel-matches-fullview a:hover {
    text-decoration: underline;
}

/* --- Match Card --- */

.panel-match-card {
    border-bottom: 1px solid var(--color-border-secondary);
}

.panel-match-header {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-1);
    padding: var(--space-2) var(--space-3);
    width: 100%;
    background: none;
    border: none;
    cursor: pointer;
    text-align: start;
    font-family: var(--font-ui);
    transition: background var(--duration-fast) var(--ease-out);
}

.panel-match-header:hover {
    background: var(--color-surface-sunken);
}

.panel-match-card.expanded .panel-match-header {
    background: rgba(139, 69, 19, 0.06);
}

.panel-match-sim {
    flex-shrink: 0;
    font-size: calc(var(--font-size-xs) * 0.75);
    font-weight: var(--font-weight-semibold);
    color: var(--color-primary);
    background: var(--color-surface-sunken);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    min-width: 36px;
    text-align: center;
    align-self: flex-start;
    margin-top: 1px;
}

.panel-match-meta {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    display: flex;
    flex-direction: row;
    align-items: center;
    flex-wrap: nowrap;
    gap: var(--space-1);
}

.panel-match-author {
    font-size: calc(var(--font-size-xs) * 0.75);
    color: var(--color-text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
    flex-shrink: 1;
}

.panel-match-author.unknown {
    font-style: italic;
}

.panel-match-title {
    font-size: calc(var(--font-size-sm) * 0.75);
    color: var(--color-text-primary);
    font-weight: var(--font-weight-medium);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.panel-match-chevron {
    flex-shrink: 0;
    font-size: 0.625rem;
    color: var(--color-text-secondary);
    align-self: flex-start;
    margin-top: 2px;
}

/* Row 1 of the two-row header: sim badge + meta (author/title/year) + chevron */
.panel-match-header-row {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    gap: var(--space-2);
    min-width: 0;
}

/* Death year tag — matches visual style of .suggestion-death-year */
.panel-match-year {
    flex-shrink: 0;
    font-family: var(--font-ui);
    font-size: calc(var(--font-size-xs) * 0.75);
    font-weight: var(--font-weight-semibold);
    color: var(--color-text-secondary);
    background: var(--color-surface-sunken);
    padding: 1px 5px;
    border-radius: var(--radius-sm);
    white-space: nowrap;
    align-self: flex-start;
}

/* Row 2: Arabic matched text preview (4 words, no context) */
.panel-match-source-preview {
    display: block;
    direction: rtl;
    text-align: start;
    font-family: var(--font-primary);
    font-size: var(--font-size-xs);
    line-height: var(--line-height-arabic);
    font-feature-settings: "liga" 1, "calt" 1, "kern" 1, "rlig" 1;
    text-rendering: optimizeLegibility;
    color: var(--color-text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.panel-match-source-preview .highlighted-match {
    background: rgba(139, 69, 19, 0.08);
    color: var(--color-text-primary);
    padding: 0 2px;
    border-radius: var(--radius-sm);
}

/* --- Expanded Detail --- */

.panel-match-detail {
    padding: 0 var(--space-3) var(--space-3);
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

.panel-match-side {
    background: var(--color-surface-primary);
    border-radius: var(--radius-md);
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-border-secondary);
}

.panel-match-side-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--space-1);
}

.panel-match-side-label {
    font-family: var(--font-ui);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.panel-match-side-header a {
    font-size: var(--font-size-sm);
    color: var(--color-primary);
    text-decoration: none;
}

.panel-match-side-header a:hover {
    text-decoration: underline;
}

.panel-match-context {
    font-family: var(--font-primary);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-relaxed);
    color: var(--color-text-primary);
    margin-bottom: var(--space-1);
}

.panel-match-context .context-before,
.panel-match-context .context-after {
    color: var(--color-text-secondary);
}

.panel-match-context .highlighted-match {
    background: rgba(139, 69, 19, 0.15);
    color: var(--color-text-primary);
    font-weight: var(--font-weight-semibold);
    padding: 1px 2px;
    border-radius: 2px;
}

.panel-match-side-info {
    font-family: var(--font-ui);
    font-size: var(--font-size-xs);
    color: var(--color-text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* --- Indicator dot on tab when data is loaded --- */

.panel-rail-tab.has-data::before {
    content: "";
    position: absolute;
    top: var(--space-2);
    inset-inline-end: var(--space-2);
    width: 8px;
    height: 8px;
    background: var(--color-primary);
    border-radius: 50%;
    border: 1.5px solid var(--rail-bg);
}


/* ============================================================================
   SUGGESTIONS PANEL
   ============================================================================ */

.panel-body.suggestions-panel {
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100%;
    direction: rtl;
}

/* Mode selector (segmented control) */
.suggestions-mode-selector {
    display: flex;
    gap: 0;
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--color-border-secondary);
    flex-shrink: 0;
}

.suggestions-mode-btn {
    flex: 1;
    padding: var(--space-1) var(--space-2);
    font-family: var(--font-ui);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-secondary);
    background: var(--color-surface-primary);
    border: 1px solid var(--color-border-secondary);
    cursor: pointer;
    transition: background var(--duration-fast) var(--ease-out),
                color var(--duration-fast) var(--ease-out);
}

.suggestions-mode-btn:first-child {
    border-start-start-radius: var(--radius-sm);
    border-end-start-radius: var(--radius-sm);
}

.suggestions-mode-btn:last-child {
    border-start-end-radius: var(--radius-sm);
    border-end-end-radius: var(--radius-sm);
}

.suggestions-mode-btn + .suggestions-mode-btn {
    border-inline-start: none;
}

.suggestions-mode-btn.active {
    background: var(--color-primary);
    color: var(--color-white, #fff);
    border-color: var(--color-primary);
}

/* Status messages */
.panel-suggestions-placeholder,
.panel-suggestions-loading,
.panel-suggestions-empty,
.panel-suggestions-error {
    padding: var(--space-6) var(--space-4);
    text-align: center;
    color: var(--color-text-secondary);
    font-size: var(--font-size-sm);
    font-family: var(--font-ui);
}

.panel-suggestions-error {
    color: var(--color-error, #dc3545);
}

/* Results count header */
.panel-suggestions-count {
    padding: var(--space-2) var(--space-3);
    font-size: var(--font-size-xs);
    font-family: var(--font-ui);
    color: var(--color-text-secondary);
    background: var(--color-surface-sunken);
    border-bottom: 1px solid var(--color-border-secondary);
    flex-shrink: 0;
}

/* Scrollable suggestions list */
.panel-suggestions-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
}

/* --- Suggestion Card --- */

.panel-suggestion-card {
    border-bottom: 1px solid var(--color-border-secondary);
}

.panel-suggestion-header {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    padding: var(--space-3);
    width: 100%;
    background: none;
    border: none;
    cursor: pointer;
    text-align: start;
    font-family: var(--font-ui);
    transition: background var(--duration-fast) var(--ease-out);
}

.panel-suggestion-header:hover {
    background: var(--color-surface-sunken);
}

.panel-suggestion-card.expanded .panel-suggestion-header {
    background: rgba(139, 69, 19, 0.06);
}

.suggestion-death-year {
    flex-shrink: 0;
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    color: var(--color-primary);
    background: rgba(139, 69, 19, 0.1);
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    min-width: 48px;
    text-align: center;
    margin-top: 2px;
}

.suggestion-death-year.unknown {
    color: var(--color-text-secondary);
    background: var(--color-surface-sunken);
}

/* Stacked meta: author+genre row, title, preview */
.panel-suggestion-meta {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.suggestion-meta-top {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.suggestion-author {
    font-size: var(--font-size-xs);
    color: var(--color-text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.suggestion-title {
    font-size: var(--font-size-sm);
    color: var(--color-text-primary);
    font-weight: var(--font-weight-medium);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.suggestion-preview {
    font-family: var(--font-primary);
    font-size: var(--font-size-xs);
    line-height: var(--line-height-relaxed);
    color: var(--color-text-secondary);
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.suggestion-genre-tag {
    flex-shrink: 0;
    font-size: 0.625rem;
    color: var(--color-text-secondary);
    background: var(--color-surface-sunken);
    padding: 1px 6px;
    border-radius: 9999px;
    white-space: nowrap;
}

.panel-suggestion-chevron {
    flex-shrink: 0;
    font-size: 0.625rem;
    color: var(--color-text-secondary);
    margin-top: 4px;
}

/* --- Expanded Detail --- */

.panel-suggestion-detail {
    padding: 0 var(--space-3) var(--space-3);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.suggestion-text {
    font-family: var(--font-primary);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-relaxed);
    color: var(--color-text-primary);
    background: var(--color-surface-primary);
    border-radius: var(--radius-md);
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-border-secondary);
    max-height: 200px;
    overflow-y: auto;
}

.suggestion-source-link {
    font-family: var(--font-ui);
    font-size: var(--font-size-sm);
    color: var(--color-primary);
    text-decoration: none;
    align-self: flex-start;
}

.suggestion-source-link:hover {
    text-decoration: underline;
}


/* Footer page indicator */
.footer-page-indicator {
    font-family: var(--font-ui);
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
}


/* ============================================================================
   RESPONSIVE — TABLET (769px – 1024px)
   Panel overlays instead of pushing content
   ============================================================================ */

@media (max-width: 1024px) and (min-width: 769px) {
    .panel-rail-content.open {
        position: absolute;
        inset-inline-start: var(--rail-width);
        top: 0;
        bottom: 0;
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.2);
    }
}


/* ============================================================================
   RESPONSIVE — MOBILE (<768px)
   Rail becomes horizontal bottom bar, panels are full-width overlays
   ============================================================================ */

@media (max-width: 768px) {
    .panel-rail-container {
        position: fixed;
        top: auto;
        bottom: var(--footer-height);
        inset-inline: 0;
        flex-direction: column-reverse;
        height: auto;
    }

    .panel-rail-tabs {
        width: 100%;
        height: var(--rail-width);
        flex-direction: row;
        padding: 0;
        border-inline-end: none;
        border-top: 1px solid var(--rail-border);
    }

    .panel-rail-tab {
        flex: 1;
        height: 100%;
        width: auto;
        border-bottom: none;
        border-inline-start: 1px solid var(--rail-border);
    }

    .panel-rail-tab:first-child {
        border-inline-start: none;
    }

    .panel-rail-tab.active {
        border-inline-end: none;
        border-top: 2px solid var(--rail-tab-active-color);
        margin-inline-end: 0;
    }

    /* Horizontal text on mobile */
    .panel-rail-tab-text {
        writing-mode: horizontal-tb;
        font-size: var(--font-size-xs);
        letter-spacing: normal;
    }

    .panel-rail-content {
        width: 100% !important;
        height: 0;
        transition: height var(--duration-base) var(--ease-out);
    }

    .panel-rail-content.open {
        width: 100% !important;
        height: 50vh;
        border-inline-end: none;
        border-top: 1px solid var(--panel-border);
    }
}


/* ============================================================================
   REDUCED MOTION
   ============================================================================ */

@media (prefers-reduced-motion: reduce) {
    .panel-rail-content {
        transition: none;
    }

    .panel-rail-tab::after {
        transition: none;
    }
}


/* ============================================================================
   PRINT — hide rail entirely
   ============================================================================ */

@media print {
    .panel-rail-container {
        display: none;
    }
}
