:root {
    --bg-color: #121212;
    --surface-color: #1e1e1e;
    --primary-text-color: #e0e0e0;
    --secondary-text-color: #a0a0a0;
    --accent-color: #8a2be2;
    --border-color: #333;
}

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

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    color: var(--primary-text-color);
    line-height: 1.6;
}

/* By default, hide main containers. JS will add a body class to show the correct one. */
/* This prevents a "flash of unstyled content" (FOUC) on page load. */
header, #auth-container, .app-container {
    display: none;
}

header {
    /* display is now controlled by body.app-view */
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    background-color: var(--surface-color);
    border-bottom: 1px solid var(--border-color);
    height: 61px;
}
body.app-view > header {
    display: flex;
}


.menu-toggle-btn {
    display: none; /* Hidden on desktop by default */
    background: none;
    border: none;
    color: var(--primary-text-color);
    font-size: 1.5rem;
    cursor: pointer;
    margin-right: 1rem;
}

.logo {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary-text-color);
}

.header-actions button {
    margin-left: 1rem;
    background-color: var(--accent-color);
    color: white;
}

.header-actions #save-status {
    color: var(--secondary-text-color);
    font-size: 0.9rem;
    transition: color 0.3s;
}

body.app-view > .app-container {
    display: flex;
}
.app-container {
    height: calc(100vh - 61px); /* Full height minus header on desktop */
}

.sidebar {
    width: 250px;
    background-color: var(--surface-color);
    padding: 1.5rem;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar h2 {
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
}

#page-list {
    list-style: none;
    flex-grow: 1;
    min-height: 0; /* CRITICAL: Allows the list to shrink and scroll properly within the flex container */
    overflow-y: auto;
}

#page-list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.2s;
}

#page-list li:hover {
    background-color: #2a2a2a;
}

#page-list li.active {
    background-color: var(--accent-color);
    color: white;
    font-weight: 500;
}

.page-title {
    flex-grow: 1;
    margin-right: 0.5rem; /* Space between title and delete button */
    word-break: break-all;
}

.delete-page-btn {
    background: none;
    border: none;
    color: var(--secondary-text-color);
    cursor: pointer;
    font-size: 1.2rem;
    padding: 0 0.25rem;
    opacity: 0; /* Hide by default */
    transition: opacity 0.2s, color 0.2s;
    flex-shrink: 0; /* Prevent button from shrinking */
}

#page-list li:hover .delete-page-btn,
#page-list li.active .delete-page-btn {
    opacity: 1; /* Show on hover or if active */
}

.delete-page-btn:hover {
    color: #d32f2f; /* Red on hover */
}

#add-page-btn, .toolbar-btn, .toolbar select, .toolbar input {
    background-color: #2a2a2a;
    color: var(--primary-text-color);
    border: 1px solid var(--border-color);
    border-radius: 5px;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    transition: background-color 0.2s, border-color 0.2s;
    font-family: 'Inter', sans-serif;
}

#add-page-btn {
    width: 100%;
    padding: 0.75rem;
    margin-top: 1rem;
}

#add-page-btn:hover, .toolbar-btn:hover, .toolbar select:hover, .toolbar input:hover {
    background-color: #333;
    border-color: #555;
}

.main-content {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.toolbar {
    padding: 0.5rem 1.5rem;
    background-color: var(--surface-color);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.toolbar-separator {
    width: 1px;
    height: 20px;
    background-color: var(--border-color);
    margin: 0 0.25rem;
}

#editor {
    flex-grow: 1;
    padding: 2rem;
    overflow-y: auto;
    outline: none;
    position: relative;
}

#editor h1, #editor h2, #editor h3 {
    margin-top: 1em;
    margin-bottom: 0.5em;
}

#editor p {
    margin: 0; /* Remove default paragraph spacing for single-line feel */
}

#editor a {
    color: #6495ed;
    text-decoration: none;
}

#editor a:hover {
    text-decoration: underline;
}

#editor ul, #editor ol {
    margin-left: 2.5em; /* Increased margin to give numbers space */
    margin-bottom: 1rem;
}

/* --- Custom Numbered List Styling --- */
#editor ol {
    list-style-type: none; /* Hide default browser numbering */
    counter-reset: list-counter; /* Create a new counter */
}

#editor ol > li {
    counter-increment: list-counter; /* Increment the counter for each list item */
    position: relative;
}

#editor ol > li::before {
    content: counters(list-counter, ".") ". "; /* Display the counter */
    position: absolute;
    left: -2.5em; /* Position number within the new margin */
    width: 2.2em; /* Give a fixed width for alignment */
    text-align: right; /* Align multi-digit numbers neatly */
}

#editor table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 1rem;
    table-layout: fixed; /* Crucial for predictable column resizing */
}

#editor th, #editor td {
    border: 1px solid var(--border-color);
    padding: 8px;
    text-align: left;
    position: relative; /* Required for some resize implementations */
}

#editor th {
    background-color: #2a2a2a;
    font-weight: 600;
}

#editor img {
    max-width: 100%;
    height: auto;
    border-radius: 5px;
}

.content-block {
    position: absolute;
    min-width: 150px; /* More flexible resizing */
    min-height: 55px; /* Increased to prevent scrollbar on new blocks */
    outline: 1px dashed transparent;
    transition: outline-color 0.2s;
    overflow: visible; /* Allow controls positioned outside the bounds (e.g., delete button) to be fully visible. */
    display: flex; /* Use flexbox to make the child fill the available space. */
}

/* The new inner container that actually holds the editable text */
.editable-area {
    flex-grow: 1; /* Fill the parent block */
    outline: none; /* No ugly focus outline on the inner div */
    overflow-y: hidden; /* Default to hidden. JS will toggle to 'auto' when active. */
    overflow-x: hidden; /* Prevent horizontal scrollbars */
    padding: 1rem; /* Add some internal padding for text */

    /* Hide scrollbar track for Firefox */
    scrollbar-width: none;
}

/* Hide scrollbar track for Webkit browsers (Chrome, Safari, Edge) */
.editable-area::-webkit-scrollbar {
    display: none;
}

/* A class for when a block is being actively edited or focused */
.content-block.is-active {
    outline-color: var(--accent-color);
    z-index: 10;
}
/* Add a class for when a block is being dragged */
.content-block.dragging {
    opacity: 0.7;
    box-shadow: 0 5px 15px rgba(0,0,0,0.3);
    z-index: 1000; /* Make sure it's on top of everything */
}

/* The delete button for content blocks */
.delete-block-btn {
    position: absolute;
    top: -10px;
    right: -10px;
    width: 22px;
    height: 22px;
    background-color: #d32f2f; /* A distinct red */
    color: white;
    border: 1px solid var(--bg-color);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    line-height: 22px;
    opacity: 0; /* Hidden by default */
    transition: opacity 0.2s ease-in-out;
    z-index: 11; /* Ensure it's on top */
}

.drag-handle {
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%); /* Center the handle */
    width: 22px;
    height: 22px;
    background-color: #4a4a4a;
    color: white;
    border: 1px solid var(--bg-color);
    border-radius: 50%;
    cursor: move; /* Use the move cursor */
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px; /* Adjusted for the icon */
    opacity: 0;
    transition: opacity 0.2s ease-in-out;
    z-index: 11;
}

.content-block.is-active .delete-block-btn,
.content-block.is-active .drag-handle,
.content-block.is-active .resize-handle-se {
    opacity: 1; /* Show when the block is active */
}

/* Custom resize handle to replace the buggy native browser implementation */
.resize-handle-se {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 12px;
    height: 12px;
    cursor: se-resize;
    opacity: 0;
    transition: opacity 0.2s ease-in-out;
    z-index: 12; /* On top of other controls */
    /* Simple visual representation of a resize handle */
    background:
        linear-gradient(135deg, transparent 60%, var(--accent-color) 60%, var(--accent-color) 80%, transparent 80%),
        linear-gradient(135deg, transparent 75%, var(--accent-color) 75%);
}

.sticky-note {
    background-color: #fffa9e;
    padding: 1rem;
    box-shadow: 3px 3px 7px rgba(0,0,0,0.2);
    transform: rotate(-1.5deg);
    font-family: 'Courier New', monospace;
    border: 1px solid #e5e08e;
}

/* The editable area inside a sticky note needs different colors */
.sticky-note .editable-area {
    color: #333;
    padding: 0; /* The parent .sticky-note already has padding */
}

.sticky-note.dragging {
    transform: rotate(0deg);
    box-shadow: 5px 5px 15px rgba(0,0,0,0.3);
}

/* Ensure text color inside sticky notes is readable */
.sticky-note p, .sticky-note h1, .sticky-note h2, .sticky-note h3, .sticky-note li {
    color: #333;
}

.sticky-note a {
    color: #0056b3;
}

/* Adjust control colors for better visibility on yellow */
.sticky-note .delete-block-btn {
    border-color: #fffa9e;
}
.sticky-note .drag-handle {
    border-color: #fffa9e;
}

.timestamp {
    font-size: 0.85em;
    color: var(--secondary-text-color);
}

/* --- Auth Screen Styles --- */
#auth-container {
    /* display is now controlled by body.auth-view */
    justify-content: center;
    align-items: center;
    height: 100vh; /* Full viewport height, as header is hidden in this view */
}
body.auth-view > #auth-container {
    display: flex;
}

.auth-box {
    background-color: var(--surface-color);
    padding: 2rem 3rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    width: 100%;
    max-width: 400px;
    text-align: center;
}

.auth-box h2 {
    margin-bottom: 1.5rem;
}

.auth-box form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.auth-box input {
    padding: 0.75rem;
    background-color: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 5px;
    color: var(--primary-text-color);
    font-size: 1rem;
}

.auth-box button {
    padding: 0.75rem;
    background-color: var(--accent-color);
    border: none;
    border-radius: 5px;
    color: white;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s;
}

.auth-box button:hover {
    background-color: #7620c9;
}

.auth-box p {
    margin-top: 1.5rem;
    color: var(--secondary-text-color);
}

.auth-box a {
    color: var(--accent-color);
    text-decoration: none;
    font-weight: 500;
}

.auth-message {
    margin-top: 1rem;
    color: #d32f2f; /* Red for errors */
    min-height: 1.2em;
}

.terms-box {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-align: left;
    font-size: 0.9rem;
}

.terms-box input[type="checkbox"] {
    /* Make checkbox larger and easier to click */
    width: 1.2em;
    height: 1.2em;
    flex-shrink: 0;
    margin: 0; /* Override other input styles */
}

.terms-box label {
    color: var(--secondary-text-color);
}

.terms-box a {
    text-decoration: underline;
}

.captcha-box {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background-color: #2a2a2a;
    padding: 0.5rem;
    border-radius: 5px;
}

#captcha-image {
    height: 40px;
    width: 120px;
    border-radius: 4px;
    border: 1px solid var(--border-color);
    flex-shrink: 0;
}

#captcha-refresh-btn {
    padding: 0.5rem;
    font-size: 1.2rem;
    line-height: 1;
    flex-shrink: 0;
    background-color: #333;
}

.captcha-box input {
    flex-grow: 1;
    margin: 0; /* Override form gap */
}

.auth-footer {
    margin-top: 2rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border-color);
    font-size: 0.9rem;
    display: flex;
    justify-content: center;
    gap: 1rem;
}

.auth-footer a {
    color: var(--secondary-text-color);
}

.app-footer {
    padding: 0.5rem 1.5rem;
    background-color: var(--surface-color);
    border-top: 1px solid var(--border-color);
    text-align: center;
    font-size: 0.9rem;
    display: flex;
    justify-content: center;
    gap: 1rem;
    flex-shrink: 0; /* Prevent it from shrinking in the flex container */
}

.app-footer a {
    color: var(--secondary-text-color);
    text-decoration: none;
}

.sidebar-overlay {
    display: none; /* Hidden by default */
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.5);
    z-index: 1000; /* Below sidebar (1002), above header (1001) */
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
}

.sidebar-overlay.visible {
    display: block;
    opacity: 1;
}

/* --- Responsive Design for Mobile --- */
@media (max-width: 768px) {
    header {
        padding: 1rem;
        /* Pin header to the viewport for robust mobile display */
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1001; /* Above sidebar overlay */
    }

    .menu-toggle-btn {
        display: block; /* Show hamburger menu on mobile */
    }

    .header-actions {
        /* Make user info and save status more compact on mobile */
        font-size: 0.85rem;
    }

    .header-actions #logout-btn {
        padding: 0.4rem 0.6rem;
        margin-left: 0.5rem;
        font-size: 0.8rem;
    }

    body.app-view > .app-container {
        display: block; /* Override flexbox for a simpler mobile layout */
        height: auto;
        /* Add padding to prevent content from being hidden by the fixed header and toolbar. */
        padding-top: 110px; /* 61px for header + ~49px for toolbar */
    }

    .sidebar {
        position: fixed;   /* Position relative to the viewport for a true overlay */
        top: 0;
        left: 0;
        height: 100vh;   /* Ensure it covers the full viewport height */
        width: 280px;
        max-width: 80%;
        left: -280px; /* Start off-screen to avoid causing horizontal scroll */
        transition: left 0.3s ease-in-out; /* Animate the left property */
        z-index: 1002;   /* Must be higher than the sticky header (1001) */
        box-shadow: 5px 0 15px rgba(0,0,0,0.2);
    }

    .sidebar.open {
        left: 0;
    }

    .main-content {
        /* Remove flex behavior that contains the editor scroll */
        display: block;
        overflow: visible;
    }

    .toolbar {
        /* Pin toolbar to the viewport below the header */
        position: fixed;
        top: 61px; /* Matches header height */
        z-index: 999;
        left: 0; /* Pin to the left edge */
        right: 0; /* Pin to the right edge */
        background-color: var(--surface-color); /* Ensure consistent background */

        /* --- Robust Mobile Toolbar Layout using Flexbox --- */
        display: flex;      /* Use flexbox for alignment and scrolling */
        overflow-x: auto;
        padding: 0.5rem 1rem;
        gap: 0.5rem;        /* Restore gap for spacing between items */
    }

    /* Ensure toolbar items don't shrink, which enables horizontal scrolling */
    .toolbar > * {
        flex-shrink: 0;
    }

    #editor {
        padding: 1.5rem;
        /* Editor no longer needs to scroll itself */
        overflow-y: visible;
        /* Ensure the editor is always tall enough to be tapped, even when empty.
           110px is the combined height of the fixed header and toolbar. */
        min-height: calc(100vh - 110px);
    }

    .auth-box {
        padding: 2rem 1.5rem;
        width: 90%;
    }
}
