:root {
    --ink: #395d59;
    --ink-deep: #24534f;
    --muted: #71817e;
    --teal: #5d958b;
    --teal-deep: #39746e;
    --sand: #e9dfca;
    --sand-pale: #f6f0e5;
    --gold: #9b6b10;
    --gold-light: #c9ab67;
    --paper: #fffefa;
    --line: #d8e0d9;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body { min-width: 320px; color: var(--ink); background: #edf2ee; direction: rtl; font-family: "Tajawal", sans-serif; }
button, input { font: inherit; }
button { border: 0; }

.app-shell { display: grid; grid-template-columns: minmax(290px, 350px) minmax(0, 1fr); min-height: 100dvh; direction: rtl; }
.editor-panel { z-index: 1; padding: 46px 28px 36px; background: #fcfdf9; box-shadow: -7px 0 28px rgba(44, 85, 75, .08); }
.eyebrow { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; color: var(--gold); font-size: .76rem; font-weight: 800; letter-spacing: .08em; }
.eyebrow::before { width: 25px; height: 2px; background: var(--gold); content: ""; }
h1 { color: var(--ink-deep); font-size: clamp(1.7rem, 3vw, 2.1rem); line-height: 1.3; }
.intro { max-width: 270px; margin: 10px 0 32px; color: var(--muted); font-size: .9rem; line-height: 1.8; }
.form-grid, .form-section { display: grid; gap: 18px; }
.form-section { border: 0; gap: 14px; }
.form-section legend { width: 100%; padding-bottom: 9px; border-bottom: 1px solid var(--line); color: var(--teal-deep); font-size: 1rem; font-weight: 800; }
label { display: grid; gap: 7px; color: #536d68; font-size: .82rem; font-weight: 700; }
input[type="text"] { width: 100%; padding: 11px 13px; border: 1px solid #d9e4dc; border-radius: 8px; outline: none; background: #fffffc; color: var(--ink); font-size: .94rem; transition: border-color .2s, box-shadow .2s; }
input[type="text"]:focus { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(155,107,16,.12); }
input[type="file"] { width: 100%; padding: 9px; border: 1px dashed #bccfc8; border-radius: 8px; background: #f8fbf8; color: var(--muted); cursor: pointer; font-size: .73rem; }
input[type="file"]::file-selector-button { margin-left: 8px; padding: 5px 9px; border: 0; border-radius: 5px; background: #e1ece6; color: var(--teal-deep); cursor: pointer; font-family: inherit; font-size: .72rem; }
small, .field-hint { color: #89988f; font-size: .7rem; font-weight: 400; line-height: 1.5; }
.checkbox-label { display: flex; align-items: center; gap: 9px; color: var(--teal-deep); }
.checkbox-label input { width: 17px; height: 17px; accent-color: var(--teal-deep); }
.reset-logo-button { justify-self: start; padding: 5px 10px; border: 1px solid #d7e2db; border-radius: 6px; background: #fffffc; color: var(--teal-deep); cursor: pointer; font-size: .72rem; font-weight: 700; }
.reset-logo-button:hover:not(:disabled) { border-color: var(--gold); background: #fffaf0; }
.reset-logo-button:disabled { cursor: not-allowed; opacity: .45; }
.actions { display: grid; gap: 9px; margin-top: 2px; }
.button { min-height: 43px; border-radius: 8px; cursor: pointer; font-size: .86rem; font-weight: 700; transition: transform .2s, box-shadow .2s, opacity .2s; }
.button:hover { transform: translateY(-1px); box-shadow: 0 5px 12px rgba(29, 82, 66, .12); }
.button-primary { background: var(--teal-deep); color: #fffefa; }
.button span { display: none; }

.preview-panel { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; min-width: 0; padding: 42px clamp(18px, 5vw, 72px); background: radial-gradient(circle at 50% 40%, #fbfdf9 0, #e9f0eb 70%); }
.preview-toolbar { display: flex; justify-content: space-between; width: min(100%, 1000px); color: #7b8f88; font-size: .72rem; font-weight: 700; }
.preview-toolbar > span:first-child { display: flex; align-items: center; gap: 7px; }
.status-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--teal); box-shadow: 0 0 0 4px rgba(93,149,139,.12); }

.certificate { width: min(100%, 1000px); aspect-ratio: 1.414 / 1; overflow: hidden; padding: 0; background: var(--paper); box-shadow: 0 22px 55px rgba(42, 80, 70, .18); }
.certificate-border { position: relative; display: flex; flex-direction: column; width: 100%; height: 100%; overflow: hidden; padding: 4.4% 6.4% 3.5%; background: var(--paper); isolation: isolate; }
.certificate-border::before { position: absolute; z-index: -1; top: -27%; right: -13%; width: 34%; height: 52%; border-radius: 0 0 0 68%; background: var(--sand); content: ""; transform: rotate(-13deg); }
.certificate-border::after { position: absolute; z-index: -1; bottom: -33%; left: -13%; width: 34%; height: 27%; border-radius: 0 66% 0 0; background: var(--teal-deep); content: ""; transform: rotate(7deg); }
.corner { position: absolute; z-index: -1; opacity: .8; }
.corner-top-right { top: -40%; right: -23%; width: 36%; height: 59%; border: clamp(16px, 3vw, 32px) solid var(--teal-deep); border-radius: 50%; opacity: .68; transform: rotate(-25deg); }
.corner-top-left { top: -3%; left: -5%; width: 25%; height: 15%; background-image: radial-gradient(circle, var(--gold-light) 1px, transparent 1.7px); background-size: 9px 9px; mask-image: linear-gradient(100deg, #000 15%, transparent 88%); }
.corner-bottom-right { right: -2%; bottom: -5%; width: 31%; height: 10%; background-image: radial-gradient(circle, var(--gold-light) 1.3px, transparent 1.9px); background-size: 10px 10px; mask-image: linear-gradient(78deg, transparent 8%, #000 62%); transform: rotate(3deg); }
.corner-bottom-left { bottom: -31%; left: -14%; width: 31%; height: 30%; border: clamp(14px, 3vw, 32px) solid #9cb8b2; border-radius: 50%; transform: rotate(-19deg); }

.certificate-header, .certificate-content, .certificate-footer { position: relative; z-index: 2; }
.certificate-header { display: grid; grid-template-areas: "association ministry info"; grid-template-columns: 1fr auto 1fr; align-items: start; justify-items: center; column-gap: 3%; direction: ltr; }
.official-info { grid-area: info; display: flex; flex-direction: column; align-items: center; width: 100%; padding-top: 2px; direction: rtl; color: #77837e; font-size: clamp(.48rem, .95vw, .78rem); line-height: 1.64; text-align: center; }
.official-info strong { color: var(--teal); font-size: 1.1em; }
.header-logos { display: contents; }
.ministry-mark { grid-area: ministry; display: flex; align-items: flex-start; justify-content: center; justify-self: center; min-height: clamp(74px, 11vw, 122px); padding-top: 0; }
.ministry-mark img { width: clamp(88px, 13vw, 154px); height: auto; max-height: clamp(40px, 6.3vw, 74px); object-fit: contain; }
.association-mark { grid-area: association; display: flex; align-items: flex-start; justify-content: center; justify-self: center; min-height: clamp(74px, 11vw, 122px); padding: 0; background: #fff; }
.association-mark img { width: clamp(96px, 14vw, 163px); height: auto; max-height: clamp(74px, 11vw, 122px); object-fit: contain; }
.association-mark.is-hidden { display: none; }

.certificate-content { display: flex; flex: 1; flex-direction: column; align-items: center; justify-content: center; min-height: 0; padding: 0 0 1%; }
.title-rule { display: flex; align-items: center; justify-content: center; gap: 11px; width: 52%; color: var(--teal); font-size: clamp(.5rem, 1vw, .82rem); letter-spacing: .08em; }
.title-rule b { font-weight: 500; }
.title-rule span { flex: 1; height: 1px; background: linear-gradient(to left, transparent, #a8c1ba); }
.title-rule span:last-child { background: linear-gradient(to right, transparent, #a8c1ba); }
.certificate h2 { margin: 5px 0 1px; color: var(--gold); font-size: clamp(1.8rem, 4.7vw, 3.8rem); font-weight: 800; line-height: 1.13; }
.certificate-lead { color: #858279; font-size: clamp(.58rem, 1.18vw, .96rem); }
.student-photo-frame { display: grid; place-items: center; width: clamp(102px, 17vw, 178px); height: clamp(102px, 17vw, 178px); aspect-ratio: 1 / 1; flex: 0 0 auto; overflow: hidden; margin: 1.3% 0 .5%; padding: 6px; border: 1px solid var(--gold-light); border-radius: 50%; background: #fffefa; box-shadow: 0 0 0 4px rgba(233,223,202,.75); }
.student-photo-frame.has-image { padding: 5px; }
.student-photo-frame img { display: none; width: 100%; height: 100%; aspect-ratio: 1 / 1; border-radius: 50%; clip-path: circle(50%); object-fit: cover; object-position: center; }
.student-photo-frame img.is-visible { display: block; }
.photo-placeholder { display: flex; align-items: center; justify-content: center; flex-direction: column; width: 100%; height: 100%; aspect-ratio: 1 / 1; border-radius: 50%; background: var(--sand-pale); color: var(--gold); }
.photo-placeholder small { color: #9a907d; font-size: clamp(.45rem, .8vw, .65rem); }
.is-hidden { display: none !important; }
.student-line { display: flex; align-items: baseline; gap: 8px; color: #827e74; font-size: clamp(.75rem, 1.4vw, 1.12rem); }
.student-line strong { min-width: clamp(120px, 25vw, 280px); padding: 0 13px 3px; border-bottom: 1px solid #c5d3ca; color: var(--ink-deep); font-size: 1.25em; text-align: center; }
.class-line { margin-top: 2px; color: #827e74; font-size: clamp(.65rem, 1.1vw, .88rem); }
.class-line strong { color: var(--ink-deep); }
.certificate-footer { display: grid; grid-template-areas: "principal teacher"; grid-template-columns: 1fr 1fr; align-items: flex-end; gap: 12%; padding: 0 4% 1%; direction: ltr; }
.teacher-signature { grid-area: teacher; justify-self: end; }
.principal-signature { grid-area: principal; justify-self: start; }
.signature { display: grid; grid-template-columns: minmax(55px,.9fr) minmax(80px,1.2fr); align-items: baseline; gap: 7px; width: min(100%,245px); color: #827e74; direction: rtl; font-size: clamp(.48rem,.95vw,.76rem); text-align: right; }
.signature strong { padding-bottom: 2px; border-bottom: 1px solid #c5d3ca; color: var(--ink-deep); font-size: clamp(.62rem,1.2vw,.95rem); text-align: center; }
.preview-note { color: #89988f; font-size: .7rem; }

@media (max-width: 900px) { .app-shell { grid-template-columns: 300px minmax(0,1fr); } .editor-panel { padding: 32px 20px; } .preview-panel { padding: 32px 18px; } }
@media (max-width: 680px) { .app-shell { display: flex; flex-direction: column; } .editor-panel { padding: 28px 20px 24px; } .intro { margin-bottom: 23px; } .form-grid { grid-template-columns: repeat(2,minmax(0,1fr)); gap: 16px; } .form-section { gap: 10px; } .form-section:first-child { grid-column: 1; } .form-section:nth-child(2) { grid-column: 2; } .logos-section,.actions { grid-column: 1 / -1; } .preview-panel { min-height: 70vw; padding: 24px 12px 34px; } }
@media (max-width: 460px) { .form-grid { grid-template-columns: 1fr; } .form-section:first-child,.form-section:nth-child(2),.logos-section,.actions { grid-column: auto; } }

@media print {
    @page { size: A4 landscape; margin: 0; }
    * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
    html, body { width: 297mm; height: 210mm; min-width: 297mm; min-height: 210mm; max-width: 297mm; max-height: 210mm; margin: 0 !important; padding: 0 !important; overflow: hidden !important; }
    body, .app-shell, .preview-panel { display: block; position: relative; width: 297mm; height: 210mm; min-width: 297mm; min-height: 0; max-width: 297mm; max-height: 210mm; overflow: hidden !important; padding: 0; background: var(--paper); }
    .editor-panel, .preview-toolbar, .preview-note { display: none !important; }
    .certificate { position: absolute; top: 0; left: 0; width: 297mm; height: 210mm; padding: 0; margin: 0; box-shadow: none; break-after: avoid; break-inside: avoid; page-break-before: avoid; page-break-after: avoid; page-break-inside: avoid; }
    .certificate-border { padding: 9mm 19mm 7mm; }
    .certificate h2 { margin: 2mm 0 1mm; font-size: 18mm; }
    .title-rule { width: 58%; font-size: 4mm; }
    .certificate-lead { font-size: 5mm; }
    .student-photo-frame { width: 49mm; height: 49mm; margin: 4mm 0 2mm; padding: 2mm; }
    .student-line { gap: 3mm; font-size: 6mm; }
    .student-line strong { min-width: 82mm; padding: 0 4mm 1mm; }
    .class-line { margin-top: 1mm; font-size: 4.5mm; }
    .association-mark, .ministry-mark { min-height: 32mm; }
    .association-mark img { width: 38mm; max-height: 32mm; }
    .ministry-mark img { width: 38mm; max-height: 20mm; }
    .corner-top-left { background-size: 2.3mm 2.3mm; }
    .corner-bottom-right { background-size: 2.5mm 2.5mm; }
}