:root {
    --gap: 10px;
    --radius: 12px;

    /* Plain background follows system scheme */
    --page-bg: #fff;
    --page-fg: #000;

    --panel: rgba(10, 12, 16, 0.78);
    --panel2: rgba(10, 12, 16, 0.62);
    --text: #e8eef9;
    --muted: rgba(232, 238, 249, 0.72);

    --shadow: 0 14px 50px rgba(0, 0, 0, 0.55);
    --btn-bg: rgba(255, 255, 255, 0.18);
    --btn-bg-hover: rgba(255, 255, 255, 0.28);
    --btn-border: rgba(255, 255, 255, 0.30);
    --accent: #7aa2ff;

    /* Height used to keep nav hotspots from covering the topbar */
    --topbar-h: 56px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --page-bg: #000;
        --page-fg: #fff;
    }
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

body {
    margin: 0;
    background: var(--page-bg);
    color: var(--page-fg);
    font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ===== Gallery title ===== */
.galleryHeader {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
    margin: 0 0 14px 0;
}

.galleryTitle {
    margin: 0;
    font-size: clamp(20px, 2.6vw, 34px);
    letter-spacing: -0.02em;
    line-height: 1.1;
}

.galleryDescription {
    margin: 0;
    max-width: 70ch;
    font-size: 16px;
    line-height: 1.35;
    opacity: 0.78;
}

/* ===== Gallery grid ===== */
.wrap {
    max-width: 1400px;
    margin: 0 auto;
    padding: clamp(10px, 2.4vw, 26px);
}

.grid {
    display: grid;
    gap: var(--gap);
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    align-items: stretch;
}

@media (min-width: 520px) {
    .grid {
        grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
    }
}

@media (min-width: 900px) {
    .grid {
        grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    }
}

.tile {
    position: relative;
    border-radius: var(--radius);
    overflow: hidden;
    background: rgba(255, 255, 255, 0.06);
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.06) inset;
    cursor: zoom-in;
    user-select: none;
    touch-action: manipulation;
    transform: translateZ(0);
    border: 0;
    padding: 0;
}

.tile::before {
    content: "";
    display: block;
    padding-top: 72%;
}

.tile>img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: transform 180ms ease, filter 180ms ease;
    filter: saturate(1.02) contrast(1.02);
}

.tile:hover>img {
    transform: scale(1.035);
    filter: saturate(1.06) contrast(1.05);
}

.tile:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 3px;
}

/* ===== Lightbox overlay ===== */
.overlay {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;

    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));

    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    z-index: 999;
}

/*
  Mobile browsers (notably iOS Safari) can show black tile artifacts when
  backdrop-filter is combined with large, frequently-updated transforms.
  Use a plain scrim on coarse pointers to keep rendering stable.
*/
@media (hover: none),
(pointer: coarse) {
    .overlay {
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
    }

    /* Avoid keeping the image in a persistent accelerated layer on mobile; this
     reduces black-rectangle tile artifacts during zoom/pan on some browsers. */
    .viewer img {
        will-change: auto;
    }

    /* Hide zoom controls on mobile; pinch-to-zoom remains available. */
    #btnZoomIn,
    #btnZoomOut,
    #btnZoomReset {
        display: none;
    }
}

.overlay.open {
    display: flex;
}

/* Nearly full-screen viewer */
.viewer {
    position: relative;
    width: min(1600px, 98vw);
    height: calc(var(--vh, 1vh) * 94);

    border-radius: 14px;
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.06), rgba(255, 255, 255, 0.03));
    box-shadow: var(--shadow);
    overflow: hidden;

    /* Helps avoid compositing glitches on mobile. */
    isolation: isolate;
}

@media (max-width: 520px) {
    .viewer {
        width: 100vw;
        height: calc(var(--vh, 1vh) * 96);
        border-radius: 0;
    }
}

/* Stage holds the image and handles panning/zooming */
.stage {
    position: absolute;
    inset: 0;
    overflow: hidden;

    /* We handle pinch/drag ourselves */
    touch-action: none;
    cursor: default;
}

.stage.grab {
    cursor: grab;
}

.stage.grabbing {
    cursor: grabbing;
}

.viewer img {
    position: absolute;
    top: 0;
    left: 0;
    width: auto;
    height: auto;
    max-width: none;
    max-height: none;
    display: block;
    background: rgba(0, 0, 0, 0.25);

    transform: translate(0, 0) scale(1);
    transform-origin: 0 0;
    will-change: transform;

    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;

    /* Stop native image dragging UI / selection */
    -webkit-user-drag: none;
    user-select: none;

    /* Let stage receive pointer events */
    pointer-events: none;
}

/* Top bar */
.topbar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    min-height: var(--topbar-h);

    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: space-between;
    padding: 10px 10px;
    background: linear-gradient(180deg, var(--panel), transparent);

    pointer-events: none;
    z-index: 3;
}

.topbar .group {
    display: flex;
    gap: 8px;
    pointer-events: auto;
}

/* Bottom-centered zoom controls */
.viewer .bottomControls {
    position: absolute;
    left: 50%;
    bottom: calc(env(safe-area-inset-bottom) + 58px);
    transform: translateX(-50%);

    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;

    padding: 8px;
    border-radius: 999px;
    background: transparent;
    border: 0;

    z-index: 4;
    pointer-events: auto;
}

@media (max-width: 520px) {
    .viewer .bottomControls {
        bottom: calc(env(safe-area-inset-bottom) + 54px);
    }
}

/* Zoom overlay badge */
.zoomOverlay {
    position: absolute;
    top: calc(var(--topbar-h) + 10px);
    right: 12px;

    padding: 8px 10px;
    border-radius: 999px;

    background: rgba(10, 12, 16, 0.62);
    border: 1px solid rgba(255, 255, 255, 0.18);
    color: var(--text);

    font-weight: 700;
    font-size: 13px;
    line-height: 1;

    z-index: 4;
    pointer-events: none;

    opacity: 0;
    transform: translateY(-4px);
    transition: opacity 140ms ease, transform 140ms ease;
}

.zoomOverlay.show {
    opacity: 1;
    transform: translateY(0);
}

/* Bottom hint */
.hint {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 10px 12px;
    color: var(--muted);
    font-size: 13px;
    background: linear-gradient(0deg, var(--panel2), transparent);
    display: flex;
    justify-content: center;
    gap: 10px;
    pointer-events: none;
}

@media (max-width: 520px) {
    .hint {
        font-size: 12px;
    }
}

/* Buttons */
.btn {
    appearance: none;
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--text);
    padding: 10px 12px;
    border-radius: 12px;
    cursor: pointer;
    font-weight: 600;
    font-size: 14px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;

    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.22);
}

.btn:hover {
    background: var(--btn-bg-hover);
    border-color: rgba(255, 255, 255, 0.28);
}

.btn:active {
    transform: translateY(1px) scale(0.99);
}

.btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.btn:disabled {
    opacity: 0.55;
    cursor: default;
}

/* Big prev/next hotspots */
.navHotspot {
    position: absolute;
    top: var(--topbar-h);
    bottom: 0;
    width: 12%;
    min-width: 72px;

    display: flex;
    align-items: center;
    justify-content: center;

    cursor: pointer;
    background: transparent;
    border: 0;
    color: var(--text);

    opacity: 0.0;
    transition: opacity 160ms ease, background 160ms ease;
    -webkit-tap-highlight-color: transparent;

    z-index: 2;
}

.viewer:hover .navHotspot {
    opacity: 1;
}

.navHotspot:focus-visible {
    opacity: 1;
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.navHotspot:hover {
    background: rgba(255, 255, 255, 0.06);
}

.navHotspot[disabled] {
    pointer-events: none;
    opacity: 0 !important;
}

.navPrev {
    left: 0;
}

.navNext {
    right: 0;
}

.navHotspot .chev {
    width: 60px;
    height: 60px;
    border-radius: 999px;
    background: rgba(10, 12, 16, 0.55);
    border: 1px solid rgba(255, 255, 255, 0.18);
    display: grid;
    place-items: center;

    font-size: 30px;
    line-height: 1;
    font-weight: 700;
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
}

.navHotspot .chevGlyph {
    display: block;
    /* These chevron glyphs sit a bit low in most fonts. */
    transform: translateY(-0.08em);
}

@media (max-width: 520px) {
    .navHotspot .chev {
        width: 56px;
        height: 56px;
        font-size: 40px;
    }
}

@media (hover: none) {
    .navHotspot {
        opacity: 1;
    }
}
