/* ===========================================================================
   Ayan Design System — foundation
   ---------------------------------------------------------------------------
   The tokens every Ayan surface is built from, and the styles of the shared
   primitives that live in this library. Loaded before any product stylesheet,
   so a product can override a token but never has to redefine one.

   A theme overrides these at runtime by emitting a later <style> block that
   redeclares the same custom properties (see the host's theme service), which
   is why everything here is a token reference rather than a literal.

   Nothing product-specific belongs in this file: no ERP page layout, no
   Exchange counter, no Control Center console. Those live in the product's own
   stylesheet and may use these tokens freely.
   =========================================================================== */

:root {
    /* ——— Colour ——— */
    --ayan-color-primary: #0F2744;
    --ayan-color-secondary: #5A6B7D;
    --ayan-color-page: #F7F5F1;
    --ayan-color-surface: #FFFFFF;
    --ayan-color-text: #142033;
    --ayan-color-muted: #5C6B7A;
    --ayan-color-success: #2F6B4F;
    --ayan-color-warning: #9A6B16;
    --ayan-color-error: #A33B3B;
    --ayan-color-accent: #A68B4B;
    --ayan-color-exchange-accent: #1F6F63;
    --ayan-color-on-primary: #F2F5F8;
    --ayan-color-success-bg: #E8F2EC;
    --ayan-color-warning-bg: #F7F0E0;
    --ayan-color-error-bg: #F7E8E8;
    --ayan-color-accent-bg: #F5F0E4;

    /* Referenced across the app with fallbacks and never declared until now. Declaring them ends the
       silent divergence between "the fallback" and "what a theme would override". */
    --ayan-color-surface-muted: color-mix(in srgb, var(--ayan-color-secondary) 8%, var(--ayan-color-surface));
    --ayan-color-border: color-mix(in srgb, var(--ayan-color-secondary) 32%, transparent);

    /* ——— Status tones ——— */
    /* Five tones, mapped from the palette. Every status in every product resolves to exactly one of
       these; a view never picks a raw colour. Tone is never the only carrier of meaning — the badge
       always has text as well. */
    --ayan-tone-danger: var(--ayan-color-error);
    --ayan-tone-danger-bg: var(--ayan-color-error-bg);
    --ayan-tone-attention: var(--ayan-color-warning);
    --ayan-tone-attention-bg: var(--ayan-color-warning-bg);
    --ayan-tone-partial: var(--ayan-color-accent);
    --ayan-tone-partial-bg: var(--ayan-color-accent-bg);
    --ayan-tone-success: var(--ayan-color-success);
    --ayan-tone-success-bg: var(--ayan-color-success-bg);
    --ayan-tone-neutral: var(--ayan-color-muted);
    --ayan-tone-neutral-bg: color-mix(in srgb, var(--ayan-color-secondary) 14%, transparent);

    /* ——— Typography ——— */
    --ayan-font-family: "IBM Plex Sans", "Noto Sans Arabic", system-ui, "Segoe UI", Tahoma, sans-serif;
    --ayan-font-size: 16px;
    --ayan-font-size-xs: 0.75rem;
    --ayan-font-size-sm: 0.875rem;
    --ayan-font-size-lg: 1.125rem;
    --ayan-font-size-xl: 1.375rem;
    --ayan-font-size-2xl: 1.75rem;
    --ayan-line-height: 1.5;
    /* Every figure a person may compare down a column: amounts, quantities, rates, counters. */
    --ayan-font-numeric: tabular-nums;

    /* ——— Space, size, shape ——— */
    --ayan-control-height: 2.35rem;
    --ayan-radius-sm: 3px;
    --ayan-radius: 4px;
    --ayan-radius-md: 8px;
    --ayan-sidebar-width: 15.5rem;
    --ayan-sidebar-collapsed-width: 4.25rem;
    --ayan-space-1: 0.25rem;
    --ayan-space-2: 0.5rem;
    --ayan-space-3: 0.75rem;
    --ayan-space-4: 1rem;
    --ayan-space-5: 1.5rem;
    --ayan-space-6: 2rem;
    /* The smallest comfortable touch target; anything interactive on a phone meets it. */
    --ayan-touch-target: 44px;
    /* Tree indent depth, read by hierarchical rows. Declared so it is never an undefined fallback. */
    --ayan-depth: 0;

    --ayan-border: 1px solid color-mix(in srgb, var(--ayan-color-secondary) 32%, transparent);
    --ayan-border-strong: 1px solid color-mix(in srgb, var(--ayan-color-secondary) 48%, transparent);
    --ayan-shadow-sm: 0 1px 2px color-mix(in srgb, #0F2744 6%, transparent);
    --ayan-shadow-md: 0 4px 14px color-mix(in srgb, #0F2744 8%, transparent);
    --ayan-focus-ring: 0 0 0 3px color-mix(in srgb, var(--ayan-color-primary) 28%, transparent);
    --ayan-surface-elevated: color-mix(in srgb, var(--ayan-color-surface) 92%, var(--ayan-color-primary) 8%);
    --ayan-shell-sidebar: var(--ayan-color-primary);
    --ayan-ws-gap: var(--ayan-space-4);

    /* ——— Layering ——— */
    /* One scale, so a new overlay never has to guess a number higher than the last one. */
    --ayan-z-sticky: 40;
    --ayan-z-drawer: 50;
    --ayan-z-dialog: 60;
    --ayan-z-toast: 70;
    --ayan-z-reconnect: 1000;
    --ayan-sticky-z: var(--ayan-z-sticky);

    /* ——— Motion ——— */
    /* Calm, short, purposeful: never used to draw attention to a number changing (see .ayan-kpi-card,
       whose value text never animates). */
    --ayan-motion-fast: 120ms;
    --ayan-motion-base: 180ms;
    --ayan-motion-easing: cubic-bezier(0.22, 0.61, 0.36, 1);

    /* ——— Print ——— */
    /* Chosen for a mono laser at 600 dpi: the greys stay distinguishable and the ink stays readable
       when a customer photocopies the page. The Arabic-capable face comes first because a font stack
       that starts with a Latin-only face renders Arabic in whatever the device happens to have. */
    --ayan-print-ink: #171717;
    --ayan-print-muted: #5C5C5C;
    --ayan-print-rule: #D0D0D0;
    --ayan-print-band: #F3F3F3;
    --ayan-print-font: "Noto Naskh Arabic", "IBM Plex Sans", serif;
}

/* ===========================================================================
   Primitives
   ---------------------------------------------------------------------------
   The styles of the components this library ships. Each was previously defined
   two to four times in the ERP stylesheet, later copies silently overriding
   earlier ones; what follows is the effective result of that cascade, stated
   once. The legacy compatibility selectors (.text-danger[role=alert] and its
   siblings) deliberately stay in the product stylesheet: they patch markup the
   design system is replacing, not markup it owns.
   =========================================================================== */

/* ——— FeedbackAlert ——— */
.ayan-alert {
    padding: 0.7rem 0.9rem;
    border-radius: var(--ayan-radius-md);
    border: 1px solid transparent;
    margin-bottom: var(--ayan-space-3);
    font-size: var(--ayan-font-size-sm);
    line-height: 1.45;
}
.ayan-alert--error {
    background: var(--ayan-tone-danger-bg);
    color: var(--ayan-tone-danger);
    border-color: color-mix(in srgb, var(--ayan-tone-danger) 35%, transparent);
}
.ayan-alert--success {
    background: var(--ayan-tone-success-bg);
    color: var(--ayan-tone-success);
    border-color: color-mix(in srgb, var(--ayan-tone-success) 35%, transparent);
}
.ayan-alert--warning {
    background: var(--ayan-tone-attention-bg);
    color: var(--ayan-tone-attention);
    border-color: color-mix(in srgb, var(--ayan-tone-attention) 35%, transparent);
}
.ayan-alert--info {
    background: var(--ayan-surface-elevated, var(--ayan-color-surface));
    color: var(--ayan-color-text);
    border-color: var(--ayan-color-border);
}
/* An alert appears, it does not slide or bounce: a message about a failed posting should not perform. */
.ayan-alert {
    animation: ayan-feedback-in var(--ayan-motion-base) var(--ayan-motion-easing);
}
@keyframes ayan-feedback-in {
    from { opacity: 0; }
    to { opacity: 1; }
}
.ayan-alert ul { padding-inline-start: 1.25rem; margin-bottom: 0; }
.ayan-alert a { color: inherit; font-weight: 600; text-decoration: underline; }

/* ——— EmptyState ——— */
.ayan-empty-state {
    display: grid;
    gap: var(--ayan-space-2);
    padding: var(--ayan-space-5);
    text-align: center;
    background: var(--ayan-color-surface);
    border: var(--ayan-border);
    border-radius: var(--ayan-radius);
    color: var(--ayan-color-muted);
}
.ayan-empty-state__title {
    margin: 0;
    font-weight: 600;
    color: var(--ayan-color-text);
}

/* ——— Pager ——— (server-paged lists, §13 / §25) */
.ayan-pager {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: var(--ayan-space-2);
    padding-block: var(--ayan-space-2);
}
.ayan-pager__status {
    color: var(--ayan-color-muted);
    font-variant-numeric: tabular-nums;
}

/* ——— SortHeader ——— (server sort, §13) */
.ayan-sort-header {
    display: inline-flex;
    align-items: center;
    gap: var(--ayan-space-1);
    padding: 0;
    border: 0;
    background: none;
    color: inherit;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}
.ayan-sort-header:focus-visible {
    outline: none;
    box-shadow: var(--ayan-focus-ring);
    border-radius: var(--ayan-radius-sm);
}
.ayan-sort-header__mark {
    color: var(--ayan-color-muted);
    font-size: var(--ayan-font-size-xs);
}

/* ——— Picker ——— (server-side typeahead, §14 / §25) */
.ayan-picker {
    position: relative;
}
.ayan-picker__control {
    position: relative;
}
.ayan-picker__control .form-control {
    padding-inline-end: calc(var(--ayan-space-5) + var(--ayan-space-2));
}
.ayan-picker__clear {
    position: absolute;
    inset-block: 0;
    inset-inline-end: var(--ayan-space-1);
    min-width: var(--ayan-space-5);
    border: 0;
    background: none;
    color: var(--ayan-color-muted);
    font-size: var(--ayan-font-size-lg);
    cursor: pointer;
}
.ayan-picker__list {
    position: absolute;
    z-index: var(--ayan-z-drawer);
    inset-inline: 0;
    margin: var(--ayan-space-1) 0 0;
    padding: var(--ayan-space-1) 0;
    list-style: none;
    max-height: 18rem;
    overflow-y: auto;
    background: var(--ayan-color-surface);
    border: var(--ayan-border);
    border-radius: var(--ayan-radius);
    box-shadow: var(--ayan-shadow-md);
}
.ayan-picker__option,
.ayan-picker__status {
    padding: var(--ayan-space-2) var(--ayan-space-3);
    min-height: var(--ayan-touch-target);
    display: flex;
    align-items: center;
}
.ayan-picker__option {
    cursor: pointer;
}
.ayan-picker__option.is-active,
.ayan-picker__option:hover {
    background: var(--ayan-color-surface-muted);
}
.ayan-picker__status {
    color: var(--ayan-color-muted);
}

/* ——— LoadingState ——— */
.ayan-loading-state {
    display: flex;
    align-items: center;
    gap: var(--ayan-space-2);
    padding: 1rem 1.1rem;
    border-radius: var(--ayan-radius-md);
    border: var(--ayan-border);
    color: var(--ayan-color-muted);
    background: var(--ayan-surface-elevated, var(--ayan-color-surface));
}
.ayan-loading-state__spinner {
    width: 1rem;
    height: 1rem;
    border-radius: 50%;
    border: 2px solid color-mix(in srgb, var(--ayan-color-primary) 25%, transparent);
    border-top-color: var(--ayan-color-primary);
    animation: ayan-spin 0.8s linear infinite;
    flex-shrink: 0;
}
@keyframes ayan-spin {
    to { transform: rotate(360deg); }
}

/* ——— StatusBadge ——— */
.ayan-badge {
    display: inline-block;
    padding: 0.12rem 0.5rem;
    border-radius: var(--ayan-radius);
    background: var(--ayan-tone-neutral-bg);
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.02em;
}
.ayan-badge--success {
    background: var(--ayan-tone-success-bg);
    color: var(--ayan-tone-success);
}
.ayan-badge--warning {
    background: var(--ayan-tone-attention-bg);
    color: var(--ayan-tone-attention);
}
.ayan-badge--error {
    background: var(--ayan-tone-danger-bg);
    color: var(--ayan-tone-danger);
}
.ayan-badge--accent {
    background: var(--ayan-tone-partial-bg);
    color: color-mix(in srgb, var(--ayan-tone-partial) 40%, var(--ayan-color-primary));
}

/* ——— ProductBadge ——— */
.ayan-product-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.18rem 0.6rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}
.ayan-product-badge--erp {
    background: var(--ayan-color-accent-bg);
    color: color-mix(in srgb, var(--ayan-color-primary) 80%, black);
}
.ayan-product-badge--exchange {
    background: color-mix(in srgb, var(--ayan-color-exchange-accent, #1F6F63) 16%, transparent);
    color: var(--ayan-color-exchange-accent, #1F6F63);
}
[data-appearance="dark"] .ayan-product-badge--exchange {
    color: color-mix(in srgb, var(--ayan-color-exchange-accent, #4FBDA8) 90%, white);
}

/* ——— KpiCard ——— */
.ayan-kpi-card {
    border: var(--ayan-border);
    border-radius: var(--ayan-radius-md);
    background: var(--ayan-color-surface);
    box-shadow: var(--ayan-shadow-sm);
    padding: var(--ayan-space-4);
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    min-width: 0;
    transition: box-shadow var(--ayan-motion-base) var(--ayan-motion-easing),
                transform var(--ayan-motion-base) var(--ayan-motion-easing);
}
.ayan-kpi-card--linked:hover,
.ayan-kpi-card--linked:focus-within {
    box-shadow: var(--ayan-shadow-md);
    transform: translateY(-1px);
}
.ayan-kpi-card__title {
    margin: 0;
    font-size: var(--ayan-font-size-sm);
    color: var(--ayan-color-muted);
    font-weight: 600;
}
.ayan-kpi-card__value {
    margin: 0;
    font-size: 1.6rem;
    font-weight: 700;
    color: var(--ayan-color-text);
    font-variant-numeric: var(--ayan-font-numeric);
    /* Financial figures never animate on change — reviewers must never see a number "count up". */
}
.ayan-kpi-card__trend {
    margin: 0;
    font-size: var(--ayan-font-size-sm);
    font-weight: 600;
}
.ayan-kpi-card__trend--positive { color: var(--ayan-tone-success); }
.ayan-kpi-card__trend--negative { color: var(--ayan-tone-danger); }
.ayan-kpi-card__trend--neutral { color: var(--ayan-tone-neutral); }
.ayan-kpi-card__extra {
    margin-top: 0.2rem;
    font-size: var(--ayan-font-size-sm);
    color: var(--ayan-color-muted);
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}
.ayan-kpi-card__link {
    margin-top: auto;
    padding-top: 0.4rem;
    font-size: var(--ayan-font-size-sm);
    font-weight: 600;
    color: var(--ayan-color-primary);
    text-decoration: none;
}
.ayan-kpi-card__link:hover { text-decoration: underline; }
.ayan-kpi-card__error {
    margin: 0;
    font-size: var(--ayan-font-size-sm);
    color: var(--ayan-tone-danger);
}
.ayan-kpi-card__notice {
    font-size: 0.78rem;
    font-style: italic;
    color: var(--ayan-color-muted);
}
.ayan-kpi-card__skeleton {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}
.ayan-skeleton-line {
    display: block;
    border-radius: var(--ayan-radius);
    background: linear-gradient(
        90deg,
        color-mix(in srgb, var(--ayan-color-secondary) 12%, transparent) 25%,
        color-mix(in srgb, var(--ayan-color-secondary) 22%, transparent) 37%,
        color-mix(in srgb, var(--ayan-color-secondary) 12%, transparent) 63%
    );
    background-size: 400% 100%;
    animation: ayan-skeleton-shimmer 1.4s ease infinite;
}
.ayan-skeleton-line--title { width: 45%; height: 0.8rem; }
.ayan-skeleton-line--value { width: 70%; height: 1.6rem; }
@keyframes ayan-skeleton-shimmer {
    0% { background-position: 100% 50%; }
    100% { background-position: 0 50%; }
}

@media (prefers-reduced-motion: reduce) {
    .ayan-loading-state__spinner,
    .ayan-skeleton-line { animation: none; }
}
