:root {
    --bg: #0b0d14;
    --surface: #1a1d29;
    --surface-hover: #262a3b;
    --primary: #8b5cf6;
    --primary-hover: #7c3aed;
    --text: #f8fafc;
    --text-muted: #94a3b8;
    --border: rgba(255, 255, 255, 0.08);
    --glass-bg: rgba(26, 29, 41, 0.6);
    --glass-border: rgba(255, 255, 255, 0.1);
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg);
    color: var(--text);
    line-height: 1.5;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Header */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    z-index: 10;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    color: var(--primary);
}

.logo h1 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text);
    letter-spacing: -0.5px;
}

/* Mode Toggle */
.mode-toggle {
    display: flex;
    background: var(--bg);
    border-radius: 99px;
    padding: 0.25rem;
    border: 1px solid var(--border);
}

.mode-toggle button {
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 0.5rem 1.25rem;
    border-radius: 99px;
    font-family: inherit;
    font-weight: 500;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.3s ease;
}

.mode-toggle button.active {
    background: var(--surface-hover);
    color: var(--text);
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.app-content {
    flex: 1;
    position: relative;
    overflow: hidden;
}

.mode-section {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    flex-direction: column;
    padding: 1.5rem;
    overflow-y: auto;
    animation: fadeIn 0.4s ease forwards;
}

.mode-section.active {
    display: flex;
}

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

/* Edit Mode */
.upload-zone {
    border: 2px dashed var(--border);
    border-radius: 1rem;
    padding: 3rem 1.5rem;
    text-align: center;
    transition: all 0.3s ease;
    background: var(--surface);
    position: relative;
    cursor: pointer;
    margin-bottom: 2rem;
}

.upload-zone:hover, .upload-zone.dragover {
    border-color: var(--primary);
    background: rgba(139, 92, 246, 0.05);
}

.upload-content {
    pointer-events: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.upload-content p {
    color: var(--text-muted);
    font-size: 1.1rem;
}

.upload-content span {
    color: var(--primary);
    font-weight: 600;
}

#file-input {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
}

.pages-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 2rem;
}

.page-item {
    display: flex;
    align-items: center;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    padding: 1rem;
    gap: 1rem;
    transition: transform 0.2s, box-shadow 0.2s;
}

.page-item.sortable-ghost {
    opacity: 0.4;
}

.drag-handle {
    cursor: grab;
    color: var(--text-muted);
    display: flex;
    align-items: center;
}

.drag-handle:active {
    cursor: grabbing;
}

.page-preview {
    width: 60px;
    height: 80px;
    border-radius: 0.25rem;
    background: #fff;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
}

.page-preview canvas {
    width: 100%;
    height: auto;
}

.page-details {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.page-title-input {
    background: transparent;
    border: 1px solid transparent;
    border-bottom: 1px solid var(--border);
    color: var(--text);
    font-family: inherit;
    font-size: 1rem;
    padding: 0.25rem;
    transition: border-color 0.3s;
}

.page-title-input:focus {
    outline: none;
    border-bottom-color: var(--primary);
}

.remove-btn {
    background: transparent;
    border: none;
    color: #ef4444;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 0.5rem;
    transition: background 0.2s;
}

.remove-btn:hover {
    background: rgba(239, 68, 68, 0.1);
}

.edit-actions {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
    margin-bottom: 2rem;
}

.book-title-input {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: inherit;
    font-size: 1.25rem;
    font-weight: 600;
    padding: 0.75rem 1.5rem;
    border-radius: 99px;
    width: 100%;
    max-width: 400px;
    text-align: center;
    transition: all 0.3s;
}

.book-title-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.2);
}

.primary-btn {
    background: linear-gradient(135deg, var(--primary), #a78bfa);
    color: white;
    border: none;
    padding: 1rem 2.5rem;
    border-radius: 99px;
    font-family: inherit;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(139, 92, 246, 0.4);
    transition: transform 0.3s, box-shadow 0.3s;
}

.primary-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(139, 92, 246, 0.6);
}

/* View/Read Mode (Book) */
#read-mode.mode-section {
    padding: 0;
    background: #000;
    overflow: hidden;
}

.read-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1.5rem;
    background: rgba(0, 0, 0, 0.85);
    color: white;
    flex-shrink: 0;
    z-index: 20;
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.read-header h2 {
    font-size: 1.1rem;
    font-weight: 500;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 50%;
}

.icon-btn {
    background: transparent;
    border: none;
    color: white;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-family: inherit;
    font-size: 1rem;
    padding: 0.5rem;
    border-radius: 0.5rem;
    transition: background 0.2s;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.book-viewport {
    flex: 1 1 0%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
}

.book-container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.flip-book {
    /* StPageFlip manages visibility */
}

.flip-book .page {
    background-color: #fff;
    box-shadow: inset 0 0 10px rgba(0,0,0,0.1);
    overflow: hidden;
}

/* Hard shadow on the spine side to simulate real pages */
.flip-book .page.--left {
    border-right: 1px solid rgba(0,0,0,0.1);
    background: linear-gradient(to right, #fff 90%, #f0f0f0 98%, #e4e4e4 100%);
}
.flip-book .page.--right {
    background: linear-gradient(to left, #fff 90%, #f0f0f0 98%, #e4e4e4 100%);
}

.page-content {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    background: #fff;
}

.page-content canvas {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.book-page-title {
    position: absolute;
    bottom: 1rem;
    left: 0;
    width: 100%;
    text-align: center;
    color: rgba(0,0,0,0.5);
    font-size: 0.8rem;
    pointer-events: none;
}

.book-controls {
    height: 70px;
    min-height: 70px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    background: var(--bg);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
    z-index: 10;
}

.control-btn {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}

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

.control-btn:active {
    transform: scale(0.95);
}

.page-indicator {
    font-variant-numeric: tabular-nums;
    font-weight: 500;
    color: var(--text-muted);
}

.page-indicator span {
    color: var(--text);
}

.center-controls {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
}

.fit-mode-select {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.2rem 0.5rem;
    font-family: inherit;
    font-size: 0.75rem;
    cursor: pointer;
}

.fit-mode-select:focus {
    outline: none;
    border-color: var(--primary);
}

/* Empty state */
.empty-book {
    color: var(--text-muted);
    font-size: 1.2rem;
    text-align: center;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Library Mode */
.library-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

.library-header h2 {
    font-size: 2rem;
    font-weight: 700;
}

.secondary-btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 0.75rem 1.5rem;
    border-radius: 99px;
    font-family: inherit;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
}

.secondary-btn:hover {
    background: var(--surface-hover);
    border-color: var(--primary);
}

.books-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 1.5rem;
}

.book-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 1rem;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    flex-direction: column;
}

.book-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
    border-color: var(--primary);
}

.book-cover {
    width: 100%;
    aspect-ratio: 2/3;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
}

.book-cover canvas {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.book-info {
    padding: 1rem;
}

.book-info h3 {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-info p {
    color: var(--text-muted);
    font-size: 0.875rem;
}

.book-delete {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: rgba(239, 68, 68, 0.9);
    color: white;
    border: none;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
}

.book-card:hover .book-delete {
    opacity: 1;
}

@media (min-width: 768px) {
    .mode-section {
        padding: 2rem 4rem;
    }
    .book-spread {
        max-height: 90%;
    }
}

/* ---------- Additions: auth, admin-only UI, modal, toast ---------- */
body { height: 100dvh; }

body:not(.is-admin) .admin-only { display: none !important; }

.header-right { display: flex; align-items: center; gap: 0.75rem; }

.auth-btn {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 0.5rem 1.1rem;
    border-radius: 99px;
    font-family: inherit;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}
.auth-btn:hover { background: var(--surface-hover); border-color: var(--primary); }
body.is-admin .auth-btn { border-color: var(--primary); }

.modal-overlay {
    position: fixed; inset: 0; z-index: 100;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex; align-items: center; justify-content: center;
    padding: 1rem;
}
.modal-overlay[hidden] { display: none; }
.modal {
    width: 100%; max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--glass-border);
    border-radius: 1rem;
    padding: 1.75rem;
    box-shadow: var(--shadow);
    display: flex; flex-direction: column; gap: 0.5rem;
}
.modal h2 { font-size: 1.4rem; margin-bottom: 0.75rem; }
.modal label { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.5rem; }
.modal input {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-family: inherit;
    font-size: 1rem;
    padding: 0.7rem 0.9rem;
    border-radius: 0.6rem;
}
.modal input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.25); }
.login-error { min-height: 1.3rem; color: #f87171; font-size: 0.875rem; margin-top: 0.5rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.75rem; margin-top: 0.5rem; }
.primary-btn.small { padding: 0.75rem 1.75rem; font-size: 1rem; }
.primary-btn:disabled { opacity: 0.6; cursor: wait; transform: none; }

.book-edit {
    position: absolute;
    top: 0.5rem; right: 3rem;
    background: rgba(139, 92, 246, 0.95);
    color: white; border: none; border-radius: 50%;
    width: 32px; height: 32px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; opacity: 0; transition: opacity 0.2s;
}
.book-card:hover .book-edit, .book-card:focus-within .book-edit,
.book-card:focus-within .book-delete { opacity: 1; }
@media (hover: none) { .book-edit, .book-delete { opacity: 1; } }

.book-loading { color: var(--text-muted); font-size: 1.1rem; }

.toast {
    position: fixed; left: 50%; bottom: 1.5rem;
    transform: translate(-50%, 20px);
    background: var(--surface-hover);
    color: var(--text);
    border: 1px solid var(--glass-border);
    padding: 0.75rem 1.25rem;
    border-radius: 99px;
    font-size: 0.9rem;
    opacity: 0; pointer-events: none;
    transition: opacity 0.25s, transform 0.25s;
    z-index: 200;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 560px) {
    .app-header { flex-wrap: wrap; gap: 0.5rem; padding: 0.75rem 1rem; }
    .mode-toggle button { padding: 0.45rem 0.9rem; }
}

/* ---------- Reading view: single-page on phones, zoom, less chrome ---------- */
.book-viewport { touch-action: none; }
.book-container { transform-origin: 0 0; will-change: transform; }
.page-content img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    user-select: none;
    -webkit-user-select: none;
    -webkit-user-drag: none;
    pointer-events: none;
}
.zoom-layer {
    position: absolute; inset: 0; z-index: 6;
    touch-action: none; cursor: grab;
}
.zoom-layer[hidden] { display: none; }
.zoom-layer:active { cursor: grabbing; }
.zoom-controls {
    position: absolute; right: 0.75rem; bottom: 0.75rem; z-index: 8;
    display: flex; flex-direction: column; gap: 0.5rem;
}
.zoom-controls .control-btn {
    width: 40px; height: 40px;
    background: rgba(26, 29, 41, 0.85);
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
}
.zoom-controls [hidden] { display: none; }

@media (max-width: 768px), (max-height: 500px) {
    /* give the book the whole screen while reading */
    body.reading .app-header { display: none; }
    .read-header { padding: 0.4rem 0.75rem; }
    .book-controls { height: 56px; min-height: 56px; padding: 0 1rem; }
    .control-btn { width: 40px; height: 40px; }
    .book-page-title { bottom: 0.4rem; font-size: 0.7rem; }

    /* library + editor */
    .mode-section { padding: 1rem; }
    .library-header { margin-bottom: 1rem; }
    .library-header h2 { font-size: 1.5rem; }
    .books-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 1rem; }
    .upload-zone { padding: 2rem 1rem; margin-bottom: 1.25rem; }
    .upload-content p { font-size: 1rem; }
    .page-item { padding: 0.75rem; gap: 0.75rem; }
    .primary-btn { width: 100%; max-width: 400px; }
    .primary-btn.small { width: auto; }
}
