@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:ital,wght@0,400;0,500;0,600;0,700;0,800;1,400&display=swap');

/* ============ Базовые / тема «Accessible & Ethical» (светлая, высокий контраст) ============ */
* { box-sizing: border-box; }
[hidden] { display: none !important; }   /* атрибут hidden должен побеждать display:flex у .field */
:root {
  --primary: #0F172A;       /* navy */
  --on-primary: #FFFFFF;
  --secondary: #334155;
  --accent: #0369A1;        /* CTA blue */
  --accent-hover: #075985;
  --bg: #F1F5F9;            /* page */
  --surface: #FFFFFF;       /* cards */
  --fg: #0F172A;            /* text */
  --muted-fg: #475569;      /* secondary text (4.5:1 on white) */
  --muted: #E8ECF1;
  --border: #CBD5E1;
  --border-strong: #94A3B8;
  --danger: #DC2626;
  --danger-bg: #FEF2F2;
  --ok: #15803D;
  --ring: #0369A1;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15,23,42,.06), 0 4px 16px rgba(15,23,42,.06);
  --tap: 48px;              /* минимальная высота интерактива */
}

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.5;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }

/* видимый фокус для клавиатуры/доступности */
:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; border-radius: 6px; }

.wrap {
  max-width: 1180px; margin: 0 auto;
  padding: 16px 16px calc(120px + env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}

.appbar {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--primary); color: var(--on-primary);
  position: sticky; top: 0; z-index: 30;
}
.appbar h1 { font-size: 17px; margin: 0; font-weight: 700; letter-spacing: -.01em; }
.appbar .sub { color: #CBD5E1; font-size: 12.5px; display: none; }
.appbar .spacer { flex: 1; }

/* Десктоп: две колонки. Моб-first => база одноколоночная */
.layout { display: grid; grid-template-columns: 1fr; gap: 18px; align-items: start; }
.layout > * { min-width: 0; }   /* не давать A4-предпросмотру растягивать колонку */

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 16px 20px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card h2 { font-size: 17px; margin: 0 0 6px; font-weight: 700; color: var(--primary); letter-spacing: -.01em; }
.card .hint { color: var(--muted-fg); font-size: 13.5px; margin: 0 0 14px; }
.card h2 + .hint { margin-top: -2px; }

/* На мобиле всё в один столбец, на широких — два/три */
.grid2, .grid3 { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 560px) {
  .grid2 { grid-template-columns: 1fr 1fr; }
  .grid3 { grid-template-columns: 1fr 1fr 1fr; }
}
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 14px; color: var(--fg); font-weight: 600; }
.field label .req { color: var(--danger); }

input[type=text], input[type=date], input[type=tel], input[type=email],
input[type=number], textarea, select {
  background: var(--surface); border: 1.5px solid var(--border); color: var(--fg);
  border-radius: 10px; padding: 12px 14px;
  font-size: 16px;                /* 16px => iOS не зумит при фокусе */
  font-family: inherit; width: 100%; min-height: var(--tap);
  transition: border-color .15s ease, box-shadow .15s ease;
}
input::placeholder, textarea::placeholder { color: #94A3B8; }
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(3,105,161,.18);
}
input:invalid:not(:placeholder-shown):not(:focus) { border-color: var(--border); }
textarea { resize: vertical; min-height: 64px; }

.checkrow { display: flex; align-items: center; gap: 12px; min-height: var(--tap); }
.checkrow input { width: 24px; height: 24px; accent-color: var(--accent); flex: 0 0 auto; }
.checkrow label { font-size: 15px; font-weight: 500; }

/* Загрузка файлов — крупная зона под палец */
.file-field { border: 1.5px dashed var(--border-strong); border-radius: 12px; padding: 14px; background: #F8FAFC; }
.file-field label.top { display: block; font-size: 14px; font-weight: 600; color: var(--fg); margin-bottom: 4px; }
.file-field .fhint { font-size: 12.5px; color: var(--muted-fg); margin-bottom: 10px; }
.file-field input[type=file] { font-size: 15px; width: 100%; }
.file-field input[type=file]::file-selector-button {
  font: inherit; font-weight: 600; background: var(--muted); color: var(--primary);
  border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; margin-right: 12px;
  min-height: 44px; cursor: pointer;
}
.file-field .preview { margin-top: 12px; }
.file-field .preview img { max-width: 100%; max-height: 220px; border-radius: 8px; border: 1px solid var(--border); display: block; }
.file-field .fname { font-size: 12.5px; color: var(--muted-fg); margin-top: 8px; word-break: break-all; }

/* Блок быстрого подбора модели */
.picker { background: #F0F7FC; border: 1px solid #BBDEFB; border-radius: 12px; padding: 14px; margin-bottom: 14px; }
.picker .hint { color: var(--secondary); }

/* Выбор «печать/подпись» — радио-карточки */
.choice { display: flex; flex-direction: column; gap: 10px; }
.opt { display: flex; align-items: flex-start; gap: 12px; padding: 14px;
  border: 1.5px solid var(--border); border-radius: 12px; cursor: pointer; background: var(--surface);
  transition: border-color .15s ease, background .15s ease; }
.opt:hover { border-color: var(--border-strong); }
.opt input[type=radio] { width: 22px; height: 22px; margin-top: 1px; accent-color: var(--accent); flex: 0 0 auto; }
.opt-body { display: flex; flex-direction: column; gap: 3px; }
.opt-title { font-weight: 700; font-size: 15.5px; color: var(--primary); }
.opt-desc { font-size: 13.5px; color: var(--muted-fg); line-height: 1.4; }
.opt:has(input:checked) { border-color: var(--accent); background: #F0F7FC; box-shadow: 0 0 0 2px rgba(3,105,161,.12) inset; }

/* Кнопки */
.btn {
  appearance: none; border: 1.5px solid var(--border); background: var(--surface); color: var(--primary);
  border-radius: 10px; padding: 12px 18px; font-size: 16px; cursor: pointer; font-weight: 700;
  min-height: var(--tap); display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  transition: background .15s ease, border-color .15s ease, transform .05s ease; touch-action: manipulation;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(.985); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { background: var(--surface); border-color: var(--danger); color: var(--danger); }
.btn.danger:hover { background: var(--danger-bg); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.mobile-only { display: inline-flex; }
@media (min-width: 900px) { .mobile-only { display: none !important; } }

/* Низ-бар с действиями (мобильный sticky) */
.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
  display: flex; flex-direction: row; gap: 10px; align-items: stretch;
  background: rgba(255,255,255,.96); backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
.actionbar .btn { flex: 1; min-width: 0; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted-fg); font-weight: 600; }

/* Тосты */
.toast { position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(8px);
  background: var(--primary); color: #fff; padding: 13px 20px; border-radius: 12px; font-size: 15px; font-weight: 600;
  box-shadow: 0 10px 30px rgba(0,0,0,.25); z-index: 60; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: 92vw; text-align: center; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* ---- Предпросмотр документа ---- */
.preview-col { display: none; }
.preview-col.open { display: block; }
.preview-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.preview-head h2 { font-size: 16px; margin: 0; color: var(--primary); }
.preview-frame { background: #64748B; border-radius: 12px; padding: 12px; overflow: auto; }

/* ============ Список анкет (админ) ============ */
.sub-list { display: flex; flex-direction: column; gap: 10px; }
.sub-item { display: flex; align-items: center; gap: 12px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px; padding: 14px; flex-wrap: wrap; }
.sub-item .si-main { flex: 1; min-width: 160px; }
.sub-item .si-name { font-weight: 700; }
.sub-item .si-meta { font-size: 13px; color: var(--muted-fg); margin-top: 2px; }
.sub-item .si-badge { font-size: 12px; padding: 3px 10px; border-radius: 20px; background: var(--muted); color: var(--secondary); font-weight: 600; }
.sub-item.need-sign { border-color: var(--accent); border-left: 4px solid var(--accent); }
.si-tag { font-size: 11.5px; font-weight: 700; padding: 2px 8px; border-radius: 6px; vertical-align: middle; white-space: nowrap; }
.si-tag.org { background: #E0F2FE; color: #075985; }
.si-tag.self { background: var(--muted); color: var(--secondary); }
.sub-item .btn { padding: 10px 14px; }
.empty { color: var(--muted-fg); padding: 28px; text-align: center; }
.keybar { display: flex; gap: 10px; margin-bottom: 18px; }

/* ============ Десктоп ============ */
@media (min-width: 900px) {
  .appbar .sub { display: inline; }
  .wrap { padding-bottom: 60px; }
  .layout { grid-template-columns: 1fr 1fr; gap: 24px; }
  .actionbar { position: static; flex-direction: row; align-items: center; gap: 10px; background: none; backdrop-filter: none; border: 0; padding: 0; margin-top: 4px; }
  .actionbar > .btn { flex: 0 0 auto; min-width: 160px; }
  .preview-col { display: block; position: sticky; top: 84px; }
  .preview-frame { max-height: 82vh; }
  .toast { bottom: 26px; }
}

/* ============ ДОКУМЕНТ (печать + предпросмотр) ============ */
.doc-root { font-family: 'Courier New', 'PT Mono', monospace; color: #000; }
.doc-page {
  background: #fff; color: #000;
  width: 210mm; min-height: 297mm; padding: 16mm 18mm 14mm;
  margin: 0 auto 16px; font-size: 11.2px; line-height: 1.32;
  box-shadow: 0 4px 18px rgba(0,0,0,.35);
}
.doc-page:last-child { margin-bottom: 0; }
.doc-root .appx { text-align: right; font-weight: 700; font-family: Georgia, 'Times New Roman', serif; font-size: 11.5px; line-height: 1.35; }
.doc-root .appx2 { text-align: right; font-weight: 700; font-family: Georgia, 'Times New Roman', serif; font-size: 11.5px; margin-top: 18px; }
.doc-h1 { text-align: center; font-size: 12.5px; font-weight: 700; margin: 34px 0 18px; }
.doc-h1 span { font-size: 12.5px; }
.doc-h2 { text-align: center; font-size: 12px; font-weight: 700; margin: 0 0 14px; }

.doc-root .row { display: flex; align-items: flex-end; gap: 4px; margin-top: 2px; }
.doc-root .lbl { white-space: normal; flex: 0 0 auto; }
.doc-root .v {
  flex: 1 1 auto; border-bottom: 1px solid #000; min-height: 1.25em;
  font-weight: 700; padding: 0 2px; white-space: pre-wrap; word-break: break-word;
}
.doc-root .cap { text-align: center; font-size: 10px; color: #000; line-height: 1.25; margin: 1px 0 4px; }
.doc-root .cap-r { text-align: right; padding-right: 4px; }
.doc-root .cap-sign { text-align: left; padding-left: 42%; }
.doc-root .row-note { margin: 12px 0 4px; }
.doc-p { text-align: justify; margin: 12px 0 4px; }
.doc-root .agree { text-align: center; margin: 14px 0 14px; }

.sign-row { align-items: flex-end; gap: 4px; margin-top: 10px; }
.sign-row .dt { border-bottom: 1px solid #000; min-width: 28px; text-align: center; font-weight: 700; padding: 0 4px; }
.sign-row .dt-l { border-bottom: 1px solid #000; min-width: 90px; text-align: center; font-weight: 700; padding: 0 6px; }
.sign-row .sign { flex: 1; border-bottom: 1px solid #000; text-align: center; font-weight: 700; padding: 0 6px; margin-left: 8px; }
.sign-blank { border-bottom: 1px solid #000; height: 1.3em; margin: 4px 0 0; }

.img-page { display: flex; flex-direction: column; align-items: center; }
.img-title { font-weight: 700; margin-bottom: 10px; font-size: 12px; align-self: flex-start; }
.passport-img { display: block; width: auto; height: auto; max-width: 100%; max-height: 245mm; object-fit: contain; margin: 0 auto; }
.pdf-note { font-size: 12px; }

/* Предпросмотр: масштабируем A4 под ширину контейнера через zoom (Safari iOS/Chrome) */
.preview-frame .doc-root { zoom: var(--ps, 0.62); }

/* Контейнер для генерации PDF (скачивание) — без теней и отступов между страницами */
.pdf-export .doc-page { box-shadow: none; margin: 0; }

/* В админке документы рендерятся в #printArea — на экране скрыт, виден только при печати */
#printArea { display: none; }

/* ============ ПЕЧАТЬ ============ */
@media print {
  body { background: #fff; }
  .no-print, .toast { display: none !important; }
  #printArea { display: block !important; }
  .preview-col { display: block !important; }
  .preview-frame { background: #fff; padding: 0; max-height: none; overflow: visible; }
  .preview-frame .doc-root { zoom: 1; }
  .doc-page {
    width: auto; min-height: auto; box-shadow: none; margin: 0;
    padding: 12mm 14mm; page-break-after: always;
  }
  .doc-page:last-child { page-break-after: auto; }
  @page { size: A4; margin: 0; }
}

/* ============ Уважение к prefers-reduced-motion ============ */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}
