/* ============================================================
   AFP Manage — rule + mapping CRUD pages.
   Loads on top of afp_data.css. All tokens (--primary, --ink,
   --border, etc.) come from afp_data.css.
   ============================================================ */

/* ===== Back link spacing ===== */
.afp-back-row { margin-bottom: 0.75rem; }

/* ===== Override Bootstrap's blue btn-primary with the module's navy.
   Scoped to .afp-dashboard so the platform-wide primary stays untouched
   elsewhere. Both default and -sm sizes are covered. ===== */
.afp-dashboard .btn-primary,
.afp-dashboard .btn-primary:focus {
    background: linear-gradient(135deg, hsl(var(--primary)) 0%, hsl(var(--primary-deep)) 100%);
    border-color: hsl(var(--primary-deep));
    color: #fff;
    box-shadow: var(--shadow-sm);
}
.afp-dashboard .btn-primary:hover,
.afp-dashboard .btn-primary:active {
    background: hsl(var(--primary-deep));
    border-color: hsl(var(--primary-deep));
    color: #fff;
}
.afp-dashboard .btn-outline-secondary {
    color: hsl(var(--muted-foreground));
    border-color: hsl(var(--border-strong));
    background: #fff;
}
.afp-dashboard .btn-outline-secondary:hover {
    color: hsl(var(--primary));
    border-color: hsl(var(--primary));
    background: hsl(var(--background));
}

/* ===== Summary tiles (manage_status) ===== */
.afp-manage-summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}
.afp-manage-summary-card .afp-card-content { padding: 1.1rem 1.25rem 0.5rem; }
.afp-manage-summary-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem 1rem;
    margin-bottom: 0.9rem;
}
.afp-manage-stat .label {
    display: block;
    font-size: 0.68rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: hsl(var(--muted-foreground));
    margin-bottom: 0.15rem;
}
.afp-manage-stat .value {
    font-size: 1.35rem;
    font-weight: 700;
    color: hsl(var(--ink));
    font-variant-numeric: tabular-nums;
    line-height: 1;
}
.afp-manage-stat .value.muted { color: hsl(var(--muted-foreground)); font-weight: 600; }
.afp-manage-breakdown {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    margin-bottom: 0.9rem;
    max-height: 240px;
    overflow-y: auto;
}
.afp-manage-breakdown-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8rem;
    color: hsl(var(--ink));
}
.afp-manage-breakdown-row code {
    background: hsl(var(--background));
    color: hsl(var(--primary));
    padding: 0.05rem 0.4rem;
    border-radius: 0.3rem;
    font-size: 0.7rem;
}
.afp-manage-breakdown-row .count {
    margin-left: auto;
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    color: hsl(var(--muted-foreground));
}
.afp-manage-card-action {
    border-top: 1px solid hsl(var(--border));
    padding: 0.7rem 1.25rem;
    background: hsl(var(--background));
}
.afp-manage-card-action a {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.82rem;
    font-weight: 600;
    color: hsl(var(--primary));
    text-decoration: none;
}
.afp-manage-card-action a:hover {
    color: hsl(var(--primary-deep));
    text-decoration: none;
}
.afp-manage-card-action a svg { width: 0.9rem; height: 0.9rem; }
.afp-empty-line { color: hsl(var(--muted-foreground)); font-size: 0.8rem; padding: 0.25rem 0; }

/* ===== Unified Rule Admin: type pills + description =====
   4 pills selecting the rule type (Fund Name / Categories / Assets / Regions).
   The active pill picks up the navy gradient; others stay neutral with a
   subtle hover. Counts live at the right edge of each pill. */
.afp-rule-types {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 0.55rem;
    margin-bottom: 0.85rem;
}
.afp-rule-type-pill {
    display: inline-flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.65rem 0.9rem;
    border-radius: var(--radius-sm);
    background: #fff;
    color: hsl(var(--ink));
    border: 1px solid hsl(var(--border));
    box-shadow: var(--shadow-sm);
    text-decoration: none;
    transition: background-color 150ms var(--ease),
                border-color 150ms var(--ease),
                color 150ms var(--ease),
                box-shadow 150ms var(--ease);
}
.afp-rule-type-pill:hover {
    color: hsl(var(--primary));
    border-color: hsl(var(--primary));
    text-decoration: none;
}
.afp-rule-type-pill svg {
    width: 1rem; height: 1rem;
    color: hsl(var(--muted-foreground));
    flex-shrink: 0;
}
.afp-rule-type-label {
    flex: 1;
    font-size: 0.88rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.afp-rule-type-count {
    font-variant-numeric: tabular-nums;
    font-size: 0.78rem;
    font-weight: 700;
    color: hsl(var(--primary));
    background: hsl(210 29% 95%);
    padding: 0.15rem 0.5rem;
    border-radius: 0.3rem;
}
.afp-rule-type-count-total {
    color: hsl(var(--muted-foreground));
    font-weight: 500;
}
.afp-rule-type-pill.active {
    background: linear-gradient(135deg, hsl(var(--primary)) 0%, hsl(var(--primary-deep)) 100%);
    color: #fff;
    border-color: hsl(var(--primary-deep));
}
.afp-rule-type-pill.active svg { color: rgba(255, 255, 255, 0.85); }
.afp-rule-type-pill.active .afp-rule-type-count {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}
.afp-rule-type-pill.active .afp-rule-type-count-total { color: rgba(255, 255, 255, 0.6); }

.afp-rule-type-desc {
    font-size: 0.82rem;
    color: hsl(var(--muted-foreground));
    margin: 0 0 1rem;
    padding: 0.5rem 0.75rem;
    background: hsl(var(--background));
    border-left: 3px solid hsl(var(--primary));
    border-radius: var(--radius-sm);
}

/* ===== Toolbar: Back link (left) + action buttons (right) =====
   Single row replacing the old afp-back-row + afp-action-row pair. The
   .afp-back link sits flush left; .afp-manage-actions hugs the right edge
   via margin-left:auto so it stays visually anchored regardless of how
   long the back-link text becomes. */
.afp-manage-toolbar {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}
.afp-manage-toolbar > .afp-back {
    margin-bottom: 0;     /* override the default trailing margin from afp_funds.css */
}
.afp-manage-actions {
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    gap: 0.55rem;
}

/* Compact square icon button -- copied from the funds-explorer filter
   toggle so the visual matches. White background, navy outline on hover,
   inverted (navy fill + white icon) when its panel is open. */
.afp-mgmt-iconbtn {
    flex: 0 0 auto;
    display: inline-flex; align-items: center; justify-content: center;
    width: 2.35rem; height: 2.35rem;
    padding: 0;
    background: #fff;
    border: 1px solid hsl(var(--border));
    color: hsl(var(--primary));
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color 150ms var(--ease),
                border-color 150ms var(--ease),
                box-shadow 150ms var(--ease),
                color 150ms var(--ease);
}
.afp-mgmt-iconbtn:hover {
    border-color: hsl(var(--primary));
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.06);
}
.afp-mgmt-iconbtn svg { width: 1.05rem; height: 1.05rem; }
.afp-mgmt-iconbtn.active {
    background: hsl(var(--primary));
    border-color: hsl(var(--primary));
    color: #fff;
}

.afp-toggle-panel {
    display: none;
    margin-bottom: 1.5rem;
}
.afp-toggle-panel.show { display: block; }

/* ===== Rule + mapping tables ===== */
/* Horizontal scroll wrapper: the table only scrolls when its min-width
   exceeds the available viewport width (i.e. on phones). On desktop the
   container is wider than the table's min-width so nothing scrolls and
   the layout is identical to before. iOS momentum scrolling enabled. */
.afp-table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.afp-table-scroll .afp-rule-table { min-width: 720px; }

.afp-rule-table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
.afp-rule-table thead th {
    background: hsl(var(--background));
    color: hsl(var(--muted-foreground));
    font-size: 0.7rem; font-weight: 600;
    text-transform: uppercase; letter-spacing: 0.05em;
    padding: 0.55rem 0.85rem;
    border-bottom: 1px solid hsl(var(--border));
    text-align: left;
    white-space: nowrap;
}
.afp-rule-table tbody td {
    padding: 0.7rem 0.85rem;
    border-bottom: 1px solid hsl(var(--border));
    vertical-align: middle;
}
.afp-rule-table tbody tr:hover { background: hsl(var(--background)); }
.afp-rule-table tbody tr.rule-inactive { opacity: 0.55; }
.afp-rule-table code {
    background: hsl(var(--background));
    color: hsl(var(--primary));
    padding: 0.1rem 0.4rem;
    border-radius: 0.3rem;
    font-size: 0.78rem;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.afp-edit-row { background: hsl(var(--background)) !important; }
.afp-edit-row td { padding: 0.5rem 0.85rem !important; }

/* ===== Rule title (column "Title") =====
   The "title" column collapses pattern, target, mapping_type, etc. into one
   readable line. Used by every CRUD page. */
.afp-rule-title { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.afp-rule-title-text {
    font-weight: 600; color: hsl(var(--ink));
    font-size: 0.92rem;
}
.afp-rule-title-text .arrow { color: hsl(var(--muted-foreground)); margin: 0 0.35rem; font-weight: 400; }
.afp-rule-title-text .target {
    color: hsl(var(--primary));
}
.afp-rule-title-text code {
    background: hsl(var(--background));
    color: hsl(var(--primary));
    padding: 0.05rem 0.45rem;
    border-radius: 0.3rem;
    font-size: 0.85rem;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.afp-rule-subtitle {
    font-size: 0.72rem;
    color: hsl(var(--muted-foreground));
    margin-top: 0.2rem;
}

/* ===== Status + priority pills ===== */
.afp-status-pill {
    display: inline-flex; align-items: center;
    padding: 0.15rem 0.55rem; border-radius: 999px;
    font-size: 0.7rem; font-weight: 600;
}
.afp-status-pill.active { background: hsl(var(--pos-bg)); color: hsl(160 84% 30%); }
.afp-status-pill.inactive { background: hsl(0 0% 95%); color: hsl(var(--muted-foreground)); }

.afp-prio-pill {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 30px;
    padding: 0.1rem 0.45rem; border-radius: 0.3rem;
    background: hsl(210 29% 95%); color: hsl(var(--primary));
    font-size: 0.78rem; font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.afp-regex-pill {
    display: inline-flex; align-items: center;
    padding: 0.05rem 0.4rem; border-radius: 0.25rem;
    background: hsl(280 70% 95%); color: hsl(280 70% 40%);
    font-size: 0.65rem; font-weight: 700;
    letter-spacing: 0.04em;
}

/* Empty / whitespace-only replacement shows as a grey BLANK pill so the
   intent ("strip this substring from the name") reads as deliberate
   instead of looking like a broken row. */
.afp-blank-tag {
    display: inline-flex; align-items: center;
    padding: 0.05rem 0.5rem; border-radius: 0.25rem;
    background: hsl(0 0% 92%);
    color: hsl(var(--muted-foreground));
    font-size: 0.68rem; font-weight: 700;
    letter-spacing: 0.08em;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
}

/* ===== Type pills (rule_type, target_field, mapping_type) ===== */
.afp-type-pill {
    display: inline-block;
    padding: 0.1rem 0.55rem; border-radius: 0.3rem;
    font-size: 0.68rem; font-weight: 600;
    color: #fff; text-transform: uppercase;
    letter-spacing: 0.03em;
}
.rt-firm_name           { background: #7c3aed; }
.rt-asset_class_keyword { background: #2563eb; }
.rt-geography_keyword   { background: #059669; }
.rt-isin_country        { background: #d97706; }
.rt-isin_pattern        { background: #db2777; }

/* Unified rule admin: one pill colour per rule type. */
.afp-type-fund_name   { background: #2C3E50; }
.afp-type-category    { background: #2563eb; }
.afp-type-asset       { background: #d97706; }
.afp-type-region      { background: #059669; }
.afp-type-credit_type { background: #7c3aed; }
.mt-geography_region     { background: #059669; }
.mt-region_order         { background: #0891b2; }
.mt-asset_class_group    { background: #2563eb; }
.mt-market_cap_normalize { background: #d97706; }
.mt-style_normalize      { background: #ca8a04; }
.mt-fi_credit_normalize  { background: #7c3aed; }
.mt-isin_name_prefix     { background: #db2777; }

.afp-target-pill {
    display: inline-block;
    padding: 0.1rem 0.5rem; border-radius: 0.3rem;
    background: hsl(var(--background));
    color: hsl(var(--muted-foreground));
    font-size: 0.7rem; font-weight: 600;
    border: 1px solid hsl(var(--border));
}

/* ===== Direct activation switch (in actions column) =====
   Visual switch that submits a tiny one-field form on change. */
.afp-switch {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 20px;
    vertical-align: middle;
}
.afp-switch input { opacity: 0; width: 0; height: 0; }
.afp-switch .slider {
    position: absolute; cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: hsl(var(--border-strong));
    border-radius: 999px;
    transition: background-color 180ms var(--ease);
}
.afp-switch .slider::before {
    position: absolute; content: "";
    height: 14px; width: 14px;
    left: 3px; bottom: 3px;
    background-color: #fff;
    border-radius: 50%;
    transition: transform 180ms var(--ease);
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}
.afp-switch input:checked + .slider {
    background-color: hsl(var(--chart-2));
}
.afp-switch input:checked + .slider::before { transform: translateX(16px); }
.afp-switch:hover .slider { box-shadow: 0 0 0 3px hsl(var(--border)); }

/* ===== Icon buttons (edit, delete) ===== */
.afp-icon-btn {
    background: transparent; border: 1px solid hsl(var(--border));
    color: hsl(var(--muted-foreground));
    padding: 0.25rem 0.55rem;
    border-radius: var(--radius-sm); font-size: 0.8rem;
    cursor: pointer; transition: all 150ms var(--ease);
}
.afp-icon-btn:hover { color: hsl(var(--primary)); border-color: hsl(var(--primary)); }
.afp-icon-btn.danger:hover { color: hsl(0 84% 50%); border-color: hsl(0 84% 50%); }

.afp-actions-cell {
    display: inline-flex; align-items: center; gap: 0.5rem;
    white-space: nowrap;
}

/* ===== Form labels ===== */
.afp-form-label {
    font-size: 0.72rem; font-weight: 600;
    text-transform: uppercase; letter-spacing: 0.04em;
    color: hsl(var(--muted-foreground));
    margin-bottom: 0.25rem;
}

/* ===== Force inputs to fill their column =====
   Bootstrap defaults to width:100% on form-control/form-select, but inside
   a CSS grid container some browsers compute the intrinsic min-width too
   large which makes the elements overflow OR appear narrower than expected.
   Locking min-width:0 + width:100% + box-sizing:border-box fixes both. */
.afp-dashboard .form-control,
.afp-dashboard .form-control-sm,
.afp-dashboard .form-select,
.afp-dashboard .form-select-sm {
    width: 100% !important;
    min-width: 0;
    box-sizing: border-box;
}

/* ===== Lock identical height for input + select inside the manage forms =====
   Bootstrap renders <input class="form-control-sm"> and <select class="form-select-sm">
   at slightly different intrinsic heights because <select> adds room for the
   dropdown chevron via padding-right + background-image. Without this override
   the Type dropdown sits ~2px taller than the Source/Target text inputs,
   breaking the row's visual alignment.

   Pinning the same height + padding + line-height + border-radius on both
   makes them sit on the same baseline. The chevron stays visible via the
   reserved padding-right and background-position. */
.afp-dashboard .form-control-sm,
.afp-dashboard .form-select-sm {
    height: 31px !important;        /* identical pixel height for both controls */
    min-height: 31px;
    padding: 0.25rem 0.55rem;
    font-size: 0.85rem;
    line-height: 1.4;
    border: 1px solid hsl(var(--border));
    border-radius: var(--radius-sm);
    background-color: #fff;
}
.afp-dashboard .form-select-sm {
    /* Reserve room for the chevron without changing the box height. */
    padding-right: 1.75rem;
    background-position: right 0.55rem center;
    background-size: 12px 10px;
    appearance: none;
    -webkit-appearance: none;
}
.afp-dashboard .form-control-sm:focus,
.afp-dashboard .form-select-sm:focus {
    border-color: hsl(var(--primary));
    box-shadow: 0 0 0 2px rgba(44, 62, 80, 0.12);
    outline: none;
}

.afp-add-form .row,
.afp-edit-form .row {
    row-gap: 0.75rem;
}
.afp-add-form .row .form-control,
.afp-add-form .row .form-select,
.afp-edit-form .row .form-control,
.afp-edit-form .row .form-select {
    width: 100%;
    box-sizing: border-box;
}
.afp-help-note {
    background: hsl(var(--background));
    border-left: 3px solid hsl(var(--primary));
    padding: 0.7rem 1rem;
    font-size: 0.82rem; color: hsl(var(--ink));
    border-radius: var(--radius-sm);
    margin-bottom: 1rem;
}
.afp-help-note code {
    background: rgba(255, 255, 255, 0.7);
    color: hsl(var(--primary));
    padding: 0.05rem 0.3rem;
    border-radius: 0.25rem;
    font-size: 0.78rem;
}

.afp-filter-bar {
    background: hsl(var(--background));
    border: 1px solid hsl(var(--border));
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
}

/* ===== In-app confirm modal — replaces browser confirm() / alert() =====
   Used by the unified delete handler so the user gets a styled, brand-coherent
   prompt instead of the OS-level dialog. Created lazily by afpConfirm() in
   afp_manage.js; we just style the markup here. */
.afp-confirm-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.48);
    z-index: 10000;
    display: flex; align-items: center; justify-content: center;
    animation: afp-confirm-fade-in 140ms var(--ease-out, ease-out);
}
@keyframes afp-confirm-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}
.afp-confirm-card {
    background: #fff;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg, 0 16px 40px rgba(15, 23, 42, 0.12));
    width: min(420px, 92vw);
    overflow: hidden;
    animation: afp-confirm-rise 200ms cubic-bezier(0.23, 1, 0.32, 1);
}
@keyframes afp-confirm-rise {
    from { transform: translateY(12px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}
.afp-confirm-header {
    background: linear-gradient(135deg, hsl(var(--primary)) 0%, hsl(var(--primary-deep)) 100%);
    color: #fff;
    padding: 0.85rem 1.2rem;
    font-size: 0.92rem;
    font-weight: 600;
    letter-spacing: 0.01em;
    display: flex; align-items: center; gap: 0.55rem;
}
.afp-confirm-header svg { width: 1.05rem; height: 1.05rem; opacity: 0.92; }
.afp-confirm-body {
    padding: 1.2rem 1.2rem 1rem;
    font-size: 0.88rem;
    color: hsl(var(--ink));
    line-height: 1.55;
}
.afp-confirm-body strong {
    color: hsl(var(--primary));
    font-weight: 700;
}
.afp-confirm-actions {
    display: flex; justify-content: flex-end; gap: 0.55rem;
    padding: 0.75rem 1.2rem 1.1rem;
    border-top: 1px solid hsl(var(--border));
    background: hsl(var(--background));
}
.afp-confirm-btn {
    padding: 0.45rem 1rem;
    border-radius: var(--radius-sm);
    font-size: 0.84rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 150ms var(--ease),
                color 150ms var(--ease),
                border-color 150ms var(--ease),
                opacity 150ms var(--ease);
    font-family: inherit;
}
.afp-confirm-cancel {
    background: #fff;
    color: hsl(var(--muted-foreground));
    border: 1px solid hsl(var(--border-strong));
}
.afp-confirm-cancel:hover {
    color: hsl(var(--ink));
    border-color: hsl(var(--primary));
}
.afp-confirm-confirm {
    background: hsl(var(--chart-3));
    color: #fff;
    border: 1px solid hsl(var(--chart-3));
}
.afp-confirm-confirm:hover { opacity: 0.92; }
.afp-confirm-confirm:focus,
.afp-confirm-cancel:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(44, 62, 80, 0.18);
}

/* ===== Clear button — mirror Funds Explorer's ghost design =====
   Same shape as ``afp_funds.css:.afp-clear-btn`` so the manage admin
   inherits the visual language. Sits next to the Search + Type inputs
   in the filter row, no longer paired with an Apply button. */
.afp-dashboard .afp-clear-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    height: 31px;                                /* match the form-*-sm controls */
    padding: 0 0.7rem;
    font-size: 0.82rem;
    font-weight: 600;
    border: 1px solid hsl(var(--border-strong, 213 27% 84%));
    background: #fff;
    color: hsl(var(--muted-foreground));
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color 150ms var(--ease),
                border-color 150ms var(--ease),
                color 150ms var(--ease);
    white-space: nowrap;
}
.afp-dashboard .afp-clear-btn svg { width: 0.85rem; height: 0.85rem; }
.afp-dashboard .afp-clear-btn:hover {
    background: hsl(var(--neg-bg, 0 86% 97%));
    border-color: hsl(var(--chart-3, 0 84% 60%) / 0.5);
    color: hsl(var(--chart-3, 0 84% 60%));
}
.afp-dashboard .afp-clear-btn:active { transform: scale(0.97); }

/* ===== Regex toggle inline in forms ===== */
.afp-regex-toggle {
    display: inline-flex; align-items: center; gap: 0.4rem;
    padding: 0.35rem 0.7rem;
    border-radius: var(--radius-sm);
    background: hsl(var(--background));
    border: 1px solid hsl(var(--border));
    font-size: 0.78rem; color: hsl(var(--muted-foreground));
    cursor: pointer;
}
.afp-regex-toggle input { margin: 0; }
.afp-regex-toggle.checked {
    background: hsl(280 70% 95%);
    color: hsl(280 70% 40%);
    border-color: hsl(280 70% 70%);
    font-weight: 600;
}
