/* Qarak Helmy Shipyard — mobile-first, RTL-aware (logical properties), light + dark. */
:root {
  --bg: #f3f6f9; --surface: #ffffff; --surface-2: #f7f9fb; --line: #e2e8ef; --text: #13212e; --muted: #5f7181;
  --brand: #0b4f71; --brand-2: #0e6a94; --brand-ink: #ffffff; --accent: #e8a33d;
  --ok: #18794e; --ok-bg: #e6f4ec; --warn: #9a5b00; --warn-bg: #fdf1dc; --bad: #b42318; --bad-bg: #fde8e6; --info: #0b5cad; --info-bg: #e5f0fb;
  --radius: 12px; --shadow: 0 1px 2px rgb(16 32 48 / .06), 0 2px 8px rgb(16 32 48 / .05);
  --font: system-ui, -apple-system, "Segoe UI", "Noto Sans Arabic", Tahoma, Roboto, sans-serif;
  --side: 248px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d151c; --surface: #141f29; --surface-2: #192734; --line: #24364a; --text: #e5edf4; --muted: #93a6b7;
    --brand: #3aa0d0; --brand-2: #5bb6e0; --brand-ink: #06131c;
    --ok: #4cc38a; --ok-bg: #12301f; --warn: #f0b45a; --warn-bg: #33270f; --bad: #ff7a6b; --bad-bg: #3a1714; --info: #6fb0f0; --info-bg: #10263c;
    --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 var(--font); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 1.25rem; } h3 { font-size: 1.05rem; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); } .small { font-size: .82rem; }
.num { text-align: end; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--ok); } .neg { color: var(--bad); }
.strike { text-decoration: line-through; opacity: .6; }
code { font-size: .85em; }

/* Icons */
.ic { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[dir="rtl"] .ic-flip, [dir="rtl"] .back .ic { transform: scaleX(-1); }

/* Buttons */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; min-height: 40px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); cursor: pointer; font-weight: 500; white-space: nowrap; text-decoration: none !important; color: var(--text); }
.btn:hover { border-color: var(--brand); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn-primary:hover { background: var(--brand-2); }
.btn-danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-ghost-danger { color: var(--bad); }
.btn-ghost-danger:hover { border-color: var(--bad); background: var(--bad-bg); }
.btn-sm { min-height: 32px; padding: 4px 10px; font-size: .88rem; }
.btn-lg { min-height: 48px; padding: 10px 22px; font-size: 1rem; }
.btn-link { border: 0; background: none; color: var(--brand); justify-content: center; }
.icon-btn { display: inline-grid; place-items: center; min-width: 40px; height: 40px; border: 0; background: none; border-radius: 10px; cursor: pointer; color: inherit; gap: 4px; grid-auto-flow: column; }
.icon-btn:hover { background: var(--surface-2); }
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* Shell */
.shell { min-height: 100vh; }
.sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; width: var(--side); background: var(--surface); border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column; z-index: 30; transition: transform .2s ease; }
.brand { display: flex; align-items: center; gap: 10px; padding: 16px; color: var(--text); text-decoration: none !important; }
.brand .logo { width: 40px; height: 40px; color: var(--brand); flex: none; }
.brand strong { display: block; font-size: .95rem; line-height: 1.2; }
.brand small, .side-foot small { color: var(--muted); font-size: .78rem; }
.nav { flex: 1; overflow-y: auto; padding: 4px 10px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 10px; color: var(--text); text-decoration: none; margin-block: 1px; }
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--info-bg); color: var(--brand); font-weight: 600; }
.nav-sep { margin: 14px 12px 4px; font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.side-foot { padding: 14px 16px; border-top: 1px solid var(--line); display: grid; }
.main { margin-inline-start: var(--side); min-width: 0; }
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px; padding: 10px 20px; padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.topbar h1 { flex: 1; font-size: 1.15rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lang-btn { font-weight: 700; }
.view { padding: 20px; max-width: 1200px; padding-bottom: calc(40px + env(safe-area-inset-bottom)); }
.page { display: grid; gap: 16px; }
.scrim { display: none; }
.only-mobile { display: none; }

@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); box-shadow: var(--shadow); }
  [dir="rtl"] .sidebar { transform: translateX(100%); }
  .nav-open .sidebar { transform: none; }
  .nav-open .scrim { display: block; position: fixed; inset: 0; background: rgb(0 0 0 / .35); z-index: 25; }
  .main { margin-inline-start: 0; }
  .only-mobile { display: inline-grid; }
  .view { padding: 14px; }
  .topbar { padding-inline: 10px; }
}

/* Sync pill */
.sync-pill { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; font-size: .8rem; background: var(--surface); border: 1px solid var(--line); color: var(--muted); text-decoration: none !important; white-space: nowrap; }
.sync-pill i { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.sp-syncing i { background: var(--info); animation: pulse 1s infinite; }
.sp-pending i { background: var(--accent); }
.sp-failed { color: var(--bad); border-color: var(--bad); } .sp-failed i { background: var(--bad); }
.sp-offline { color: var(--warn); } .sp-offline i { background: var(--muted); }
@keyframes pulse { 50% { opacity: .3; } }
@media (max-width: 480px) { .sync-pill span { display: none; } }

/* Cards & layout */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); display: grid; gap: 12px; min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.card-head h2 { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.actions.end { justify-content: flex-end; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.toolbar > .search { flex: 1; min-width: 200px; }
.back { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-size: .9rem; justify-self: start; }
.hello { margin: 0; color: var(--muted); }
.note { margin: 0; padding: 10px 12px; background: var(--surface-2); border-radius: 8px; white-space: pre-wrap; }
.empty { padding: 26px 12px; text-align: center; color: var(--muted); }
.loading { padding: 20px; text-align: center; color: var(--muted); }
.alert { padding: 10px 12px; border-radius: 8px; font-size: .9rem; }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.bad { background: var(--bad-bg); color: var(--bad); }

.search { display: flex; align-items: center; gap: 8px; padding: 0 12px; height: 42px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; }
.search input { border: 0; outline: 0; background: none; flex: 1; min-width: 0; height: 100%; }
.toolbar select, .toolbar input[type=date] { height: 42px; padding: 0 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }

.tabs { display: flex; gap: 4px; padding: 4px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; }
.tabs-scroll { overflow-x: auto; scrollbar-width: none; }
.tab { border: 0; background: none; padding: 7px 12px; border-radius: 8px; cursor: pointer; white-space: nowrap; color: var(--muted); font-weight: 500; }
.tab.active { background: var(--brand); color: var(--brand-ink); }
.tab-body { display: grid; gap: 16px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 5px 12px; cursor: pointer; }
.seg button.active { background: var(--brand); color: var(--brand-ink); }

/* KPIs & accounts */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: grid; gap: 2px; color: var(--text); text-decoration: none !important; box-shadow: var(--shadow); }
.kpi small { color: var(--muted); }
.kpi strong { font-size: 1.45rem; font-variant-numeric: tabular-nums; }
.kpi span { color: var(--muted); font-variant-numeric: tabular-nums; }
.quick { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.qa { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); cursor: pointer; font-weight: 500; text-align: start; }
.qa .ic { color: var(--brand); width: 22px; height: 22px; }
.qa:hover { border-color: var(--brand); }
.acc-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px; }
.acc { display: grid; gap: 2px; text-align: start; padding: 12px 14px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); cursor: pointer; color: var(--text); text-decoration: none !important; border-inline-start: 4px solid var(--accent); }
.acc-bank { border-inline-start-color: var(--brand); }
.acc.active { outline: 2px solid var(--brand); }
.acc small { color: var(--muted); font-size: .78rem; }
.acc-bal { font-size: 1.15rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.chips { display: flex; gap: 10px; flex-wrap: wrap; }
.chip { padding: 8px 12px; background: var(--surface-2); border-radius: 10px; display: grid; }
.chip small { color: var(--muted); font-size: .75rem; }
.chip-bad strong { color: var(--bad); }

/* Facts */
.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px 16px; margin: 0; }
.facts dt { font-size: .78rem; color: var(--muted); }
.facts dd { margin: 0; word-break: break-word; }

/* Badges */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .76rem; font-weight: 600; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-bg); color: var(--ok); } .badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); } .badge.info { background: var(--info-bg); color: var(--info); }
.badge + .badge { margin-inline-start: 4px; }

/* Tables (cards on phones) */
.tbl-wrap { overflow-x: auto; margin-inline: -4px; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: start; font-size: .78rem; font-weight: 600; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl th.num { text-align: end; }
.tbl tbody tr:last-child td { border-bottom: 0; }
.tbl tfoot td { border-top: 2px solid var(--line); font-weight: 600; }
tr[data-href] { cursor: pointer; }
tr[data-href]:hover td { background: var(--surface-2); }
tr.is-pending td { opacity: .7; } tr.is-pending td:first-child { border-inline-start: 3px solid var(--accent); }
.row-actions { display: flex; gap: 2px; justify-content: flex-end; }
@media (max-width: 700px) {
  .tbl:not(.inv-lines) thead { display: none; }
  .tbl:not(.inv-lines), .tbl:not(.inv-lines) tbody, .tbl:not(.inv-lines) tr, .tbl:not(.inv-lines) td { display: block; width: 100%; }
  .tbl:not(.inv-lines) tr { padding: 10px 4px; border-bottom: 1px solid var(--line); }
  .tbl:not(.inv-lines) td { border: 0; padding: 3px 4px; display: flex; justify-content: space-between; gap: 12px; text-align: end; }
  .tbl:not(.inv-lines) td::before { content: attr(data-label); color: var(--muted); font-size: .8rem; text-align: start; }
  .tbl:not(.inv-lines) td[data-label=""]::before { content: none; }
  .tbl:not(.inv-lines) tfoot { display: block; }
  .tbl.lines td input { max-width: 60%; }
}

/* Forms */
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.form-grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 600px) { .form-grid, .form-grid.cols-4 { grid-template-columns: 1fr; } }
.fld { display: grid; gap: 4px; min-width: 0; }
.fld > span { font-size: .84rem; font-weight: 500; color: var(--muted); }
.fld.inline { display: flex; align-items: center; gap: 6px; }
.req { color: var(--bad); margin-inline-start: 2px; }
.fld input, .fld select, .fld textarea, .lines input, .totals input {
  width: 100%; min-height: 42px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface); }
.fld textarea { resize: vertical; }
.fld input:focus, .fld select:focus, .fld textarea:focus, .lines input:focus { border-color: var(--brand); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 18%, transparent); }
.fld-check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.fld-check input { width: 18px; height: 18px; min-height: 0; accent-color: var(--brand); }
.hint { color: var(--muted); font-size: .76rem; }
.err { color: var(--bad); font-size: .78rem; }
.err:empty { display: none; }
.has-err input, .has-err select, .has-err textarea { border-color: var(--bad); }
.form-err { color: var(--bad); font-size: .88rem; }
.form-err:empty { display: none; }

/* Invoice builder */
.lines input { min-height: 36px; padding: 6px 8px; }
.lines .num input { text-align: end; }
.w-unit { max-width: 90px; } .w-money { max-width: 160px; text-align: end; }
.add-line { display: flex; gap: 8px; flex-wrap: wrap; }
.add-line select { flex: 1; min-width: 220px; height: 40px; border: 1px dashed var(--line); border-radius: 9px; padding: 0 10px; background: var(--surface-2); }
.job-strip { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: space-between; padding: 10px 12px; background: var(--surface-2); border-radius: 9px; font-size: .9rem; }
.totals { display: grid; gap: 6px; margin: 0; justify-self: end; min-width: min(340px, 100%); }
.totals > div { display: flex; justify-content: space-between; gap: 20px; align-items: center; }
.totals dt { color: var(--muted); }
.totals dd { margin: 0; font-variant-numeric: tabular-nums; }
.totals .grand { border-top: 1px solid var(--line); padding-top: 6px; font-weight: 700; font-size: 1.05rem; }
.totals .grand dt { color: var(--text); }

/* Invoice document */
.invoice-doc { gap: 18px; }
.invoice-doc.is-void { background-image: repeating-linear-gradient(-45deg, transparent 0 30px, color-mix(in srgb, var(--bad) 6%, transparent) 30px 60px); }
.inv-head { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; border-bottom: 2px solid var(--brand); padding-bottom: 12px; }
.inv-meta { text-align: end; display: grid; gap: 2px; }
.inv-meta h3 { color: var(--brand); letter-spacing: .08em; }
.inv-parties { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.inv-parties > div { display: grid; padding: 10px 12px; background: var(--surface-2); border-radius: 9px; }
.inv-parties small { color: var(--muted); }
.inv-foot { color: var(--muted); font-size: .85rem; white-space: pre-wrap; border-top: 1px solid var(--line); padding-top: 10px; }
.inv-sign { color: var(--muted); font-size: .85rem; }

/* Documents */
.docs { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.doc { display: grid; grid-template-rows: 120px auto; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface-2); cursor: pointer; padding: 0; text-align: start; }
.doc img { width: 100%; height: 120px; object-fit: cover; display: block; }
.doc-ic { display: grid; place-items: center; height: 120px; color: var(--muted); }
.doc-ic .ic { width: 40px; height: 40px; }
.doc-cap { padding: 8px; display: grid; background: var(--surface); }
.doc-cap strong { font-size: .86rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-cap small { color: var(--muted); font-size: .74rem; }
.doc.is-pending { border-style: dashed; border-color: var(--accent); }
.doc-preview { display: grid; place-items: center; margin-bottom: 12px; padding: 10px; background: var(--surface-2); border-radius: 9px; gap: 6px; }
.doc-preview img { max-width: 100%; max-height: 260px; border-radius: 6px; }
.doc-full { max-width: 100%; max-height: 75vh; display: block; margin: auto; border-radius: 8px; }

/* Dialogs */
.dlg { border: 0; padding: 0; border-radius: 16px; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 24px); background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgb(0 0 0 / .3); }
.dlg-wide { width: min(900px, calc(100vw - 24px)); }
.dlg::backdrop { background: rgb(8 20 30 / .5); }
.dlg[open] { display: flex; flex-direction: column; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.dlg-head h2 { font-size: 1.1rem; }
.dlg-body { padding: 16px; overflow-y: auto; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); flex-wrap: wrap; }
@media (max-width: 600px) {
  .dlg { width: 100vw; max-width: 100vw; max-height: 92vh; margin: auto 0 0; border-radius: 16px 16px 0 0; }
  .dlg-foot { padding-bottom: max(12px, env(safe-area-inset-bottom)); }
  .dlg-foot .btn { flex: 1; justify-content: center; }
}
.menu-list { display: grid; gap: 4px; }
.menu-item { display: flex; align-items: center; gap: 10px; padding: 12px; border: 0; background: none; border-radius: 10px; cursor: pointer; text-align: start; color: var(--text); text-decoration: none !important; }
.menu-item:hover { background: var(--surface-2); }
.menu-item.danger { color: var(--bad); }

/* Toasts & banners */
#toast-root { position: fixed; inset-inline: 0; bottom: calc(16px + env(safe-area-inset-bottom)); display: grid; justify-items: center; gap: 8px; z-index: 100; pointer-events: none; }
.toast { padding: 10px 16px; border-radius: 10px; background: #13212e; color: #fff; box-shadow: 0 8px 24px rgb(0 0 0 / .2); transition: opacity .3s, transform .3s; max-width: calc(100vw - 32px); }
.toast-err { background: var(--bad); } .toast-info { background: var(--info); }
.toast.out { opacity: 0; transform: translateY(8px); }
.update-bar { position: fixed; inset-inline: 12px; top: 12px; z-index: 90; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 14px; background: var(--surface); border: 1px solid var(--brand); border-radius: 12px; box-shadow: 0 8px 24px rgb(0 0 0 / .15); max-width: 480px; margin-inline: auto; }

/* Auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, var(--brand) 0%, #062c40 100%); }
.auth-card { width: min(400px, 100%); background: var(--surface); border-radius: 18px; padding: 26px 22px; display: grid; gap: 14px; box-shadow: 0 20px 60px rgb(0 0 0 / .3); }
.auth-brand { display: flex; align-items: center; gap: 12px; }
.auth-brand .logo { width: 48px; height: 48px; color: var(--brand); }
.auth-brand h1 { font-size: 1.2rem; }
.auth-brand small { color: var(--muted); }

/* Settings / users / audit / sync */
.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.perm-grid fieldset { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; display: grid; gap: 8px; margin: 0; }
.perm-grid legend { font-weight: 600; padding: 0 4px; }
.pager { display: flex; align-items: center; justify-content: center; gap: 12px; padding-top: 12px; }
.diff-old { color: var(--bad); } .diff-new { color: var(--ok); }
.json { background: var(--surface-2); padding: 10px; border-radius: 8px; overflow: auto; font-size: .8rem; max-height: 50vh; }
.outbox { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.outbox li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; flex-wrap: wrap; }
.outbox li > div:first-child { display: grid; }
.outbox small { color: var(--muted); }
.outbox .is-failed { border-color: var(--bad); background: var(--bad-bg); }
.err-text { color: var(--bad); font-size: .85rem; }

/* Print: only the invoice */
@media print {
  @page { margin: 14mm; }
  body { background: #fff; color: #000; font-size: 12px; }
  .sidebar, .topbar, .no-print, .toast, .update-bar, #toast-root { display: none !important; }
  .main { margin: 0; }
  .view { padding: 0; max-width: none; }
  .card { border: 0; box-shadow: none; padding: 0; }
  .inv-parties > div { background: none; border: 1px solid #ccc; }
  .tbl th, .tbl td { padding: 5px 6px; }
  a { color: inherit; }
}
