:root {
    --bg-color: #f8f9fa;
    --text-color: #212529;
    --primary-color: #2c3e50;
    --accent-color: #e67e22;
    --border-color: #dee2e6;
    --card-bg: #ffffff;
    --font-serif: 'Merriweather', serif;
    --font-sans: 'Inter', sans-serif;
}

* {
    box-sizing: border-box;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-color: #121212;
        --text-color: #e0e0e0;
        --primary-color: #ecf0f1;
        --accent-color: #d35400;
        --border-color: #333;
        --card-bg: #1e1e1e;
    }
}

/* Manual Dark Mode Override */
body.dark-mode {
    --bg-color: #121212;
    --text-color: #e0e0e0;
    --primary-color: #ecf0f1;
    --accent-color: #d35400;
    --border-color: #333;
    --card-bg: #1e1e1e;
}

/* Manual Light Mode Override (for when system is dark but user wants light) */
body.light-mode {
    --bg-color: #f8f9fa;
    --text-color: #212529;
    --primary-color: #2c3e50;
    --accent-color: #e67e22;
    --border-color: #dee2e6;
    --card-bg: #ffffff;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-sans);
    margin: 0;
    padding: 0;
    line-height: 1.6;
    transition: background-color 0.3s, color 0.3s;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.container {
    max-width: 800px;
    margin: 0 auto;
    padding: 2rem 1rem;
    flex: 1;
    width: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
}

main {
    flex: 1;
    width: 100%;
}

.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    background-color: var(--primary-color);
    color: var(--bg-color);
    border: 1px solid transparent;
    border-radius: 4px;
    cursor: pointer;
    font-family: var(--font-sans);
    font-weight: 600;
    text-align: center;
    font-size: 1rem;
    line-height: 1.5;
    text-decoration: none;
}

.btn:hover {
    opacity: 0.9;
    text-decoration: none;
}

.btn:focus,
a:focus,
input:focus,
textarea:focus,
button:focus {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
}

.form-group {
    margin-bottom: 1rem;
}

label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
}

input[type="text"],
input[type="password"],
textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background-color: var(--card-bg);
    color: var(--text-color);
    font-family: inherit;
    box-sizing: border-box;
}

.card {
    background-color: var(--card-bg);
    padding: 2rem;
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    border: 1px solid transparent;
    /* Placeholder for dark mode */
}

body.dark-mode .card {
    border-color: var(--border-color);
    box-shadow: none;
}

@media (prefers-color-scheme: dark) {
    .card {
        border-color: var(--border-color);
        box-shadow: none;
    }
}

.btn-icon {
    padding: 0.25rem 0.5rem;
    font-size: 1.1rem;
    line-height: 1;
    background: none;
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    min-height: 32px;
}

.btn-icon:hover {
    background-color: var(--border-color);
}

.toolbar {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
    padding: 0.5rem;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    flex-wrap: wrap;
}

.toolbar-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-color);
    font-weight: bold;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
}

.toolbar-btn:hover {
    background-color: var(--bg-color);
}

.form-select {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background-color: var(--card-bg);
    color: var(--text-color);
    font-family: inherit;
    box-sizing: border-box;
}

.alert {
    padding: 1rem;
    margin-bottom: 1rem;
    border-radius: 4px;
    background-color: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 0;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 2rem;
}

.logo {
    font-family: var(--font-serif);
    font-size: 1.5rem;
    font-weight: bold;
    color: var(--text-color);
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 1rem;
}

nav ul {
    list-style: none;
    display: flex;
    gap: 1rem;
    padding: 0;
    margin: 0;
    align-items: center;
}

.mobile-menu-toggle {
    display: none;
    flex-direction: column;
    justify-content: space-between;
    width: 30px;
    height: 21px;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    z-index: 10;
}

.mobile-menu-toggle .bar {
    width: 100%;
    height: 3px;
    background-color: var(--text-color);
    border-radius: 10px;
    transition: all 0.3s linear;
    position: relative;
    transform-origin: 1px;
}

@media (max-width: 768px) {
    .container {
        padding: 1rem;
    }

    header {
        padding: 1rem 0;
        margin-bottom: 1rem;
        position: relative;
    }

    .mobile-menu-toggle {
        display: flex;
    }

    /* Connection status always visible on mobile */
    .header-controls {
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    /* Show mobile connection status */
    .connection-status-mobile {
        display: inline-flex !important;
        font-size: 0.75rem;
        padding: 0.2rem 0.5rem;
    }

    .connection-status-mobile .status-dot {
        width: 10px;
        height: 10px;
    }

    /* Hide desktop connection status in nav menu */
    .connection-status-desktop {
        display: none !important;
    }

    .nav-menu {
        position: absolute;
        top: 100%;
        right: 0;
        background-color: var(--card-bg);
        width: 200px;
        border: 1px solid var(--border-color);
        border-radius: 8px;
        padding: 1rem;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        display: none;
        z-index: 10000;
    }

    .nav-menu.active {
        display: block;
    }

    nav ul {
        flex-direction: column;
        gap: 0.5rem;
        align-items: flex-start;
    }

    nav ul li {
        width: 100%;
    }

    nav ul li a,
    nav ul li button {
        display: block;
        width: 100%;
        padding: 0.5rem 0;
        text-align: left;
    }

    /* Ensure chapter toolbar is sticky on mobile */
    .chapter-toolbar {
        position: -webkit-sticky;
        position: sticky;
        top: 0;
        z-index: 999;
        background-color: var(--bg-color);
        padding: 0.75rem 0;
        margin-bottom: 1rem;
        font-size: 0.9rem;
    }

    .chapter-toolbar .btn {
        padding: 0.4rem 0.8rem;
        font-size: 0.85rem;
    }

    #autosave-status {
        font-size: 0.75rem;
    }
}

.chapter-toolbar {
    position: -webkit-sticky;
    /* Safari */
    position: sticky;
    top: 0;
    background-color: var(--bg-color);
    z-index: 900;
    padding: 1rem 0;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    /* Ensure full width */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* Connection Status Indicator */
.connection-status {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    padding: 0.25rem 0.75rem;
    border-radius: 12px;
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
}

.connection-status .status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
}

.connection-status.online .status-dot {
    background-color: #27ae60;
    animation: pulse 2s infinite;
}

.connection-status.offline .status-dot {
    background-color: #e74c3c;
}

/* Hide mobile connection status on desktop */
.connection-status-mobile {
    display: none;
}

/* Show desktop connection status */
.connection-status-desktop {
    display: list-item;
}

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

/* Sync Status */
.sync-status {
    display: inline-block;
    font-size: 0.8rem;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
}

.sync-status.syncing {
    color: #3498db;
}

.sync-status.synced {
    color: #27ae60;
}

.sync-status.error {
    color: #e74c3c;
}

/* Conflict Resolution Modal */
.conflict-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.conflict-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
}

.conflict-dialog {
    position: relative;
    background-color: var(--card-bg);
    border-radius: 8px;
    padding: 2rem;
    max-width: 800px;
    width: 90%;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

.conflict-dialog h2 {
    margin-top: 0;
    color: #e74c3c;
}

.conflict-versions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin: 1.5rem 0;
}

@media (max-width: 768px) {
    .conflict-versions {
        grid-template-columns: 1fr;
    }
}

.conflict-versions .version {
    border: 2px solid var(--border-color);
    border-radius: 8px;
    padding: 1rem;
}

.conflict-versions .version h3 {
    margin-top: 0;
    font-size: 1rem;
    color: var(--accent-color);
}

.conflict-versions .version-preview {
    background-color: var(--bg-color);
    padding: 1rem;
    border-radius: 4px;
    font-size: 0.9rem;
}

.conflict-versions .version-preview strong {
    display: block;
    margin-bottom: 0.5rem;
}

.conflict-versions .version-preview p {
    margin: 0.5rem 0;
    color: var(--text-color);
    opacity: 0.8;
}

.conflict-versions .version-preview small {
    display: block;
    margin-top: 0.5rem;
    opacity: 0.6;
}

.conflict-actions {
    display: flex;
    gap: 1rem;
    justify-content: flex-end;
    margin-top: 1.5rem;
    flex-wrap: wrap;
}

.conflict-actions .btn {
    flex: 0 1 auto;
}

.btn-primary {
    background-color: #27ae60;
    color: white;
}

.btn-secondary {
    background-color: #3498db;
    color: white;
}

/* Full Screen Editor Mode */
body.full-screen-editor header,
body.full-screen-editor footer {
    display: none !important;
}

body.full-screen-editor .container {
    max-width: 100% !important;
    padding: 0 !important;
    margin: 0 !important;
    height: 100vh;
}

body.full-screen-editor .chapter-toolbar {
    padding: 0.5rem 1rem;
    margin-bottom: 0;
    border-bottom: 1px solid var(--border-color);
}

body.full-screen-editor #chapter-form {
    height: calc(100vh - 60px); /* Adjust based on toolbar height */
    padding: 1rem;
}

body.full-screen-editor .form-group {
    margin-bottom: 0.5rem;
}

body.full-screen-editor textarea {
    height: 100% !important;
    min-height: unset !important;
}

/* Hide delete button in full screen to avoid clutter/accidents */
body.full-screen-editor #delete-chapter-form {
    display: none !important;
}