/* =========================================================
   Cabinet Medical — Styles
   Editorial paper-like aesthetic with medical authority
   ========================================================= */
@import url('https://fonts.googleapis.com/css2?family=Manrope:wght@300;400;500;600;700;800&family=Cormorant+Garamond:ital,wght@0,400;0,500;0,600;1,400;1,500&family=JetBrains+Mono:wght@400;500&display=swap');

:root {
  --paper:      #FAF7EE;
  --paper-2:    #F2EDDE;
  --paper-3:    #E8E0CB;
  --ink:        #1A2440;
  --ink-soft:   #3A4566;
  --ink-mute:   #6B7388;
  --rule:       #D8D2C0;
  --gold:       #B89B5E;
  --wine:       #7A2541;
  --moss:       #4A6741;
  --bg:         #EFEAD8;

  --shadow-sm:   0 2px 6px -2px rgba(26,36,64,.10);
  --shadow-md:   0 6px 18px -6px rgba(26,36,64,.15);
  --shadow-paper:0 14px 40px -10px rgba(26,36,64,.20);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink);
  font-family: 'Manrope', -apple-system, BlinkMacSystemFont, sans-serif; font-size: 14px; line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: clip;            /* clip horizontal overflow WITHOUT breaking
                                  position:sticky or creating a scroll container */
  max-width: 100%;
  width: 100%; }

/* === GLOBAL RESPONSIVE HARDENING — prevents any horizontal overflow === */
.shell { overflow-x: clip; max-width: 100%; width: 100%; }
/* Media never exceeds its container */
img, svg, canvas, video, iframe { max-width: 100%; }
/* Long words / URLs / numbers wrap instead of pushing the layout wider */
.card, .page, .field, .modal-body, .ro-v, .med-item, .auth-card,
.editor-form, p, h1, h2, h3, td, th, label, .badge {
  overflow-wrap: break-word; word-break: break-word; }

button { font-family: inherit; cursor: pointer; }
/* font-size:16px on form controls — below 16px iOS Safari auto-zooms the
   page on focus and never zooms back out. 16px disables that behaviour. */
input, select, textarea { font-family: inherit; font-size: 16px; }
a { color: var(--ink); }

#root { min-height: 100vh; }

/* === BUTTONS ============================================= */
.btn { display: inline-flex; align-items: center; justify-content: center;
  gap: 6px; padding: 10px 16px; font-size: 13px; font-weight: 600;
  border: 1px solid var(--ink); background: var(--ink); color: #fff;
  cursor: pointer; transition: all 0.15s; letter-spacing: 0.01em; }
.btn:hover:not(:disabled) { background: var(--ink-soft); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--ink); color: #fff; width: 100%; }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--rule); }
.btn.ghost:hover:not(:disabled) { background: var(--paper-2); border-color: var(--ink-mute); }
.btn.danger { background: var(--wine); border-color: var(--wine); color: #fff; }
.btn.danger:hover:not(:disabled) { background: #5a1a31; }
.btn.whatsapp { background: #25D366; border-color: #25D366; color: #fff; }
.btn.whatsapp:hover:not(:disabled) { background: #20b558; border-color: #20b558; }
.btn.email { background: #4A6741; border-color: #4A6741; color: #fff; }
.btn.email:hover:not(:disabled) { background: #3a5232; border-color: #3a5232; }
.send-grid { display: grid; grid-template-columns: minmax(0,1fr) minmax(0,1fr); gap: 8px; }
.send-grid .btn { width: 100%; }

@keyframes spin { to { transform: rotate(360deg); } }
.spin { animation: spin 0.9s linear infinite; transform-origin: center; }

.btn.sm { padding: 6px 10px; font-size: 12px; }

.ico-btn { width: 30px; height: 30px; display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: 1px solid var(--rule); color: var(--ink); border-radius: 4px; cursor: pointer; }
.ico-btn:hover { background: var(--paper-2); }

/* === FIELDS / INPUTS ===================================== */
.field { margin-bottom: 10px; position: relative; }
.field .lab { display: block; font-size: 11px; text-transform: uppercase;
  letter-spacing: 0.14em; color: var(--ink-mute); font-weight: 600; margin-bottom: 4px; }
.field-row { display: flex; align-items: center; gap: 6px; position: relative; }

/* Responsive form rows — 2/3-column on desktop, single column on phones.
   Replaces inline gridTemplateColumns styles that never collapsed. */
.frow { display: grid; gap: 8px; }
.frow-2  { grid-template-columns: minmax(0,1fr) minmax(0,1fr); }
.frow-2w { grid-template-columns: minmax(0,2fr) minmax(0,1fr); }
.frow-15 { grid-template-columns: minmax(0,1.5fr) minmax(0,1fr); }
.frow-3  { grid-template-columns: minmax(0,1fr) minmax(0,1fr) minmax(0,1fr); }
.frow-g12 { gap: 12px; }
@media (max-width: 640px) {
  .frow-2, .frow-2w, .frow-15, .frow-3 { grid-template-columns: minmax(0,1fr); }
}
.inp { width: 100%; min-width: 0; padding: 10px 12px; font-size: 16px;
  border: 1px solid var(--rule); background: #fff; color: var(--ink);
  border-radius: 0; transition: border-color 0.15s; }
.inp:focus { outline: none; border-color: var(--ink); }
.inp:disabled { background: var(--paper-2); color: var(--ink-mute); }
textarea.inp { resize: vertical; min-height: 60px; }
select.inp { cursor: pointer; }

.muted { color: var(--ink-mute); font-size: 13px; font-style: italic; }

/* === AUTH SCREENS (login, install, force-pwd) ============ */
.auth-screen { min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 20px; background: var(--bg); }
.auth-card { width: 100%; max-width: 400px; padding: 32px;
  background: var(--paper); box-shadow: var(--shadow-paper); border-top: 3px solid var(--ink); }
.auth-brand { text-align: center; margin-bottom: 24px; color: var(--ink); }
.auth-brand svg { margin-bottom: 12px; color: var(--gold); }
.auth-title { font-size: 22px; font-weight: 700; font-family: 'Cormorant Garamond', serif; letter-spacing: 0.02em; }
.auth-subtitle { font-size: 12px; text-transform: uppercase; letter-spacing: 0.16em;
  color: var(--ink-mute); margin-top: 4px; }
.auth-sec { font-size: 11px; text-transform: uppercase; letter-spacing: 0.16em;
  color: var(--ink-mute); font-weight: 700; margin: 16px 0 6px; }
.auth-hint { font-size: 12.5px; color: var(--ink-soft); margin-bottom: 12px;
  font-family: 'Cormorant Garamond', serif; font-style: italic; }
.auth-error { padding: 10px 12px; background: #fdeeee; color: var(--wine);
  border-left: 3px solid var(--wine); font-size: 13px; margin: 10px 0; }
.auth-info { padding: 10px 12px; background: #eef3ed; color: var(--moss);
  border-left: 3px solid var(--moss); font-size: 13px; margin: 10px 0; }
.auth-secret { padding: 12px; background: var(--paper-2); border: 1px dashed var(--rule);
  font-family: 'JetBrains Mono', monospace; font-size: 14px; letter-spacing: 0.1em;
  text-align: center; margin-bottom: 6px; word-break: break-all; }

/* Biometric login */
.auth-divider { display: flex; align-items: center; gap: 10px; margin: 18px 0 12px;
  color: var(--ink-mute); font-size: 11px; text-transform: uppercase; letter-spacing: 0.2em; }
.auth-divider::before, .auth-divider::after {
  content: ''; flex: 1; height: 1px; background: var(--rule);
}
.btn.bio { width: 100%; background: var(--ink); color: #fff; border-color: var(--ink);
  display: flex; align-items: center; justify-content: center; gap: 8px;
  font-weight: 600; padding: 12px; }
.btn.bio:hover:not(:disabled) { background: var(--gold); border-color: var(--gold); }
.btn.bio:disabled { opacity: 0.5; cursor: not-allowed; }

/* === SHELL =============================================== */
.shell { min-height: 100vh; display: flex; flex-direction: column; }
/* Top bar: respects iOS safe-area-inset-top so the status bar in PWA
   standalone mode (apple-mobile-web-app-status-bar-style = black-translucent)
   doesn't overlap brand/icons. */
.top { display: flex; align-items: center; gap: 16px;
  padding: calc(12px + env(safe-area-inset-top)) calc(24px + env(safe-area-inset-right)) 12px calc(24px + env(safe-area-inset-left));
  background: var(--paper); border-bottom: 1px solid var(--rule);
  position: sticky; top: 0; z-index: 50; }
.top-l { display: flex; align-items: center; gap: 8px; color: var(--ink); }
.top-l svg { color: var(--gold); flex-shrink: 0; }
.top .brand { font-family: 'Cormorant Garamond', serif; font-size: 18px; font-weight: 600;
  white-space: nowrap; }
.top-nav { display: flex; gap: 4px; flex: 1; }
.top-nav button { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px;
  background: transparent; border: none; color: var(--ink-mute); font-size: 13px; font-weight: 600;
  border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap; }
.top-nav button:hover { color: var(--ink); }
.top-nav button.on { color: var(--ink); border-bottom-color: var(--gold); }
.top-r { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.top-r .who { font-size: 13px; color: var(--ink-soft); }

main { flex: 1; padding: 24px; }

/* === PAGES ============================================== */
.page { max-width: 1200px; margin: 0 auto; }
.page-h { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; color: var(--ink); }
.page-h svg { color: var(--gold); }
.page-h h1 { margin: 0; font-family: 'Cormorant Garamond', serif; font-size: 28px; font-weight: 600; }

.card { background: var(--paper); padding: 20px; margin-bottom: 16px;
  box-shadow: var(--shadow-sm); }
.card-title { font-size: 11px; text-transform: uppercase; letter-spacing: 0.16em;
  color: var(--ink-mute); font-weight: 700; margin-bottom: 12px; padding-bottom: 8px;
  border-bottom: 1px solid var(--rule); }

/* === TABLES ============================================= */
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th { text-align: left; padding: 10px 12px; font-size: 11px;
  text-transform: uppercase; letter-spacing: 0.12em; color: var(--ink-mute);
  font-weight: 700; border-bottom: 2px solid var(--rule); }
.tbl td { padding: 10px 12px; border-bottom: 1px solid var(--rule); vertical-align: middle; }
.tbl tr:hover td { background: var(--paper-2); }
.tbl code { font-family: 'JetBrains Mono', monospace; font-size: 12px;
  background: var(--paper-2); padding: 1px 6px; border-radius: 2px; }

.badge { display: inline-block; padding: 2px 8px; font-size: 10px;
  text-transform: uppercase; letter-spacing: 0.1em; font-weight: 700;
  background: var(--paper-2); color: var(--ink-mute); }
.badge.admin { background: var(--gold); color: #fff; }
.badge.doctor { background: var(--moss); color: #fff; }
.badge.ok { background: var(--moss); color: #fff; }
.badge.sm { font-size: 9px; padding: 1px 5px; }

.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; }
.dot.ok { background: var(--moss); }
.dot.off { background: var(--ink-mute); }
.dot.mfa { background: var(--gold); }
.dot.warn { background: var(--wine); }

/* === MODAL ============================================== */
.modal-bg { position: fixed; inset: 0; background: rgba(26, 36, 64, 0.6);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 100; }
.modal { width: 100%; max-width: 560px; max-height: 90vh; display: flex; flex-direction: column;
  background: var(--paper); box-shadow: var(--shadow-paper); }
.modal-h { display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--rule); }
.modal-title { font-family: 'Cormorant Garamond', serif; font-size: 20px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 8px; }
.modal-body { padding: 20px; overflow-y: auto; flex: 1; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end;
  padding: 14px 20px; border-top: 1px solid var(--rule); background: var(--paper-2); }

/* === UPLOAD ============================================= */
.upload-box {}
.upload-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.14em;
  color: var(--ink-mute); font-weight: 600; margin-bottom: 6px; }
.upload-empty { width: 100%; min-height: 130px; padding: 16px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  background: var(--paper-2); border: 1px dashed var(--rule); color: var(--ink-mute);
  cursor: pointer; transition: all 0.15s; }
.upload-empty:hover { background: var(--paper-3); border-color: var(--ink-mute); color: var(--ink); }
.upload-empty svg { color: var(--gold); }
.upload-preview { padding: 12px; background: #fff; border: 1px solid var(--rule);
  display: flex; flex-direction: column; align-items: center; gap: 10px; }
.upload-preview img { max-width: 100%; max-height: 90px; object-fit: contain; }
.upload-actions { display: flex; gap: 6px; }

/* === SECURITY ========================================== */
.sec-row { display: flex; justify-content: space-between; align-items: center;
  padding: 12px 0; border-bottom: 1px solid var(--rule); gap: 12px; }
.sec-row:last-child { border-bottom: none; }
.sec-t { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.sec-s { font-size: 12px; color: var(--ink-mute); margin-top: 2px; }

.cred-row { display: flex; justify-content: space-between; align-items: center;
  padding: 8px 12px; background: var(--paper-2); margin-bottom: 4px; font-size: 12px; }

.diag { padding: 10px 12px; background: var(--paper-2); margin-top: 8px;
  font-family: 'JetBrains Mono', monospace; font-size: 11px; line-height: 1.7; color: var(--ink-mute); }
.diag b { color: var(--ink); }

.ro-row { display: flex; padding: 8px 0; border-bottom: 1px solid var(--rule); font-size: 13px; }
.ro-row:last-child { border-bottom: none; }
.ro-l { width: 180px; color: var(--ink-mute); font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; font-weight: 600; }
.ro-v { flex: 1; }

/* === DOC EDITOR ========================================== */
.editor { max-width: 1400px; margin: 0 auto; }
.editor-toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 16px;
  flex-wrap: wrap; }
.seg { display: inline-flex; background: var(--paper); border: 1px solid var(--rule); }
.seg-b { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px;
  background: transparent; border: none; font-size: 13px; font-weight: 600;
  color: var(--ink-mute); cursor: pointer; }
.seg-b.on { background: var(--ink); color: #fff; }

.editor-grid { display: grid; grid-template-columns: 460px minmax(0, 1fr); gap: 20px; }
.editor-form { display: flex; flex-direction: column; gap: 12px; }
.editor-preview { background: var(--bg); padding: 16px; overflow: auto; }

/* MED PICKER */
.med-picker {}
.med-search-row { display: flex; gap: 6px; align-items: stretch; width: 100%; }
.med-search-row > .med-search-input { flex: 1 1 auto; min-width: 0; position: relative; }
.med-search-row > .med-search-input .inp { width: 100%; padding-left: 32px; }
.med-search-row > .med-search-input > svg {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  color: var(--ink-mute); pointer-events: none; z-index: 1;
}
/* Override .btn.primary width:100% so button only takes its content size here */
.med-search-row > .btn { flex: 0 0 auto; width: auto; padding: 0 16px; white-space: nowrap; }
.med-list { background: #fff; border: 1px solid var(--rule); max-height: 280px; overflow-y: auto; margin-top: 6px; }
.med-list-hint { padding: 6px 10px; background: var(--paper-2); font-size: 11px;
  color: var(--ink-mute); border-bottom: 1px solid var(--rule);
  font-family: 'Cormorant Garamond', serif; font-style: italic; }
.med-item { padding: 8px 12px; border-bottom: 1px solid var(--rule); cursor: pointer;
  transition: background 0.12s; }
.med-item:last-child { border-bottom: none; }
.med-item:hover { background: var(--paper-2); }
.med-name { font-weight: 600; font-size: 13px; }
.med-form { font-size: 11px; color: var(--ink-mute); margin-top: 2px; }
.med-no-match { padding: 10px 12px; margin-top: 6px;
  background: #fcf7e8; border-left: 3px solid var(--gold);
  font-size: 12px; color: var(--ink-soft); }
.med-no-match kbd { display: inline-block; padding: 1px 6px; margin: 0 2px;
  background: var(--paper-2); border: 1px solid var(--rule); border-radius: 3px;
  font-family: 'JetBrains Mono', monospace; font-size: 10px; }

.med-edit { padding: 12px; background: var(--paper-2); margin-top: 8px;
  border-left: 3px solid var(--gold); }
.med-edit-head { display: flex; justify-content: space-between; align-items: center;
  gap: 8px; margin-bottom: 8px; }
.med-edit-num { font-family: 'JetBrains Mono', monospace; font-size: 11px;
  font-weight: 700; color: var(--gold); background: var(--ink); padding: 2px 8px;
  border-radius: 2px; flex-shrink: 0; }
.med-edit-name-inp { font-weight: 700; font-size: 16px; }
.med-edit-name { font-weight: 700; font-size: 13px; }

/* LAB GRID */
.lab-search { display: flex; align-items: center; gap: 6px; padding: 8px 10px;
  background: var(--paper-2); border: 1px solid var(--rule); margin-bottom: 8px; }
.lab-search svg { color: var(--ink-mute); flex-shrink: 0; }
.lab-search .inp { border: none; background: transparent; padding: 4px 0; }
.lab-search .inp:focus { outline: none; }
.lab-actions { display: flex; gap: 6px; margin-bottom: 8px; }
.lab-scroll { max-height: 480px; overflow-y: auto; padding-right: 4px;
  border: 1px solid var(--rule); background: #fff; }
.lab-group { padding: 8px 8px 12px; border-bottom: 1px solid var(--rule); }
.lab-group:last-child { border-bottom: none; }
.lab-group-title { font-size: 10px; text-transform: uppercase; letter-spacing: 0.14em;
  color: var(--gold); font-weight: 700; padding: 4px 4px 6px;
  position: sticky; top: 0; background: #fff; z-index: 1; border-bottom: 1px solid var(--rule); }
.lab-grid { display: grid; grid-template-columns: minmax(0,1fr) minmax(0,1fr); gap: 4px; margin-top: 6px; }
@media (max-width: 640px) { .lab-grid { grid-template-columns: minmax(0,1fr); } }
.lab-chip { display: flex; align-items: center; gap: 6px; padding: 5px 8px;
  background: var(--paper-2); border: 1px solid var(--rule); cursor: pointer; font-size: 11.5px;
  line-height: 1.3; transition: all 0.1s; user-select: none; }
.lab-chip:hover { background: var(--paper-3); }
.lab-chip.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.lab-chip input { margin: 0; flex-shrink: 0; }
.lab-chip span { flex: 1; }

/* === PAPER (PDF preview) — EXACT A4 (794 × 1123 px = 210 × 297 mm @ 96dpi) ==
   Pixel dimensions (not mm) play more reliably with html2canvas + jsPDF.
   box-sizing:border-box ensures padding is INCLUDED in the A4 size, so the
   element is always exactly 794×1123 px = 1 A4 page. Without border-box,
   padding would add 120 px → element becomes 1243 px → jsPDF spills onto
   a 2nd blank page. */
.paper {
  width: 794px;
  min-height: 1123px;
  margin: 0 auto;
  background: #fff;
  padding: 50px 56px;
  box-sizing: border-box;
  box-shadow: var(--shadow-paper);
  position: relative;
}

/* Wrapper that scales the fixed-size .paper down to fit the screen.
   JS (useLayoutEffect) sets .paper transform:scale() and this wrapper's
   height before first paint. The CSS fallbacks below apply a rough
   scale on mobile widths in case JS is momentarily delayed — so the
   page is never wider than the screen even for a single frame. */
.paper-fit {
  width: 100%;
  margin: 0 auto;
  overflow: hidden;
}
@media (max-width: 850px) {
  .paper-fit > .paper { transform: scale(0.78); transform-origin: top left; }
}
@media (max-width: 640px) {
  .paper-fit > .paper { transform: scale(0.55); transform-origin: top left; }
}
@media (max-width: 430px) {
  .paper-fit > .paper { transform: scale(0.42); transform-origin: top left; }
}

.paper-head { display: flex; justify-content: space-between; gap: 30px; margin-bottom: 30px; }
.paper-head-l { font-family: 'Manrope', sans-serif; }
.paper-head-l .dr-name { font-weight: 700; font-size: 16px; margin-bottom: 6px; }
.paper-head-l .dr-addr { font-size: 12.5px; color: var(--ink-soft); line-height: 1.55; }
.paper-head-r { text-align: right; font-family: 'Cormorant Garamond', serif; font-style: italic; }
.paper-head-r .dr-spec { font-weight: 600; font-size: 14px; margin-bottom: 6px; }
.paper-head-r .dr-meta { font-size: 12.5px; color: var(--ink-soft); }

/* SINGLE-COLUMN header layout (cardiology/specialty style — Dr. Turchina) */
.paper-head-single { display: block; font-family: 'Manrope', sans-serif;
  margin-bottom: 30px; }
.paper-head-single .dr-name-big { font-weight: 700; font-size: 19px; line-height: 1.2; }
.paper-head-single .dr-spec-under { font-weight: 700; font-size: 16px;
  margin-left: 60px; margin-top: 1px; margin-bottom: 18px; }
.paper-head-single .dr-credentials { font-size: 12.5px; line-height: 1.55;
  color: var(--ink); margin-bottom: 14px; }
.paper-head-single .dr-credentials > div { margin: 0; }
.paper-head-single .dr-contact-block { font-size: 12.5px; line-height: 1.55;
  color: var(--ink); }

.barcodes { display: flex; gap: 30px; margin-bottom: 24px; }
.bc { display: flex; flex-direction: column; align-items: center; }
.bc-l { font-size: 9px; letter-spacing: 0.2em; font-weight: 700; color: var(--ink-mute); margin-bottom: 4px; }
.bc-v { font-family: 'JetBrains Mono', monospace; font-size: 10px; margin-top: 2px; }

.paper-meta { font-size: 13px; line-height: 1.7; margin-bottom: 20px; }
.paper-title { text-align: center; font-size: 18px; font-weight: 600;
  letter-spacing: 0.4em; margin: 30px 0; }

.paper-meds { display: flex; flex-direction: column; gap: 14px; margin-bottom: 30px; }
.paper-med {}
.paper-med-name { font-size: 14px; }
.paper-med-qty { font-weight: 600; color: var(--ink); font-size: 13px; }
.paper-med-pos { font-style: italic; font-family: 'Cormorant Garamond', serif;
  font-size: 13.5px; color: var(--ink-soft); margin-left: 12px; }
.paper-med-notes { color: var(--wine); }

.paper-labs { margin-bottom: 20px; }
.paper-lab-group { margin-bottom: 14px; }
.paper-lab-group:last-child { margin-bottom: 0; }
.paper-lab-cat { font-size: 10px; text-transform: uppercase; letter-spacing: 0.18em;
  font-weight: 700; color: var(--ink); border-bottom: 0.5px solid var(--rule);
  padding-bottom: 2px; margin-bottom: 4px; }
.paper-labs ul { margin: 0 0 4px 0; padding-left: 20px; }
.paper-labs li { padding: 1px 0; font-size: 12.5px; line-height: 1.4; }

.paper-referral { font-size: 13.5px; line-height: 1.6; }

/* STAMP + SIGNATURE (signature OVER stamp, as on real prescriptions) */
.stamp-block { margin-top: 36px; display: flex; justify-content: flex-end;
  padding-right: 8px; min-height: 140px; }
.stamp-frame { position: relative; display: inline-block; }
.stamp { text-align: center; transform: rotate(-1.5deg); padding: 8px 12px;
  position: relative; z-index: 1; }
.stamp .s1 { font: 700 13px/1.3 'Manrope'; }
.stamp .s2 { font: italic 500 11.5px/1.4 'Cormorant Garamond', serif; margin-top: 2px; }
.stamp .s3 { font: 400 11px/1.4 'Manrope'; margin-top: 2px; }
.stamp .s4 { font: 400 10px/1.4 'Manrope'; color: var(--ink-soft); margin-top: 4px; }
/* Image-based stamp (uploaded PNG/JPG) — typically scanned at ~375×215px
   for an A4 prescription. Caps width to keep proportions consistent. */
.stamp-image { display: block; max-width: 280px; width: 100%; height: auto;
  position: relative; z-index: 1; }
.signature-overlay { position: absolute; z-index: 2;
  top: 18px; left: 50%; transform: translateX(-50%) rotate(-3deg);
  pointer-events: none; }
.signature-overlay img { width: 170px; height: auto; display: block;
  /* The processed signature is already transparent + dark blue ink.
     No blend mode needed — just slight opacity for a natural pen look. */
  opacity: 0.92;
  filter: contrast(1.1) drop-shadow(0 0 0.4px rgba(20,35,80,0.3));
}

/* === RESPONSIVE ========================================= */
/* IMPORTANT: never apply CSS transforms to .paper — html2canvas
   captures the transformed (scaled-down) version which results in
   tiny PDFs. Instead, make the preview container scrollable so the
   user can pan around the true-size A4 page.                        */
/* On narrower screens the editor switches to a single column and the
   A4 preview is scaled down by JS (.paper-fit wrapper) to fit the
   viewport width. PDF generation is unaffected — it clones .paper at
   full 794px with transform:none. */
@media (max-width: 1024px) {
  .editor-grid { grid-template-columns: minmax(0, 1fr); }
  .editor-preview { overflow: visible; }
}

@media (max-width: 640px) {
  main { padding: 12px; }
  .top { padding: calc(8px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 8px calc(12px + env(safe-area-inset-left));
    gap: 8px; flex-wrap: wrap; }
  .top-nav { width: 100%; order: 3; overflow-x: auto; gap: 0; padding-bottom: 2px; }
  .top-nav button { padding: 6px 10px; font-size: 12px; }
  .top-r .who { display: none; }
  .top .brand { display: none; }   /* keep only the shield icon to save space */
  .auth-card { padding: 20px; }
  .modal { max-height: 100vh; }
  .tbl { font-size: 12px; }
  .tbl th, .tbl td { padding: 6px 8px; }
  /* Toolbar: doc-type selector fills row 1, PDF button drops to row 2 */
  .editor-toolbar > .seg { flex: 1 1 100%; }
  .editor-toolbar .seg-b { flex: 1; justify-content: center; padding: 8px 6px; font-size: 12px; }
  .editor-toolbar > div[style] { display: none; }   /* hide the flex spacer */
  .editor-toolbar > .btn { flex: 1 1 100%; justify-content: center; }
  /* Tables scroll within themselves instead of widening the page */
  .tbl { display: block; overflow-x: auto; white-space: nowrap;
    -webkit-overflow-scrolling: touch; }
  /* Profile read-only rows: smaller label, stacked if needed */
  .ro-l { width: 110px; flex-shrink: 0; }
  .ro-v { min-width: 0; }
  /* Cards / pages: tighter padding to maximise usable width */
  .card { padding: 16px; }
  .page-h h1 { font-size: 22px; }
  /* Paper stays exact A4 — the .paper-fit wrapper scales it to fit */
}

/* === PRINT ============================================= */
@page { margin: 0; size: A4; }
@media print {
  body { background: #fff; }
  .top, .editor-toolbar, .editor-form, .ico-btn, .seg, .editor-preview { display: none !important; }
  .editor-grid { display: block; }
  .paper { box-shadow: none; margin: 0; padding: 18mm 16mm; width: 210mm; min-height: 297mm; }
}
