:root {
    --bg: #f6f4ef;
    --surface: #ffffff;
    --surface-2: #efece4;
    --text: #1d1b16;
    --muted: #6d675b;
    --line: #ddd8cc;
    --accent: #f2b705;
    --accent-ink: #1d1b16;
    --danger: #c62828;
    --ok: #2e7d32;
    --radius: 10px;
    --shadow: 0 6px 24px rgba(30, 25, 10, .14);
    --tile-filter: none;
    --shimmer: #fbfaf6;
    color-scheme: light;
}
/* Dark: follows the system unless the theme button forced light; or forced dark (html[data-theme]). */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #151412;
        --surface: #1f1d1a;
        --surface-2: #2a2824;
        --text: #f1ede4;
        --muted: #a69f90;
        --line: #3a3732;
        --shadow: 0 6px 24px rgba(0, 0, 0, .5);
        --tile-filter: invert(1) hue-rotate(180deg) brightness(.92) contrast(.9) saturate(.8);
        --shimmer: #3a3732;
        color-scheme: dark;
    }
}
:root[data-theme="dark"] {
    --bg: #151412;
    --surface: #1f1d1a;
    --surface-2: #2a2824;
    --text: #f1ede4;
    --muted: #a69f90;
    --line: #3a3732;
    --shadow: 0 6px 24px rgba(0, 0, 0, .5);
    --tile-filter: invert(1) hue-rotate(180deg) brightness(.92) contrast(.9) saturate(.8);
    --shimmer: #3a3732;
    color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    background: var(--bg);
    color: var(--text);
    font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    overflow-x: hidden;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
a { color: inherit; }
img { display: block; max-width: 100%; }
[hidden] { display: none !important; }

button, .btn, select, input, textarea {
    font: inherit;
    color: inherit;
}
button, .btn {
    cursor: pointer;
    border: 1px solid var(--line);
    background: var(--surface);
    border-radius: 8px;
    padding: 7px 14px;
    text-decoration: none;
    white-space: nowrap;
}
button:hover, .btn:hover { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
button.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input[type=search], input[type=password], select, textarea {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 7px 10px;
}
textarea { width: 100%; resize: vertical; }

/* ---- header ---- */
.top {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 1000;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo {
    width: 40px; height: 36px;
    display: grid; place-items: center;
    background: var(--accent);
    color: #1d1b16;
    font-weight: 900; font-size: 22px;
    clip-path: polygon(50% 0, 100% 100%, 0 100%);
    padding-top: 10px;
    flex: none;
}
h1 { font-size: 19px; letter-spacing: -.01em; }
.stats { margin: 2px 0 0; font-size: 13px; color: var(--muted); }
.views { display: flex; gap: 4px; margin-left: auto; background: var(--surface-2); padding: 3px; border-radius: 10px; }
.views button { border: 0; background: transparent; padding: 6px 14px; }
.views button.active { background: var(--surface); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); font-weight: 600; }
.actions { display: flex; gap: 8px; }

/* ---- toolbar ---- */
.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px 14px;
    padding: 12px 20px;
}
#search { flex: 1 1 220px; min-width: 0; max-width: 360px; }
.sort { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
/* Colour filters are plain swatches; the name is in the tooltip and aria-label. */
.chip {
    width: 28px; height: 28px; padding: 0;
    border-radius: 50%;
    background: var(--c);
    border: 1px solid color-mix(in srgb, var(--text) 35%, transparent);
    box-shadow: 0 0 0 2px var(--bg);
}
.chip:hover { border-color: color-mix(in srgb, var(--text) 35%, transparent); transform: scale(1.1); }
.chip.on { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--text); }
.icon-btn { width: 36px; padding: 7px 0; text-align: center; font-weight: 600; font-size: 17px; line-height: 1.2; }

/* ---- settings menu (popover, placed under its button by wireChrome) ---- */
.menu {
    position: fixed;
    inset: auto;
    margin: 0;
    width: min(300px, calc(100vw - 32px));
    padding: 16px 18px 18px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
}
.menu h2 { font-size: 15px; margin-bottom: 12px; }
.menu > * + * { margin-top: 14px; }
.menu .check { display: flex; color: var(--text); }
.set-range { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.range-row { display: flex; align-items: center; gap: 10px; }
.range-row input { flex: 1; accent-color: var(--accent); }
.range-row output { min-width: 52px; text-align: right; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.seg { border: 0; padding: 0; margin-inline: 0; display: flex; flex-wrap: wrap; gap: 4px; }
.seg legend { padding: 0; margin-bottom: 6px; font-size: 14px; }
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
    display: block;
    text-align: center;
    padding: 6px 8px;
    border: 1px solid var(--line);
    border-radius: 8px;
    font-size: 13px;
    cursor: pointer;
}
.seg input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); }
.count { color: var(--muted); font-size: 13px; }

.banner {
    display: flex; align-items: center; gap: 12px;
    margin: 0 20px 10px;
    padding: 8px 12px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
}
.banner img { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; }

/* ---- grid ---- */
main { padding: 0 20px 40px; }
/* --thumb comes from the settings menu; without it 180px (120px on phones). Gaps shrink with small thumbnails. */
.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(var(--thumb, 180px), 100%), 1fr));
    gap: clamp(6px, calc(var(--thumb, 180px) / 15), 14px);
}
.no-captions .tile .cap { display: none; }
.group {
    grid-column: 1 / -1;
    font-size: 15px;
    color: var(--muted);
    margin: 14px 0 0;
    padding-bottom: 4px;
    border-bottom: 1px solid var(--line);
}
.group:first-child { margin-top: 0; }
.tile {
    position: relative;
    padding: 0;
    border: 0;
    border-radius: var(--radius);
    overflow: hidden;
    aspect-ratio: 1;
    /* Shimmering placeholder until the thumbnail has loaded (app.js adds .loaded), then the photo fades in.
       Only tiles on or near the screen (.near, set by an IntersectionObserver) animate. */
    background: linear-gradient(100deg, var(--surface-2) 35%, var(--shimmer) 50%, var(--surface-2) 65%) 0 0 / 300% 100%;
}
.tile.near:not(.loaded) { animation: shimmer 1.3s linear infinite; }
.tile.loaded { background: var(--surface-2); }
@keyframes shimmer {
    from { background-position: 100% 0; }
    to { background-position: 0 0; }
}
@media (prefers-reduced-motion: reduce) {
    .tile.near:not(.loaded) { animation: none; }
}
.tile img { width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: transform .25s, opacity .3s; }
.tile.loaded img { opacity: 1; }
.tile:hover img { transform: scale(1.04); }
/* Dominant-colour stripe along the bottom edge, drawn above the photo and the caption gradient. */
.tile::after {
    content: '';
    position: absolute; left: 0; right: 0; bottom: 0;
    height: 4px;
    background: var(--c);
}
.tile .cap {
    position: absolute; left: 0; right: 0; bottom: 0;
    padding: 18px 8px 10px;
    font-size: 12px; text-align: left;
    color: #fff;
    background: linear-gradient(transparent, rgba(0, 0, 0, .7));
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tile .cap:empty { display: none; }
.pin {
    display: inline-block; width: 8px; height: 8px; margin-right: 5px;
    border-radius: 50%; background: #fff; vertical-align: middle;
}
.empty { color: var(--muted); grid-column: 1 / -1; }

/* ---- map ---- */
#mapView { position: relative; }
#map { height: calc(100vh - 190px); min-height: 360px; border-radius: var(--radius); border: 1px solid var(--line); }
.map-note { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
/* Leaflet in the page theme: darkened tiles in dark mode, popups and controls on the surface colour. */
.leaflet-tile-pane { filter: var(--tile-filter); }
.leaflet-container { background: var(--surface-2); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.leaflet-bar a, .leaflet-bar a:hover { background: var(--surface); color: var(--text); border-color: var(--line); }
.leaflet-control-attribution { background: color-mix(in srgb, var(--surface) 80%, transparent) !important; color: var(--muted); }
.leaflet-control-attribution a { color: var(--text); }
.map-pop span { color: var(--text); }
.map-pop { display: block; padding: 0; border: 0; background: none; text-align: left; }
.map-pop img { display: block; max-width: none; border-radius: 6px; margin-bottom: 4px; background: var(--surface-2); }

/* ---- dialogs ---- */
dialog {
    border: 0;
    padding: 0;
    border-radius: 14px;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
    max-width: calc(100vw - 32px);
}
dialog::backdrop { background: rgba(10, 8, 4, .65); }
.close {
    position: absolute; top: 8px; right: 8px; z-index: 2;
    width: 36px; height: 36px; padding: 0;
    border-radius: 50%;
    font-size: 22px; line-height: 1;
}

.detail { width: 1200px; height: min(860px, calc(100vh - 32px)); }
/* The row is pinned to the dialog height so portrait photos are scaled down instead of growing the row. */
.detail-body { display: grid; grid-template-columns: minmax(0, 1fr) 340px; grid-template-rows: minmax(0, 100%); height: 100%; }
.detail-img { margin: 0; position: relative; background: #0d0c0a; min-height: 0; overflow: hidden; touch-action: pan-y; cursor: grab; }
.detail-img img { width: 100%; height: 100%; object-fit: contain; user-select: none; -webkit-user-drag: none; pointer-events: none; }
.nav {
    position: absolute; top: 50%; transform: translateY(-50%);
    width: 44px; height: 64px; padding: 0;
    border: 0; border-radius: 8px;
    background: rgba(0, 0, 0, .45); color: #fff; font-size: 30px;
}
.nav.prev { left: 10px; }
.nav.next { right: 10px; }
.pos {
    position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%);
    padding: 3px 10px; border-radius: 999px;
    background: rgba(0, 0, 0, .5); color: #fff;
    font-size: 12px; font-variant-numeric: tabular-nums;
    pointer-events: none;
}
.pos:empty { display: none; }
.detail-info { padding: 20px 20px 24px; overflow-y: auto; display: flex; flex-direction: column; gap: 18px; }
.detail-info h2 { font-size: 18px; padding-right: 40px; }
.detail-info h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 8px; }
.detail-info .row { display: flex; gap: 8px; margin-top: 8px; }
.sign-text { margin: 0; font-size: 16px; font-weight: 600; }
.palette { display: flex; align-items: center; gap: 6px; }
.sw { display: inline-block; width: 18px; height: 18px; border-radius: 4px; border: 1px solid rgba(0, 0, 0, .2); vertical-align: middle; }
.sw.big { width: 30px; height: 30px; }
.fam { color: var(--muted); text-transform: capitalize; margin-left: 4px; }
#miniMap { height: 160px; border-radius: 8px; margin-bottom: 6px; }
.meta { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; font-size: 12px; color: var(--muted); margin-top: auto; }
.meta .danger { margin-left: auto; }
.muted { color: var(--muted); font-size: 13px; margin: 0; }
.mini-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; margin-bottom: 8px; }
.mini { padding: 0; border: 0; background: none; text-align: left; font-size: 11px; color: var(--muted); }
.mini img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 6px; margin-bottom: 3px; }

.upload { width: 820px; padding: 20px; }
.upload-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.upload-head .close { position: static; }
.drop {
    display: block;
    padding: 28px;
    border: 2px dashed var(--line);
    border-radius: var(--radius);
    text-align: center;
    cursor: pointer;
    margin-bottom: 10px;
}
.drop small { display: block; color: var(--muted); margin-top: 4px; }
.drop.over { border-color: var(--accent); background: var(--surface-2); }
.upload-list { display: flex; flex-direction: column; gap: 10px; margin: 14px 0; max-height: 55vh; overflow-y: auto; }
.up-item { display: grid; grid-template-columns: 88px 1fr auto; gap: 12px; padding: 10px; border: 1px solid var(--line); border-radius: var(--radius); }
.up-item[data-status=duplicate], .up-item[data-status=error] { border-color: var(--danger); }
.up-item[data-status=added] { border-color: var(--ok); }
.up-thumb { width: 88px; height: 88px; object-fit: cover; border-radius: 6px; background: var(--surface-2); }
.up-title { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 4px; word-break: break-all; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.up-meta { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.up-actions { display: flex; flex-direction: column; gap: 6px; }
.upload-foot { display: flex; justify-content: flex-end; }

.login { width: 340px; padding: 22px; }
.login form { display: flex; flex-direction: column; gap: 12px; }
.login .row { display: flex; justify-content: flex-end; gap: 8px; }
.error { color: var(--danger); margin: 0; min-height: 1em; font-size: 13px; }

#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 5000; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 8px; box-shadow: var(--shadow); font-size: 14px; }
.toast.err { background: var(--danger); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }

/* ---- admin ---- */
.admin-main { max-width: 900px; margin: 0 auto; padding: 20px 16px 60px; display: flex; flex-direction: column; gap: 20px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; }
.card h2 { font-size: 17px; margin-bottom: 6px; }
.card p { color: var(--muted); margin: 0 0 12px; }
.card .row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
progress { width: 100%; height: 10px; margin: 12px 0 4px; accent-color: var(--accent); }
.log { font: 12px/1.5 ui-monospace, Consolas, monospace; color: var(--muted); max-height: 200px; overflow-y: auto; white-space: pre-wrap; margin-top: 8px; }
.ocr-preview { display: flex; gap: 12px; align-items: flex-start; margin-top: 10px; }
.ocr-preview img { width: 120px; height: 120px; object-fit: cover; border-radius: 6px; }

/* ---- phone ---- */
@media (max-width: 760px) {
    .top { flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
    .views { margin-left: 0; order: 3; }
    .actions { margin-left: 0; width: 100%; flex-wrap: wrap; gap: 6px; }
    .actions button, .actions .btn { padding: 6px 10px; }
    .actions .icon-btn { width: 34px; padding: 6px 0; }
    .stats { font-size: 12px; }
    .toolbar { padding: 10px 16px; }
    #search { max-width: none; flex-basis: 100%; }
    main { padding: 0 16px 30px; }
    .grid {
        grid-template-columns: repeat(auto-fill, minmax(min(var(--thumb, 120px), 100%), 1fr));
        gap: clamp(6px, calc(var(--thumb, 120px) / 15), 10px);
    }
    .banner { margin: 0 16px 10px; }
    .detail { width: 100vw; height: 100vh; max-width: 100vw; max-height: 100vh; border-radius: 0; }
    .detail-body { grid-template-columns: minmax(0, 1fr); grid-template-rows: 50vh minmax(0, 1fr); }
    .detail-info { min-height: 0; }
    .up-item { grid-template-columns: 64px 1fr; }
    .up-thumb { width: 64px; height: 64px; }
    .up-actions { grid-column: 1 / -1; flex-direction: row; }
    #map { height: calc(100vh - 230px); }
}
