:root {
    --bg: #f7f7f8;
    --fg: #1d1d1f;
    --muted: #6b6b70;
    --accent: #2457d6;
    --accent-dark: #1b44ab;
    --border: #e2e2e6;
    --surface: #fff;
    --error-bg: #fdecec;
    --error-fg: #a11d1d;
    --ok-bg: #e8f5ec;
    --ok-fg: #1e6b34;
    --radius: 8px;
}

* { box-sizing: border-box; }

/* Elements with the hidden attribute stay hidden even when a class sets display. */
[hidden] { display: none !important; }

body {
    margin: 0;
    font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--fg);
}

a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: 0 0 1rem; }
.muted { color: var(--muted); }

/* Header */
.site-header {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    flex-wrap: wrap;
    padding: 0.75rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.brand { font-weight: 600; text-decoration: none; color: var(--fg); }
.nav { display: flex; gap: 1rem; }
.nav a { text-decoration: none; }
.user-menu { margin-left: auto; display: flex; align-items: center; gap: 0.75rem; }
.user-menu form { margin: 0; }

.container { max-width: 1100px; margin: 0 auto; padding: 2rem 1.5rem; }

/* Forms */
.form { display: grid; gap: 1rem; }
.form label { display: grid; gap: 0.3rem; font-weight: 500; }
.form .checkbox { display: flex; align-items: center; gap: 0.5rem; font-weight: 400; }

input[type=email], input[type=password], input[type=text], input[type=search], select {
    width: 100%;
    padding: 0.6rem 0.75rem;
    font: inherit;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.button {
    padding: 0.6rem 1.1rem;
    font: inherit;
    font-weight: 600;
    color: #fff;
    background: var(--accent);
    border: 0;
    border-radius: var(--radius);
    cursor: pointer;
}
.button:hover { background: var(--accent-dark); }

.link-button {
    padding: 0;
    font: inherit;
    color: var(--accent);
    background: none;
    border: 0;
    cursor: pointer;
}
.link-button:hover { text-decoration: underline; }

.alert { padding: 0.75rem 1rem; border-radius: var(--radius); margin: 0 0 1rem; }
.alert-error { background: var(--error-bg); color: var(--error-fg); }
.alert-ok { background: var(--ok-bg); color: var(--ok-fg); }

/* Login page */
.guest { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.guest-card {
    width: 100%;
    max-width: 380px;
    padding: 2rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
}
.guest-brand { margin: 0 0 1.5rem; color: var(--muted); }

/* Item list */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.stat {
    display: grid;
    padding: 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.stat-value { font-size: 1.5rem; font-weight: 600; }
.stat-label { color: var(--muted); font-size: 0.875rem; }

.filters { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; margin-bottom: 1rem; }
.filters input[type=search] { flex: 1 1 260px; width: auto; }
.filters select { flex: 0 1 180px; width: auto; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 0.65rem 1rem; text-align: left; border-bottom: 1px solid var(--border); }
.table th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }

.tag { display: inline-block; padding: 0.1rem 0.5rem; font-size: 0.8rem; background: var(--bg); border: 1px solid var(--border); border-radius: 999px; }
.empty { color: var(--muted); }

/* ---------------------------------------------------------------------------
 * App pages
 * ------------------------------------------------------------------------- */
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.prose { white-space: normal; max-width: 65ch; }
h2 { font-size: 1.1rem; margin: 0 0 0.75rem; }
section { margin-bottom: 1.75rem; }

.nav a { color: var(--muted); padding: 0.25rem 0; border-bottom: 2px solid transparent; }
.nav a[aria-current="page"] { color: var(--fg); border-bottom-color: var(--accent); }

.breadcrumb { margin: 0 0 0.5rem; font-size: 0.9rem; color: var(--muted); }
.page-head, .section-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head h1, .section-head h2 { margin: 0; }
.actions { display: flex; gap: 0.5rem; align-items: center; }
.actions form { margin: 0; }

a.button { display: inline-block; text-decoration: none; }
.button-secondary { color: var(--accent); background: var(--surface); border: 1px solid var(--border); }
.button-secondary:hover { background: var(--bg); }
.button-danger { background: #c62828; }
.button-danger:hover { background: #a11d1d; }
.button-small { padding: 0.35rem 0.75rem; font-size: 0.9rem; }
.link-button.danger { color: #c62828; }

.card { padding: 1.25rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.card h2 { margin-bottom: 1rem; }
.narrow { max-width: 560px; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 1rem; }
.card-link { color: inherit; text-decoration: none; }
.card-link:hover { border-color: var(--accent); }
.card-link p { margin: 0; }
a.stat { color: inherit; text-decoration: none; }
a.stat:hover { border-color: var(--accent); }

/* Forms */
.item-form { max-width: 720px; }
.item-form .card { display: grid; gap: 1rem; margin: 0; }
.form .field { display: grid; gap: 0.3rem; }
.form .field > label { font-weight: 500; }
textarea, input[type=number], input[type=file] {
    width: 100%;
    padding: 0.6rem 0.75rem;
    font: inherit;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
}
input[type=file] { padding: 0.5rem; }
textarea { resize: vertical; }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; }
.input-row { display: flex; gap: 0.5rem; }
.input-row input { flex: 1; min-width: 0; }
.input-row.grow { flex: 1 1 260px; }
.hint { margin: 0; font-size: 0.875rem; color: var(--muted); font-weight: 400; }
.field-error { margin: 0; font-size: 0.875rem; color: var(--error-fg); font-weight: 400; }
.has-error input, .has-error select, .has-error textarea { border-color: var(--error-fg); }
.form-actions { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; }
select:disabled { opacity: 0.6; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; width: fit-content; }
.segmented label { display: flex; align-items: center; gap: 0.4rem; padding: 0.5rem 0.9rem; font-weight: 500; cursor: pointer; }
.segmented label + label { border-left: 1px solid var(--border); }
.segmented label:has(input:checked) { background: var(--accent); color: #fff; }
.segmented input { accent-color: #fff; }

.current-picture { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.current-picture img, .picture-preview { max-width: 160px; max-height: 160px; border-radius: var(--radius); border: 1px solid var(--border); object-fit: cover; }

/* Lists */
.thumb-col { width: 56px; padding-right: 0 !important; }
.thumb { display: block; width: 40px; height: 40px; object-fit: cover; border-radius: 6px; background: var(--bg); }
.thumb-empty { border: 1px dashed var(--border); }
.empty { padding: 2rem; text-align: center; background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }
.empty p { margin: 0.25rem 0; }

.shelf-list { list-style: none; margin: 0 0 1rem; padding: 0; }
.shelf-list li { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; padding: 0.5rem 0; border-bottom: 1px solid var(--border); }
.shelf-list form { margin: 0; }
.shelf-counts { font-size: 0.875rem; margin-left: auto; }
.inline-form { display: flex; gap: 0.5rem; align-items: center; }
.inline-form input[type=text] { width: auto; min-width: 0; flex: 1; }
.add-shelf { max-width: 420px; }

/* Item page */
.item-detail { display: grid; grid-template-columns: minmax(0, 320px) 1fr; gap: 2rem; align-items: start; }
.item-detail:not(:has(.item-picture)) { grid-template-columns: 1fr; }
.item-picture img { width: 100%; border-radius: var(--radius); border: 1px solid var(--border); }
.details { display: grid; grid-template-columns: max-content 1fr; gap: 0.6rem 1.5rem; margin: 0 0 1.5rem; }
.details dt { color: var(--muted); }
.details dd { margin: 0; }

/* Scanner */
.scanner { width: min(480px, calc(100vw - 2rem)); padding: 1rem; border: 0; border-radius: 12px; }
.scanner::backdrop { background: rgba(0, 0, 0, 0.6); }
.scanner-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; }
.scanner-head h2 { margin: 0; }
.scanner-view { position: relative; aspect-ratio: 4 / 3; background: #000; border-radius: var(--radius); overflow: hidden; }
.scanner-view video { width: 100%; height: 100%; object-fit: cover; }
.scanner-frame { position: absolute; inset: 30% 10%; border: 2px solid rgba(255, 255, 255, 0.85); border-radius: 8px; box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.25); }
.scanner-status { margin: 0.75rem 0; font-size: 0.9rem; }
.scanner-manual { display: grid; gap: 0.3rem; }
.scanner-manual label { font-size: 0.9rem; font-weight: 500; }

/* Phones */
.show-sm { display: none; }
.row-meta { font-size: 0.85rem; color: var(--muted); line-height: 1.35; margin-top: 0.15rem; }

@media (max-width: 640px) {
    .container { padding: 1.25rem 1rem; }
    .site-header { gap: 0.5rem 1.25rem; padding: 0.75rem 1rem; }
    .site-header .brand { order: 1; }
    .site-header .user-menu { order: 2; }
    .site-header .nav { order: 3; width: 100%; gap: 1.5rem; }
    .hide-sm { display: none; }
    .show-sm { display: block; }
    .user-name { display: none; }
    .item-detail { grid-template-columns: 1fr; }
    .details { grid-template-columns: 1fr; gap: 0.1rem; }
    .details dd { margin-bottom: 0.6rem; }
    .table th, .table td { padding: 0.55rem 0.6rem; }
    .filters select { flex: 1 1 100%; }
}
.scanner-photo { width: 100%; margin-bottom: 1rem; }
.cover-found { display: flex; gap: 1rem; align-items: center; padding: 0.75rem; background: var(--ok-bg); border-radius: var(--radius); }
.cover-found img { width: 72px; height: auto; max-height: 110px; object-fit: cover; border-radius: 4px; border: 1px solid var(--border); background: #fff; }
.cover-found p { margin: 0 0 0.25rem; }
.check-status { width: 2.5rem; }
.check { display: inline-grid; place-items: center; width: 1.5rem; height: 1.5rem; border-radius: 50%; font-size: 0.85rem; font-weight: 700; }
.check.ok { background: var(--ok-bg); color: var(--ok-fg); }
.check.fail { background: var(--error-bg); color: var(--error-fg); }
.check.info { background: var(--bg); color: var(--muted); border: 1px solid var(--border); font-style: italic; }

/* Dropdowns: same look and height as text inputs in every browser (Safari ignores most select styling otherwise). */
select {
    -webkit-appearance: none;
    appearance: none;
    color: inherit;
    line-height: 1.5;
    padding-right: 2.25rem;
    background-color: var(--surface);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%236b6b70' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.85rem center;
    background-size: 12px 8px;
    cursor: pointer;
}
select::-ms-expand { display: none; }

/* Buttons line up exactly with inputs next to them (inputs have a 1px border). */
.button { border: 1px solid transparent; line-height: 1.5; }
.button-secondary { border-color: var(--border); }

/* Fields in a row keep their natural height even if a neighbour label wraps. */
.form label { align-content: start; }
.subtitle { font-weight: 400; color: var(--muted); font-size: 0.75em; margin-left: 0.35rem; }
.nowrap { white-space: nowrap; }
.cover-missing { margin: 0; padding: 0.65rem 0.85rem; background: #fff6e0; color: #6b4a00; border-radius: var(--radius); font-size: 0.925rem; }
.cover-missing .link-button { font-weight: 600; margin-left: 0.25rem; }
