/*
 * Mateen Labs: page-based PDF tools (rotate, organize, page numbers, watermark, PDF to JPG).
 * Builds on /tools/assets/file-tool.css and the site tokens, so light and dark mode follow the site.
 * Mobile first: nothing here may be wider than a 320px screen.
 */

.pdf-app { min-width: 0; }

.pdf-filebar { margin: 0 0 14px; flex-wrap: wrap; }
.pdf-filebar .tool-actions { margin: 0; }

[hidden] { display: none !important; }

/* ---------- Toolbars and options ---------- */

.pdf-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 10px;
    margin: 10px 0;
}

.pdf-bar > .pdf-bar-label { color: var(--muted); font-size: 14px; font-weight: 700; }

.pdf-bar button,
.pdf-btn {
    min-height: 44px;
    padding: 8px 14px;
    border: 1px solid var(--line-strong);
    border-radius: 11px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: 14px;
    font-weight: 650;
    cursor: pointer;
}

.pdf-bar button:hover:not(:disabled),
.pdf-btn:hover:not(:disabled) { border-color: var(--accent); background: var(--panel); }

.pdf-bar button:disabled,
.pdf-btn:disabled { opacity: .5; cursor: not-allowed; }

.pdf-bar button.primary,
.pdf-btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.pdf-bar button.primary:hover:not(:disabled),
.pdf-btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }

.pdf-bar button.danger { border-color: var(--err-line); color: var(--err-text); background: var(--err-bg); }

.pdf-bar button:focus-visible,
.pdf-btn:focus-visible,
.pg-card button:focus-visible,
.pg-card input:focus-visible,
.pos-grid input:focus-visible + span,
.pdf-bar input:focus-visible,
.file-options select:focus-visible,
.file-options input:focus-visible { outline: none; box-shadow: var(--ring); }

.pdf-bar input[type="text"] {
    flex: 1 1 140px;
    min-width: 0;
    max-width: 260px;
    min-height: 44px;
    padding: 6px 12px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font: inherit;
}

.pdf-count { color: var(--text); font-size: 15px; font-weight: 700; }
.pdf-scope { color: var(--muted); font-size: 14px; }

.pdf-field-error {
    margin: 0;
    color: var(--err-text);
    font-size: 14px;
    font-weight: 600;
}

.pdf-note {
    margin: 0;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-left: 4px solid var(--accent);
    border-radius: 10px;
    background: var(--panel);
    color: var(--text);
    font-size: 14px;
    line-height: 1.55;
    overflow-wrap: anywhere;
}

.pdf-note.is-warning {
    border-color: var(--warn-line);
    border-left-color: var(--warn-text);
    background: var(--warn-bg);
    color: var(--warn-text);
}

.opt-row input[type="number"] { width: 110px; }

.opt-row .opt-text { flex: 1 1 200px; min-width: 0; }

.opt-row input[aria-invalid="true"] { border-color: var(--err-text); }

.pdf-split {
    display: grid;
    gap: 18px;
    align-items: start;
}

@media (min-width: 900px) {
    .pdf-split { grid-template-columns: minmax(0, 1fr) minmax(280px, 380px); }
}

.pdf-preview {
    min-width: 0;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel);
    text-align: center;
}

.pdf-preview h2 { margin: 0 0 10px; font-size: 16px; }

.pdf-preview canvas {
    display: block;
    max-width: 100%;
    height: auto;
    margin: 0 auto;
    border-radius: 4px;
    background: #fff;
    box-shadow: var(--shadow-md);
}

.pdf-preview-note { margin: 10px 0 0; color: var(--muted); font-size: 13px; }

/* ---------- 3 x 3 position picker ---------- */

.pos-grid {
    display: grid;
    grid-template-columns: repeat(3, 48px);
    gap: 6px;
    margin: 0;
    padding: 0;
    border: 0;
}

.pos-grid legend { margin-bottom: 6px; padding: 0; font-weight: 700; }

.pos-grid label { position: relative; display: block; width: 48px; height: 48px; }

.pos-grid input {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    opacity: 0;
    cursor: pointer;
}

.pos-grid.opt-row > label { min-width: 0; }

.pos-grid span[aria-hidden="true"] {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    border: 1px solid var(--line-strong);
    border-radius: 10px;
    background: var(--surface);
}

.pos-grid span[aria-hidden="true"]::after {
    content: "";
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--line-strong);
}

.pos-grid input:checked + span { border-color: var(--accent); background: var(--accent-soft); }
.pos-grid input:checked + span::after { background: var(--accent); }

.pos-grid .sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Page grid ---------- */

.pg-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(150px, 100%), 1fr));
    gap: 12px;
    margin: 12px 0;
    padding: 0;
    list-style: none;
}

.pg-card {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
    padding: 8px;
    border: 2px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
}

.pg-card.is-selected { border-color: var(--accent); background: var(--accent-soft); }
.pg-card.is-dragging { opacity: .75; border-style: dashed; border-color: var(--accent); box-shadow: var(--shadow-md); z-index: 2; }
.pg-card.drop-before { box-shadow: -6px 0 0 var(--accent); }
.pg-card.drop-after { box-shadow: 6px 0 0 var(--accent); }
.pg-card.is-changed .pg-badge { display: block; }
.pg-thumb {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 170px;
    overflow: hidden;
    border-radius: 8px;
    background: var(--panel);
    color: var(--muted);
    font-size: 13px;
}

.pg-thumb canvas {
    display: block;
    flex: 1 1 auto;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.pg-hint {
    position: absolute;
    inset: auto 0 8px 0;
    text-align: center;
    pointer-events: none;
}

.pg-thumb canvas[data-state="done"] ~ .pg-hint { display: none; }
.pg-thumb canvas[data-state="failed"] ~ .pg-hint::after { content: " (no preview)"; }

.pg-label {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
}

.pg-label input { flex: none; width: 22px; height: 22px; margin: 0; accent-color: var(--accent); }

.pg-badge {
    display: none;
    position: absolute;
    top: 6px;
    right: 6px;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--accent);
    color: var(--on-accent);
    font-size: 12px;
    font-weight: 800;
}

.pg-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
}

.pg-actions button {
    flex: 1 1 44px;
    min-width: 44px;
    min-height: 44px;
    padding: 4px;
    border: 1px solid var(--line-strong);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
}

.pg-actions button:hover:not(:disabled) { border-color: var(--accent); background: var(--panel); }
.pg-actions button:disabled { opacity: .4; cursor: not-allowed; }

.pg-actions .pg-drag { cursor: grab; touch-action: none; }
.pg-card.is-dragging .pg-drag { cursor: grabbing; }

.pg-live {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

/* ---------- Result ---------- */

.pdf-result {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px 16px;
    margin: 16px 0;
    padding: 14px 16px;
    border: 1px solid var(--ok-line);
    border-radius: var(--radius);
    background: var(--ok-bg);
}

.pdf-result-text { flex: 1 1 200px; margin: 0; color: var(--ok-text); font-weight: 650; overflow-wrap: anywhere; }

@media (max-width: 560px) {
    .pdf-result .result-link { width: 100%; justify-content: center; box-sizing: border-box; }
}

/* ---------- PDF to JPG results ---------- */

.img-list { display: grid; gap: 8px; margin: 12px 0; padding: 0; list-style: none; }

.img-list li {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 14px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
}

.img-list .img-name { flex: 1 1 180px; min-width: 0; font-weight: 700; overflow-wrap: anywhere; }
.img-list .img-meta { flex: 1 1 160px; color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.img-list a { min-height: 44px; display: inline-flex; align-items: center; padding: 6px 14px; border: 1px solid var(--line-strong); border-radius: 10px; color: var(--accent-text); font-weight: 700; text-decoration: none; }
.img-list a:hover { background: var(--panel); }
.img-list a:focus-visible { outline: none; box-shadow: var(--ring); }

.opt-group { display: grid; gap: 16px; min-width: 0; }
.opt-group[hidden] { display: none; }

/* Safari ignores min-height on native selects, so draw our own arrow (keeps the 44px target). */
.pdf-app select {
    -webkit-appearance: none;
    appearance: none;
    min-height: 44px;
    padding-right: 38px;
    background-image:
        linear-gradient(45deg, transparent 50%, var(--muted) 50%),
        linear-gradient(135deg, var(--muted) 50%, transparent 50%);
    background-position: calc(100% - 20px) 52%, calc(100% - 14px) 52%;
    background-size: 6px 6px;
    background-repeat: no-repeat;
}

/* On wider screens the cards are wide enough for three 44px buttons in one row. */
@media (min-width: 640px) {
    .pg-grid { grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); }
}
