/* The printable certificate, and the page that frames it.
 *
 * Two rules run through everything here:
 *
 * 1. **The certificate itself is sized in `mm`, not in `px` or `%`.** It is a
 *    document that ends up on A4 landscape, and a page whose layout only
 *    resolves at the viewport width prints as something else than what was on
 *    screen. The screen view scales the whole sheet with a `transform`, so the
 *    proportions the teacher approved are the ones that come out of the printer.
 *
 * 2. **Every colour comes from a custom property the JS writes inline on the
 *    sheet.** The palette is per-certificate and snapshotted at issue time, so
 *    no rule here may hardcode a brand colour — a stylesheet that did would
 *    restyle certificates issued years ago the day it changed.
 *
 * This file is also linked on the student list page, which reuses `.cert-sheet`
 * at a small scale as the card preview: one renderer, so the thumbnail cannot
 * disagree with the document.
 */

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

/* ── The page around the certificate ─────────────────────────────────────── */

.cert-page {
    min-height: 100vh;
    /* Deliberately a neutral slate, not a theme variable: this page is opened
       by strangers on the apex domain and on teacher subdomains alike, and the
       one thing that must read consistently is the paper against its backdrop. */
    background: #f1f5f9;
    padding: 24px 16px 64px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

@media (prefers-color-scheme: dark) {
    .cert-page { background: #0f172a; }
}

.cert-topbar {
    width: 100%;
    max-width: 1100px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.cert-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    color: #0f172a;
}

@media (prefers-color-scheme: dark) {
    .cert-brand { color: #e2e8f0; }
}

.cert-brand img { height: 34px; width: auto; }

.cert-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.cert-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    border: none;
    border-radius: 10px;
    background: #137980;
    color: #fff;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}

.cert-btn.secondary { background: #475569; }
.cert-btn:disabled { opacity: .55; cursor: default; }

/* The verification banner. Green/red/amber are hardcoded on purpose — this is
   the one part of the page that must NOT follow the teacher's palette, because
   a teacher-coloured "valid" badge is a claim the teacher styled himself. */
.cert-verdict {
    width: 100%;
    max-width: var(--cert-w);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 18px;
    border-radius: 12px;
    font-weight: 600;
    border: 1px solid transparent;
}

.cert-verdict i { font-size: 1.4rem; }
.cert-verdict.valid { background: #ecfdf5; color: #065f46; border-color: #6ee7b7; }
.cert-verdict.revoked { background: #fef2f2; color: #991b1b; border-color: #fca5a5; }
.cert-verdict.missing { background: #fffbeb; color: #92400e; border-color: #fcd34d; }

/* ── The sheet ───────────────────────────────────────────────────────────── */

.cert-scaler {
    width: 100%;
    max-width: var(--cert-w);
    /* The sheet is a fixed 297mm wide and scaled down to fit; the wrapper has to
       claim the *scaled* height or the page keeps a full-size gap under it. */
    overflow: hidden;
}

.cert-sheet {
    width: var(--cert-w);
    height: var(--cert-h);
    transform-origin: top right; /* RTL page: shrink toward the right edge. */
    position: relative;
    box-sizing: border-box;
    background: var(--c-bg, #fff);
    color: var(--c-text, #1f2937);
    font-family: 'Cairo', 'Tajawal', system-ui, sans-serif;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 18px 45px rgba(15, 23, 42, .18);
}

.cert-body {
    position: relative;
    z-index: 2;
    /* `min-height: 0` is load-bearing: a flex child will not shrink below its
       content without it, so a long body sentence pushed the footer out through
       the sheet's `overflow: hidden` and the signature and QR were cut clean off
       the bottom of the document. */
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 16mm 28mm 8mm;
    gap: 4mm;
}

.cert-logo { max-height: 22mm; max-width: 70mm; object-fit: contain; }

.cert-kicker {
    font-size: 4.6mm;
    letter-spacing: .18em;
    color: var(--c-accent, #137980);
    font-weight: 700;
}

.cert-title {
    font-size: 13mm;
    line-height: 1.15;
    margin: 0;
    font-weight: 800;
    color: var(--c-accent, #137980);
}

.cert-student {
    font-size: 10mm;
    font-weight: 800;
    margin: 0;
    padding-bottom: 2mm;
    border-bottom: .8mm solid var(--c-secondary, #c9a227);
}

.cert-text {
    font-size: 5.4mm;
    line-height: 1.9;
    max-width: 200mm;
    margin: 0;
}

.cert-subject {
    font-size: 7mm;
    font-weight: 700;
    color: var(--c-accent, #137980);
    margin: 0;
}

.cert-score-row { display: flex; gap: 14mm; align-items: center; }

.cert-score {
    display: flex;
    flex-direction: column;
    gap: 1mm;
    padding: 3mm 8mm;
    border-radius: 4mm;
    border: .6mm solid var(--c-secondary, #c9a227);
}

.cert-score b { font-size: 8mm; color: var(--c-accent, #137980); }
.cert-score span { font-size: 3.6mm; opacity: .75; }

/* The footer row: signature on one side, QR + serial on the other. */
.cert-footer {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 10mm;
    padding: 0 24mm 12mm;
    /* `flex: none`, so the footer keeps its height and the body is what gives
       way — the reverse clips the signature. */
    flex: none;
}

.cert-sign { text-align: center; min-width: 55mm; }
.cert-sign img { max-height: 18mm; max-width: 55mm; object-fit: contain; }
.cert-sign .line {
    border-top: .5mm solid var(--c-text, #1f2937);
    margin-top: 2mm;
    padding-top: 2mm;
    font-weight: 700;
    font-size: 4.4mm;
}
.cert-sign .role { font-size: 3.6mm; opacity: .7; }

.cert-verify { text-align: center; }
.cert-verify img { width: 20mm; height: 20mm; display: block; margin: 0 auto 1.5mm; }
.cert-serial {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    /* The serial is Latin letters and digits inside an RTL document: without an
       explicit direction the bidi algorithm reorders the groups around the
       hyphens and prints a serial that verifies as something else. */
    direction: ltr;
    unicode-bidi: isolate;
    font-size: 3.6mm;
    letter-spacing: .06em;
}
.cert-issued { font-size: 3.4mm; opacity: .7; margin-top: 1mm; }

/* A revoked certificate says so on its face. Without this, printing one that
   was withdrawn produces a document indistinguishable from a valid one. */
.cert-sheet.is-revoked::after {
    content: 'ملغاة';
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 60mm;
    font-weight: 900;
    color: rgba(185, 28, 28, .16);
    transform: rotate(-22deg);
    pointer-events: none;
}

/* ── Design 1: classic — double rule inside the margin ──────────────────── */

/* One decoration node per sheet, styled differently by each design. It exists
   rather than using the sheet's own ::before/::after because those had to serve
   two jobs at once: the classic frame drawn on the *body* stopped above the
   footer, and the revoked stamp collided with the elegant flourishes. */
.cert-deco {
    position: absolute;
    inset: 0;
    z-index: 1;
    pointer-events: none;
}

.cert-sheet[data-design='classic'] .cert-deco {
    inset: 8mm;
    border: 1.6mm solid var(--c-accent, #137980);
}

.cert-sheet[data-design='classic'] .cert-deco::before {
    content: '';
    position: absolute;
    inset: 3mm;
    border: .5mm solid var(--c-secondary, #c9a227);
}

/* ── Design 2: modern — gradient band down the outer edge ──────────────────
 * `inset-inline-end` puts it on the *left* on these RTL pages, opposite the
 * reader's starting edge, so it frames the block of text rather than sitting in
 * front of it. The body's `padding-inline-end` is what keeps the two apart.
 */

.cert-sheet[data-design='modern'] .cert-deco::before {
    content: '';
    position: absolute;
    inset-block: 0;
    inset-inline-end: 0;
    width: 26mm;
    background: linear-gradient(180deg, var(--c-accent, #137980), var(--c-secondary, #c9a227));
}

.cert-sheet[data-design='modern'] .cert-body,
.cert-sheet[data-design='modern'] .cert-footer {
    padding-inline-end: 38mm;
}

.cert-sheet[data-design='modern'] .cert-title { letter-spacing: -.02em; }

/* ── Design 3: elegant — corner flourishes ──────────────────────────────── */

/* Both flourishes are on the **top** corners, and that is a fix rather than a
   preference. Diagonally opposite corners is the prettier idea, but the footer
   spans the full width of the sheet — so a bottom-corner ornament draws a gold
   curve straight through the QR code and the serial, whichever side the QR is
   on. The top edge carries only centred text, which 38mm corners clear. */
.cert-sheet[data-design='elegant'] .cert-deco::before,
.cert-sheet[data-design='elegant'] .cert-deco::after {
    content: '';
    position: absolute;
    top: 10mm;
    width: 38mm;
    height: 38mm;
    border: 2mm solid var(--c-secondary, #c9a227);
    border-bottom: none;
}

.cert-sheet[data-design='elegant'] .cert-deco::before {
    right: 10mm;
    border-left: none;
    border-top-right-radius: 20mm;
}

.cert-sheet[data-design='elegant'] .cert-deco::after {
    left: 10mm;
    border-right: none;
    border-top-left-radius: 20mm;
}

.cert-sheet[data-design='elegant'] .cert-title {
    font-family: 'Amiri', 'Cairo', serif;
    /* Amiri's cap height is far shorter than Cairo's at the same size, which left
       the title reading smaller than the student's name below it — the wrong way
       round for the heading of a document. */
    font-size: 17mm;
}

/* The footer clears the corner flourishes on this design. */
.cert-sheet[data-design='elegant'] .cert-footer { padding: 0 30mm 12mm; }

/* ── Print ───────────────────────────────────────────────────────────────── */

@page {
    size: A4 landscape;
    margin: 0;
}

@media print {
    /* The UA default body margin (usually 8px) survives `@page{margin:0}` —
       nothing else on this page resets it — and was the actual cause of the
       sheet spilling a near-empty second page. */
    html, body {
        margin: 0 !important;
        padding: 0 !important;
        background: #fff;
    }

    /* Everything but the sheet goes. `visibility` rather than `display: none` on
       the ancestors, so the sheet keeps its place in the flow. */
    .cert-topbar,
    .cert-verdict,
    .cert-note,
    .cert-verify-box { display: none !important; }

    .cert-page {
        display: block !important;
        background: #fff;
        padding: 0;
        gap: 0;
        min-height: 0;
    }
    .cert-scaler {
        /* certificate-render.js writes an inline scaled px height for the
           screen layout; left alone in print it holds the wrapper far
           shorter than the un-transformed sheet inside it. */
        height: auto !important;
        max-width: none;
        overflow: visible;
    }

    .cert-sheet {
        /* The screen scale is undone: on paper the sheet IS the page. Fixed
           to the actual mm size (not 100vh, which never resolves smaller
           than the page box and so always spills a pixel onto a second one). */
        transform: none !important;
        box-shadow: none;
        width: var(--cert-w);
        height: var(--cert-h);
        overflow: hidden;
        break-inside: avoid;
        page-break-after: avoid;
    }

    /* Backgrounds and the coloured band are the design, not decoration. Browsers
       still need the user's "print backgrounds" setting; this is the half we can
       state. */
    * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }
}
