/* ============ Envelope Label Maker ============ */
:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --ink: #1d2433;
  --muted: #687083;
  --line: #e2ded6;
  --accent: #1f5f8b;
  --accent-ink: #ffffff;
  --accent-soft: #e6f0f7;
  --danger: #b4372f;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Arial, sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.4; }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 40px; height: 40px; display: grid; place-items: center; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink); font-size: 22px;
}
.brand h1 { margin: 0; font-size: 18px; }
.brand p { margin: 0; color: var(--muted); font-size: 13px; }
.tabs { display: flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 10px; }
.tab { border: 0; background: none; padding: 8px 14px; border-radius: 8px; font: inherit; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.08); font-weight: 600; }

.alert { margin: 16px 20px 0; padding: 12px 16px; border-radius: var(--radius); background: #fdecea; color: var(--danger); border: 1px solid #f4c7c3; }

/* ---------- Layout ---------- */
.layout {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(320px, 520px);
  gap: 20px; padding: 20px; max-width: 1400px; margin: 0 auto; align-items: start;
}
.forms { display: grid; gap: 16px; min-width: 0; }
.preview-col { display: grid; gap: 12px; min-width: 0; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 0; min-width: 0; }
fieldset.card { padding-top: 8px; }
legend { font-weight: 700; font-size: 15px; padding: 0 6px; display: flex; align-items: center; gap: 8px; }
legend em { font-weight: 400; color: var(--muted); font-style: normal; }
.step {
  display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent); font-size: 12px;
}

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; margin-top: 10px; }
.grid .full { grid-column: 1 / -1; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); font-weight: 500; }
input, textarea, select {
  font: inherit; font-size: 15px; color: var(--ink); width: 100%;
  padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff;
}
textarea { resize: vertical; line-height: 1.75; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label.check { display: flex; align-items: center; gap: 8px; color: var(--ink); font-size: 14px; }
label.check input { width: auto; }

.picker { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.picker select, .picker input { flex: 1 1 180px; }

.card-foot { display: flex; gap: 10px; margin-top: 12px; align-items: center; }

/* ---------- Logo ---------- */
.logo-row { display: flex; gap: 12px; align-items: center; }
.logo-thumb {
  width: 72px; height: 72px; border: 1px dashed var(--line); border-radius: 8px;
  display: grid; place-items: center; overflow: hidden; background: #fafafa; flex: none;
}
.logo-thumb span { font-size: 11px; color: var(--muted); }
.logo-thumb img { max-width: 100%; max-height: 100%; object-fit: contain; }
.logo-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.logo-actions small { width: 100%; color: var(--muted); }
.file-btn { display: inline-flex; }

/* ---------- Sizes ---------- */
.sizes { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; margin-top: 10px; }
.size { display: block; }
.size input { position: absolute; opacity: 0; pointer-events: none; }
.size span {
  display: block; text-align: center; padding: 10px 6px; border: 1px solid var(--line); border-radius: 8px;
  cursor: pointer; color: var(--ink); font-weight: 600; font-size: 14px; background: #fff;
}
.size input:checked + span { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.size input:focus-visible + span { outline: 2px solid var(--accent); }

/* ---------- Buttons ---------- */
.btn {
  font: inherit; font-weight: 600; border-radius: 8px; padding: 11px 16px; cursor: pointer;
  border: 1px solid transparent; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.small { padding: 7px 12px; font-size: 13px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.secondary { background: var(--accent-soft); color: var(--accent); }
.btn.ghost { background: #fff; border-color: var(--line); color: var(--ink); }
.btn.ghost:hover { border-color: var(--muted); }
.btn.danger-link { background: none; color: var(--danger); margin-left: auto; }
.btn:disabled { opacity: .6; cursor: wait; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---------- Preview ---------- */
.preview-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.preview-head h2 { margin: 0; font-size: 16px; }
.dim { color: var(--muted); font-size: 13px; }
.hint { margin: 0; font-size: 12.5px; color: var(--muted); }
.preview-stage {
  background: repeating-linear-gradient(45deg, #e9e6df 0 10px, #e3e0d8 10px 20px);
  border-radius: var(--radius); padding: 18px; display: flex; justify-content: center; align-items: flex-start;
  overflow: auto; max-height: 78vh;
}
.pages { display: flex; flex-direction: column; gap: 16px; align-items: center; }
.page-wrap { position: relative; flex: none; box-shadow: 0 6px 20px rgba(0,0,0,.18); background: #fff; }
.page-wrap .sheet { transform-origin: top left; position: absolute; left: 0; top: 0; }
.page-no { position: absolute; right: 6px; bottom: -18px; font-size: 11px; color: var(--muted); }
.hint.warn { color: var(--danger); font-weight: 600; }
.calc { margin: 12px 0 0; padding: 8px 12px; border-radius: 8px; background: var(--accent-soft); color: var(--accent); font-size: 13.5px; font-weight: 600; }

/* ---------- Layout modes ---------- */
.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.mode input { position: absolute; opacity: 0; pointer-events: none; }
.mode span { display: grid; gap: 2px; height: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; background: #fff; color: var(--ink); }
.mode b { font-size: 14px; }
.mode small { color: var(--muted); font-weight: 400; }
.mode input:checked + span { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.mode input:focus-visible + span { outline: 2px solid var(--accent); }
.panel { margin-top: 4px; }
.font-status { font-size: 12px; color: var(--muted); }
.font-status.bad { color: var(--danger); }

/* ---------- Queue ---------- */
.queue { display: grid; gap: 10px; }
.queue-list { margin: 0; padding: 0 0 0 22px; display: grid; gap: 6px; max-height: 220px; overflow: auto; }
.queue-list li { font-size: 14px; }
.q-row { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.q-row span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.q-row .q-btns { display: flex; gap: 4px; flex: none; }
.q-row button { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 2px 8px; cursor: pointer; font: inherit; font-size: 12px; }
.queue-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.queue .grid { margin-top: 0; }
.fill-wrap { display: flex; align-items: end; }

/* ---------- Address book dialog ---------- */
.dialog { border: 0; border-radius: 14px; padding: 0; width: min(560px, 94vw); max-height: 88vh; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.dialog::backdrop { background: rgba(20, 24, 33, .45); }
.dialog-body { padding: 18px; display: grid; gap: 12px; }
.book-tools { display: flex; justify-content: space-between; align-items: center; }
.book-list { max-height: 46vh; overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
.book-list label { display: flex; gap: 10px; align-items: flex-start; padding: 9px 12px; border-bottom: 1px solid var(--line); color: var(--ink); font-weight: 400; cursor: pointer; }
.book-list label:last-child { border-bottom: 0; }
.book-list input { width: auto; margin-top: 3px; }
.book-list small { display: block; color: var(--muted); }

/* ---------- Sheets and labels (all physical units) ---------- */
.sheet { position: relative; background: #fff; overflow: hidden; color: #000; }
.label {
  --k: 1;           /* text scale, set per label by JS (auto-fit) */
  --pad: 4mm;       /* inner padding */
  --inset: 0mm;     /* border inset (single labels get a small inset) */
  --ulh: 1.85;      /* line height for Urdu lines (Nastaleeq needs room) */
  --sf: Arial, sans-serif;   /* sender font stack   */
  --rf: Arial, sans-serif;   /* receiver font stack */
  position: absolute; box-sizing: border-box; background: #fff; color: #000;
  padding: var(--pad);
  display: flex; flex-direction: column; gap: calc(var(--pad) * 0.45);
  overflow: hidden; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
  overflow-wrap: anywhere;
  -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
.label::after { content: ""; position: absolute; inset: var(--inset); pointer-events: none; }
.label.b-solid::after  { border: 0.25mm solid #000; }
.label.b-dashed::after { border: 0.25mm dashed #666; }

.label .ur { line-height: var(--ulh); }
.label .rtl { direction: rtl; text-align: right; }
.label .rtl > * { text-align: right; }

.lbl-caption { font-size: calc(7pt * var(--k)); font-weight: 700; letter-spacing: .1em; color: #444; line-height: 1.3; }
.rtl > .lbl-caption { letter-spacing: 0; font-size: calc(8pt * var(--k)); }

/* Sender */
.lbl-from { display: flex; gap: calc(3mm * var(--k)); align-items: flex-start; padding-bottom: calc(1.6mm * var(--k)); border-bottom: 0.25mm solid #999; font-family: var(--sf); flex: none; }
.lbl-from.rtl { flex-direction: row-reverse; }
.lbl-from.rtl .lbl-from-text { direction: rtl; text-align: right; }
.lbl-logo { width: calc(20mm * var(--k)); height: calc(17mm * var(--k)); object-fit: contain; object-position: left top; flex: none; }
.lbl-from.rtl .lbl-logo { object-position: right top; }
.lbl-from-text { min-width: 0; flex: 1; }
.lbl-s-name { font-size: calc(10.5pt * var(--k)); font-weight: 700; line-height: 1.25; }
.lbl-s-line { font-size: calc(8.5pt * var(--k)); line-height: 1.3; white-space: pre-line; }
.lbl-from.oneline { display: block; font-size: calc(8pt * var(--k)); line-height: 1.35; }
.lbl-from.oneline .lbl-s-name { display: inline; font-size: inherit; }

/* Phone numbers: always exactly 16px, never scaled */
.lbl-phone { font-size: 16px !important; line-height: 1.3 !important; font-weight: 600; letter-spacing: 0; white-space: nowrap; }
.lbl-phone { font-family: "ELM-noto-naskh", Arial, sans-serif; }   /* compact Naskh for the Urdu word فون so the line stays tight */
.lbl-phone bdi { font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; }
.lbl-to .lbl-phone { font-weight: 700; }
.lbl-from.oneline .lbl-phone { display: inline; }

/* Receiver: large and bold */
.lbl-to { flex: 1 1 auto; display: flex; flex-direction: column; justify-content: center; min-height: 0; font-family: var(--rf); }
.lbl-to .lbl-caption { font-size: calc(9pt * var(--k)); }
.lbl-r-name { font-size: calc(19pt * var(--k)); font-weight: 800; line-height: 1.2; }
.lbl-r-org  { font-size: calc(13pt * var(--k)); font-weight: 700; line-height: 1.25; }
.lbl-r-addr { font-size: calc(15pt * var(--k)); font-weight: 700; line-height: 1.28; white-space: pre-line; }
.lbl-r-email { font-size: calc(10pt * var(--k)); line-height: 1.3; }
.lbl-r-name.ur, .lbl-r-org.ur, .lbl-r-addr.ur { line-height: var(--ulh); }

/* Courier strip */
.lbl-courier { border-top: 0.25mm solid #999; padding-top: calc(1.4mm * var(--k)); display: flex; flex-wrap: wrap; gap: 0.5mm 5mm; font-size: calc(9pt * var(--k)); line-height: 1.3; flex: none; }
.lbl-courier b { font-weight: 700; }

/* Wide envelope: receiver block moves towards the centre-right */
.label.env .lbl-to { padding-left: 32%; }
.label.env .lbl-from { border-bottom: 0; max-width: 62%; }
.label.env .lbl-from.rtl { margin-left: auto; }

/* Small labels: tighter spacing */
.label.sm { gap: calc(var(--pad) * 0.3); }
.label.sm .lbl-from { padding-bottom: 0.8mm; }
.label.sm .lbl-caption { display: none; }
.label.sm .lbl-courier { padding-top: 0.6mm; gap: 0 3mm; }

.empty-hint { color: #aaa !important; font-weight: 400 !important; font-style: italic; }
.slot-empty { background: repeating-linear-gradient(45deg, #fafafa 0 6px, #f1f1f1 6px 12px); }

@media screen {
  .label.overflow { box-shadow: inset 0 0 0 2px #d33; }
}


/* ---------- History ---------- */
.history { padding: 20px; max-width: 1000px; margin: 0 auto; }
.history-list { display: grid; gap: 10px; margin-top: 12px; }
.h-item {
  display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center;
  border: 1px solid var(--line); border-radius: 8px; padding: 12px;
}
.h-item b { display: block; }
.h-meta { font-size: 12.5px; color: var(--muted); }
.h-btns { display: flex; gap: 6px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px;
  opacity: 0; transition: .2s; pointer-events: none; z-index: 50; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--danger); }

#print-root { display: none; }

/* ---------- Mobile ---------- */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 12px; }
  .preview-col { order: 2; }
  .history { padding: 12px; }
}
@media (max-width: 520px) {
  .grid { grid-template-columns: 1fr; }
  .topbar { padding: 10px 12px; }
  .brand p { display: none; }
  .actions { grid-template-columns: 1fr; }
  .modes { grid-template-columns: 1fr; }
  .h-item { grid-template-columns: 1fr; }
}

/* ---------- PRINT: only the pages, at exact physical size ---------- */
@media print {
  html, body { margin: 0 !important; padding: 0 !important; background: #fff !important; }
  .app, .toast, dialog { display: none !important; }
  #print-root { display: block !important; }
  #print-root .sheet { margin: 0; break-after: page; page-break-after: always; transform: none !important; position: relative !important; }
  #print-root .sheet:last-child { break-after: auto; page-break-after: auto; }
  .slot-empty { background: none !important; }
}


/* ===================== v3: address book, records, print settings ===================== */
.opt { font-style: normal; font-weight: 400; color: var(--muted); }
.page { padding: 20px; max-width: 1200px; margin: 0 auto; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.page-head h2 { margin: 0; font-size: 18px; }
.page-head p { margin: 2px 0 0; }
.toolbar { display: flex; gap: 10px; margin: 14px 0; flex-wrap: wrap; }
.toolbar input { flex: 1 1 260px; }
.toolbar select { flex: 0 0 180px; }
.center { text-align: center; margin-top: 12px; }
.alert.small { margin: 0; font-size: 14px; }

/* Document bar */
.doc-bar .doc-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.doc-bar h2 { margin: 0; font-size: 17px; }
.doc-bar p { margin: 2px 0 0; }
.doc-bar.editing { border-color: var(--accent); box-shadow: inset 4px 0 0 var(--accent); }
.dirty-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #d08a00; margin-left: 6px; vertical-align: middle; }

/* Segmented controls */
.setting-row { display: grid; grid-template-columns: 140px 1fr; gap: 10px; align-items: center; margin-top: 12px; }
.setting-label { font-size: 13px; color: var(--muted); font-weight: 600; }
.seg { display: flex; flex-wrap: wrap; gap: 0; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; width: fit-content; max-width: 100%; background: #fff; }
.seg label { display: block; color: var(--ink); font-weight: 600; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 8px 14px; cursor: pointer; font-size: 14px; border-right: 1px solid var(--line); white-space: nowrap; }
.seg label:last-child span { border-right: 0; }
.seg input:checked + span { background: var(--accent); color: #fff; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.seg input:disabled + span { opacity: .45; cursor: not-allowed; }

/* Searchable address picker */
.combo { position: relative; flex: 1 1 240px; min-width: 0; }
.combo-input { width: 100%; }
.combo-list {
  position: absolute; z-index: 30; left: 0; right: 0; top: calc(100% + 4px); margin: 0; padding: 4px; list-style: none;
  background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 30px rgba(0,0,0,.14);
  max-height: 320px; overflow: auto;
}
.combo-list li { padding: 8px 10px; border-radius: 7px; cursor: pointer; display: grid; gap: 1px; }
.combo-list li b { font-size: 14px; }
.combo-list li small { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.combo-list li.active, .combo-list li:hover { background: var(--accent-soft); }
.combo-list li.add-new { color: var(--accent); font-weight: 600; border-top: 1px solid var(--line); border-radius: 0 0 7px 7px; margin-top: 2px; }
.combo-list li.empty { color: var(--muted); cursor: default; }
.combo-list li.empty:hover { background: none; }

.linked {
  display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; justify-content: space-between;
  margin-top: 10px; padding: 8px 12px; border-radius: 8px; background: var(--accent-soft); color: var(--accent); font-size: 13.5px;
}
.linked.edited { background: #fff6e0; color: #8a5a00; }
.linked-btns { display: flex; gap: 4px; flex-wrap: wrap; }
.link-btn { border: 0; background: rgba(255,255,255,.75); color: inherit; font: inherit; font-size: 12.5px; font-weight: 600; padding: 4px 9px; border-radius: 6px; cursor: pointer; }
.link-btn:hover { background: #fff; }

/* Badges */
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 700; background: var(--accent-soft); color: var(--accent); vertical-align: middle; }
.badge.sender { background: #e8f3ea; color: #23703a; }
.badge.receiver { background: #e6f0f7; color: #1f5f8b; }
.badge.both { background: #f1e9f7; color: #6b3d8f; }
.badge.muted { background: #eee; color: #555; }

/* Address table */
.table-wrap { overflow-x: auto; }
.addr-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.addr-table th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.addr-table td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.addr-table tr:hover td { background: #faf9f6; }
.addr-table .cell-main b { display: block; }
.addr-table .cell-main small { color: var(--muted); display: block; }
.row-btns { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.row-btns button { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 4px 9px; font: inherit; font-size: 12.5px; cursor: pointer; white-space: nowrap; }
.row-btns button:hover { border-color: var(--muted); }
.row-btns button.danger { color: var(--danger); }
.empty-state { padding: 30px 10px; text-align: center; color: var(--muted); }

/* Records */
.rec-list { display: grid; gap: 10px; }
.rec {
  display: grid; grid-template-columns: 170px 1fr auto; gap: 12px; align-items: center;
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px;
}
.rec-no b { display: block; font-size: 14px; }
.rec-no small { color: var(--muted); font-size: 12px; }
.rec-main { min-width: 0; }
.rec-main .to { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rec-main .meta { font-size: 12.5px; color: var(--muted); }

/* Dialogs */
.dialog.wide { width: min(760px, 96vw); }
.dialog.full { width: min(1100px, 98vw); height: 94vh; max-height: 94vh; }
.dialog.full .dialog-body { height: 100%; display: flex; flex-direction: column; box-sizing: border-box; }
.preview-stage.big { flex: 1; max-height: none; }
.pv-btns { display: flex; gap: 8px; }
.actions.three { grid-template-columns: 1.4fr 1fr 1fr; }

/* Batch */
.batch summary { cursor: pointer; list-style: revert; }
.batch[open] summary { margin-bottom: 8px; }
.batch .grid { margin-top: 10px; }

@media (max-width: 760px) {
  .page { padding: 12px; }
  .setting-row { grid-template-columns: 1fr; gap: 6px; }
  .addr-table thead { display: none; }
  .addr-table tr { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 10px 0; border-bottom: 1px solid var(--line); }
  .addr-table td { border: 0; padding: 2px 4px; }
  .addr-table td.c-name { grid-column: 1 / -1; }
  .addr-table td.c-hide { display: none; }
  .addr-table td.c-btns { grid-column: 1 / -1; }
  .row-btns { justify-content: flex-start; }
  .rec { grid-template-columns: 1fr; }
  .actions.three { grid-template-columns: 1fr 1fr; }
  .actions.three .primary { grid-column: 1 / -1; }
}

/* Hidden radio inputs behind the custom controls: keep them tiny and inside their label */
.seg label, .size, .mode { position: relative; }
.seg input, .size input, .mode input { width: 1px; height: 1px; left: 0; top: 0; margin: 0; }
