/* ==========================================================================
   Trade ERP — application stylesheet
   --------------------------------------------------------------------------
   Bilingual: the <html> element carries dir="ltr"|"rtl" and lang="en"|"ar".
   Everything below is written with CSS *logical* properties
   (inline-start / inline-end) so one rule serves both directions. The only
   [dir="rtl"] overrides in this file are for things logical properties
   genuinely cannot express: mirroring glyphs/icons and a couple of
   background-position values. There are no `left:` / `right:` layout rules.

   Design intent: a dense, calm, high-legibility internal tool. Merchandisers,
   buyers, warehouse staff and accountants stare at this for eight hours, so:
   compact type, 1px borders instead of shadows, one accent hue, and data
   tables treated as the primary surface rather than an afterthought.

   Contents
     1. Tokens (:root) + dark scheme
     2. Base / reset / typography
     3. Layout shell (sidebar, topbar, content)
     4. Page furniture
     5. Cards + grid
     6. Stats
     7. Tables
     8. Forms
     9. Buttons
    10. Badges
    11. Alerts
    12. Misc components
    13. Login
    14. Document line editor (line-table, totals, item search)
    15. Utilities
    16. Responsive
    17. Print
   ========================================================================== */

/* ==========================================================================
   1. Tokens
   Every colour has its base definition here on plain :root; the dark scheme
   below only *redefines* these same custom properties, so no component rule
   ever needs to know which scheme is active.
   ========================================================================== */

:root {
    /* --- Neutral ramp (surfaces, borders, text) --- */
    --c-canvas:        #f4f5f7;   /* app background behind cards            */
    --c-surface:       #ffffff;   /* card / table background                */
    --c-surface-2:     #fafbfc;   /* zebra stripe, table head, inset panes   */
    --c-surface-3:     #eef0f3;   /* hover on neutral surfaces               */
    --c-sunken:        #e9ecf0;   /* wells, disabled inputs                  */

    --c-border:        #dfe3e8;   /* the workhorse 1px line                  */
    --c-border-strong: #c6ccd4;   /* input borders, table outer edge         */
    --c-border-soft:   #ebeef1;   /* row separators inside dense tables      */

    --c-text:          #1c2430;   /* primary copy                            */
    --c-text-2:        #4a5563;   /* secondary copy, table headers           */
    --c-text-3:        #77828f;   /* muted, hints, timestamps                */
    --c-text-inverse:  #ffffff;

    /* --- Accent (single hue: indigo-leaning blue) --- */
    --c-accent:        #2f5fd0;
    --c-accent-hover:  #2750b4;
    --c-accent-active: #21428f;
    --c-accent-weak:   #e8eefb;   /* tinted background                       */
    --c-accent-weak-2: #d5e1f8;
    --c-accent-text:   #1f4396;   /* accent text on a weak background        */

    /* --- Semantic --- */
    --c-success:       #1f7a4d;
    --c-success-weak:  #e3f4ea;
    --c-success-line:  #b9e0c9;

    --c-warning:       #8a5a06;
    --c-warning-weak:  #fdf1dc;
    --c-warning-line:  #ecd3a3;

    --c-danger:        #b3261e;
    --c-danger-weak:   #fdeceb;
    --c-danger-line:   #f2c2be;

    --c-info:          #1d6382;
    --c-info-weak:     #e4f1f7;
    --c-info-line:     #b7d9e7;

    /* --- Sidebar (slightly darker than the canvas, not black) --- */
    --c-sidebar-bg:     #1e2530;
    --c-sidebar-bg-2:   #262f3c;
    --c-sidebar-border: #313b4a;
    --c-sidebar-text:   #c3cad4;
    --c-sidebar-text-2: #7e8a99;
    --c-sidebar-active-bg:   #2f5fd0;
    --c-sidebar-active-text: #ffffff;

    /* --- Focus --- */
    --c-focus:      #2f5fd0;
    --c-focus-ring: rgba(47, 95, 208, 0.32);

    /* --- Typography --- */
    --font-latin: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                  "Helvetica Neue", Arial, "Noto Sans", sans-serif;
    /* Arabic: prefer faces that ship on Windows/macOS/Linux desktops so the
       page never depends on a webfont download. */
    --font-arabic: "Segoe UI", Tahoma, "Noto Naskh Arabic", "Traditional Arabic",
                   "Arabic Typesetting", "Geeza Pro", Arial, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
                 "Liberation Mono", monospace;

    --fs-base:  13.5px;
    --fs-xs:    11px;
    --fs-sm:    12px;
    --fs-md:    13.5px;
    --fs-lg:    15px;
    --fs-xl:    18px;
    --fs-2xl:   22px;
    --lh-base:  1.5;
    --lh-tight: 1.25;

    /* --- Spacing scale (4px rhythm) --- */
    --sp-1: 4px;
    --sp-2: 8px;
    --sp-3: 12px;
    --sp-4: 16px;
    --sp-5: 20px;
    --sp-6: 24px;
    --sp-8: 32px;
    --sp-10: 40px;

    /* --- Radii, shadow, geometry --- */
    --radius-sm: 3px;
    --radius:    5px;
    --radius-lg: 8px;
    --radius-pill: 999px;

    /* Shadows are deliberately faint — borders do the structural work. */
    --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
    --shadow-md: 0 4px 12px rgba(16, 24, 40, 0.10);
    --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.16);

    /* Boxes — cards, panels and stat tiles.
       These are separate from the scale above on purpose: a card is the one
       element whose shape is the interface's signature, and changing it should
       not drag every input and badge along with it. The shadow is two layers,
       a tight contact edge and a wider soft one, which reads as a panel lifted
       off the page rather than a rectangle with a grey blur behind it. */
    --radius-card:  10px;
    --shadow-card:  0 1px 2px rgba(16, 24, 40, 0.04),
                    0 6px 16px -6px rgba(16, 24, 40, 0.10);
    --card-pad:     var(--sp-5);
    --card-pad-tight: var(--sp-4);
    /* The rail that marks a card header and a stat tile. */
    --card-rail:    3px;

    --sidebar-w:   232px;
    --topbar-h:    52px;
    --row-h:       34px;   /* the comfortable-but-dense table row height */
    --row-h-compact: 28px;
    --control-h:   32px;
    --control-h-sm: 26px;

    --transition: 120ms ease;
}

/* --------------------------------------------------------------------------
   Dark scheme — token redefinition only, no structural rules.
   -------------------------------------------------------------------------- */
@media (prefers-color-scheme: dark) {
    :root {
        --c-canvas:        #14181e;
        --c-surface:       #1b2028;
        --c-surface-2:     #20262f;
        --c-surface-3:     #272e38;
        --c-sunken:        #11151a;

        --c-border:        #2e3641;
        --c-border-strong: #3d4753;
        --c-border-soft:   #262d36;

        --c-text:          #e4e8ee;
        --c-text-2:        #aeb7c3;
        --c-text-3:        #7d8794;
        --c-text-inverse:  #10141a;

        --c-accent:        #5b8def;
        --c-accent-hover:  #6f9bf3;
        --c-accent-active: #85aaf6;
        --c-accent-weak:   #1d2a44;
        --c-accent-weak-2: #26375a;
        --c-accent-text:   #9dbcf8;

        --c-success:       #57c08a;
        --c-success-weak:  #14291f;
        --c-success-line:  #245c40;

        --c-warning:       #d9a441;
        --c-warning-weak:  #2c2313;
        --c-warning-line:  #5d4a1e;

        --c-danger:        #f28b82;
        --c-danger-weak:   #2e1917;
        --c-danger-line:   #6b302b;

        --c-info:          #6cbede;
        --c-info-weak:     #12262f;
        --c-info-line:     #275264;

        --c-sidebar-bg:     #11161c;
        --c-sidebar-bg-2:   #171d25;
        --c-sidebar-border: #232b35;
        --c-sidebar-text:   #b3bcc8;
        --c-sidebar-text-2: #6f7a88;
        --c-sidebar-active-bg:   #2b4d92;
        --c-sidebar-active-text: #ffffff;

        --c-focus:      #6f9bf3;
        --c-focus-ring: rgba(111, 155, 243, 0.36);

        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
        --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.5);
        --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.6);
        /* On a dark canvas a shadow alone cannot separate a panel from its
           background, so the contact edge is stronger and the border does more
           of the work than it does in daylight. */
        --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.5),
                       0 8px 20px -8px rgba(0, 0, 0, 0.65);
    }
}

/* ==========================================================================
   2. Base
   ========================================================================== */

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    -webkit-text-size-adjust: 100%;
    color-scheme: light dark;
}

body {
    margin: 0;
    background: var(--c-canvas);
    color: var(--c-text);
    font-family: var(--font-latin);
    font-size: var(--fs-base);
    line-height: var(--lh-base);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Arabic pages get a naskh-friendly stack and a hair more leading, because
   Arabic glyphs sit taller and need the extra breathing room to stay legible
   at 13.5px. */
html[lang="ar"] body {
    font-family: var(--font-arabic);
    line-height: 1.65;
}

h1, h2, h3, h4, h5, h6 {
    margin: 0;
    font-weight: 600;
    line-height: var(--lh-tight);
    color: var(--c-text);
}

p {
    margin: 0 0 var(--sp-3);
}

p:last-child {
    margin-bottom: 0;
}

a {
    color: var(--c-accent);
    text-decoration: none;
}

a:hover {
    color: var(--c-accent-hover);
    text-decoration: underline;
}

hr {
    border: 0;
    border-block-start: 1px solid var(--c-border);
    margin: var(--sp-4) 0;
}

small {
    font-size: var(--fs-sm);
}

code, kbd, samp, pre {
    font-family: var(--font-mono);
    font-size: 0.92em;
}

img, svg {
    max-width: 100%;
    vertical-align: middle;
}

/* One consistent, unmistakable keyboard focus treatment. Mouse users never
   see it (:focus-visible), keyboard users always do. */
:focus-visible {
    outline: 2px solid var(--c-focus);
    outline-offset: 1px;
    border-radius: var(--radius-sm);
}

::selection {
    background: var(--c-accent-weak-2);
    color: var(--c-text);
}

/* ==========================================================================
   3. Layout shell
   ========================================================================== */

.app {
    display: flex;
    min-height: 100vh;
    align-items: stretch;
}

/* --- Sidebar ------------------------------------------------------------ */

.sidebar {
    flex: 0 0 var(--sidebar-w);
    width: var(--sidebar-w);
    background: var(--c-sidebar-bg);
    color: var(--c-sidebar-text);
    /* Logical border: appears on the right in LTR, the left in RTL. */
    border-inline-end: 1px solid var(--c-sidebar-border);
    display: flex;
    flex-direction: column;
    position: sticky;
    inset-block-start: 0;
    align-self: flex-start;
    height: 100vh;
    z-index: 40;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    height: var(--topbar-h);
    padding-inline: var(--sp-4);
    flex: 0 0 auto;
    border-block-end: 1px solid var(--c-sidebar-border);
    color: #fff;
    font-size: var(--fs-lg);
    font-weight: 650;
    letter-spacing: 0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-brand:hover {
    text-decoration: none;
    color: #fff;
}

.sidebar-nav {
    flex: 1 1 auto;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: var(--sp-3) 0 var(--sp-6);
}

.nav-group {
    margin-block-end: var(--sp-3);
}

.nav-group-title {
    padding: var(--sp-2) var(--sp-4) var(--sp-1);
    font-size: var(--fs-xs);
    font-weight: 650;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    color: var(--c-sidebar-text-2);
    /* start = left in LTR, right in RTL */
    text-align: start;
}

/* Arabic has no case, so uppercasing is meaningless and slightly damaging;
   keep the label as authored and lean on weight/colour instead. */
html[lang="ar"] .nav-group-title {
    text-transform: none;
    letter-spacing: 0;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: 6px var(--sp-4);
    min-height: 30px;
    color: var(--c-sidebar-text);
    font-size: var(--fs-md);
    text-decoration: none;
    /* The active marker rides the inline start edge, so it flips automatically. */
    border-inline-start: 3px solid transparent;
    transition: background var(--transition), color var(--transition);
}

.nav-link:hover {
    background: var(--c-sidebar-bg-2);
    color: #fff;
    text-decoration: none;
}

.nav-link.is-active {
    background: var(--c-sidebar-active-bg);
    color: var(--c-sidebar-active-text);
    font-weight: 600;
    border-inline-start-color: rgba(255, 255, 255, 0.75);
}

.nav-link:focus-visible {
    outline-offset: -2px;
}

/* --- Main column -------------------------------------------------------- */

.main {
    flex: 1 1 auto;
    min-width: 0;               /* lets wide tables scroll instead of pushing */
    display: flex;
    flex-direction: column;
}

.topbar {
    position: sticky;
    inset-block-start: 0;
    z-index: 30;
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    height: var(--topbar-h);
    padding-inline: var(--sp-4);
    background: var(--c-surface);
    border-block-end: 1px solid var(--c-border);
}

.topbar-search {
    flex: 1 1 340px;
    max-width: 420px;
    height: var(--control-h);
    padding-inline: var(--sp-3);
    background: var(--c-surface-2);
    border: 1px solid var(--c-border);
    border-radius: var(--radius);
    color: var(--c-text);
    font: inherit;
    font-size: var(--fs-md);
    text-align: start;
}

.topbar-search::placeholder {
    color: var(--c-text-3);
}

.topbar-search:focus {
    outline: none;
    background: var(--c-surface);
    border-color: var(--c-focus);
    box-shadow: 0 0 0 3px var(--c-focus-ring);
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    /* auto margin on the inline start pushes the block to the inline end
       in both directions without any dir-specific rule. */
    margin-inline-start: auto;
}

.content {
    flex: 1 1 auto;
    padding: var(--sp-5) var(--sp-6) var(--sp-10);
    min-width: 0;
}

/* Burger — hidden on desktop, revealed in the responsive block. */
.sidebar-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    width: var(--control-h);
    height: var(--control-h);
    padding: 0;
    background: transparent;
    border: 1px solid var(--c-border);
    border-radius: var(--radius);
    color: var(--c-text-2);
    font-size: var(--fs-lg);
    line-height: 1;
    cursor: pointer;
}

.sidebar-toggle:hover {
    background: var(--c-surface-3);
    color: var(--c-text);
}

/* ==========================================================================
   4. Page furniture
   ========================================================================== */

.page-header {
    display: flex;
    align-items: flex-start;
    gap: var(--sp-4);
    flex-wrap: wrap;
    margin-block-end: var(--sp-5);
}

.page-title {
    font-size: var(--fs-2xl);
    font-weight: 650;
    letter-spacing: -0.01em;
    margin: 0;
}

html[lang="ar"] .page-title {
    letter-spacing: 0;
}

.page-subtitle {
    margin-block-start: 2px;
    color: var(--c-text-3);
    font-size: var(--fs-md);
}

.page-actions {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    flex-wrap: wrap;
    margin-inline-start: auto;   /* flips with direction */
}

.breadcrumb {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--sp-1);
    margin-block-end: var(--sp-2);
    padding: 0;
    list-style: none;
    font-size: var(--fs-sm);
    color: var(--c-text-3);
}

.breadcrumb a {
    color: var(--c-text-3);
}

.breadcrumb a:hover {
    color: var(--c-accent);
}

/* The separator is a chevron: it must point along the reading direction, so
   this is one of the few genuine [dir="rtl"] cases — a mirrored glyph. */
.breadcrumb > * + *::before {
    content: "\203A";           /* › */
    display: inline-block;
    margin-inline-end: var(--sp-1);
    color: var(--c-border-strong);
}

[dir="rtl"] .breadcrumb > * + *::before {
    content: "\2039";           /* ‹ */
}

/* ==========================================================================
   5. Cards + grid
   ========================================================================== */

.card {
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-card);
    box-shadow: var(--shadow-card);
    margin-block-end: var(--sp-5);
    overflow: hidden;           /* keeps table corners inside the radius */
}

.card:last-child {
    margin-block-end: 0;
}

/* --------------------------------------------------------------------------
   Card header.

   The title carries a short coloured rail on its leading edge rather than the
   header carrying a coloured band: a full band on every card turns a screen
   with six panels into six competing stripes, while a rail beside the words
   marks the section and stays quiet when there are many of them. It is drawn
   with a pseudo-element and logical properties, so it moves to the right-hand
   side on an Arabic page without a second rule.
   -------------------------------------------------------------------------- */
.card-header {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    flex-wrap: wrap;
    padding: var(--sp-4) var(--card-pad);
    background: var(--c-surface-2);
    border-block-end: 1px solid var(--c-border);
    min-height: 48px;
}

.card-title {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    font-size: var(--fs-lg);
    font-weight: 650;
    letter-spacing: -0.01em;
    margin: 0;
}

.card-title::before {
    content: "";
    flex: 0 0 auto;
    inline-size: var(--card-rail);
    block-size: 1.05em;
    border-radius: var(--radius-pill);
    background: var(--c-accent);
}

/* A header that is only a toolbar — filters, a search box, buttons — has no
   title to hang the rail on, so it keeps the plain band it always had. */
.card-header:not(:has(.card-title)) {
    padding-block: var(--sp-3);
}

/* Anything after the title in a header (buttons, filters) drifts to the end. */
.card-header > .card-title ~ * {
    margin-inline-start: auto;
}

.card-header > .card-title ~ * ~ * {
    margin-inline-start: 0;
}

.card-body {
    padding: var(--card-pad);
}

/* A card that is nothing but a body reads better with the same generous inset
   on all four sides; one that follows a header does not need the extra top. */
.card > .card-header + .card-body {
    padding-block-start: var(--card-pad-tight);
}

/* A table dropped straight into a card should sit flush to the edges. */
.card-body > .table-wrap:only-child,
.card > .table-wrap {
    margin: 0;
}

.card > .table-wrap {
    border: 0;
    border-radius: 0;
}

.card-footer {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    flex-wrap: wrap;
    padding: var(--sp-3) var(--card-pad);
    background: var(--c-surface-2);
    border-block-start: 1px solid var(--c-border);
}

.grid {
    display: grid;
    gap: var(--sp-4);
    grid-template-columns: 1fr;
}

.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }

/* .grid children that are cards shouldn't also carry the stacking margin. */
.grid > .card {
    margin-block-end: 0;
}

/* Stat tiles inside a card that itself sits in a grid column: let them wrap
   onto a second row instead of clipping their figures. */
.grid > .card .grid-3,
.grid > .card .grid-4 {
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
}

/* ==========================================================================
   6. Stats
   ========================================================================== */

.stat {
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-card);
    padding: var(--card-pad-tight) var(--card-pad);
    /* Coloured status rail on the inline start edge — flips with direction. */
    border-inline-start: var(--card-rail) solid var(--c-border-strong);
    box-shadow: var(--shadow-card);
}

.stat-label {
    display: block;
    font-size: var(--fs-xs);
    font-weight: 650;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--c-text-3);
}

html[lang="ar"] .stat-label {
    text-transform: none;
    letter-spacing: 0;
    font-size: var(--fs-sm);
}

.stat-value {
    display: block;
    margin-block-start: 2px;
    font-size: var(--fs-2xl);
    font-weight: 650;
    line-height: 1.15;
    letter-spacing: -0.015em;
    font-variant-numeric: tabular-nums;
    /* Figures stay LTR even on an Arabic page. */
    direction: ltr;
    text-align: start;
}

/* On an RTL page the value block itself should still hug the start edge of
   the card, but its internal digit order is LTR (set above). */
[dir="rtl"] .stat-value {
    text-align: right;
}

.stat-sub {
    display: block;
    margin-block-start: 2px;
    font-size: var(--fs-sm);
    color: var(--c-text-3);
}

.stat.is-warning {
    border-inline-start-color: var(--c-warning);
}

.stat.is-warning .stat-value {
    color: var(--c-warning);
}

.stat.is-danger {
    border-inline-start-color: var(--c-danger);
}

.stat.is-danger .stat-value {
    color: var(--c-danger);
}

/* ==========================================================================
   7. Tables — the primary surface of the whole application
   ========================================================================== */

.table-wrap {
    /* Wide documents scroll inside their own container; the page body never
       scrolls sideways. */
    overflow-x: auto;
    overflow-y: visible;
    max-width: 100%;
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-lg);
    -webkit-overflow-scrolling: touch;
}

.table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: var(--fs-md);
    background: var(--c-surface);
}

/* Sticky header: buyers scroll hundreds of lines and must keep the column
   labels. `position: sticky` on <th> requires border-collapse: separate,
   which is why the borders below are applied per-cell. */
.table thead th {
    position: sticky;
    inset-block-start: 0;
    z-index: 2;
    background: var(--c-surface-2);
    color: var(--c-text-2);
    font-size: var(--fs-xs);
    font-weight: 650;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    text-align: start;
    white-space: nowrap;
    padding: var(--sp-2) var(--sp-3);
    border-block-end: 1px solid var(--c-border-strong);
    vertical-align: middle;
}

html[lang="ar"] .table thead th {
    text-transform: none;
    letter-spacing: 0;
    font-size: var(--fs-sm);
}

.table tbody td {
    height: var(--row-h);
    padding: var(--sp-1) var(--sp-3);
    /* Horizontal rules only. Vertical gridlines make dense financial tables
       read like a spreadsheet and slow scanning down — deliberately omitted. */
    border-block-end: 1px solid var(--c-border-soft);
    vertical-align: middle;
    text-align: start;
}

.table tbody tr:last-child td {
    border-block-end: 0;
}

.table tbody tr:hover td {
    background: var(--c-surface-3);
}

/* Zebra beneath the hover highlight for long lists. */
.table tbody tr:nth-child(even) td {
    background: var(--c-surface-2);
}

.table tbody tr:nth-child(even):hover td {
    background: var(--c-surface-3);
}

.table tbody tr.is-selected td {
    background: var(--c-accent-weak);
}

.table tfoot td,
.table tfoot th {
    padding: var(--sp-2) var(--sp-3);
    background: var(--c-surface-2);
    border-block-start: 2px solid var(--c-border-strong);
    font-weight: 650;
    color: var(--c-text);
    text-align: start;
}

.table caption {
    caption-side: top;
    padding: var(--sp-2) var(--sp-3);
    color: var(--c-text-3);
    font-size: var(--fs-sm);
    text-align: start;
}

.table-compact tbody td {
    height: var(--row-h-compact);
    padding-block: 0;
    font-size: var(--fs-sm);
}

.table-compact thead th {
    padding-block: var(--sp-1);
}

/* --- Numerics ----------------------------------------------------------
   Money and quantities are written with Latin digits even on Arabic pages.
   Left to inherit, an RTL paragraph direction can visually reorder a run
   like "1,250.00 -" or "(1,250.00)". Forcing direction: ltr on the cell
   guarantees the bidi algorithm treats the number as one LTR run, while
   text-align: end keeps it hugging the column's reading end (right in LTR,
   left in RTL) so decimal points still line up.
   ----------------------------------------------------------------------- */
.num,
.table td.num,
.table th.num,
.table tfoot .num {
    direction: ltr;
    text-align: end;
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum" 1, "lnum" 1;
    white-space: nowrap;
    /* Slight tracking + the system UI font's tabular figures give a
       monospace-ish column without dropping to a true mono face. */
    letter-spacing: 0.01em;
    font-variant-ligatures: none;
}

/* In RTL the "end" of an LTR-scoped box is its right edge; we want the number
   pinned to the column's visual left edge instead, matching how Arabic
   spreadsheets present figures. */
[dir="rtl"] .num,
[dir="rtl"] .table td.num,
[dir="rtl"] .table th.num {
    text-align: left;
}

.num.is-negative,
.table td.num.is-negative {
    color: var(--c-danger);
}

.table-actions {
    display: flex;
    align-items: center;
    gap: var(--sp-1);
    justify-content: flex-end;   /* flex honours direction automatically */
    white-space: nowrap;
}

/* ==========================================================================
   8. Forms
   ========================================================================== */

.form {
    display: block;
}

.form-row {
    display: flex;
    flex-wrap: wrap;
    gap: var(--sp-3) var(--sp-4);
    margin-block-end: var(--sp-3);
    align-items: flex-start;
}

.form-row > .field {
    flex: 1 1 220px;
    min-width: 0;
}

.field {
    display: block;
    margin-block-end: var(--sp-3);
    min-width: 0;
}

.form-row > .field:last-child,
.field:last-child {
    margin-block-end: 0;
}

.field-label {
    display: block;
    margin-block-end: var(--sp-1);
    font-size: var(--fs-sm);
    font-weight: 600;
    color: var(--c-text-2);
    text-align: start;
}

.field-hint {
    display: block;
    margin-block-start: var(--sp-1);
    font-size: var(--fs-sm);
    color: var(--c-text-3);
    text-align: start;
}

.field-error {
    display: block;
    margin-block-start: var(--sp-1);
    font-size: var(--fs-sm);
    font-weight: 500;
    color: var(--c-danger);
    text-align: start;
}

/* A field laid out on one line: label, control, then anything else. */
.field-inline {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    margin-block-end: var(--sp-3);
    flex-wrap: wrap;
}

.field-inline .field-label {
    margin-block-end: 0;
    white-space: nowrap;
}

/* The little red dot marking a required field. It sits after the label text
   in the reading direction. */
.required-dot {
    display: inline-block;
    inline-size: 5px;
    block-size: 5px;
    margin-inline-start: var(--sp-1);
    border-radius: 50%;
    background: var(--c-danger);
    vertical-align: 3px;
}

.input,
.select,
.textarea {
    display: block;
    inline-size: 100%;
    min-inline-size: 0;
    block-size: var(--control-h);
    padding-block: 0;
    padding-inline: var(--sp-2);
    background: var(--c-surface);
    color: var(--c-text);
    border: 1px solid var(--c-border-strong);
    border-radius: var(--radius);
    font: inherit;
    font-size: var(--fs-md);
    line-height: calc(var(--control-h) - 2px);
    text-align: start;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.textarea {
    block-size: auto;
    min-block-size: 76px;
    padding-block: var(--sp-2);
    line-height: var(--lh-base);
    resize: vertical;
}

/* Native select arrow: drawn as a background image so it can be positioned
   at the inline end. background-position has no logical equivalent, so this
   is a legitimate [dir] override (see the RTL rule below). */
.select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    padding-inline-end: var(--sp-6);
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath fill='%2377828f' d='M0 0h10L5 6z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--sp-2) center;
    background-size: 10px 6px;
}

[dir="rtl"] .select {
    background-position: left var(--sp-2) center;
}

.input::placeholder,
.textarea::placeholder {
    color: var(--c-text-3);
}

.input:hover,
.select:hover,
.textarea:hover {
    border-color: var(--c-text-3);
}

.input:focus,
.select:focus,
.textarea:focus {
    outline: none;
    border-color: var(--c-focus);
    box-shadow: 0 0 0 3px var(--c-focus-ring);
}

.input:disabled,
.select:disabled,
.textarea:disabled,
.input[readonly],
.textarea[readonly] {
    background: var(--c-sunken);
    color: var(--c-text-3);
    cursor: not-allowed;
    border-color: var(--c-border);
}

.input.has-error,
.select.has-error,
.textarea.has-error {
    border-color: var(--c-danger);
    background: var(--c-danger-weak);
}

.input.has-error:focus,
.select.has-error:focus,
.textarea.has-error:focus {
    box-shadow: 0 0 0 3px rgba(179, 38, 30, 0.22);
}

/* Number inputs holding money should behave like .num. */
.input[type="number"],
.input.num {
    direction: ltr;
    text-align: end;
    font-variant-numeric: tabular-nums;
}

.checkbox {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    font-size: var(--fs-md);
    cursor: pointer;
    user-select: none;
}

.checkbox input[type="checkbox"],
.checkbox input[type="radio"] {
    inline-size: 15px;
    block-size: 15px;
    margin: 0;
    accent-color: var(--c-accent);
    cursor: pointer;
    flex: 0 0 auto;
}

.checkbox:has(input:disabled) {
    color: var(--c-text-3);
    cursor: not-allowed;
}

/* ==========================================================================
   9. Buttons
   ========================================================================== */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    block-size: var(--control-h);
    padding-inline: var(--sp-3);
    background: var(--c-surface);
    color: var(--c-text);
    border: 1px solid var(--c-border-strong);
    border-radius: var(--radius);
    font: inherit;
    font-size: var(--fs-md);
    font-weight: 550;
    line-height: 1;
    white-space: nowrap;
    text-decoration: none;
    cursor: pointer;
    transition: background var(--transition), border-color var(--transition),
                color var(--transition);
}

.btn:hover {
    background: var(--c-surface-3);
    border-color: var(--c-text-3);
    color: var(--c-text);
    text-decoration: none;
}

.btn:active {
    background: var(--c-sunken);
}

.btn:disabled,
.btn.is-disabled,
.btn[aria-disabled="true"] {
    opacity: 0.55;
    cursor: not-allowed;
    background: var(--c-sunken);
    border-color: var(--c-border);
    color: var(--c-text-3);
    pointer-events: none;
}

.btn-primary {
    background: var(--c-accent);
    border-color: var(--c-accent);
    color: #fff;
}

.btn-primary:hover {
    background: var(--c-accent-hover);
    border-color: var(--c-accent-hover);
    color: #fff;
}

.btn-primary:active {
    background: var(--c-accent-active);
    border-color: var(--c-accent-active);
}

.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--c-text-2);
}

.btn-ghost:hover {
    background: var(--c-surface-3);
    border-color: transparent;
    color: var(--c-text);
}

.btn-danger {
    background: var(--c-surface);
    border-color: var(--c-danger-line);
    color: var(--c-danger);
}

.btn-danger:hover {
    background: var(--c-danger);
    border-color: var(--c-danger);
    color: #fff;
}

.btn-sm {
    block-size: var(--control-h-sm);
    padding-inline: var(--sp-2);
    font-size: var(--fs-sm);
}

.btn-icon {
    inline-size: var(--control-h);
    padding-inline: 0;
    flex: 0 0 auto;
}

.btn-icon.btn-sm {
    inline-size: var(--control-h-sm);
}

/* Directional glyphs inside buttons (‹ › ← →) must mirror in RTL. */
[dir="rtl"] .btn .icon-chevron,
[dir="rtl"] .btn .icon-arrow,
[dir="rtl"] .pagination .icon-chevron {
    transform: scaleX(-1);
}

/* ==========================================================================
   10. Badges — document statuses. Muted tints, not saturated pills: a screen
   with forty rows of statuses should not look like confetti.
   ========================================================================== */

.badge {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    padding: 1px var(--sp-2);
    min-block-size: 19px;
    border: 1px solid transparent;
    border-radius: var(--radius-pill);
    background: var(--c-surface-3);
    color: var(--c-text-2);
    font-size: var(--fs-xs);
    font-weight: 650;
    letter-spacing: 0.02em;
    line-height: 1.5;
    white-space: nowrap;
    vertical-align: baseline;
}

html[lang="ar"] .badge {
    font-size: var(--fs-sm);
    letter-spacing: 0;
}

/* Neutral / not-yet-started */
.badge-draft {
    background: var(--c-surface-3);
    border-color: var(--c-border-strong);
    color: var(--c-text-2);
}

/* Waiting on a human */
.badge-pending {
    background: var(--c-warning-weak);
    border-color: var(--c-warning-line);
    color: var(--c-warning);
}

/* Signed off */
.badge-approved {
    background: var(--c-success-weak);
    border-color: var(--c-success-line);
    color: var(--c-success);
}

/* Out the door, awaiting the counterparty */
.badge-sent {
    background: var(--c-info-weak);
    border-color: var(--c-info-line);
    color: var(--c-info);
}

/* Goods in */
.badge-received {
    background: var(--c-accent-weak);
    border-color: var(--c-accent-weak-2);
    color: var(--c-accent-text);
}

/* Some, but not all */
.badge-partial {
    background: var(--c-warning-weak);
    border-color: var(--c-warning-line);
    color: var(--c-warning);
}

/* Done and locked */
.badge-closed {
    background: var(--c-sunken);
    border-color: var(--c-border-strong);
    color: var(--c-text-3);
}

/* Dead */
.badge-cancelled {
    background: var(--c-danger-weak);
    border-color: var(--c-danger-line);
    color: var(--c-danger);
    text-decoration: line-through;
    text-decoration-thickness: 1px;
}

.badge-muted {
    background: var(--c-surface-2);
    border-color: var(--c-border);
    color: var(--c-text-3);
}

.badge-success {
    background: var(--c-success-weak);
    border-color: var(--c-success-line);
    color: var(--c-success);
}

.badge-danger {
    background: var(--c-danger-weak);
    border-color: var(--c-danger-line);
    color: var(--c-danger);
}

/* ==========================================================================
   11. Alerts
   ========================================================================== */

.alert {
    display: flex;
    align-items: flex-start;
    gap: var(--sp-2);
    padding: var(--sp-3) var(--sp-4);
    margin-block-end: var(--sp-4);
    background: var(--c-surface-2);
    border: 1px solid var(--c-border);
    /* Accent rail on the reading-start edge. */
    border-inline-start: 3px solid var(--c-border-strong);
    border-radius: var(--radius);
    font-size: var(--fs-md);
    color: var(--c-text);
    transition: opacity 260ms ease;
}

.alert.is-hiding {
    opacity: 0;
}

.alert p:last-child {
    margin-block-end: 0;
}

.alert-success {
    background: var(--c-success-weak);
    border-color: var(--c-success-line);
    border-inline-start-color: var(--c-success);
    color: var(--c-success);
}

.alert-danger {
    background: var(--c-danger-weak);
    border-color: var(--c-danger-line);
    border-inline-start-color: var(--c-danger);
    color: var(--c-danger);
}

.alert-warning {
    background: var(--c-warning-weak);
    border-color: var(--c-warning-line);
    border-inline-start-color: var(--c-warning);
    color: var(--c-warning);
}

.alert-info {
    background: var(--c-info-weak);
    border-color: var(--c-info-line);
    border-inline-start-color: var(--c-info);
    color: var(--c-info);
}

/* ==========================================================================
   12. Misc components
   ========================================================================== */

.muted {
    color: var(--c-text-3);
}

/* --- Pagination --------------------------------------------------------- */

.pagination {
    display: flex;
    align-items: center;
    gap: var(--sp-1);
    flex-wrap: wrap;
    margin-block-start: var(--sp-4);
    padding: 0;
    list-style: none;
}

.pagination a,
.pagination span {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-inline-size: var(--control-h-sm);
    block-size: var(--control-h-sm);
    padding-inline: var(--sp-2);
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius);
    color: var(--c-text-2);
    font-size: var(--fs-sm);
    font-variant-numeric: tabular-nums;
    text-decoration: none;
}

.pagination a:hover {
    background: var(--c-surface-3);
    border-color: var(--c-border-strong);
    color: var(--c-text);
    text-decoration: none;
}

.pagination .is-current {
    background: var(--c-accent);
    border-color: var(--c-accent);
    color: #fff;
    font-weight: 650;
}

.pagination .is-disabled {
    opacity: 0.45;
    pointer-events: none;
}

/* --- Empty state -------------------------------------------------------- */

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    padding: var(--sp-10) var(--sp-6);
    color: var(--c-text-3);
    text-align: center;
}

.empty-state strong {
    color: var(--c-text-2);
    font-size: var(--fs-lg);
    font-weight: 600;
}

/* --- Timeline (audit trail / document history) -------------------------- */

.timeline {
    list-style: none;
    margin: 0;
    padding: 0;
    /* The spine runs down the reading-start edge and flips with direction. */
    border-inline-start: 2px solid var(--c-border);
    padding-inline-start: var(--sp-4);
}

.timeline-item {
    position: relative;
    padding-block: var(--sp-2);
    font-size: var(--fs-md);
}

.timeline-item::before {
    content: "";
    position: absolute;
    inline-size: 7px;
    block-size: 7px;
    border-radius: 50%;
    background: var(--c-surface);
    border: 2px solid var(--c-accent);
    /* inset-inline-start is logical: right edge in RTL, left in LTR. */
    inset-inline-start: calc(var(--sp-4) * -1 - 6px);
    inset-block-start: 15px;
}

.timeline-item + .timeline-item {
    border-block-start: 1px solid var(--c-border-soft);
}

.timeline-time {
    display: block;
    font-size: var(--fs-sm);
    color: var(--c-text-3);
    font-variant-numeric: tabular-nums;
    /* Timestamps are Latin-digit and must not be reordered on Arabic pages. */
    direction: ltr;
    text-align: start;
}

[dir="rtl"] .timeline-time {
    text-align: right;
}

/* --- Tabs --------------------------------------------------------------- */

.tabs {
    display: flex;
    align-items: flex-end;
    gap: var(--sp-1);
    margin-block-end: var(--sp-4);
    border-block-end: 1px solid var(--c-border);
    overflow-x: auto;
  flex-wrap: wrap;
}

.tab {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    padding: var(--sp-2) var(--sp-3);
    margin-block-end: -1px;
    background: transparent;
    border: 1px solid transparent;
    border-block-end: 2px solid transparent;
    border-radius: var(--radius) var(--radius) 0 0;
    color: var(--c-text-2);
    font-size: var(--fs-md);
    font-weight: 550;
    white-space: nowrap;
    cursor: pointer;
    text-decoration: none;
}

.tab:hover {
    color: var(--c-text);
    background: var(--c-surface-2);
    text-decoration: none;
}

.tab.is-active {
    color: var(--c-accent);
    border-block-end-color: var(--c-accent);
    font-weight: 650;
}

/* --- Chip (filter token) ------------------------------------------------ */

.chip {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-1);
    padding: 1px var(--sp-2);
    background: var(--c-accent-weak);
    border: 1px solid var(--c-accent-weak-2);
    border-radius: var(--radius-pill);
    color: var(--c-accent-text);
    font-size: var(--fs-sm);
    white-space: nowrap;
}

.chip a,
.chip button {
    background: none;
    border: 0;
    padding: 0;
    color: inherit;
    font: inherit;
    line-height: 1;
    cursor: pointer;
    opacity: 0.7;
}

.chip a:hover,
.chip button:hover {
    opacity: 1;
    text-decoration: none;
}

/* --- Toolbar / filters -------------------------------------------------- */

.toolbar {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    flex-wrap: wrap;
    margin-block-end: var(--sp-3);
}

.toolbar > .btn + .btn {
    margin-inline-start: 0;
}

.filters {
    display: flex;
    align-items: flex-end;
    gap: var(--sp-3);
    flex-wrap: wrap;
    padding: var(--sp-3) var(--sp-4);
    margin-block-end: var(--sp-4);
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-lg);
}

.filters .field {
    margin-block-end: 0;
    flex: 0 1 200px;
    min-inline-size: 140px;
}

.filters .btn {
    flex: 0 0 auto;
}

/* --- Divider / key-value list ------------------------------------------- */

.divider {
    block-size: 1px;
    background: var(--c-border);
    border: 0;
    margin-block: var(--sp-4);
}

.kv {
    display: grid;
    /* The label column follows its longest label, but never past 45% of the
       card: an Arabic "VAT registration no." must wrap rather than squeeze
       every value beside it into a sliver. */
    grid-template-columns: fit-content(45%) 1fr;
    gap: var(--sp-1) var(--sp-4);
    margin: 0;
    font-size: var(--fs-md);
}

.kv dt {
    color: var(--c-text-3);
    font-weight: 550;
    text-align: start;
    margin: 0;
    min-width: 120px;
}

.kv dd {
    margin: 0;
    color: var(--c-text);
    text-align: start;
    min-width: 0;
    overflow-wrap: anywhere;
}

.kv dd.num {
    text-align: start;
    direction: ltr;
    /* A value like "JV-2026-000012 · 13/09/2026" may break at the separator
       rather than run out of a narrow card. */
    white-space: normal;
}

/* --- Spinner ------------------------------------------------------------ */

.spinner {
    display: inline-block;
    inline-size: 14px;
    block-size: 14px;
    border: 2px solid var(--c-border-strong);
    border-block-start-color: var(--c-accent);
    border-radius: 50%;
    /* Rotation direction is direction-agnostic — no RTL override needed. */
    animation: erp-spin 620ms linear infinite;
    vertical-align: -2px;
}

@keyframes erp-spin {
    to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
    .spinner {
        animation-duration: 2s;
    }

    * {
        transition-duration: 1ms !important;
    }
}

/* ==========================================================================
   13. Login
   ========================================================================== */

.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-block-size: 100vh;
    padding: var(--sp-6);
    background: var(--c-canvas);
}

.login-card {
    inline-size: 100%;
    max-inline-size: 360px;
    padding: var(--sp-6);
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
}

.login-logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    margin-block-end: var(--sp-5);
    font-size: var(--fs-xl);
    font-weight: 650;
    color: var(--c-text);
}

.login-card .btn-primary {
    inline-size: 100%;
    margin-block-start: var(--sp-2);
}

/* --- Language switch ---------------------------------------------------- */

.lang-switch {
    display: inline-flex;
    align-items: center;
    gap: 0;
    border: 1px solid var(--c-border-strong);
    border-radius: var(--radius);
    overflow: hidden;
    /* Always presented in a fixed EN | AR order regardless of page direction,
       so the control does not appear to swap when the user switches. */
    direction: ltr;
}

.lang-switch a,
.lang-switch button {
    display: inline-flex;
    align-items: center;
    block-size: calc(var(--control-h) - 2px);
    padding-inline: var(--sp-2);
    background: var(--c-surface);
    border: 0;
    color: var(--c-text-2);
    font: inherit;
    font-size: var(--fs-sm);
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}

.lang-switch a + a,
.lang-switch button + button {
    border-inline-start: 1px solid var(--c-border);
}

.lang-switch a:hover,
.lang-switch button:hover {
    background: var(--c-surface-3);
    text-decoration: none;
}

.lang-switch .is-active,
.lang-switch [aria-current="true"] {
    background: var(--c-accent);
    color: #fff;
}

.login-page .lang-switch {
    display: flex;
    margin-block-start: var(--sp-4);
    inline-size: fit-content;
    margin-inline: auto;
}

/* ==========================================================================
   14. Document line editor
   ========================================================================== */

.line-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: var(--fs-sm);
    background: var(--c-surface);
}

.line-table thead th {
    position: sticky;
    inset-block-start: 0;
    z-index: 2;
    background: var(--c-surface-2);
    color: var(--c-text-2);
    font-size: var(--fs-xs);
    font-weight: 650;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    text-align: start;
    white-space: nowrap;
    padding: var(--sp-2) var(--sp-2);
    border-block-end: 1px solid var(--c-border-strong);
}

html[lang="ar"] .line-table thead th {
    text-transform: none;
    letter-spacing: 0;
    font-size: var(--fs-sm);
}

.line-table td {
    padding: 3px var(--sp-1);
    border-block-end: 1px solid var(--c-border-soft);
    vertical-align: middle;
    text-align: start;
}

.line-table tbody tr:hover td {
    background: var(--c-surface-2);
}

/* Controls inside the grid are borderless until touched — this keeps a
   fifty-line purchase order from looking like a wall of boxes. */
.line-table .input,
.line-table .select {
    block-size: 26px;
    line-height: 24px;
    padding-inline: var(--sp-1);
    font-size: var(--fs-sm);
    background: transparent;
    border-color: transparent;
    border-radius: var(--radius-sm);
}

.line-table .input:hover,
.line-table .select:hover {
    background: var(--c-surface);
    border-color: var(--c-border);
}

.line-table .input:focus,
.line-table .select:focus {
    background: var(--c-surface);
    border-color: var(--c-focus);
}

.line-table .input.has-error {
    background: var(--c-danger-weak);
    border-color: var(--c-danger);
}

.line-table [data-line-total] {
    font-variant-numeric: tabular-nums;
    direction: ltr;
    text-align: end;
    font-weight: 600;
    white-space: nowrap;
}

[dir="rtl"] .line-table [data-line-total] {
    text-align: left;
}

.line-table tbody tr.is-free td {
    color: var(--c-text-3);
    font-style: italic;
}

.line-table .row-index {
    inline-size: 30px;
    color: var(--c-text-3);
    font-variant-numeric: tabular-nums;
    text-align: center;
}

/* --- Totals block ------------------------------------------------------- */

.totals {
    inline-size: 100%;
    max-inline-size: 340px;
    /* Pushes the block to the reading end of its container in both dirs. */
    margin-inline-start: auto;
    padding: var(--sp-3) var(--sp-4);
    background: var(--c-surface-2);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-lg);
}

.totals-row {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--sp-4);
    padding-block: 3px;
    font-size: var(--fs-md);
    color: var(--c-text-2);
}

.totals-row > :last-child {
    font-variant-numeric: tabular-nums;
    direction: ltr;
    text-align: end;
    white-space: nowrap;
    color: var(--c-text);
}

.totals-row.is-grand {
    margin-block-start: var(--sp-2);
    padding-block-start: var(--sp-2);
    border-block-start: 2px solid var(--c-border-strong);
    font-size: var(--fs-lg);
    font-weight: 650;
    color: var(--c-text);
}

.totals-row.is-grand > :last-child {
    font-size: var(--fs-xl);
    font-weight: 700;
}

/* --- Item lookup dropdown ---------------------------------------------- */

.item-search-results {
    position: absolute;
    z-index: 60;
    inline-size: max(280px, 100%);
    max-block-size: 300px;
    overflow-y: auto;
    margin: 2px 0 0;
    padding: 0;
    list-style: none;
    background: var(--c-surface);
    border: 1px solid var(--c-border-strong);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
}

.item-search-results:empty,
.item-search-results[hidden] {
    display: none;
}

.item-search-result {
    display: flex;
    align-items: baseline;
    gap: var(--sp-2);
    flex-wrap: wrap;
    padding: var(--sp-2) var(--sp-3);
    border-block-end: 1px solid var(--c-border-soft);
    cursor: pointer;
    font-size: var(--fs-sm);
    text-align: start;
}

.item-search-result:last-child {
    border-block-end: 0;
}

.item-search-result:hover,
.item-search-result.is-active {
    background: var(--c-accent-weak);
}

/* Keyboard cursor gets a visible rail so it is distinguishable from hover. */
.item-search-result.is-active {
    box-shadow: inset 3px 0 0 0 var(--c-accent);
}

[dir="rtl"] .item-search-result.is-active {
    box-shadow: inset -3px 0 0 0 var(--c-accent);
}

.item-search-result .sku,
.item-search-result .num {
    font-variant-numeric: tabular-nums;
    direction: ltr;
    color: var(--c-text-3);
    font-size: var(--fs-xs);
    white-space: nowrap;
}

.item-search-result .sku {
    font-family: var(--font-mono);
}

.item-search-result .desc {
    flex: 1 1 160px;
    min-inline-size: 0;
    color: var(--c-text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.item-search-result .num {
    margin-inline-start: auto;
    color: var(--c-text-2);
}

/* The element that owns the dropdown needs a positioning context. Applied
   unconditionally to the likely hosts (cheap, no visual effect) so the
   dropdown anchors correctly even without :has() support. */
.line-table td,
.field,
.field-inline,
.toolbar {
    position: relative;
}

/* ==========================================================================
   15. Utilities the views rely on incidentally
   ========================================================================== */

.no-print {
    /* No screen styling — this class exists purely for the print block. */
}

/* ==========================================================================
   16. Responsive
   ========================================================================== */

@media (max-width: 900px) {
    /* Every grid collapses to a single column at tablet width; three columns
       of dense numbers below this is unreadable. */
    .grid-2,
    .grid-3,
    .grid-4 {
        grid-template-columns: 1fr;
    }

    .content {
        padding: var(--sp-4) var(--sp-4) var(--sp-8);
    }

    .totals {
        max-inline-size: none;
    }

    .kv {
        grid-template-columns: 1fr;
        gap: 0 0;
    }

    .kv dt {
        margin-block-start: var(--sp-2);
    }
}

@media (max-width: 1023px) {
    .sidebar-toggle {
        display: inline-flex;
    }

    /* Off-canvas drawer. It slides out past the inline-start edge, so RTL
       gets the mirrored behaviour for free from the logical inset. */
    .sidebar {
        position: fixed;
        inset-block: 0;
        inset-inline-start: 0;
        transform: translateX(-100%);
        transition: transform 180ms ease;
        box-shadow: var(--shadow-lg);
    }

    /* translateX is a physical transform: in RTL the drawer must hide toward
       the right instead. This is a genuine [dir] exception. */
    [dir="rtl"] .sidebar {
        transform: translateX(100%);
    }

    .sidebar.is-open {
        transform: translateX(0);
    }

    [dir="rtl"] .sidebar.is-open {
        transform: translateX(0);
    }

    /* Scrim over the content while the drawer is open; clicking it closes
       the drawer (handled in app.js). */
    .sidebar.is-open ~ .main .content::before {
        content: "";
        position: fixed;
        inset: 0;
        background: rgba(12, 16, 22, 0.45);
        z-index: 35;
    }
}

@media (max-width: 640px) {
    .topbar-search {
        flex: 1 1 auto;
        max-width: none;
    }

    .page-header {
        gap: var(--sp-2);
    }

    .page-actions {
        margin-inline-start: 0;
        inline-size: 100%;
    }

    .filters .field {
        flex: 1 1 100%;
    }

    /* On a phone the generous inset costs more than it gives: the content is
       already narrow, so the box tightens rather than the text. */
    .card-body,
    .card > .card-header + .card-body {
        padding: var(--sp-3);
    }

    .card-header,
    .card-footer {
        padding-inline: var(--sp-3);
    }

    .stat {
        padding: var(--sp-3);
        padding-inline-start: var(--sp-3);
    }
}

/* ==========================================================================
   17. Print — purchase orders, GRNs and pick lists all get printed daily.
   ========================================================================== */

@media print {
    :root {
        /* Force the light palette; a dark-scheme machine must not print a
           black page. */
        --c-canvas: #ffffff;
        --c-surface: #ffffff;
        --c-surface-2: #ffffff;
        --c-surface-3: #ffffff;
        --c-sunken: #ffffff;
        --c-border: #999999;
        --c-border-strong: #666666;
        --c-border-soft: #bbbbbb;
        --c-text: #000000;
        --c-text-2: #222222;
        --c-text-3: #555555;
    }

    body {
        margin: 0;
        background: none;
        color: #000;
        font-size: 10.5pt;
    }

    .no-print,
    .sidebar,
    .topbar,
    .sidebar-toggle,
    .pagination,
    .page-actions,
    .toolbar,
    .filters,
    .table-actions {
        display: none !important;
    }

    .app {
        display: block;
    }

    .main,
    .content {
        display: block;
        padding: 0;
        margin: 0;
    }

    .card {
        border: 1px solid #999;
        border-radius: 0;
        box-shadow: none;
        break-inside: avoid;
        margin-block-end: 8pt;
    }

    .card-header,
    .card-footer {
        background: none;
        border-color: #999;
    }

    /* The accent rail beside a card title is a screen device: on paper it is a
       blue tick with nothing to signal. */
    .card-title::before {
        display: none;
    }

    /* Printed tables need visible rules on every cell — screen styling drops
       vertical gridlines, but on paper they help the eye track a wide row. */
    .table-wrap {
        overflow: visible;
        border: 0;
        border-radius: 0;
    }

    .table {
        width: 100%;
        border-collapse: collapse;
        page-break-inside: auto;
    }

    .table th,
    .table td {
        border: 1px solid #999 !important;
        padding: 3pt 4pt !important;
        height: auto;
        background: none !important;
        color: #000 !important;
    }

    .table thead th {
        position: static;
        background: #eee !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .table thead {
        display: table-header-group;   /* repeat headers across pages */
    }

    .table tbody tr {
        page-break-inside: avoid;
    }

    .badge {
        border: 1px solid #666;
        background: none !important;
        color: #000 !important;
        text-decoration: none;
    }

    .alert {
        border: 1px solid #999;
        background: none;
        color: #000;
    }

    a {
        color: #000;
        text-decoration: none;
    }

    .totals {
        border: 1px solid #999;
        background: none;
        break-inside: avoid;
    }

    .item-search-results,
    .spinner {
        display: none !important;
    }
}

/* Utility: a figure that is wrong reads red in a table cell. */
.text-danger { color: var(--c-danger); }

/* ==========================================================================
   Module boxes -- the menu as tiles on the dashboard.
   One box per navigation group, its screens inside. The hue comes in as a
   custom property from the template, so the palette lives in one place and a
   box keeps its colour wherever it sits in the grid.
   ========================================================================== */
.module-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
    gap: 0.9rem;
    margin-bottom: 1.25rem;
}
.module-box {
    display: flex;
    flex-direction: column;
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: 8px;
    border-top: 4px solid hsl(var(--module-hue, 212) 55% 48%);
    overflow: hidden;
    transition: box-shadow 0.15s ease, border-color 0.15s ease;
}
.module-box:hover {
    border-color: hsl(var(--module-hue, 212) 45% 60%);
    box-shadow: 0 4px 18px rgba(20, 26, 34, 0.08);
}
.module-box-head {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.8rem 0.9rem 0.6rem;
    color: var(--c-text);
    text-decoration: none;
}
.module-box-head:hover .module-title { color: hsl(var(--module-hue, 212) 55% 40%); }
.module-mark {
    flex: none;
    width: 34px; height: 34px;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 8px;
    background: hsl(var(--module-hue, 212) 70% 94%);
    color: hsl(var(--module-hue, 212) 55% 38%);
    font-weight: 700; font-size: 1rem;
}
.module-title { flex: 1; font-weight: 600; font-size: 0.95rem; letter-spacing: 0.01em; }
.module-count {
    font-size: 0.7rem; color: var(--c-text-3);
    background: var(--c-surface-3); border-radius: 999px; padding: 0.1rem 0.5rem;
}
.module-links {
    display: flex; flex-wrap: wrap; gap: 0.35rem;
    padding: 0 0.9rem 0.9rem;
}
.module-link {
    display: inline-block;
    padding: 0.32rem 0.65rem;
    border: 1px solid var(--c-border);
    border-radius: 6px;
    background: var(--c-surface-2);
    color: var(--c-text-2);
    font-size: 0.82rem;
    text-decoration: none;
    line-height: 1.2;
}
.module-link:hover {
    background: hsl(var(--module-hue, 212) 70% 95%);
    border-color: hsl(var(--module-hue, 212) 45% 70%);
    color: hsl(var(--module-hue, 212) 55% 34%);
}
@media (prefers-color-scheme: dark) {
    .module-mark { background: hsl(var(--module-hue, 212) 40% 22%); color: hsl(var(--module-hue, 212) 70% 78%); }
    .module-link:hover { background: hsl(var(--module-hue, 212) 35% 22%); color: hsl(var(--module-hue, 212) 70% 82%); }
}
@media print { .module-grid { display: none; } }

/* ==========================================================================
   The trade cycle -- the home screen's sequence from purchase to collection.
   One track per channel; phases run as bands, steps as numbered tiles that
   wrap. The colour of a band says which part of the business it is.
   ========================================================================== */
.flow {
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: 8px;
    padding: 1rem 1.1rem 0.6rem;
    margin-bottom: 1.25rem;
}
.flow-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.8rem; }
.flow-title { margin: 0 0 0.15rem; font-size: 1.05rem; font-weight: 600; }
.flow-switch { display: inline-flex; border: 1px solid var(--c-border-strong); border-radius: 6px; overflow: hidden; }
.flow-switch-btn {
    border: 0; background: var(--c-surface-2); color: var(--c-text-2);
    padding: 0.45rem 0.9rem; font: inherit; font-weight: 600; cursor: pointer;
}
.flow-switch-btn + .flow-switch-btn { border-inline-start: 1px solid var(--c-border-strong); }
.flow-switch-btn.is-active { background: var(--c-accent); color: #fff; }

.flow-phase {
    --phase: 212;
    display: grid;
    grid-template-columns: 8.5rem 1fr;
    gap: 0.6rem;
    align-items: start;
    padding: 0.55rem 0;
    border-top: 1px solid var(--c-border-soft);
}
.flow-phase:first-child { border-top: 0; }
.flow-phase-purchase { --phase: 160; }
.flow-phase-stock    { --phase: 28; }
.flow-phase-sales    { --phase: 275; }
.flow-phase-money    { --phase: 340; }
.flow-phase-setup    { --phase: 212; }
.flow-phase-label {
    font-size: 0.72rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
    color: hsl(var(--phase) 50% 40%);
    padding-top: 0.6rem;
    border-inline-start: 3px solid hsl(var(--phase) 55% 55%);
    padding-inline-start: 0.6rem;
}
.flow-steps { display: flex; flex-wrap: wrap; gap: 0.45rem; }

.flow-step {
    position: relative;
    display: inline-flex; align-items: center; gap: 0.55rem;
    min-width: 12rem; max-width: 16rem;
    padding: 0.5rem 0.7rem 0.5rem 0.55rem;
    border: 1px solid var(--c-border); border-radius: 7px;
    background: var(--c-surface);
    color: var(--c-text); text-decoration: none;
}
a.flow-step:hover { border-color: hsl(var(--phase) 45% 60%); background: hsl(var(--phase) 70% 96%); }
.flow-step-no {
    flex: none; width: 26px; height: 26px; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    background: hsl(var(--phase) 55% 48%); color: #fff; font-weight: 700; font-size: 0.8rem;
}
.flow-step-body { display: flex; flex-direction: column; min-width: 0; }
.flow-step-name { font-weight: 600; font-size: 0.88rem; line-height: 1.2; }
.flow-step-hint { font-size: 0.72rem; color: var(--c-text-3); line-height: 1.25; }
.flow-step-count {
    margin-inline-start: auto; flex: none;
    background: var(--c-warning-weak); color: var(--c-warning);
    border-radius: 999px; padding: 0.05rem 0.5rem; font-size: 0.75rem; font-weight: 700;
}
.flow-step-tag {
    margin-inline-start: auto; flex: none;
    font-size: 0.68rem; color: var(--c-text-3); border: 1px dashed var(--c-border-strong);
    border-radius: 999px; padding: 0.05rem 0.45rem;
}
.flow-step.is-unbuilt, .flow-step.is-locked { opacity: 0.55; border-style: dashed; }
.flow-step.is-unbuilt .flow-step-no, .flow-step.is-locked .flow-step-no { background: var(--c-border-strong); }

.flow-all { margin-bottom: 1.25rem; }
.flow-all > summary { cursor: pointer; color: var(--c-text-2); font-weight: 600; padding: 0.4rem 0; list-style: none; }
.flow-all > summary::before { content: "▸ "; color: var(--c-text-3); }
.flow-all[open] > summary::before { content: "▾ "; }
.flow-all[open] > summary { margin-bottom: 0.6rem; }

@media (max-width: 760px) {
    .flow-phase { grid-template-columns: 1fr; }
    .flow-step { min-width: 0; width: 100%; max-width: none; }
}
@media print { .flow, .flow-all { display: none; } }

/* A stored logo or stamp, shown on the profile as it will print. */
.company-preview {
    margin-top: 0.4rem; padding: 0.5rem;
    border: 1px dashed var(--c-border-strong); border-radius: 6px;
    background: #fff; display: inline-block;
}
.company-preview img { max-height: 80px; max-width: 240px; display: block; }

/* ==========================================================================
   The guide -- the window after a save, while staff are learning.
   ========================================================================== */
.guide {
    position: fixed; inset: 0; z-index: 70;
    background: rgba(20, 26, 34, 0.5);
    display: flex; align-items: center; justify-content: center; padding: 1rem;
}
.guide-card {
    width: min(620px, 100%); max-height: 92vh; overflow: auto;
    background: var(--c-surface); color: var(--c-text);
    border-radius: 10px; box-shadow: 0 24px 70px rgba(0, 0, 0, 0.35);
    border-top: 5px solid var(--c-success);
}
.guide-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; padding: 1rem 1.2rem 0.4rem; }
.guide-eyebrow { font-size: 0.7rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--c-success); }
.guide-title { margin: 0.15rem 0 0; font-size: 1.2rem; font-weight: 650; }
.guide-close { background: none; border: 0; font-size: 1.6rem; line-height: 1; color: var(--c-text-3); cursor: pointer; }
.guide-body { padding: 0.4rem 1.2rem 1rem; display: flex; flex-direction: column; gap: 0.9rem; }
.guide-message { margin: 0; font-size: 1rem; line-height: 1.5; }
.guide-next {
    display: flex; flex-wrap: wrap; gap: 0.35rem 0.6rem; align-items: baseline;
    padding: 0.65rem 0.8rem; border-radius: 7px;
    background: var(--c-info-weak); color: var(--c-info); border: 1px solid var(--c-info-line);
}
.guide-next.is-waiting { background: var(--c-warning-weak); color: var(--c-warning); border-color: var(--c-warning-line); }
.guide-next-label { font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; }
.guide-options { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.guide-option-form { display: inline; }
.guide-option { min-height: 42px; }
.guide-foot {
    display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
    padding: 0.7rem 1.2rem; border-top: 1px solid var(--c-border); background: var(--c-surface-2);
    font-size: 0.85rem; color: var(--c-text-2);
}
.guide-foot kbd { font-family: inherit; font-size: 0.7rem; border: 1px solid currentColor; border-radius: 3px; padding: 0 0.3rem; margin-inline-start: 0.3rem; opacity: 0.7; }
@media print { .guide { display: none; } }

/* The hidden attribute wins over any display a class sets, so a script can
   hide a field or a row with el.hidden = true and nothing fights it. */
[hidden] {
    display: none !important;
}

/* Stat tiles followed by a notice need air between them. */
.grid + .alert {
    margin-block-start: var(--sp-3);
}
