/* StackScanner — mobile-first, single-hand-usable layout.
   No build step: plain CSS, custom properties for theming. */

:root {
    --bg: #0f172a;
    --panel: #1e293b;
    --panel-alt: #273449;
    --text: #f1f5f9;
    --text-muted: #94a3b8;
    --accent: #38bdf8;
    --accent-strong: #0ea5e9;
    --success: #34d399;
    --warn: #fbbf24;
    --danger: #f87171;
    --radius: 14px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior-y: none;
}

.app-shell {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    max-width: 560px;
    margin: 0 auto;
}

/* ---------------- Header ---------------- */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: calc(10px + var(--safe-top)) 16px 10px;
    background: var(--panel);
    flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.1rem; }
.brand-mark { font-size: 1.3rem; }
/* ---------------- Scanner ---------------- */
.scanner-panel {
    position: relative;
    flex-shrink: 0;
    background: #000;
    height: 42vh;
    min-height: 220px;
    max-height: 340px;
    overflow: hidden;
    /* Centre the video; whatever doesn't fit is clipped at the edges. */
    display: flex;
    align-items: center;
    justify-content: center;
}
/* html5-qrcode maps its scan box onto the camera frame assuming the video
   is shown whole: full width, natural height. Don't stretch or crop the
   <video> itself (no height:100% / object-fit:cover), or the pixels it
   decodes stop matching the box on screen. */
.scanner-viewport {
    width: 100%;
    flex-shrink: 0;
}
.scanner-viewport video {
    display: block;
    height: auto !important;
}
/* We draw our own frame at the exact decode size (see .scan-frame), so
   hide the library's shading/corner markers. */
#qr-shaded-region { display: none !important; }
.scanner-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    transition: opacity 0.2s ease;
}
.scanner-overlay--paused { opacity: 0.35; }
.scan-frame {
    /* Fallback until the camera reports the real box; then app.js sets
       width/height inline to exactly what html5-qrcode decodes. */
    width: 78%;
    height: 34%;
    max-width: 100%;
    border: 2px solid var(--accent);
    border-radius: 10px;
    box-shadow: 0 0 0 999px rgba(0,0,0,0.35);
}
.scanner-controls {
    position: absolute;
    top: 10px;
    right: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.icon-btn {
    background: rgba(15, 23, 42, 0.75);
    color: var(--text);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 999px;
    padding: 6px 12px;
    font-size: 0.8rem;
    backdrop-filter: blur(4px);
}
.scan-hint {
    position: absolute;
    bottom: 8px;
    left: 0;
    right: 0;
    text-align: center;
    margin: 0;
    font-size: 0.8rem;
    color: rgba(255,255,255,0.85);
    text-shadow: 0 1px 2px rgba(0,0,0,0.8);
}
.scan-hint--error { color: var(--danger); font-weight: 600; }

/* ---------------- Queue ---------------- */
.queue-panel {
    flex: 1;
    overflow-y: auto;
    padding: 12px 14px 10px;
    -webkit-overflow-scrolling: touch;
}
.queue-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}
.queue-header h2 {
    font-size: 0.95rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    margin: 0;
}
.link-btn {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 0.85rem;
    padding: 4px;
}
.queue-empty {
    color: var(--text-muted);
    text-align: center;
    padding: 40px 20px;
    font-size: 0.9rem;
}
.queue-list { display: flex; flex-direction: column; gap: 8px; }
.queue-item {
    background: var(--panel);
    border-radius: var(--radius);
    padding: 10px;
}
.queue-item-row {
    display: flex;
    align-items: center;
    gap: 12px;
}
.queue-thumb {
    width: 44px;
    height: 60px;
    border-radius: 6px;
    object-fit: cover;
    background: var(--panel-alt);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
}
.queue-thumb--pending { color: var(--accent); }
.queue-thumb--warn { color: var(--warn); }
.queue-thumb--muted { color: var(--text-muted); }
.queue-thumb--error { color: var(--danger); }
.queue-meta { min-width: 0; flex: 1; }
.queue-title {
    font-weight: 600;
    font-size: 0.92rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.queue-author, .queue-status, .queue-isbn {
    font-size: 0.8rem;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.queue-item--not_found .queue-title,
.queue-item--error .queue-title { color: var(--text); }

.spinner {
    width: 16px;
    height: 16px;
    border: 2px solid rgba(56,189,248,0.25);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------- Commit bar ---------------- */
.commit-bar {
    flex-shrink: 0;
    display: flex;
    gap: 10px;
    padding: 10px 14px calc(10px + var(--safe-bottom));
    background: var(--panel);
    border-top: 1px solid rgba(255,255,255,0.06);
}
.shelf-select {
    flex: 1;
    min-width: 0;
    background: var(--panel-alt);
    color: var(--text);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 10px;
    padding: 0 10px;
    font-size: 16px;
}
.commit-btn {
    flex-shrink: 0;
    background: var(--success);
    color: #04231a;
    font-weight: 700;
    border: none;
    border-radius: 10px;
    padding: 12px 18px;
    font-size: 0.95rem;
    min-width: 44px;
    min-height: 44px;
}
.commit-btn:disabled {
    background: var(--panel-alt);
    color: var(--text-muted);
}

/* ---------------- Toast ---------------- */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(84px + var(--safe-bottom));
    transform: translateX(-50%);
    background: rgba(15,23,42,0.95);
    border: 1px solid rgba(255,255,255,0.1);
    color: var(--text);
    padding: 10px 16px;
    border-radius: 999px;
    font-size: 0.85rem;
    max-width: 90%;
    text-align: center;
    z-index: 50;
}

/* Buttons: comfortable tap targets everywhere */
button { font-family: inherit; cursor: pointer; }
button, select { -webkit-appearance: none; appearance: none; }

/* =====================================================================
   Views, tabs, library, sheets (added with the Library tab)
   ===================================================================== */

[x-cloak] { display: none !important; }

.brand-name { white-space: nowrap; }

.tabs {
    display: flex;
    gap: 4px;
    background: var(--panel-alt);
    padding: 3px;
    border-radius: 999px;
}
.tab {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 600;
    padding: 7px 14px;
    border-radius: 999px;
    min-height: 36px;
}
.tab--active {
    background: var(--accent-strong);
    color: #04202e;
}
.tab-count {
    font-size: 0.72rem;
    background: rgba(0, 0, 0, 0.18);
    padding: 1px 7px;
    border-radius: 999px;
}
.tab:not(.tab--active) .tab-count { background: rgba(255, 255, 255, 0.08); }

.view {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.queue-note {
    font-size: 0.75rem;
    color: var(--accent);
    margin-top: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ---------------- Form controls ---------------- */
.field {
    width: 100%;
    background: var(--panel-alt);
    color: var(--text);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    padding: 10px 12px;
    font-size: 16px; /* anything smaller makes iOS Safari zoom on focus */
    font-family: inherit;
    min-height: 44px;
}
.field::placeholder { color: var(--text-muted); }
.field:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
select.field {
    padding-right: 32px;
    background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%),
                      linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
    background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
    background-size: 5px 5px, 5px 5px;
    background-repeat: no-repeat;
}
.field-label {
    display: block;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin: 16px 0 6px;
}

.btn {
    background: var(--panel-alt);
    color: var(--text);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    padding: 11px 16px;
    font-size: 0.95rem;
    font-weight: 600;
    min-height: 44px;
}
.btn:disabled { opacity: 0.5; }
.btn--primary { background: var(--accent-strong); border-color: transparent; color: #04202e; }
.btn--danger { background: transparent; border-color: rgba(248, 113, 113, 0.5); color: var(--danger); }
.link-btn--danger { color: var(--danger); }

/* ---------------- Library ---------------- */
.library-toolbar {
    display: flex;
    gap: 8px;
    padding: 12px 14px 8px;
    flex-shrink: 0;
}
.search-input { flex: 1; min-width: 0; }
.sort-select { width: auto; flex-shrink: 0; }

.chip-row {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 2px 14px 10px;
    flex-shrink: 0;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
}
.chip-row::-webkit-scrollbar { display: none; }
.chip {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--panel);
    color: var(--text);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 999px;
    padding: 7px 13px;
    font-size: 0.85rem;
    min-height: 36px;
    white-space: nowrap;
}
.chip--active {
    background: var(--accent-strong);
    border-color: transparent;
    color: #04202e;
    font-weight: 600;
}
.chip-count { opacity: 0.7; font-size: 0.78rem; }
.chip--ghost { background: none; border-style: dashed; color: var(--accent); }

.library-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0 14px calc(16px + var(--safe-bottom));
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.book-row {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    text-align: left;
    background: var(--panel);
    color: var(--text);
    border: none;
    border-radius: var(--radius);
    padding: 10px;
}
.book-row:active { background: var(--panel-alt); }
.book-cover {
    width: 44px;
    height: 64px;
    border-radius: 6px;
    object-fit: cover;
    background: var(--panel-alt);
    flex-shrink: 0;
}
.book-meta { display: flex; flex-direction: column; min-width: 0; flex: 1; gap: 2px; }
.book-title, .book-author {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.book-title { font-weight: 600; font-size: 0.92rem; }
.book-author { font-size: 0.8rem; color: var(--text-muted); }
.book-shelf {
    align-self: flex-start;
    margin-top: 3px;
    font-size: 0.72rem;
    color: var(--accent);
    background: rgba(56, 189, 248, 0.12);
    padding: 1px 8px;
    border-radius: 999px;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.book-shelf--none { color: var(--text-muted); background: rgba(148, 163, 184, 0.12); }

.library-empty, .library-error {
    color: var(--text-muted);
    text-align: center;
    padding: 36px 20px;
    font-size: 0.9rem;
    line-height: 1.5;
}
.library-error { color: var(--danger); padding: 12px; }
.library-loading { display: flex; justify-content: center; padding: 18px; }
.load-more { align-self: center; margin: 6px 0 12px; }

/* ---------------- Bottom sheets ---------------- */
.sheet-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 23, 0.6);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    z-index: 40;
}
.sheet {
    width: 100%;
    max-width: 560px;
    max-height: 88dvh;
    overflow-y: auto;
    background: var(--panel);
    border-radius: 18px 18px 0 0;
    padding: 20px 16px calc(16px + var(--safe-bottom));
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.35);
}
.sheet-heading { margin: 0 0 12px; font-size: 1.1rem; }
.sheet-book { display: flex; gap: 14px; align-items: flex-start; }
.sheet-cover {
    width: 76px;
    height: 110px;
    border-radius: 8px;
    object-fit: cover;
    background: var(--panel-alt);
    flex-shrink: 0;
}
.sheet-book-meta { min-width: 0; }
.sheet-title { margin: 0 0 4px; font-size: 1.05rem; line-height: 1.3; }
.sheet-author { margin: 0 0 8px; color: var(--text-muted); }
.sheet-detail { margin: 0 0 3px; font-size: 0.8rem; color: var(--text-muted); overflow-wrap: anywhere; }
.sheet-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 20px;
}
.sheet-actions .btn--danger { margin-right: auto; }
.sheet-note { font-size: 0.8rem; color: var(--text-muted); margin: 10px 0 0; }

.shelf-form { display: flex; flex-direction: column; gap: 8px; }
.form-error { color: var(--danger); font-size: 0.85rem; margin: 2px 0 0; }
.shelf-list { list-style: none; margin: 16px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.shelf-item { background: var(--panel-alt); border-radius: 12px; padding: 10px 12px; }
.shelf-item-row { display: flex; align-items: center; gap: 6px; }
.shelf-meta { flex: 1; min-width: 0; }
.shelf-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.shelf-sub { font-size: 0.78rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.shelf-item .link-btn { min-height: 36px; padding: 4px 8px; }
.shelf-edit { display: flex; flex-direction: column; gap: 8px; }
.shelf-edit .field { background: var(--panel); }
.shelf-edit-actions { display: flex; gap: 8px; }

/* ---------------- Toast with action ---------------- */
.toast {
    display: flex;
    align-items: center;
    gap: 12px;
    border-radius: 14px;
}
.toast-action {
    background: none;
    border: none;
    color: var(--accent);
    font-weight: 700;
    font-size: 0.85rem;
    padding: 4px 2px;
    flex-shrink: 0;
}

@media (max-width: 360px) {
    .brand-name { display: none; }
}

/* =====================================================================
   Responsive layout for larger windows
   ---------------------------------------------------------------------
   < 720px   phone: single column (everything above this block)
   ≥ 720px   tablet: library becomes a cover grid, sheets become
             centered dialogs, scan view stays a centered column
   ≥ 960px   desktop: scan view splits camera | queue + commit;
             library gets a shelf sidebar beside the grid
   ===================================================================== */


@media (min-width: 720px) {
    .app-shell { max-width: 1280px; }

    .topbar { padding-left: 20px; padding-right: 20px; }

    /* Scan view: keep the phone layout, just centered at a sane width. */
    .view--scan {
        width: 100%;
        max-width: 640px;
        margin: 0 auto;
    }
    .view--scan .scanner-panel { max-height: 420px; }

    /* Library: cover grid */
    .library-toolbar { padding: 14px 20px 8px; }
    .chip-row { padding-left: 20px; padding-right: 20px; flex-wrap: wrap; overflow-x: visible; }
    .library-list {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
        align-content: start;
        gap: 16px;
        padding: 4px 20px 24px;
    }
    .book-row {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        height: 100%;
        padding: 10px 10px 12px;
    }
    .book-cover {
        width: 100%;
        height: auto;
        aspect-ratio: 2 / 3;
        border-radius: 8px;
    }
    .book-title {
        white-space: normal;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        line-height: 1.3;
    }
    .library-empty,
    .library-error,
    .library-loading,
    .load-more { grid-column: 1 / -1; }
    .load-more { justify-self: center; }

    /* Sheets become centered dialogs */
    .sheet-backdrop { align-items: center; padding: 24px; }
    .sheet {
        max-width: 480px;
        max-height: min(88dvh, 760px);
        border-radius: 18px;
        padding: 22px 20px 20px;
    }

}

@media (min-width: 960px) {
    .topbar {
        background: transparent;
        border-bottom: 1px solid rgba(255, 255, 255, 0.06);
        padding-top: calc(14px + var(--safe-top));
        padding-bottom: 14px;
    }

    /* ---- Scan: camera left, queue + commit right ---- */
    .view--scan {
        max-width: none;
        display: grid;
        grid-template-columns: minmax(0, 1.2fr) minmax(360px, 0.8fr);
        grid-template-rows: minmax(0, 1fr) auto;
        grid-template-areas:
            "camera queue"
            "camera commit";
        gap: 16px 20px;
        padding: 20px;
    }
    .view--scan .scanner-panel {
        grid-area: camera;
        height: auto;
        min-height: 0;
        max-height: none;
        border-radius: 16px;
    }
    .view--scan .queue-panel {
        grid-area: queue;
        padding: 0 2px 0 0;
    }
    .view--scan .commit-bar {
        grid-area: commit;
        border-top: none;
        border-radius: 14px;
        padding: 12px;
    }
    .scanner-controls { top: 14px; right: 14px; }
    .scan-hint { bottom: 14px; }

    /* ---- Library: shelf sidebar + grid ---- */
    .view--library {
        display: grid;
        grid-template-columns: 240px minmax(0, 1fr);
        grid-template-rows: auto minmax(0, 1fr);
        grid-template-areas:
            "shelves toolbar"
            "shelves list";
        column-gap: 28px;
        padding: 0 20px;
    }
    .library-toolbar { grid-area: toolbar; padding: 20px 0 14px; }
    .search-input { max-width: 520px; }
    .library-list { grid-area: list; padding: 4px 0 28px; }

    .chip-row {
        grid-area: shelves;
        flex-direction: column;
        flex-wrap: nowrap;
        overflow-x: visible;
        overflow-y: auto;
        gap: 2px;
        padding: 20px 0 24px;
        border-right: 1px solid rgba(255, 255, 255, 0.06);
        padding-right: 16px;
    }
    .chip-row::before {
        content: 'Shelves';
        font-size: 0.75rem;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.06em;
        color: var(--text-muted);
        padding: 0 12px 8px;
    }
    .chip {
        width: 100%;
        justify-content: space-between;
        background: none;
        border: none;
        border-radius: 10px;
        padding: 9px 12px;
        font-size: 0.92rem;
        white-space: nowrap;
        overflow: hidden;
    }
    .chip > span:first-child { overflow: hidden; text-overflow: ellipsis; }
    .chip--active {
        background: var(--panel-alt);
        color: var(--text);
        font-weight: 600;
    }
    .chip--active .chip-count { color: var(--accent); opacity: 1; }
    .chip--ghost {
        margin-top: 8px;
        justify-content: flex-start;
        border: 1px dashed rgba(56, 189, 248, 0.35);
    }
}

@media (min-width: 1280px) {
    .library-list { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 20px; }
}

/* Hover feedback only where there's a real pointer (not on touch). */
@media (hover: hover) and (pointer: fine) {
    .book-row { transition: background 0.15s ease, transform 0.15s ease; }
    .book-row:hover { background: var(--panel-alt); transform: translateY(-2px); }
    .chip:not(.chip--active):hover { background: var(--panel-alt); }
    .tab:not(.tab--active):hover { color: var(--text); }
    .btn:not(:disabled):hover { filter: brightness(1.12); }
    .commit-btn:not(:disabled):hover { filter: brightness(1.08); }
    .link-btn:hover { text-decoration: underline; }
}

/* Re-scanning a book already in the queue flashes its card. */
@keyframes queue-flash {
    0%   { box-shadow: 0 0 0 0 rgba(251, 191, 36, 0); background: var(--panel); }
    12%  { box-shadow: 0 0 0 3px var(--warn); background: rgba(251, 191, 36, 0.14); }
    100% { box-shadow: 0 0 0 0 rgba(251, 191, 36, 0); background: var(--panel); }
}
.queue-item--flash { animation: queue-flash 1.3s ease-out; }

/* Mirroring flips only what's displayed. Decoding reads the raw video
   frames, so it's unaffected (and 1D barcodes read either way anyway). */
.scanner-panel--mirrored #reader video { transform: scaleX(-1); }

.icon-btn { transition: background 0.15s ease, color 0.15s ease; }
.icon-btn:disabled { opacity: 0.6; }
.icon-btn--toggle[aria-pressed="true"] {
    background: rgba(14, 165, 233, 0.85);
    border-color: transparent;
    color: #04202e;
    font-weight: 600;
}

/* Header on narrow phones: keep "StackScanner" and both tabs (with their
   counts) on one line. The tabs never shrink; the name gives way first,
   ending in "…" only in the tightest case (below 360px it's hidden). */
.topbar { gap: 10px; }
.brand { min-width: 0; }
.brand-name { overflow: hidden; text-overflow: ellipsis; }
.tabs { flex-shrink: 0; }

@media (max-width: 430px) {
    .topbar { padding-left: 12px; padding-right: 12px; gap: 8px; }
    .brand { gap: 6px; font-size: 1rem; }
    .brand-mark { font-size: 1.15rem; }
    .tab { padding: 7px 10px; gap: 5px; font-size: 0.85rem; }
    .tab-count { padding: 1px 6px; }
}
