/* ============================================================
   Benan CRM — Design System
   One primary accent + neutral scale + semantic colors.
   ============================================================ */

:root {
    /* Primary — the single interactive accent (links, buttons, focus) */
    --c-primary:       #2563eb;            /* blue-600 */
    --c-primary-hover: #1d4ed8;            /* blue-700 */
    --c-primary-soft:  #eff6ff;            /* blue-50  */
    --c-ring:          rgba(37, 99, 235, .35);

    /* Neutrals */
    --c-text:    #1f2937;                  /* slate-800 */
    --c-muted:   #64748b;                  /* slate-500 */
    --c-border:  #e5e7eb;                  /* gray-200  */
    --c-surface: #ffffff;
    --c-hover:   rgba(37, 99, 235, .06);   /* subtle primary-tinted row hover */

    /* Semantic */
    --c-success: #16a34a;                  /* green-600 — also WhatsApp */
    --c-success-hover: #15803d;            /* green-700 */
    --c-warning: #d97706;                  /* amber-600 */
    --c-danger:  #dc2626;                  /* red-600   */
}

/* Align Bootstrap/Orchid's own primary + links to our system,
   so command-bar buttons and built-in links match everything else. */
:root,
body {
    --bs-primary: #2563eb;
    --bs-primary-rgb: 37, 99, 235;
    --bs-link-color: #2563eb;
    --bs-link-color-rgb: 37, 99, 235;
    --bs-link-hover-color: #1d4ed8;
}

.btn-primary {
    --bs-btn-bg: var(--c-primary);
    --bs-btn-border-color: var(--c-primary);
    --bs-btn-hover-bg: var(--c-primary-hover);
    --bs-btn-hover-border-color: var(--c-primary-hover);
    --bs-btn-active-bg: var(--c-primary-hover);
    --bs-btn-active-border-color: var(--c-primary-hover);
    --bs-btn-focus-shadow-rgb: 37, 99, 235;
}

/* ---------- Links (consistent interactive treatment) ---------- */
.crm-link {
    color: var(--c-primary);
    font-weight: 500;
    text-decoration: none;
}
.crm-link:hover {
    color: var(--c-primary-hover);
    text-decoration: underline;
}

/* Secondary / quieter link — used for low-priority row actions (Edit) */
.crm-link-muted {
    color: var(--c-muted);
    font-weight: 500;
    text-decoration: none;
}
.crm-link-muted:hover {
    color: var(--c-primary);
    text-decoration: underline;
}

/* WhatsApp — intentional brand/semantic action, mapped onto the success token */
.crm-wa {
    color: var(--c-success);
    font-weight: 600;
    text-decoration: none;
}
.crm-wa:hover {
    color: var(--c-success-hover);
    text-decoration: underline;
}

/* ---------- Buttons ---------- */
.crm-btn-primary {
    background: var(--c-primary);
    color: #fff;
    border: 0;
    border-radius: .5rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s, box-shadow .15s;
}
.crm-btn-primary:hover { background: var(--c-primary-hover); }
.crm-btn-primary:focus-visible {
    outline: 0;
    box-shadow: 0 0 0 3px var(--c-ring);
}

.crm-reset {
    color: var(--c-muted);
    text-decoration: none;
}
.crm-reset:hover { color: var(--c-text); }

/* ---------- Filter controls ---------- */
.crm-filter-ctrl {
    transition: border-color .15s, box-shadow .15s;
}
.crm-filter-ctrl:focus {
    outline: 0;
    border-color: var(--c-primary);
    box-shadow: 0 0 0 3px var(--c-ring);
}

/* ---------- Table ---------- */
.table tbody tr:hover > td,
.table tbody tr:hover > th {
    background-color: var(--c-hover) !important;
}

/* Responsive tables (design rule 9): every cell sizes to its own content and is
   kept on one line, so columns never get squashed and badges/dates/headers are
   never clipped or wrapped ("Maintenan/ce", "Next/billing"). When the columns add
   up to more than the card, the card itself scrolls horizontally; when they fit,
   `width:100%` makes the table fill the card and share the spare space. This works
   the same at every viewport / zoom level — no breakpoint needed. */
.table-responsive { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; table-layout: auto; }
.table th, .table td { white-space: nowrap; }

/* ============================================================
   Dashboard charts — one unified card system
   ------------------------------------------------------------
   Orchid renders every chart (via Frappe) as this DOM:
     [data-controller="chart"] > .bg-white            (the card)
        > .d-flex > legend > small                    (title row)
        > .position-relative > figure                 (Frappe target)
            > .chart-container
                > svg.frappe-chart                    (the graphic)
                > .chart-legend > .legend-dot/.legend-dataset-text
   The blade styles this with ad-hoc utilities (pt-3 / mb-3 / px-2 / mt-2)
   and NO bottom padding, so legends hugged the card's bottom edge and each
   card had slightly different internal spacing. We override that once here
   so every widget shares the same height, padding, title, centered graphic,
   and legend treatment — the charts read as a single designed system.
   ============================================================ */

/* RTL fix: Frappe charts are laid out left-to-right (axes, legend dots, dx offsets).
   On an Arabic (dir=rtl) page the SVG text gets bidi-scrambled — "جديد: 15" flips and
   the legend dot detaches. Force the chart SVG back to LTR; Arabic glyphs still render
   correctly within it, and the title (HTML, outside the SVG) stays right-aligned. */
[dir="rtl"] svg.frappe-chart,
[dir="rtl"] svg.frappe-chart text { direction: ltr; }

/* More breathing room BETWEEN the chart cards. Orchid's columns blade renders
   each chart row as `.row.g-3` (1rem gutters); widen the gutter (both axes) and
   the per-card bottom margin so every widget is clearly separated. Scoped with
   :has() so only chart rows are affected, not the metric/table rows. */
.row.g-3:has([data-controller="chart"]) {
    --bs-gutter-x: 1.5rem;
    --bs-gutter-y: 1.5rem;
}
[data-controller="chart"] > .bg-white { margin-bottom: 1.5rem !important; } /* gap to the next row */

/* Card: equal height across a row (the column already stretches) + ONE uniform
   internal padding box, including the bottom padding the blade never gave. The
   per-type min-height below sizes each card so the chart fills it with minimal
   wasted space while still containing Frappe's in-SVG legend (see the legend
   note further down for why the height alone is not the clip fix). */
[data-controller="chart"] > .bg-white {
    display: flex;
    flex-direction: column;
    padding: 1.25rem 1.25rem 1.5rem !important;   /* overrides .pt-3 */
    border: 1px solid var(--c-border);
}

/* Each card sizes to its content (title + chart + legend). We deliberately do
   NOT force height:100% or a tall min-height on the cards: that stretched every
   card to the row's tallest and left big empty gaps inside the shorter ones.
   A small min-height only guarantees the axis charts (no legend) aren't too
   short; circular charts size naturally and their legend clearance is handled
   by top-aligning + svg overflow:visible below. */
[data-controller="chart"][data-chart-type="bar"] > .bg-white,
[data-controller="chart"][data-chart-type="line"] > .bg-white { min-height: 372px; }

/* Narrow 3-column range only: when the columns get narrow enough that the
   6-label Arabic legend on "Leads by Status" wraps to one-per-row, the legend
   needs more height. Give the circular cards just enough min-height in THAT
   range so the legend never clips. Wide screens (>=1600px) and the stacked
   single-column layout (<768px) both have wide cards where the legend stays
   short, so they keep the compact content-sized card with no wasted space. */
@media (min-width: 768px) and (max-width: 1599.98px) {
    [data-controller="chart"][data-chart-type="donut"] > .bg-white,
    [data-controller="chart"][data-chart-type="pie"] > .bg-white { min-height: 432px; }
}

/* Title row: same position, size, and gap below for every chart. */
[data-controller="chart"] > .bg-white > .d-flex { padding: 0 !important; }
[data-controller="chart"] legend {
    width: 100%;
    padding: 0 !important;                          /* overrides .px-2 */
    margin: 0 0 1rem !important;                    /* overrides .mt-2 .mb-0 */
}
[data-controller="chart"] legend small {
    font-size: .9rem;
    font-weight: 600;
    color: var(--c-text);
}

/* Chart body: fills the remaining card height and centers the graphic both
   axes, so every chart sits proportionate and centered in its card. */
[data-controller="chart"] > .bg-white > .position-relative {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 0;
}
[data-controller="chart"] figure {
    width: 100%;
    height: 100%;
    align-items: center;
    justify-content: center;
}
[data-controller="chart"] .chart-container { width: 100%; }
/* overflow:visible is the key legend fix — the legend is a <g> inside the SVG,
   and a fixed-height SVG clips anything below it. Letting it paint past the SVG
   box (the card is sized to contain it) means legend rows are never cut off. */
[data-controller="chart"] svg.frappe-chart { display: block; margin: 0 auto; overflow: visible; }

/* Circular charts: top-align the SVG (both the body flex AND the inner figure)
   so its in-SVG legend stays as high as possible — centering would push the
   legend down toward the card edge. Any spare height then pools harmlessly at
   the bottom as legend clearance. Axis charts stay centered (default above). */
[data-controller="chart"][data-chart-type="donut"] > .bg-white > .position-relative,
[data-controller="chart"][data-chart-type="pie"] > .bg-white > .position-relative,
[data-controller="chart"][data-chart-type="donut"] figure,
[data-controller="chart"][data-chart-type="pie"] figure {
    align-items: flex-start;
}

/* IMPORTANT — the legend-clipping fix. Frappe renders the pie/donut legend as a
   <g class="chart-legend"> INSIDE the chart SVG, and the SVG has a fixed height.
   When the legend wraps to many rows (6 Arabic labels stacking one-per-row in a
   narrow 3-column layout) the donut + legend exceed that height. Simply making
   the SVG taller does NOT help — Frappe scales the donut up with it, so the
   legend still overflows. The real fix is three parts working together:
     1. svg overflow:visible (above) — the legend is never clipped by the SVG box;
     2. top-aligning the circular charts (above) — the legend stays high instead
        of being pushed toward the card edge;
     3. the narrow-range circular min-height (432px, in the media query above) —
        only kicks in at 768–1599px where the legend actually wraps to 6 rows, so
        wide screens keep compact content-sized cards. Verified clip-free across
        768 → 1920px. The legend text size still applies here (SVG <text>). */
[data-controller="chart"] .chart-legend .legend-dataset-text { font-size: .78rem; }

/* ---------- Layout fixes (Orchid stretch / spacing) ---------- */
.h-100 { height: auto !important; }
.mb-md-4 { margin-bottom: 0 !important; }

/* ---------- Balanced content area (desktop only) ----------
   Bootstrap renders the content column as col-xl-9 (width:75%) + mx-auto, so it
   never fills the space beside the sidebar — worst in the COLLAPSED state, where
   the narrow rail leaves a big empty gap on the right and the content looks
   shifted left. Fix: force the column to FILL beside the sidebar in BOTH states
   (kill the 75% width + mx-auto), then let the inner .container-xl fill that
   column with EQUAL gutters — same breathing room on both sides in either
   sidebar state, and the width isn't wasted. */
@media (min-width: 992px) {
    .container-fluid > .row > div:not(.aside) {
        flex: 1 1 0 !important;             /* take exactly the free space beside the sidebar */
        min-width: 0 !important;            /* KEY: let it shrink — stops a wide table forcing overflow/wrap */
        max-width: 100% !important;
        margin-inline: 0 !important;        /* cancel mx-auto narrowing */
    }
    .container-fluid > .row > div:not(.aside) > .container-xl {
        max-width: 100%;                    /* fill the column, not a narrow centered box */
        margin-inline: 0;
        padding-inline: 1.75rem !important; /* equal breathing room both sides (beats .p-0) */
    }
    /* THE real blocker: Orchid caps the inner workspace at ~1135px via
       `.workspace-limit`, so every card (header, filter, table) stayed narrow
       no matter how wide the column grew. Remove the cap so the workspace —
       and the table inside it — fills the available width and reflows with the
       sidebar state. */
    .workspace.workspace-limit { max-width: none !important; }
}

/* ============================================================
   Sidebar — light theme, integrated with the cards/table system
   ============================================================ */

/* white surface + soft separator (overrides Orchid's bg-dark) */
.aside.bg-dark {
    background-color: var(--c-surface) !important;
    border-inline-end: 1px solid var(--c-border);
}

/* flip text + icons to dark (Orchid forces a dark color-scheme on the aside) */
.aside,
.aside .nav-link,
.aside .header-brand,
.aside .link-body-emphasis,
.aside .text-body-emphasis { color: var(--c-text) !important; }
.aside .text-muted { color: var(--c-muted) !important; }
.aside .divider { border-color: var(--c-border) !important; }

/* inner dark boxes (profile card) → light tint */
.aside .bg-dark { background-color: var(--c-primary-soft) !important; }
/* profile name was .text-white (invisible on a light card) → dark */
.aside .profile-container .text-white { color: var(--c-text) !important; }

/* nav links: padding + radius so hover reads as a clean pill */
.aside .nav-link {
    border-radius: .5rem;
    margin: .1rem .5rem;
    padding: .5rem .75rem;
    transition: background-color .15s, color .15s;
}

/* hover = the SAME subtle tint used on table rows.
   !important matches the table rule so Orchid's built-in .nav-link:hover
   can't override it — both highlights now resolve to the exact same token. */
.aside .nav-link:hover {
    background-color: var(--c-hover) !important;
    color: var(--c-text) !important;
}

/* active = stronger than hover, clearly distinguishable */
.aside .nav-item.active > .nav-link,
.aside .nav-link.active {
    background-color: var(--c-primary-soft) !important;
    color: var(--c-primary) !important;
    font-weight: 600;
}

/* keyboard focus stays visible */
.aside .nav-link:focus-visible {
    outline: 0;
    box-shadow: 0 0 0 3px var(--c-ring);
}

/* ---------- Collapse / expand handle (floating, edge-mounted) ----------
   position:fixed + appended to <body> so it can never be clipped by the
   sidebar's overflow. JS keeps `left` glued to the sidebar's right edge
   (and animates it while the sidebar collapses/expands). */
.crm-aside-toggle {
    position: fixed;
    top: 76px;
    left: 0;                         /* JS sets the real left = sidebar right edge */
    z-index: 1045;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: 1px solid var(--c-border);
    border-radius: 50%;              /* circular handle */
    background: var(--c-surface);
    color: var(--c-muted);
    box-shadow: 0 2px 8px rgba(0, 0, 0, .15);
    cursor: pointer;
    transition: background-color .15s, color .15s, box-shadow .15s, transform .12s;
}
.crm-aside-toggle:hover {
    background: var(--c-primary-soft);
    color: var(--c-primary);
    box-shadow: 0 4px 12px rgba(0, 0, 0, .2);
}
.crm-aside-toggle:active { transform: scale(.9); }
.crm-aside-toggle:focus-visible { outline: 0; box-shadow: 0 0 0 3px var(--c-ring); }
.crm-aside-toggle svg { transition: transform .2s ease; }
/* chevron flips to point the other way depending on state */
html.aside-collapsed .crm-aside-toggle svg { transform: rotate(180deg); }
/* sidebar collapse is desktop-only → hide the handle on mobile */
@media (max-width: 991.98px) { .crm-aside-toggle { display: none !important; } }

/* smooth width animation (desktop only) */
@media (min-width: 992px) {
    /* sidebar + content always share ONE line so content never wraps below the rail */
    .container-fluid > .row.h-100 { flex-wrap: nowrap !important; }

    .container-fluid > .row.h-100 > .aside {
        transition: flex-basis .2s ease, max-width .2s ease, width .2s ease;
    }

    /* collapsed: narrow rail, icons only */
    html.aside-collapsed .container-fluid > .row.h-100 > .aside {
        flex: 0 0 76px !important;
        width: 76px !important;
        max-width: 76px !important;
    }
    /* NOTE: the content column is handled ONCE by the "Balanced content area" rule
       above (flex:1 1 0 + min-width:0) for BOTH states. A separate collapsed
       override here previously reset flex-basis to `auto` WITHOUT min-width:0,
       which let the wide table force a wrap below the rail — the empty-gap bug.
       So there is intentionally no content override in collapsed mode. */

    /* fully hide labels/text that shouldn't appear collapsed */
    html.aside-collapsed .aside .nav-link span.text-break,
    html.aside-collapsed .aside .nav-item.mt-3.mb-1 small,   /* section titles */
    html.aside-collapsed .aside .header-brand,
    html.aside-collapsed .aside .profile-container small,    /* name + role */
    html.aside-collapsed .aside .profile-container .bi-bell, /* notification bell */
    html.aside-collapsed .aside .profile-container .badge {  /* unread badge */
        display: none !important;
    }

    /* nav: icons centered + evenly spaced, no overflow */
    html.aside-collapsed .aside .nav-link {
        justify-content: center;
        padding-inline: 0;
        margin-block: .15rem;
    }
    html.aside-collapsed .aside { overflow-x: hidden; }

    /* "Scroll to top": its label is a raw text node — font-size:0 drops the text,
       the icon gets an explicit px size so it stays visible + centered (icon-only). */
    html.aside-collapsed .aside .to-top .bottom-left { padding-inline-start: 0 !important; }
    html.aside-collapsed .aside .scroll-to-top {
        justify-content: center;
        gap: 0 !important;
        font-size: 0 !important;
    }
    html.aside-collapsed .aside .scroll-to-top svg { width: 18px !important; height: 18px !important; }

    /* profile footer: avatar only, centered, not cramped/oversized */
    html.aside-collapsed .aside .profile-container {
        justify-content: center;
        padding: .6rem !important;
    }
    html.aside-collapsed .aside .profile-container .col-10 { justify-content: center; gap: 0 !important; }
}

/* ── Kanban board (Tasks > Board view) ───────────────────────────── */
.task-column {
    transition: background .12s ease, box-shadow .12s ease;
}
.task-column.task-column-over {
    background: var(--c-primary-soft) !important;
    box-shadow: inset 0 0 0 2px var(--c-primary);
}
.task-card { transition: box-shadow .12s ease, transform .06s ease; touch-action: none; }
.task-card:hover { box-shadow: 0 4px 12px rgba(0,0,0,.10); }
.task-card:active { cursor: grabbing; }

/* The original card stays in place but dimmed while its clone follows the cursor. */
.task-card-dragging { opacity: .35; }

/* Floating clone that follows the pointer during a drag (see platform.js). */
.task-card-clone {
    position: fixed;
    z-index: 9999;
    pointer-events: none;      /* so elementFromPoint sees the column underneath */
    opacity: .95;
    transform: rotate(2deg);
    box-shadow: 0 10px 26px rgba(0,0,0,.22);
    cursor: grabbing;
}

/* No text selection while dragging cards. */
body.task-board-dragging { user-select: none; -webkit-user-select: none; }

/* Empty columns keep a droppable area. */
[data-task-board] [data-column-body] { min-height: 4rem; }
