/* ==========================================================================
   Theme tokens — dark (default) and light
   ========================================================================== */
:root,
:root[data-theme="dark"] {
    --bg: #0b0d10;
    --surface: #14171b;
    --surface-2: #1b1f24;
    --surface-hover: #1f242a;
    --border: #262b31;
    --border-strong: #333a42;
    --text: #e8eaed;
    --text-muted: #9aa1ab;
    --accent: #4ade80;
    --accent-strong: #22c55e;
    --accent-contrast: #06210f;
    --amber: #f0b444;
    --danger: #f87171;
    --success: #4ade80;
    --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 12px 32px rgba(0,0,0,0.35);
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.35);
    --radius: 10px;
    --radius-sm: 6px;
    --sans: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

:root[data-theme="light"] {
    --bg: #f6f7f9;
    --surface: #ffffff;
    --surface-2: #f1f3f5;
    --surface-hover: #eaecef;
    --border: #e3e6ea;
    --border-strong: #d1d5db;
    --text: #1a1d21;
    --text-muted: #666e7a;
    --accent: #16a34a;
    --accent-strong: #15803d;
    --accent-contrast: #ffffff;
    --amber: #b45309;
    --danger: #dc2626;
    --success: #16a34a;
    --shadow: 0 1px 2px rgba(20,20,30,0.04), 0 12px 32px rgba(20,20,30,0.08);
    --shadow-sm: 0 1px 2px rgba(20,20,30,0.06);
}

/* ==========================================================================
   Base
   ========================================================================== */
* { box-sizing: border-box; }

html { color-scheme: dark; }
html[data-theme="light"] { color-scheme: light; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    line-height: 1.5;
    font-size: 15px;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.mono { font-family: var(--mono); }

.error { color: var(--danger); font-size: 0.85rem; }
.success { color: var(--success); font-size: 0.85rem; }
.muted { color: var(--text-muted); font-size: 0.85rem; }

/* ==========================================================================
   Navbar (shared across all pages)
   ========================================================================== */
.navbar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 14px 24px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-wrap: wrap;
    position: sticky;
    top: 0;
    z-index: 50;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    font-size: 1rem;
    color: var(--text);
    order: 1;
}
.brand:hover { text-decoration: none; color: var(--text); }
.brand img { height: 26px; width: 26px; }

.navbar-links {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    order: 3;
}

.nav-link {
    padding: 7px 12px;
    border-radius: var(--radius-sm);
    font-size: 0.88rem;
    color: var(--text-muted);
}
.nav-link:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-link.active { color: var(--accent); }

.navbar-right {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-left: auto;
    order: 4;
}

.nav-username { white-space: nowrap; }

.nav-toggles {
    display: flex;
    align-items: center;
    gap: 8px;
}

.lang-switch {
    display: flex;
    border: 1px solid var(--border-strong);
    border-radius: 999px;
    overflow: hidden;
}
.lang-btn {
    background: none;
    border: none;
    padding: 6px 11px;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--text-muted);
    cursor: pointer;
    font-family: var(--sans);
}
.lang-btn.active { background: var(--accent); color: var(--accent-contrast); }
.lang-btn:not(.active):hover { background: var(--surface-2); color: var(--text); }

.icon-btn {
    display: flex; align-items: center; justify-content: center;
    width: 34px; height: 34px;
    border-radius: 50%;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text-muted);
    cursor: pointer;
    flex-shrink: 0;
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); text-decoration: none; }
.icon-sun, .icon-moon { display: block; }
:root[data-theme="dark"] .icon-moon { display: none; }
:root[data-theme="light"] .icon-sun { display: none; }

/* logout: icon-only circular button, label hidden until the mobile dropdown */
.logout-btn { gap: 8px; }
.logout-label { display: none; }

/* hamburger menu button — hidden on desktop, shown only on mobile */
.menu-toggle { display: none; order: 2; margin-left: auto; }

/* ==========================================================================
   Layout containers
   ========================================================================== */
.page-container {
    max-width: 1180px;
    margin: 0 auto;
    padding: 24px;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}

/* ==========================================================================
   Public collection page
   ========================================================================== */
.toolbar {
    display: flex;
    gap: 10px;
    align-items: center;
    padding: 16px 20px;
    flex-wrap: wrap;
    border-bottom: 1px solid var(--border);
}

.toolbar input[type="search"] {
    flex: 1;
    min-width: 220px;
    padding: 10px 14px;
    background: var(--bg);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-family: var(--sans);
    font-size: 0.9rem;
}
.toolbar input[type="search"]:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent); }

.toolbar select {
    padding: 10px 12px;
    background: var(--bg);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-family: var(--sans);
    font-size: 0.88rem;
}

.result-count { padding: 12px 20px 0; color: var(--text-muted); font-size: 0.82rem; }

.table-wrap { padding: 8px 4px 4px; overflow-x: auto; }

table { width: 100%; border-collapse: collapse; background: transparent; }

thead th {
    text-align: left;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}
thead th:hover { color: var(--accent); }

tbody td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    font-size: 0.88rem;
    white-space: nowrap;
    color: var(--text);
}

tbody tr { cursor: pointer; transition: background 0.1s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:hover td:first-child { color: var(--accent); font-weight: 600; }
tbody tr:last-child td { border-bottom: none; }

.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
    background: color-mix(in srgb, var(--accent) 15%, transparent);
    color: var(--accent);
}
.badge-amber {
    background: color-mix(in srgb, var(--amber) 18%, transparent);
    color: var(--amber);
}

/* ==========================================================================
   Modal (tool details)
   ========================================================================== */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(5, 7, 10, 0.6);
    display: none;
    align-items: flex-start;
    justify-content: center;
    padding: 48px 20px;
    overflow-y: auto;
    z-index: 100;
}
.modal-overlay.open { display: flex; }

.modal {
    max-width: 640px;
    width: 100%;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 22px;
    border-bottom: 1px solid var(--border);
}
.modal-header-title { font-weight: 600; font-size: 0.92rem; color: var(--text); }

.modal-close {
    display: flex; align-items: center; justify-content: center;
    width: 30px; height: 30px;
    border-radius: 50%;
    border: 1px solid var(--border-strong);
    background: none;
    color: var(--text-muted);
    font-size: 1.1rem;
    line-height: 1;
    cursor: pointer;
}
.modal-close:hover { color: var(--danger); border-color: var(--danger); }

.modal-body { padding: 22px 26px 28px; }

.modal-logo {
    display: block;
    max-height: 130px;
    max-width: 100%;
    margin: 0 auto 18px;
    object-fit: contain;
}

.modal h2 { margin: 0 0 4px; color: var(--text); font-size: 1.15rem; text-align: center; }
.modal dl { display: grid; grid-template-columns: 140px 1fr; gap: 8px 16px; margin: 20px 0; }
.modal dt { color: var(--text-muted); font-size: 0.8rem; font-weight: 500; }
.modal dd { margin: 0; color: var(--text); font-size: 0.87rem; }

.gallery { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }
.gallery img {
    width: 120px; height: 120px; object-fit: cover;
    border-radius: var(--radius-sm); border: 1px solid var(--border);
    cursor: pointer;
    transition: transform 0.12s, border-color 0.12s;
}
.gallery img:hover { transform: translateY(-2px); border-color: var(--accent); }

/* ==========================================================================
   Details JSON (compact nested list)
   ========================================================================== */
.pst-details { margin-top: 20px; border-top: 1px solid var(--border); padding-top: 14px; }
.pst-details summary { cursor: pointer; color: var(--amber); font-size: 0.85rem; font-weight: 500; }
.details-tree { font-size: 0.82rem; margin-top: 10px; font-family: var(--mono); }
.details-tree ul { margin: 4px 0 4px 14px; padding-left: 12px; border-left: 1px solid var(--border); }
.details-tree li { margin: 3px 0; }
.details-tree .key { color: var(--text-muted); }

/* ==========================================================================
   Lightbox (photo popup) — intentionally stays dark regardless of site theme
   ========================================================================== */
.lightbox-overlay {
    position: fixed; inset: 0;
    background: rgba(4, 5, 7, 0.94);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 200;
}
.lightbox-overlay.open { display: flex; }

.lightbox { width: 100%; height: 100%; display: flex; flex-direction: column; }

.lightbox-header {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px 20px;
    border-bottom: 1px solid #262b31;
    flex-shrink: 0;
}
.lightbox-title { color: #e8eaed; font-size: 0.9rem; font-weight: 600; }
.lightbox-counter { color: #9aa1ab; font-size: 0.8rem; margin-left: auto; }
.lightbox-close {
    background: none; border: 1px solid #333a42; border-radius: 50%;
    color: #9aa1ab; font-size: 1.2rem; line-height: 1; width: 32px; height: 32px;
    cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.lightbox-close:hover { color: #f87171; border-color: #f87171; }

.lightbox-stage {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 16px;
    touch-action: pan-y;
}
.lightbox-stage.zoomed {
    overflow: auto;
    touch-action: auto;
    /* Centered flex alignment leaves the "before center" portion of an
       overflowing child unreachable by scrolling (scroll position can't go
       negative). Switching to flex-start while zoomed puts the image flush at
       (0,0) of the scrollable area so every part of it can be scrolled into view. */
    align-items: flex-start;
    justify-content: flex-start;
}

.lightbox-image {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    border-radius: 8px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.6);
    user-select: none;
    -webkit-user-drag: none;
    cursor: zoom-in;
    transition: width 0.2s ease, height 0.2s ease;
}
.lightbox-image.zoomed {
    cursor: zoom-out;
    max-width: none;
    max-height: none;
    /* width/height are set inline in px by JS, sized toward the clicked point */
}

.lightbox-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(20, 23, 27, 0.8);
    border: 1px solid #333a42;
    color: #e8eaed;
    width: 44px; height: 44px;
    border-radius: 50%;
    font-size: 1.1rem;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.lightbox-nav:hover { color: #4ade80; border-color: #4ade80; }
.lightbox-prev { left: 16px; }
.lightbox-next { right: 16px; }

.lightbox-thumbs {
    display: flex;
    gap: 8px;
    padding: 14px 20px;
    overflow-x: auto;
    border-top: 1px solid #262b31;
    flex-shrink: 0;
}
.lightbox-thumbs img {
    width: 56px; height: 56px; object-fit: cover;
    border-radius: var(--radius-sm); border: 1px solid #262b31;
    opacity: 0.5; cursor: pointer; flex-shrink: 0;
    transition: opacity 0.12s, border-color 0.12s;
}
.lightbox-thumbs img:hover { opacity: 0.8; }
.lightbox-thumbs img.active { opacity: 1; border-color: #4ade80; }

@media (max-width: 640px) {
    .lightbox-nav { width: 38px; height: 38px; }
    .lightbox-prev { left: 6px; }
    .lightbox-next { right: 6px; }
    .lightbox-thumbs { display: none; }
}

/* ==========================================================================
   Site footer
   ========================================================================== */
.site-footer {
    max-width: 1180px;
    margin: 8px auto 0;
    padding: 18px 24px 32px;
    color: var(--text-muted);
    font-size: 0.78rem;
}
.site-footer p { margin: 3px 0; }

/* ==========================================================================
   Auth pages (login / setup)
   ========================================================================== */
.auth-page { display: flex; align-items: center; justify-content: center; min-height: calc(100vh - 65px); padding: 24px; }
.auth-box { width: 100%; max-width: 380px; }
.auth-box .card-body { padding: 24px; }
.auth-box h1 { margin: 0 0 4px; font-size: 1.15rem; }
.auth-box form { display: flex; flex-direction: column; gap: 14px; margin-top: 16px; }
.auth-box label { display: flex; flex-direction: column; gap: 6px; font-size: 0.82rem; color: var(--text-muted); }
.auth-back { display: block; margin-top: 16px; text-align: center; font-size: 0.85rem; }

input, textarea, select {
    font-family: var(--sans);
    padding: 9px 12px;
    background: var(--bg);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 0.9rem;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
textarea.mono { font-size: 0.82rem; }

input[type="file"] {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

button { font-family: var(--sans); cursor: pointer; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 9px 16px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
    font-size: 0.86rem;
    font-family: var(--sans);
}
.btn:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); font-weight: 600; }
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); color: var(--accent-contrast); }

/* ==========================================================================
   Admin
   ========================================================================== */
.admin-main { max-width: 1180px; margin: 0 auto; padding: 24px; }

.admin-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 14px; }
.admin-toolbar h1 { font-size: 1.1rem; font-weight: 600; color: var(--text); margin: 0; }
.search-form { display: flex; gap: 8px; flex: 1; min-width: 240px; }
.search-form input[type="search"] {
    flex: 1;
    padding: 9px 13px;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 0.88rem;
}
.search-form input[type="search"]:focus { outline: none; border-color: var(--accent); }
.clear-link { align-self: center; font-size: 0.82rem; }

.admin-table { width: 100%; border-collapse: collapse; table-layout: auto; }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-scroll .admin-table { min-width: 640px; }
.admin-table th { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); cursor: pointer; user-select: none; white-space: nowrap; }
.admin-table th:hover { color: var(--accent); }
.admin-table td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); font-size: 0.88rem; vertical-align: middle; white-space: nowrap; }
.admin-table tr:last-child td { border-bottom: none; }
.admin-table tbody tr { cursor: pointer; }
.admin-table tbody tr:hover { background: var(--surface-2); }
.admin-table .col-logo { width: 56px; }
.admin-table .col-actions { width: 1%; white-space: nowrap; }
.row-actions { display: flex; align-items: center; gap: 14px; }
.inline-form { display: inline; }
.hidden-form { display: none; }
.link-danger { background: none; border: none; color: var(--danger); padding: 0; font-size: 0.85rem; text-decoration: underline; font-family: var(--sans); cursor: pointer; vertical-align: middle; }

.inline-toolbar { flex: 1; min-width: 240px; border: none; background: none; padding: 0; margin: 0; }

.tool-form { display: flex; flex-direction: column; gap: 18px; max-width: 900px; padding: 24px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; }
.tool-form label { display: flex; flex-direction: column; gap: 6px; font-size: 0.82rem; color: var(--text-muted); }

.checkbox-label { flex-direction: row !important; align-items: center; gap: 8px !important; justify-content: flex-start; }
.checkbox-label input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.checkbox-label span { color: var(--text); font-size: 0.88rem; }

.combo { position: relative; }
.combo-input { width: 100%; }
.combo-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    max-height: 260px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    z-index: 20;
}
.combo-dropdown.open { display: block; }
.combo-item { padding: 9px 12px; font-size: 0.86rem; cursor: pointer; color: var(--text); }
.combo-item:hover { background: var(--surface-2); }
.combo-empty { padding: 9px 12px; font-size: 0.84rem; color: var(--text-muted); }
.full-width { width: 100%; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
legend { padding: 0 6px; color: var(--text-muted); font-size: 0.82rem; }

.photo-grid { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 14px; }
.photo-item { display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 0.76rem; color: var(--text-muted); }
.photo-item img { width: 100px; height: 100px; object-fit: cover; border-radius: var(--radius-sm); border: 1px solid var(--border); }

.variant-thumb { display: block; width: 44px; height: 44px; object-fit: contain; background: var(--bg); border-radius: var(--radius-sm); border: 1px solid var(--border); }
.variant-preview { max-height: 100px; max-width: 220px; object-fit: contain; background: var(--bg); border-radius: var(--radius-sm); border: 1px solid var(--border); display: block; margin-bottom: 10px; }
.variant-preview-row { display: flex; align-items: center; gap: 16px; margin-top: -6px; }

/* ==========================================================================
   Mobile — collapsed navbar (hamburger dropdown) + public collection table
   ========================================================================== */
@media (max-width: 640px) {
    .page-container { padding: 12px; }
    .navbar { padding: 12px 16px; }

    .table-wrap { padding: 6px 0 0; overflow-x: visible; }

    .table-wrap table { table-layout: fixed; width: 100%; }
    .table-wrap thead th, .table-wrap tbody td { white-space: normal; word-break: break-word; padding: 9px 8px; }

    .col-no, .col-type, .col-packaging, .col-condition, .col-photos, .col-added { display: none; }
    .col-model { width: 42%; }
    .col-colour { width: 30%; }
    .col-datecode { width: 28%; }

    .toolbar { padding: 12px 14px; }
    .toolbar select { flex: 1; }
    .result-count { padding-left: 14px; padding-right: 14px; }

    .modal-overlay { padding: 0; }
    .modal { max-width: 100%; height: 100%; border-radius: 0; }

    /* --- collapsed navbar: everything on the right (nav links, user, lang,
       theme, logout) moves into a single hamburger dropdown --- */
    .menu-toggle { display: flex; }

    .navbar-links,
    .navbar-right {
        display: none;
        flex-basis: 100%;
        width: 100%;
    }

    .navbar.menu-open .navbar-links,
    .navbar.menu-open .navbar-right {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: 4px;
        margin-left: 0;
        padding-top: 10px;
        margin-top: 10px;
        border-top: 1px solid var(--border);
    }

    .navbar.menu-open .nav-link {
        padding: 10px 12px;
        border-radius: var(--radius-sm);
    }

    .navbar.menu-open .nav-username {
        padding: 6px 12px;
    }

    .navbar.menu-open .nav-toggles {
        justify-content: space-between;
        padding: 6px 12px;
    }

    /* logout becomes a full-width labeled row instead of a bare icon circle */
    .navbar.menu-open .logout-btn {
        width: auto;
        height: auto;
        border-radius: var(--radius-sm);
        border-color: transparent;
        justify-content: flex-start;
        padding: 10px 12px;
        gap: 10px;
    }
    .navbar.menu-open .logout-btn:hover { background: var(--surface-2); }
    .navbar.menu-open .logout-label { display: inline; font-size: 0.88rem; }

    /* admin tables scroll horizontally instead of hiding columns */
    .table-scroll { border-radius: var(--radius); }
}
