/* timeline.css: the gallery viewer, the message list with its loading lines and pills, and the show-more button
   (js/ui/gallery.js and js/ui/timeline.js; templates t-gallery, t-messages, t-loading-older and t-pill). The rows
   are in message.css. css/README.md has the map of the stylesheets. */

/* ==== Gallery viewer ==== */
/* js/ui/gallery.js; t-gallery */

/* Full-screen media viewer. Layout and gestures are client-owned (see js/ui/gallery.js): the
   strip swipes with native scroll-snap, "fill" / "dragging" / "closing" are
   classes gallery.js toggles, --drag is a 0..1 inline variable it sets during a drag-to-close. */

.image-modal {
    position: fixed;
    inset: 0;
    z-index: 1001; /* over the pins panel (.pins-panel, 1000, frame.css): the viewer opens from a pin (SH-48). With
                      equal values it was on top only by the order of insertion. */
    --bg-alpha: 0.9;
    /* The backdrop follows the finger during a drag-to-close; the picture stays opaque. */
    background: rgb(0 0 0 / calc(var(--bg-alpha) * (1 - var(--drag, 0))));
}

/* The fade-out of the gallery (TL-27). gallery.js removes the element when it ends. */
.image-modal.closing {
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s;
}

/* ---- Strip: one viewport-sized stage per item, the browser snaps between them ---- */

.gallery-strip {
    position: absolute;
    inset: 0;
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
}

.gallery-strip::-webkit-scrollbar {
    display: none;              /* Safari before 18.2 ignores scrollbar-width */
}

.modal-stage {
    position: relative;
    flex: 0 0 100%;
    height: 100%;
    scroll-snap-align: start;
    scroll-snap-stop: always;   /* a fling moves one item, never three */
    display: flex;              /* fit mode: centres the picture */
    overflow: hidden;
    /* pan-x leaves horizontal swipes to the strip. No pan-y: vertical drags reach gallery.js
       (drag-to-close) instead of the browser, and native double-tap zoom is off, so the
       fill double-tap wins. Pinch stays native, as it always was. */
    touch-action: pan-x pinch-zoom;
    /* A pan inside a filled stage must stop at the picture edge, not leak into the strip and
       flip to the next item. (iOS < 16 lacks this and does leak; soft failure.) */
    overscroll-behavior: contain;
    transition: transform 0.2s;  /* springs back after a released drag-to-close */
}

.modal-stage.dragging {
    transition: none;           /* the picture follows the finger with no lag */
}

.modal-stage img {
    /* margin:auto centres a smaller picture but, unlike justify-content:center, lets a larger
       one overflow to the right only, so the left edge is always reachable by scrolling. */
    margin: auto;
    max-width: 85%;
    max-height: 85%;
    object-fit: contain;
    /* iOS: no save menu on a long press (gallery.js blocks the contextmenu event for the other browsers); the
       Download control is the one way to save (TL-24). user-select: no selection highlight on the hold either. */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* ---- Fill: the picture covers the viewport, native scroll is the pan ---- */

.modal-stage.fill {
    display: block;             /* block, not flex: the picture's own size drives the scroll area */
    overflow: auto;
    touch-action: pan-x pan-y pinch-zoom;   /* both axes pan the picture now */
}

/* Cover = scale along one axis and let the other overflow. Which axis depends on the picture's
   aspect against the stage's, which CSS cannot compare, so gallery.js adds fill-w for pictures
   narrower than the stage. (min-width/min-height tricks fail here: once one axis is specified
   the CSS 2.1 ratio table no longer applies, and the box stretches around a letterboxed picture.) */
.modal-stage.fill img {
    display: block;
    max-width: none;
    max-height: none;
    height: 100%;
    width: auto;
}

.modal-stage.fill.fill-w img {
    width: 100%;
    height: auto;
}

/* ---- Chrome: round buttons floating over the picture ---- */

.modal-close,
.modal-nav,
.modal-fill,
.modal-download {
    position: absolute;
    width: 48px;
    height: 48px;
    border: none;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.1);
    color: white;
    font-size: 2rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s;
}

.modal-close:hover,
.modal-nav:hover,
.modal-fill:hover,
.modal-download:hover {
    background-color: rgba(255, 255, 255, 0.25);
}

.modal-close {
    top: 20px;
    right: 20px;
}

/* Download sits left of Close: 20 px edge + 48 px Close + 12 px gap (TL-24). Feather "download" (MIT). */
.modal-download {
    top: 20px;
    right: 80px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 20px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4'/%3E%3Cpolyline points='7 10 12 15 17 10'/%3E%3Cline x1='12' y1='15' x2='12' y2='3'/%3E%3C/svg%3E");
}

/* The installed app on iOS fetches the file before the share sheet (gallery.js): the control pulses meanwhile, and the
   hint under it asks for a second tap when the tap expired during a long fetch. */
.modal-download.busy {
    animation: modalBusy 0.6s ease-in-out infinite alternate;
}

@keyframes modalBusy {
    to { opacity: 0.3; }
}

.modal-hint {
    position: absolute;
    top: 76px;   /* under the top-right controls: 20 px edge + 48 px control + 8 px gap */
    right: 20px;
    padding: 4px 12px;
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.6);
    color: white;
    font-size: 0.875rem;
    pointer-events: none;
}

.modal-nav {
    top: 50%;
    transform: translateY(-50%);
}

.modal-nav[hidden] {
    display: none;   /* the control at an end of the gallery (TL-25); `hidden` alone loses to the display: flex above */
}

.modal-nav.prev {
    left: 20px;
}

.modal-nav.next {
    right: 20px;
}

.modal-fill {
    bottom: 20px;
    right: 20px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 20px;
    /* Feather "maximize-2" (MIT). Swapped for "minimize-2" while the visible stage is filled. */
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='15 3 21 3 21 9'/%3E%3Cpolyline points='9 21 3 21 3 15'/%3E%3Cline x1='21' y1='3' x2='14' y2='10'/%3E%3Cline x1='3' y1='21' x2='10' y2='14'/%3E%3C/svg%3E");
}

.image-modal:has(.modal-stage.fill) .modal-fill {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='4 14 10 14 10 20'/%3E%3Cpolyline points='20 10 14 10 14 4'/%3E%3Cline x1='14' y1='10' x2='21' y2='3'/%3E%3Cline x1='3' y1='21' x2='10' y2='14'/%3E%3C/svg%3E");
}

.modal-counter {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255, 255, 255, 0.7);
    font-size: 0.875rem;
}

/* ---- Phones: the picture gets the whole app area, the chrome floats over it ---- */
/* 600px = compact-component tier (see .claude-memory breakpoints note). The viewport meta is
   untouched on purpose, so nothing goes under the status bar or notch. */

@media (max-width: 600px) {
    .image-modal {
        --bg-alpha: 1;
    }

    .modal-stage img {
        max-width: 100%;
        max-height: 100%;
    }

    .modal-close,
    .modal-nav,
    .modal-fill,
    .modal-download {
        width: 40px;
        height: 40px;
        font-size: 1.5rem;
        background-color: rgba(0, 0, 0, 0.45);
    }

    .modal-close {
        top: 8px;
        right: 8px;
    }

    .modal-download {
        top: 8px;
        right: 56px;   /* 8 px edge + 40 px Close + 8 px gap */
    }

    .modal-hint {
        top: 56px;   /* 8 px edge + 40 px control + 8 px gap */
        right: 8px;
    }

    .modal-nav.prev {
        left: 8px;
    }

    .modal-nav.next {
        right: 8px;
    }

    .modal-fill {
        bottom: 8px;
        right: 8px;
    }

    .modal-counter {
        bottom: 8px;
        padding: 2px 10px;
        border-radius: 999px;
        background: rgba(0, 0, 0, 0.45);
    }
}

/* ==== Message list: the scroller, the spacer, the loading lines, the top pills, the history start ==== */
/* js/ui/timeline.js; t-messages, t-loading-tail, t-loading-older, t-catching-up */
/* The frame around the list (.chat-container, .chat-main,
   .messages-container, scene, sidebar backdrop, page error) is in frame.css. */

/* Messages area */
.messages {
    flex: 1;
    overflow-y: auto;
    /* overflow-y: auto alone turns overflow-x into auto as well. Any row that spilled past the right edge
       then made the timeline scroll sideways, and on a phone the finger moved the view on two axes: the
       chat drifted while scrolling up. Nothing is meant to poke out to the right, so the spill is clipped. */
    overflow-x: hidden;
    padding: 1rem 0 0.25rem 0;
    min-width: 0; /* Allow flex item to shrink below content size */
}

/* The scrollable content. Carries the flex column that used to live on .messages,
   so the spacer still pushes a short conversation to the bottom (min-height 100%
   gives the spacer room to stretch into). */
.messages-flow {
    display: flex;
    flex-direction: column;
    min-height: 100%;
}

/* Spacer pushes messages to bottom when few messages */
.messages-spacer {
    flex: 1 1 auto;
}

/* Scrollbar styling */
.messages::-webkit-scrollbar {
    width: 16px;
}

.messages::-webkit-scrollbar-track {
    background: var(--bg-primary);
    border-radius: 8px;
}

.messages::-webkit-scrollbar-thumb {
    background: var(--bg-header);
    border: 4px solid var(--bg-primary);
    border-radius: 8px;
}

.messages::-webkit-scrollbar-thumb:hover {
    background: var(--bg-darkest);
}

/* The "Loading messages..." line (TL-02). The older-page pill is .top-pill below. */
.loading-more {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 1rem;
    color: var(--text-muted);
    font-size: 0.875rem;
}

.loading-spinner {
    width: 16px;
    height: 16px;
    border: 2px solid var(--bg-muted);
    border-top-color: var(--text-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* The line shown while a conversation's first answer is on its way (TL-02). On a fast connection the answer
   comes within a few hundred milliseconds, and a line that appears and vanishes in that time reads as a
   flash. It stays invisible for the first 400 ms, so only a slow answer shows it. */
.loading-tail {
    opacity: 0;
    animation: loading-tail-appear 150ms ease-out 400ms forwards;
}

@keyframes loading-tail-appear {
    to { opacity: 1; }
}

/* The pills over the top of the list: the loading pill (TL-06) and the catching-up pill (TL-39). They sit outside
   the scroller and take no space of their own, so no row moves when they come or go. The loading row that lived in
   the flow moved the first rows by its height, and as the first box in the flow it could become the browser's
   scroll anchor near the top, which froze the view while pictures loaded below it. Invisible for the first 400 ms
   like .loading-tail: on a fast link the server answers before anyone could read it. pointer-events: none, because
   they overlap the first row's edge and a tap there belongs to the row. */
.top-pill {
    position: absolute; left: 50%; top: 0.5rem; transform: translateX(-50%); z-index: 5;
    display: flex; align-items: center; gap: 0.5rem;
    background: var(--bg-tertiary); color: var(--text-secondary); border-radius: 999px;
    padding: 0.25rem 0.75rem; font-size: 0.8125rem; white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    pointer-events: none;
    opacity: 0; animation: loading-tail-appear 150ms ease-out 400ms forwards;
}
.top-pill .loading-spinner { width: 12px; height: 12px; }
.catching-up.failed .loading-spinner { display: none; }
@media (prefers-reduced-motion: reduce) { .top-pill .loading-spinner { animation: none; } }

/* History start message */
.history-start {
    text-align: center;
    padding: 1rem;
    color: var(--text-placeholder);
    font-size: 0.875rem;
    font-weight: 600;
}

/* ==== Show more button: Load earlier, GIF picker, GIF library, Admin lists ==== */
/* js/ui/timeline.js, js/ui/gif-picker.js, js/ui/gif-library.js, js/pages/admin.js; t-load-earlier, t-gif-library */

.show-more {
    width: 100%;
    margin-top: 8px;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: none;
    border-radius: 6px;
    padding: 8px;
    font-size: 0.82rem;
    cursor: pointer;
}

.show-more:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

/* ==== New messages pill, down arrow and end of window ==== */
/* TL-16, TL-40, TL-42; js/ui/timeline.js; t-pill, t-down-arrow, t-newer-messages */

/* The New messages pill (TL-16): centred above the typing strip. */
.new-messages-pill {
    position: absolute; left: 50%; bottom: calc(18px + 0.75rem); transform: translateX(-50%); z-index: 5;
    display: flex; align-items: center; gap: 0.25rem;
    background: var(--accent-primary); color: var(--accent-ink); border: none; border-radius: 999px;
    padding: 0.375rem 0.875rem; font-size: 0.875rem; font-weight: 600; cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

/* The down arrow (TL-40): a round button in the bottom right corner above the typing strip, while the bottom is more
   than a screen away. 1.5rem from the edge keeps it clear of the 16 px scrollbar of the list. */
.down-arrow {
    position: absolute; right: 1.5rem; bottom: calc(18px + 0.75rem); z-index: 5;
    display: flex; align-items: center; justify-content: center; width: 36px; height: 36px; padding: 0;
    background: var(--bg-secondary); color: var(--text-secondary); border: 1px solid var(--bg-muted); border-radius: 50%;
    cursor: pointer; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
.down-arrow:hover { color: var(--text-primary); background: var(--bg-hover); }

/* The end of a window after a jump (TL-42): a row in the flow under the last message, a line with the text and the
   button under it. The button wears the arrow's colours because it does what the arrow does. */
.newer-messages { display: flex; flex-direction: column; align-items: center; gap: 0.625rem; padding: 1rem 1rem 1.25rem; }
.newer-messages-line { display: flex; align-items: center; gap: 0.75rem; width: 100%; color: var(--text-muted); font-size: 0.8125rem; font-weight: 600; white-space: nowrap; }
.newer-messages-line::before, .newer-messages-line::after { content: ''; flex: 1; height: 1px; background: var(--bg-muted); }
.newer-messages-button {
    display: flex; align-items: center; gap: 0.375rem; padding: 0.375rem 0.875rem;
    background: var(--bg-secondary); color: var(--text-secondary); border: 1px solid var(--bg-muted); border-radius: 999px;
    font-size: 0.875rem; font-weight: 600; cursor: pointer;
}
.newer-messages-button:hover { color: var(--text-primary); background: var(--bg-hover); }
