:root {
    --bg: #faf6f0;
    --surface: #ffffff;
    --text: #2b2118;
    --muted: #7a6a5c;
    --border: #e6dccf;
    --brand: #7c4a1e;
    --brand-contrast: #ffffff;
    --owned: #2f7d4f;
    --owned-bg: #e3f3e9;
    --wishlist: #9a6a00;
    --wishlist-bg: #fdf1d3;
    --to-buy: #2a5d9f;
    --to-buy-bg: #e1ecfa;
    --danger: #b3261e;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(43, 33, 24, 0.08), 0 4px 12px rgba(43, 33, 24, 0.05);
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1c1814;
        --surface: #27211c;
        --text: #f1e9df;
        --muted: #b3a393;
        --border: #3d342b;
        --brand: #d89a5e;
        --brand-contrast: #1c1814;
        --owned: #7fd3a0;
        --owned-bg: #1f3a2a;
        --wishlist: #f2c35a;
        --wishlist-bg: #3d3113;
        --to-buy: #8ab8f0;
        --to-buy-bg: #1d2e45;
        --danger: #f2877f;
        --shadow: none;
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    line-height: 1.5;
}

a { color: var(--brand); }
h1 { font-size: 1.6rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 0 0 0.75rem; }

.visually-hidden {
    position: absolute !important;
    width: 1px; height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

.topbar {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; justify-content: space-between; gap: 1rem;
    padding: 0.75rem 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.topbar-actions { display: flex; align-items: center; gap: 0.5rem; }
.topbar-actions form { margin: 0; }
.brand { white-space: nowrap; font-weight: 700; font-size: 1.15rem; text-decoration: none; color: var(--text); }

.container { max-width: 1100px; margin: 0 auto; padding: 1.25rem 16px 3rem; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 0.35rem;
    padding: 0.55rem 1rem;
    border: 1px solid var(--border); border-radius: 999px;
    white-space: nowrap;
    background: var(--surface); color: var(--text);
    font: inherit; font-weight: 600; text-decoration: none;
    cursor: pointer;
}
.btn:hover { border-color: var(--brand); }
.btn-primary { background: var(--brand); border-color: var(--brand); color: var(--brand-contrast); }
.btn-large { padding: 0.85rem 1.4rem; font-size: 1.05rem; }
.btn-small { padding: 0.3rem 0.7rem; font-size: 0.8rem; }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.25rem;
    margin-bottom: 1rem;
}

.flash {
    padding: 0.8rem 1rem; margin-bottom: 1rem;
    border-radius: var(--radius);
    background: var(--owned-bg); color: var(--text);
    border: 1px solid var(--owned);
}
.flash ul { margin: 0.25rem 0 0; padding-left: 1.2rem; }
.flash-error { background: transparent; border-color: var(--danger); }
.flash-warning { background: var(--wishlist-bg); border-color: var(--wishlist); }

/* ---- Index ---- */

.tabs { display: flex; gap: 0.5rem; overflow-x: auto; padding-bottom: 0.25rem; margin-bottom: 1rem; }
.tab {
    flex: none;
    padding: 0.5rem 0.9rem;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    text-decoration: none; font-weight: 600; font-size: 0.95rem;
}
.tab.active { background: var(--brand); border-color: var(--brand); color: var(--brand-contrast); }
.tab .count { opacity: 0.7; font-weight: 400; margin-left: 0.2rem; }

.search { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.search { flex-wrap: wrap; }
.search input { flex: 1 1 200px; min-width: 0; }
.search select { flex: 1 1 160px; width: auto; }
.book-form label select { margin-top: 0.3rem; font-weight: 400; }

.letters { display: flex; flex-wrap: wrap; gap: 0.25rem; margin-bottom: 1rem; }
.letters a {
    display: inline-block; min-width: 2rem; padding: 0.2rem 0.4rem;
    text-align: center; text-decoration: none; font-weight: 700;
    border-radius: 6px; background: var(--surface); border: 1px solid var(--border);
}

.shelf { margin-bottom: 2rem; scroll-margin-top: 4.5rem; }
.shelf-letter {
    font-size: 1.4rem; color: var(--brand);
    border-bottom: 3px solid var(--border);
    padding-bottom: 0.25rem; margin-bottom: 1rem;
}

.book-grid {
    list-style: none; margin: 0; padding: 0;
    display: grid; gap: 1rem;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
}
.book-card {
    display: flex; flex-direction: column;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
}
.book-card .cover { display: block; aspect-ratio: 2 / 3; background: var(--border); }
.book-card .cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-placeholder {
    display: flex; align-items: center; justify-content: center;
    height: 100%; padding: 1rem;
    text-align: center; font-weight: 700; color: var(--muted);
}
.book-info { padding: 0.6rem 0.75rem 0.25rem; flex: 1; }
.book-info h3 { font-size: 0.95rem; margin: 0 0 0.15rem; line-height: 1.3; }
.book-info h3 a { color: var(--text); text-decoration: none; }
.author { margin: 0 0 0.4rem; color: var(--muted); font-size: 0.85rem; }
.genre { margin: -0.3rem 0 0.4rem; color: var(--muted); font-size: 0.8rem; font-style: italic; }
.quick-status { display: flex; flex-wrap: wrap; gap: 0.3rem; padding: 0.5rem 0.75rem 0.75rem; }

.badge {
    display: inline-block; padding: 0.1rem 0.55rem;
    border-radius: 999px; font-size: 0.75rem; font-weight: 700;
}
.badge-owned { background: var(--owned-bg); color: var(--owned); }
.badge-wishlist { background: var(--wishlist-bg); color: var(--wishlist); }
.badge-to_buy { background: var(--to-buy-bg); color: var(--to-buy); }

.empty { text-align: center; padding: 3rem 1rem; color: var(--muted); }

/* ---- Forms ---- */

input[type="text"], input[type="search"], input[type="number"], input[type="email"], input[type="password"], select, textarea {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border); border-radius: 8px;
    background: var(--bg); color: var(--text);
    font: inherit;
}
input:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 1px; }

.book-form label { display: block; font-weight: 600; margin-bottom: 0.85rem; }
.book-form label input, .book-form label textarea { margin-top: 0.3rem; font-weight: 400; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; }
details { margin: 1rem 0; }
summary { cursor: pointer; font-weight: 600; margin-bottom: 0.75rem; }
.actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 1rem; }

.status-picker { border: 0; padding: 0; margin: 1rem 0 0; display: grid; gap: 0.5rem; grid-template-columns: repeat(3, 1fr); }
.status-picker legend { font-weight: 600; margin-bottom: 0.5rem; padding: 0; }
.book-form .status-option { margin: 0; }
.status-option input { position: absolute; opacity: 0; }
.status-option span {
    display: block; padding: 0.75rem 0.5rem;
    text-align: center; font-weight: 600;
    border: 2px solid var(--border); border-radius: var(--radius);
    cursor: pointer;
}
.status-option input:focus-visible + span { outline: 2px solid var(--brand); outline-offset: 2px; }
.status-owned input:checked + span { border-color: var(--owned); background: var(--owned-bg); color: var(--owned); }
.status-wishlist input:checked + span { border-color: var(--wishlist); background: var(--wishlist-bg); color: var(--wishlist); }
.status-to_buy input:checked + span { border-color: var(--to-buy); background: var(--to-buy-bg); color: var(--to-buy); }

.auth-card { max-width: 420px; margin: 2rem auto; }
.book-form label.checkbox { display: flex; align-items: center; gap: 0.5rem; font-weight: 400; }
.book-form label.checkbox input { margin: 0; width: auto; }

/* ---- Scan ---- */

.capture-buttons { display: flex; flex-wrap: wrap; gap: 0.75rem; }
.capture-buttons .btn { flex: 1 1 180px; }
.preview { margin: 1rem 0 0; }
.preview img { max-width: 100%; max-height: 360px; border-radius: 8px; display: block; margin: 0 auto; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0.75rem 0 0; }

.progress { margin-top: 1rem; }
.progress p { margin: 0.4rem 0 0; font-size: 0.9rem; }
.progress-bar { height: 8px; background: var(--border); border-radius: 999px; overflow: hidden; }
.progress-bar span { display: block; height: 100%; width: 0; background: var(--brand); transition: width 0.2s; }

.suggestions { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.5rem; }
.suggestion {
    display: flex; gap: 0.75rem; align-items: center; width: 100%;
    padding: 0.5rem; text-align: left;
    border: 2px solid var(--border); border-radius: var(--radius);
    background: var(--bg); color: var(--text); font: inherit;
    cursor: pointer;
}
.suggestion.selected { border-color: var(--brand); }
.suggestion img, .suggestion-nocover {
    flex: none; width: 48px; height: 72px; object-fit: cover; border-radius: 4px;
    display: flex; align-items: center; justify-content: center; background: var(--border); font-size: 1.5rem;
}
.suggestion-info { display: flex; flex-direction: column; gap: 0.1rem; min-width: 0; }
.suggestion-info .author { margin: 0; }
.suggestion-info .year { color: var(--muted); font-size: 0.8rem; }
.suggestion-loading { color: var(--muted); }

.barcode-scanner { margin-top: 1rem; text-align: center; }
.barcode-viewport { position: relative; max-width: 480px; margin: 0 auto; border-radius: var(--radius); overflow: hidden; background: #000; aspect-ratio: 4 / 3; }
.barcode-viewport video { width: 100%; height: 100%; object-fit: cover; display: block; }
.barcode-guide {
    position: absolute; left: 12%; right: 12%; top: 32%; bottom: 32%;
    border: 3px solid rgba(255, 255, 255, 0.85); border-radius: 8px;
    box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.35);
}
.barcode-scanner .btn { margin-top: 0.75rem; }

/* ---- Edit ---- */

.edit-layout { display: grid; gap: 1rem; grid-template-columns: 1fr; }
.edit-cover { max-width: 220px; border-radius: var(--radius); box-shadow: var(--shadow); }
@media (min-width: 720px) {
    .edit-layout { grid-template-columns: 240px 1fr; align-items: start; }
}
.danger-zone { margin-top: 2rem; }

@media (max-width: 480px) {
    .status-picker { grid-template-columns: 1fr; }
    .topbar { padding-left: 12px; padding-right: 12px; }
    .topbar .btn { padding: 0.45rem 0.7rem; font-size: 0.85rem; }
    .brand { font-size: 1rem; }
    .hide-narrow { display: none; }
}
