:root {
  --bg: #eef0ec;
  --surface: #ffffff;
  --ink: #1b1f1c;
  --muted: #5d665f;
  --line: #d5dad4;
  --accent: #1f3b2d;
  --accent-ink: #ffffff;
  --danger: #a12c2c;
  --bar-h: 56px;
  --act-h: 68px;
}

* { box-sizing: border-box; }
html, body { margin: 0; overflow-x: hidden; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: 'IBM Plex Sans Arabic', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: inherit; }
[hidden] { display: none !important; }

/* ---------- app chrome ---------- */
.bar {
  position: sticky; top: 0; z-index: 10;
  height: calc(var(--bar-h) + env(safe-area-inset-top));
  display: flex; align-items: center; gap: 10px;
  padding: env(safe-area-inset-top) 12px 0;
  background: var(--accent); color: var(--accent-ink);
}
.brand { font-weight: 600; font-size: 17px; padding-inline: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.install {
  margin-inline-start: auto; flex: none; height: 38px; padding: 0 14px; border-radius: 10px; cursor: pointer;
  font: inherit; font-size: 14px; font-weight: 600; border: 0; background: var(--surface); color: var(--accent);
}
.back {
  flex: none; display: inline-flex; align-items: center; gap: 4px;
  height: 40px; padding-inline: 10px 14px; border-radius: 10px;
  background: rgba(255,255,255,.14); text-decoration: none; font-weight: 600; font-size: 15px;
}
.back svg { width: 18px; height: 18px; }
.switch { position: relative; flex: 1; min-width: 0; display: flex; align-items: center; }
.switch select {
  appearance: none; -webkit-appearance: none; width: 100%; min-width: 0;
  font: inherit; font-weight: 600; font-size: 16px; color: inherit;
  background: transparent; border: 1px solid rgba(255,255,255,.3); border-radius: 10px;
  height: 40px; padding-inline: 12px 34px; cursor: pointer; text-overflow: ellipsis;
}
.switch select option { color: var(--ink); background: #fff; }
.switch svg { position: absolute; inset-inline-end: 10px; width: 18px; height: 18px; pointer-events: none; }
.tabs {
  position: sticky; top: calc(var(--bar-h) + env(safe-area-inset-top)); z-index: 9;
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px;
  padding: 8px 12px; background: var(--bg); border-bottom: 1px solid var(--line);
}
.tabs button {
  font: inherit; font-size: 15px; height: 42px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--muted);
}
.tabs button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }

.home { max-width: 1100px; margin: 0 auto; padding: 20px 16px 40px; }
.lead { color: var(--muted); margin: 0 0 18px; line-height: 1.7; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
.card {
  text-decoration: none; background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; overflow: hidden; display: flex; flex-direction: column;
  transition: transform .15s ease, box-shadow .15s ease;
}
.card:active { transform: scale(.98); }
@media (hover: hover) { .card:hover { box-shadow: 0 6px 20px rgba(0,0,0,.08); } }
.card .thumb { margin: 10px 10px 0; border: 1px solid var(--line); background: #fff; }
.card-txt { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 2px; }
.card-txt b { font-weight: 600; font-size: 15px; }
.card-txt small { color: var(--muted); font-size: 12.5px; }

.thumb, .sheet { position: relative; overflow: hidden; direction: ltr; }
.thumb > .page, .sheet > .page { position: absolute; top: 0; left: 0; transform-origin: 0 0; }

.editor { padding-bottom: calc(var(--act-h) + env(safe-area-inset-bottom) + 12px); }
.panel { background: var(--surface); padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.panel h2 { font-size: 16px; margin: 0; font-weight: 600; }
.fld { display: flex; flex-direction: column; gap: 5px; }
.fld > span { font-size: 13.5px; color: var(--muted); }
.fld input, .fld textarea, .fld select {
  font: inherit; font-size: 16px; color: var(--ink);
  border: 1px solid var(--line); border-radius: 9px; background: #fbfcfb;
  padding: 10px 12px; width: 100%; resize: vertical;
}
.fld input:focus, .fld textarea:focus, .fld select:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: #fff; }
.fld input[dir=ltr], .fld textarea[dir=ltr] { text-align: left; }
details { border-top: 1px solid var(--line); padding-top: 12px; }
details[open] { display: flex; flex-direction: column; gap: 12px; }
summary { cursor: pointer; font-weight: 500; color: var(--accent); }
.photo-in { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.photo-in > span { font-size: 13.5px; color: var(--muted); }
.photo-in > div { display: flex; gap: 8px; }

.preview { padding: 12px 10px; }
.sheet { background: #fff; box-shadow: 0 2px 14px rgba(0,0,0,.12); width: 100%; margin: 0 auto; }

.btn {
  font: inherit; font-size: 15px; font-weight: 500; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 10px; padding: 0 16px; height: 46px;
  display: inline-flex; align-items: center; justify-content: center;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.small { height: 36px; font-size: 13.5px; padding: 0 12px; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #e6caca; align-self: flex-start; }
.btn.next { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.btn:disabled { opacity: .6; }
.actions {
  position: fixed; inset-inline: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: 1fr 1.3fr 1fr; gap: 8px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: rgba(255,255,255,.96); border-top: 1px solid var(--line);
}

@media (max-width: 899px) {
  .editor[data-view=form] .preview, .editor[data-view=preview] .panel { display: none; }
}
@media (min-width: 900px) {
  .tabs, .btn.next { display: none; }
  .bar.ed { padding-inline-end: 400px; }
  .editor {
    display: grid; grid-template-columns: minmax(340px, 420px) 1fr; padding-bottom: 0;
    height: calc(100vh - var(--bar-h)); height: calc(100dvh - var(--bar-h));
  }
  .panel { overflow-y: auto; border-inline-end: 1px solid var(--line); }
  .preview { overflow: auto; padding: 24px; }
  /* whole page visible at once */
  .sheet { width: min(100%, 820px, calc((100vh - var(--bar-h) - 48px) / 1.4143)); }
  .actions {
    inset-inline: auto; left: 0; top: 0; bottom: auto; z-index: 11;
    height: var(--bar-h); display: flex; align-items: center; gap: 8px;
    padding: 0 12px; background: transparent; border: 0;
  }
  .actions .btn { height: 38px; background: transparent; color: var(--accent-ink); border-color: rgba(255,255,255,.4); }
  .actions .btn.primary { background: var(--surface); color: var(--accent); border-color: var(--surface); }
}

/* ---------- A4 document ---------- */
.page {
  width: 210mm; height: 297mm; background: #fff; color: #000;
  padding: var(--pt) var(--px) var(--pb);
  font-family: var(--ff, 'Noto Naskh Arabic', 'Tinos', serif);
  font-size: var(--fs); font-weight: 700; line-height: 1.5;
  overflow: hidden;
  -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
.page bdi { font-weight: 700; }
.frame, .frame-in { height: 100%; }
.frame-in { padding: 4mm 7mm 6mm; }

.top { display: flex; justify-content: space-between; align-items: flex-start; }
.hdr { font-size: 15pt; line-height: 1.45; text-align: center; }
.hdr.fr-head { font-family: 'Carlito', 'Calibri', sans-serif; font-style: italic; font-size: 15.5pt; line-height: 1.38; }
.numrow { display: flex; align-items: baseline; gap: 1.5mm; margin-top: .5mm; }
.photo {
  width: 40mm; height: 40mm; border: 1.6px solid #000; flex: none;
  display: grid; place-items: center; overflow: hidden; font-size: 12pt;
}
.photo img { width: 100%; height: 100%; object-fit: cover; }

.title { text-align: center; font-weight: 700; line-height: 1.3; }
.title[data-font=ruqaa] { font-family: 'Aref Ruqaa', 'Noto Naskh Arabic', serif; line-height: 1.75; }
.title[data-font=naskh] { font-family: 'Noto Naskh Arabic', serif; line-height: 1.45; }
.calibri { font-family: 'Carlito', 'Calibri', sans-serif; }
.line { margin: 0; line-height: var(--L); }
.title .u { display: inline-block; border-bottom: 2px solid #000; line-height: 1.25; padding-bottom: .5mm; }
.title[data-font=ruqaa] .u { line-height: 1.5; padding-bottom: 0; }

.ruled { position: relative; line-height: var(--L); height: calc(var(--L) * var(--n)); overflow: hidden; }
.ruled .rl { position: absolute; left: 0; right: 0; top: calc(var(--L) * (var(--i) + 1) - 0.8mm); border-top: 1px var(--ls) #000; }
.ruled .lab { display: inline-block; position: relative; z-index: 1; background: #fff; padding-inline: var(--ind, 0) 1.5mm; }
/* zero line-height keeps every ruled line exactly --L tall even when the value is shrunk */
.ruled .val { position: relative; z-index: 1; line-height: 0; }

.row { display: flex; align-items: baseline; gap: 1.2mm; line-height: var(--L); min-height: var(--L); }
.row .t { white-space: nowrap; flex: none; }
.blank {
  flex: 1 1 0; min-width: 8mm; display: block;
  border-bottom: 1px var(--ls) #000; line-height: 1.3;
  white-space: nowrap; overflow: hidden; text-align: center; padding: 0 1mm;
}
.blank.split { display: flex; justify-content: space-between; gap: 4mm; padding: 0 2mm; }
.blank.wrap { white-space: normal; overflow-wrap: anywhere; line-height: 1.2; flex-wrap: wrap; align-self: flex-end; }
.bi .fr, .bi .fv { font-family: 'Tinos', 'Times New Roman', serif; }
.bi .fr { font-size: 15pt; }
.sig { width: max-content; margin-inline-start: auto; text-align: center; line-height: 1.6; }
.tpl-residence .sig { line-height: 1.35; font-size: 17.5pt; }

/* hidden full-size copy used for print and export */
#render-root { position: absolute; top: 0; left: 0; width: 0; height: 0; overflow: hidden; visibility: hidden; pointer-events: none; }

@page { size: A4 portrait; margin: 0; }
@media print {
  html, body { background: #fff; overflow: visible; }
  #app { display: none !important; }
  #render-root { position: static; width: auto; height: auto; overflow: visible; visibility: visible; }
  #render-root .page { height: 296.5mm; page-break-after: avoid; break-after: avoid; }
}
