:root {
    --bg: #fff;
    --fg: #1b1f24;
    --muted: #5f6b7a;
    --accent: #0b63ce;
    --border: #d6dbe1;
    --surface: #f5f7fa;
    --danger: #b42318;
    --tile-width: 24.5%;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1216;
        --fg: #e6e9ee;
        --muted: #9aa4b2;
        --accent: #6aa8ff;
        --border: #2a313a;
        --surface: #171b21;
        --danger: #ff8a80;
    }
}

* { box-sizing: border-box; }

html, body { margin: 0; background: var(--bg); color: var(--fg); }

body {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

/* Leave page panning to the browser but let the app own pinch-zoom of the images. */
body.display-mode { touch-action: pan-x pan-y; }

a { color: var(--accent); }

.topbar {
    padding: 0.25rem 0.5rem;
    font-size: 0.875rem;
}

.topbar nav { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; }
.topbar nav a { text-decoration: none; padding: 0.35rem 0.25rem; }
.topbar nav a:hover, .topbar nav a:focus-visible { text-decoration: underline; }
.topbar nav a.tribal-link {
    color: var(--muted);
    letter-spacing: 0.12em;
    opacity: 0.55;
    filter: blur(0.045rem);
}
.topbar nav a.tribal-link:hover,
.topbar nav a.tribal-link:focus-visible {
    opacity: 0.85;
    filter: none;
}
.crumb { color: var(--muted); }
.crumb:empty { display: none; }

main { flex: 1; }

/* ---------- Gallery ---------- */
.gallery {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    align-content: flex-start;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.tile {
    position: relative;
    display: block;
    flex: 0 0 auto;
    width: var(--tile-width);
    line-height: 0;
    padding: 1px;
    outline-offset: -3px;
}

.tile img {
    display: block;
    width: 100%;
    height: auto;
    border: 0;
    -webkit-user-drag: none;
}

.tile .pdf-preview {
    display: block;
    width: 100%;
    aspect-ratio: 4 / 3;
    border: 0;
    background: var(--surface);
    pointer-events: none;
}

/* Unreachable feeds become a neutral placeholder labelled by the always-visible caption. */
.tile img.broken {
    aspect-ratio: 4 / 3;
    visibility: hidden;
}
.tile:has(img.broken) {
    background: var(--surface);
    outline: 1px dashed var(--border);
    outline-offset: -2px;
}

.tile .caption {
    position: absolute;
    left: 1px;
    bottom: 1px;
    max-width: calc(100% - 2px);
    padding: 0.2rem 0.45rem;
    font-size: 0.75rem;
    line-height: 1.2;
    color: #fff;
    background: rgb(0 0 0 / 0.6);
    border-top-right-radius: 0.35rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
}

/* Touch screens have no hover tooltips, so always show the title there. */
@media (hover: none) {
    .tile .caption { opacity: 1; }
}
.tile:focus-visible .caption,
.tile:has(img.broken) .caption { opacity: 1; }
.tile .caption:empty { display: none; }

.empty { padding: 2rem 1rem; text-align: center; color: var(--muted); }

/* ---------- Zoom controls ---------- */
.zoom-controls {
    position: fixed;
    right: max(0.75rem, env(safe-area-inset-right));
    bottom: max(0.75rem, env(safe-area-inset-bottom));
    display: flex;
    gap: 0.25rem;
    padding: 0.25rem;
    border-radius: 999px;
    background: rgb(127 127 127 / 0.25);
    backdrop-filter: blur(6px);
    opacity: 0.35;
    transition: opacity 0.2s;
    z-index: 10;
}
.zoom-controls:hover, .zoom-controls:focus-within { opacity: 1; }
@media (hover: none) { .zoom-controls { opacity: 0.8; } }
.zoom-controls[hidden] { display: none; }

.zoom-controls button {
    width: 2.75rem;
    height: 2.75rem;
    padding: 0;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--fg);
    font-size: 1.35rem;
    line-height: 1;
    cursor: pointer;
    touch-action: manipulation;
}

.statusbar {
    padding: 0.25rem 0.5rem;
    color: var(--muted);
    font-size: 0.7rem;
}

/* ---------- Forms / editor ---------- */
button, input { font: inherit; }

button {
    min-height: 2.5rem;
    padding: 0.35rem 0.85rem;
    border: 1px solid var(--border);
    border-radius: 0.4rem;
    background: var(--surface);
    color: var(--fg);
    cursor: pointer;
    touch-action: manipulation;
}
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--danger); }
button:disabled { opacity: 0.5; cursor: default; }

input {
    width: 100%;
    min-height: 2.5rem;
    padding: 0.35rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 0.4rem;
    background: var(--bg);
    color: var(--fg);
}

label { display: grid; gap: 0.25rem; font-size: 0.875rem; color: var(--muted); }

.card {
    max-width: 22rem;
    margin: 3rem auto;
    padding: 1.5rem;
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    background: var(--surface);
    display: grid;
    gap: 1rem;
}
.card h1 { margin: 0; font-size: 1.25rem; }

.actions { display: flex; gap: 0.75rem; align-items: center; }
.error { color: var(--danger); margin: 0; min-height: 1.2em; }
.error:empty { display: none; }

.editor { padding: 0 0.5rem 1rem; }
.editor-head { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: end; margin-bottom: 0.75rem; }
.editor-head h1 { margin: 0; font-size: 1.25rem; align-self: center; }
.editor-head .spacer { flex: 1; }
.editor-head a { align-self: center; }
.category-picker { display: flex; gap: 0.5rem; align-items: end; }
.category-picker input { width: 12rem; }

.grid { width: 100%; border-collapse: collapse; }
.grid th, .grid td { padding: 0.35rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.grid th { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.grid td:first-child { white-space: nowrap; width: 1%; }
.grid td:first-child button { margin-right: 0.25rem; }
.grid img.thumb, .grid object.thumb { width: 5rem; height: auto; display: block; }
.grid object.thumb.pdf-thumb { height: 3.75rem; border: 0; background: var(--surface); }
.grid .link-cell { word-break: break-all; font-size: 0.85rem; }
.grid tfoot td { border-bottom: 0; padding-top: 0.75rem; }
.grid tr.editing { background: var(--surface); }

/* Stack the grid into cards on narrow touch screens. */
@media (max-width: 40rem) {
    .grid thead { display: none; }
    .grid, .grid tbody, .grid tfoot, .grid tr, .grid td { display: block; width: 100%; }
    .grid tr { border: 1px solid var(--border); border-radius: 0.5rem; padding: 0.5rem; margin-bottom: 0.75rem; }
    .grid td { border: 0; }
    .grid td:first-child { width: 100%; white-space: normal; }
    .grid td[data-label]:not([data-label=""])::before {
        content: attr(data-label);
        display: block;
        font-size: 0.75rem;
        color: var(--muted);
    }
    .grid td:first-child button { margin-bottom: 0.25rem; }
}
