:root {
    --viewer-primary: #00A5E4;
    --viewer-bg: #0b1320;
    /* Higher alpha than the previous round: that version relied on
       backdrop-filter: blur() to stay readable over the PDF behind it.
       Blur is removed (expensive, not needed for a solid dark toolbar),
       so this surface is made more opaque on its own instead. */
    --viewer-surface: rgba(13, 22, 34, 0.88);
    --viewer-border: rgba(255, 255, 255, 0.14);
    --viewer-text-muted: #c6d6e3;
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    width: 100%;
    height: 100%;
    background: var(--viewer-bg);
    font-family: 'Segoe UI', Tahoma, Arial, sans-serif;
    overflow: hidden;
}

/* Visible, consistent focus ring across the viewer's otherwise
   borderless dark chrome -- without this, keyboard focus on the
   topbar/bottombar controls was effectively invisible. */
a:focus-visible,
button:focus-visible,
input:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 2px;
}

.viewer-topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    /* Kept as a single fixed row on purpose (no flex-wrap): the stage
       inset below reserves a fixed 64px for exactly this row's height.
       If this were allowed to wrap onto a second line (long title +
       logo + several 44px locale pills), that second line would sit
       underneath the fixed-height inset and get covered by the PDF
       instead of the toolbar growing to fit it. Overflow is prevented
       instead via .viewer-brand/.viewer-brand-text shrinking (flex:1,
       min-width:0, text ellipsis) and .viewer-locales scrolling
       internally rather than wrapping or pushing off wider than 100%. */
    flex-wrap: nowrap;
    overflow: hidden;
    padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
    background: linear-gradient(180deg, rgba(0,0,0,0.65), rgba(0,0,0,0));
    color: #fff;
}

.viewer-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    /* flex:1 lets the brand take whatever space .viewer-locales doesn't
       need, and min-width:0 is required for its own ellipsis/shrink
       behavior (and .viewer-brand-text's) to actually take effect
       inside a flex row instead of being ignored by the default
       min-width:auto on flex items. */
    flex: 1 1 auto;
    min-width: 0;
}

.viewer-brand-text {
    min-width: 0;
}

.viewer-brand img {
    height: 36px;
    width: auto;
    border-radius: 6px;
    background: #fff;
    flex-shrink: 0;
}

/* The fallback THD mark (no profile logo set) sits on the same dark
   gradient as the rest of the topbar, so it gets a little breathing
   room/padding instead of butting directly against the title text. */
.viewer-brand-fallback {
    padding: 3px;
}

.viewer-brand h1 {
    font-size: 15px;
    margin: 0;
    color: var(--viewer-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.viewer-brand p {
    font-size: 11px;
    margin: 2px 0 0;
    color: var(--viewer-text-muted);
    max-width: 320px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.viewer-locales {
    display: flex;
    align-items: center;
    gap: 6px;
    /* Never wraps (would push a second row under the fixed-height
       topbar) and never shrinks its pills below 44px -- instead, once
       there isn't room for all of them, this strip scrolls
       horizontally within its own bounded max-width. All locale links
       stay reachable (none are ever hidden), just via a scroll instead
       of a wrap. */
    flex: 0 1 auto;
    flex-wrap: nowrap;
    max-width: 48vw;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.viewer-locales a,
.viewer-locales-current {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: #fff;
    text-decoration: none;
    border: 1px solid var(--viewer-border);
    border-radius: 6px;
    padding: 4px 11px;
    font-size: 12px;
    min-height: 44px;
    min-width: 44px;
}

.viewer-locales a:hover {
    border-color: var(--viewer-primary);
    background: rgba(255, 255, 255, 0.08);
}

.viewer-locales-current {
    border-color: var(--viewer-primary);
    color: var(--viewer-primary);
    font-weight: 600;
}

.viewer-stage {
    position: fixed;
    /* Reserve space for the fixed topbar/bottombar instead of inset:0:
       with inset:0, "safe" start-alignment on an oversized/scrolled
       page puts its top-left at the stage's own top-left corner, which
       sits *underneath* the fixed topbar/bottombar — so scrolling to
       the start never actually reveals that content, it's hidden behind
       the chrome. Insetting the stage itself means the scrollable area's
       edges line up with the visible area between the bars.
       The bottom figure must cover the actual rendered bottombar height
       (controls row(s) + optional download/print row + its own padding)
       plus any device safe-area inset, or zoomed content can end up
       hidden behind the toolbar instead of reachable by scrolling — see
       the "without"/"with .has-extra-actions" values below and in the
       420px breakpoint, each sized from the real padding/row heights in
       .viewer-topbar/.viewer-bottombar/.viewer-controls, with a buffer. */
    inset: calc(64px + env(safe-area-inset-top)) 0 calc(88px + env(safe-area-inset-bottom)) 0;
    display: flex;
    /* "safe center": centers the page while it fits, but falls back to
       start-alignment instead of equally overflowing both directions
       once zoomed content is bigger than the viewport — without "safe",
       centered overflow leaves the left/top edge permanently
       unreachable even with overflow:auto (a well-known flexbox+scroll
       pitfall). */
    align-items: safe center;
    justify-content: safe center;
    gap: 6px;
    /* Zoomed-in content can exceed the viewport; let the stage scroll so
       every edge of the page is reachable (mouse wheel/drag-scroll on
       desktop always works here regardless of touch-action below). */
    overflow: auto;
    /* Swipe/pinch are handled entirely by our own touch handlers while
       the page is shown at its fit size, so default touch gestures are
       disabled to avoid fighting them. Once zoomed in (.zoomed, toggled
       by pdf-viewer.js), native touch panning is re-enabled — otherwise
       there would be no way to pan an enlarged page on a touch device. */
    touch-action: none;
}

/* Set on <body> by viewer.blade.php only when the optional download/
   print row actually renders, so the stage reserves the extra space
   that row needs -- without this, the default inset above assumes
   there is no such row and zoomed content could end up hidden behind
   it instead of reachable by scrolling. */
body.has-extra-actions .viewer-stage {
    inset: calc(64px + env(safe-area-inset-top)) 0 calc(140px + env(safe-area-inset-bottom)) 0;
}

.viewer-stage.zoomed {
    touch-action: pan-x pan-y;
}

.viewer-stage canvas {
    box-shadow: 0 10px 40px rgba(0,0,0,0.6);
    background: #fff;
    /* No max-width/max-height here: that previously capped the
       *displayed* size regardless of zoom, so zooming in rendered more
       pixels without the canvas ever actually growing on screen past
       that cap. Sizing is fully controlled by pdf-viewer.js
       (copyCanvas), which already fits the initial view to the
       viewport via computeFitScale(). */
    flex-shrink: 0;
}

/* Page-turn transition removed: speed/smoothness over decorative
   animation (see REVIEW-RESPONSE.md). The .reduced-motion class below
   still exists solely to disable the loading spinner's animation. */

.viewer-controls button:disabled,
#viewer-page-range:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

#viewer-loading, #viewer-error {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
    color: #fff;
    background: rgba(11, 19, 32, 0.92);
    text-align: center;
    padding: 20px;
}

/*
 * Root cause of the stuck-visible overlay bug: an ID selector
 * (#viewer-loading / #viewer-error, specificity 0,1,0,0) beats the
 * browser's default UA rule for the hidden attribute ([hidden],
 * specificity 0,0,1,0), so `display: flex` above was winning even when
 * the element had `hidden` set via JS. Re-assert display:none with an
 * ID+attribute selector (0,2,0,0) so the hidden attribute always wins.
 */
#viewer-loading[hidden], #viewer-error[hidden] {
    display: none;
}

.viewer-spinner {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 4px solid rgba(255,255,255,0.25);
    border-top-color: var(--viewer-primary);
    animation: viewer-spin 0.8s linear infinite;
}

.reduced-motion .viewer-spinner {
    animation: none;
}

@keyframes viewer-spin {
    to { transform: rotate(360deg); }
}

#viewer-error button {
    background: var(--viewer-primary);
    color: #06202c;
    border: none;
    border-radius: 8px;
    padding: 8px 18px;
    min-height: 44px;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
}

.viewer-bottombar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 10px 14px calc(16px + env(safe-area-inset-bottom));
    background: linear-gradient(0deg, rgba(0,0,0,0.7), rgba(0,0,0,0));
}

.viewer-controls {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--viewer-surface);
    border: 1px solid var(--viewer-border);
    border-radius: 999px;
    padding: 6px 12px;
    max-width: calc(100vw - 20px);
    box-sizing: border-box;
}

/* 44px is the usual minimum comfortable touch-target size, used as the
   default for every pointer/screen size. Under the 420px breakpoint
   below, these buttons wrap onto their own row (via .viewer-controls'
   flex-wrap) rather than shrinking, so they stay 44px everywhere. */
.viewer-controls button {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    background: rgba(255,255,255,0.12);
    color: #fff;
    cursor: pointer;
    font-size: 17px;
    line-height: 1;
    flex: 0 0 auto;
}

.viewer-controls button:hover {
    background: var(--viewer-primary);
    color: #06202c;
}

#viewer-page-range {
    width: 160px;
    flex: 1 1 60px;
    min-width: 0;
    accent-color: var(--viewer-primary);
}

#viewer-page-label {
    color: #fff;
    font-size: 12px;
    min-width: 70px;
    flex: 0 0 auto;
    text-align: center;
}

.viewer-extra-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
}

.viewer-extra-actions a {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    color: #fff;
    background: var(--viewer-surface);
    border: 1px solid var(--viewer-border);
    border-radius: 8px;
    padding: 6px 16px;
    font-size: 12px;
    text-decoration: none;
}

.viewer-extra-actions a:hover {
    border-color: var(--viewer-primary);
}

@media (max-width: 768px) {
    .viewer-brand h1 { font-size: 13px; }
    .viewer-brand p { display: none; }
    .viewer-brand img { height: 30px; }
    #viewer-page-range { width: 100px; }
}

/* Narrow phones (320–420px, e.g. 320/390/412 widths): 5 buttons at a
   full 44px + slider + label + gaps add up to well over 300px, which
   would clip the zoom-out/fullscreen buttons off-screen in a single
   row. Rather than shrinking the touch targets below 44px, the range
   slider + page label are pulled onto their own first row (via
   order: -1 and a combined width that leaves no room for a button
   next to them, forcing a wrap), and the five buttons wrap onto a
   second, centered row at their normal 44px size -- this is the
   "second toolbar row on very narrow screens" instead of smaller
   buttons. No JS/markup order changes; this is CSS-only via `order`. */
@media (max-width: 420px) {
    .viewer-controls {
        flex-wrap: wrap;
        justify-content: center;
        row-gap: 8px;
        border-radius: 20px;
        padding: 8px 10px;
    }
    #viewer-page-range {
        order: -1;
        flex: 1 1 65%;
        min-width: 0;
        width: auto;
    }
    #viewer-page-label {
        order: -1;
        flex: 0 0 auto;
        min-width: 54px;
    }

    /* Two 44px-tall rows (buttons + range/label) now make the controls
       pill taller than the single-row desktop/tablet case, so the
       stage must reserve more space -- otherwise zoomed content could
       end up hidden behind this toolbar instead of reachable by
       scrolling. Figures: ~114px controls pill (2x44px rows + row-gap
       + its own padding) + 10px top/16px bottom bottombar padding,
       rounded up with a buffer. */
    .viewer-stage {
        inset: calc(64px + env(safe-area-inset-top)) 0 calc(148px + env(safe-area-inset-bottom)) 0;
    }

    body.has-extra-actions .viewer-stage {
        /* Adds the optional download/print row's own ~44px height
           plus the bottombar's row gap on top of the figure above. */
        inset: calc(64px + env(safe-area-inset-top)) 0 calc(200px + env(safe-area-inset-bottom)) 0;
    }
}
