/* Styles for the tool pages (images-to-pdf.html, organize-pdf.html), loaded after styles.css.
   Everything uses the tokens from styles.css, so light and dark both work.

   Page grid (js/page-grid.js builds this markup; a tool only supplies an empty <ol class="pg-list">)
     ol.pg-list                 responsive grid of cards; gets .pg-sorting while a card is being dragged
     li.pg-card                 one item, focusable (one tab stop for the whole grid, arrow keys walk it)
                                [data-thumb="idle|loading|ready|error"]  state of its thumbnail
                                .pg-dragging                             the card being dragged
                                .pg-drop-before / .pg-drop-after         drop marker on the card it would land beside
       .pg-thumb                fixed 3:4 frame (so nothing shifts as thumbnails arrive)
                                CSS variables: --ar (canvas aspect), --rot (turn), --fit (scale after a quarter turn)
         canvas.pg-canvas       the thumbnail; rotated by a transform, never redrawn
         .pg-num                position badge
         button.pg-grip         drag handle
         span.pg-src.src-tag    source label (A, B, ...) when a tool passes badge(); [data-tone="0..5"] picks the colour
         button.pg-open         Enlarge, only when the list has .pg-openable (the tool passed onOpen)
       .pg-actions              button row
         button.pg-btn          .pg-rotate-left  .pg-rotate-right  .pg-remove  .pg-earlier  .pg-later

   Page level, shared by the tool pages
     .pg-head                   title and tools row above the grid
     .pg-count                  "3 images" / "12 pages"
     .pg-tools                  group of small buttons
     .work-tool                 modifier on .work: status line inside .preview-actions, narrower Start over button
     .btn-small                 44px-high secondary button for toolbars
     .src-tag                   small coloured label for "which file is this from"

   Page viewer (js/lightbox.js builds this markup into a <dialog class="lb">)
     .lb-bar  .lb-stage (.lb-nav .lb-view > canvas.lb-canvas)  .lb-tools
*/

/* ---------- Grid ---------- */
.pg-list {
    position: relative;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));   /* two columns fit a 390px phone */
    gap: 12px;
    list-style: none;
    margin: 14px 0 0;
    padding: 0;
}
.pg-list.pg-sorting { cursor: grabbing; -webkit-user-select: none; user-select: none; }

.pg-card {
    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--sheet);
    border: 1px solid var(--line);
    border-radius: 8px;
    /* Off-screen cards skip layout and paint; the remembered size keeps the scrollbar steady. */
    content-visibility: auto;
    contain-intrinsic-size: auto 150px auto 310px;
}
.pg-card:focus-within { border-color: var(--seal); }
.pg-card:focus-visible { border-radius: 8px; }   /* the global focus rule would square the corners */
.pg-card.pg-dragging { z-index: 20; opacity: .92; box-shadow: var(--shadow); cursor: grabbing; }
.pg-drop-before::after,
.pg-drop-after::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    width: 5px;
    border-radius: 3px;
    background: var(--seal);
    z-index: 3;
    pointer-events: none;
}
.pg-drop-before::after { left: 0; }
.pg-drop-after::after { right: 0; }

/* ---------- Thumbnail frame ---------- */
.pg-thumb {
    --pg-frame-h: 133.3333%;   /* frame height as a share of its width: 4 / 3 */
    position: relative;
    aspect-ratio: 3 / 4;
    overflow: hidden;
    background: var(--paper);
    border-radius: 7px 7px 0 0;
    border-bottom: 1px solid var(--line);
}
.pg-canvas {
    position: absolute;
    left: 50%;
    top: 50%;
    /* Contain-fit inside the frame whatever the page's shape; the percentages are of the frame width. */
    width: min(100%, calc(var(--pg-frame-h) * var(--ar, .75)));
    height: auto;
    aspect-ratio: var(--ar, .75);
    background: #fff;
    box-shadow: 0 0 0 1px rgba(20, 33, 61, .18), 0 2px 6px rgba(20, 33, 61, .2);
    transform: translate(-50%, -50%) rotate(var(--rot, 0deg)) scale(var(--fit, 1));
    transition: transform .2s ease;
}
.pg-card[data-thumb="loading"] .pg-canvas { animation: pg-pulse 1.2s ease-in-out infinite alternate; }
@keyframes pg-pulse { from { opacity: .55; } to { opacity: 1; } }
.pg-card[data-thumb="error"] .pg-thumb::after {
    content: 'No preview';
    position: absolute;
    left: 0;
    right: 0;
    bottom: 12px;
    text-align: center;
    font-size: .8rem;
    color: var(--ink-soft);
}

.pg-num {
    position: absolute;
    left: 8px;
    top: 8px;
    z-index: 2;
    min-width: 26px;
    height: 26px;
    padding: 0 8px;
    display: grid;
    place-items: center;
    border-radius: 13px;
    background: var(--ink);
    color: var(--paper);
    font-size: .8rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    pointer-events: none;
}
.pg-grip {
    position: absolute;
    right: 4px;
    top: 4px;
    z-index: 2;
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 8px;
    background: color-mix(in srgb, var(--sheet) 84%, transparent);
    color: var(--ink-soft);
    cursor: grab;
    touch-action: none;   /* this handle only: the rest of the card still scrolls with a finger */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}
.pg-grip:hover { color: var(--ink); background: var(--sheet); }
.pg-grip:focus-visible { outline-offset: -3px; border-radius: 8px; }

/* ---------- Card buttons ---------- */
.pg-actions {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 2px;
    padding: 4px;
}
.pg-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 44px;
    min-width: 44px;
    padding: 0 4px;
    font: inherit;
    font-size: .85rem;
    font-weight: 500;
    color: var(--ink);
    background: transparent;
    border: 0;
    border-radius: 6px;
    cursor: pointer;
}
.pg-btn svg { flex: none; }
.pg-btn:focus-visible { outline-offset: -3px; border-radius: 6px; }
.pg-btn:hover:not(:disabled) { color: var(--seal); background: color-mix(in srgb, var(--seal) 10%, transparent); }
.pg-btn:disabled { opacity: .35; cursor: default; }
.pg-remove:hover:not(:disabled) { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
/* Row one: rotate left, rotate right, remove. Row two: earlier, later (the wide, easy targets for a thumb). */
.pg-rotate-left, .pg-rotate-right, .pg-remove { grid-column: span 2; }
.pg-earlier, .pg-later { grid-column: span 3; }

/* ---------- Tool page pieces ---------- */
.pg-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px 16px; flex-wrap: wrap; }
.pg-count { color: var(--ink-soft); font-size: .9rem; font-variant-numeric: tabular-nums; }
.pg-tools { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-small { min-height: 44px; padding: 8px 14px; font-size: .95rem; }
#uploadError:empty, .upload-section .status:empty { display: none; }
/* The status line lives in the action bar, so it stays in view when that bar sticks to the bottom of a phone screen. */
.work-tool .preview-actions .status { flex: 1 1 100%; margin: 0; }
.work-tool .preview-actions .status:empty { display: none; }
.work-tool .preview-actions .btn-secondary { flex: 1 1 110px; }
.work-tool .preview-actions .btn-success { flex: 2 1 180px; }
.sheet.drag-over { outline: 2px dashed var(--seal); outline-offset: -2px; }
.control select:disabled { opacity: .55; cursor: not-allowed; }

/* ---------- Source label and Enlarge button on a card ---------- */
.src-tag {
    --tone: #1F4E9E;
    display: inline-grid;
    place-items: center;
    flex: none;
    min-width: 22px;
    height: 22px;
    padding: 0 7px;
    border-radius: 11px;
    background: var(--tone);
    color: #fff;
    font-size: .75rem;
    font-weight: 700;
    line-height: 1;
}
.src-tag[data-tone="1"] { --tone: #B45309; }
.src-tag[data-tone="2"] { --tone: #15803D; }
.src-tag[data-tone="3"] { --tone: #9D174D; }
.src-tag[data-tone="4"] { --tone: #6D28D9; }
.src-tag[data-tone="5"] { --tone: #0F766E; }
.pg-src { position: absolute; left: 8px; bottom: 8px; z-index: 2; pointer-events: none; box-shadow: 0 0 0 2px color-mix(in srgb, var(--sheet) 85%, transparent); }

.pg-open {
    position: absolute;
    right: 4px;
    bottom: 4px;
    z-index: 2;
    width: 44px;
    height: 44px;
    display: none;
    place-items: center;
    border: 0;
    border-radius: 8px;
    background: color-mix(in srgb, var(--sheet) 84%, transparent);
    color: var(--ink-soft);
    cursor: zoom-in;
}
.pg-openable .pg-open { display: grid; }
.pg-openable .pg-thumb { cursor: zoom-in; }
.pg-open:hover { color: var(--ink); background: var(--sheet); }
.pg-open:focus-visible { outline-offset: -3px; border-radius: 8px; }

/* ---------- Page viewer ---------- */
html:has(.lb[open]) { overflow: hidden; }
.lb {
    position: fixed;
    inset: 0;
    margin: auto;
    width: min(96vw, 1120px);
    height: min(94vh, 980px);
    height: min(94dvh, 980px);
    max-width: none;
    max-height: none;
    padding: 0;
    overflow: hidden;
    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);
}
.lb[open] { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; animation: lb-in .16s ease-out; }
.lb::backdrop { background: rgba(10, 16, 32, .68); animation: lb-fade .16s ease-out; }
@keyframes lb-in { from { opacity: 0; transform: translateY(8px) scale(.985); } }
@keyframes lb-fade { from { opacity: 0; } }

.lb-bar { display: flex; align-items: center; gap: 12px; padding: 4px 6px 4px 18px; border-bottom: 1px solid var(--line); }
.lb-title { flex: 1; min-width: 0; font-size: 1rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-btn, .lb-nav, .lb-act { font: inherit; color: var(--ink); background: transparent; border: 0; cursor: pointer; }
.lb-close { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 8px; }
.lb-close:hover { background: color-mix(in srgb, var(--ink) 8%, transparent); }

.lb-stage {
    position: relative;
    display: grid;
    grid-template-columns: 60px minmax(0, 1fr) 60px;
    min-height: 0;
    padding-block: 14px;
    background: color-mix(in srgb, var(--paper) 72%, var(--line));
}
.lb-view { position: relative; display: grid; place-items: center; min-width: 0; min-height: 0; overflow: hidden; touch-action: pan-y pinch-zoom; }
.lb-view:focus { outline: none; }
.lb-canvas { display: block; max-width: 100%; max-height: 100%; background: #fff; box-shadow: 0 0 0 1px rgba(20, 33, 61, .18), 0 8px 28px rgba(20, 33, 61, .3); }
.lb-view[data-state="loading"]::after {
    content: '';
    position: absolute;
    inset: 0;
    margin: auto;
    width: 30px;
    height: 30px;
    border: 3px solid var(--line);
    border-top-color: var(--seal);
    border-radius: 50%;
    animation: lb-spin .8s linear infinite;
}
.lb-view[data-state="error"]::after { content: 'This page could not be drawn.'; color: var(--ink-soft); }
@keyframes lb-spin { to { transform: rotate(360deg); } }

.lb-nav { display: grid; place-items: center; width: 100%; color: var(--ink-soft); }
.lb-nav:hover:not(:disabled) { color: var(--seal); background: color-mix(in srgb, var(--seal) 8%, transparent); }
.lb-nav:disabled { opacity: .25; cursor: default; }
.lb-nav:focus-visible, .lb-act:focus-visible, .lb-close:focus-visible { outline-offset: -3px; border-radius: 8px; }

.lb-tools { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px)); border-top: 1px solid var(--line); }
.lb-act { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 14px; font-weight: 500; border: 1px solid var(--line); border-radius: 8px; }
.lb-act:hover:not(:disabled) { border-color: var(--ink); }
.lb-remove:hover:not(:disabled) { color: var(--danger); border-color: var(--danger); }
.lb-act:disabled { opacity: .4; cursor: not-allowed; }

@media (max-width: 820px) {
    .lb { width: 100vw; height: 100vh; height: 100dvh; border: 0; border-radius: 0; }
    .lb-stage { grid-template-columns: minmax(0, 1fr); padding: 10px 8px; }
    /* The arrows float over the page edges; swiping works too. */
    .lb-nav {
        position: absolute;
        top: 50%;
        z-index: 2;
        width: 44px;
        height: 72px;
        margin-top: -36px;
        border-radius: 10px;
        background: color-mix(in srgb, var(--sheet) 82%, transparent);
        box-shadow: 0 1px 6px rgba(0, 0, 0, .25);
    }
    .lb-prev { left: 6px; }
    .lb-next { right: 6px; }
    .lb-act { flex: 1 1 0; justify-content: center; padding: 0 8px; }
}

/* On a phone the three actions share one row as icons; their names stay for screen readers. */
@media (max-width: 480px) {
    .lb-act span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
    .lb-act { min-width: 56px; }
}

@media (prefers-reduced-motion: reduce) {
    .lb[open], .lb::backdrop { animation: none; }
    .lb-view[data-state="loading"]::after { animation-duration: 2.4s; }
}

/* Keep a focused card clear of the sticky action bar on phones. */
@media (max-width: 820px) {
    html { scroll-padding-bottom: 150px; }
}

@media (prefers-reduced-motion: reduce) {
    .pg-canvas { transition: none; animation: none !important; }
}
