/* Layout: phone-first single column, cards per section; bottom tab bar on phones, top tabs on desktop. */
:root {
  --bg: #f2f3f5;
  --surface: #ffffff;
  --ink: #1f2a36;
  --muted: #5d6976;
  --line: #d9dee3;
  --brand: #d6844a;
  --brand-ink: #9a5626;
  --navy: #1f2a36;
  --on-navy: #f4f1ec;
  --input: #fff8e3;
  --input-line: #e8d9a8;
  --ok: #1d7a46; --ok-bg: #ddf2e5;
  --warn: #8a6500; --warn-bg: #fff0bf;
  --bad: #a3282a; --bad-bg: #fbe1e1;
  --shadow: 0 1px 2px rgba(31, 42, 54, .08);
  --font: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, Arial, sans-serif;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11161c; --surface: #1a2129; --ink: #e8ecf0; --muted: #9ba7b3; --line: #2c3641;
    --brand: #e29a63; --brand-ink: #f0b384; --navy: #0b1016; --on-navy: #f4f1ec;
    --input: #26231d; --input-line: #4a412d;
    --ok: #7fd3a1; --ok-bg: #173826; --warn: #f1cf6b; --warn-bg: #3a3014; --bad: #f19a9a; --bad-bg: #431d1e;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #11161c; --surface: #1a2129; --ink: #e8ecf0; --muted: #9ba7b3; --line: #2c3641;
  --brand: #e29a63; --brand-ink: #f0b384; --navy: #0b1016; --on-navy: #f4f1ec;
  --input: #26231d; --input-line: #4a412d;
  --ok: #7fd3a1; --ok-bg: #173826; --warn: #f1cf6b; --warn-bg: #3a3014; --bad: #f19a9a; --bad-bg: #431d1e;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink); font-family: var(--font); font-size: 15px; line-height: 1.5;
  padding-bottom: calc(76px + env(safe-area-inset-bottom, 0px));
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
h1, h2, h3 { text-wrap: balance; margin: 0; }

/* ---------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--navy); color: var(--on-navy);
  padding: calc(8px + env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) 8px max(16px, env(safe-area-inset-left, 0px));
  border-bottom: 3px solid var(--brand);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { height: 38px; width: auto; background: #fff; border-radius: 6px; padding: 3px 6px; }
.brand-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.brand-text strong { font-size: 16px; }
.brand-text span { font-size: 12px; opacity: .75; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; align-items: center; gap: 8px; }

.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 99px; white-space: nowrap; }
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill[data-state="ok"] { background: var(--ok-bg); color: var(--ok); }
.pill[data-state="warn"] { background: var(--warn-bg); color: var(--warn); }
.pill[data-state="bad"] { background: var(--bad-bg); color: var(--bad); }

.banner { background: var(--warn-bg); color: var(--warn); font-size: 13px; font-weight: 500; padding: 8px max(16px, env(safe-area-inset-right, 0px)) 8px max(16px, env(safe-area-inset-left, 0px)); }

/* ---------- main */
.view { max-width: 1000px; margin: 0 auto; padding-block: 14px 24px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); display: grid; gap: 14px; }
.form-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 8px; }
.form-head h1 { font-size: 20px; }
.form-head .sub { color: var(--muted); font-size: 12px; }
.code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12px; color: var(--brand-ink); background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 1px 8px; }

.toolbar {
  position: sticky; top: calc(var(--hdr, 60px) + var(--tabsh, 0px)); z-index: 10;
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 8px; margin: 0 -8px;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.toolbar .spacer { flex: 1; }
.state-note { font-size: 12px; color: var(--muted); }

button { cursor: pointer; border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 8px 14px; font-weight: 600; font-size: 14px; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
button.ghost { background: transparent; border-color: rgba(255, 255, 255, .25); color: inherit; }
button.small { padding: 4px 10px; font-size: 13px; }
button.danger { color: var(--bad); }
button:disabled { opacity: .5; cursor: not-allowed; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.card > h2 { font-size: 14px; font-weight: 700; padding: 9px 14px; background: var(--navy); color: var(--on-navy); display: flex; justify-content: space-between; gap: 8px; }
.card > h2 small { font-weight: 400; opacity: .7; }
.card-body { padding: 12px 14px; display: grid; gap: 12px; }

.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(126px, 100%), 1fr)); gap: 10px 12px; }
.field { display: grid; gap: 3px; min-width: 0; }
.field > span { font-size: 12px; font-weight: 600; color: var(--muted); }
input, select, textarea {
  width: 100%; min-width: 0; background: var(--input); border: 1px solid var(--input-line); border-radius: 7px; padding: 7px 9px; font-size: 15px;
}
textarea { min-height: 84px; resize: vertical; }
input[type="number"] { font-variant-numeric: tabular-nums; direction: ltr; text-align: center; }

/* ---------- doors */
.door { border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.door-head { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; padding: 9px 12px; background: var(--surface); width: 100%; border: 0; border-radius: 0; text-align: start; font-weight: 400; }
.door-num { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; background: var(--navy); color: var(--on-navy); font-size: 13px; font-weight: 700; }
.door-sum { min-width: 0; display: grid; }
.door-sum b { font-size: 15px; }
.door-sum span { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.door-prog { display: grid; justify-items: end; gap: 3px; font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
.bar { width: 74px; height: 6px; border-radius: 4px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--brand); }
.door[data-done="1"] .bar i { background: var(--ok); }
.door-body { padding: 12px; border-top: 1px dashed var(--line); display: grid; gap: 12px; background: color-mix(in srgb, var(--bg) 45%, var(--surface)); }

.stages { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 6px; }
.chip {
  display: grid; gap: 1px; text-align: center; padding: 6px 4px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); font-weight: 500;
}
.chip small { font-size: 11px; color: var(--muted); line-height: 1.2; }
.chip b { font-size: 14px; min-height: 21px; }
.chip[data-v="✓"] { background: var(--ok-bg); border-color: transparent; color: var(--ok); }
.chip[data-v="✓"] small { color: var(--ok); }
.chip[data-v="جاري"] { background: var(--warn-bg); border-color: transparent; color: var(--warn); }
.chip[data-v="متوقف"], .chip[data-v="إعادة"] { background: var(--bad-bg); border-color: transparent; color: var(--bad); }
.hint { font-size: 12px; color: var(--muted); margin: 0; }

.totals { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.stat { border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; display: grid; }
.stat span { font-size: 12px; color: var(--muted); }
.stat b { font-size: 22px; font-variant-numeric: tabular-nums; }
.stage-totals { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 6px; }
.stage-totals div { border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; font-size: 12px; display: flex; justify-content: space-between; gap: 6px; }
.stage-totals b { font-variant-numeric: tabular-nums; }

/* ---------- line items (daily / qc) */
.items { display: grid; gap: 8px; }
.item { display: grid; gap: 6px; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.item:last-child { border-bottom: 0; padding-bottom: 0; }
.item-title { display: flex; justify-content: space-between; gap: 8px; font-weight: 600; font-size: 14px; }
.item-title small { color: var(--muted); font-weight: 400; }
.inline { display: grid; grid-template-columns: repeat(auto-fit, minmax(74px, 1fr)); gap: 6px 8px; align-items: end; }
.inline .wide { grid-column: span 2; }
.inline .field > span { font-size: 11px; }
.calc { min-height: 36px; display: grid; place-items: center; border-radius: 7px; background: var(--bg); border: 1px solid var(--line); font-weight: 700; font-variant-numeric: tabular-nums; font-size: 14px; }
.calc[data-state="ok"], .result[data-state="ok"] { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.calc[data-state="bad"], .result[data-state="bad"] { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.calc[data-state="warn"] { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.sumline { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--muted); }
.sumline b { color: var(--ink); font-variant-numeric: tabular-nums; }

.marks { display: grid; grid-template-columns: repeat(3, 1fr) 1.4fr; gap: 6px; align-items: center; }
.mark { padding: 6px 0; text-align: center; font-weight: 700; }
.mark small { display: block; font-size: 11px; font-weight: 500; color: var(--muted); }
.mark[data-v="✓"] { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.mark[data-v="✗"] { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.result { text-align: center; padding: 8px 4px; border-radius: 7px; font-weight: 700; font-size: 13px; background: var(--bg); }
.decision { font-size: 17px; font-weight: 700; text-align: center; padding: 12px; border-radius: 9px; background: var(--bg); color: var(--muted); }
.decision[data-state="ok"] { background: var(--ok-bg); color: var(--ok); }
.decision[data-state="bad"] { background: var(--bad-bg); color: var(--bad); }

/* ---------- records */
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button[aria-pressed="true"] { background: var(--navy); color: var(--on-navy); border-color: var(--navy); }
.rec { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); align-items: center; }
.rec:last-child { border-bottom: 0; }
.rec-main { min-width: 0; display: grid; gap: 2px; }
.rec-main b { font-size: 15px; overflow-wrap: anywhere; }
.rec-main span { font-size: 12px; color: var(--muted); }
.tag { display: inline-block; font-size: 11px; font-weight: 700; padding: 1px 8px; border-radius: 99px; background: var(--input); color: var(--brand-ink); margin-inline-end: 6px; }
.rec-act { display: flex; gap: 6px; }
.empty { padding: 26px 14px; text-align: center; color: var(--muted); }

/* ---------- login */
.login { max-width: 400px; width: 100%; margin: 5vh auto 0; }
.login .card-body, .login-form { gap: 14px; display: grid; }
.error { color: var(--bad); font-size: 13px; min-height: 1.2em; margin: 0; }
.google { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 11px 14px; font-size: 15px; background: var(--surface); }
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.login-links { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px; }
button.link { border: 0; background: none; padding: 4px 0; color: var(--brand-ink); font-weight: 600; text-decoration: underline; text-underline-offset: 3px; }
#usersBtn[data-alert="1"] { background: var(--brand); border-color: var(--brand); color: #fff; }
.rec-act select { width: auto; min-width: 130px; }

/* ---------- tab bar */
.tabs {
  position: fixed; inset: auto 0 0 0; z-index: 30; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding: 4px max(8px, env(safe-area-inset-right, 0px)) calc(4px + env(safe-area-inset-bottom, 0px)) max(8px, env(safe-area-inset-left, 0px));
}
.tabs button { border: 0; background: transparent; display: grid; justify-items: center; gap: 2px; padding: 6px 2px; font-size: 12px; font-weight: 600; color: var(--muted); border-radius: 8px; }
.tabs svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tabs button[aria-current="page"] { color: var(--brand-ink); background: var(--input); }

@media (min-width: 800px) {
  body { padding-bottom: 0; }
  .fields { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
  .tabs { position: sticky; inset: auto; top: var(--hdr, 60px); display: flex; justify-content: center; gap: 4px; border-top: 0; border-bottom: 1px solid var(--line); }
  .tabs button { grid-auto-flow: column; gap: 8px; padding: 8px 16px; font-size: 14px; }
}
@media (max-width: 420px) {
  .totals { grid-template-columns: 1fr 1fr; }
  .marks { grid-template-columns: repeat(3, 1fr); }
  .marks .result { grid-column: 1 / -1; }
}

.toast {
  position: fixed; z-index: 50; left: 50%; transform: translateX(-50%); bottom: calc(86px + env(safe-area-inset-bottom, 0px));
  background: var(--navy); color: var(--on-navy); padding: 10px 18px; border-radius: 9px; font-size: 14px; max-width: calc(100vw - 32px); box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
}

/* ---------- paper (off-screen render target for PDF) */
.paper-host { position: fixed; left: -10000px; top: 0; }
.paper { background: #fff; color: #111; font-family: var(--font); padding: 28px 30px; direction: rtl; font-size: 13px; }
.paper.landscape { width: 1500px; }
.paper.portrait { width: 1000px; }
.paper .ph { display: grid; grid-template-columns: 180px 1fr 230px; align-items: center; gap: 16px; border-bottom: 4px solid #d6844a; padding-bottom: 10px; margin-bottom: 14px; }
.paper .ph img { width: 170px; }
.paper .ph h1 { font-size: 22px; text-align: center; color: #1f2a36; }
.paper .ph p { margin: 2px 0 0; text-align: center; color: #5d6976; }
.paper table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.paper th, .paper td { border: 1px solid #9aa3ad; padding: 5px 6px; text-align: center; }
.paper th { background: #3a4a5c; color: #fff; font-weight: 600; font-size: 12px; }
.paper .sec { background: #1f2a36; color: #fff; font-weight: 700; padding: 6px 10px; margin: 10px 0 0; }
.paper .kv td:nth-child(odd) { background: #fbf3ec; font-weight: 600; width: 16%; text-align: right; }
.paper .meta td { font-size: 12px; }
.paper .meta td:first-child { background: #fbf3ec; font-weight: 600; }
.paper .s-ok { background: #c6efce; color: #006100; font-weight: 700; }
.paper .s-warn { background: #ffeb9c; }
.paper .s-bad { background: #ffc7ce; color: #9c0006; font-weight: 700; }
.paper .tot td { background: #eef0f2; font-weight: 700; }
.paper .left { text-align: right; }
.paper .sign { display: grid; grid-template-columns: repeat(3, 1fr); gap: 24px; margin-top: 22px; }
.paper .sign div { border-top: 1px solid #9aa3ad; padding-top: 6px; font-weight: 600; }
.paper .foot { margin-top: 18px; text-align: center; color: #5d6976; font-size: 11px; }

@media (prefers-reduced-motion: no-preference) {
  .bar i { transition: width .25s ease; }
}
