:root {
    --bg: #f6f4ef;
    --surface: #ffffff;
    --surface-alt: #efece4;
    --text: #1f2421;
    --muted: #626965;
    --border: #e1ddd3;
    --primary: #255f4b;
    --primary-hover: #1b4838;
    --primary-soft: #e6f0ec;
    --accent: #c96a2b;
    --danger: #b83333;
    --radius: 12px;
    --shadow: 0 2px 10px rgba(31, 36, 33, 0.06);
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

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

.container {
    max-width: 1220px;
    margin: 0 auto;
    padding: 0 18px;
}

/* Header */
.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
}

.topbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding-top: 12px;
    padding-bottom: 12px;
    flex-wrap: wrap;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
}

.brand-logo {
    font-size: 1.8rem;
    line-height: 1;
}

.brand h1 {
    margin: 0;
    font-size: 1.2rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.brand-sub {
    margin: 0;
    font-size: 0.78rem;
    color: var(--muted);
}

.tabs {
    display: flex;
    gap: 8px;
    background: var(--surface-alt);
    padding: 4px;
    border-radius: 10px;
}

.tab-btn {
    border: 0;
    background: transparent;
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 0.92rem;
    font-weight: 600;
    color: var(--muted);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: all 0.15s ease;
}

.tab-btn.active {
    background: var(--surface);
    color: var(--text);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.badge {
    background: var(--primary);
    color: #fff;
    font-size: 0.75rem;
    padding: 1px 7px;
    border-radius: 999px;
    font-weight: 700;
}

.sync-pill {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    border: 1px solid var(--border);
    background: var(--surface);
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 0.82rem;
    color: var(--muted);
    cursor: pointer;
}

.sync-pill:hover {
    border-color: var(--primary);
    color: var(--text);
}

.sync-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #2e9d5b;
}

.sync-dot.syncing {
    background: var(--accent);
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.35; }
}

/* Main */
.main-content {
    padding-top: 22px;
    padding-bottom: 56px;
}

.tab-panel[hidden] {
    display: none;
}

/* Search Box */
.search-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    box-shadow: var(--shadow);
}

.search-input-wrap {
    display: flex;
    align-items: center;
    gap: 10px;
}

.search-icon {
    font-size: 1.1rem;
    padding-left: 4px;
}

.search-input-wrap input {
    flex: 1;
    border: 1px solid var(--border);
    border-radius: 9px;
    padding: 11px 14px;
    font-size: 1rem;
    outline: none;
    background: var(--bg);
}

.search-input-wrap input:focus {
    border-color: var(--primary);
    background: #fff;
}

.filters-row {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-top: 12px;
    flex-wrap: wrap;
}

.filter-group {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font-size: 0.86rem;
    color: var(--muted);
}

.filter-group select,
.input-sm {
    border: 1px solid var(--border);
    background: var(--bg);
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 0.88rem;
    color: var(--text);
}

/* Genre Chips */
.genre-chips {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 14px 2px 6px;
    scrollbar-width: thin;
}

.chip {
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    padding: 7px 14px;
    border-radius: 999px;
    font-size: 0.86rem;
    font-weight: 500;
    white-space: nowrap;
    cursor: pointer;
    transition: all 0.15s ease;
}

.chip:hover {
    border-color: var(--primary);
}

.chip.active {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.section-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    margin: 16px 0 12px;
    gap: 12px;
}

.section-header h2 {
    margin: 0;
    font-size: 1.15rem;
}

.muted {
    color: var(--muted);
    font-size: 0.86rem;
}

/* Books Grid */
.books-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 16px;
}

.book-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    box-shadow: var(--shadow);
    transition: transform 0.15s ease, border-color 0.15s ease;
}

.book-card:hover {
    transform: translateY(-2px);
    border-color: #cfc9bc;
}

.book-top {
    display: flex;
    gap: 12px;
    cursor: pointer;
}

.book-cover {
    width: 78px;
    height: 114px;
    border-radius: 6px;
    object-fit: cover;
    background: var(--surface-alt);
    flex-shrink: 0;
    border: 1px solid var(--border);
}

.book-cover-fallback {
    width: 78px;
    height: 114px;
    border-radius: 6px;
    background: linear-gradient(135deg, #255f4b, #3a7d65);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-size: 0.72rem;
    font-weight: 700;
    padding: 6px;
    flex-shrink: 0;
}

.book-info {
    min-width: 0;
    flex: 1;
}

.book-title {
    margin: 0 0 4px;
    font-size: 0.98rem;
    font-weight: 700;
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.book-author {
    margin: 0 0 6px;
    font-size: 0.84rem;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-meta-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
}

.tag {
    font-size: 0.72rem;
    padding: 2px 7px;
    border-radius: 6px;
    background: var(--surface-alt);
    color: var(--muted);
    max-width: 150px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-desc {
    margin: 0;
    font-size: 0.83rem;
    color: var(--muted);
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    flex: 1;
}

.book-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-top: auto;
    padding-top: 8px;
    border-top: 1px solid var(--surface-alt);
}

.list-select {
    flex: 1;
    border: 1px solid var(--border);
    background: var(--bg);
    border-radius: 8px;
    padding: 6px 8px;
    font-size: 0.83rem;
    min-width: 0;
}

/* Buttons */
.btn {
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 9px 15px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: background 0.15s ease;
}

.btn-sm {
    padding: 6px 11px;
    font-size: 0.83rem;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-ghost {
    background: var(--surface);
    border-color: var(--border);
    color: var(--text);
}

.btn-ghost:hover {
    background: var(--surface-alt);
}

.btn-danger {
    background: #fdf0f0;
    border-color: #f2c8c8;
    color: var(--danger);
}

.btn-danger:hover {
    background: #fae0e0;
}

/* Lists View */
.lists-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 14px;
}

.lists-pills {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.list-pill {
    border: 1px solid var(--border);
    background: var(--surface);
    padding: 7px 13px;
    border-radius: 999px;
    font-size: 0.86rem;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.list-pill.active {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

.list-pill .del-list-x {
    margin-left: 4px;
    opacity: 0.7;
    font-size: 0.78rem;
}

.list-pill .del-list-x:hover {
    opacity: 1;
}

.lists-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.export-group {
    display: flex;
    gap: 6px;
}

.lists-subbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.lists-subbar .input-sm {
    width: 100%;
    max-width: 340px;
    background: var(--surface);
}

.saved-sections {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.genre-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 2px solid var(--border);
    padding-bottom: 6px;
    margin-bottom: 12px;
}

.genre-section-header h3 {
    margin: 0;
    font-size: 1.05rem;
    color: var(--primary);
}

.genre-count {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--muted);
    background: var(--surface-alt);
    padding: 2px 9px;
    border-radius: 999px;
}

.saved-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 14px;
}

.saved-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.saved-controls {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--surface-alt);
}

.stars {
    display: inline-flex;
    gap: 3px;
}

.star-btn {
    border: 0;
    background: transparent;
    font-size: 1.15rem;
    cursor: pointer;
    color: #d0cbc0;
    padding: 0 2px;
}

.star-btn.filled {
    color: #e2a026;
}

.notes-input {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 7px 10px;
    font-size: 0.84rem;
    font-family: inherit;
    resize: vertical;
    min-height: 52px;
    background: var(--bg);
}

.saved-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-wrap: wrap;
}

/* Modals */
.modal {
    border: 0;
    border-radius: 14px;
    padding: 0;
    max-width: 680px;
    width: calc(100% - 28px);
    box-shadow: 0 18px 48px rgba(0, 0, 0, 0.22);
}

.modal::backdrop {
    background: rgba(20, 24, 22, 0.55);
}

.modal-card {
    padding: 22px;
    position: relative;
    background: var(--surface);
}

.modal-card-sm {
    max-width: 440px;
}

.modal-close {
    position: absolute;
    top: 14px;
    right: 14px;
    border: 0;
    background: var(--surface-alt);
    width: 30px;
    height: 30px;
    border-radius: 50%;
    cursor: pointer;
    font-weight: 700;
}

.stack-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 12px;
}

.stack-form label {
    display: flex;
    flex-direction: column;
    gap: 5px;
    font-size: 0.88rem;
    font-weight: 600;
}

.stack-form input,
.stack-form select {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 9px 11px;
    font-size: 0.94rem;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 14px;
}

.detail-layout {
    display: flex;
    gap: 18px;
    flex-wrap: wrap;
}

.detail-cover {
    width: 130px;
    height: 190px;
    object-fit: cover;
    border-radius: 8px;
    border: 1px solid var(--border);
}

.detail-meta {
    flex: 1;
    min-width: 220px;
}

.detail-meta h3 {
    margin: 0 0 6px;
}

.detail-desc {
    margin-top: 14px;
    font-size: 0.92rem;
    color: var(--text);
    max-height: 220px;
    overflow-y: auto;
}

.sync-details {
    font-size: 0.9rem;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: var(--bg);
    padding: 12px;
    border-radius: 8px;
    border: 1px solid var(--border);
}

/* Empty & Loading */
.empty-state {
    grid-column: 1 / -1;
    background: var(--surface);
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    padding: 36px 20px;
    text-align: center;
    color: var(--muted);
}

/* Toasts */
.toast-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 50;
}

.toast {
    background: var(--text);
    color: #fff;
    padding: 10px 16px;
    border-radius: 10px;
    font-size: 0.88rem;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
}

/* Auth Screen */
.auth-screen[hidden],
#app-shell[hidden] {
    display: none;
}

.auth-screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.auth-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 28px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 12px 34px rgba(31, 36, 33, 0.1);
}

.auth-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.auth-brand h1 {
    margin: 0;
    font-size: 1.3rem;
}

.auth-card h2 {
    margin: 0 0 4px;
    font-size: 1.2rem;
}

.auth-error {
    background: #fdf0f0;
    border: 1px solid #f2c8c8;
    color: var(--danger);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 0.86rem;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.users-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 10px;
}

.user-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 10px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.9rem;
}

@media (max-width: 640px) {
    .search-input-wrap {
        flex-wrap: wrap;
    }
    .search-input-wrap input {
        width: 100%;
    }
    .saved-grid {
        grid-template-columns: 1fr;
    }
}
