/* ============================================================
   Image Annotator v3 — Viewer only, no sidebar, no gallery
   ============================================================ */

*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    -webkit-text-size-adjust: 100%;
}

body {
    font-size: 14px;
    line-height: 1.5;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    overflow-x: hidden;
}

/* ============================================================
   MAIN
   ============================================================ */
main {
    padding: 16px 20px 24px;
    flex: 1;
    display: flex;
    flex-direction: column;
}

@media (max-width: 600px) {
    main {
        padding: 10px 12px calc(16px + env(safe-area-inset-bottom, 0px));
    }
}

/* ============================================================
   VIEWER CARD
   ============================================================ */
.viewer-card {
    background: #fff;
    border: 1px solid #e2e0dc;
    border-radius: 10px;
    overflow: visible;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    padding: 10px;
}

/* ============================================================
   MASONRY GRID (wrapper)
   ============================================================ */
#masonry-grid {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* ============================================================
   SUB-GRID — stack single column for 1–4 images,
               grid layout for 5+ images
   ============================================================ */
.sub-grid {
    display: grid;
    gap: 10px;
}

/* 1–4 images: single column, stack vertically */
.sub-grid.count-1,
.sub-grid.count-2,
.sub-grid.count-3,
.sub-grid.count-4 {
    grid-template-columns: 1fr;
}

/* 5+ images: 3-column grid */
.sub-grid.count-many {
    grid-template-columns: repeat(3, 1fr);
}

@media (max-width: 600px) {
    .sub-grid.count-many {
        grid-template-columns: 1fr;
    }
}

@media (min-width: 601px) and (max-width: 900px) {
    .sub-grid.count-many {
        grid-template-columns: 1fr 1fr;
    }
}

/* ── Page Item ── */
.page-item {
    position: relative;
    line-height: 0;
    background: #eceae6;
    border-radius: 10px;
    overflow: visible;
    cursor: default;
}

.page-item img {
    width: 100%;
    height: auto;
    display: block;
    user-select: none;
    -webkit-user-drag: none;
    border-radius: 10px;
}

/* Featured image — full width, natural height */
.page-item.featured {
    width: 100%;
    overflow: visible;
}

.page-item.featured img {
    width: 100%;
    object-fit: cover;
    border-radius: 10px;
}

/* Sub images 1–4: natural height (no crop) */
.sub-grid.count-1 .page-item img,
.sub-grid.count-2 .page-item img,
.sub-grid.count-3 .page-item img,
.sub-grid.count-4 .page-item img {
    height: auto;
    object-fit: cover;
}

/* Sub images 5+: fixed height so grid rows align */
.sub-grid.count-many .page-item img {
    height: 220px;
    object-fit: cover;
}

@media (max-width: 600px) {
    .sub-grid.count-many .page-item img {
        height: 180px;
    }
}

.page-markers-layer {
    position: absolute;
    inset: 0;
    pointer-events: none;
}

/* ── Marker Dot ── */
.marker-point {
    position: absolute;
    transform: translate(-50%, -50%);
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: #ffffff59;
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    pointer-events: all;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.28);
    transition: transform 0.15s, box-shadow 0.15s;
    z-index: 10;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.marker-point:hover,
.marker-point.active {
    transform: translate(-50%, -50%) scale(1.22);
    box-shadow: 0 4px 14px rgba(255, 166, 0, 0.45);
}

.marker-point::after {
    content: '';
    position: absolute;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #FF9800;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
}

@media (hover: none) {
    .marker-point::before {
        content: '';
        position: absolute;
        inset: -10px;
        border-radius: 50%;
    }
}

/* ── Popover (desktop) — teleported to body ── */
.marker-popover {
    display: none;
    position: fixed;
    background: #fff;
    border: 1px solid #e2e0dc;
    border-radius: 10px;
    padding: 11px 13px;
    width: max-content;
    max-width: min(260px, 80vw);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    z-index: 9000;
    pointer-events: all;
}

.marker-popover h6 {
    font-size: 13px;
    font-weight: 700;
    margin-bottom: 4px;
    color: #1a1a1a;
}

.marker-popover p {
    /* font-size: 12px;
    color: #888;
    line-height: 1.5; */

    font-size: 12px;
    color: #888;
    line-height: 1.5;

    display: -webkit-box;
    -webkit-line-clamp: 5; /* จำนวนบรรทัดที่ต้องการ */
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.popover-img {
    width: 100%;
    height: 88px;
    object-fit: cover;
    border-radius: 7px;
    margin-bottom: 7px;
    display: block;
}

.popover-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-top: 8px;
    font-size: 11px;
    font-weight: 600;
    color: #FF9800;
    text-decoration: none;
    padding: 3px 9px;
    border-radius: 5px;
    background: rgba(251, 235, 213, 0.12);
    transition: background 0.15s;
}

.popover-link:hover {
    background: rgba(255, 193, 100, 0.22);
}

@media (max-width: 600px) {
    .marker-popover {
        display: none !important;
    }
}

/* ============================================================
   MOBILE BOTTOM SHEET
   ============================================================ */
.bs-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.38);
    z-index: 2000;
    align-items: flex-end;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    opacity: 0;
    transition: opacity 0.22s ease;
}

.bs-overlay.active {
    display: flex;
}

.bs-overlay.visible {
    opacity: 1;
}

.bs-sheet {
    background: #fff;
    border-radius: 18px 18px 0 0;
    width: 100%;
    max-height: 72vh;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    transform: translateY(100%);
    transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
}

.bs-overlay.visible .bs-sheet {
    transform: translateY(0);
}

.bs-handle {
    width: 38px;
    height: 4px;
    background: #e2e0dc;
    border-radius: 2px;
    margin: 10px auto 0;
}

.bs-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px 10px;
    border-bottom: 1px solid #e2e0dc;
}

.bs-head h5 {
    font-size: 15px;
    font-weight: 700;
    color: #1a1a1a;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: calc(100% - 40px);
}

.bs-close {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: #f5f4f2;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #888;
    font-size: 14px;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
}

.bs-body {
    padding: 14px 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.bs-img {
    width: 100%;
    height: 180px;
    object-fit: cover;
    border-radius: 10px;
    display: block;
}

.bs-desc {
    font-size: 13px;
    color: #888;
    line-height: 1.65;
}

.bs-link {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    padding: 12px;
    border-radius: 9px;
    background: #4f74e3;
    color: #fff;
    text-decoration: none;
    font-size: 13px;
    font-weight: 600;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.15s;
}

.bs-link:active {
    background: #3a5ecf;
}

.bs-empty {
    text-align: center;
    padding: 20px 0;
    font-size: 13px;
    color: #888;
}

/* ============================================================
   LOADER / TOAST
   ============================================================ */
@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

#loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(255, 255, 255, .75);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    font-size: .9rem;
    color: #666;
    gap: 10px;
}

.toast {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 9px 18px;
    border-radius: 99px;
    font-size: 13px;
    font-weight: 600;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    z-index: 9999;
    opacity: 0;
    pointer-events: none;
    white-space: nowrap;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast--visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast--success {
    background: #1a7a4a;
    color: #fff;
}

.toast--error {
    background: #e05252;
    color: #fff;
}

:focus-visible {
    outline: 2px solid #4f74e3;
    outline-offset: 2px;
}