/* Custom Moroccan Maghrebi Font: Arabswell 1 */
@font-face {
  font-family: 'Arabswell 1';
  src: url('arabswell-1.woff2') format('woff2'),
       url('arabswell-1.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* Official Moroccan Ministerial Circulars Font: Simplified Arabic */
@font-face {
  font-family: 'Simplified Arabic';
  src: url('simplified-arabic.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Simplified Arabic';
  src: url('simplified-arabic-bold.ttf') format('truetype');
  font-weight: bold;
  font-style: normal;
  font-display: swap;
}

/* Traditional Arabic Backup */
@font-face {
  font-family: 'Traditional Arabic';
  src: url('traditional-arabic.ttf') format('truetype');
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: 'Traditional Arabic';
  src: url('traditional-arabic-bold.ttf') format('truetype');
  font-weight: bold;
  font-style: normal;
  font-display: swap;
}

/* styles.css - مولد التقارير التربوية الرسمية الذكي المدعوم بـ Gemini AI */

:root {
  --bg-app: #10202d;
  --surface: #182837;
  --surface-border: #233748;
  --text-main: #f1f5f9;
  --text-muted: #94a3b8;
  --brand-navy: #f1f5f9;
  --brand-teal: #0d9488;
  --brand-coral: #e5242a;
  --brand-gold: #d97706;
  --brand-purple: #7c3aed;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.5);
  --radius-lg: 4px;
  --radius-md: 4px;
  --radius-sm: 4px;
  --font-family: 'Cairo', system-ui, -apple-system, sans-serif;
  --font-calligraphy: 'Amiri', 'Cairo', serif;
  --report-font-size: 14pt;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-family);
  background: var(--bg-app);
  color: var(--text-main);
  min-height: 100vh;
  line-height: 1.6;
}

/* App Header */
.app-header {
  background: #182837;
  border-bottom: 1px solid #233748;
  padding: 0.85rem 1.5rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  max-width: 1500px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 1rem;
}

.brand-wrapper {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.back-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 4px;
  background: #10202d;
  color: #f1f5f9;
  text-decoration: none;
  font-size: 1.1rem;
  font-weight: bold;
  border: 1px solid #233748;
  transition: all 0.2s;
}

.back-link:hover {
  background: #182837;
  border-color: #3b556e;
}

.brand-title {
  font-size: 1.25rem;
  font-weight: 900;
  color: #ffffff;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.app-top-tag {
  background: #ffffff !important;
  color: #06264a !important;
  font-weight: 800 !important;
  font-size: 13px !important;
  padding: 4px 14px !important;
  border-radius: 2px !important;
  display: inline-block !important;
}

.brand-badge {
  font-size: 0.72rem;
  background: #10202d;
  color: #38bdf8;
  border: 1px solid #233748;
  padding: 0.2rem 0.65rem;
  border-radius: 4px;
  font-weight: 800;
}

.brand-sub {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-wrap: wrap;
}

/* Standardized Buttons (Matching Screenshots 1 & 2) */
.btn {
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 700;
  padding: 8px 16px;
  border-radius: 4px !important;
  border: 1px solid #233748 !important;
  background: #10202d !important;
  color: #ffffff !important;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.15s ease;
  box-shadow: none !important;
  text-decoration: none;
}

.btn:hover {
  background: #182837 !important;
  border-color: #3b556e !important;
  color: #ffffff !important;
  transform: none !important;
}

.btn-outline {
  background: #10202d !important;
  color: #ffffff !important;
  border: 1px solid #233748 !important;
}

.btn-primary,
.btn-ai,
#btnDownloadPdf,
#btnSaveFixedInfo,
#btnAiGenerateFull {
  background: #10202d !important;
  color: #ffffff !important;
  border: 1px solid #233748 !important;
  border-right: 3px solid #e5242a !important;
  font-weight: 800 !important;
  border-radius: 4px !important;
}

.btn-primary:hover,
.btn-ai:hover,
#btnDownloadPdf:hover,
#btnSaveFixedInfo:hover,
#btnAiGenerateFull:hover {
  background: #182d3f !important;
  border-color: #3b556e !important;
  border-right: 3px solid #e5242a !important;
}

.btn-danger {
  background: #3a151b !important;
  color: #fca5a5 !important;
  border: 1px solid #7f1d1d !important;
  border-radius: 4px !important;
}

/* Layout */
.app-container {
  max-width: 1560px;
  margin: 1.5rem auto;
  padding: 0 1rem;
  display: grid;
  grid-template-columns: 410px 1fr;
  gap: 1.5rem;
  align-items: start;
}

@media (max-width: 1180px) {
  .app-container {
    grid-template-columns: 1fr;
  }
}

/* Sidebar Panel */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  position: sticky;
  top: 90px;
}

.panel-card {
  background: #182837 !important;
  border: 1px solid #233748 !important;
  border-radius: 4px !important;
  padding: 1.25rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.panel-title {
  font-size: 1rem;
  font-weight: 800;
  color: #f1f5f9 !important;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.85rem;
  padding: 4px 10px 4px 0 !important;
  border-right: 3px solid #e5242a !important;
  border-bottom: none !important;
}

.field-group {
  margin-bottom: 0.85rem;
}

.field-group:last-child {
  margin-bottom: 0;
}

.field-label {
  display: block;
  font-size: 0.82rem;
  font-weight: 700;
  color: #f1f5f9 !important;
  margin-bottom: 0.35rem;
}

.input-control, .select-control, .textarea-control {
  width: 100%;
  font-family: inherit;
  font-size: 0.86rem;
  padding: 0.55rem 0.75rem;
  border: 1px solid #1f3347 !important;
  border-radius: 4px !important;
  background: #0c1924 !important;
  color: #f1f5f9 !important;
  transition: all 0.2s;
}

.input-control:focus, .select-control:focus, .textarea-control:focus {
  outline: none;
  border-color: #38bdf8 !important;
  background: #0c1924 !important;
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.2) !important;
}

.textarea-control {
  resize: vertical;
  min-height: 80px;
}

/* AI Panel Box (Matching Screenshot 3) */
.ai-box {
  background: #142433 !important;
  border: 1px solid #233748 !important;
  border-radius: 4px !important;
  padding: 1rem;
}

.ai-status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.78rem;
  color: #38bdf8;
  font-weight: 700;
  margin-bottom: 0.75rem;
}

.ai-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #10b981;
  box-shadow: 0 0 8px #10b981;
}

.ai-btn-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

/* Presets List in Sidebar (Matching Screenshot 4) */
.preset-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 340px;
  overflow-y: auto;
  padding-left: 0.25rem;
}

.preset-item {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.6rem 0.75rem;
  border-radius: 4px !important;
  border: 1px solid #233748 !important;
  background: #10202d !important;
  color: #f1f5f9 !important;
  cursor: pointer;
  transition: all 0.15s;
  text-align: right;
  width: 100%;
}

.preset-item:hover {
  background: #182837 !important;
  border-color: #3b556e !important;
}

.preset-item.active {
  background: #182837 !important;
  border-color: #233748 !important;
  border-right: 3px solid #e5242a !important;
}

.preset-icon {
  font-size: 1.25rem;
  flex-shrink: 0;
}

.preset-name {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--brand-navy);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.preset-tag {
  font-size: 0.68rem;
  background: rgba(13, 148, 136, 0.1);
  color: var(--brand-teal);
  padding: 0.1rem 0.45rem;
  border-radius: 4px;
  font-weight: 700;
}

/* Photo Upload Box */
.photo-uploader {
  border: 1px dashed #233748 !important;
  border-radius: 4px !important;
  padding: 1rem;
  text-align: center;
  background: #0c1924 !important;
  color: #f1f5f9 !important;
  cursor: pointer;
  transition: all 0.2s;
}

.photo-uploader:hover {
  border-color: #38bdf8 !important;
  background: #142433 !important;
}

.photo-preview-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.photo-thumb {
  position: relative;
  aspect-ratio: 1;
  border-radius: 4px !important;
  overflow: hidden;
  border: 1px solid #233748 !important;
}

.photo-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.photo-thumb-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  background: rgba(220, 38, 38, 0.85);
  color: white;
  border: none;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 11px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Workspace & A4 Sheet */
.workspace {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.sheet-toolbar {
  background: #182837 !important;
  border: 1px solid #233748 !important;
  border-radius: 4px !important;
  padding: 0.45rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3) !important;
  font-size: 0.85rem;
  font-weight: 700;
  color: #f1f5f9 !important;
  width: 210mm;
  max-width: 100%;
  box-sizing: border-box;
}

.toolbar-info span strong {
  color: #38bdf8 !important;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.sheet-placeholder {
  color: #94a3b8 !important;
  font-style: italic;
  user-select: text;
  transition: color 0.2s ease, opacity 0.2s ease;
}

.sheet-placeholder:hover {
  color: #64748b !important;
}

.font-scale-label {
  color: #cbd5e1;
  font-size: 12.5px;
  font-weight: 700;
  white-space: nowrap;
}

.font-scale-select {
  background: #10202d !important;
  color: #ffffff !important;
  border: 1px solid #233748 !important;
  border-radius: 4px !important;
  font-size: 12.5px;
  font-weight: 700;
  padding: 4px 8px;
  cursor: pointer;
  outline: none;
  transition: all 0.2s ease;
}

.font-scale-select:hover,
.font-scale-select:focus {
  border-color: var(--brand-teal) !important;
}

.btn-page2-toggle {
  background: #10202d !important;
  color: #ffffff !important;
  border: 1px solid #233748 !important;
  border-radius: 4px !important;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 6px 14px !important;
}

.btn-page2-toggle:hover {
  background: #182837 !important;
  border-color: #3b556e !important;
}

.btn-page2-toggle.active {
  background: #3a151b !important;
  color: #fca5a5 !important;
  border-color: #7f1d1d !important;
}

/* ==========================================================================
   A4 SHEET - STANDALONE OFFICIAL DOCUMENT (210mm x 297mm)
   ========================================================================== */
.a4-sheet-wrapper {
  box-shadow: var(--shadow-lg);
  border-radius: 4px;
  overflow: visible;
  background: #fff;
  transition: transform 0.2s;
}

.a4-sheet {
  width: 210mm;
  min-height: 297mm;
  padding: 15mm 18mm 14mm 18mm;
  background: #ffffff;
  color: #111827;
  position: relative;
  box-sizing: border-box;
  font-family: 'Amiri', 'Traditional Arabic', serif;
  font-size: var(--report-font-size, 14pt);
  line-height: 1.85;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

/* Official Ministry Header Layout */
.official-header {
  border-bottom: 2px solid #06264a;
  padding-bottom: 12px;
  margin-bottom: 14px;
  text-align: center;
}

.header-logo-row {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 8px;
  width: 100%;
}

.header-logo-row img {
  width: 440px;
  max-width: 95%;
  height: auto;
  display: block;
  margin: 0 auto;
  object-fit: contain;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.04));
}

.header-regional-center {
  text-align: center;
  margin-top: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.academy-name {
  font-family: 'Amiri', 'Traditional Arabic', serif;
  font-size: 16pt;
  font-weight: 700;
  color: #06264a;
  line-height: 1.35;
}

.directorate-name {
  font-family: 'Amiri', 'Traditional Arabic', serif;
  font-size: 14.5pt;
  font-weight: 700;
  color: #06264a;
  line-height: 1.35;
}

/* Document Title Bar */
.report-date-badge {
  text-align: left;
  font-size: 13pt;
  font-weight: 700;
  color: #334155;
  margin-bottom: 5px;
  direction: rtl;
}

/* Report Title (Text Only, Centered) */
.report-main-title {
  text-align: center;
  font-family: 'Amiri', 'Traditional Arabic', serif;
  font-size: 20pt;
  font-weight: 700;
  color: #06264a;
  line-height: 1.35;
  margin: 6px 0 14px 0;
  padding: 0;
}

/* Report Subject Line (Text Only, No Box) */
.report-subject-line {
  font-family: 'Amiri', 'Traditional Arabic', serif;
  font-size: 14.5pt;
  line-height: 1.7;
  color: #06264a;
  margin-bottom: 14px;
}

.subject-label {
  font-weight: 700;
  color: #06264a;
  margin-left: 6px;
  text-decoration: underline;
}

/* Body Content */
.report-body-text {
  font-size: var(--report-font-size, 14pt);
  text-align: justify;
  line-height: 1.85;
  color: #1e293b;
  margin-bottom: 12px;
  text-indent: 2em;
}

/* Activities List */
.activities-container {
  margin-bottom: 12px;
}

.section-subhead {
  font-size: 14.5pt;
  font-weight: 700;
  color: #06264a;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.activities-list {
  list-style: none;
  font-size: var(--report-font-size, 14pt);
  color: #1e293b;
  padding-right: 8px;
}

.activities-list li {
  position: relative;
  padding-right: 20px;
  margin-bottom: 4px;
  line-height: 1.75;
  text-align: justify;
}

.activities-list li::before {
  content: '–';
  position: absolute;
  right: 0;
  color: #06264a;
  font-weight: 700;
  font-size: var(--report-font-size, 14pt);
}

/* ==========================================================================
   Page 2: Activity Photos Annex (ملحق التوثيق المصور للنشاط)
   ========================================================================== */
.page2-wrapper {
  margin-top: 1.5rem;
  transition: all 0.3s ease;
}

.a4-sheet-page2 {
  min-height: 297mm;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.page2-title-section {
  text-align: center;
  margin-bottom: 10px;
}

.page2-photos-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  margin: 6px 0;
}

.page2-photos-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
  width: 100%;
}

.page2-photo-card {
  display: flex;
  flex-direction: column;
  background: transparent;
  border: none;
  border-radius: 0;
  overflow: visible;
  box-shadow: none;
}

.page2-photo-img-wrap {
  position: relative;
  width: 100%;
  height: 66mm;
  background: #f8fafc;
  overflow: hidden;
  border: 1px solid #64748b;
  border-radius: 0;
}

.page2-photo-img-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.page2-btn-remove-photo {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(220, 38, 38, 0.88);
  color: #ffffff;
  border: none;
  border-radius: 50%;
  width: 26px;
  height: 26px;
  font-size: 13px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  z-index: 10;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}

.page2-btn-remove-photo:hover {
  background: #dc2626;
  transform: scale(1.1);
}

.page2-photo-caption {
  padding: 6px 4px 2px 4px;
  font-family: 'Amiri', 'Traditional Arabic', serif;
  font-size: var(--report-font-size, 14pt);
  font-weight: 700;
  line-height: 1.5;
  color: #06264a;
  text-align: center;
  background: transparent;
  border: none;
  min-height: 32px;
  outline: none;
  cursor: text;
  box-sizing: border-box;
}

.page2-photo-caption:empty::before {
  content: attr(data-placeholder);
  color: #94a3b8;
  font-style: italic;
  font-size: 12.5pt;
  font-weight: normal;
}

.page2-photo-caption:hover {
  background: rgba(13, 148, 136, 0.05);
  border-radius: 2px;
}

.page2-photo-caption:focus {
  background: #ffffff;
  box-shadow: inset 0 0 0 1px var(--brand-teal);
  border-radius: 2px;
}

/* Upload Slot Card inside Page 2 Grid */
.page2-upload-slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1.5px dashed #94a3b8;
  border-radius: 0;
  background: #fafbfc;
  min-height: 75mm;
  cursor: pointer;
  transition: all 0.2s;
  padding: 1.5rem 1rem;
  text-align: center;
  gap: 0.25rem;
}

.page2-upload-slot:hover {
  border-color: var(--brand-teal);
  background: rgba(13, 148, 136, 0.04);
}

.upload-slot-icon {
  font-size: 2.2rem;
  margin-bottom: 0.25rem;
  color: var(--brand-teal);
}

.upload-slot-title {
  font-size: 16px;
  font-weight: 700;
  color: #06264a;
  font-family: 'Amiri', 'Traditional Arabic', serif;
}

.upload-slot-hint {
  font-size: 12.5px;
  color: #64748b;
}

/* Page 2 Footer Elements */
.page2-footer-box {
  margin-top: 24px;
  margin-bottom: 15px;
  padding-top: 10px;
  border-top: 1px dashed #cbd5e1;
}

.page2-page-indicator {
  font-size: 13.5pt;
  color: #64748b;
  font-family: 'Amiri', 'Traditional Arabic', serif;
  font-weight: normal;
}

/* Supervison & Beneficiaries */
.info-strip {
  font-size: var(--report-font-size, 14pt);
  color: #1e293b;
  margin-bottom: 5px;
  line-height: 1.7;
}

/* Signatures Block (Simple Official Footer) */
.signatures-box {
  margin-top: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: 'Amiri', 'Traditional Arabic', serif;
  font-size: 14.5pt;
  font-weight: 700;
  color: #06264a;
  padding: 0 12px;
  margin-bottom: 20px;
}

.sign-item {
  direction: rtl;
  user-select: text;
}

/* Contenteditable Styling */
[contenteditable="true"] {
  outline: none;
  cursor: text;
  transition: all 0.2s;
  border-radius: 3px;
  padding: 1px 3px;
}

[contenteditable="true"]:hover {
  background: rgba(13, 148, 136, 0.08);
  box-shadow: 0 0 0 1px rgba(13, 148, 136, 0.3);
}

[contenteditable="true"]:focus {
  background: rgba(13, 148, 136, 0.12);
  box-shadow: 0 0 0 2px var(--brand-teal);
}

/* Modal Overlay */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 38, 74, 0.65);
  backdrop-filter: blur(4px);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--surface-border);
  max-width: 500px;
  width: 100%;
  padding: 1.75rem;
  box-shadow: var(--shadow-lg);
  position: relative;
}

.modal-close {
  position: absolute;
  top: 14px;
  left: 14px;
  background: none;
  border: none;
  font-size: 1.25rem;
  color: var(--text-muted);
  cursor: pointer;
}

/* Loading Overlay */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 38, 74, 0.75);
  backdrop-filter: blur(5px);
  z-index: 300;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  gap: 1rem;
}

.spinner {
  width: 50px;
  height: 50px;
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--brand-teal);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Toast */
/* Compact feedback beside the action that triggered it. */
.toast {
  display: none;
  position: static;
  inset: auto;
  transform: none;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  flex-basis: 100%;
  box-sizing: border-box;
  margin: 8px 0 0;
  padding: 9px 12px;
  border: 1px solid #d5dfe6;
  border-inline-start: 3px solid #607482;
  border-radius: 6px;
  background: #f7f9fa;
  color: #334f61;
  font: 400 13px/1.8 'PortfolioUI', 'StudioCairo', 'Cairo', sans-serif;
  text-align: start;
  overflow-wrap: anywhere;
  box-shadow: none;
  opacity: 1;
  transition: none;
  z-index: auto;
  pointer-events: none;
}
.toast.show { display: block; transform: none; opacity: 1; }
.toast.toast-success { background: #f0f8f4; color: #286348; border-color: #cfe4d8; }
.toast.toast-warning { background: #fffbf2; color: #805d20; border-color: #eadfc2; }
.toast.toast-error { background: #fff5f5; color: #9d3438; border-color: #efd2d4; }
@media print { .toast { display: none !important; } }

.btn-cancel-loading:hover {
  background: rgba(229, 36, 42, 0.3) !important;
  border-color: rgba(229, 36, 42, 0.6) !important;
  color: #fff !important;
}

/* ==========================================================================
   PRINT MEDIA RULES - PERFECT A4 MARGINS
   ========================================================================== */
@media print {
  body {
    background: #ffffff !important;
    padding: 0 !important;
    margin: 0 !important;
  }

  .app-header,
  .sidebar,
  .sheet-toolbar,
  .modal-overlay,
  .toast,
  .loading-overlay {
    display: none !important;
  }

  .app-container {
    display: block !important;
    max-width: none !important;
    padding: 0 !important;
    margin: 0 !important;
  }

  .workspace {
    display: block !important;
  }

  .a4-sheet-wrapper {
    box-shadow: none !important;
    border-radius: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  .a4-sheet {
    width: 100% !important;
    min-height: auto !important;
    padding: 12mm 15mm !important;
    page-break-after: avoid !important;
  }

  [contenteditable="true"] {
    outline: none !important;
    background: transparent !important;
  }

  .page2-wrapper {
    page-break-before: always !important;
    break-before: page !important;
  }

  .page2-btn-remove-photo,
  .page2-upload-slot {
    display: none !important;
  }
}

/* Empty ad preview, shown only while preparing a full report. */
.loading-overlay { padding: 1.25rem; overflow-y: auto; text-align: center; }
.loading-ad-slot {
  width: min(100%, 360px);
  min-height: 220px;
  padding: 1rem;
  border: 1px dashed rgba(255, 255, 255, 0.45);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.06);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 1rem;
}
.loading-ad-slot[hidden] { display: none; }
.loading-ad-label { align-self: flex-start; color: #cbd5e1; font-size: 0.75rem; }
.loading-ad-placeholder { color: #e2e8f0; font-weight: 700; }
.loading-ad-slot small { color: #94a3b8; font-size: 0.75rem; }
@media (max-height: 600px) {
  .loading-overlay { justify-content: flex-start; gap: 0.65rem; }
  .loading-ad-slot { min-height: 140px; }
}


/* Responsive UI only: the A4 sheet and PDF capture dimensions stay unchanged. */
.app-container { grid-template-columns: 410px minmax(0, 1fr); }
.app-container > *, .panel-card, .field-group, .toolbar-info, .toolbar-actions { min-width: 0; }
.institution-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.5rem; }
.workspace { width: 100%; min-width: 0; overflow: visible; }
.sheet-toolbar { flex-wrap: wrap; padding: 0.8rem; }
.toolbar-info { flex: 1 1 220px; }
.toolbar-actions { flex: 1 1 360px; flex-wrap: wrap; justify-content: flex-end; }
.toolbar-actions > button { min-height: 44px; white-space: normal; }
.sheet-preview-viewport { position: relative; width: 100%; max-width: 210mm; overflow: auto; direction: ltr; }
.sheet-preview-viewport.preview-ready { overflow: hidden; }
.sheet-preview-viewport.preview-ready > .a4-sheet-wrapper {
  position: absolute; top: 0; right: 0; width: 210mm; direction: rtl; transform-origin: top right;
  transition: none;
}
.sheet-preview-viewport.preview-actual { overflow-x: auto; overscroll-behavior-x: contain; padding-bottom: 18px; box-sizing: content-box; }
.sheet-preview-viewport.preview-actual > .a4-sheet-wrapper { right: auto; left: 0; }
.preview-help { width: 100%; max-width: 210mm; color: #cbd5e1; font-size: 0.8rem; line-height: 1.7; }
.preview-help[hidden], #btnPreviewZoom[hidden] { display: none; }
.preview-jump { display: none; }
.provider-heading, .panel-title { flex-wrap: wrap; gap: 0.5rem; }
.provider-tabs, .settings-tabs, .settings-actions { flex-wrap: wrap; }
.provider-tabs > button, .settings-tabs > button { min-width: 0; min-height: 44px; flex-basis: 90px !important; }
.modal-overlay { z-index: 10010; overflow-y: auto; overscroll-behavior: contain; }
.modal-card {
  min-width: 0; max-height: calc(100vh - 2rem); max-height: calc(100dvh - 2rem);
  overflow-y: auto; overscroll-behavior: contain; scrollbar-gutter: stable; overflow-wrap: anywhere;
}
.modal-dismiss-bar {
  position: sticky; top: 0; z-index: 3; display: flex; justify-content: flex-end;
  min-height: 44px; margin: -0.5rem -0.5rem 0.5rem; background: var(--surface);
}
.modal-dismiss-bar .modal-close {
  position: static; width: 44px; height: 44px; margin: 0;
  background: var(--surface); border: 1px solid var(--surface-border); border-radius: 8px;
}
.modal-card { scroll-padding-top: 60px; }
.settings-actions > button { min-width: 0; min-height: 44px; flex-basis: 140px !important; }
.loading-overlay { z-index: 10020; }
.toast { z-index: 10030; overflow-wrap: anywhere; }
.btn:focus-visible, .modal-close:focus-visible, .btn-provider-tab:focus-visible, .tab-btn-modal:focus-visible {
  outline: 3px solid #38bdf8; outline-offset: 3px;
}
@media screen and (max-width: 1180px) {
  .app-container { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; }
  .app-header { position: static; }
  .preview-jump { display: flex; margin-top: 0.65rem; min-height: 44px; }
  #reportWorkspace { scroll-margin-top: 60px; }
}
@media screen and (max-width: 600px) {
  .app-container { margin: 0.85rem auto; padding: 0 0.75rem; gap: 1rem; }
  .panel-card { padding: 1rem; }
  .app-header { padding: 0.8rem 0.75rem; }
  .brand-wrapper, .brand-title { min-width: 0; flex-wrap: wrap; }
  .brand-wrapper { align-items: flex-start; gap: 0.65rem; }
  .brand-wrapper > div { min-width: 0; flex: 1; }
  .brand-title { font-size: 1.05rem; }
  .back-link { flex-shrink: 0; }
  .institution-fields { grid-template-columns: minmax(0, 1fr); }
  .input-control, .select-control, .textarea-control, .modal-card input, .modal-card select { max-width: 100%; min-width: 0; font-size: 16px !important; }
  .btn-provider-tab, .tab-btn-modal { overflow-wrap: anywhere; }
  .sheet-toolbar { gap: 0.65rem; padding: 0.75rem; }
  .toolbar-info, .toolbar-actions { flex-basis: 100%; width: 100%; }
  .toolbar-actions { gap: 0.5rem; }
  .toolbar-actions > button { flex: 1 1 145px; justify-content: center; }
  #btnDownloadPdf { order: -1; flex-basis: 100%; min-height: 48px; }
  .modal-overlay { padding: max(0.65rem, env(safe-area-inset-top)) 0.65rem max(0.65rem, env(safe-area-inset-bottom)); }
  .modal-card { width: 100% !important; padding: 1rem; }
  .settings-tabs { gap: 0.4rem; }
  .preset-list { max-height: 320px; }
}
@media print {
  .sheet-preview-viewport { height: auto !important; width: 100% !important; overflow: visible !important; }
  .sheet-preview-viewport.preview-ready > .a4-sheet-wrapper {
    position: static !important; width: 100% !important; transform: none !important;
  }
  .preview-help, .preview-jump { display: none !important; }
}
