/* ── Reset & Base ─────────────────────────────────────────────────── */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #0a0a0a;
    color: #e0e0e0;
    min-height: 100vh;
    padding-bottom: 80px;
}

/* ── Header ──────────────────────────────────────────────────────── */

.app-header {
    text-align: center;
    padding: 20px 16px 8px;
}

.app-header h1 {
    font-size: 1.5rem;
    font-weight: 600;
    color: #ffffff;
}

.item-count {
    color: #888;
    font-size: 0.85rem;
}

/* ── Search Bar ──────────────────────────────────────────────────── */

.search-bar {
    position: sticky;
    top: 0;
    z-index: 10;
    background: #0a0a0a;
    padding: 8px 16px 8px;
    border-bottom: 1px solid #222;
    display: flex;
    align-items: center;
    gap: 8px;
}

.search-icon {
    color: #666;
    font-size: 0.9rem;
}

.search-bar input {
    flex: 1;
    height: 44px;
    background: #141414;
    border: 1px solid #222;
    border-radius: 12px;
    padding: 0 16px;
    color: #e0e0e0;
    font-size: 1rem;
    outline: none;
}

.search-bar input::placeholder {
    color: #666;
}

.search-bar input:focus {
    border-color: #7c8aff;
}

/* ── Tab Bar ─────────────────────────────────────────────────────── */

.tab-bar {
    display: flex;
    border-bottom: 1px solid #222;
    padding: 0 16px;
}

.tab {
    flex: 1;
    height: 44px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    color: #888;
    font-size: 0.9rem;
    font-family: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: color 0.2s, border-color 0.2s;
}

.tab.active {
    color: #7c8aff;
    border-bottom-color: #7c8aff;
}

.tab-count {
    font-size: 0.75rem;
    opacity: 0.7;
}

/* ── Card Grid ───────────────────────────────────────────────────── */

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px;
    padding: 12px;
}

@media (min-width: 600px) {
    .card-grid {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
        gap: 16px;
        padding: 16px;
    }
}

@media (min-width: 900px) {
    .card-grid {
        max-width: 900px;
        margin: 0 auto;
    }
}

/* ── Cards ────────────────────────────────────────────────────────── */

.card {
    background: #141414;
    border: 1px solid #222;
    border-radius: 12px;
    overflow: hidden;
    transition: border-color 0.2s, transform 0.2s;
    cursor: default;
}

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

.card-cover {
    width: 100%;
    aspect-ratio: 2 / 3;
    object-fit: cover;
    display: block;
    background: #1a1a2e;
}

.card-placeholder {
    width: 100%;
    aspect-ratio: 2 / 3;
    background: #1a1a2e;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #7c8aff;
    font-size: 2rem;
}

.card-info {
    padding: 8px;
}

.card-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.card-subtitle {
    font-size: 0.75rem;
    color: #999;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
}

.card-badge {
    display: inline-block;
    font-size: 0.65rem;
    padding: 2px 6px;
    border-radius: 8px;
    background: #1a1a2e;
    color: #7c8aff;
    margin-top: 4px;
}

/* ── Empty State ─────────────────────────────────────────────────── */

.empty-state {
    text-align: center;
    padding: 60px 24px;
    color: #888;
}

.empty-state i {
    font-size: 3rem;
    color: #7c8aff;
    margin-bottom: 16px;
    display: block;
}

.empty-state p {
    font-size: 1rem;
    line-height: 1.5;
}

/* ── Scan FAB ────────────────────────────────────────────────────── */

.scan-fab {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: #7c8aff;
    color: #fff;
    border: none;
    font-size: 1.25rem;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(124, 138, 255, 0.3);
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
}

.scan-fab:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(124, 138, 255, 0.4);
}

/* ── Scanner Overlay ─────────────────────────────────────────────── */

.overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    background: #0a0a0a;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 16px;
}

.overlay-header {
    width: 100%;
    max-width: 500px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.overlay-header h2 {
    font-size: 1.1rem;
    color: #fff;
}

.scanner-region {
    width: 100%;
    max-width: 400px;
    border-radius: 12px;
    overflow: hidden;
}

.scan-photo-btn {
    margin-top: 16px;
    cursor: pointer;
}

.link-btn {
    background: none;
    border: none;
    color: #7c8aff;
    font-size: 0.9rem;
    font-family: inherit;
    cursor: pointer;
    margin-top: 16px;
    padding: 8px 16px;
}

.link-btn:hover {
    text-decoration: underline;
}

/* ── Modals ───────────────────────────────────────────────────────── */

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.modal {
    background: #141414;
    border: 1px solid #222;
    border-radius: 12px;
    width: 100%;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
    padding: 20px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.modal-header h2 {
    font-size: 1.1rem;
    color: #fff;
}

.close-btn {
    width: 44px;
    height: 44px;
    background: none;
    border: none;
    color: #888;
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
}

.close-btn:hover {
    color: #fff;
    background: #222;
}

/* ── Selection Options ───────────────────────────────────────────── */

.selection-options {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.option-card {
    display: flex;
    gap: 12px;
    padding: 12px;
    border: 1px solid #222;
    border-radius: 10px;
    cursor: pointer;
    transition: border-color 0.2s;
    background: none;
    color: inherit;
    font-family: inherit;
    text-align: left;
    width: 100%;
}

.option-card:hover {
    border-color: #7c8aff;
}

.option-thumb {
    width: 60px;
    height: 90px;
    object-fit: cover;
    border-radius: 6px;
    flex-shrink: 0;
    background: #1a1a2e;
}

.option-details {
    flex: 1;
    min-width: 0;
}

.option-title {
    font-weight: 600;
    color: #fff;
    font-size: 0.9rem;
}

.option-meta {
    color: #999;
    font-size: 0.8rem;
    margin-top: 4px;
}

/* ── Manual Form ─────────────────────────────────────────────────── */

.manual-form {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.manual-form input {
    height: 44px;
    background: #1a1a1a;
    border: 1px solid #222;
    border-radius: 12px;
    padding: 0 16px;
    color: #e0e0e0;
    font-size: 0.95rem;
    font-family: inherit;
    outline: none;
}

.manual-form input::placeholder {
    color: #666;
}

.manual-form input:focus {
    border-color: #7c8aff;
}

.form-row {
    display: flex;
    gap: 8px;
}

.btn {
    height: 44px;
    border: none;
    border-radius: 12px;
    font-size: 0.95rem;
    font-family: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 0 20px;
    transition: opacity 0.2s;
}

.btn:hover {
    opacity: 0.85;
}

.btn-primary {
    background: #7c8aff;
    color: #fff;
    width: 100%;
}

.btn-secondary {
    background: #222;
    color: #e0e0e0;
}

/* ── Toast ────────────────────────────────────────────────────────── */

.toast-container {
    position: fixed;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.toast {
    background: #1a1a2e;
    border: 1px solid #7c8aff;
    border-radius: 12px;
    padding: 12px 20px;
    color: #e0e0e0;
    font-size: 0.9rem;
    white-space: nowrap;
    animation: toast-in 0.3s ease, toast-out 0.3s ease 2.7s forwards;
    pointer-events: auto;
}

.toast.error {
    border-color: #ff4444;
}

@keyframes toast-in {
    from { opacity: 0; transform: translateY(-10px); }
    to   { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out {
    from { opacity: 1; }
    to   { opacity: 0; }
}

/* ── Loading Overlay ─────────────────────────────────────────────── */

.loading-overlay {
    position: fixed;
    inset: 0;
    z-index: 150;
    background: rgba(10, 10, 10, 0.9);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    color: #888;
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid #222;
    border-top-color: #7c8aff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ── Mystical Tower Background ──────────────────────────────────── */

body.tower-mode {
    background: radial-gradient(ellipse at 50% 80%, #1a1040 0%, #0d0d1a 40%, #0a0a0a 100%);
    transition: background 0.6s ease;
}

body.tower-mode .app-header h1 {
    background: linear-gradient(135deg, #c9b3ff, #7c8aff);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

body.tower-mode .search-bar {
    background: rgba(10, 10, 10, 0.6);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom-color: rgba(124, 138, 255, 0.15);
}

.tower-particles {
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    display: none;
}

body.tower-mode .tower-particles {
    display: block;
}

/* ── Tower Container ────────────────────────────────────────────── */

.tower-container {
    position: relative;
    z-index: 1;
    max-width: 600px;
    margin: 20px auto;
    padding: 20px;
    overflow-y: auto;
    max-height: calc(100vh - 180px);
    scrollbar-width: thin;
    scrollbar-color: rgba(124, 138, 255, 0.3) transparent;
}

.tower-container::-webkit-scrollbar {
    width: 6px;
}

.tower-container::-webkit-scrollbar-thumb {
    background: rgba(124, 138, 255, 0.3);
    border-radius: 3px;
}

.tower-stack {
    position: relative;
    width: 100%;
}

/* ── Book Spine — Leather Tome ──────────────────────────────────── */

.book-spine {
    position: absolute;
    left: 50%;
    border-radius: 3px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 0 16px;
    cursor: pointer;
    transition: opacity 0.4s ease, filter 0.4s ease, box-shadow 0.3s ease;
    background:
        /* Subtle leather grain via layered radial gradients */
        radial-gradient(ellipse at 20% 50%, rgba(255, 255, 255, 0.05) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 50%, rgba(0, 0, 0, 0.07) 0%, transparent 50%),
        /* Rounded spine shading — lighter center, darker edges */
        linear-gradient(
            90deg,
            var(--leather-dark) 0%,
            var(--leather-light) 12%,
            var(--leather-base) 50%,
            var(--leather-light) 88%,
            var(--leather-dark) 100%
        );
    box-shadow:
        0 1px 0 rgba(0, 0, 0, 0.7),
        0 3px 6px rgba(0, 0, 0, 0.4),
        inset 0 1px 0 rgba(255, 255, 255, 0.07),
        inset 0 -1px 0 rgba(0, 0, 0, 0.3);
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    border-bottom: 1px solid rgba(0, 0, 0, 0.5);
}

/* Decorative bands at head and tail of spine */
.book-spine::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background:
        linear-gradient(90deg, rgba(201, 168, 76, 0.25) 0px, transparent 4px),
        linear-gradient(90deg, transparent calc(100% - 4px), rgba(201, 168, 76, 0.25) 100%);
    pointer-events: none;
}

/* Subtle grain texture overlay */
.book-spine::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
    background-image: url("data:image/svg+xml,%3Csvg width='6' height='6' xmlns='http://www.w3.org/2000/svg'%3E%3Ccircle cx='1' cy='1' r='0.5' fill='%23000' opacity='0.15'/%3E%3Ccircle cx='4' cy='3' r='0.4' fill='%23fff' opacity='0.06'/%3E%3Ccircle cx='2' cy='5' r='0.3' fill='%23000' opacity='0.1'/%3E%3C/svg%3E");
    background-size: 6px 6px;
    opacity: 0.5;
    pointer-events: none;
}

.book-spine:hover {
    filter: brightness(1.12);
    z-index: 5;
    box-shadow:
        0 0 14px rgba(201, 168, 76, 0.2),
        0 1px 0 rgba(0, 0, 0, 0.7),
        0 3px 6px rgba(0, 0, 0, 0.4),
        inset 0 1px 0 rgba(255, 255, 255, 0.07),
        inset 0 -1px 0 rgba(0, 0, 0, 0.3);
}

/* Debossed gold lettering */
.spine-title {
    font-family: "Georgia", "Palatino Linotype", "Book Antiqua", serif;
    font-size: 0.75rem;
    font-weight: 700;
    color: #c9a84c;
    text-shadow:
        0 -1px 0 rgba(0, 0, 0, 0.6),
        0 1px 0 rgba(255, 255, 255, 0.08);
    letter-spacing: 0.04em;
    text-transform: uppercase;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 60%;
    line-height: 1.2;
    position: relative;
    z-index: 1;
}

.spine-author {
    font-family: "Georgia", "Palatino Linotype", "Book Antiqua", serif;
    font-size: 0.6rem;
    color: #a08940;
    text-shadow:
        0 -1px 0 rgba(0, 0, 0, 0.5),
        0 1px 0 rgba(255, 255, 255, 0.06);
    letter-spacing: 0.02em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 30%;
    line-height: 1.2;
    position: relative;
    z-index: 1;
}

/* ── Tower Search Effects ───────────────────────────────────────── */

.book-spine.search-dim {
    opacity: 0.15;
    filter: grayscale(0.6);
}

.book-spine.search-match {
    filter: drop-shadow(0 0 12px rgba(201, 168, 76, 0.7))
            drop-shadow(0 0 25px rgba(201, 168, 76, 0.3));
    animation: levitate 2s ease-in-out infinite;
    z-index: 10 !important;
}

@keyframes levitate {
    0%, 100% { transform: translateX(var(--offset-x)) rotateZ(var(--rotation)) translateY(0); }
    50%      { transform: translateX(var(--offset-x)) rotateZ(var(--rotation)) translateY(-6px); }
}

/* ── View Toggle FAB ────────────────────────────────────────────── */

.view-toggle-fab {
    position: fixed;
    bottom: 92px;
    right: 24px;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: #1a1a1a;
    color: #7c8aff;
    border: 1px solid #333;
    font-size: 1rem;
    cursor: pointer;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, background 0.2s, box-shadow 0.2s;
}

.view-toggle-fab:hover {
    background: #222;
    transform: scale(1.08);
}

body.tower-mode .view-toggle-fab {
    background: #7c8aff;
    color: #fff;
    border-color: #7c8aff;
    box-shadow: 0 4px 16px rgba(124, 138, 255, 0.35);
}

/* ── Book Detail Modal ──────────────────────────────────────────── */

.detail-modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.detail-content {
    background: #141414;
    border: 1px solid #222;
    border-radius: 16px;
    width: 100%;
    max-width: 360px;
    overflow: hidden;
    animation: detail-in 0.25s ease;
}

@keyframes detail-in {
    from { opacity: 0; transform: scale(0.95) translateY(10px); }
    to   { opacity: 1; transform: scale(1) translateY(0); }
}

.detail-cover {
    width: 100%;
    aspect-ratio: 2 / 3;
    object-fit: cover;
    display: block;
    background: #1a1a2e;
}

.detail-cover-placeholder {
    width: 100%;
    aspect-ratio: 2 / 3;
    background: #1a1a2e;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #7c8aff;
    font-size: 3rem;
}

.detail-body {
    padding: 20px;
}

.detail-title {
    font-size: 1.15rem;
    font-weight: 600;
    color: #fff;
    line-height: 1.3;
    margin-bottom: 4px;
}

.detail-author {
    font-size: 0.9rem;
    color: #999;
    margin-bottom: 12px;
}

.detail-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}

.detail-tag {
    font-size: 0.75rem;
    padding: 4px 10px;
    border-radius: 8px;
    background: #1a1a2e;
    color: #7c8aff;
}

.detail-close {
    width: 100%;
    height: 44px;
    background: none;
    border: 1px solid #333;
    border-radius: 12px;
    color: #888;
    font-size: 0.9rem;
    font-family: inherit;
    cursor: pointer;
    transition: border-color 0.2s, color 0.2s;
}

.detail-close:hover {
    border-color: #7c8aff;
    color: #fff;
}

.card {
    cursor: pointer;
}

/* ── Tower Mobile ───────────────────────────────────────────────── */

@media (max-width: 600px) {
    .tower-container {
        padding: 12px;
        max-height: calc(100vh - 160px);
    }

    .tower-stack {
        transform: scale(0.7);
        transform-origin: top center;
    }

    .spine-title {
        font-size: 0.65rem;
    }

    .spine-author {
        font-size: 0.5rem;
    }

    .view-toggle-fab {
        bottom: 84px;
        right: 16px;
        width: 42px;
        height: 42px;
    }
}
