/* ============================================================================
   library.css — header, book grid, search, autocomplete, empty state
   ============================================================================ */

/* ---- Header ---- */

.site-header {
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border-default);
    position: sticky;
    top: 0;
    z-index: var(--z-sticky);
}
.header-container { max-width: 1800px; margin: 0 auto; padding: 0.625rem 1.5rem; }
.header-row { display: flex; align-items: center; gap: var(--space-6); min-width: 0; }
.header-row > * { min-width: 0; }

.logo-container { display: flex; align-items: center; gap: 0.625rem; text-decoration: none; flex-shrink: 0; }
.logo-icon {
    width: 32px; height: 32px;
    border-radius: var(--radius-md);
    display: block; flex-shrink: 0;
    transition: transform var(--duration-fast) var(--ease);
}
.logo-container:hover .logo-icon { transform: scale(1.05); }
.logo-text {
    font-family: var(--font-display);
    font-size: var(--text-xl);
    color: var(--text-primary);
    letter-spacing: -0.01em;
}

.search-container { flex: 1; max-width: 600px; }
.search-input-wrapper {
    display: flex; align-items: center;
    background: var(--bg-secondary);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    transition: border-color var(--duration-fast) var(--ease), box-shadow var(--duration-fast) var(--ease);
    overflow: hidden;
}
.search-input-wrapper:focus-within {
    border-color: var(--accent-500);
    box-shadow: 0 0 0 3px rgba(194, 112, 60, 0.15);
}
.search-input-wrapper svg { width: 16px; height: 16px; margin: 0 0.5rem 0 0.75rem; color: var(--text-tertiary); flex-shrink: 0; }
.search-input-wrapper input[type="search"] {
    flex: 1; border: 0; background: transparent; padding: 0.55rem 0;
    font-size: var(--text-sm); color: var(--text-primary);
}
.search-input-wrapper input[type="search"]:focus { outline: none; box-shadow: none; }
.search-input-wrapper input[type="search"]::placeholder { color: var(--text-tertiary); }
.search-field-select {
    border: 0; background: transparent; padding: 0 0.75rem;
    font-size: var(--text-sm); color: var(--text-secondary);
    border-left: 1px solid var(--border-default); border-radius: 0;
    width: auto; flex-shrink: 0;
}
.search-field-select:focus { outline: none; box-shadow: none; }

.header-controls { display: flex; align-items: center; gap: var(--space-3); flex-shrink: 0; }
.sort-controls { display: flex; gap: var(--space-2); align-items: center; }
.sort-controls select {
    padding: 0.5rem 0.75rem; font-size: var(--text-sm);
    background: var(--bg-elevated); border: 1px solid var(--border-default);
    border-radius: var(--radius-md); color: var(--text-secondary);
}

#theme-toggle {
    width: 38px; height: 38px;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--bg-elevated); border: 1px solid var(--border-default);
    border-radius: var(--radius-md); cursor: pointer;
    color: var(--text-secondary);
    transition: all var(--duration-fast) var(--ease);
}
#theme-toggle:hover { border-color: var(--border-strong); color: var(--text-primary); }
#theme-toggle svg { width: 18px; height: 18px; }
/* The icon shows the theme currently in effect, including when `auto`
   resolves to dark via the OS preference. */
#theme-toggle .moon-icon { display: none; }
[data-theme="dark"] #theme-toggle .sun-icon,
.dark #theme-toggle .sun-icon { display: none; }
[data-theme="dark"] #theme-toggle .moon-icon,
.dark #theme-toggle .moon-icon { display: block; }
@media (prefers-color-scheme: dark) {
    :root:not([data-theme]) #theme-toggle .sun-icon  { display: none; }
    :root:not([data-theme]) #theme-toggle .moon-icon { display: block; }
}

.header-signin {
    white-space: nowrap;
    padding: 0.45rem 0.95rem; border: 1px solid var(--border-default);
    border-radius: var(--radius-md); font-size: var(--text-sm);
    color: var(--text-primary); text-decoration: none;
    transition: all var(--duration-fast) var(--ease);
}
.header-signin:hover { border-color: var(--accent-500); color: var(--accent-600); }

.user-menu { position: relative; }
.user-menu summary {
    list-style: none; cursor: pointer; display: inline-flex; align-items: center;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-avatar {
    width: 32px; height: 32px; border-radius: var(--radius-full);
    background: var(--accent-600); color: var(--accent-fg);
    display: inline-flex; align-items: center; justify-content: center;
    font-weight: 600; font-size: var(--text-sm);
}
.user-menu-dropdown {
    position: absolute; right: 0; top: calc(100% + 0.5rem);
    background: var(--bg-elevated); border: 1px solid var(--border-default);
    border-radius: var(--radius-md); box-shadow: var(--shadow-large);
    min-width: 200px; padding: var(--space-3); z-index: var(--z-overlay);
}
.user-menu-name { font-weight: 600; font-size: var(--text-sm); }
.user-menu-email { color: var(--text-tertiary); font-size: var(--text-xs); margin-top: 2px; }
.user-menu-dropdown hr { margin: var(--space-2) calc(var(--space-3) * -1); }
.user-menu-dropdown a {
    display: block; padding: var(--space-2) var(--space-3); margin: 0 calc(var(--space-3) * -1);
    color: var(--text-primary); text-decoration: none; font-size: var(--text-sm);
    border-radius: var(--radius-sm);
}
.user-menu-dropdown a:hover { background: var(--bg-tertiary); color: var(--accent-600); }

/* ---- Main content / library page ---- */

main { padding: var(--space-8) var(--space-4); min-height: calc(100vh - 80px); }
.content-wrapper { max-width: 1800px; margin: 0 auto; }

.section-header {
    display: flex; align-items: baseline; justify-content: space-between;
    margin-bottom: var(--space-8); flex-wrap: wrap; gap: var(--space-3);
}
.section-title {
    font-family: var(--font-display); font-size: clamp(2rem, 4vw, var(--text-5xl));
    color: var(--text-primary); letter-spacing: -0.02em;
}
.book-count {
    font-size: var(--text-sm); color: var(--text-tertiary);
}

/* ---- Guest banner ---- */

.guest-banner {
    display: flex; justify-content: space-between; align-items: center; gap: var(--space-4);
    padding: var(--space-4) var(--space-5);
    background: var(--secondary-50);
    border: 1px solid color-mix(in srgb, var(--secondary-500) 25%, transparent);
    border-radius: var(--radius-lg);
    margin-bottom: var(--space-8); flex-wrap: wrap;
}
.guest-banner-text { font-size: var(--text-sm); color: var(--secondary-700); }
[data-theme="dark"] .guest-banner-text,
.dark .guest-banner-text { color: var(--secondary-200); }
.guest-banner-actions { display: flex; gap: var(--space-2); flex-shrink: 0; }

/* ---- Book grid ---- */

.book-grid {
    display: grid;
    gap: var(--space-6);
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
}
@media (min-width: 1024px) {
    .book-grid { grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: var(--space-10); }
}
@media (max-width: 480px) {
    .book-grid { grid-template-columns: repeat(2, 1fr); gap: var(--space-3); }
}

.book-card {
    display: flex; flex-direction: column; gap: var(--space-3);
    transition: transform var(--duration) var(--ease);
    cursor: pointer;
    border-radius: var(--radius-lg);
}
.book-card:focus-visible { outline-offset: 6px; }
.book-card:hover { transform: translateY(-4px); }

.book-cover-wrapper {
    position: relative; display: block; aspect-ratio: 2 / 3;
    border-radius: var(--radius-md); overflow: hidden;
    background: var(--bg-tertiary);
    box-shadow: var(--shadow-default);
    transition: box-shadow var(--duration) var(--ease);
}
.book-card:hover .book-cover-wrapper { box-shadow: var(--shadow-large); }
.book-cover {
    position: absolute; inset: 0;
    background-position: center; background-size: cover; background-repeat: no-repeat;
}
.book-cover-placeholder {
    position: absolute; inset: 0;
    display: flex; flex-direction: column; justify-content: flex-end;
    padding: var(--space-4);
    background: linear-gradient(135deg, var(--accent-200), var(--accent-400));
    color: var(--accent-700);
    font-family: var(--font-display);
}
.placeholder-title { font-size: var(--text-base); font-weight: 500; line-height: 1.2; }
.placeholder-author { font-size: var(--text-xs); opacity: 0.7; margin-top: var(--space-1); font-family: var(--font-body); }

/* Format + length chips sit above the cover art and below the hover overlay,
   so they read at a glance in the grid without fighting the "Read" affordance. */
.book-format-badge,
.book-page-badge {
    position: absolute;
    z-index: 1;
    padding: 2px 7px;
    border-radius: var(--radius-full);
    font-size: 0.6875rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    line-height: 1.5;
    backdrop-filter: blur(6px);
    pointer-events: none;
}
.book-format-badge {
    top: var(--space-2); left: var(--space-2);
    background: rgba(28, 25, 23, 0.72);
    color: #fff;
}
.book-format-badge.format-pdf { background: rgba(185, 28, 28, 0.85); }
.book-format-badge.format-cbz { background: rgba(13, 115, 119, 0.88); }
.book-page-badge {
    bottom: var(--space-2); right: var(--space-2);
    background: rgba(28, 25, 23, 0.6);
    color: #fff;
    font-weight: 500;
}
.book-card:hover .book-format-badge,
.book-card:hover .book-page-badge { opacity: 0; transition: opacity var(--duration-fast) var(--ease); }

.book-overlay {
    position: absolute; inset: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: var(--space-2);
    background: rgba(28, 25, 23, 0.55); backdrop-filter: blur(2px);
    color: white; opacity: 0;
    transition: opacity var(--duration) var(--ease);
}
.book-overlay svg { width: 40px; height: 40px; }
.book-overlay span { font-size: var(--text-sm); font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; }
.book-card:hover .book-overlay,
.book-card:focus-within .book-overlay { opacity: 1; }

.book-info { padding: 0 var(--space-1); }
.book-title {
    font-family: var(--font-display); font-size: var(--text-lg);
    line-height: 1.25; color: var(--text-primary);
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
    overflow: hidden; margin-bottom: var(--space-1);
}
.book-author { font-size: var(--text-sm); color: var(--text-tertiary); }
.book-meta {
    display: flex; gap: var(--space-3); margin-top: var(--space-2);
    font-size: var(--text-xs);
}
.book-details-link, .book-download {
    color: var(--text-tertiary); text-decoration: none;
    display: inline-flex; align-items: center; gap: 4px;
    transition: color var(--duration-fast) var(--ease);
}
.book-details-link:hover, .book-download:hover { color: var(--accent-600); }
.book-download svg { width: 12px; height: 12px; }

/* ---- Empty state ---- */

.empty-state {
    text-align: center; padding: var(--space-16) var(--space-6);
    max-width: 480px; margin: 0 auto;
}
.empty-icon {
    width: 80px; height: 80px; margin: 0 auto var(--space-6);
    background: var(--bg-tertiary); border-radius: var(--radius-full);
    display: flex; align-items: center; justify-content: center;
    color: var(--text-tertiary);
}
.empty-icon svg { width: 36px; height: 36px; }
.empty-title { font-size: var(--text-2xl); margin-bottom: var(--space-2); }
.empty-description { color: var(--text-secondary); margin-bottom: var(--space-6); }
.empty-actions { display: flex; justify-content: center; gap: var(--space-2); flex-wrap: wrap; }

/* ---- Autocomplete listbox ---- */

.autocomplete-listbox {
    position: absolute; list-style: none; padding: var(--space-2);
    background: var(--bg-elevated); border: 1px solid var(--border-default);
    border-radius: var(--radius-md); box-shadow: var(--shadow-large);
    max-width: 600px; width: 100%; z-index: var(--z-overlay);
}
.autocomplete-listbox[hidden] { display: none; }
.autocomplete-option {
    padding: var(--space-2) var(--space-3); cursor: pointer;
    border-radius: var(--radius-sm); font-size: var(--text-sm);
    color: var(--text-primary);
}
.autocomplete-option[aria-selected="true"],
.autocomplete-option:hover { background: var(--bg-tertiary); color: var(--accent-600); }

/* ---- Footer ---- */

.site-footer {
    border-top: 1px solid var(--border-default);
    padding: var(--space-6); margin-top: var(--space-16);
}
.footer-inner {
    max-width: 1800px; margin: 0 auto;
    display: flex; gap: var(--space-3); flex-wrap: wrap;
    color: var(--text-tertiary); font-size: var(--text-sm);
}
.footer-sep { color: var(--text-tertiary); }

/* ---- Responsive header ---- */

/* ---- Responsive header ----
   Phones get three stacked bands: brand + account, then the search field,
   then the sort selects. `min-width: 0` on the flex children is what actually
   stops the row from pushing past the viewport — without it a flex item
   refuses to shrink below its content width. */
@media (max-width: 900px) {
    .header-row { flex-wrap: wrap; gap: var(--space-3) var(--space-2); }
    .search-container { order: 3; flex: 1 1 100%; max-width: none; min-width: 0; }
    .header-controls { order: 2; margin-left: auto; }
    .sort-controls { order: 4; flex: 1 1 100%; gap: var(--space-2); min-width: 0; }
    .sort-controls select { flex: 1 1 0; min-width: 0; min-height: 40px; }
}
@media (max-width: 768px) {
    .logo-text { display: none; }
    .sort-controls select {
        padding: 0.55rem 0.65rem; font-size: var(--text-sm);
        min-height: 44px;  /* tap target */
    }
    main { padding: var(--space-6) var(--space-3); }
}
@media (max-width: 480px) {
    .header-container { padding: 0.5rem 0.75rem; }
}

/* Ensure all icon buttons hit the 44x44 minimum on touch devices */
@media (pointer: coarse) {
    .btn, #theme-toggle, .reader-btn, .reader-back, .reader-panel-close {
        min-height: 44px; min-width: 44px;
    }
}

/* Account page */
.account-wrapper { max-width: 720px; margin: 0 auto; }
.account-section {
    margin-top: var(--space-8);
    padding: var(--space-6);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    background: var(--bg-elevated);
}
.account-section h2 { font-size: var(--text-xl); margin-bottom: var(--space-4); }
.account-readonly {
    display: grid; grid-template-columns: max-content 1fr; gap: var(--space-2) var(--space-4);
    margin: var(--space-4) 0;
    font-size: var(--text-sm);
}
.account-readonly dt { color: var(--text-tertiary); }
