/* R31 – statistics screens (Accounting / Inventory / Point of Sale): KPI tiles, chart cards, the StatChart components. Colours come from app.css
   variables; the six categorical colours below were validated (lightness band, chroma, colour-blind separation, contrast) on the light surface and
   re-stepped for the dark one. Logical properties keep RTL and LTR symmetric. */
:root {
    --sx-c1: var(--series-1); --sx-c2: var(--series-2); --sx-c3: #2a78d6; --sx-c4: #d8a000; --sx-c5: #d55181; --sx-c6: #6250d6;
    --sx-cmp: var(--muted);
}
@media (prefers-color-scheme: dark) {
    :root:not([data-theme^="light"]) { --sx-c3: #3987e5; --sx-c4: #c98500; --sx-c5: #d55181; --sx-c6: #9085e9; }
}
[data-theme="midnight"] { --sx-c3: #3987e5; --sx-c4: #c98500; --sx-c5: #d55181; --sx-c6: #9085e9; }

.sx-c1 { --sx: var(--sx-c1); } .sx-c2 { --sx: var(--sx-c2); } .sx-c3 { --sx: var(--sx-c3); }
.sx-c4 { --sx: var(--sx-c4); } .sx-c5 { --sx: var(--sx-c5); } .sx-c6 { --sx: var(--sx-c6); } .sx-cmp { --sx: var(--sx-cmp); }

/* page */
.sx-period { color: var(--muted); font-size: 13px; margin: -8px 0 -6px; }
.sx-section { margin: 10px 0 -4px; font-size: 13px; font-weight: 600; letter-spacing: .02em; color: var(--muted); text-transform: uppercase; display: flex; align-items: center; gap: 8px; }
.sx-section::after { content: ""; flex: 1; border-top: 1px solid var(--border); }
.sx-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; align-items: start; }
.sx-grid.wide-left { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.sx-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.sx-note { font-size: 12px; color: var(--muted); margin: 0; }

/* KPI tiles */
.sx-kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; }
.sx-kpi { gap: 4px; padding: 14px 16px; min-width: 0; }
.sx-kpi .kpi-label { font-size: 12.5px; }
.sx-kpi-value { font-size: 22px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sx-kpi-value small { font-family: var(--font); font-size: 12px; font-weight: 400; color: var(--muted); }
.sx-kpi-value.neg, .sx-bars-value.neg { color: var(--red-fg); }
.sx-delta { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 600; flex-wrap: wrap; }
.sx-delta-abs { font-family: var(--mono); font-weight: 400; color: var(--muted); font-size: 12px; }
.sx-delta-pct { font-family: var(--mono); }
.sx-good { color: var(--green-fg); } .sx-bad { color: var(--red-fg); } .sx-flat { color: var(--muted); }
.sx-kpi .kpi-sub bdi { font-size: 12px; }

/* chart card */
.sx-card { gap: 10px; min-width: 0; }
.sx-card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.sx-card-title { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.sx-card-tools { display: flex; gap: 6px; flex-wrap: wrap; }
.sx-card-tools .seg-btn { padding: 4px 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: var(--radius-sm); font: inherit; font-size: 12px; cursor: pointer; }
.sx-card-tools .seg-btn.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.sx-card-foot { display: flex; gap: 6px; justify-content: flex-end; }
.sx-data { display: none; }
.sx-data.open { display: block; }
.sx-data .grid td { white-space: nowrap; }

/* legend & keys */
.sx-legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12.5px; color: var(--muted); margin-bottom: 6px; }
.sx-legend-item { display: inline-flex; align-items: center; gap: 6px; }
.sx-key { display: inline-block; width: 10px; height: 10px; border-radius: 3px; background: var(--sx); flex-shrink: 0; }

/* column / line chart */
.sx-svg-wrap { width: 100%; overflow: hidden; }
.sx-svg { width: 100%; display: block; }
.sx-axis { fill: var(--muted); font-size: 11px; font-family: var(--font); }
line.sx-gridline { stroke: var(--row-line); stroke-width: 1; }
.sx-zero { stroke: var(--border); stroke-width: 1.5; }
.sx-bar { fill: var(--sx); }
.sx-bar:hover { opacity: .8; }
.sx-line { stroke: var(--sx); stroke-width: 2; fill: none; stroke-linejoin: round; stroke-linecap: round; }
.sx-line.dashed { stroke-dasharray: 6 4; stroke-width: 1.6; }
.sx-dot { fill: var(--sx); stroke: var(--surface); stroke-width: 2; }
.sx-hit { fill: transparent; }
.sx-pt:hover .sx-dot { r: 4.5; }

/* ranked bars */
.sx-bars { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.sx-bars-row { display: grid; grid-template-columns: minmax(110px, 38%) minmax(40px, 1fr) auto; align-items: center; gap: 10px; font-size: 13px; }
.sx-bars-row:has(.sx-bars-extra) { grid-template-columns: minmax(110px, 36%) minmax(40px, 1fr) auto 52px; }
.sx-bars-label { display: flex; align-items: baseline; gap: 6px; min-width: 0; }
.sx-bars-label .muted { white-space: nowrap; flex-shrink: 0; }
.sx-bars-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sx-rank { font-family: var(--mono); font-size: 11px; color: var(--muted); min-width: 16px; }
.sx-bars-track { height: 10px; border-radius: 3px; background: var(--row-line); overflow: hidden; }
.sx-bars-fill { display: block; height: 100%; background: var(--sx); border-start-end-radius: 3px; border-end-end-radius: 3px; min-width: 2px; }
.sx-bars-value { font-family: var(--mono); font-size: 12.5px; text-align: end; white-space: nowrap; }
.sx-bars-extra { font-family: var(--mono); font-size: 12px; text-align: end; }

/* donut */
.sx-donut { display: grid; grid-template-columns: 150px minmax(0, 1fr); gap: 18px; align-items: center; }
.sx-donut-svg { width: 150px; height: 150px; }
.sx-donut-track { fill: none; stroke: var(--row-line); stroke-width: 6; }
.sx-donut-seg { fill: none; stroke: var(--sx); stroke-width: 6; }
.sx-donut-seg:hover { stroke-width: 7; }
.sx-donut-total { font-family: var(--mono); font-size: 4.2px; font-weight: 600; fill: var(--text); }
.sx-donut-caption { font-family: var(--font); font-size: 3px; fill: var(--muted); }
.sx-donut-legend { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 7px; font-size: 13px; }
.sx-donut-legend li { display: grid; grid-template-columns: 10px minmax(0, 1fr) auto 52px; gap: 8px; align-items: center; }
.sx-donut-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sx-donut-val, .sx-donut-pct { font-family: var(--mono); font-size: 12.5px; text-align: end; white-space: nowrap; }

/* stacked bars */
.sx-stack { display: flex; flex-direction: column; gap: 10px; }
.sx-stack-row { display: grid; grid-template-columns: minmax(90px, 22%) minmax(0, 1fr) auto; gap: 10px; align-items: center; font-size: 13px; }
.sx-stack-track { display: flex; gap: 2px; height: 22px; border-radius: 4px; background: var(--row-line); overflow: hidden; }
.sx-stack-seg { display: block; height: 100%; background: var(--sx); min-width: 2px; }
.sx-stack-total { font-family: var(--mono); font-size: 12.5px; white-space: nowrap; }

/* heat-map */
.sx-heat-wrap { margin-top: 2px; }
.sx-heat { border-collapse: separate; border-spacing: 2px; width: 100%; font-size: 11px; }
.sx-heat th { color: var(--muted); font-weight: 500; padding: 2px 4px; text-align: center; white-space: nowrap; }
.sx-heat tbody th { text-align: start; padding-inline-end: 8px; }
.sx-heat td { height: 22px; min-width: 22px; border-radius: 3px; background: color-mix(in srgb, var(--sx-c1) var(--sx-heat, 0%), var(--surface)); }
.sx-heat td.none { background: var(--row-line); opacity: .5; }
.sx-heat-scale { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.sx-heat-ramp { width: 120px; height: 8px; border-radius: 4px; background: linear-gradient(to right, color-mix(in srgb, var(--sx-c1) 8%, var(--surface)), var(--sx-c1)); }
[dir="rtl"] .sx-heat-ramp { background: linear-gradient(to left, color-mix(in srgb, var(--sx-c1) 8%, var(--surface)), var(--sx-c1)); }

/* ratios */
.sx-ratios { display: flex; flex-direction: column; }
.sx-ratio { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 12px; padding: 10px 0; border-bottom: 1px solid var(--row-line); }
.sx-ratio:last-child { border-bottom: 0; }
.sx-ratio-name { font-weight: 600; }
.sx-ratio-value { font-family: var(--mono); font-size: 18px; font-weight: 600; text-align: end; }
.sx-ratio-help { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.sx-ratio .sx-delta { grid-column: 2; justify-self: end; }

/* multi-select (warehouses) */
.sx-multi { position: relative; }
.sx-multi > summary { list-style: none; cursor: pointer; padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); min-width: 180px; }
.sx-multi > summary::-webkit-details-marker { display: none; }
.sx-multi-list { position: absolute; z-index: 20; inset-inline-start: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
    box-shadow: 0 8px 24px rgb(0 0 0 / .12); padding: 8px; max-height: 280px; overflow: auto; min-width: 240px; display: flex; flex-direction: column; gap: 4px; }
.sx-multi-list label { display: flex; gap: 8px; align-items: center; font-size: 13px; }

/* tables: numbers keep their sign in front in Arabic ("-18,885.625", not "18,885.625-") – LTR inside, aligned to the end of the RTL cell */
.sx-table td.num, .sx-table th.num { white-space: nowrap; }
[dir="rtl"] .sx-table td.num { direction: ltr; text-align: left; }
.sx-data .grid.compact th, .sx-data .grid.compact td { padding-inline: 6px; font-size: 12.5px; }

@media (max-width: 1300px) {
    .sx-kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
@media (max-width: 1100px) {
    .sx-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .sx-grid.three { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 800px) {
    .sx-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .sx-grid, .sx-grid.wide-left, .sx-grid.three { grid-template-columns: minmax(0, 1fr); }
    .sx-donut { grid-template-columns: minmax(0, 1fr); justify-items: center; }
}

/* print: charts and their tables on paper, black on white */
@media print {
    .sx-kpis { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
    .sx-grid, .sx-grid.wide-left, .sx-grid.three { display: block; }
    .sx-grid > * { margin-bottom: 10px; break-inside: avoid; }
    .sx-card { break-inside: avoid; }
    .sx-data { display: block; }
    .sx-heat td { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
    .sx-bars-fill, .sx-stack-seg, .sx-key { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
}
