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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: linear-gradient(135deg, #f5f7fa 0%, #e4e8ec 100%);
    padding: 20px;
    min-height: 100vh;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
}

header {
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    padding: 30px;
    margin-bottom: 20px;
}

h1 {
    color: #1e293b;
    font-size: 2em;
    margin-bottom: 8px;
}

.subtitle {
    color: #64748b;
}

/* ============================================================================
   MULTI-PAGE SYSTEM - Neue Struktur
   ============================================================================ */

.editor-container {
    background: #e5e7eb;
    border-radius: 12px;
    padding: 40px 20px;
    margin-bottom: 20px;
    display: flex;
    justify-content: center;
}

#pagesWrapper {
    display: flex;
    flex-direction: column;
    gap: 0; /* Gap entfernt - wird durch margin-bottom auf page-container ersetzt */
    align-items: center;
    padding: 20px 0 200px 0; /* Unten viel mehr Platz! */
}

.page-container {
    width: 21cm;
    height: 29.7cm;
    background: white;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    position: relative;
    box-sizing: border-box;
    overflow: hidden;
    transform-origin: top center;
    /* FIX: Kein statisches margin - wird dynamisch durch applyZoom() gesetzt */
    flex-shrink: 0;
}

.page-container:last-child {
    margin-bottom: 0; /* Letzte Seite braucht kein margin */
}

.page-editor {
    /* ✅ DYNAMISCH! Nimmt vollen verfügbaren Platz */
    min-height: 100%;
    height: 100%;
    outline: none;
    line-height: 1.6;
    white-space: pre-wrap;
    word-wrap: break-word;
    font-family: "Times New Roman", serif;
    font-size: 12pt;
    padding: 0;
    margin: 0;
}

.page-editor:focus {
    outline: 1px solid #72f63b;
    outline-offset: 5px;
}

/* Page-Editor Outline auch bei Ruler-Hover */
.ruler-horizontal:hover ~ .page-editor,
.ruler-vertical:hover ~ .page-editor {
    outline: 2px solid #72f63b;
    outline-offset: -2px;
}

.margin-grip:hover ~ .page-editor {
    outline: 2px solid #72f63b;
    outline-offset: -2px;
}

.page-info {
    position: absolute;
    bottom: 10px;
    right: 10px;
    font-size: 10px;
    color: #6c757d;
}

/* ============================================================================
   RULER SYSTEM - Lineale und Greifer (pro Seite)
   ============================================================================ */

.ruler-horizontal {
    position: absolute;
    top: 0;
    left: 30px; /* Breite des vertikalen Lineals */
    width: calc(100% - 30px);
    height: 30px;
    background: #f0f0f0;
    border-bottom: 1px solid #ccc;
    box-sizing: border-box;
}

.ruler-vertical {
    position: absolute;
    top: 30px; /* Höhe des horizontalen Lineals */
    left: 0;
    width: 30px;
    height: calc(100% - 30px);
    background: #f0f0f0;
    border-right: 1px solid #ccc;
    box-sizing: border-box;
}

/* Margin-Bereiche (nur an Ecken wie Word) */
.ruler-horizontal::before,
.ruler-horizontal::after {
    content: "";
    position: absolute;
    top: 0;
    height: 100%;
    width: 0;
    pointer-events: none;
}

.ruler-vertical::before,
.ruler-vertical::after {
    content: "";
    position: absolute;
    left: 0;
    width: 100%;
    height: 0;
    pointer-events: none;
}

/* Margin-Schatten (dezente Ecken-Markierung) */
.page-container::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    border: 2px solid transparent;
    box-sizing: border-box;
}

/* Ecken-Markierungen für Margins */
.margin-corner-tl,
.margin-corner-tr,
.margin-corner-bl,
.margin-corner-br {
    position: absolute;
    width: 15px;
    height: 15px;
    border: 2px solid #4285f4;
    pointer-events: none;
    z-index: 100;
}

.margin-corner-tl {
    top: 0;
    left: 0;
    border-right: none;
    border-bottom: none;
}

.margin-corner-tr {
    top: 0;
    right: 0;
    border-left: none;
    border-bottom: none;
}

.margin-corner-bl {
    bottom: 0;
    left: 0;
    border-right: none;
    border-top: none;
}

.margin-corner-br {
    bottom: 0;
    right: 0;
    border-left: none;
    border-top: none;
}

.ruler-marks {
    position: relative;
    width: 100%;
    height: 100%;
}

.ruler-mark {
    position: absolute;
    background: #6c757d;
}

.ruler-horizontal .ruler-mark {
    width: 1px;
    height: 5px;
    bottom: 0;
}

.ruler-horizontal .ruler-mark.major {
    height: 8px;
    width: 1px;
    background: #495057;
}

.ruler-vertical .ruler-mark {
    height: 1px;
    width: 5px;
    right: 0;
}

.ruler-vertical .ruler-mark.major {
    width: 8px;
    height: 1px;
    background: #495057;
}

.ruler-label {
    position: absolute;
    font-size: 9px;
    color: #495057;
    font-weight: 500;
    user-select: none;
}

.ruler-horizontal .ruler-label {
    bottom: 2px;
    transform: translateX(-50%);
}

.ruler-vertical .ruler-label {
    right: 2px;
    transform: translateY(-50%);
    writing-mode: vertical-rl;
    text-orientation: mixed;
}

/* ============================================================================
   MARGIN GRIPS - Nur kleine unsichtbare Handles
   ============================================================================ */

.margin-grip {
    position: absolute;
    cursor: pointer;
    z-index: 110;
    opacity: 0; /* Standardmäßig unsichtbar */
    transition:
        opacity 0.2s,
        background 0.2s;
}

/* Greifer nur beim Hover über Lineal sichtbar */
.ruler-horizontal:hover .margin-grip.horizontal,
.ruler-vertical:hover .margin-grip.vertical {
    opacity: 0.6;
}

.margin-grip:hover {
    opacity: 1 !important;
}

/* Horizontale Greifer (Left/Right im horizontalen Lineal) */
.margin-grip.horizontal {
    width: 3px; /* Sehr schmal! */
    height: 100%;
    top: 0;
    background: #4285f4;
    border-radius: 2px;
}

/* Vertikale Greifer (Top/Bottom im vertikalen Lineal) */
.margin-grip.vertical {
    width: 100%;
    height: 3px; /* Sehr flach! */
    left: 0;
    background: #4285f4;
    border-radius: 2px;
}

/* Beim Dragging */
.margin-grip.dragging {
    opacity: 1 !important;
    background: #1967d2;
    box-shadow: 0 0 8px rgba(66, 133, 244, 0.5);
}

/* Grip-Label */
.grip-label {
    position: absolute;
    background: #4285f4;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
    pointer-events: none;
    z-index: 120;
    opacity: 0;
    transition: opacity 0.2s;
}

/* Label nur beim Dragging sichtbar */
.margin-grip.dragging .grip-label {
    opacity: 1;
}

/* Label-Positionen */
.margin-grip.horizontal .grip-label {
    top: 35px;
    left: 50%;
    transform: translateX(-50%);
}

.margin-grip.vertical .grip-label {
    left: 35px;
    top: 50%;
    transform: translateY(-50%);
}

/* ============================================================================
   MARGIN GUIDE LINES - Visuelle Seitenrand-Markierungen
   ============================================================================ */

.margin-guide {
    position: absolute;
    pointer-events: none;
    z-index: 105;
    opacity: 0;
    transition: opacity 0.2s;
}

.margin-guide-top,
.margin-guide-bottom {
    left: 30px;
    right: 0;
    height: 1px;
    background: #4285f4;
    box-shadow: 0 0 4px rgba(66, 133, 244, 0.5);
}

.margin-guide-left,
.margin-guide-right {
    top: 30px;
    bottom: 0;
    width: 1px;
    background: #4285f4;
    box-shadow: 0 0 4px rgba(66, 133, 244, 0.5);
}

/* Guide Lines beim Grip Hover sichtbar */
.margin-grip:hover ~ .margin-guide,
.margin-grip.dragging ~ .margin-guide {
    opacity: 1;
}

/* Alternative: Guides direkt beim Ruler Hover */
.page-container:hover .margin-guide {
    opacity: 0.3;
}

.margin-grip:hover ~ .margin-guide,
.margin-grip.dragging ~ .margin-guide {
    opacity: 1 !important;
}

/* ============================================================================
   MARGIN GRIPS - Nur kleine unsichtbare Handles
   ============================================================================ */

.margin-grip {
    position: absolute;
    cursor: pointer;
    z-index: 110;
    opacity: 0; /* Standardmäßig unsichtbar */
    transition:
        opacity 0.2s,
        background 0.2s;
}

/* Greifer nur beim Hover über Lineal sichtbar */
.ruler-horizontal:hover .margin-grip.horizontal,
.ruler-vertical:hover .margin-grip.vertical {
    opacity: 0.6;
}

.margin-grip:hover {
    opacity: 1 !important;
}

/* Horizontale Greifer (Left/Right im horizontalen Lineal) */
.margin-grip.horizontal {
    width: 3px; /* Sehr schmal! */
    height: 100%;
    top: 0;
    background: #4285f4;
    border-radius: 2px;
}

/* Vertikale Greifer (Top/Bottom im vertikalen Lineal) */
.margin-grip.vertical {
    width: 100%;
    height: 3px; /* Sehr flach! */
    left: 0;
    background: #4285f4;
    border-radius: 2px;
}

/* Beim Dragging */
.margin-grip.dragging {
    opacity: 1 !important;
    background: #1967d2;
    box-shadow: 0 0 8px rgba(66, 133, 244, 0.5);
}

/* Grip-Label */
.grip-label {
    position: absolute;
    background: #4285f4;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
    pointer-events: none;
    z-index: 120;
    opacity: 0;
    transition: opacity 0.2s;
}

/* Label nur beim Dragging sichtbar */
.margin-grip.dragging .grip-label {
    opacity: 1;
}

/* Label-Positionen */
.margin-grip.horizontal .grip-label {
    top: 35px;
    left: 50%;
    transform: translateX(-50%);
}

.margin-grip.vertical .grip-label {
    left: 35px;
    top: 50%;
    transform: translateY(-50%);
}

/* ============================================================================
   TOOLBAR
   ============================================================================ */

.libreoffice-toolbar {
    position: sticky;
    top: 0;
    z-index: 1500;
    background: linear-gradient(to bottom, #f5f5f5 0%, #e8e8e8 100%);
    border-bottom: 1px solid #c0c0c0;
    padding: 4px 8px;
    display: flex;
    align-items: center;
    gap: 4px;
    flex-wrap: wrap;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.lo-button {
    background: transparent;
    border: 1px solid transparent;
    padding: 4px 6px;
    cursor: pointer;
    border-radius: 3px;
    font-size: 14px;
    color: #333;
    transition: all 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
    height: 28px;
}

.lo-button:hover {
    background: #fff;
    border-color: #b4b4b4;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.lo-button:active {
    background: #e0e0e0;
}

.lo-separator {
    width: 1px;
    height: 24px;
    background: #c0c0c0;
    margin: 0 4px;
}

.lo-dropdown {
    background: white;
    border: 1px solid #b4b4b4;
    padding: 4px 8px;
    border-radius: 3px;
    font-size: 12px;
    cursor: pointer;
    height: 28px;
}

.lo-dropdown:hover {
    border-color: #7a7a7a;
}

.lo-font-select {
    min-width: 140px;
}

.lo-size-select {
    min-width: 60px;
}

.lo-line-height-select {
    min-width: 80px;
}

.lo-color-picker {
    width: 28px;
    height: 28px;
    border: 1px solid #b4b4b4;
    border-radius: 3px;
    cursor: pointer;
    padding: 0;
}

.zoom-control {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-left: auto;
}

.zoom-display {
    font-size: 11px;
    color: #555;
    min-width: 40px;
    text-align: center;
}

/* ============================================================================
   DIALOGS
   ============================================================================ */

.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 0;
    height: 0;
    background: transparent;
    display: none;
    z-index: 2000;
    pointer-events: none;
}

.overlay.active {
    display: block;
}

.dialog {
    position: fixed;
    top: 10%;
    left: 50%;
    transform: translateX(-50%);
    pointer-events: all;
    z-index: 2001;
    background: white;
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    max-width: 90%;
}

.dialog-close-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    font-size: 2em;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.dialog-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: move;
    user-select: none;
    border-radius: 16px 16px 0 0;
}

.dialog-header h2 {
    font-size: 1.5em;
    margin: 0;
}

.dialog-close-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    font-size: 2em;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.dialog-close-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: rotate(90deg);
}

/* Dialoge draggable machen */
.dialog.draggable {
    cursor: move;
}

.dialog.dragging {
    user-select: none;
    opacity: 0.9;
}

.dialog-header {
    padding: 15px 20px;
    border-bottom: 1px solid #eee;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: move;
    user-select: none;
}

.dialog-header:active {
    cursor: grabbing;
}

.dialog-header h2 {
    font-size: 1.5em;
    margin: 0;
}

.dialog-close-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    font-size: 2em;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.dialog-close-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: rotate(90deg);
}

.dialog-body {
    padding: 30px;
    max-height: 70vh;
}

.underscore-setting {
    margin-bottom: 20px;
    font-size: 1em;
}

.underscore-setting label {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
    cursor: pointer;
}

.underscore-setting input[type="number"] {
    width: 80px;
    padding: 8px 12px;
    border: 2px solid #ddd;
    border-radius: 8px;
    font-size: 1em;
    margin-left: 10px;
}

.setting-info-compact {
    background: #e3f2fd;
    padding: 12px;
    border-radius: 8px;
    color: #1976d2;
    font-size: 0.95em;
    margin-top: 15px;
}

.functions-grid {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.category-title {
    font-weight: 700;
    font-size: 1.2em;
    color: #667eea;
    margin-top: 15px;
    padding-bottom: 10px;
    border-bottom: 2px solid #667eea;
}

.category-title:first-child {
    margin-top: 0;
}

.function-item {
    display: flex;
    gap: 15px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.3s;
    border: 2px solid transparent;
}

.function-item:hover {
    background: #e9ecef;
    border-color: #667eea;
    transform: translateX(5px);
}

.function-icon {
    font-size: 2em;
    width: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.function-content {
    flex: 1;
}

.function-title {
    font-weight: 600;
    font-size: 1.1em;
    margin-bottom: 5px;
    color: #333;
}

.function-desc {
    color: #666;
    font-size: 0.95em;
}

.dialog-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    margin-bottom: 20px;
}

.dialog-option {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px;
    background: #f8f9fa;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
}

.dialog-option:hover {
    background: #e9ecef;
    transform: translateX(4px);
}

.dialog-info {
    background: #e3f2fd;
    padding: 15px;
    border-radius: 8px;
    color: #1976d2;
    line-height: 1.6;
}

.dialog-body::-webkit-scrollbar {
    width: 8px;
}

.dialog-body::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.dialog-body::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
}

.dialog-body::-webkit-scrollbar-thumb:hover {
    background: #555;
}

footer {
    text-align: center;
    color: #64748b;
    margin-top: 30px;
    font-size: 0.9em;
}

/* ============================================================================
   TABELLEN STYLES
   ============================================================================ */

/* Tabellen im Editor */
.page-editor table {
    border-collapse: collapse;
    margin: 10px 0;
    font-family: inherit;
    font-size: inherit;
}

.page-editor table td,
.page-editor table th {
    padding: 8px 12px;
    text-align: left;
    vertical-align: top;
    min-width: 50px;
}

.page-editor table.table-bordered td,
.page-editor table.table-bordered th {
    border: 1px solid #ddd;
}

.page-editor table th {
    background-color: #f5f5f5;
    font-weight: bold;
}

.page-editor table tr:hover td {
    background-color: #f9f9f9;
}

/* Tabellen-Dialog Styles */
#tableDialog .dialog-section {
    margin-bottom: 15px;
}

#tableDialog label {
    display: block;
    margin-bottom: 5px;
    font-weight: 500;
}

#tableDialog input[type="number"],
#tableDialog select {
    width: 100%;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 14px;
}

#tableDialog input[type="checkbox"] {
    width: auto;
    margin-right: 8px;
}

#tableDialog .primary-btn {
    width: 100%;
    padding: 10px;
    background: #007bff;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
    font-weight: 500;
}

#tableDialog .primary-btn:hover {
    background: #0056b3;
}

/* Tabellen Kontext-Menü */
.context-menu {
    position: fixed;
    background: white;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    z-index: 10000;
    min-width: 200px;
    padding: 5px 0;
}

.context-menu-item {
    padding: 8px 16px;
    cursor: pointer;
    font-size: 14px;
    color: #333;
}

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

.context-menu-divider {
    height: 1px;
    background: #ddd;
    margin: 5px 0;
}

/* Tabellen-Zellen Hover Highlight */
.page-editor table td.cell-highlight,
.page-editor table th.cell-highlight {
    background-color: #e3f2fd !important;
}

/* ============================================================================
   TABELLEN DRAG & DROP
   ============================================================================ */

/* Wrapper für verschiebbare Tabellen */
.table-drag-wrapper {
    display: inline-block;
    position: absolute;
    cursor: default;
    transition: opacity 0.2s;
}

.table-drag-wrapper:hover {
    box-shadow: 0 0 0 2px #4285f4;
}

/* Drag-Handle */
.table-drag-handle {
    position: absolute;
    top: -25px;
    left: 0;
    background: #4285f4;
    color: white;
    width: 25px;
    height: 25px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    font-size: 16px;
    opacity: 0;
    transition: opacity 0.2s;
    z-index: 100;
}

.table-drag-wrapper:hover .table-drag-handle {
    opacity: 1;
}

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

.table-drag-handle:hover {
    background: #1967d2;
}

/* Tabelle im Wrapper */
.table-drag-wrapper table {
    margin: 0;
    position: relative;
}