/* ============================================================================
   FILTER WORKBENCH — three-column book-curation modal (search)
   [ RIGHT: criteria ] [ MIDDLE: browse ] [ LEFT: chosen ]  (RTL source order)
   Uses semantic tokens only so it tracks every theme; logical properties for RTL.
   ============================================================================ */

.fw-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
    z-index: 1100; /* above the legacy filter modal (1000) */
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-4, 16px);
    animation: fwFade var(--duration-fast, 150ms) var(--ease-out);
}

.fw-modal {
    direction: rtl;
    width: 96%;
    max-width: 1100px;
    height: 88vh;
    max-height: 88vh;
    background: var(--color-surface-primary, #FAF6F0);
    border: 1px solid var(--color-border-primary, #D4C4A8);
    border-radius: var(--radius-xl, 12px);
    box-shadow: var(--shadow-2xl, 0 20px 60px rgba(0, 0, 0, 0.3));
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: fwUp var(--duration-fast, 150ms) var(--ease-out);
}

@keyframes fwFade { from { opacity: 0; } to { opacity: 1; } }
@keyframes fwUp { from { transform: translateY(12px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---- header / footer ---- */
.fw-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3, 12px) var(--space-5, 20px);
    border-bottom: 1px solid var(--color-border-primary, #D4C4A8);
    background: var(--color-surface-secondary, #F5EDE0);
}
.fw-title {
    margin: 0;
    font-family: var(--font-heading, var(--font-primary, 'Amiri', serif));
    font-size: 1.15rem;
    font-weight: var(--font-weight-bold, 700);
    color: var(--color-primary, #8B4513);
}
.fw-close {
    border: none;
    background: transparent;
    font-size: 1.6rem;
    line-height: 1;
    cursor: pointer;
    color: var(--color-text-secondary, #666);
    padding: 0 var(--space-2, 8px);
    border-radius: var(--radius-sm, 4px);
}
.fw-close:hover { color: var(--color-primary, #8B4513); background: var(--color-surface-sunken, #efe7d8); }

.fw-footer {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: var(--space-2, 8px);
    padding: var(--space-3, 12px) var(--space-5, 20px);
    border-top: 1px solid var(--color-border-primary, #D4C4A8);
    background: var(--color-surface-secondary, #F5EDE0);
}
.fw-footer-spacer { flex: 1; }

.fw-btn {
    padding: var(--space-2, 8px) var(--space-4, 16px);
    border-radius: var(--radius-sm, 4px);
    font-family: var(--font-ui, sans-serif);
    font-size: 0.9rem;
    font-weight: var(--font-weight-medium, 500);
    cursor: pointer;
    border: 1px solid var(--color-border-secondary, #D4C4A8);
    background: var(--color-surface-primary, #fff);
    color: var(--color-text-primary, #333);
    transition: background var(--duration-fast, 150ms) var(--ease-out), border-color var(--duration-fast, 150ms) var(--ease-out);
}
.fw-btn:hover:not(:disabled) { border-color: var(--color-primary, #8B4513); }
.fw-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.fw-btn-ghost { background: transparent; }
.fw-btn-primary {
    background: var(--color-primary, #8B4513);
    border-color: var(--color-primary, #8B4513);
    color: var(--color-text-inverse, #fff);
}
.fw-btn-primary:hover:not(:disabled) { background: var(--color-primary-dark, #6B3410); }
.fw-btn-small { padding: 4px var(--space-2, 8px); font-size: 0.8rem; white-space: nowrap; }
/* Per-group bulk-add buttons (two-group browse column) */
.fw-btn--g1 {
    border-color: var(--group1-color, var(--color-primary, #8B4513));
    color: var(--group1-color, var(--color-primary, #8B4513));
    font-weight: var(--font-weight-semibold, 600);
}
.fw-btn--g1:hover:not(:disabled) {
    background: var(--group1-color, var(--color-primary, #8B4513));
    color: var(--color-text-inverse, #fff);
}
.fw-btn--g2 {
    border-color: var(--group2-color, #9A8466);
    color: var(--group2-color, #9A8466);
    font-weight: var(--font-weight-semibold, 600);
}
.fw-btn--g2:hover:not(:disabled) {
    background: var(--group2-color, #9A8466);
    color: var(--color-text-inverse, #fff);
}

/* ---- body: 3-column grid ---- */
.fw-body {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(210px, 1fr) minmax(280px, 1.4fr) minmax(230px, 1fr);
    gap: 0;
}

.fw-col {
    min-height: 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    border-inline-start: 1px solid var(--color-border-primary, #D4C4A8);
}
.fw-col:first-child { border-inline-start: none; }

.fw-col-head {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: var(--space-2, 8px);
    padding: var(--space-2, 8px) var(--space-3, 12px);
    font-family: var(--font-ui, sans-serif);
    font-weight: var(--font-weight-semibold, 600);
    font-size: 0.9rem;
    color: var(--color-primary, #8B4513);
    background: var(--color-surface-secondary, #F5EDE0);
    border-bottom: 1px solid var(--color-border-primary, #D4C4A8);
}
.fw-col-count {
    font-size: 0.75rem;
    background: var(--color-primary, #8B4513);
    color: var(--color-text-inverse, #fff);
    border-radius: var(--radius-full, 9999px);
    padding: 1px 8px;
    font-variant-numeric: tabular-nums;
}
/* Clear-all for the curated list, pinned to the end of the column header. */
.fw-clear-list {
    margin-inline-start: auto;
    border: 1px solid var(--color-border-secondary, #D4C4A8);
    background: transparent;
    color: var(--color-text-secondary, #666);
    font-family: var(--font-ui, sans-serif);
    font-size: 0.72rem;
    padding: 2px 8px;
    border-radius: var(--radius-sm, 4px);
    cursor: pointer;
    transition: color var(--duration-fast, 150ms) var(--ease-out), border-color var(--duration-fast, 150ms) var(--ease-out);
}
.fw-clear-list:hover { color: var(--color-error, #b3261e); border-color: var(--color-error, #b3261e); }
.fw-col-tools {
    flex-shrink: 0;
    display: flex;
    flex-wrap: wrap;               /* order/group/add toggles wrap to a new line if the column is narrow */
    align-items: center;
    gap: var(--space-2, 8px);
    padding: var(--space-2, 8px);
    border-bottom: 1px solid var(--color-border-subtle, #e6dcc8);
}
.fw-col-tools .fw-search { flex: 1 1 130px; min-width: 110px; }
.fw-order-seg { flex-shrink: 0; }

.fw-search {
    width: 100%;
    box-sizing: border-box;
    padding: 6px var(--space-2, 8px);
    border: 1px solid var(--color-border-secondary, #D4C4A8);
    border-radius: var(--radius-sm, 4px);
    background: var(--color-surface-primary, #fff);
    color: var(--color-text-primary, #333);
    font-family: var(--font-ui, sans-serif);
    font-size: 0.85rem;
    direction: rtl;
}
.fw-search:focus {
    border-color: var(--color-primary, #8B4513);
    box-shadow: 0 0 0 2px rgba(139, 69, 19, 0.15);
}
/* Suppress the outline only for pointer focus; keyboard focus keeps the
   global :focus-visible ring (this rule would otherwise outrank it). */
.fw-search:focus:not(:focus-visible) {
    outline: none;
}

.fw-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--space-1, 4px);
}

/* ---- criteria accordion (RIGHT) ----
   Each criterion carries its own muted earth-tone accent (a keyed dot, an
   open-state stripe, and a faintly tinted body) so the rows AND their expanded
   bodies read as distinct sections. Tinted fills are color-mixed with the theme
   surfaces, with solid fallbacks first, so they adapt to every theme and degrade
   gracefully where color-mix is unsupported. */
.fw-crit-list { flex: 1; min-height: 0; overflow-y: auto; }

/* Class-based accents (NOT nth-child: row sets vary per surface, so positional
   colouring would mis-key). Same muted earth band throughout. */
.fw-crit, .fw-wide { --fw-accent: #7a6a55; }        /* fallback           */
.fw-crit--authors    { --fw-accent: #8b5e3c; }      /* المؤلفون — sepia   */
.fw-crit--titles     { --fw-accent: #3f7d8c; }      /* العناوين — teal    */
.fw-crit--genre      { --fw-accent: #6b7b34; }      /* العلوم — olive     */
.fw-crit--death      { --fw-accent: #8a5273; }      /* سنة الوفاة — plum  */
.fw-crit--quality    { --fw-accent: #b0842f; }      /* عتبة الجودة — ochre*/
.fw-crit--similarity { --fw-accent: #4a6c9b; }      /* التشابه — indigo   */
.fw-crit--length     { --fw-accent: #7c6a9c; }      /* طول المطابقة — mauve*/
.fw-crit--isnad      { --fw-accent: #a05a3c; }      /* الإسناد — rust     */
.fw-crit--quran      { --fw-accent: #2f7d55; }      /* القرآن — emerald   */
.fw-crit--scope      { --fw-accent: #4f7f63; }      /* نطاق المطابقات — pine*/

/* Night Scholar: the light-theme earth hexes above turn muddy / sub-AA on the
   ~#111 dark surfaces, where the accent is read as TEXT (keyed dot, caret,
   badge, the open-row heading and the wide-panel heading). Lift each to a
   lighter tint of the SAME hue — every one clears WCAG AA as text on #111
   (all >=7:1). The summary-strip pills mirror the same six accents. */
[data-theme="night-scholar"] .fw-crit,
[data-theme="night-scholar"] .fw-wide { --fw-accent: #bfae90; }        /* taupe  */
[data-theme="night-scholar"] .fw-crit--authors    { --fw-accent: #d3a06b; } /* sepia  */
[data-theme="night-scholar"] .fw-crit--titles     { --fw-accent: #6ec5d6; } /* teal   */
[data-theme="night-scholar"] .fw-crit--genre      { --fw-accent: #b3c76c; } /* olive  */
[data-theme="night-scholar"] .fw-crit--death      { --fw-accent: #cf90b3; } /* plum   */
[data-theme="night-scholar"] .fw-crit--quality    { --fw-accent: #e2b45c; } /* ochre  */
[data-theme="night-scholar"] .fw-crit--similarity { --fw-accent: #8fb2e0; } /* indigo */
[data-theme="night-scholar"] .fw-crit--length     { --fw-accent: #b8a9d8; } /* mauve  */
[data-theme="night-scholar"] .fw-crit--isnad      { --fw-accent: #de9068; } /* rust   */
[data-theme="night-scholar"] .fw-crit--quran      { --fw-accent: #6cc292; } /* emerald*/
[data-theme="night-scholar"] .fw-crit--scope      { --fw-accent: #80c199; } /* pine   */
[data-theme="night-scholar"] .fw-pill--death      { --fw-accent: #cf90b3; }
[data-theme="night-scholar"] .fw-pill--quality    { --fw-accent: #e2b45c; }
[data-theme="night-scholar"] .fw-pill--similarity { --fw-accent: #8fb2e0; }
[data-theme="night-scholar"] .fw-pill--length     { --fw-accent: #b8a9d8; }
[data-theme="night-scholar"] .fw-pill--isnad      { --fw-accent: #de9068; }
[data-theme="night-scholar"] .fw-pill--quran      { --fw-accent: #6cc292; }
[data-theme="night-scholar"] .fw-pill--scope      { --fw-accent: #80c199; }

.fw-crit {
    border-bottom: 1px solid var(--color-border-subtle, #e6dcc8);
    border-inline-start: 3px solid transparent;
    transition: border-color var(--duration-fast, 150ms) var(--ease-out), background var(--duration-fast, 150ms) var(--ease-out);
}
.fw-crit-open {
    border-inline-start-color: var(--fw-accent);
    background: var(--color-surface-sunken, #f3ecdd);
    background: color-mix(in srgb, var(--fw-accent) 6%, transparent);
}

.fw-crit-head {
    width: 100%;
    display: flex;
    align-items: center;
    gap: var(--space-2, 8px);
    padding: var(--space-2, 8px) var(--space-3, 12px);
    background: transparent;
    border: none;
    cursor: pointer;
    font-family: var(--font-ui, sans-serif);
    font-size: 0.9rem;
    color: var(--color-text-primary, #333);
    text-align: start;
}
.fw-crit-head:hover {
    background: var(--color-surface-sunken, #efe7d8);
    background: color-mix(in srgb, var(--fw-accent) 10%, transparent);
}
.fw-crit-open > .fw-crit-head { color: var(--fw-accent); font-weight: var(--font-weight-semibold, 600); }

.fw-crit-caret { color: var(--fw-accent, var(--color-text-muted, #8a6d4b)); font-size: 0.75rem; width: 1em; }
.fw-crit-label { flex: 1; display: flex; align-items: center; min-width: 0; }
/* Coloured dot keys each row to its accent. */
.fw-crit-label::before {
    content: "";
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--fw-accent);
    margin-inline-end: 7px;
    flex-shrink: 0;
}
.fw-crit-badge { color: var(--fw-accent); font-weight: 700; }

.fw-crit-body {
    padding: var(--space-2, 8px);
    display: flex;
    flex-direction: column;
    gap: var(--space-2, 8px);
    max-height: 46vh;
    border-inline-start: 2px solid var(--fw-accent);
    border-inline-start-color: color-mix(in srgb, var(--fw-accent) 45%, transparent);
    background: var(--color-surface-sunken, #f3ecdd);
    background: color-mix(in srgb, var(--fw-accent) 12%, var(--color-surface-sunken, #f3ecdd));
}
.fw-crit-body .fw-scroll { max-height: 38vh; }

.fw-author-list { display: flex; flex-direction: column; gap: 2px; }
.fw-author {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2, 8px);
    padding: 5px var(--space-2, 8px);
    border: none;
    background: transparent;
    border-radius: var(--radius-sm, 4px);
    cursor: pointer;
    text-align: start;
    font-family: var(--font-primary, 'Amiri', serif);
    font-size: 0.95rem;
    color: var(--color-text-primary, #333);
}
.fw-author:hover { background: var(--color-surface-primary, #fff); }
.fw-author-sel { background: var(--color-primary, #8B4513); color: var(--color-text-inverse, #fff); }
.fw-author-sel:hover { background: var(--color-primary-dark, #6B3410); }
.fw-author-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fw-author-count { font-size: 0.75rem; opacity: 0.8; font-variant-numeric: tabular-nums; }

/* ---- category bulk-select checkbox (RIGHT column authors + genres) ----
   One primary box in single mode; two group-coloured boxes (١/٢) in two-group
   mode, matching the two-group palette (--group1-color / --group2-color).
   Adds/removes ALL of a category's books in one click — the mirror of the
   middle column's "add all" button. Tri-state: empty / dashed(some) / filled(all). */
.fw-author-row { display: flex; align-items: center; gap: 4px; }
.fw-author-row .fw-author { flex: 1 1 auto; min-width: 0; }
.gbt-parent .gbt-label { flex: 1 1 auto; min-width: 0; }
.gbt-child-row { display: flex; align-items: center; gap: 2px; }
.gbt-child-row .gbt-label { flex: 1 1 auto; min-width: 0; }

.fw-catboxes { display: inline-flex; gap: 4px; flex: 0 0 auto; }
.fw-catbox {
    width: 18px; height: 18px; flex: 0 0 auto; padding: 0;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: var(--radius-sm, 4px);
    background: var(--color-surface-primary, #fff);
    cursor: pointer;
    font-size: 0.72rem; line-height: 1; font-weight: 700;
    transition: background 0.12s ease, border-color 0.12s ease;
}
.fw-catbox--single { border: 2px solid var(--color-primary, #8B4513); color: var(--color-primary, #8B4513); }
.fw-catbox--g1 { border: 2px solid var(--group1-color, #b8742a); color: var(--group1-color, #b8742a); }
.fw-catbox--g2 { border: 2px solid var(--group2-color, #556B2F); color: var(--group2-color, #556B2F); }
/* partial = SOME (not all) of the category's books are in this bucket — the rest are
   unassigned or, for overlapping genres, sitting in the OTHER group. Dashed border PLUS a
   light fill so it reads clearly as "partially selected", distinct from empty (no fill) and
   full (solid fill). */
.fw-catbox.partial { border-style: dashed; }
.fw-catbox--single.partial { background: color-mix(in srgb, var(--color-primary, #8B4513) 30%, transparent); }
.fw-catbox--g1.partial { background: color-mix(in srgb, var(--group1-color, #b8742a) 30%, transparent); }
.fw-catbox--g2.partial { background: color-mix(in srgb, var(--group2-color, #556B2F) 30%, transparent); }
.fw-catbox--single.on { background: var(--color-primary, #8B4513); color: var(--color-text-inverse, #fff); }
.fw-catbox--g1.on { background: var(--group1-color, #b8742a); color: var(--color-text-inverse, #fff); }
.fw-catbox--g2.on { background: var(--group2-color, #556B2F); color: var(--color-text-inverse, #fff); }
.fw-catbox:hover { filter: brightness(0.95); }
.fw-catbox:focus-visible { outline: 2px solid var(--color-primary, #8B4513); outline-offset: 1px; }

/* ---- book rows (MIDDLE + LEFT) ---- */
.fw-book-list { display: flex; flex-direction: column; gap: 2px; }
.fw-book {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 2px var(--space-2, 8px);
    padding: 5px var(--space-2, 8px);
    border: 1px solid transparent;
    background: var(--color-surface-primary, #fff);
    border-radius: var(--radius-sm, 4px);
    cursor: pointer;
    text-align: start;
    width: 100%;
    box-sizing: border-box;
}
.fw-book:hover { border-color: var(--color-primary, #8B4513); }
.fw-book-title {
    grid-column: 1;
    font-family: var(--font-primary, 'Amiri', serif);
    /* Amiri (Naskh) needs ~1rem to stay legible over a long curation session;
       0.92rem sat below the Arabic legibility floor (Ibrahim, P2 audit). */
    font-size: 1rem;
    color: var(--color-text-primary, #333);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.fw-book-author {
    grid-column: 1;
    font-size: 0.75rem;
    color: var(--color-text-secondary, #666);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.fw-book-chosen { background: var(--color-surface-secondary, #F5EDE0); }
.fw-book-muted { opacity: 0.55; }
.fw-remove {
    grid-column: 2;
    grid-row: 1 / span 2;
    color: var(--color-text-secondary, #999);
    font-size: 1.1rem;
    line-height: 1;
}
.fw-book-chosen:hover .fw-remove { color: var(--color-error, #b3261e); }

/* ---- settle animation ----
   A just-retagged / reordered row wears "fw-book-settled" for ~450ms after the
   resort commits (the Rust lane toggles the class); the row slides down into its
   new slot with a soft accent flash so the move reads as a deliberate move, not a
   teleport. The flash is an inset box-shadow overlay rather than a background so
   it layers over ANY resting row background (chosen rows differ) and returns to
   nothing when the animation ends. Reduced-motion users get no animation. */
@media (prefers-reduced-motion: no-preference) {
    @keyframes fwBookSettle {
        0% {
            transform: translateY(-6px);
            box-shadow: inset 0 0 0 999px color-mix(in srgb, var(--color-primary, #8B4513) 20%, transparent);
        }
        35% {
            box-shadow: inset 0 0 0 999px color-mix(in srgb, var(--color-primary, #8B4513) 14%, transparent);
        }
        100% {
            transform: none;
            box-shadow: inset 0 0 0 999px transparent;
        }
    }
    .fw-book-settled {
        animation: fwBookSettle 450ms var(--ease-out, ease-out);
    }
}

/* Group block headers in the «الكل» curated view */
.fw-group-divider {
    position: sticky;
    top: 0;
    z-index: 1;
    padding: 3px var(--space-2, 8px);
    font-family: var(--font-ui, sans-serif);
    font-size: 0.72rem;
    font-weight: var(--font-weight-semibold, 600);
    background: var(--color-surface-sunken, #f3ecdd);
    border-inline-start: 3px solid var(--color-border-secondary, #D4C4A8);
    border-radius: var(--radius-sm, 4px);
    margin-top: var(--space-1, 4px);
}
.fw-group-divider--g1 {
    color: var(--group1-color, var(--color-primary, #8B4513));
    border-inline-start-color: var(--group1-color, var(--color-primary, #8B4513));
}
.fw-group-divider--g2 {
    color: var(--group2-color, #9A8466);
    border-inline-start-color: var(--group2-color, #9A8466);
}

.fw-outrange { margin-top: var(--space-2, 8px); border-top: 1px dashed var(--color-border-secondary, #D4C4A8); padding-top: var(--space-2, 8px); }
.fw-outrange-head {
    font-size: 0.75rem;
    color: var(--color-text-muted, #8a6d4b);
    padding: 2px var(--space-2, 8px) 4px;
}

/* ---- states / hints ---- */
.fw-empty, .fw-loading, .fw-hint {
    padding: var(--space-4, 16px);
    text-align: center;
    color: var(--color-text-secondary, #666);
    font-family: var(--font-ui, sans-serif);
    font-size: 0.85rem;
    line-height: 1.5;
}
.fw-hint { text-align: start; }
.fw-more {
    padding: var(--space-2, 8px);
    text-align: center;
    color: var(--color-text-muted, #8a6d4b);
    font-size: 0.75rem;
    font-family: var(--font-ui, sans-serif);
}

/* ---- genre browse tree ---- */
.genre-browse-tree { display: flex; flex-direction: column; gap: 1px; }
.gbt-parent { display: flex; align-items: center; gap: 2px; }
.gbt-expander, .gbt-expander-spacer {
    width: 20px;
    flex-shrink: 0;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--color-text-muted, #8a6d4b);
    font-size: 0.7rem;
}
.gbt-label {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2, 8px);
    padding: 4px var(--space-2, 8px);
    border: none;
    background: transparent;
    border-radius: var(--radius-sm, 4px);
    cursor: pointer;
    text-align: start;
    font-family: var(--font-primary, 'Amiri', serif);
    font-size: 0.9rem;
    color: var(--color-text-primary, #333);
}
.gbt-label:hover { background: var(--color-surface-primary, #fff); }
.gbt-selected { background: var(--color-primary, #8B4513); color: var(--color-text-inverse, #fff); }
.gbt-selected:hover { background: var(--color-primary-dark, #6B3410); }
.gbt-child { padding-inline-start: var(--space-4, 16px); font-size: 0.85rem; }
.gbt-children { margin-inline-start: 10px; border-inline-start: 1px solid var(--color-border-subtle, #e6dcc8); }
.gbt-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gbt-count { font-size: 0.72rem; opacity: 0.75; font-variant-numeric: tabular-nums; }

/* ---- summary strip (always visible: orientation across modes) ---- */
.fw-summary {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-2, 8px);
    padding: var(--space-2, 8px) var(--space-4, 16px);
    background: var(--color-surface-sunken, #f3ecdd);
    border-bottom: 1px solid var(--color-border-subtle, #e6dcc8);
    font-family: var(--font-ui, sans-serif);
    font-size: 0.78rem;
}
.fw-summary-pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background: var(--color-surface-primary, #fff);
    border: 1px solid var(--color-border-secondary, #D4C4A8);
    border-radius: var(--radius-full, 9999px);
    padding: 2px 10px;
    color: var(--color-text-primary, #333);
    font-variant-numeric: tabular-nums;
}
.fw-summary-pill--g1 { border-color: var(--group1-color, var(--color-primary, #8B4513)); color: var(--group1-color, var(--color-primary, #8B4513)); }
.fw-summary-pill--g2 { border-color: var(--group2-color, #9A8466); color: var(--group2-color, #9A8466); }
.fw-summary-crit { border-inline-start: 3px solid var(--fw-accent, var(--color-primary)); }
.fw-pill--death      { --fw-accent: #8a5273; }
.fw-pill--quality    { --fw-accent: #b0842f; }
.fw-pill--similarity { --fw-accent: #4a6c9b; }
.fw-pill--length     { --fw-accent: #7c6a9c; }
.fw-pill--isnad      { --fw-accent: #a05a3c; }
.fw-pill--quran      { --fw-accent: #2f7d55; }
.fw-pill--scope      { --fw-accent: #4f7f63; }
.fw-pill-x {
    border: none;
    background: transparent;
    cursor: pointer;
    color: var(--color-text-secondary, #999);
    font-size: 0.9rem;
    line-height: 1;
    padding: 0 2px;
}
.fw-pill-x:hover { color: var(--color-error, #b3261e); }
.fw-back-to-books {
    border: 1px solid var(--color-border-secondary, #D4C4A8);
    background: var(--color-surface-primary, #fff);
    border-radius: var(--radius-sm, 4px);
    padding: 2px 10px;
    cursor: pointer;
    font-family: var(--font-ui, sans-serif);
    font-size: 0.78rem;
    color: var(--color-primary, #8B4513);
}
.fw-back-to-books:hover { border-color: var(--color-primary, #8B4513); }

/* ---- takeover layout ---- */
.fw-body--takeover { grid-template-columns: minmax(210px, 1fr) 2.4fr; }
.fw-wide {
    min-height: 0;
    display: flex;
    flex-direction: column;
    border-inline-start: 1px solid var(--color-border-primary, #D4C4A8);
}
.fw-wide-head {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: var(--space-2, 8px);
    padding: var(--space-2, 8px) var(--space-4, 16px);
    background: var(--color-surface-secondary, #F5EDE0);
    background: color-mix(in srgb, var(--fw-accent) 10%, var(--color-surface-secondary, #F5EDE0));
    border-bottom: 2px solid var(--fw-accent);
    font-family: var(--font-ui, sans-serif);
    font-weight: var(--font-weight-semibold, 600);
    color: var(--fw-accent);
}
.fw-wide-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--fw-accent); }
.fw-wide-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: var(--space-5, 20px);
    display: flex;
    flex-direction: column;
    gap: var(--space-4, 16px);
    max-width: 560px;
}

/* ---- segmented controls (isnad, add-to, group toggle, view filter) ---- */
.fw-seg {
    display: inline-flex;
    border: 1px solid var(--color-border-secondary, #D4C4A8);
    border-radius: var(--radius-sm, 4px);
    overflow: hidden;
    flex-shrink: 0;
}
.fw-seg-btn {
    border: none;
    background: var(--color-surface-primary, #fff);
    padding: 4px 10px;
    cursor: pointer;
    font-family: var(--font-ui, sans-serif);
    font-size: 0.78rem;
    color: var(--color-text-secondary, #666);
    border-inline-start: 1px solid var(--color-border-subtle, #e6dcc8);
}
.fw-seg-btn:first-child { border-inline-start: none; }
.fw-seg-btn:hover { color: var(--color-text-primary, #333); }
/* Active segment: the button always carries BOTH classes, so the compound
   .fw-seg-btn.fw-seg-on outranks .fw-seg-btn:hover on its own — no !important. */
.fw-seg-btn.fw-seg-on { background: var(--color-primary, #8B4513); color: var(--color-text-inverse, #fff); }
.fw-seg--g1.fw-seg-on { background: var(--group1-color, var(--color-primary, #8B4513)); }
.fw-seg--g2.fw-seg-on { background: var(--group2-color, #9A8466); }
.fw-addto { display: inline-flex; align-items: center; gap: 6px; flex-shrink: 0; }
.fw-addto-label { font-family: var(--font-ui, sans-serif); font-size: 0.75rem; color: var(--color-text-secondary, #666); white-space: nowrap; }
.fw-tg-seg { margin: var(--space-2, 8px); align-self: flex-start; }
.fw-tg-hint { padding: var(--space-1, 4px) var(--space-2, 8px); font-size: 0.72rem; color: var(--color-text-muted, #8a6d4b); font-family: var(--font-ui, sans-serif); }
.fw-view-seg { flex-shrink: 0; }

/* ---- group chips on chosen rows ---- */
.fw-book-chosen { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: var(--space-2, 8px); padding: 0; }
.fw-book-main {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 2px var(--space-2, 8px);
    padding: 5px var(--space-2, 8px);
    border: none;
    background: transparent;
    cursor: pointer;
    text-align: start;
    width: 100%;
    box-sizing: border-box;
    min-width: 0;
}
.fw-group-chips { display: inline-flex; gap: 2px; padding-inline-start: 4px; }
.fw-gchip {
    width: 20px;
    height: 20px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--color-border-secondary, #D4C4A8);
    border-radius: var(--radius-sm, 4px);
    font-size: 0.72rem;
    cursor: pointer;
    color: var(--color-text-secondary, #999);
    background: var(--color-surface-primary, #fff);
    user-select: none;
}
.fw-gchip--g1.fw-gchip-on { background: var(--group1-color, var(--color-primary, #8B4513)); border-color: var(--group1-color, var(--color-primary, #8B4513)); color: var(--color-text-inverse, #fff); }
.fw-gchip--g2.fw-gchip-on { background: var(--group2-color, #9A8466); border-color: var(--group2-color, #9A8466); color: var(--color-text-inverse, #fff); }
.fw-count--g1 { background: var(--group1-color, var(--color-primary, #8B4513)); }
.fw-count--g2 { background: var(--group2-color, #9A8466); }
/* Chips are real <button>s now (keyboard-operable); the global button reset
   already strips padding/border/background so the pill look is unchanged. Give
   the chips and the close «×» a visible keyboard focus ring. */
.fw-gchip:focus-visible,
.fw-close:focus-visible {
    outline: 2px solid var(--color-primary, #8B4513);
    outline-offset: 2px;
}

/* ---- takeover form bits ---- */
.fw-field-row { display: flex; align-items: center; gap: var(--space-3, 12px); }
.fw-field-row label { min-width: 180px; font-family: var(--font-ui, sans-serif); font-size: 0.85rem; color: var(--color-text-primary, #333); }
.fw-number {
    width: 110px;
    padding: 6px var(--space-2, 8px);
    border: 1px solid var(--color-border-secondary, #D4C4A8);
    border-radius: var(--radius-sm, 4px);
    background: var(--color-surface-primary, #fff);
    color: var(--color-text-primary, #333);
    font-family: var(--font-ui, sans-serif);
    direction: ltr;
    text-align: center;
}
.fw-radio {
    display: grid;
    grid-template-columns: auto auto 1fr;
    align-items: baseline;
    gap: var(--space-2, 8px);
    padding: var(--space-2, 8px);
    border: 1px solid var(--color-border-subtle, #e6dcc8);
    border-radius: var(--radius-sm, 4px);
    cursor: pointer;
    background: var(--color-surface-primary, #fff);
}
.fw-radio-disabled { opacity: 0.5; cursor: not-allowed; }
.fw-radio-label { font-family: var(--font-ui, sans-serif); font-weight: var(--font-weight-semibold, 600); font-size: 0.88rem; }
.fw-radio-desc { font-family: var(--font-ui, sans-serif); font-size: 0.76rem; color: var(--color-text-secondary, #666); }

/* ---- misc ---- */
.fw-restrict-note {
    flex-shrink: 0;
    padding: var(--space-1, 4px) var(--space-2, 8px);
    font-family: var(--font-ui, sans-serif);
    font-size: 0.72rem;
    color: var(--color-text-muted, #8a6d4b);
    background: var(--color-surface-sunken, #f3ecdd);
    border-bottom: 1px solid var(--color-border-subtle, #e6dcc8);
}
.fw-apply-block {
    font-family: var(--font-ui, sans-serif);
    font-size: 0.8rem;
    color: var(--color-error, #b3261e);
}
.fw-modal-loading { height: auto; min-height: 120px; justify-content: center; }

/* ---- responsive: stack the columns ---- */
@media (max-width: 768px) {
    .fw-modal { width: 100%; height: 100%; max-height: 100%; border-radius: 0; }
    .fw-body, .fw-body--takeover {
        grid-template-columns: 1fr;
        grid-auto-rows: minmax(180px, auto);
        overflow-y: auto;
    }
    .fw-col, .fw-wide { border-inline-start: none; border-top: 1px solid var(--color-border-primary, #D4C4A8); }
    .fw-col:first-child { border-top: none; }
    .fw-summary { row-gap: 4px; }
}
