:root {
    /* Colors - Dark Mode (Default for Workspace) */
    --bg-workspace: #18191a;
    /* Deeper, richer dark */
    --bg-sidebar: #242526;
    --bg-toolbar: #242526;
    --text-primary: #e4e6eb;
    --text-secondary: #b0b3b8;
    --accent: #2d88ff;
    /* Vibrant Blue */
    --accent-hover: #1a73e8;
    --border: #3e4042;
    --shadow-soft: 0 4px 6px rgba(0, 0, 0, 0.3);
    --shadow-page: 0 8px 24px rgba(0, 0, 0, 0.4);

    /* Colors - Light Mode (Document Override) */
    --page-bg: #ffffff;
    --page-text: #1c1e21;

    /* Layout */
    --header-height: 56px;
    /* Increased from 48px for better spacing */
    --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-editor: 'Times New Roman', Times, serif;
    /* Default doc font */
    --font-mono: 'JetBrains Mono', 'Courier New', monospace;
}

[data-theme="light"] {
    --bg-workspace: #f0f2f5;
    --bg-sidebar: #ffffff;
    --bg-toolbar: #ffffff;
    --text-primary: #050505;
    --text-secondary: #65676b;
    --border: #dddfe2;
    --shadow-soft: 0 2px 4px rgba(0, 0, 0, 0.05);
    --shadow-page: 0 4px 12px rgba(0, 0, 0, 0.1);
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    /* Ensure no padding on body */
    font-family: var(--font-ui);
    background-color: var(--bg-workspace);
    color: var(--text-primary);
    overflow: hidden;
    /* App-like feel */
    height: 100vh;
    transition: background-color 0.3s ease, color 0.3s ease;
}

#app-container {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* CRITICAL: Prevent body scroll */
}

/* Scrollbars */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(128, 128, 128, 0.3);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(128, 128, 128, 0.5);
}

/* Header / Top Bar */
/* Header / Top Bar */
.app-header {
    width: 100%;
    height: auto;
    /* Allow sizing by content + padding */
    min-height: 80px;
    /* Increased to ensure space */
    background-color: var(--bg-toolbar);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    /* keep center but rely on height */
    justify-content: space-between;
    padding: 20px 20px 5px 20px;
    /* SIGNIFICANT top padding to push content down */
    z-index: 200;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    flex: 0 0 auto;
    margin: 0;
    box-sizing: border-box;
    position: relative;
    top: 0;
}

/* Force specific spacing if center alignment fails on specific browsers */
/* But first let's ensure icons don't blow up layout */
.brand .icon {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

.app-header * {
    margin-top: 0;
    margin-bottom: 0;
    line-height: normal;
    /* Reset line height to prevent vertical shift */
}

.brand {
    font-weight: 700;
    font-size: 1.3rem;
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.brand .icon {
    stroke-width: 2.5px;
}

.brand span {
    background: linear-gradient(135deg, var(--accent), #a855f7);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

/* Main Content Wrapper */
.main-wrapper {
    display: flex;
    flex: 1;
    min-height: 0;
    /* Critical: replaces fixed height to allow proper flex sizing */
    overflow: hidden;
    position: relative;
    background-color: var(--bg-workspace);
}

/* Find & Replace Bar */
.fr-bar {
    position: absolute;
    top: 70px;
    right: 20px;
    background: var(--bg-toolbar);
    border: 1px solid var(--border);
    padding: 8px;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    display: flex;
    gap: 8px;
    align-items: center;
    z-index: 2000;
}

.fr-group {
    display: flex;
    align-items: center;
    background: var(--bg-body);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0 8px;
}

.fr-input {
    border: none;
    background: transparent;
    padding: 6px;
    outline: none;
    color: var(--text-primary);
    width: 120px;
}

.fr-actions {
    display: flex;
    gap: 4px;
}

.fr-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    justify-content: center;
}

.fr-btn:hover {
    background-color: rgba(0, 0, 0, 0.05);
}

.fr-divider {
    width: 1px;
    height: 16px;
    background: var(--border);
    margin: 0 4px;
}

/* Utilities */
.hidden {
    display: none !important;
}

/* Ad Sidebar - HIDDEN FOR CLEAN LAUNCH */
.ad-sidebar {
    display: none !important;
}

@media (min-width: 1200px) {
    .ad-sidebar {
        display: none !important;
    }
}

.header-right {
    display: flex;
    align-items: center;
    gap: 20px;
}

/* Mode Toggle */
.mode-toggle {
    background: rgba(0, 0, 0, 0.05);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 3px;
    display: flex;
}

[data-theme="dark"] .mode-toggle {
    background: rgba(255, 255, 255, 0.05);
}

.mode-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.2s;
}

.mode-btn:hover {
    color: var(--text-primary);
}

.mode-btn.active {
    background-color: var(--bg-workspace);
    /* Use workspace bg for "tab" feel or accent? User likes accent usually. */
    background-color: var(--accent);
    color: white;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.header-actions {
    display: flex;
    gap: 10px;
    align-items: center;
    padding-left: 20px;
    border-left: 1px solid var(--border);
}

.action-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 8px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.action-btn:hover {
    background-color: rgba(125, 125, 125, 0.1);
    color: var(--text-primary);
}

/* Notepad Mode */
#notepad-mode {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    background-color: var(--bg-workspace);
    align-items: center;
    padding-top: 20px;
    height: 100%;
    overflow-y: auto;
    /* SCROLL HERE (Panel) */
}

.notepad-editor {
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
    padding: 40px;
    font-family: var(--font-mono);
    font-size: 1rem;
    line-height: 1.6;
    background-color: var(--page-bg);
    color: var(--page-text);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-page);
    resize: none;
    border-radius: 4px;
    border: none;
    outline: none;
    /* Auto-Grow Logic */
    min-height: calc(100vh - 100px);
    height: auto;
    overflow-y: hidden;
    /* No internal scrollbar */
    display: block;
}

/* Lined paper effect only if user wants? Modern style usually clean. */
/* Let's keep it clean for modern look, maybe adding line count in future */

[data-theme="dark"] .notepad-editor {
    background-color: #1e1e1e;
    /* Slightly lighter than workspace */
    color: #d4d4d4;
}

.header-stats {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-right: 15px;
    font-weight: 500;
}

.stat-sep {
    color: var(--border);
}

#save-indicator.saving {
    color: var(--accent);
}

/* Ribbon UI */
.ribbon-container {
    background-color: var(--bg-toolbar);
    border-bottom: 1px solid var(--border);
    user-select: none;
    flex-shrink: 0;
    /* FIXED TOP */
    z-index: 50;
    width: 100%;
    /* Ensure full width */
}

.ribbon-tabs {
    display: flex;
    padding-left: 10px;
    height: 32px;
    align-items: flex-end;
    background-color: var(--bg-toolbar);
    border-bottom: 1px solid var(--border);
}

.ribbon-content {
    height: 90px;
    display: flex;
    align-items: center;
    padding: 0 10px;
    overflow-x: visible;
    /* Fix: Allow content to breathe unless widely overflowing */
    /* Only scroll if absolutely necessary */
    width: 100%;
}

@media (max-width: 800px) {
    .ribbon-content {
        overflow-x: auto;
        /* Fallback for mobile */
    }
}

.ribbon-panel {
    display: none;
    width: 100%;
    height: 100%;
    align-items: center;
    justify-content: flex-start;
    /* Force left align */
    gap: 5px;
}

.ribbon-panel.active {
    display: flex;
}

.ribbon-group {
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 10px;
    border-right: 1px solid var(--border);
    height: 80%;
    gap: 4px;
    min-width: max-content;
}

.ribbon-group:last-child {
    border-right: none;
}

.ribbon-row {
    display: flex;
    gap: 4px;
}

.ribbon-btn {
    border: 1px solid transparent;
    background: transparent;
    border-radius: 4px;
    padding: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-primary);
    transition: all 0.1s;
}

.ribbon-btn:hover {
    background-color: rgba(128, 128, 128, 0.1);
}

.ribbon-btn.large {
    flex-direction: column;
    width: 50px;
    height: 60px;
    gap: 6px;
}

.group-label {
    text-align: center;
    font-size: 0.65rem;
    color: var(--text-secondary);
    margin-top: auto;
    width: 100%;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Main Wrapper Content */
.main-wrapper {
    flex: 1;
    /* Taking remaining height */
    display: flex;
    flex-direction: row;
    overflow: hidden;
    /* Do not scroll wrapper */
    position: relative;
    /* Height is effectively calculated by flex */
}

/* Document Mode Container */
#document-mode {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    /* Internal workspace handles scroll */
}

/* Document Workspace */
.doc-workspace {
    flex: 1;
    height: 100%;
    overflow-y: auto;
    /* SCROLL HERE */
    padding: 40px 20px;
    display: flex;
    flex-direction: column;
    /* VERTICAL STACK */
    align-items: center;
    /* CENTER HORIZONTALLY */
    background-color: var(--bg-workspace);
    margin: 0 auto;
    width: 100%;
    gap: 30px;
    /* Space between pages */
}

.doc-page {
    width: 210mm;
    /* Fixed A4 width */
    height: 297mm;
    /* FIXED HEIGHT for Pagination */
    min-height: 297mm;
    background-color: var(--page-bg);
    color: var(--page-text);
    box-shadow: var(--shadow-page);
    padding: 20mm 25.4mm;
    display: flex;
    flex-direction: column;
    outline: none;
    margin-bottom: 30px;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
    /* Hide overflow until moved */
}

/* Mobile Responsive Scaling for A4 */
@media (max-width: 850px) {
    .doc-workspace {
        padding: 20px 5px;
        align-items: flex-start;
        /* Allow scroll if needed, but we scale */
        overflow-x: hidden;
        /* Prevent horiz scroll if we scale */
    }

    .doc-page {
        /* Scale down to fit width */
        transform-origin: top center;
        transform: scale(calc((100vw - 20px) / 800));
        /* Approximate 210mm in px */
        margin-bottom: calc(-297mm * (1 - ((100vw - 20px) / 800)) + 30px);
        /* Compensate whitespace */
    }
}

.doc-content {
    flex: 1;
    overflow: hidden;
    /* Hide to prevent scrollbars on page */
    outline: none;
    min-height: 50px;
    /* Ensure editable area */
}

/* Footer Stats */
.app-footer {
    flex-shrink: 0;
    /* FIXED BOTTOM */
    height: 30px;
    background-color: var(--bg-toolbar);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 15px;
    font-size: 0.75rem;
    color: var(--text-secondary);
    z-index: 100;
}

.privacy-notice {
    display: none;
    /* Hide to prevent layout issues, user knows from footer */
}

/* Chrome-style Tab Bar */
.tab-bar {
    display: flex;
    align-items: flex-end;
    background-color: var(--bg-titlebar, #dfe1e5);
    padding: 4px 10px 0 10px;
    height: 34px;
    gap: 4px;
    flex-shrink: 0;
}

[data-theme="dark"] .tab-bar {
    background-color: #202124;
}

.tab-item {
    padding: 6px 12px;
    font-size: 0.8rem;
    border-radius: 8px 8px 0 0;
    background: transparent;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    max-width: 200px;
}

.tab-item.active {
    background: var(--bg-toolbar);
    color: var(--text-primary);
    box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.1);
}

.tab-add-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px;
    border-radius: 50%;
}

.tab-add-btn:hover {
    background: rgba(128, 128, 128, 0.2);
}

/* Responsive adjustments */
@media (max-width: 768px) {
    /* --- MOBILE OPTIMIZATION: MINIMAL UI --- */

    /* Hide Ribbon completely on mobile */
    .ribbon-bar {
        display: none !important;
    }

    /* Hide tabs on mobile */
    .tab-bar {
        display: none !important;
    }

    /* Hide ads on mobile */
    .ad-sidebar,
    .ad-slot {
        display: none !important;
    }

    /* Compact Header */
    .app-header {
        padding: 8px 10px;
        height: auto;
        min-height: 50px;
    }

    .brand {
        font-size: 0.9rem;
    }

    .brand span {
        display: inline;
        /* Keep brand name visible */
    }

    /* Hide stats on mobile */
    .header-stats {
        display: none !important;
    }

    /* Simplify header actions - show only essential buttons */
    .header-actions {
        gap: 8px;
    }

    /* Hide SEO button on mobile */
    #btn-seo-content {
        display: none !important;
    }

    /* Hide download/fullscreen on mobile */
    #btn-download,
    #btn-fullscreen {
        display: none !important;
    }

    /* Keep only theme toggle and undo/redo */
    .action-btn {
        padding: 6px;
    }

    .action-btn .icon {
        width: 18px;
        height: 18px;
    }

    /* Compact mode toggle */
    .mode-toggle {
        gap: 4px;
    }

    .mode-btn {
        padding: 6px 10px;
        font-size: 0.85rem;
    }

    .mode-btn .icon {
        width: 12px;
        height: 12px;
    }

    /* Main wrapper - full height */
    .main-wrapper {
        flex: 1;
    }

    /* Notepad - Full screen */
    #notepad-input {
        font-size: 16px;
        /* Prevents zoom on iOS */
        padding: 15px;
    }

    /* Document mode - responsive */
    .doc-workspace {
        padding: 10px 5px;
        align-items: flex-start;
        overflow-x: hidden;
    }

    .doc-page {
        /* Scale down to fit mobile */
        transform-origin: top center;
        transform: scale(calc((100vw - 20px) / 210mm));
        width: 210mm;
        margin-bottom: calc(-297mm * (1 - ((100vw - 20px) / 210mm)) + 20px);
        padding: 15mm 10mm;
        /* Reduced padding on mobile */
    }

    /* Hide privacy notice on mobile */
    .privacy-notice {
        display: none !important;
    }

    /* Voice widget - smaller and in corner */
    #voice-widget {
        width: 45px;
        height: 45px;
        bottom: 15px;
        right: 15px;
    }

    #voice-widget .icon {
        width: 20px;
        height: 20px;
    }

    /* Modals - Full screen on mobile */
    .modal-overlay {
        padding: 0;
    }

    .modal-content,
    .seo-content-wrapper {
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;
        width: 100%;
        height: 100%;
    }

    .modal-body,
    .seo-body {
        padding: 15px;
    }

    /* History modal - compact */
    .history-item {
        padding: 10px;
        font-size: 0.85rem;
    }
}

/* Extra small mobile (phones in portrait) */
@media (max-width: 480px) {
    .app-header {
        flex-wrap: wrap;
    }

    .brand {
        font-size: 0.85rem;
    }

    .mode-toggle {
        order: 3;
        width: 100%;
        margin-top: 5px;
        justify-content: center;
    }

    .mode-btn {
        flex: 1;
        max-width: 150px;
    }

    #notepad-input {
        font-size: 16px;
        line-height: 1.5;
    }
}

/* --- MODALS --- */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 5000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-content {
    background: var(--bg-workspace);
    color: var(--text-primary);
    width: 400px;
    max-width: 90%;
    border-radius: 8px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid var(--border);
}

.modal-header {
    background: var(--bg-toolbar);
    padding: 10px 15px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h3 {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
}

.modal-close {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
    max-height: 60vh;
    overflow-y: auto;
}

.modal-footer {
    padding: 15px;
    border-top: 1px solid var(--border);
    background: var(--bg-body);
    display: flex;
    justify-content: flex-end;
}

/* Form Elements in Modal */
.form-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
}

.form-group label {
    font-size: 0.9rem;
}

.form-group input[type="number"] {
    padding: 5px;
    border-radius: 4px;
    border: 1px solid var(--border);
    background: var(--bg-body);
    color: var(--text-primary);
    width: 60px;
}

.btn-primary {
    background: var(--accent);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 500;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

/* Symbol Grid */
.symbol-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 8px;
}

.symbol-btn {
    aspect-ratio: 1;
    background: var(--bg-body);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 1.2rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-primary);
}

.symbol-btn:hover {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/* Equation List */
.eq-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.eq-btn {
    text-align: left;
    background: var(--bg-body);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 10px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 4px;
    color: var(--text-primary);
}

.eq-btn:hover {
    border-color: var(--accent);
    background: rgba(45, 136, 255, 0.05);
    /* very light accent */
}

.eq-btn code {
    font-family: var(--font-mono);
    color: var(--accent);
    font-size: 0.9rem;
}

/* --- SEO CONTENT PANEL --- */
.seo-panel {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 6000;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
    padding: 20px;
}

.seo-panel.hidden {
    display: none;
}

.seo-content-wrapper {
    background: var(--bg-body);
    border-radius: 12px;
    max-width: 900px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.seo-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 30px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--bg-body);
    z-index: 10;
}

.seo-header h2 {
    margin: 0;
    color: var(--text-primary);
    font-size: 1.5rem;
}

.seo-body {
    padding: 30px;
    color: var(--text-primary);
    line-height: 1.8;
}

.seo-body section {
    margin-bottom: 30px;
}

.seo-body h3 {
    color: var(--accent);
    margin-top: 0;
    margin-bottom: 15px;
    font-size: 1.3rem;
}

.seo-body p {
    margin-bottom: 15px;
}

.seo-body ul {
    padding-left: 25px;
}

.seo-body li {
    margin-bottom: 10px;
}

.seo-body strong {
    color: var(--text-primary);
    font-weight: 600;
}

/* SEO Panel - Light Theme Override */
.seo-content-wrapper {
    background: #ffffff;
    color: #1a1a1a;
}

.seo-header {
    background: #ffffff;
    border-bottom: 1px solid #e5e5e5;
}

.seo-header h2 {
    color: #1a1a1a;
}

.seo-body {
    color: #333333;
}

.seo-body h3 {
    color: #2d88ff;
}

.seo-body strong {
    color: #1a1a1a;
}