/* ==========================================================
   شهادات فلو — نظام التصميم (Design Tokens)
   ========================================================== */
:root {
  --ink: #14202f;
  --ink-soft: #48586a;
  --flow-teal: #0e6e64;
  --flow-teal-dark: #0a4f48;
  --gold: #c69b3d;
  --gold-soft: #e7cf9a;
  --paper: #fbf8f2;
  --app-bg: #eef2f1;
  --panel-bg: #ffffff;
  --cta: #d65f46;
  --cta-dark: #b94a33;
  --line: #dfe6e4;

  --font-display: "Cairo", sans-serif;
  --font-body: "Tajawal", sans-serif;

  --cert-w: 297mm;
  --cert-h: 210mm;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--app-bg);
  color: var(--ink);
  font-family: var(--font-body);
}

button, input { font-family: inherit; }

/* ==========================================================
   شريط علوي
   ========================================================== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 34px; height: 34px; flex-shrink: 0; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong { font-family: var(--font-display); font-weight: 800; font-size: 18px; color: var(--flow-teal-dark); }
.brand-text span { font-size: 11px; color: var(--ink-soft); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: none;
  border-radius: 10px;
  padding: 11px 18px;
  font-weight: 700;
  font-size: 14.5px;
  cursor: pointer;
  transition: transform .15s ease, box-shadow .15s ease, background .15s ease;
}
.btn-primary {
  background: var(--cta);
  color: #fff;
  box-shadow: 0 4px 14px rgba(214, 95, 70, .28);
}
.btn-primary:hover { background: var(--cta-dark); transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }

/* ==========================================================
   الهيكل العام
   ========================================================== */
.app-shell {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 20px;
  padding: 20px;
  align-items: start;
  min-height: calc(100vh - 130px);
}

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 22px;
}

.panel-title {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 19px;
  color: var(--ink);
  margin: 0 0 4px;
}
.panel-hint {
  font-size: 13px;
  color: var(--ink-soft);
  margin: 0 0 18px;
  line-height: 1.6;
}

/* ==========================================================
   الفورم
   ========================================================== */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field span { font-size: 13px; font-weight: 700; color: var(--ink); }
.field span em { color: var(--cta); font-style: normal; }
.field input {
  border: 1.5px solid var(--line);
  border-radius: 9px;
  padding: 10px 12px;
  font-size: 14.5px;
  color: var(--ink);
  background: #fdfdfc;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.field input:focus {
  outline: none;
  border-color: var(--flow-teal);
  box-shadow: 0 0 0 3px rgba(14, 110, 100, .12);
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.template-picker { margin-top: 22px; border-top: 1px solid var(--line); padding-top: 18px; }
.template-picker h3 { font-family: var(--font-display); font-size: 15px; font-weight: 800; margin: 0 0 12px; }
.template-list { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.template-card {
  border: 2px solid var(--line);
  border-radius: 12px;
  background: #fff;
  padding: 8px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  transition: border-color .15s ease, transform .15s ease;
}
.template-card:hover { transform: translateY(-2px); }
.template-card.is-active { border-color: var(--flow-teal); box-shadow: 0 0 0 3px rgba(14,110,100,.12); }

.template-thumb-wrap {
  width: 100%;
  aspect-ratio: 297 / 210;
  overflow: hidden;
  border-radius: 6px;
  background: var(--paper);
  position: relative;
}
.template-thumb {
  position: absolute;
  top: 0; left: 0;
  width: var(--cert-w);
  height: var(--cert-h);
  transform: scale(0.13);
  transform-origin: top left;
  pointer-events: none;
}
.template-name { font-size: 12.5px; font-weight: 700; color: var(--ink-soft); }

/* ==========================================================
   لوحة المعاينة
   ========================================================== */
.preview-panel {
  padding: 24px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow: hidden;
}
.preview-scaler {
  position: relative;
  width: 100%;
  max-width: 1000px;
  aspect-ratio: 297 / 210;
}

.certificate-page {
  position: absolute;
  top: 0;
  left: 50%;
  width: var(--cert-w);
  height: var(--cert-h);
  transform-origin: top center;
  box-shadow: 0 18px 50px rgba(20, 32, 47, .18);
  border-radius: 3px;
  overflow: hidden;
}

/* ==========================================================
   قالب: الكلاسيكي الفاخر (classic-teal)
   ========================================================== */
.tpl { position: relative; width: 100%; height: 100%; background: var(--paper); }

.tpl-frame-outer {
  position: absolute; inset: 7mm;
  border: 1.6pt solid var(--flow-teal);
}
.tpl-frame-inner {
  position: absolute; inset: 10.5mm;
  border: 0.9pt solid var(--gold);
}

.tpl-corner { position: absolute; width: 16mm; height: 16mm; pointer-events: none; }
.tpl-corner-tr { top: 7mm; right: 7mm; border-top: 2pt solid var(--gold); border-right: 2pt solid var(--gold); }
.tpl-corner-tl { top: 7mm; left: 7mm; border-top: 2pt solid var(--gold); border-left: 2pt solid var(--gold); }
.tpl-corner-br { bottom: 7mm; right: 7mm; border-bottom: 2pt solid var(--gold); border-right: 2pt solid var(--gold); }
.tpl-corner-bl { bottom: 7mm; left: 7mm; border-bottom: 2pt solid var(--gold); border-left: 2pt solid var(--gold); }

.tpl-content {
  position: relative;
  height: 100%;
  padding: 18mm 24mm 14mm;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.tpl-mark { width: 11mm; height: 11mm; margin-bottom: 3mm; }
.tpl-mark svg { width: 100%; height: 100%; }
.tpl-mark-logo { width: 100%; height: 100%; object-fit: contain; }

.tpl-eyebrow {
  font-size: 3.6mm;
  font-weight: 700;
  color: var(--flow-teal-dark);
  letter-spacing: .5px;
  margin: 0 0 2mm;
}

.tpl-title {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 15mm;
  color: var(--ink);
  margin: 0;
  letter-spacing: 1px;
}

.tpl-flourish { width: 55mm; margin: 2mm 0 5mm; }
.tpl-flourish svg { width: 100%; height: 5mm; }

.tpl-lead {
  font-size: 4.4mm;
  color: var(--ink-soft);
  margin: 0 0 4mm;
}

.tpl-name {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 11mm;
  color: var(--flow-teal-dark);
  margin: 0 0 5mm;
  padding-bottom: 3mm;
  border-bottom: 0.8pt solid var(--gold);
  display: inline-block;
  min-width: 60%;
}

.tpl-body {
  font-size: 4.6mm;
  line-height: 1.9;
  color: var(--ink);
  max-width: 190mm;
  margin: 0 0 auto;
}
.tpl-course { color: var(--flow-teal-dark); font-weight: 700; }

.tpl-meta-row {
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding-top: 6mm;
  margin-top: 6mm;
}
.tpl-meta { display: flex; flex-direction: column; gap: 1.5mm; flex: 1; }
.tpl-meta-center { align-items: center; }
.tpl-meta:last-child { align-items: flex-end; }
.tpl-meta-label { font-size: 3mm; color: var(--ink-soft); font-weight: 700; }
.tpl-meta-value { font-size: 3.8mm; font-weight: 700; color: var(--ink); }

.tpl-signatures {
  width: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  margin-top: 8mm;
}
.tpl-sign { display: flex; flex-direction: column; align-items: center; gap: 1.5mm; width: 65mm; }
.tpl-sign-line { width: 100%; border-top: 0.7pt solid var(--ink-soft); margin-bottom: 1mm; }
.tpl-sign-label { font-size: 3.6mm; font-weight: 700; color: var(--ink); }
.tpl-sign-role { font-size: 2.8mm; color: var(--ink-soft); }
.tpl-seal { width: 20mm; height: 20mm; margin-bottom: 2mm; }
.tpl-seal svg { width: 100%; height: 100%; }

.tpl-watermark {
  position: absolute;
  bottom: 3mm; left: 0; right: 0;
  text-align: center;
  font-size: 2.6mm;
  color: var(--ink-soft);
  opacity: .45;
}

.fld { display: inline; }
.fld.is-placeholder { color: #a9b4ba; font-style: italic; }

/* ==========================================================
   رفع لوجو الجهة المانحة (فورم)
   ========================================================== */
.logo-field { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.logo-preview {
  width: 52px; height: 52px;
  border: 1.5px dashed var(--line);
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  background: var(--paper);
  flex-shrink: 0;
}
.logo-preview img { width: 100%; height: 100%; object-fit: contain; }
.logo-preview.is-empty::after {
  content: "لا يوجد";
  font-size: 10px;
  color: var(--ink-soft);
}
.logo-actions { display: flex; flex-direction: column; gap: 6px; }
.logo-actions .btn-upload {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--flow-teal);
  background: rgba(14,110,100,.08);
  border: none;
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  width: fit-content;
}
.logo-actions .btn-remove {
  font-size: 12px;
  color: var(--cta);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  width: fit-content;
  text-decoration: underline;
}
.logo-hint { font-size: 11px; color: var(--ink-soft); margin: 2px 0 0; }

/* ==========================================================
   قالب: العصري بالشريط الجانبي (modern-sidebar)
   ========================================================== */
.tpl-modern-sidebar,
.tpl.tpl-modern-sidebar {
  background: var(--paper);
  display: flex;
  align-items: stretch;
}

.tpl-ms-panel {
  width: 78mm;
  flex-shrink: 0;
  background: linear-gradient(165deg, var(--flow-teal-dark), var(--flow-teal));
  color: #fff;
  padding: 16mm 12mm;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  text-align: right;
}

.tpl-ms-mark { width: 16mm; height: 16mm; margin-bottom: 10mm; }
.tpl-ms-mark svg { width: 100%; height: 100%; }
.tpl-ms-mark svg path { stroke: #fff; }
.tpl-ms-mark svg circle { fill: var(--gold); }
.tpl-ms-mark .tpl-mark-logo {
  width: 100%; height: 100%;
  object-fit: contain;
  background: #fff;
  border-radius: 6px;
  padding: 2mm;
}

.tpl-ms-side-info { display: flex; flex-direction: column; gap: 6mm; width: 100%; }
.tpl-ms-side-item { display: flex; flex-direction: column; gap: 1.2mm; }
.tpl-ms-side-label { font-size: 2.8mm; color: var(--gold-soft); font-weight: 700; letter-spacing: .3px; }
.tpl-ms-side-value { font-size: 3.8mm; font-weight: 700; color: #fff; }

.tpl-ms-side-brand {
  margin-top: auto;
  padding-top: 10mm;
  font-size: 2.8mm;
  color: rgba(255,255,255,.6);
}

.tpl-ms-main {
  flex: 1;
  padding: 20mm 22mm;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  position: relative;
}

.tpl-ms-eyebrow {
  font-size: 3.6mm;
  font-weight: 700;
  color: var(--flow-teal-dark);
  letter-spacing: .5px;
  margin: 0 0 3mm;
}

.tpl-ms-title {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 13mm;
  color: var(--ink);
  margin: 0 0 10mm;
  letter-spacing: 1px;
}

.tpl-ms-name {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 12mm;
  color: var(--flow-teal-dark);
  margin: 0 0 6mm;
  padding-bottom: 3mm;
  border-bottom: 0.8pt solid var(--gold);
  display: inline-block;
  min-width: 65%;
}

.tpl-ms-body {
  font-size: 4.6mm;
  line-height: 1.9;
  color: var(--ink);
  max-width: 150mm;
  margin: 0 0 auto;
}
.tpl-ms-course { color: var(--flow-teal-dark); font-weight: 700; }

.tpl-ms-signatures {
  width: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: space-around;
  margin-top: 10mm;
}
.tpl-ms-sign { display: flex; flex-direction: column; align-items: center; gap: 1.5mm; width: 65mm; }
.tpl-ms-sign-line { width: 100%; border-top: 0.7pt solid var(--ink-soft); margin-bottom: 1mm; }
.tpl-ms-sign-label { font-size: 3.6mm; font-weight: 700; color: var(--ink); }
.tpl-ms-sign-role { font-size: 2.8mm; color: var(--ink-soft); }

/* أنيميشن ظهور الشهادة عند توليدها/تبديل القالب — إشارة "التدفق" */
@keyframes flowReveal {
  0% { clip-path: inset(0 0 0 100%); opacity: .4; }
  100% { clip-path: inset(0 0 0 0%); opacity: 1; }
}
.certificate-page.is-revealing .tpl { animation: flowReveal .65s cubic-bezier(.22,.61,.36,1); }

@media (prefers-reduced-motion: reduce) {
  .certificate-page.is-revealing .tpl { animation: none; }
}

/* ==========================================================
   تذييل
   ========================================================== */
.app-footer {
  text-align: center;
  font-size: 12.5px;
  color: var(--ink-soft);
  padding: 16px;
}

/* ==========================================================
   الاستجابة (موبايل/تابلت)
   ========================================================== */
@media (max-width: 980px) {
  .app-shell { grid-template-columns: 1fr; }
  .preview-panel { order: -1; padding: 14px; }
  .template-list { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 520px) {
  .topbar { padding: 12px 16px; }
  .btn-primary span { display: none; }
  .field-row { grid-template-columns: 1fr; }
}

/* ==========================================================
   الطباعة / تصدير PDF
   ========================================================== */
@page { size: A4 landscape; margin: 0; }

@media print {
  html, body { background: #fff; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; color-adjust: exact; }
  .topbar, .app-footer, .form-panel { display: none !important; }
  .app-shell { display: block; padding: 0; min-height: 0; }
  .preview-panel { display: block; padding: 0; overflow: visible; }
  .preview-scaler { width: var(--cert-w); height: var(--cert-h); max-width: none; aspect-ratio: auto; }
  .certificate-page {
    position: static;
    transform: none !important;
    box-shadow: none;
    border-radius: 0;
    width: var(--cert-w);
    height: var(--cert-h);
  }
}
