/* NOTE: Contract Editor — A4 page styling
   Prefix: mw-ce-* (contract editor)
   Applied via ContainerClass when Mode == Contract */

/* NOTE: Fullscreen layout — sidebar + scaled page area */
.mw-ce-fullscreen {
    display: flex;
    height: calc(100dvh - 48px);
    overflow: hidden;
}

.mw-ce-sidebar {
    width: 300px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--mud-palette-lines-default);
    background: var(--mud-palette-surface);
}

.mw-ce-sb-header {
    padding: 12px 14px;
    border-bottom: 1px solid var(--mud-palette-lines-default);
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex-shrink: 0;
}

.mw-ce-sb-scroll {
    flex: 1;
    overflow-y: auto;
}

.mw-ce-page-area {
    flex: 1;
    display: flex;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--mud-palette-background-grey);
    position: relative;
}

/* NOTE: Zoom the editor to fit the page area. Unlike transform: scale(),
   zoom affects layout size — so scrolling and centering work naturally. */
.mw-ce-page-area .mw-ce-root {
    zoom: var(--mw-ce-scale, 1);
    margin: 0 auto;
    padding: 0;
    background: transparent;
}

/* NOTE: Remove shadow in fullscreen — page fills the area, shadow looks like a gap */
.mw-ce-fullscreen .rm-with-pagination {
    box-shadow: none;
}

/* NOTE: Hide internal toolbar — formatting moved to sidebar accordion */
.mw-ce-fullscreen .mw-text-editor-toolbar {
    display: none;
}

.mw-ce-root {
    background: var(--mud-palette-background-grey);
    border: none;
    border-radius: 0;
    overflow: visible;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 24px 16px;
}

.mw-ce-root .mw-text-editor-toolbar {
    width: 794px;
    border-radius: 4px 4px 0 0;
    border-bottom: none;
}

.mw-ce-root .mw-text-editor-content {
    width: 794px;
    min-height: 1123px;
    padding: 76px;
    background: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    font-family: Arial, sans-serif;
    font-size: 11pt;
    color: #000;
}

.mw-ce-root .tiptap {
    min-height: auto;
}

/* NOTE: Page break marker */
.page-break-marker {
    border: none;
    border-top: 2px dashed var(--mud-palette-lines-default);
    text-align: center;
    color: var(--mud-palette-text-secondary);
    font-size: 0.75rem;
    font-weight: 500;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    padding: 8px 0;
    margin: 16px 0;
    user-select: none;
    cursor: default;
}

/* NOTE: Fillable field chip (author mode) — amber pill, matches sequence editor style */
.fillable-field-chip {
    display: inline-block;
    background: #F57C00;
    color: #fff;
    border-radius: 10px;
    padding: 1px 8px;
    font-size: 0.78rem;
    font-weight: 600;
    line-height: 1.4;
    user-select: none;
    cursor: grab;
    border: none;
}

.fillable-field-chip:active {
    cursor: grabbing;
}

.fillable-field-required::after {
    content: ' *';
    color: var(--mud-palette-error);
}

/* NOTE: Fill mode — interactive input styling */
.mw-ce-fillable-input {
    border: none;
    border-bottom: 1px solid var(--mud-palette-lines-default);
    background: transparent;
    font: inherit;
    color: inherit;
    min-width: 180px;
    padding: 2px 4px;
    outline: none;
}

.mw-ce-fillable-input:focus {
    border-bottom-color: var(--mud-palette-primary);
}

.mw-ce-fillable-input[data-required="true"] {
    border-bottom-color: var(--mud-palette-warning);
}

.mw-ce-fillable-input[data-required="true"]:focus {
    border-bottom-color: var(--mud-palette-primary);
}

/* NOTE: Signature field — block-level signing area */
.mw-ce-signature-area {
    display: inline-flex;
    flex-direction: column;
    align-items: stretch;
    min-width: 200px;
    max-width: 300px;
}

.mw-ce-signature-box {
    border: 1.5px dashed var(--mud-palette-primary);
    background: transparent;
    border-radius: 4px;
    cursor: pointer;
    padding: 12px;
    min-height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.15s;
}

.mw-ce-signature-box:hover {
    background: var(--mud-palette-primary-lighten);
}

.mw-ce-signature-label {
    color: var(--mud-palette-primary);
    font: inherit;
    font-weight: 500;
    user-select: none;
}

.mw-ce-signature-preview {
    max-height: 70px;
    max-width: 100%;
    object-fit: contain;
}

.mw-ce-signature-line {
    border-top: 1px solid #000;
    margin-top: 4px;
}

.mw-ce-signature-role {
    font-size: 0.8em;
    color: var(--mud-palette-text-secondary);
    margin-top: 2px;
}

/* NOTE: Fill mode — error highlight for invalid fields */
.mw-ce-fillable-input--error {
    border-bottom-color: var(--mud-palette-error) !important;
    background-color: rgba(244, 67, 54, 0.05);
}

/* NOTE: Fill mode — hide toolbar (safety net) */
.mw-ce-fill .mw-text-editor-toolbar {
    display: none;
}

/* NOTE: Line height — matches QuestPDF 11pt Arial inter-line spacing */
.mw-ce-root .mw-text-editor-content {
    line-height: 1.4;
}

/* NOTE: Table styling — fixed layout matches QuestPDF RelativeColumn() */
.mw-ce-root table {
    border-collapse: collapse;
    table-layout: fixed;
    width: 100%;
    border: none;
}

/* NOTE: Table cells — author mode (subtle dashed grid for editing visibility) */
.mw-ce-root th,
.mw-ce-root td {
    border: 1px dashed #E0E0E0;
    padding: 4px;
    vertical-align: top;
}

/* NOTE: Table cells — fill mode (matches PDF: bottom border only) */
.mw-ce-fill th,
.mw-ce-fill td {
    border: none;
    border-bottom: 0.5px solid #E0E0E0;
    padding: 4px;
    vertical-align: top;
}

/* NOTE: Column spacing — matches QuestPDF PaddingHorizontal(4) */
.mw-ce-root td + td,
.mw-ce-root th + th {
    padding-left: 5px;
}

/* NOTE: Cell selection highlight for merge/split operations */
.mw-ce-root .selectedCell {
    background: color-mix(in srgb, var(--mud-palette-primary) 15%, transparent);
    border-color: var(--mud-palette-primary) !important;
    border-style: solid !important;
}

/* NOTE: Paragraph spacing — matches QuestPDF inter-block spacing */
.mw-ce-root .tiptap p {
    margin: 0 0 4px 0;
}

/* NOTE: Heading spacing + weight — matches QuestPDF (no extra top margin, SemiBold) */
.mw-ce-root .tiptap h1,
.mw-ce-root .tiptap h2,
.mw-ce-root .tiptap h3 {
    margin: 0 0 4px 0;
    font-weight: 600;
}

/* NOTE: Images — fill available width by default (matches QuestPDF behavior).
   Inline style from ResizableNodeView overrides this when image has been resized. */
.mw-ce-root [data-resize-wrapper] img {
    width: 100%;
    max-width: 100%;
}

.mw-ce-root .tiptap img {
    max-width: 100%;
}

/* NOTE: Column resize handles — visible on table hover, prominent on handle hover */
.mw-ce-root .column-resize-handle {
    width: 4px;
    background: var(--mud-palette-primary);
    opacity: 0;
    transition: opacity 0.15s;
    cursor: col-resize;
}

.mw-ce-root .tableWrapper:hover .column-resize-handle {
    opacity: 0.3;
}

.mw-ce-root .column-resize-handle:hover {
    opacity: 0.8;
}

.mw-ce-fill .column-resize-handle {
    display: none;
}

.mw-ce-fill .tableWrapper {
    overflow: visible;
}

/* NOTE: Table alignment */
.mw-ce-root table[data-table-align="center"] {
    margin-left: auto;
    margin-right: auto;
}

.mw-ce-root table[data-table-align="right"] {
    margin-left: auto;
    margin-right: 0;
}

/* NOTE: Auto-width tables */
.mw-ce-root table[data-table-width="auto"] {
    width: auto !important;
    min-width: auto !important;
    table-layout: auto;
}

/* NOTE: Table context toolbar */
.mw-ce-table-toolbar {
    position: absolute;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 4px;
    background: var(--mud-palette-surface);
    border: 1px solid var(--mud-palette-lines-default);
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    pointer-events: auto;
}

.mw-ce-table-toolbar button {
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    color: var(--mud-palette-text-primary);
    padding: 0;
    line-height: 1;
}

.mw-ce-table-toolbar button:hover {
    background: var(--mud-palette-action-default-hover);
}

.mw-ce-table-toolbar button.active {
    background: var(--mud-palette-primary-lighten);
    color: var(--mud-palette-primary);
}

.mw-ce-table-toolbar button[title]:hover::after {
    content: attr(title);
    position: absolute;
    bottom: -24px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--mud-palette-text-primary);
    color: var(--mud-palette-surface);
    font-size: 11px;
    padding: 2px 6px;
    border-radius: 3px;
    white-space: nowrap;
    pointer-events: none;
}

.mw-ce-table-toolbar .mw-ce-tb-divider {
    width: 1px;
    height: 20px;
    background: var(--mud-palette-lines-default);
    margin: 0 4px;
    flex-shrink: 0;
}

/* NOTE: Paginated mode — when tiptap-pagination-plus is active,
   it handles page sizing and margins. Remove our own padding to avoid double spacing. */
.mw-ce-root .mw-text-editor-content:has(.rm-with-pagination) {
    padding: 0;
    box-shadow: none;
    background: transparent;
    min-height: auto;
}

.mw-ce-root .rm-with-pagination {
    font-family: Arial, sans-serif;
    font-size: 11pt;
    color: #000;
    background: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

/* NOTE: Image resize handles — TipTap ResizableNodeView */
.mw-ce-root [data-resize-container] {
    display: inline-flex;
}

.mw-ce-root [data-resize-wrapper] {
    position: relative;
    display: inline-block;
}

.mw-ce-root [data-resize-handle] {
    width: 8px;
    height: 8px;
    background: var(--mud-palette-primary);
    border: 1px solid #fff;
    border-radius: 50%;
    opacity: 0;
    transition: opacity 0.15s;
    z-index: 1;
}

.mw-ce-root [data-resize-wrapper]:hover [data-resize-handle],
.mw-ce-root [data-resize-container][data-resize-state="true"] [data-resize-handle] {
    opacity: 1;
}

.mw-ce-root [data-resize-handle="bottom-right"] { cursor: nwse-resize; }
.mw-ce-root [data-resize-handle="bottom-left"] { cursor: nesw-resize; }
.mw-ce-root [data-resize-handle="top-right"] { cursor: nesw-resize; }
.mw-ce-root [data-resize-handle="top-left"] { cursor: nwse-resize; }

/* NOTE: Hide resize handles in fill mode */
.mw-ce-fill [data-resize-handle] {
    display: none;
}

/* NOTE: Margin drag handles — overlay on page for dragging margins */
.mw-ce-margin-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 10;
}

.mw-ce-margin-handle {
    position: absolute;
    pointer-events: auto;
    opacity: 0;
    transition: opacity 0.15s;
}

.mw-ce-margin-handle:hover,
.mw-ce-margin-overlay--dragging .mw-ce-margin-handle {
    opacity: 1;
}

.mw-ce-margin-handle--top,
.mw-ce-margin-handle--bottom {
    left: 0;
    right: 0;
    height: 6px;
    cursor: ns-resize;
    border-top: 1.5px dashed var(--mud-palette-primary);
}

.mw-ce-margin-handle--left,
.mw-ce-margin-handle--right {
    top: 0;
    bottom: 0;
    width: 6px;
    cursor: ew-resize;
    border-left: 1.5px dashed var(--mud-palette-primary);
}

/* NOTE: Mobile — responsive scaling for fill mode */
@media (max-width: 850px) {
    .mw-ce-fill .mw-text-editor-content {
        transform: scale(var(--mw-ce-scale, 1));
        transform-origin: top center;
        margin-bottom: calc((1 - var(--mw-ce-scale, 1)) * -1123px);
    }
    .mw-ce-fill {
        padding: 8px 0;
        overflow-x: hidden;
    }
    .mw-ce-fillable-input {
        min-height: 36px;
        font-size: 16px; /* prevents iOS zoom */
    }
    .mw-ce-signature-box {
        min-height: 80px;
        padding: 12px;
    }
}
