/* ==========================================================================
   Megal Deliveries — shared design system
   Used by frontend.html (staff/admin) and rep.html (Field Sales). Flutter
   and PDFKit can't load this file directly (different renderers) but use
   the SAME hex values -- see megal_customer_app/lib/theme/brand.dart and
   lib/print.js's NAVY/RUST/GREEN/WARNING constants.

   Sections: tokens -> base -> buttons -> badges -> cards/kpi -> forms ->
   tables -> tabs -> modals/sheets -> toast -> skeleton -> empty/error ->
   chat -> dropzone -> utilities
   ========================================================================== */

:root {
  /* ---- Brand palette (single source of truth) ---- */
  --navy: #111d3a;
  --navy-2: #1a2748;
  --rust: #a73d49;
  --rust-dark: #8d303b;
  --green: #2f8f67;
  --green-tint: #e4f4ec;
  --warning: #b8722c;
  --warning-tint: #f6ead9;
  --cream: #f8f7f7;
  --paper: #ffffff;
  --ink: #1c2233;
  --ink-soft: #6f7380;
  --line: #efe8e9;
  --line-strong: #e0d7d9;
  --rose: #fff5f5;
  --rust-tint: #fbeceb;

  /* ---- Scale ---- */
  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --shadow-card: 0 4px 18px rgba(44, 32, 43, 0.05);
  --shadow-modal: 0 20px 60px rgba(17, 29, 58, 0.28);
  --font-stack: 'Inter', system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --cream: #0e1424;
    --paper: #161d30;
    --ink: #e7e9f0;
    --ink-soft: #8890a6;
    --line: #242c42;
    --line-strong: #323b57;
    --rose: #241a22;
    --green-tint: #123625;
    --warning-tint: #3a2a15;
    --rust-tint: #33161a;
  }
}

/* ---- Base ---- */
.mds-page { background: var(--cream); color: var(--ink); font-family: var(--font-stack); }
.mds-hidden { display: none !important; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border-radius: var(--radius-sm); border: none;
  font-family: var(--font-stack); font-weight: 700; font-size: 0.88rem;
  min-height: 40px; cursor: pointer; transition: background 0.15s, transform 0.05s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn.primary { background: var(--rust); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--rust-dark); }
.btn.green { background: var(--green); color: #fff; }
.btn.navy { background: var(--navy); color: #fff; }
.btn.outline { background: #fff; color: var(--navy); border: 1.5px solid var(--line-strong); }
.btn.outline:hover:not(:disabled) { border-color: var(--rust); color: var(--rust); }
.btn.danger { background: #fff; color: var(--rust-dark); border: 1.5px solid var(--rust-tint); }
.btn.danger:hover:not(:disabled) { background: var(--rust-tint); }
.btn.block { width: 100%; }
.btn.sm { padding: 7px 12px; font-size: 0.78rem; min-height: 32px; }
.btn.icon-only { padding: 8px; min-width: 36px; }
.btn .spinner { width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.4); border-top-color: #fff; animation: mds-spin 0.7s linear infinite; }
.btn.outline .spinner { border-color: var(--line-strong); border-top-color: var(--navy); }

/* ---- Badges (status) ---- */
.badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px;
  border-radius: 20px; font-size: 0.7rem; font-weight: 700; letter-spacing: 0.01em;
  background: var(--line); color: var(--ink-soft); white-space: nowrap;
}
.badge.green { background: var(--green-tint); color: var(--green); }
.badge.rust { background: var(--rust-tint); color: var(--rust-dark); }
.badge.navy { background: #e8eaf3; color: var(--navy); }
.badge.warning { background: var(--warning-tint); color: var(--warning); }
.badge.solid-navy { background: var(--navy); color: #fff; }
.badge.solid-green { background: var(--green); color: #fff; }
.badge.solid-rust { background: var(--rust); color: #fff; }

/* ---- Cards / KPI ---- */
.mds-card {
  background: var(--paper); border-radius: var(--radius-md); padding: 16px;
  box-shadow: var(--shadow-card); border: 1px solid transparent;
}
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kpi-card {
  background: var(--paper); border-radius: var(--radius-md); padding: 14px 16px;
  box-shadow: var(--shadow-card); display: flex; flex-direction: column; gap: 4px;
}
.kpi-card .kpi-icon { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: var(--rose); color: var(--rust); margin-bottom: 4px; }
.kpi-card .kpi-value { font-size: 1.35rem; font-weight: 800; color: var(--navy); line-height: 1.15; }
.kpi-card .kpi-label { font-size: 0.72rem; font-weight: 600; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.03em; }
.kpi-card .kpi-delta { font-size: 0.72rem; font-weight: 700; }
.kpi-card .kpi-delta.up { color: var(--green); }
.kpi-card .kpi-delta.down { color: var(--rust-dark); }

.progress-track { background: var(--line); border-radius: 20px; height: 10px; overflow: hidden; }
.progress-fill { background: var(--green); height: 100%; border-radius: 20px; transition: width 0.3s; }
.progress-fill.warning { background: var(--warning); }
.progress-fill.rust { background: var(--rust); }

/* ---- Forms ---- */
.mds-field { margin-bottom: 14px; }
.mds-field label {
  display: flex; align-items: center; gap: 4px; font-size: 0.72rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-soft); margin-bottom: 5px;
}
.mds-field label .required { color: var(--rust); }
.mds-field label .hint { text-transform: none; font-weight: 500; letter-spacing: 0; color: var(--ink-soft); font-size: 0.72rem; }
.mds-field input, .mds-field select, .mds-field textarea {
  width: 100%; font-family: var(--font-stack); font-size: 0.92rem; padding: 10px 12px;
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm); background: #fff; color: var(--ink);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.mds-field input:focus, .mds-field select:focus, .mds-field textarea:focus {
  outline: none; border-color: var(--rust); box-shadow: 0 0 0 3px rgba(167, 61, 73, 0.12);
}
.mds-field.has-error input, .mds-field.has-error select, .mds-field.has-error textarea { border-color: var(--rust); }
.mds-field .field-error { color: var(--rust-dark); font-size: 0.78rem; margin-top: 4px; min-height: 1em; }
.mds-field input:disabled, .mds-field select:disabled, .mds-field textarea:disabled { background: var(--line); color: var(--ink-soft); cursor: not-allowed; }

.search-field { position: relative; }
.search-field input { padding-left: 34px; }
.search-field::before {
  content: '⌕'; position: absolute; left: 11px; top: 50%; transform: translateY(-50%);
  color: var(--ink-soft); font-size: 1rem; pointer-events: none;
}
.filter-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.filter-row select, .filter-row input[type="date"], .filter-row input[type="month"] { width: auto; min-width: 120px; }

/* ---- Tables ---- */
.mds-table-wrap { overflow-x: auto; }
.mds-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.mds-table thead th {
  text-align: left; font-size: 0.68rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.04em; color: var(--ink-soft); padding: 10px 12px; border-bottom: 2px solid var(--line);
  position: sticky; top: 0; background: var(--paper); white-space: nowrap;
}
.mds-table thead th.num, .mds-table td.num { text-align: right; }
.mds-table tbody td { padding: 11px 12px; border-bottom: 1px solid var(--line); color: var(--ink); }
.mds-table tbody tr:last-child td { border-bottom: none; }
.mds-table tbody tr:hover { background: var(--cream); }
.mds-table tfoot td { padding: 11px 12px; font-weight: 800; color: var(--navy); border-top: 2px solid var(--line); }
.mds-table .row-actions { display: flex; gap: 6px; justify-content: flex-end; }

/* Mobile: collapse wide tables into stacked cards instead of horizontal scroll,
   driven by data-label on each <td> (progressive enhancement -- tables without
   data-label just fall back to the horizontal-scroll wrapper above). */
@media (max-width: 680px) {
  .mds-table.mds-table-collapse thead { display: none; }
  .mds-table.mds-table-collapse, .mds-table.mds-table-collapse tbody, .mds-table.mds-table-collapse tr, .mds-table.mds-table-collapse td { display: block; width: 100%; }
  .mds-table.mds-table-collapse tr { background: var(--paper); border-radius: var(--radius-md); box-shadow: var(--shadow-card); margin-bottom: 10px; padding: 6px 4px; }
  .mds-table.mds-table-collapse td { border-bottom: none; display: flex; justify-content: space-between; gap: 12px; padding: 7px 12px; }
  .mds-table.mds-table-collapse td::before { content: attr(data-label); font-size: 0.68rem; font-weight: 700; text-transform: uppercase; color: var(--ink-soft); }
}

/* ---- Tabs ---- */
.mds-tabs { display: flex; gap: 6px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.mds-tabs button {
  flex: 0 0 auto; padding: 8px 14px; border-radius: 20px; border: 1px solid var(--line-strong);
  background: #fff; font-family: var(--font-stack); font-size: 0.8rem; font-weight: 700; color: var(--ink-soft); cursor: pointer;
}
.mds-tabs button.active { background: var(--navy); color: #fff; border-color: var(--navy); }

/* ---- Modals / bottom sheets ---- */
.mds-overlay {
  position: fixed; inset: 0; background: rgba(17, 29, 58, 0.5); z-index: 1000;
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.mds-modal {
  background: #fff; border-radius: var(--radius-lg); width: 100%; max-width: 480px;
  max-height: 88vh; overflow-y: auto; box-shadow: var(--shadow-modal);
}
.mds-modal-header { padding: 18px 20px 8px; }
.mds-modal-header h3 { margin: 0 0 2px; color: var(--navy); font-size: 1.05rem; }
.mds-modal-header p { margin: 0; color: var(--ink-soft); font-size: 0.82rem; }
.mds-modal-body { padding: 8px 20px; }
.mds-modal-footer {
  display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px;
  border-top: 1px solid var(--line); position: sticky; bottom: 0; background: #fff;
}
.mds-modal-footer.spread { justify-content: space-between; }
@media (max-width: 640px) {
  .mds-overlay { align-items: flex-end; padding: 0; }
  .mds-modal { max-width: 100%; border-radius: var(--radius-lg) var(--radius-lg) 0 0; max-height: 90vh; }
  .mds-modal::before { content: ''; display: block; width: 36px; height: 4px; background: var(--line-strong); border-radius: 4px; margin: 10px auto 4px; }
}

/* ---- Toast ---- */
.mds-toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--navy); color: #fff; padding: 11px 20px; border-radius: 30px;
  font-size: 0.85rem; font-weight: 600; z-index: 2000; opacity: 0; pointer-events: none;
  transition: opacity 0.25s, transform 0.25s; max-width: 90vw; text-align: center; box-shadow: var(--shadow-modal);
}
.mds-toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
.mds-toast.error { background: var(--rust-dark); }
.mds-toast.success { background: var(--green); }

/* ---- Skeleton loading ---- */
.skeleton {
  background: linear-gradient(90deg, var(--line) 25%, var(--line-strong) 37%, var(--line) 63%);
  background-size: 400% 100%; animation: mds-shimmer 1.4s ease infinite; border-radius: var(--radius-sm);
}
.skeleton.text { height: 0.9em; margin: 4px 0; }
.skeleton.title { height: 1.3em; width: 55%; margin: 4px 0 10px; }
.skeleton.avatar { width: 40px; height: 40px; border-radius: 50%; }
.skeleton.card { height: 90px; border-radius: var(--radius-md); }
.skeleton.row { height: 44px; border-radius: var(--radius-sm); margin-bottom: 8px; }
@keyframes mds-shimmer { 0% { background-position: 100% 0; } 100% { background-position: -100% 0; } }
@keyframes mds-spin { to { transform: rotate(360deg); } }

/* ---- Empty / error states ---- */
.mds-empty, .mds-error {
  text-align: center; padding: 40px 16px; color: var(--ink-soft);
}
.mds-empty .icon, .mds-error .icon { font-size: 2rem; margin-bottom: 10px; opacity: 0.7; }
.mds-empty h4, .mds-error h4 { margin: 0 0 4px; color: var(--navy); font-size: 0.95rem; }
.mds-empty p, .mds-error p { margin: 0; font-size: 0.82rem; }
.mds-error .icon { color: var(--rust); }
.mds-error h4 { color: var(--rust-dark); }
.mds-empty .btn, .mds-error .btn { margin-top: 12px; }

/* ---- Chat ---- */
.chat-shell { display: flex; flex-direction: column; height: 100%; background: var(--cream); }
.chat-header { display: flex; align-items: center; gap: 10px; padding: 12px 16px; background: var(--paper); border-bottom: 1px solid var(--line); }
.chat-header .avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--navy); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 0.9rem; flex-shrink: 0; }
.chat-header .name { font-weight: 700; color: var(--navy); font-size: 0.92rem; }
.chat-header .status { font-size: 0.72rem; color: var(--ink-soft); display: flex; align-items: center; gap: 5px; }
.chat-header .status .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--green); }
.chat-messages { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.chat-bubble-row { display: flex; gap: 8px; max-width: 78%; }
.chat-bubble-row.mine { align-self: flex-end; flex-direction: row-reverse; }
.chat-bubble { padding: 9px 13px; border-radius: 16px; font-size: 0.88rem; line-height: 1.4; box-shadow: var(--shadow-card); }
.chat-bubble-row:not(.mine) .chat-bubble { background: var(--paper); color: var(--ink); border-bottom-left-radius: 4px; }
.chat-bubble-row.mine .chat-bubble { background: var(--rust); color: #fff; border-bottom-right-radius: 4px; }
.chat-bubble img { max-width: 220px; border-radius: 10px; margin-top: 4px; display: block; }
.chat-timestamp { font-size: 0.65rem; color: var(--ink-soft); margin-top: 3px; }
.chat-bubble-row.mine .chat-timestamp { text-align: right; }
.chat-composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px; background: var(--paper); border-top: 1px solid var(--line); position: sticky; bottom: 0; }
.chat-composer textarea { flex: 1; resize: none; max-height: 100px; border: 1px solid var(--line-strong); border-radius: 20px; padding: 9px 14px; font-family: var(--font-stack); font-size: 0.9rem; }
.chat-composer button { flex-shrink: 0; }

/* ---- Photo upload / dropzone ---- */
.dropzone {
  border: 2px dashed var(--line-strong); border-radius: var(--radius-md); padding: 22px;
  text-align: center; color: var(--ink-soft); font-size: 0.85rem; cursor: pointer; transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover, .dropzone.dragover { border-color: var(--rust); background: var(--rose); }
.dropzone .icon { font-size: 1.6rem; margin-bottom: 6px; }
.photo-thumb-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.photo-thumb { position: relative; width: 68px; height: 68px; border-radius: 10px; overflow: hidden; background: var(--line); }
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; }
.photo-thumb .remove { position: absolute; top: 2px; right: 2px; background: rgba(0,0,0,0.55); color: #fff; border: none; border-radius: 50%; width: 18px; height: 18px; font-size: 0.7rem; line-height: 1; cursor: pointer; }

/* ---- Export controls ---- */
.export-row { display: flex; flex-wrap: wrap; gap: 8px; }
.export-row .btn svg, .export-row .btn .ico { width: 15px; height: 15px; }

/* ---- Scanner (QR/barcode) ---- */
.scan-frame { position: relative; border-radius: var(--radius-md); overflow: hidden; background: #000; aspect-ratio: 1/1; max-width: 320px; margin: 0 auto; }
.scan-frame video { width: 100%; height: 100%; object-fit: cover; }
.scan-frame::after {
  content: ''; position: absolute; inset: 14%; border: 2.5px solid rgba(255,255,255,0.85); border-radius: 16px;
  box-shadow: 0 0 0 999px rgba(0,0,0,0.35);
}
.scan-status { text-align: center; font-size: 0.82rem; color: var(--ink-soft); margin-top: 10px; }

/* ---- RTL / Sorani ---- */
html[dir="rtl"] {
  font-family: 'Noto Sans Arabic', 'Inter', 'Noto Naskh Arabic', Tahoma, system-ui, sans-serif;
}
html[dir="rtl"] body {
  font-family: 'Noto Sans Arabic', 'Inter', system-ui, sans-serif;
}

/* Logical padding/margin helpers for common surfaces */
html[dir="rtl"] .search-field input,
html[dir="rtl"] .search-bar input {
  padding-inline-start: 38px;
  padding-inline-end: 12px;
}
html[dir="rtl"] .search-field::before,
html[dir="rtl"] .search-bar::before {
  left: auto;
  right: 11px;
  inset-inline-start: auto;
  inset-inline-end: 11px;
}

/* Tables — start-align text, end-align numeric columns */
html[dir="rtl"] .mds-table thead th,
html[dir="rtl"] .mds-table tbody td,
html[dir="rtl"] .mds-table tfoot td,
html[dir="rtl"] table thead th,
html[dir="rtl"] table tbody td { text-align: start; }
html[dir="rtl"] .mds-table thead th.num,
html[dir="rtl"] .mds-table td.num,
html[dir="rtl"] table th.num,
html[dir="rtl"] table td.num { text-align: end; }

/* Modal / sheet footers — reverse action button order in RTL */
html[dir="rtl"] .mds-modal-footer,
html[dir="rtl"] .sheet-footer {
  flex-direction: row-reverse;
}

/* Sheet drag handles stay visually centered regardless of dir */
html[dir="rtl"] .sheet-handle,
html[dir="rtl"] .sheet::before {
  margin-inline: auto;
  inset-inline: auto;
}

/* Chat — mirror bubble alignment and corner radii */
html[dir="rtl"] .chat-bubble-row.mine { flex-direction: row; align-self: flex-start; }
html[dir="rtl"] .chat-bubble-row:not(.mine) { flex-direction: row-reverse; align-self: flex-end; }
html[dir="rtl"] .chat-bubble-row:not(.mine) .chat-bubble {
  border-bottom-left-radius: 16px;
  border-bottom-right-radius: 4px;
}
html[dir="rtl"] .chat-bubble-row.mine .chat-bubble {
  border-bottom-right-radius: 16px;
  border-bottom-left-radius: 4px;
}
html[dir="rtl"] .chat-bubble-row.mine .chat-timestamp { text-align: start; }
html[dir="rtl"] .chat-composer { flex-direction: row-reverse; }

/* Photo thumb remove control mirrors to the start edge */
html[dir="rtl"] .photo-thumb .remove {
  right: auto;
  left: 2px;
  inset-inline-start: 2px;
  inset-inline-end: auto;
}

/* KPI / list / activity rows */
html[dir="rtl"] .kpi-card .kpi-value,
html[dir="rtl"] .kpi-card .kpi-label,
html[dir="rtl"] .stat-tile .n,
html[dir="rtl"] .stat-tile .l { text-align: start; }
html[dir="rtl"] .activity-row,
html[dir="rtl"] .item-row,
html[dir="rtl"] .list-item .row,
html[dir="rtl"] .more-link { direction: rtl; }

/* Language switcher spacing uses logical margins */
html[dir="rtl"] .i18n-lang-btn { margin-inline: 0 4px; margin-block: 0 4px; }
html[dir="rtl"] .lang-pill { direction: rtl; }

/* Progress fills grow from the start edge in RTL */
html[dir="rtl"] .progress-fill { transform-origin: right center; }

.i18n-lang-btn.active {
  background: var(--rust) !important;
  color: #fff !important;
  border-color: var(--rust) !important;
}
