:root {
  /* Brand colours sampled from Logo.jpg. Too light for text or for white text on them (about 2:1),
     so they are decoration only. Text and buttons use the darker shades below. */
  --brand-blue: #32c2f4;
  --brand-pink: #f99abd;
  --brand-gradient: linear-gradient(90deg, var(--brand-pink), var(--brand-blue));

  --primary: #0a6c96;        /* 5.84:1 with white */
  --primary-dark: #085577;   /* 8.15:1 with white */
  --primary-soft: #e6f6fd;
  --accent: #a62a5f;         /* 6.74:1 on white; sparing use, never a status colour */

  --bg: #f4f8fb;
  --surface: #ffffff;
  --surface-alt: #f1f6f9;
  --text: #102a3a;
  --muted: #4e6573;
  --border: #d8e4ec;
  --border-input: #74909f;   /* 3.37:1 on white */

  /* Status colours: deliberately away from the brand sky blue and pink. */
  --danger: #b42318;
  --danger-soft: #fdecea;
  --danger-border: #efc2bd;
  --warn: #8a5300;
  --warn-soft: #fff3d6;
  --ok: #146c2e;
  --ok-soft: #e4f5e9;
  --ship: #3538cd;
  --ship-soft: #ecedfd;
  --hold: #475467;
  --hold-soft: #eaecf0;

  --radius: 12px;
  --shadow: 0 1px 2px rgb(16 42 58 / 6%), 0 4px 14px rgb(16 42 58 / 5%);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); font: 400 15px/1.65 Cairo, "Segoe UI", Tahoma, sans-serif; }
h1 { font-size: 1.4rem; margin: 0; }
.page-head h1 { position: relative; padding-bottom: .3rem; }
.page-head h1::after { content: ""; position: absolute; inset-inline-start: 0; bottom: 0; width: 44px; height: 3px; border-radius: 3px; background: var(--brand-gradient); }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
a { color: var(--primary); }
small, .muted { color: var(--muted); }
bdi { unicode-bidi: isolate; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.skip { position: absolute; top: -100px; inset-inline-start: 8px; background: var(--surface); padding: .5rem 1rem; z-index: 5; }
.skip:focus { top: 8px; }
:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }
.hero :focus-visible { outline-color: #fff; }
#main:focus { outline: none; }
.loading, .empty { text-align: center; color: var(--muted); padding: 1.5rem; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: var(--brand-gradient) top / 100% 4px no-repeat, var(--surface); border-inline-end: 1px solid var(--border); padding: 1.1rem .75rem 1rem; position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; gap: 1rem; }
.side-panel { display: flex; flex-direction: column; gap: 1rem; flex: 1; }
.brand { display: flex; justify-content: center; align-items: center; }
.brand-logo { display: block; max-width: 100%; height: auto; }
.sidebar .brand-logo { width: 168px; }
.menu { display: flex; flex-direction: column; gap: 2px; }
.menu a { color: var(--text); text-decoration: none; padding: .55rem .75rem; border-radius: 8px; border-inline-start: 3px solid transparent; font-weight: 600; }
.menu a:hover { background: var(--surface-alt); }
.menu a[aria-current="page"] { background: var(--primary-soft); color: var(--primary-dark); border-inline-start-color: var(--accent); font-weight: 800; }
.who { margin-top: auto; display: flex; flex-direction: column; gap: .25rem; padding: .75rem .4rem 0; border-top: 1px solid var(--border); }
.sidebar .menu-toggle { display: none; }
main { padding: 1.5rem; min-width: 0; }
.view { max-width: 1280px; margin-inline: auto; display: flex; flex-direction: column; gap: 1rem; }
.view p { margin: .25rem 0; }
.page-head, .card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .75rem; }
.card-head h2 { margin: 0; }
.card-head { margin-bottom: .75rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem; min-width: 0; }
.two-cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); gap: 1rem; align-items: start; }
.actions, .row-actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }

/* ---------- controls ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .35rem; min-height: 40px; padding: .35rem .95rem; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font: inherit; font-weight: 600; text-decoration: none; cursor: pointer; }
.btn:hover { border-color: var(--primary); }
.btn:disabled:hover { border-color: var(--border); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn.danger { color: var(--danger); border-color: var(--danger-border); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.small { min-height: 34px; padding: .15rem .7rem; font-size: .87rem; }
.btn.block { width: 100%; }
.btn.link { border: 0; background: none; color: var(--primary); text-decoration: underline; }
.who .btn { align-self: flex-start; }
input, select, textarea { width: 100%; min-height: 40px; padding: .35rem .65rem; border: 1px solid var(--border-input); border-radius: 8px; background: var(--surface); color: var(--text); font: inherit; }
input[type="checkbox"] { width: 18px; min-height: 18px; height: 18px; accent-color: var(--primary); flex: none; }
.field { display: flex; flex-direction: column; gap: .2rem; min-width: 0; }
.field label, .field > span { font-weight: 600; font-size: .87rem; }
.check { display: flex; align-items: center; gap: .5rem; cursor: pointer; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 230px), 1fr)); gap: .85rem; }
.wide { grid-column: 1 / -1; }
.filters { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 160px), 1fr)); gap: .75rem; align-items: end; }
.filters .grow { grid-column: span 2; }
.month-picker { display: flex; align-items: center; gap: .5rem; font-weight: 600; }
.month-picker select { width: auto; }
.form-error { background: var(--danger-soft); color: var(--danger); border-radius: 8px; padding: .5rem .75rem; font-weight: 600; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: start; font-size: .82rem; color: var(--muted); background: var(--surface-alt); border-bottom: 2px solid var(--border); padding: .45rem .5rem; white-space: nowrap; }
.table td { padding: .6rem .5rem; border-bottom: 1px solid var(--border); vertical-align: top; overflow-wrap: anywhere; }
.table tbody tr:hover { background: #f7fbfe; }
.table bdi, .money, .order-link { white-space: nowrap; overflow-wrap: normal; }
.table .row-actions { flex-wrap: nowrap; }
.table .status-select { min-width: 128px; }
.line-item { display: block; }
.order-link { font-weight: 800; display: inline-block; min-width: 3.2rem; }
.pager { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: .75rem; margin-top: 1rem; }
.total { text-align: end; margin-top: .75rem !important; }

/* ---------- badges ---------- */
.badge { display: inline-block; padding: 0 .6rem; border-radius: 99px; font-size: .78rem; font-weight: 700; background: var(--surface-alt); color: var(--text); white-space: nowrap; }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.on, .badge.act-create, .badge.act-restore { background: var(--ok-soft); color: var(--ok); }
.badge.off, .badge.act-delete { background: var(--danger-soft); color: var(--danger); }
.st-delivered { --st: var(--ok); --st-bg: var(--ok-soft); }
.st-shipping { --st: var(--ship); --st-bg: var(--ship-soft); }
.st-cancelled { --st: var(--danger); --st-bg: var(--danger-soft); }
.st-no_answer { --st: var(--warn); --st-bg: var(--warn-soft); }
.st-postponed { --st: var(--hold); --st-bg: var(--hold-soft); }
.badge[class*="st-"], .status-select { background-color: var(--st-bg); color: var(--st); font-weight: 700; }

/* ---------- dashboard ---------- */
.hero { background: linear-gradient(135deg, #093f5c, var(--primary) 55%, #7a3566); color: #fff; border-radius: var(--radius); padding: 1.4rem; display: flex; flex-direction: column; align-items: flex-start; gap: .3rem; }
.hero.loss { background: linear-gradient(135deg, #4a130e, #9b1c12); }
.hero strong { font-size: clamp(1.7rem, 6vw, 2.6rem); line-height: 1.3; }
.hero small { color: #e3f1f8; }
.hero .btn:hover { border-color: var(--brand-blue); }
.hero .btn { margin-top: .5rem; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 210px), 1fr)); gap: 1rem; }
.stat { display: flex; flex-direction: column; }
.stat > span { color: var(--muted); font-size: .87rem; }
.stat strong { font-size: 1.4rem; }
.big { font-size: 1.35rem; font-weight: 800; }
.summary, .details { margin: 0 0 .75rem; }
.summary > div { display: flex; justify-content: space-between; gap: 1rem; padding: .35rem 0; border-bottom: 1px dashed var(--border); }
.summary dt { color: var(--muted); }
.summary dd { margin: 0; font-weight: 700; text-align: end; }
.pos { color: var(--ok); }
.neg { color: var(--danger); }
.details { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr)); gap: .85rem; margin: 0; }
.details dt { color: var(--muted); font-size: .82rem; }
.details dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.notes { white-space: pre-wrap; overflow-wrap: anywhere; }
progress { display: block; width: 100%; height: 9px; border: 0; border-radius: 9px; overflow: hidden; background: #e6eef3; appearance: none; }
progress::-webkit-progress-bar { background: #e6eef3; }
progress::-webkit-progress-value { background: var(--st, var(--primary)); border-radius: 9px; }
progress::-moz-progress-bar { background: var(--st, var(--primary)); }
.status-bars { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .6rem; }
.status-bars li { display: grid; grid-template-columns: 6.5rem 1fr 3rem; align-items: center; gap: .75rem; }

/* ---------- alerts ---------- */
.alert { background: var(--warn-soft); color: var(--warn); border: 1px solid #f0d28a; border-radius: var(--radius); padding: .75rem 1rem; }
.alert ul { margin: .25rem 0; padding-inline-start: 1.2rem; }
.alert a { color: inherit; font-weight: 700; }
.alert.blocking { background: var(--danger-soft); color: var(--danger); border-color: var(--danger-border); }

/* ---------- report ---------- */
.calc { width: 100%; border-collapse: collapse; }
.calc th { text-align: start; font-weight: 600; }
.calc th > small { display: block; font-weight: 400; }
.calc th, .calc td { padding: .4rem .5rem; border-bottom: 1px solid var(--border); }
.calc td { text-align: end; font-weight: 700; }
.calc tr.sub { background: var(--surface-alt); }
.calc tr.key { background: var(--primary-soft); }
.letterhead { display: none; }

/* ---------- order form & history ---------- */
.order-form { display: flex; flex-direction: column; gap: 1rem; }
.line { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 1fr) minmax(0, 1.4fr) auto; gap: .6rem; align-items: end; border: 0; border-bottom: 1px solid var(--border); padding: 0 0 .75rem; margin: 0 0 .75rem; min-width: 0; }
.total-box { margin-top: 1rem; background: var(--surface-alt); border-radius: 8px; padding: .85rem; display: flex; flex-direction: column; gap: .5rem; }
.total-box .field { max-width: 260px; }
.history { list-style: none; margin: 0; padding: 0; }
.history.card { padding: 0 1.1rem; }
.history li { padding: .8rem 0; border-bottom: 1px solid var(--border); overflow-wrap: anywhere; }
.history li:last-child { border-bottom: 0; }
.changes { margin: .35rem 0; padding-inline-start: 1.2rem; font-size: .9rem; overflow-wrap: anywhere; }
.changes del { color: var(--danger); }
.changes ins { color: var(--ok); text-decoration: none; font-weight: 700; }
summary { cursor: pointer; color: var(--primary); font-size: .87rem; }

/* ---------- login, modal, toast ---------- */
.login { min-height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 1rem; padding: 1rem;
  background: radial-gradient(60% 55% at 100% 0%, rgb(50 194 244 / 16%), transparent 70%), radial-gradient(60% 55% at 0% 100%, rgb(249 154 189 / 18%), transparent 70%); }
.login .card { width: min(100%, 420px); display: flex; flex-direction: column; gap: .75rem; }
.login form.card { background: var(--brand-gradient) top / 100% 4px no-repeat, var(--surface); padding-top: 1.35rem; }
.login .brand-logo { width: 190px; }
.login h1 { text-align: center; }
.demo { border-style: dashed !important; }
.demo p, .demo h2 { margin: 0; }
.demo-account { flex-direction: column; gap: 0; }
.modal { width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 24px); border: 0; border-radius: var(--radius); padding: 1.25rem; color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / 30%); }
.modal::backdrop { background: rgb(9 32 48 / 55%); }
.modal .actions { margin-top: 1.1rem; }
#toasts { position: fixed; bottom: 16px; inset-inline: 16px; display: flex; flex-direction: column; align-items: center; gap: .5rem; pointer-events: none; z-index: 10; }
.toast { background: var(--text); color: #fff; padding: .6rem 1.1rem; border-radius: 8px; box-shadow: var(--shadow); max-width: 100%; }
.toast.error { background: var(--danger); }

/* ---------- installable app ---------- */
.install-bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: .5rem .75rem; background: var(--primary-soft); border-bottom: 1px solid var(--border);
  padding: calc(.5rem + env(safe-area-inset-top)) max(.75rem, env(safe-area-inset-right)) .5rem max(.75rem, env(safe-area-inset-left)); }
.install-bar p { margin: 0; flex: 1 1 15rem; max-width: 46rem; font-size: .9rem; }
.offline-screen { width: 100%; max-width: 100%; height: 100%; max-height: 100%; margin: 0; border: 0; padding: 1.5rem; background: var(--surface); color: var(--text); text-align: center; }
.offline-screen[open] { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .5rem; }
.offline-screen p { margin: 0; }
.offline-screen .btn { min-width: 200px; min-height: 48px; }

/* viewport-fit=cover lets the page reach the screen edges, so keep content clear of notches and the home bar.
   All of these are 0 on ordinary screens. */
body { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
#toasts { bottom: calc(16px + env(safe-area-inset-bottom)); }
.login { min-height: calc(100vh - env(safe-area-inset-bottom)); min-height: calc(100dvh - env(safe-area-inset-bottom)); padding-top: calc(1rem + env(safe-area-inset-top)); padding-bottom: calc(1rem + env(safe-area-inset-bottom)); }
main:not(.login) { padding-bottom: calc(1.5rem + env(safe-area-inset-bottom)); }
a, button, select, input, label, summary { touch-action: manipulation; }

/* ---------- tablet & phone ---------- */
@media (max-width: 860px) {
  .shell { display: block; }
  .sidebar { height: auto; max-height: 100vh; flex-direction: row; flex-wrap: wrap; align-items: center; justify-content: space-between; border-inline-end: 0; border-bottom: 1px solid var(--border); padding: .7rem .75rem .5rem; z-index: 4; gap: .5rem; }
  .sidebar .brand-logo { width: 116px; }
  .sidebar { padding-top: calc(.7rem + env(safe-area-inset-top)); max-height: 100dvh; }
  .sidebar .menu-toggle { display: inline-flex; }
  .side-panel { display: none; flex-basis: 100%; }
  .sidebar.open .side-panel { display: flex; }
  main { padding: .85rem; }
  main:not(.login) { padding-bottom: calc(1.25rem + env(safe-area-inset-bottom)); }
}
/* Below this width wide tables become stacked cards, so the page never scrolls sideways. */
@media screen and (max-width: 1100px) {
  .table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .table, .table tbody, .table tr { display: block; width: 100%; }
  .table tr { border: 1px solid var(--border); border-radius: 10px; margin-bottom: .75rem; padding: .35rem .75rem; }
  .table td { display: grid; grid-template-columns: minmax(5.5rem, 34%) minmax(0, 1fr); gap: .6rem; padding: .35rem 0; border-bottom: 1px dashed var(--border); }
  .table td:last-child { border-bottom: 0; }
  .table td::before { content: attr(data-label); color: var(--muted); font-size: .82rem; font-weight: 700; }
  .table .row-actions { flex-wrap: wrap; }
}
@media (max-width: 560px) {
  .filters { grid-template-columns: 1fr 1fr; }
  .filters .grow { grid-column: 1 / -1; }
  .line { grid-template-columns: 1fr 1fr; }
  .line > :first-of-type { grid-column: 1 / -1; }
  .line .btn { justify-self: start; }
  .card { padding: .85rem; }
}
/* Touch screens and phones: 16px controls (smaller text makes iOS zoom in on focus) and 44px tap targets. */
@media screen and (max-width: 860px), screen and (pointer: coarse) {
  input, select, textarea { font-size: 16px; min-height: 46px; }
  /* iOS gives date and search fields their own sizing, which can overflow a narrow column. */
  input[type="date"], input[type="search"] { -webkit-appearance: none; appearance: none; min-width: 0; }
  input[type="date"]::-webkit-date-and-time-value { text-align: start; }
  input[type="checkbox"] { width: 22px; height: 22px; min-height: 22px; }
  .check { min-height: 44px; }
  .btn, .btn.small { min-height: 44px; }
  .menu a { padding-block: .7rem; }
  .table .order-link { display: inline-flex; align-items: center; min-height: 44px; }
  summary { padding-block: .6rem; }
}
/* Phone: the save button stays within reach while filling the order form. */
@media screen and (max-width: 560px) {
  .order-form > .actions { position: sticky; bottom: 0; z-index: 3; flex-wrap: nowrap; margin-inline: -.85rem; padding: .6rem .85rem calc(.6rem + env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--border); }
  .order-form > .actions > * { flex: 1; }
  .order-form input, .order-form select, .order-form textarea { scroll-margin-bottom: 90px; }
  /* The sticky bar must be able to reach the bottom edge. */
  main:has(.order-form) { padding-bottom: 0; }
}

@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; transition: none !important; } }

/* ---------- print (monthly report) ---------- */
@media print {
  @page { margin: 12mm; }
  body { background: #fff; font-size: 11pt; }
  .sidebar, .skip, .no-print, #toasts { display: none !important; }
  .shell { display: block; }
  main, main:not(.login) { padding: 0; }
  .card { box-shadow: none; }
  .calc tr, .table tr, .alert { break-inside: avoid; }
  .calc tr.sub, .calc tr.key, .letterhead, .page-head h1::after { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
  .letterhead { display: flex; direction: ltr; justify-content: flex-end; padding-bottom: 3mm; background: var(--brand-gradient) bottom / 100% 1mm no-repeat; }
  .letterhead img { width: 48mm; height: auto; }
  .alert a { display: none; }
}
