/* Fill PDF (fill-pdf.html), loaded after styles.css and tools.css. Uses the same tokens, so light and dark both work.
   The page itself is always white paper, like the file.

   Page view (js/tool-fill.js builds this markup)
     .fp                        the work area; [data-active="select|text|date|tick|cross|signature"] is the current tool
       .fp-bar                  sticky toolbar: .fp-group > .fp-tool / .fp-btn / .fp-label
       .fp-body                 .fp-pages-wrap > .fp-pages > section.fp-page + aside.fp-side
         .fp-page               one PDF page; --k = CSS px per PDF point, set from JS, drives every size below
           canvas.fp-canvas     the drawn page
           .fp-layer            everything on top of it
             .fp-w              an input over one of the PDF's own boxes (.fp-text .fp-check .fp-radio .fp-dropdown .fp-slot)
             .fp-ann            something the person added (.fp-ann-text .fp-ann-tick .fp-ann-cross .fp-ann-image)
*/

.fp {
    outline: none;
    padding: clamp(14px, 3vw, 28px);
    background: var(--sheet);
    border: 1px solid var(--line);
    border-radius: 6px;
    box-shadow: var(--shadow);
}
.fp-head { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 14px; }
.fp-head .org-file { min-width: 0; font-weight: 500; overflow-wrap: anywhere; }
.fp-head .fp-end { margin-left: auto; }

/* ---------- Toolbar ---------- */
.fp-bar {
    position: sticky;
    top: 0;
    z-index: 6;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 14px;
    margin: 14px 0 0;
    padding: 8px 0;
    background: var(--sheet);
    border-bottom: 1px solid var(--line);
}
.fp-group { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.fp-end { margin-left: auto; }
.fp-sep { width: 1px; align-self: stretch; margin: 6px 4px; background: var(--line); }
.fp-tool, .fp-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 44px;
    min-width: 44px;
    padding: 0 12px;
    font: inherit;
    font-size: .92rem;
    font-weight: 500;
    color: var(--ink);
    background: transparent;
    border: 1px solid transparent;
    border-radius: 8px;
    cursor: pointer;
}
.fp-btn { padding: 0; }
.fp-tool:hover, .fp-btn:hover:not(:disabled) { background: color-mix(in srgb, var(--ink) 7%, transparent); }
.fp-tool[aria-pressed="true"] { color: var(--seal-ink); background: var(--seal); }
.fp-btn:disabled { opacity: .35; cursor: default; }
.fp-danger:hover:not(:disabled) { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.fp-tool:focus-visible, .fp-btn:focus-visible { outline-offset: -3px; border-radius: 8px; }
.fp-label { min-width: 4.5ch; text-align: center; color: var(--ink-soft); font-size: .88rem; font-variant-numeric: tabular-nums; }
.fp-hint { min-height: 1.6em; margin: 8px 0 0; color: var(--ink-soft); font-size: .9rem; }

/* ---------- Pages ---------- */
.fp-body { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 18px; align-items: start; margin-top: 8px; }
.fp-pages-wrap {
    min-width: 0;
    padding: 14px;
    overflow-x: auto;
    background: color-mix(in srgb, var(--paper) 70%, var(--line));
    border-radius: 6px;
}
.fp-pages { display: flex; flex-direction: column; align-items: flex-start; gap: 14px; }
.fp-page {
    --k: 1;
    position: relative;
    flex: none;
    margin-inline: auto;
    background: #fff;
    color: #0d0d14;
    box-shadow: 0 0 0 1px rgba(20, 33, 61, .18), 0 6px 22px rgba(20, 33, 61, .22);
}
.fp-canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.fp-layer { position: absolute; inset: 0; }
.fp-page[data-error] .fp-layer::before { content: 'This page could not be drawn.'; position: absolute; inset: 40% 0 auto; text-align: center; color: #5b6578; }
[data-active="text"] .fp-layer, [data-active="date"] .fp-layer { cursor: text; }
[data-active="tick"] .fp-layer, [data-active="cross"] .fp-layer, [data-active="signature"] .fp-layer { cursor: crosshair; }
/* While a tool is active, clicks must reach the page, not the boxes under the pointer. */
[data-active]:not([data-active="select"]) .fp-w { pointer-events: none; }

/* ---------- Boxes of the PDF's own form ---------- */
.fp-w {
    position: absolute;
    margin: 0;
    min-width: 0;
    padding: 0 calc(var(--k) * 2px);
    font: calc(var(--k) * var(--fs, 10) * 1px) / 1.15 Helvetica, Arial, "Liberation Sans", sans-serif;
    color: #0d0d14;
    background: rgba(214, 230, 255, .94);
    border: 1px solid rgba(31, 78, 158, .55);
    border-radius: 2px;
    z-index: 2;
}
textarea.fp-w { padding-block: calc(var(--k) * 2px); resize: none; }
select.fp-w { padding: 0; }
.fp-w:hover { border-color: #1F4E9E; }
.fp-w:focus { z-index: 2; background: #fff; border-color: #1F4E9E; outline: 2px solid #1F4E9E; outline-offset: 0; }
.fp-w[aria-readonly="true"] { background: rgba(225, 228, 236, .9); border-color: rgba(90, 100, 120, .4); }
.fp-comb { letter-spacing: var(--ls, 0); }
.fp-check, .fp-radio { appearance: none; -webkit-appearance: none; display: grid; place-items: center; padding: 0; cursor: pointer; }
.fp-radio { border-radius: 50%; }
.fp-check::after, .fp-radio::after { content: ''; display: none; }
.fp-check:checked::after { display: block; width: 28%; height: 56%; border: solid #1b2f7a; border-width: 0 max(2px, calc(var(--k) * 1.6px)) max(2px, calc(var(--k) * 1.6px)) 0; transform: translateY(-8%) rotate(45deg); }
.fp-radio:checked::after { display: block; width: 52%; height: 52%; border-radius: 50%; background: #1b2f7a; }
.fp-slot { padding: 0; color: #7c4a03; background: rgba(255, 240, 190, .94); border: 1px dashed #b45309; font: 600 calc(var(--k) * 9px) / 1 var(--font); cursor: pointer; }
.fp-slot:hover { background: rgba(255, 232, 160, .98); }
.fp-flash { animation: fp-flash .9s ease-out; }
@keyframes fp-flash { 0%, 60% { box-shadow: 0 0 0 4px rgba(31, 78, 158, .55); } 100% { box-shadow: 0 0 0 0 rgba(31, 78, 158, 0); } }

/* ---------- Things the person adds ---------- */
.fp-ann {
    position: absolute;
    width: calc(var(--k) * var(--w) * 1px);
    height: calc(var(--k) * var(--h) * 1px);
    color: #0d0d14;
    outline-offset: 3px;
    cursor: move;
    /* In the saved file a form's boxes sit above the page's own content, so what is added shows under them,
       unless the fields are locked (then they are part of the page and the additions go on top). */
    z-index: 1;
}
.fp-ann.is-selected, .fp.is-locked .fp-ann { z-index: 3; }
.fp-ann:hover { outline: 1px dashed #1F4E9E; }
.fp-ann.is-selected, .fp-ann:focus-visible { outline: 2px solid #1F4E9E; }
.fp-ann-tick, .fp-ann-cross, .fp-ann-image { touch-action: none; }
.fp-ann-text textarea {
    display: block;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    white-space: pre;
    resize: none;
    color: inherit;
    background: transparent;
    border: 0;
    outline: none;
    font: calc(var(--k) * var(--fs) * 1px) / 1.2 Helvetica, Arial, "Liberation Sans", sans-serif;
    cursor: text;
}
.fp-ann svg { display: block; width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: var(--mark, .12); stroke-linecap: round; stroke-linejoin: round; }
.fp-ann canvas { display: block; width: 100%; height: 100%; }
.fp-grip, .fp-size { display: none; position: absolute; background: #1F4E9E; touch-action: none; }
.is-selected > .fp-grip, .is-selected > .fp-size { display: block; }
.fp-grip {
    left: -36px;
    top: -2px;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    cursor: grab;
    background-image: radial-gradient(circle, #fff 1.6px, transparent 1.8px);
    background-size: 8px 8px;
    background-position: 4px 4px;
    background-color: #1F4E9E;
}
.fp-size { right: -10px; bottom: -10px; width: 18px; height: 18px; border: 2px solid #fff; border-radius: 50%; cursor: nwse-resize; box-shadow: 0 1px 4px rgba(0, 0, 0, .35); }
@media (pointer: coarse) {
    .fp-size { right: -16px; bottom: -16px; width: 30px; height: 30px; }
}

/* ---------- Side panel ---------- */
.fp-side { display: grid; gap: 14px; min-width: 0; position: sticky; top: 76px; max-height: calc(100vh - 92px); overflow-y: auto; }
.fp-card { padding: 14px; background: var(--paper); border: 1px solid var(--line); border-radius: 8px; }
.fp-card-title { font-size: 1rem; font-weight: 600; }
summary.fp-card-title { cursor: pointer; min-height: 28px; }
.fp-count { margin-left: 6px; padding: 1px 8px; font-size: .8rem; font-weight: 600; color: var(--seal-ink); background: var(--ink-soft); border-radius: 999px; }
.fp-count:empty { display: none; }
.fp-empty { margin-top: 6px; color: var(--ink-soft); font-size: .88rem; }
.fp-card-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.fp-sign-preview { display: block; width: 100%; height: 72px; margin-top: 10px; padding: 6px; object-fit: contain; background: #fff; border: 1px solid var(--line); border-radius: 6px; }
.fp-pagehead { margin: 16px 0 6px; color: var(--ink-soft); font-size: .78rem; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; }
.fp-field { display: grid; gap: 4px; margin-top: 10px; }
.fp-field label, .fp-field legend { font-size: .9rem; font-weight: 500; }
.fp-field input[type="text"], .fp-field input[type="password"], .fp-field select, .fp-field textarea {
    width: 100%;
    min-height: 44px;
    padding: 8px 10px;
    font: inherit;
    font-size: 1rem;   /* 16px or more, so iOS does not zoom the page when this is focused */
    color: var(--ink);
    background: var(--sheet);
    border: 1px solid var(--line);
    border-radius: 8px;
}
.fp-field select[multiple] { min-height: 96px; }
.fp-field fieldset { border: 0; display: grid; gap: 2px; }
.fp-choice { display: flex !important; align-items: center; gap: 10px; min-height: 36px; font-weight: 400 !important; }
.fp-choice input { width: 20px; height: 20px; accent-color: var(--seal); flex: none; }
.fp-field input:disabled, .fp-field select:disabled, .fp-field textarea:disabled,
.fp-field input[readonly], .fp-field textarea[readonly] { opacity: .6; }

/* ---------- Actions ---------- */
.fp-lock { display: flex; align-items: flex-start; gap: 10px; flex: 1 1 260px; font-size: .92rem; font-weight: 500; cursor: pointer; }
.fp-lock input { width: 20px; height: 20px; margin-top: 3px; accent-color: var(--seal); flex: none; }
.fp-lock small { display: block; color: var(--ink-soft); font-weight: 400; font-size: .82rem; }
.fp-lock.is-disabled { opacity: .5; cursor: not-allowed; }
#previewSection .preview-actions { margin-top: 14px; align-items: center; }
.preview-actions .status { flex: 1 0 100%; min-height: 0; margin: 0; }
.preview-actions .status:empty { display: none; }
@media (min-width: 821px) {
    #previewSection .preview-actions { justify-content: space-between; }
    #previewSection .preview-actions .btn { flex: 0 1 auto; min-width: 200px; }
    #previewSection .preview-actions .fp-lock { flex: 1 1 0; }
}
@media (max-width: 820px) {
    #previewSection .preview-actions { gap: 8px; }
    #previewSection .preview-actions .btn { flex: 1 1 0; min-width: 0; padding-inline: 10px; }
    #previewSection .preview-actions .fp-lock { order: -1; flex: 1 0 100%; }
}

/* ---------- Narrow screens ---------- */
@media (max-width: 900px) {
    /* The signature card shrinks to one row so the page is not pushed off the screen. */
    .fp-sign { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; padding: 10px 14px; }
    .fp-sign .fp-empty { display: none; }
    .fp-sign .fp-card-actions { margin-top: 0; }
    .fp-sign .fp-sign-preview { width: auto; max-width: 150px; height: 44px; margin: 0; }
    .fp-body { grid-template-columns: minmax(0, 1fr); }
    .fp-side { order: -1; position: static; max-height: none; overflow: visible; }
    .fp-end { margin-left: 0; }
}
@media (max-width: 640px) {
    .fp-bar { gap: 2px 8px; }
    .fp-tool { padding: 0; }
    /* Icons only: the names stay for screen readers. */
    .fp-tool span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
    .fp-pages-wrap { padding: 8px; }
}

/* ---------- Signature dialog (js/signature.js) ---------- */
.sg {
    /* The site's reset removes a dialog's auto margins, so centre it explicitly. */
    position: fixed;
    inset: 0;
    margin: auto;
    width: min(94vw, 580px);
    height: fit-content;
    max-width: none;
    max-height: 94vh;
    max-height: 94dvh;
    overflow-y: auto;
    padding: 0;
    color: var(--ink);
    background: var(--sheet);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .55);
}
.sg::backdrop { background: rgba(10, 16, 32, .68); }
.sg[open] { animation: lb-in .16s ease-out; }
.sg-bar { display: flex; align-items: center; justify-content: space-between; padding: 6px 6px 6px 18px; border-bottom: 1px solid var(--line); }
.sg-bar h2 { font-size: 1.05rem; }
.sg-x { display: grid; place-items: center; width: 44px; height: 44px; color: var(--ink); background: transparent; border: 0; border-radius: 8px; cursor: pointer; }
.sg-x:hover { background: color-mix(in srgb, var(--ink) 8%, transparent); }
.sg-body { padding: 14px 18px 4px; }
.sg-tabs { display: flex; gap: 4px; padding: 3px; background: var(--paper); border: 1px solid var(--line); border-radius: 10px; }
.sg-tabs [role="tab"] { flex: 1; min-height: 40px; font: inherit; font-weight: 500; color: var(--ink-soft); background: transparent; border: 0; border-radius: 8px; cursor: pointer; }
.sg-tabs [aria-selected="true"] { color: var(--ink); background: var(--sheet); box-shadow: 0 1px 3px rgba(20, 33, 61, .18); }
.sg-panel { margin-top: 14px; }
.sg-pad, .sg-preview { display: block; width: 100%; background: #fff; border: 1px solid var(--line); border-radius: 8px; }
.sg-pad {
    aspect-ratio: 10 / 3;
    height: auto;
    cursor: crosshair;
    touch-action: none;
    background-image: linear-gradient(#c8cfdd, #c8cfdd);
    background-size: calc(100% - 40px) 1px;
    background-position: center 78%;
    background-repeat: no-repeat;
}
.sg-preview { height: 110px; margin-top: 12px; object-fit: contain; }
.sg-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 10px; }
.sg-ink { display: flex; gap: 8px; }
.sg-swatch { width: 36px; height: 36px; border: 3px solid var(--sheet); border-radius: 50%; cursor: pointer; box-shadow: 0 0 0 1px var(--line); }
.sg-swatch[data-ink="blue"] { background: #1b2f7a; }
.sg-swatch[data-ink="black"] { background: #111114; }
.sg-swatch[aria-checked="true"] { box-shadow: 0 0 0 2px var(--seal); }
.sg-label { display: block; font-weight: 500; font-size: .95rem; }
.sg-input { width: 100%; min-height: 48px; margin-top: 6px; padding: 8px 12px; font: inherit; font-size: 1.1rem; color: var(--ink); background: var(--sheet); border: 1px solid var(--line); border-radius: 8px; }
.sg-styles { display: flex; gap: 8px; margin-top: 10px; }
.sg-style { flex: 1; min-height: 40px; font: inherit; font-weight: 500; color: var(--ink); background: transparent; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.sg-style[aria-checked="true"] { border-color: var(--seal); box-shadow: inset 0 0 0 1px var(--seal); }
.sg-help { color: var(--ink-soft); font-size: .92rem; margin-bottom: 10px; }
.sg-check { display: flex; align-items: center; gap: 10px; margin-top: 12px; font-size: .92rem; }
.sg-check input { width: 18px; height: 18px; accent-color: var(--seal); }
.sg-error { min-height: 1.4em; margin-top: 8px; color: var(--danger); font-size: .9rem; }
.sg-note { margin: 4px 0 12px; color: var(--ink-soft); font-size: .84rem; }
.sg-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 12px 18px 16px; border-top: 1px solid var(--line); }
.sg-foot .btn { min-width: 130px; }

@media (prefers-reduced-motion: reduce) {
    .fp-flash, .sg[open] { animation: none; }
}
