/* ============================================================================
   Admin — mobile-first, touch-friendly.
   - Action bar always visible at bottom (states: empty / one / many)
   - Bottom sheets for Move + Edit (iOS-style)
   - 44px+ touch targets, no hover-only affordances
   ========================================================================= */

body.is-admin {
    background: #FAF7F2;
    /* leave room for the always-visible action bar */
    padding-bottom: env(safe-area-inset-bottom);
}

.admin-shell {
    padding: clamp(1rem, 3vw, 2rem);
    max-width: 1400px;
    margin: 0 auto;
    padding-bottom: 7rem; /* room for fixed action bar */
}
.admin-shell h1 { font-family: var(--f-display); font-size: clamp(1.6rem, 4vw, 2.2rem); margin: 0 0 1.5rem; font-weight: 400; }
.admin-shell h3 { font-family: var(--f-display); font-size: 1.15rem; margin: 0 0 .25rem; font-weight: 500; }

/* ---------- Top admin nav ---------- */
.admin-nav {
    position: sticky; top: 0; z-index: 30;
    display: flex; gap: 1rem; align-items: center; flex-wrap: wrap;
    padding: .75rem clamp(.85rem, 3vw, 2rem);
    background: rgba(250,247,242,.96);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--c-rule);
}
.admin-nav a { font-size: .92rem; color: var(--c-ink-soft); padding: .3rem .15rem; }
.admin-nav a.active, .admin-nav a:hover { color: var(--c-ink); }
.admin-nav .spacer { flex: 1; }
.admin-nav strong { font-family: var(--f-display); font-size: 1.05rem; }
@media (max-width: 540px) {
    .admin-nav a { font-size: .85rem; }
}

/* ---------- Cards ---------- */
.admin-card {
    background: #fff;
    padding: 1.25rem 1.4rem;
    border: 1px solid var(--c-rule);
    border-radius: 6px;
    margin-bottom: 1rem;
}
.admin-card--collapsible summary {
    cursor: pointer;
    font-family: var(--f-display);
    font-size: 1.1rem;
    padding: .25rem 0;
    user-select: none;
}
.admin-card--collapsible[open] summary { margin-bottom: 1.2rem; }

.settings-form {
    display: grid; gap: 1rem;
    grid-template-columns: minmax(0, 2fr) minmax(0, 3fr) 100px minmax(0, 1.4fr);
    align-items: end;
}
.settings-form .btn { grid-column: 1 / -1; justify-self: end; }
@media (max-width: 760px) { .settings-form { grid-template-columns: 1fr; } }

/* ---------- Fields ---------- */
.field { display: grid; gap: .35rem; }
.field > span {
    font-size: .78rem; letter-spacing: .12em; text-transform: uppercase;
    color: var(--c-ink-soft); font-weight: 500;
}
.field input, .field textarea, .field select {
    font: inherit; padding: .85rem 1rem;
    background: #fff; color: var(--c-ink);
    border: 1px solid var(--c-rule); border-radius: 6px;
    transition: border-color 150ms ease;
    min-height: 44px;
}
.field input:focus, .field textarea:focus, .field select:focus {
    outline: none; border-color: var(--c-ink);
}
.field textarea { min-height: 140px; resize: vertical; line-height: 1.6; }

/* ---------- Tables ---------- */
.admin-table { width: 100%; border-collapse: collapse; }
/* Wide tables (e.g. galleries: 5 columns) scroll horizontally on small screens
   instead of forcing the page to overflow. */
@media (max-width: 600px) {
    .admin-card { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .admin-table { min-width: 460px; }
}
.admin-table th, .admin-table td {
    padding: .9rem .6rem; text-align: left;
    border-bottom: 1px solid var(--c-rule);
    font-size: .94rem; vertical-align: middle;
}
.admin-table th {
    font-size: .72rem; letter-spacing: .12em; text-transform: uppercase;
    color: var(--c-ink-soft); font-weight: 500;
}

/* ---------- Buttons ---------- */
.btn-sm { padding: .55rem .9rem; font-size: .85rem; min-height: 40px; }
.btn-danger { background: #6e2a20; color: #fff; border: 0; }
.btn-danger:hover { background: #8a3528; color: #fff; }

/* ---------- Mega upload button (mobile-first) ---------- */
.uploader-card { padding: 0; overflow: hidden; }
.upload-mega {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1.6rem 1.4rem;
    cursor: pointer;
    background: var(--c-paper-2);
    transition: background 150ms ease;
    min-height: 88px;
}
.upload-mega:active { background: rgba(216,160,95,.18); }
.upload-mega__icon {
    display: grid; place-items: center;
    width: 56px; height: 56px;
    border-radius: 50%;
    background: var(--c-ink); color: #fff;
    font-size: 2rem; line-height: 1; font-weight: 200;
    flex-shrink: 0;
}
.upload-mega__text {
    display: flex; flex-direction: column; gap: .15rem; min-width: 0;
}
.upload-mega__title { font-family: var(--f-display); font-size: 1.25rem; font-weight: 500; }
.upload-mega__sub { font-size: .85rem; color: var(--c-ink-soft); }

.upload-progress {
    list-style: none; padding: .25rem 1.4rem 1rem;
    margin: 0; max-height: 240px; overflow-y: auto;
    font-size: .88rem; color: var(--c-ink-soft);
}
.upload-progress li { padding: .3rem 0; border-bottom: 1px solid var(--c-rule); }
.upload-progress li:last-child { border-bottom: 0; }
.upload-progress li.fail { color: #6e2a20; }

/* ---------- Items grid ---------- */
.items-card { padding: 1rem; }
.items-card__head {
    display: flex; align-items: center; justify-content: space-between;
    gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap;
}

.item-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 6px;
}
@media (min-width: 700px)  { .item-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; } }
@media (min-width: 1100px) { .item-grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; } }

.item {
    position: relative; display: block;
    aspect-ratio: 1; overflow: hidden;
    background: var(--c-paper-2);
    border: 0; border-radius: 4px; padding: 0;
    cursor: pointer; user-select: none; text-decoration: none;
    color: inherit;
    -webkit-tap-highlight-color: transparent;
    transition: transform 150ms ease, box-shadow 150ms ease;
}
.item:hover { color: inherit; }
/* In SELECT mode the anchor must NOT navigate — JS handles tap */
#items[data-mode="select"] .item { cursor: pointer; }
/* Photo count badge (multi-photo piece) */
.item__count {
    position: absolute; top: 8px; left: 8px;
    display: inline-flex; align-items: center; gap: 3px;
    padding: 3px 6px 3px 5px;
    background: rgba(31,26,20,.78);
    color: #fff;
    border-radius: 4px;
    font-size: 11px; font-weight: 600; line-height: 1;
    backdrop-filter: blur(4px);
    pointer-events: none; z-index: 2;
}
.item__empty {
    position: absolute; inset: 0;
    display: grid; place-items: center;
    color: var(--c-ink-soft); font-size: .8rem;
}
.item > img {
    width: 100%; height: 100%;
    object-fit: cover; display: block;
    background: var(--c-paper-2);
    pointer-events: none;
}
.item__check {
    position: absolute; top: 8px; right: 8px;
    width: 28px; height: 28px;
    background: rgba(255,255,255,.6);
    border-radius: 50%;
    display: grid; place-items: center;
    color: transparent;
    transition: background 180ms ease, color 180ms ease, transform 180ms ease;
    box-shadow: 0 1px 4px rgba(0,0,0,.18);
}
.item.is-selected {
    box-shadow: 0 0 0 3px var(--c-ink);
    transform: scale(.965);
}
.item.is-selected .item__check {
    background: var(--c-ink); color: #fff;
}

/* ---------- Action bar (fixed bottom, always visible) ---------- */
.action-bar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    background: rgba(255,255,255,.97);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--c-rule);
    box-shadow: 0 -6px 20px rgba(31,26,20,.08);
    padding-bottom: env(safe-area-inset-bottom);
}
.action-bar__inner {
    display: flex; align-items: center; gap: .65rem;
    padding: .65rem clamp(.65rem, 3vw, 1.5rem);
    max-width: 1400px; margin: 0 auto; min-height: 64px;
}

/* Cancel "×" button — hidden when nothing selected */
.action-bar__cancel {
    width: 40px; height: 40px;
    display: grid; place-items: center;
    background: var(--c-paper-2);
    color: var(--c-ink);
    border: 0; border-radius: 50%;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 150ms ease, transform 200ms ease, opacity 200ms ease;
}
.action-bar__cancel:active { background: var(--c-rule); }
.action-bar[data-state="empty"] .action-bar__cancel { display: none; }

/* Status text (hint when empty, count otherwise) */
.action-bar__status { flex: 1; min-width: 0; }
.action-bar__hint  { font-size: .88rem; color: var(--c-ink-soft); }
.action-bar__count { display: none; font-size: .98rem; color: var(--c-ink); font-weight: 500; }
.action-bar__count b { font-family: var(--f-display); font-weight: 500; font-size: 1.1rem; margin-right: .2rem; }
.action-bar[data-state="empty"] .action-bar__count { display: none; }
.action-bar[data-state="empty"] .action-bar__hint  { display: block; }
.action-bar:not([data-state="empty"]) .action-bar__hint  { display: none; }
.action-bar:not([data-state="empty"]) .action-bar__count { display: block; }

/* Action buttons (Edit / Move / Delete) */
.action-bar__actions {
    display: flex; gap: .4rem; align-items: center; flex-shrink: 0;
}
.action-bar__btn {
    display: inline-flex; align-items: center; gap: .4rem;
    padding: .55rem .85rem;
    background: var(--c-ink); color: #fff;
    border: 0; border-radius: 999px;
    font: inherit; font-size: .88rem; font-weight: 500;
    cursor: pointer;
    min-height: 44px;
    transition: background 150ms ease, transform 120ms ease, opacity 200ms ease;
}
.action-bar__btn svg { flex-shrink: 0; }
.action-bar__btn:active { transform: scale(.96); }
.action-bar__btn--delete { background: #6e2a20; }
.action-bar__btn--delete:active { background: #8a3528; }
.action-bar__btn--edit  { background: var(--c-paper-2); color: var(--c-ink); }
.action-bar__btn--edit:active { background: var(--c-rule); }
.action-bar__btn--group { background: var(--c-accent-2); color: #fff; }
.action-bar__btn--group:active { background: var(--c-accent); }
.action-bar__btn--ungroup { background: var(--c-paper-2); color: var(--c-ink); }
.action-bar__btn--ungroup:active { background: var(--c-rule); }

/* Cover badge on a photo within a piece */
.item__cover-badge {
    position: absolute; left: 8px; bottom: 8px;
    padding: 3px 7px;
    background: var(--c-ink); color: #fff;
    border-radius: 4px;
    font-size: 10px; font-weight: 700; letter-spacing: .06em;
    text-transform: uppercase;
    pointer-events: none; z-index: 2;
    box-shadow: 0 1px 3px rgba(0,0,0,.25);
}
.item.is-cover { box-shadow: 0 0 0 2px var(--c-accent-2); }

/* Brief pulse after a Group operation completes — shows which tile absorbed others */
.item.is-just-grouped {
    animation: grouped-pulse 1.2s ease;
}
@keyframes grouped-pulse {
    0%   { box-shadow: 0 0 0 0px rgba(140,79,45,0); transform: scale(1); }
    20%  { box-shadow: 0 0 0 5px rgba(140,79,45,.55); transform: scale(1.03); }
    100% { box-shadow: 0 0 0 0px rgba(140,79,45,0); transform: scale(1); }
}

.piece-form { display: grid; gap: 1rem; }

/* Admin pager (gallery edit) */
.admin-pager {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    margin: 1.5rem 0 .25rem;
    padding-top: 1rem;
    border-top: 1px solid var(--c-rule);
    gap: .75rem;
}
.admin-pager > a:first-child { justify-self: start; }
.admin-pager > a:last-child  { justify-self: end; }
.admin-pager__center { text-align: center; }

/* Visibility per state:
   - empty:  hide all actions
   - one:    show Edit (item-level pages); hide Group (need 2+)
   - many:   hide Edit; show Group, Move, Delete */
.action-bar[data-state="empty"] .action-bar__actions { display: none; }
.action-bar[data-state="one"]  .action-bar__btn--group { display: none; }
.action-bar[data-state="many"] .action-bar__btn--edit  { display: none; }
/* Split (ungroup) is only meaningful when at least 1 photo selected */
.action-bar[data-state="empty"] .action-bar__btn--ungroup { display: none; }

/* On narrow screens, hide button labels — icons only */
@media (max-width: 540px) {
    .action-bar__btn span { display: none; }
    .action-bar__btn { padding: .55rem .7rem; }
}

/* ---------- Bottom sheet ---------- */
.sheet {
    position: fixed; inset: 0; z-index: 60;
    display: flex; align-items: flex-end; justify-content: center;
    padding: 0;
}
.sheet[hidden] { display: none; }
.sheet__backdrop {
    position: absolute; inset: 0;
    background: rgba(31,26,20,.4);
    opacity: 0;
    transition: opacity 240ms ease;
}
.sheet.is-on .sheet__backdrop { opacity: 1; }
.sheet__panel {
    position: relative;
    background: #fff;
    border-radius: 16px 16px 0 0;
    width: 100%; max-width: 560px;
    max-height: 85vh;
    transform: translateY(100%);
    transition: transform 280ms cubic-bezier(.2,.7,.3,1);
    display: flex; flex-direction: column;
    padding-bottom: env(safe-area-inset-bottom);
    overflow: hidden;
    box-shadow: 0 -20px 60px rgba(0,0,0,.25);
}
.sheet.is-on .sheet__panel { transform: translateY(0); }
.sheet__grip {
    width: 44px; height: 5px; border-radius: 3px;
    background: var(--c-rule); margin: .65rem auto .35rem;
}
.sheet__head {
    display: flex; align-items: center; justify-content: space-between;
    padding: .25rem 1.25rem .75rem;
    font-family: var(--f-display); font-size: 1.15rem; font-weight: 500;
}
.sheet__close {
    width: 36px; height: 36px;
    background: transparent; border: 0;
    font-size: 1.6rem; line-height: 1; color: var(--c-ink-soft);
    cursor: pointer; border-radius: 50%;
}
.sheet__close:active { background: var(--c-paper-2); color: var(--c-ink); }

.sheet__list {
    list-style: none; padding: 0 .65rem .65rem; margin: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}
.sheet__list button {
    width: 100%; text-align: left;
    padding: 1rem 1.1rem;
    background: var(--c-paper-2); color: var(--c-ink);
    border: 0; border-radius: 8px;
    cursor: pointer; font: inherit;
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: .35rem;
    min-height: 56px;
    transition: background 150ms ease, transform 100ms ease;
}
.sheet__list button:active {
    background: var(--c-ink); color: #fff;
    transform: scale(.98);
}
.sheet__list-title { font-family: var(--f-display); font-size: 1.05rem; font-weight: 500; }
.sheet__list-meta  { font-size: .8rem; color: var(--c-ink-soft); }
.sheet__list button:active .sheet__list-meta { color: rgba(255,255,255,.7); }

.sheet__body {
    padding: .25rem 1.25rem 1.25rem;
    overflow-y: auto;
}
.edit-preview {
    margin: 0 0 1rem;
    aspect-ratio: 1;
    background: var(--c-paper-2);
    border-radius: 8px;
    overflow: hidden;
    max-height: 280px;
    display: grid; place-items: center;
}
.edit-preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
.edit-actions {
    display: flex; gap: .65rem; align-items: center; margin-top: 1rem;
}
.edit-actions .btn { flex: 1; justify-content: center; }

/* ---------- Toast ---------- */
.toast {
    position: fixed;
    bottom: 6rem;
    left: 50%; transform: translate(-50%, 1rem);
    background: var(--c-ink); color: #fff;
    padding: .75rem 1.4rem; border-radius: 999px;
    font-size: .9rem;
    pointer-events: none; z-index: 70;
    opacity: 0; transition: opacity 250ms ease, transform 250ms ease;
}
.toast.is-on { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Misc ---------- */
.muted.small { font-size: .82rem; }
.notice { padding: .9rem 1.1rem; border-radius: 6px; margin-bottom: 1.2rem; font-size: .92rem; }
.notice--ok  { background: rgba(96,130,90,.12); color: #2f4a30; }
.notice--err { background: rgba(165,80,60,.12); color: #6e2a20; }
.flash-saved {
    display: inline-block;
    padding: .35rem .85rem; background: #2f4a30; color: #fff;
    border-radius: 999px; font-size: .8rem;
}

/* ---------- Danger zone (delete piece) ---------- */
.admin-card--danger { border-color: rgba(165,80,60,.35); }
.danger-zone {
    display: flex; align-items: center; justify-content: space-between;
    gap: 1rem; flex-wrap: wrap;
}
.danger-zone strong { color: #6e2a20; }
.btn--danger {
    background: #a5503c; color: #fff; border: 1px solid #a5503c;
}
.btn--danger:hover { background: #8a4031; border-color: #8a4031; color: #fff; }
