/* ========================================
   Document Editor - Stylesheet
   ======================================== */

:root {
    /* Colors */
    --bg-primary: #f0f2f5;
    --bg-page: #ffffff;
    --bg-toolbar: #ffffff;
    --text-primary: #1a1a2e;
    --text-secondary: #64748b;
    --border-color: #e2e8f0;
    --accent-color: #3b82f6;
    --accent-hover: #2563eb;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);

    /* A4 Dimensions */
    --page-width: 794px;
    --page-height: 1123px;
    --page-margin: 72px;
    /* ~1 inch margins */
    --page-gap: 40px;

    /* Typography */
    --font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

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

body {
    font-family: var(--font-family);
    background: var(--bg-primary);
    min-height: 100vh;
    color: var(--text-primary);
    overflow-x: hidden;
}

/* ========================================
   Menu Bar
   ======================================== */

.menu-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 32px;
    background: var(--bg-toolbar);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 8px;
    z-index: 1001;
    font-size: 13px;
}

.menu-logo {
    height: 40px;
    max-height: 100%;
    width: auto;
    border-radius: 6px;
    margin-right: 12px;
    object-fit: contain;
}

.menu-item {
    position: relative;
    height: 100%;
    display: flex;
    align-items: center;
}

.menu-title {
    padding: 4px 12px;
    cursor: pointer;
    border-radius: 4px;
    transition: background-color 0.15s;
    user-select: none;
}

.menu-title:hover,
.menu-item:hover>.menu-title {
    background-color: rgba(0, 0, 0, 0.08);
}

.menu-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    min-width: 220px;
    background: var(--bg-page);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    padding: 4px 0;
    z-index: 1002;
}

.menu-item:hover>.menu-dropdown {
    display: block;
}

.menu-action {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 8px 12px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 13px;
    text-align: left;
    gap: 10px;
    transition: background-color 0.15s;
    color: var(--text-primary);
}

.menu-action:hover {
    background-color: var(--accent-color);
    color: white;
}

.menu-action:hover .menu-shortcut {
    color: rgba(255, 255, 255, 0.8);
}

.menu-icon {
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
}

.menu-icon img {
    max-width: 16px;
    max-height: 16px;
    object-fit: contain;
}

.menu-label {
    flex: 1;
}

.menu-shortcut {
    color: var(--text-secondary);
    font-size: 12px;
    margin-left: auto;
}

.menu-arrow {
    font-size: 10px;
    color: var(--text-secondary);
}

.menu-divider {
    height: 1px;
    background: var(--border-color);
    margin: 4px 0;
}

/* Submenu */
.menu-submenu {
    position: relative;
}

.menu-submenu>.menu-dropdown.submenu {
    display: none;
    position: absolute;
    left: 100%;
    top: -4px;
}

.menu-submenu:hover>.menu-dropdown.submenu {
    display: block;
}

.menu-has-submenu {
    padding-right: 8px;
}

/* ========================================
   Toolbar Styles
   ======================================== */

.toolbar {
    position: fixed;
    top: 32px;
    left: 0;
    right: 0;
    height: 56px;
    background: var(--bg-toolbar);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 8px;
    z-index: 1000;
    box-shadow: var(--shadow-sm);
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 4px;
}

.toolbar-divider {
    width: 1px;
    height: 20px;
    background: rgba(0, 0, 0, 0.1);
    margin: 0 4px;
}

.tool-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-width: 36px;
    height: 36px;
    padding: 0 10px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.15s ease;
    font-family: var(--font-family);
    font-size: 14px;
    font-weight: 500;
}

.tool-btn:hover {
    background: var(--bg-primary);
}

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

.tool-btn.active {
    background: #e0e7ff;
    color: var(--accent-color);
}

.tool-btn-primary {
    background: var(--accent-color);
    color: white;
    padding: 0 16px;
}

.tool-btn-primary:hover {
    background: var(--accent-hover);
}

.tool-select {
    height: 36px;
    padding: 0 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-page);
    color: var(--text-primary);
    font-family: var(--font-family);
    font-size: 13px;
    cursor: pointer;
    outline: none;
    transition: border-color 0.15s ease;
}

.tool-select:hover {
    border-color: var(--accent-color);
}

.tool-select:focus {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

/* Color Picker Buttons */
.color-picker-wrapper {
    position: relative;
}

.color-btn {
    flex-direction: column;
    padding: 4px 8px;
    gap: 2px;
}

.color-icon {
    font-weight: bold;
    font-size: 14px;
}

.highlight-icon {
    font-size: 14px;
}

.color-bar {
    width: 16px;
    height: 3px;
    border-radius: 1px;
}

/* Font family dropdown - wider to accommodate font names */
.font-select {
    width: 150px;
    min-width: 150px;
}

.font-select option {
    padding: 8px 12px;
}

.font-select optgroup {
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 11px;
}

/* Toolbar label for paragraph symbol */
.toolbar-label {
    font-size: 14px;
    color: var(--text-secondary);
    padding: 0 4px;
}

/* Smaller toolbar select for space controls */
.tool-select-small {
    width: 50px !important;
    min-width: 50px !important;
    padding: 4px 2px !important;
    font-size: 11px;
}

.tool-select-compact {
    width: 55px !important;
    min-width: 55px !important;
}

/* Paragraph Spacing Popup */
.para-spacing-group {
    position: relative;
}

.para-spacing-popup {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    background: var(--bg-page);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 12px;
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    min-width: 140px;
}

.para-spacing-popup.show {
    display: block;
}

.popup-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
}

.popup-row:last-child {
    margin-bottom: 0;
}

.popup-row label {
    font-size: 12px;
    color: var(--text-primary);
}

.popup-row select {
    padding: 4px 8px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 12px;
    background: var(--bg-primary);
}

/* Insert Popup */
.insert-popup-group {
    position: relative;
}

.insert-popup {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    background: var(--bg-page);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 6px;
    box-shadow: var(--shadow-lg);
    z-index: 1000;
    min-width: 100px;
}

.insert-popup.show {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.insert-popup-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 4px;
    font-size: 13px;
    color: var(--text-primary);
    transition: background 0.15s;
}

.insert-popup-btn:hover {
    background: var(--bg-primary);
}

.insert-popup-btn svg {
    flex-shrink: 0;
}

/* ========================================
   Font Status Indicator
   ======================================== */

.font-status-indicator {
    position: fixed;
    bottom: 20px;
    left: 20px;
    background: var(--bg-page);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 8px 16px;
    box-shadow: var(--shadow-md);
    font-size: 13px;
    z-index: 1000;
    display: flex;
    align-items: center;
    gap: 8px;
}

.font-status-label {
    color: var(--text-secondary);
    font-weight: 500;
}

.font-status-name {
    color: var(--text-primary);
    font-weight: 600;
}

/* Zoom Indicator */
.zoom-indicator {
    position: fixed;
    bottom: 20px;
    right: 140px;
    background: var(--bg-page);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 4px 8px;
    box-shadow: var(--shadow-md);
    font-size: 13px;
    z-index: 1000;
    display: flex;
    align-items: center;
    gap: 4px;
}

.zoom-btn {
    width: 24px;
    height: 24px;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 4px;
    font-size: 16px;
    font-weight: bold;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.zoom-btn:hover {
    background: var(--accent-color);
    color: white;
}

.zoom-level {
    min-width: 45px;
    text-align: center;
    font-weight: 600;
    color: var(--text-primary);
}

/* ========================================
   Editor Container
   ======================================== */

.editor-container {
    padding-top: 112px;
    padding-bottom: 60px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--page-gap);
    min-height: 100vh;
}

/* ========================================
   A4 Page Styles
   ======================================== */

.page {
    width: var(--page-width);
    height: var(--page-height);
    background: var(--bg-page);
    box-shadow: var(--shadow-lg);
    padding: var(--page-margin);
    outline: none;
    position: relative;
    overflow: hidden;
    overflow-y: clip;
    /* Modern browsers - hard clip without scrollbar */
    line-height: 1.5;
    /* University assignment default */
    font-size: 12pt;
    /* University standard */
    flex-shrink: 0;
    /* Ensure content can't visually escape */
    contain: content;
}

/* Line height inheritance for all content */
.page p,
.page h1,
.page h2,
.page h3,
.page li,
.page div:not(.toc-container):not(.lof-container):not(.lot-container):not(.figure-container):not(.table-container) {
    line-height: inherit;
}

.page:focus {
    box-shadow: var(--shadow-lg), 0 0 0 2px var(--accent-color);
}

/* Page number indicator */
.page::after {
    content: attr(data-page-number);
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 12px;
    color: var(--text-secondary);
}

/* ========================================
   Header & Footer Regions
   ======================================== */

.page-header,
.page-footer {
    position: absolute;
    left: var(--page-margin);
    right: var(--page-margin);
    font-size: 10pt;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    pointer-events: none;
    z-index: 5;
}

.page-header {
    top: 24px;
    height: 40px;
    border-bottom: 1px solid transparent;
}

.page-footer {
    bottom: 24px;
    height: 40px;
    border-top: 1px solid transparent;
}

.page-header.has-content,
.page-footer.has-content {
    border-color: #e5e5e5;
}

/* Header/Footer alignment sections */
.hf-left,
.hf-center,
.hf-right {
    flex: 1;
    padding: 4px 8px;
}

.hf-left {
    text-align: left;
}

.hf-center {
    text-align: center;
}

.hf-right {
    text-align: right;
}

/* Hide default page number when footer is active */
.page.has-footer::after {
    display: none;
}

/* ========================================
   Header & Footer Style Variations
   ======================================== */

/* Style 1: Classic (Default) - Plain, no borders */
.page.hf-style-classic .page-header,
.page.hf-style-classic .page-footer {
    border-color: transparent;
    background: transparent;
}

/* Style 2: Minimal Line - Gray borders */
.page.hf-style-minimal .page-header.has-content {
    border-bottom: 1px solid #666;
}

.page.hf-style-minimal .page-footer.has-content {
    border-top: 1px solid #666;
}

/* Style 3: Word-style Blue Line */
.page.hf-style-blue .page-header.has-content {
    border-bottom: 1px solid #4472C4;
}

.page.hf-style-blue .page-footer.has-content {
    border-top: 1px solid #4472C4;
}

/* Style 4: Soft Accent - Light background band */
.page.hf-style-accent .page-header.has-content {
    background: #f8f9fa;
    border-bottom: none;
}

.page.hf-style-accent .page-footer.has-content {
    background: #f8f9fa;
    border-top: none;
}

/* Style 5: Dark Accent - Dark background with white text */
.page.hf-style-dark .page-header.has-content {
    background: #2d3748;
    border-bottom: none;
    color: #ffffff;
}

.page.hf-style-dark .page-footer.has-content {
    background: #2d3748;
    border-top: none;
    color: #ffffff;
}

/* Style 6: Dark Blue Accent - Blue background with white text */
.page.hf-style-darkblue .page-header.has-content {
    background: #4472C4;
    border-bottom: none;
    color: #ffffff;
}

.page.hf-style-darkblue .page-footer.has-content {
    background: #4472C4;
    border-top: none;
    color: #ffffff;
}

/* ========================================
   Typography within Pages
   ======================================== */

.page h1 {
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 16px;
    line-height: 1.3;
}

.page h2 {
    font-size: 22px;
    font-weight: 600;
    margin-bottom: 14px;
    line-height: 1.35;
}

.page h3 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 12px;
    line-height: 1.4;
}

.page p {
    margin-top: 0;
    margin-bottom: 6pt;
    /* Default: 6pt after (university standard) */
}

.page ul,
.page ol {
    margin-left: 24px;
    margin-bottom: 12px;
}

.page li {
    margin-bottom: 4px;
}

/* ========================================
   Tables within Pages
   ======================================== */

.page table {
    width: 100%;
    border-collapse: collapse;
    margin: 16px 0;
}

.page th,
.page td {
    border: 1px solid var(--border-color);
    padding: 10px 14px;
    text-align: left;
}

.page th {
    background: var(--bg-primary);
    font-weight: 600;
}

.page td {
    min-width: 80px;
}

/* ========================================
   Images within Pages
   ======================================== */

/* Image wrapper for resize functionality */
.image-wrapper {
    position: relative;
    display: inline-block;
    margin: 12px 0;
    max-width: 100%;
    cursor: pointer;
    border-radius: 4px;
    transition: box-shadow 0.15s ease;
}

.image-wrapper:hover {
    box-shadow: 0 0 0 2px var(--accent-color);
}

.image-wrapper.selected {
    box-shadow: 0 0 0 2px var(--accent-color);
}

.editor-image {
    display: block;
    max-width: 100%;
    height: auto;
    border-radius: 4px;
    pointer-events: none;
}

.tool-btn svg,
.tool-btn i,
.tool-btn img {
    margin: 0;
    pointer-events: none;
    display: block;
}

.tool-btn img {
    width: 20px;
    height: 20px;
    object-fit: contain;
}

/* Resize handles - positioned at corners */
.resize-handle {
    position: absolute;
    width: 10px;
    height: 10px;
    background: var(--accent-color);
    border: 2px solid white;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    z-index: 10;
}

.resize-handle-nw {
    top: -5px;
    left: -5px;
    cursor: nwse-resize;
}

.resize-handle-ne {
    top: -5px;
    right: -5px;
    cursor: nesw-resize;
}

.resize-handle-sw {
    bottom: -5px;
    left: -5px;
    cursor: nesw-resize;
}

.resize-handle-se {
    bottom: -5px;
    right: -5px;
    cursor: nwse-resize;
}

/* Legacy image styles (for backwards compatibility) */
.page img:not(.editor-image):not(.figure-image) {
    max-width: 100%;
    height: auto;
    margin: 12px 0;
    border-radius: 4px;
}

/* ========================================
   Figures with Captions
   ======================================== */

.figure-container {
    display: block;
    text-align: center;
    margin: 16px 0;
    padding: 0;
    background: transparent;
}

/* Selected image highlight - subtle */
img.image-selected {
    box-shadow: 0 0 0 2px var(--accent-color);
}

.figure-image {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
}

.figure-caption {
    display: block;
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-secondary);
    font-style: italic;
    text-align: center;
}

.figure-number {
    font-weight: 600;
    font-style: normal;
    color: var(--text-primary);
}

/* ========================================
   List of Figures
   ======================================== */

.lof-container {
    position: relative;
    margin: 20px 0;
    padding: 16px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: #fafafa;
    font-size: 13px;
}

.lof-title {
    font-size: 14px;
    font-weight: bold;
    margin-bottom: 12px;
    padding-bottom: 6px;
    border-bottom: 2px solid var(--accent-color);
}

.lof-entries {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.lof-entry {
    display: flex;
    align-items: baseline;
    cursor: pointer;
    padding: 3px 6px;
    border-radius: 4px;
    font-size: 12px;
    transition: background-color 0.15s;
}

.lof-entry:hover {
    background-color: rgba(59, 130, 246, 0.1);
}

.lof-text {
    flex-shrink: 0;
}

.lof-leader {
    flex: 1;
    border-bottom: 1px dotted var(--text-secondary);
    margin: 0 6px;
    min-width: 15px;
    height: 0.6em;
}

.lof-page {
    flex-shrink: 0;
    font-weight: 500;
    color: var(--text-secondary);
    font-size: 11px;
}

/* LOF Delete Button */
.lof-delete-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    border: none;
    border-radius: 6px;
    background: #fee2e2;
    color: #dc2626;
    cursor: pointer;
    font-size: 16px;
    display: none;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.lof-delete-btn:hover {
    background: #dc2626;
    color: white;
}

.lof-container.lof-selected .lof-delete-btn {
    display: flex;
}

.lof-container.lof-selected {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

/* ========================================
   Tables with Captions
   ======================================== */

.table-container {
    display: block;
    text-align: center;
    margin: 16px 0;
}

table.table-selected {
    box-shadow: 0 0 0 2px var(--accent-color);
}

.table-caption {
    display: block;
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-secondary);
    font-style: italic;
    text-align: center;
}

.table-number {
    font-weight: 600;
    font-style: normal;
    color: var(--text-primary);
}

/* ========================================
   List of Tables
   ======================================== */

.lot-container {
    position: relative;
    margin: 20px 0;
    padding: 16px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: #fafafa;
    font-size: 13px;
}

.lot-title {
    font-size: 14px;
    font-weight: bold;
    margin-bottom: 12px;
    padding-bottom: 6px;
    border-bottom: 2px solid var(--accent-color);
}

.lot-entries {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.lot-entry {
    display: flex;
    align-items: baseline;
    cursor: pointer;
    padding: 3px 6px;
    border-radius: 4px;
    font-size: 12px;
    transition: background-color 0.15s;
}

.lot-entry:hover {
    background-color: rgba(59, 130, 246, 0.1);
}

.lot-text {
    flex-shrink: 0;
}

.lot-leader {
    flex: 1;
    border-bottom: 1px dotted var(--text-secondary);
    margin: 0 6px;
    min-width: 15px;
    height: 0.6em;
}

.lot-page {
    flex-shrink: 0;
    font-weight: 500;
    color: var(--text-secondary);
    font-size: 11px;
}

.lot-delete-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    border: none;
    border-radius: 6px;
    background: #fee2e2;
    color: #dc2626;
    cursor: pointer;
    font-size: 16px;
    display: none;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.lot-delete-btn:hover {
    background: #dc2626;
    color: white;
}

.lot-container.lot-selected .lot-delete-btn {
    display: flex;
}

.lot-container.lot-selected {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

/* ========================================
   Table of Contents
   ======================================== */

.toc-container {
    position: relative;
    margin: 20px 0;
    padding: 16px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: #fafafa;
    font-size: 13px;
}

.toc-title {
    font-size: 14px;
    font-weight: bold;
    margin-bottom: 12px;
    padding-bottom: 6px;
    border-bottom: 2px solid var(--accent-color);
}

.toc-entries {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.toc-entry {
    display: flex;
    align-items: baseline;
    cursor: pointer;
    padding: 3px 6px;
    border-radius: 4px;
    font-size: 12px;
    transition: background-color 0.15s;
}

.toc-entry:hover {
    background-color: rgba(59, 130, 246, 0.1);
}

.toc-entry.toc-h1 {
    padding-left: 0;
    font-weight: 600;
    font-size: 13px;
}

.toc-entry.toc-h2 {
    padding-left: 16px;
    font-size: 12px;
}

.toc-entry.toc-h3 {
    padding-left: 32px;
    font-size: 11px;
    color: var(--text-secondary);
}

.toc-text {
    flex-shrink: 0;
}

.toc-leader {
    flex: 1;
    border-bottom: 1px dotted var(--text-secondary);
    margin: 0 6px;
    min-width: 15px;
    height: 0.6em;
}

.toc-page {
    flex-shrink: 0;
    font-weight: 500;
    color: var(--text-secondary);
    font-size: 11px;
}

/* TOC Delete Button */
.toc-delete-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    border: none;
    border-radius: 6px;
    background: #fee2e2;
    color: #dc2626;
    cursor: pointer;
    font-size: 16px;
    display: none;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.toc-delete-btn:hover {
    background: #dc2626;
    color: white;
}

.toc-container.toc-selected .toc-delete-btn {
    display: flex;
}

.toc-container.toc-selected {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

/* ========================================
   Page Indicator (Footer)
   ======================================== */

.page-indicator {
    position: fixed;
    bottom: 16px;
    right: 24px;
    padding: 8px 16px;
    background: var(--bg-toolbar);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    font-size: 13px;
    color: var(--text-secondary);
    box-shadow: var(--shadow-md);
    z-index: 100;
}

/* ========================================
   Print Styles (for PDF Export)
   ======================================== */

@media print {
    body {
        background: white;
    }

    .toolbar,
    .page-indicator {
        display: none !important;
    }

    .editor-container {
        padding: 0;
        gap: 0;
    }

    .page {
        box-shadow: none;
        page-break-after: always;
        margin: 0;
        width: 210mm;
        min-height: 297mm;
        height: 297mm;
    }

    .page:last-child {
        page-break-after: auto;
    }

    .page::after {
        display: none;
    }
}

@page {
    size: A4;
    margin: 0;
}

/* ========================================
   Scrollbar Styling
   ======================================== */

::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

::-webkit-scrollbar-track {
    background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 5px;
}

::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* ========================================
   Selection Styling
   ======================================== */

::selection {
    background: rgba(59, 130, 246, 0.25);
}

/* ========================================
   Animations
   ======================================== */

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

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.page {
    animation: fadeIn 0.3s ease-out;
}

/* ========================================
   Modal Styles
   ======================================== */

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.modal {
    background: var(--bg-page);
    border-radius: 12px;
    box-shadow: var(--shadow-lg);
    width: 400px;
    max-width: 90vw;
    animation: fadeIn 0.2s ease-out;
}

.modal.modal-small {
    width: 320px;
}

.input-hint {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 8px;
    margin-bottom: 0;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.modal-close {
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    font-size: 20px;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover {
    background: var(--bg-primary);
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
}

/* Form elements */
.table-size-inputs {
    display: flex;
    gap: 16px;
    margin-bottom: 16px;
}

.input-group {
    flex: 1;
}

.input-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.input-group input {
    width: 100%;
    height: 40px;
    padding: 0 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 14px;
    font-family: var(--font-family);
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.input-group input:focus {
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.table-options {
    margin-bottom: 16px;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--accent-color);
}

.table-preview {
    background: var(--bg-primary);
    border-radius: 8px;
    padding: 12px;
    min-height: 80px;
    overflow: auto;
}

.table-preview table {
    border-collapse: collapse;
    font-size: 11px;
}

.table-preview td,
.table-preview th {
    border: 1px solid var(--border-color);
    padding: 4px 8px;
    min-width: 40px;
    text-align: center;
}

.table-preview th {
    background: #e2e8f0;
    font-weight: 600;
}

/* Buttons */
.btn {
    padding: 10px 18px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    font-family: var(--font-family);
    cursor: pointer;
    border: none;
    transition: all 0.15s ease;
}

.btn-primary {
    background: var(--accent-color);
    color: white;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background: #e2e8f0;
}

/* ========================================
   Context Menu
   ======================================== */

.context-menu {
    position: fixed;
    background: var(--bg-page);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-shadow: var(--shadow-lg);
    min-width: 200px;
    z-index: 3000;
    padding: 6px 0;
    animation: fadeIn 0.15s ease-out;
}

.context-menu-section {
    padding: 4px 0;
}

.context-menu-item {
    padding: 8px 14px;
    font-size: 13px;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.1s;
}

.context-menu-item:hover {
    background: var(--bg-primary);
}

.context-menu-item-small {
    padding: 8px 12px;
    flex: 1;
    justify-content: center;
    font-size: 16px;
}

.context-menu-item-danger {
    color: #dc2626;
}

.context-menu-item-danger:hover {
    background: #fef2f2;
}

.context-menu-icon {
    width: 18px;
    text-align: center;
    font-size: 14px;
}

.context-menu-divider {
    height: 1px;
    background: var(--border-color);
    margin: 4px 0;
}

.context-menu-label {
    padding: 6px 14px 4px;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.context-menu-row {
    display: flex;
    padding: 0 6px;
}

/* Submenu styles */
.context-menu-has-submenu {
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.submenu-arrow {
    font-size: 10px;
    color: var(--text-secondary);
    margin-left: auto;
}

.context-submenu {
    display: none;
    position: absolute;
    left: 100%;
    top: -6px;
    background: var(--bg-page);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    min-width: 150px;
    padding: 6px 0;
    z-index: 3001;
}

.context-menu-has-submenu:hover>.context-submenu {
    display: block;
}

.context-submenu .context-menu-item {
    padding: 6px 12px;
    font-size: 13px;
}

.context-submenu .context-menu-label {
    padding: 4px 12px 2px;
    font-size: 10px;
}

/* ========================================
   Academic Table Styles
   ======================================== */

.page table {
    width: 100%;
    border-collapse: collapse;
    margin: 16px 0;
    font-size: 14px;
    /* Prevent table-level selection from escaping */
    user-select: none;
}

/* Table alignment classes */
.page table.table-align-left {
    margin-left: 0;
    margin-right: auto;
}

.page table.table-align-center {
    margin-left: auto;
    margin-right: auto;
}

.page table.table-align-right {
    margin-left: auto;
    margin-right: 0;
}

.page th,
.page td {
    border: 1px solid #000;
    padding: 8px 12px;
    text-align: left;
    vertical-align: top;
    min-height: 24px;
    /* CRITICAL: Contain selection within cell to prevent cross-cell selection bug */
    -webkit-user-select: text;
    -moz-user-select: text;
    user-select: text;
    /* Make cell an isolated editing context */
    -webkit-user-modify: read-write-plaintext-only;
    outline: none;
}

/* Make cells accept contenteditable properly */
.page th[contenteditable="true"],
.page td[contenteditable="true"] {
    outline: none;
    /* Extra isolation for contenteditable cells */
    -webkit-user-select: text;
    user-select: text;
}

.page th:focus,
.page td:focus {
    outline: 2px solid var(--accent-color);
    outline-offset: -2px;
}

.page th {
    background: #f5f5f5;
    font-weight: 600;
}

.page td {
    min-width: 60px;
}

/* Table header row styling */
.page tr.table-header-row th,
.page tr.table-header-row td {
    background: #f5f5f5;
    font-weight: 600;
}

/* Cell alignment classes - with high specificity to override defaults */
.page th.cell-align-left,
.page td.cell-align-left {
    text-align: left !important;
}

.page th.cell-align-center,
.page td.cell-align-center {
    text-align: center !important;
}

.page th.cell-align-right,
.page td.cell-align-right {
    text-align: right !important;
}

.page th.cell-valign-top,
.page td.cell-valign-top {
    vertical-align: top !important;
}

.page th.cell-valign-middle,
.page td.cell-valign-middle {
    vertical-align: middle !important;
}

.page th.cell-valign-bottom,
.page td.cell-valign-bottom {
    vertical-align: bottom !important;
}

/* Table selection highlight */
.page table:focus-within {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
}

/* Column resize cursor */
.page th,
.page td {
    position: relative;
}

.col-resize-handle {
    position: absolute;
    right: -2px;
    top: 0;
    bottom: 0;
    width: 4px;
    cursor: col-resize;
    background: transparent;
    z-index: 1;
}

.col-resize-handle:hover,
.col-resize-handle.active {
    background: var(--accent-color);
}

/* Continued table indicator */
.page table[data-table-continued="true"] {
    margin-top: 0;
}

.page table[data-table-continued="true"]::before {
    content: "(continued)";
    display: block;
    font-size: 11px;
    color: #666;
    font-style: italic;
    margin-bottom: 4px;
}

/* ========================================
   Toast Notifications
   ======================================== */

.toast-notification {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: #1a1a2e;
    color: white;
    padding: 12px 24px;
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    z-index: 5000;
    font-size: 14px;
    opacity: 0;
    transition: all 0.3s ease;
}

.toast-notification.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast-notification.success {
    background: #059669;
}

.toast-notification.error {
    background: #dc2626;
}

/* ========================================
   Preset List Styling
   ======================================== */

.preset-list {
    max-height: 250px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    margin-top: 8px;
}

.preset-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-color);
    transition: background 0.15s;
}

.preset-item:last-child {
    border-bottom: none;
}

.preset-item:hover {
    background: var(--bg-primary);
}

.preset-item-name {
    font-weight: 500;
}

.preset-item-details {
    font-size: 11px;
    color: var(--text-secondary);
}

.preset-item-actions {
    display: flex;
    gap: 4px;
}

.preset-item-btn {
    padding: 4px 8px;
    font-size: 12px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.15s;
}

.preset-item-btn.apply {
    background: var(--accent-color);
    color: white;
}

.preset-item-btn.apply:hover {
    background: var(--accent-hover);
}

.preset-item-btn.delete {
    background: #fee2e2;
    color: #dc2626;
}

.preset-item-btn.delete:hover {
    background: #dc2626;
    color: white;
}

.preset-system-badge {
    font-size: 9px;
    background: #e0e7ff;
    color: var(--accent-color);
    padding: 2px 6px;
    border-radius: 10px;
    margin-left: 8px;
}

/* ========================================
   RESPONSIVE STYLES - TABLET
   ======================================== */

@media screen and (max-width: 1024px) {
    :root {
        --page-margin: 48px;
        --page-gap: 24px;
    }

    .toolbar {
        padding: 0 12px;
        gap: 4px;
    }

    .tool-select {
        font-size: 12px;
        padding: 0 8px;
    }

    .font-select {
        width: 120px;
        min-width: 120px;
    }

    .zoom-indicator {
        right: 20px;
    }

    .font-status-indicator {
        display: none;
    }
}

/* ========================================
   RESPONSIVE STYLES - MOBILE
   Mobile-first: touch-friendly, scrollable toolbar
   ======================================== */

@media screen and (max-width: 768px) {
    :root {
        --page-margin: 24px;
        --page-gap: 20px;
    }

    /* Menu Bar - Mobile */
    .menu-bar {
        height: 44px;
        padding: 0 8px;
    }

    .menu-logo {
        height: 32px;
        margin-right: 8px;
    }

    .menu-title {
        padding: 8px 10px;
        font-size: 14px;
    }

    .menu-dropdown {
        position: fixed;
        left: 8px !important;
        right: 8px !important;
        top: 44px !important;
        width: auto;
        max-height: 70vh;
        overflow-y: auto;
    }

    .menu-action {
        padding: 12px 16px;
        font-size: 14px;
    }

    .menu-shortcut {
        display: none;
    }

    /* Toolbar - Mobile: Horizontal scrollable - SINGLE ROW */
    .toolbar {
        top: 44px;
        height: 52px;
        padding: 0 8px;
        gap: 4px;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        /* CRITICAL: Prevent wrapping - single row only */
        flex-wrap: nowrap !important;
        white-space: nowrap;
        /* Hide scrollbar but keep scrollable */
        scrollbar-width: none;
        /* Firefox */
        -ms-overflow-style: none;
        /* IE/Edge */
    }

    /* Hide scrollbar for Chrome, Safari, Opera */
    .toolbar::-webkit-scrollbar {
        display: none;
    }

    /* REORDER TOOLBAR ON MOBILE using specific IDs */
    /* Mobile order: Format(BIU) → Align → Lists → Insert → Export → Undo → Font → Para */

    #tg-format {
        order: 1 !important;
    }

    /* B/I/U/Colors → FIRST */
    #tg-align {
        order: 2 !important;
    }

    /* Alignment → SECOND */
    #tg-undo {
        order: 3 !important;
    }

    /* Undo/Redo → THIRD (next to align) */
    #tg-lists {
        order: 4 !important;
    }

    /* Lists → fourth */
    #tg-insert {
        order: 5 !important;
    }

    /* Insert → fifth */
    #tg-export {
        order: 6 !important;
    }

    /* Export → sixth */
    #tg-font {
        order: 7 !important;
    }

    /* Font/Heading → END */
    #tg-para {
        order: 8 !important;
    }

    /* Para spacing → END */

    /* Dividers - move to end on mobile */
    .toolbar>.toolbar-divider {
        order: 10 !important;
    }

    /* After Insert */

    .toolbar-group {
        flex-shrink: 0;
        flex-wrap: nowrap;
        gap: 2px;
    }

    .toolbar-divider {
        margin: 0 2px;
        height: 24px;
    }

    .tool-btn {
        min-width: 40px;
        height: 40px;
        padding: 0 8px;
        border-radius: 6px;
        font-size: 16px;
    }

    .tool-btn svg {
        width: 18px;
        height: 18px;
    }

    .tool-select {
        height: 40px;
        padding: 0 8px;
        font-size: 13px;
        min-width: 70px;
    }

    .font-select {
        width: 100px;
        min-width: 100px;
    }

    .tool-select-small {
        width: 45px !important;
        min-width: 45px !important;
    }

    .tool-select-compact {
        width: 50px !important;
        min-width: 50px !important;
    }

    /* Hide some toolbar items on mobile to reduce clutter */
    .toolbar-label {
        display: none;
    }

    /* Editor Container - Mobile */
    .editor-container {
        padding-top: 108px;
        /* menu (44px) + toolbar (52px) + gap (12px) */
        padding-bottom: 80px;
        padding-left: 8px;
        padding-right: 8px;
    }

    /* A4 Page - Mobile: Scale down to fit */
    .page {
        width: 100%;
        max-width: 100%;
        min-height: auto;
        height: auto;
        min-height: 600px;
        padding: var(--page-margin);
        font-size: 14px;
        /* Ensure proper line height for readability */
        line-height: 1.6;
    }

    /* Page number on mobile */
    .page::after {
        bottom: 12px;
        font-size: 11px;
    }

    /* Typography adjustments for mobile */
    .page h1 {
        font-size: 22px;
        margin-bottom: 12px;
    }

    .page h2 {
        font-size: 18px;
        margin-bottom: 10px;
    }

    .page h3 {
        font-size: 16px;
        margin-bottom: 8px;
    }

    .page ul,
    .page ol {
        margin-left: 20px;
    }

    /* Tables on mobile - horizontal scroll */
    .page table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .page th,
    .page td {
        padding: 8px 10px;
        font-size: 13px;
        white-space: nowrap;
    }

    /* Images on mobile */
    .image-wrapper {
        max-width: 100%;
    }

    .editor-image {
        max-width: 100%;
    }

    /* Hide resize handles on touch */
    .resize-handle {
        display: none;
    }

    /* Zoom indicator - Mobile */
    .zoom-indicator {
        bottom: 16px;
        right: 16px;
        padding: 6px 10px;
        font-size: 12px;
    }

    .zoom-btn {
        width: 28px;
        height: 28px;
    }

    /* Font status - Hide on mobile */
    .font-status-indicator {
        display: none;
    }

    /* Modals - Mobile fullscreen */
    .modal-content {
        max-width: 100% !important;
        width: 100% !important;
        margin: 0 !important;
        max-height: 100vh !important;
        border-radius: 0 !important;
    }

    .modal-body {
        max-height: calc(100vh - 120px);
        overflow-y: auto;
    }

    /* Toast on mobile */
    .toast-notification {
        left: 16px;
        right: 16px;
        transform: translateX(0) translateY(100px);
        text-align: center;
    }

    .toast-notification.show {
        transform: translateX(0) translateY(0);
    }

    /* Popups on mobile */
    .para-spacing-popup,
    .insert-popup {
        position: fixed;
        left: 16px !important;
        right: 16px !important;
        top: auto !important;
        bottom: 70px !important;
        width: auto;
        max-height: 50vh;
        overflow-y: auto;
    }

    /* Context menu on mobile */
    .context-menu {
        position: fixed !important;
        left: 16px !important;
        right: 16px !important;
        bottom: 16px !important;
        top: auto !important;
        max-height: 60vh;
        overflow-y: auto;
        border-radius: 12px;
    }

    .context-menu-item {
        padding: 14px 16px;
    }
}

/* ========================================
   RESPONSIVE STYLES - SMALL MOBILE
   Extra optimizations for very small screens
   ======================================== */

@media screen and (max-width: 480px) {
    :root {
        --page-margin: 16px;
    }

    .menu-bar {
        padding: 0 4px;
    }

    .menu-logo {
        height: 28px;
        margin-right: 4px;
    }

    .menu-title {
        padding: 8px 6px;
        font-size: 13px;
    }

    .toolbar {
        height: 48px;
        padding: 0 4px;
    }

    .tool-btn {
        min-width: 36px;
        height: 36px;
        padding: 0 6px;
    }

    .tool-select {
        height: 36px;
        font-size: 12px;
    }

    .font-select {
        width: 80px;
        min-width: 80px;
    }

    .editor-container {
        padding-top: 100px;
        padding-left: 4px;
        padding-right: 4px;
    }

    .page {
        padding: 16px;
        font-size: 14px;
        min-height: 500px;
    }

    .page h1 {
        font-size: 20px;
    }

    .page h2 {
        font-size: 17px;
    }

    .page h3 {
        font-size: 15px;
    }
}

/* ========================================
   PRINT STYLES
   Ensure proper A4 printing
   ======================================== */

@media print {

    .menu-bar,
    .toolbar,
    .zoom-indicator,
    .font-status-indicator,
    .grammar-panel,
    .grammar-toggle-btn {
        display: none !important;
    }

    .editor-container {
        padding: 0;
    }

    .page {
        width: 210mm !important;
        height: 297mm !important;
        padding: 20mm !important;
        margin: 0 !important;
        box-shadow: none !important;
        page-break-after: always;
    }

    .page:last-child {
        page-break-after: avoid;
    }
}

/* ========================================
   Bamini Font
   ======================================== */

@font-face {
    font-family: 'Bamini';
    src: url('fonts/Bamini.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
    font-display: swap;
}

/* ========================================
   Bamini Toast Notification
   ======================================== */

.bamini-toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: linear-gradient(135deg, #1e3a5f 0%, #0f2439 100%);
    color: white;
    padding: 16px 20px;
    border-radius: 16px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.25), 0 0 0 1px rgba(255, 255, 255, 0.1);
    z-index: 6000;
    display: flex;
    align-items: flex-start;
    gap: 14px;
    max-width: 420px;
    opacity: 0;
    transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
    backdrop-filter: blur(10px);
}

.bamini-toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.bamini-toast-icon {
    font-size: 24px;
    line-height: 1;
    flex-shrink: 0;
}

.bamini-toast-content {
    flex: 1;
}

.bamini-toast-title {
    font-size: 15px;
    font-weight: 600;
    margin-bottom: 4px;
    color: #fff;
}

.bamini-toast-message {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.8);
    line-height: 1.4;
}

.bamini-toast-close {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 24px;
    height: 24px;
    border: none;
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.7);
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.bamini-toast-close:hover {
    background: rgba(255, 255, 255, 0.2);
    color: white;
}

/* ========================================
   Bamini Mode Indicator
   ======================================== */

.bamini-mode-indicator {
    position: fixed;
    bottom: 16px;
    left: 16px;
    background: linear-gradient(135deg, #059669 0%, #047857 100%);
    color: white;
    padding: 8px 14px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 4px 12px rgba(5, 150, 105, 0.3);
    z-index: 4000;
    animation: baminiIndicatorPulse 2s ease-in-out infinite;
}

.bamini-mode-indicator .indicator-dot {
    width: 8px;
    height: 8px;
    background: #86efac;
    border-radius: 50%;
    animation: dotPulse 1.5s ease-in-out infinite;
}

@keyframes baminiIndicatorPulse {

    0%,
    100% {
        box-shadow: 0 4px 12px rgba(5, 150, 105, 0.3);
    }

    50% {
        box-shadow: 0 4px 20px rgba(5, 150, 105, 0.5);
    }
}

@keyframes dotPulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

/* Mobile responsive for Bamini UI */
@media screen and (max-width: 768px) {
    .bamini-toast {
        left: 16px;
        right: 16px;
        max-width: none;
        transform: translateX(0) translateY(100px);
        bottom: 80px;
    }

    .bamini-toast.show {
        transform: translateX(0) translateY(0);
    }

    .bamini-mode-indicator {
        bottom: 60px;
        left: 50%;
        transform: translateX(-50%);
    }
}