/* =========================================================
   Conex Mailer — Templates Page
   Clean page stylesheet. All Templates styling lives here.
   ========================================================= */

#templatesPage,
#templatesPage * {
    box-sizing: border-box;
}

#templatesPage {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(430px, 1fr);
    gap: 25px;
    align-items: stretch;
    width: 100%;
    min-width: 0;
}

#templatesPage > .template-card {
    width: 100%;
    min-width: 0;
    padding: 28px;
    background: #fff;
    border-radius: 22px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04);
}

#templatesPage > .saved-templates-card {
    display: flex;
    flex-direction: column;
    align-self: stretch;
    height: 100%;
    min-height: 0;
    max-height: none;
    overflow: hidden;
}

#templatesPage .template-header,
#templatesPage .saved-templates-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    margin-bottom: 20px;
}

#templatesPage .template-title {
    margin: 0;
    color: #2563eb;
    font-size: 24px;
    font-weight: 700;
    line-height: 1.2;
}

#templatesPage .saved-templates-header h2 {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0;
    color: #111827;
    font-size: 20px;
    font-weight: 700;
    line-height: 1.2;
    white-space: nowrap;
}

#templatesPage .new-template-btn,
#templatesPage .save-template-btn,
#templatesPage .test-template-btn,
#templatesPage .edit-btn,
#templatesPage .delete-btn {
    border: 0;
    font-family: inherit;
    cursor: pointer;
}

#templatesPage .new-template-btn {
    flex: 0 0 auto;
    padding: 14px 20px;
    border-radius: 14px;
    background: #2563eb;
    color: #fff;
    font-size: 15px;
    font-weight: 600;
}

#templatesPage #templateName,
#templatesPage #templateSubject,
#templatesPage #templateReplyTo {
    display: block;
    width: 100%;
    min-height: 54px;
    margin: 0 0 18px;
    padding: 15px 16px;
    border: 1px solid #dbe1ea;
    border-radius: 14px;
    outline: 0;
    background: #fff;
    color: #0f172a;
    font: inherit;
    font-size: 15px;
}

#templatesPage #templateName:focus,
#templatesPage #templateSubject:focus,
#templatesPage #templateReplyTo:focus,
#templatesPage #testEmailAddress:focus,
#templatesPage #templateSearch:focus {
    border-color: #7aa2ff;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.10);
}

/* ---------- Professional Quill toolbar ---------- */

#templatesPage #editorContainer {
    overflow: visible;
    border: 0;
    border-radius: 16px;
    background: #fff;
}

#templatesPage #toolbar.ql-toolbar.ql-snow {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 7px;
    min-height: 56px;
    padding: 12px;
    overflow: visible;
    border: 1px solid #d8e1ee;
    border-radius: 16px 16px 0 0;
    background: #f8fafc;
}

#templatesPage #toolbar .ql-formats {
    float: none;
    display: flex;
    align-items: center;
    flex: 0 0 auto;
    min-width: 0;
    margin: 0;
    padding-right: 7px;
    border-right: 1px solid #dbe4ef;
}

#templatesPage #toolbar .ql-picker.ql-font {
    width: 150px;
}

#templatesPage #toolbar .ql-picker.ql-size {
    width: 72px;
}

#templatesPage #toolbar .ql-picker.ql-color,
#templatesPage #toolbar .ql-picker.ql-background,
#templatesPage #toolbar .ql-picker.ql-align {
    width: 30px;
}

#templatesPage #toolbar button,
#templatesPage #toolbar .ql-picker-label {
    height: 28px;
}

#templatesPage #toolbar .template-spacing-format {
    position: relative;
    overflow: visible;
}

#templatesPage #toolbar .template-spacing-control {
    position: relative;
}

#templatesPage #toolbar .template-spacing-button {
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    width: 118px;
    height: 30px;
    padding: 0 9px;
    border: 1px solid #cbd5e1;
    border-radius: 7px;
    background: #fff;
    color: #334155;
    font: inherit;
    font-size: 12px;
    font-weight: 800;
    line-height: 1;
    cursor: pointer;
}

#templatesPage #toolbar .template-spacing-button:hover,
#templatesPage #toolbar .template-spacing-button[aria-expanded="true"] {
    border-color: #2563eb;
    background: #eff6ff;
    color: #1d4ed8;
}

#templatesPage #toolbar .template-spacing-menu {
    position: absolute;
    z-index: 10050;
    top: calc(100% + 7px);
    left: 0;
    width: 230px;
    padding: 6px;
    border: 1px solid #cbd5e1;
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 12px 30px rgba(15, 23, 42, 0.18);
}

#templatesPage #toolbar .template-spacing-menu[hidden] {
    display: none;
}

#templatesPage #toolbar .template-spacing-menu button {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
    min-height: 48px;
    padding: 8px 10px;
    border: 0;
    border-radius: 7px;
    background: #fff;
    color: #0f172a;
    font: inherit;
    text-align: left;
    cursor: pointer;
}

#templatesPage #toolbar .template-spacing-menu button:hover {
    background: #eff6ff;
    color: #1d4ed8;
}

#templatesPage #toolbar .template-spacing-menu strong {
    display: block;
    font-size: 12px;
    line-height: 1.3;
}

#templatesPage #toolbar .template-spacing-menu small {
    display: block;
    margin-top: 2px;
    color: #64748b;
    font-size: 10px;
    line-height: 1.35;
}

#templatesPage #toolbar .template-merge-fields {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px;
    padding-left: 4px;
}

#templatesPage #toolbar .template-merge-title {
    margin-right: 2px;
    color: #475569;
    font-size: 12px;
    font-weight: 800;
    white-space: nowrap;
}

#templatesPage #toolbar button.template-merge-btn {
    width: auto;
    height: 31px;
    padding: 0 11px;
    border: 1px solid #bdd0ff;
    border-radius: 9px;
    background: #eef4ff;
    color: #2458d3;
    font: inherit;
    font-size: 12px;
    font-weight: 800;
    line-height: 29px;
    white-space: nowrap;
    cursor: pointer;
}

#templatesPage #toolbar button.template-merge-btn:hover {
    border-color: #2563eb;
    background: #2563eb;
    color: #fff;
}

#templatesPage #templateEditor.ql-container.ql-snow {
    height: 520px !important;
    min-height: 520px;
    max-height: 520px !important;
    overflow: hidden !important;
    border: 1px solid #d8e1ee;
    border-top: 0;
    border-radius: 0 0 16px 16px;
    background: #fff;
    font-family: Arial, sans-serif;
    font-size: 14px;
}

#templatesPage #templateEditor .ql-editor {
    height: 100% !important;
    min-height: 0 !important;
    max-height: 100% !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    overscroll-behavior: auto;
    scrollbar-gutter: stable;
    padding: 24px 26px;
    line-height: 1.35;
}

#templatesPage #templateEditor .ql-editor.ql-blank::before {
    left: 26px;
    right: 26px;
    color: #64748b;
}

#templatesPage #templateEditor .ql-editor img {
    max-width: 100%;
    height: auto;
    cursor: pointer;
}

/* Quill custom font labels */
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-label[data-value="Arial"]::before,
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-item[data-value="Arial"]::before { content: "Arial"; font-family: Arial, sans-serif; }
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-label[data-value="Helvetica"]::before,
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-item[data-value="Helvetica"]::before { content: "Helvetica"; font-family: Helvetica, Arial, sans-serif; }
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-label[data-value="Georgia"]::before,
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-item[data-value="Georgia"]::before { content: "Georgia"; font-family: Georgia, serif; }
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-label[data-value="Times New Roman"]::before,
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-item[data-value="Times New Roman"]::before { content: "Times New Roman"; font-family: "Times New Roman", serif; }
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-label[data-value="Tahoma"]::before,
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-item[data-value="Tahoma"]::before { content: "Tahoma"; font-family: Tahoma, sans-serif; }
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-label[data-value="Verdana"]::before,
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-item[data-value="Verdana"]::before { content: "Verdana"; font-family: Verdana, sans-serif; }
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-label[data-value="Trebuchet MS"]::before,
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-item[data-value="Trebuchet MS"]::before { content: "Trebuchet MS"; font-family: "Trebuchet MS", sans-serif; }
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-label[data-value="Courier New"]::before,
#templatesPage #toolbar .ql-picker.ql-font .ql-picker-item[data-value="Courier New"]::before { content: "Courier New"; font-family: "Courier New", monospace; }
#templatesPage #toolbar .ql-picker.ql-size .ql-picker-item::before,
#templatesPage #toolbar .ql-picker.ql-size .ql-picker-label::before { content: attr(data-value); }

#templatesPage .template-editor-note,
#templatesPage .template-image-note {
    margin: 8px 4px 0;
    padding: 10px 13px;
    border-radius: 10px;
    font-size: 12px;
    line-height: 1.5;
}

#templatesPage .template-editor-note {
    background: #eff6ff;
    color: #334155;
}

#templatesPage .template-editor-note strong {
    color: #1d4ed8;
}

#templatesPage .template-image-note {
    border: 1px solid #bbf7d0;
    background: #f0fdf4;
    color: #334155;
}

#templatesPage .template-image-note strong {
    color: #15803d;
}

/* ---------- Image resize controls ---------- */

.conex-template-image-overlay {
    position: fixed;
    z-index: 100000;
    border: 2px solid #2563eb;
    box-sizing: border-box;
    pointer-events: none;
    background: transparent;
}

.conex-template-image-handle {
    position: absolute;
    width: 12px;
    height: 12px;
    border: 2px solid #fff;
    border-radius: 50%;
    background: #2563eb;
    box-shadow: 0 1px 5px rgba(15, 23, 42, 0.35);
    pointer-events: auto;
}

.conex-template-image-handle[data-corner="nw"] { left: -7px; top: -7px; cursor: nwse-resize; }
.conex-template-image-handle[data-corner="ne"] { right: -7px; top: -7px; cursor: nesw-resize; }
.conex-template-image-handle[data-corner="sw"] { left: -7px; bottom: -7px; cursor: nesw-resize; }
.conex-template-image-handle[data-corner="se"] { right: -7px; bottom: -7px; cursor: nwse-resize; }

.conex-template-image-toolbar {
    position: absolute;
    left: 50%;
    bottom: calc(100% + 10px);
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: max-content;
    padding: 7px;
    border: 1px solid #cbd5e1;
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.18);
    pointer-events: auto;
    font-family: Arial, sans-serif;
}

.conex-template-image-toolbar button {
    height: 30px;
    padding: 0 9px;
    border: 1px solid #cbd5e1;
    border-radius: 7px;
    background: #f8fafc;
    color: #334155;
    font-size: 11px;
    font-weight: 800;
    cursor: pointer;
}

.conex-template-image-toolbar button:hover {
    border-color: #93c5fd;
    background: #eff6ff;
    color: #1d4ed8;
}

.conex-template-image-toolbar button[data-action="delete"] {
    border-color: #fecdd3;
    background: #fff1f2;
    color: #be123c;
}

.conex-template-image-width-control {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0 5px;
    color: #475569;
    font-size: 11px;
    font-weight: 800;
}

.conex-template-image-width-control input {
    width: 66px;
    height: 30px;
    padding: 0 7px;
    border: 1px solid #cbd5e1;
    border-radius: 7px;
    outline: 0;
    font-size: 12px;
    font-weight: 800;
    text-align: center;
}

/* ---------- Bottom actions ---------- */

#templatesPage .template-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 15px;
    margin-top: 20px;
}

#templatesPage .save-template-btn {
    flex: 0 0 auto;
    margin: 0;
    padding: 15px 22px;
    border-radius: 14px;
    background: #0f172a;
    color: #fff;
    font-size: 15px;
    font-weight: 600;
}

#templatesPage .test-email-box {
    display: grid;
    grid-template-columns: minmax(240px, 1fr) auto;
    align-items: center;
    justify-content: end;
    gap: 8px;
    flex: 1 1 auto;
    min-width: 0;
}

#templatesPage #testEmailAddress {
    width: 100%;
    min-width: 0;
    height: 50px;
    padding: 0 14px;
    border: 1px solid #dbe1ea;
    border-radius: 14px;
    outline: 0;
    background: #fff;
    color: #0f172a;
    font: inherit;
    font-size: 14px;
}

#templatesPage #testEmailAddress:focus {
    border-color: #7aa2ff;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.10);
}

#templatesPage #testEmailAddress:disabled,
#templatesPage .test-template-btn:disabled,
#templatesPage .save-template-btn:disabled,
#templatesPage .new-template-btn:disabled {
    cursor: not-allowed;
    opacity: .58;
}

#templatesPage .test-template-btn {
    flex: 0 0 auto;
    min-height: 50px;
    padding: 0 20px;
    border-radius: 14px;
    background: #22c55e;
    color: #fff;
    font-size: 14px;
    font-weight: 700;
    white-space: nowrap;
}

#templatesPage #toolbar .ql-image.template-image-uploading {
    opacity: .55;
    cursor: wait;
}

/* ---------- Saved templates panel ---------- */

/* The Campaign Builder also uses the id templatesList.
   Keep the Templates-page list governed only by this page stylesheet. */
#templatesPage > .saved-templates-card #templatesList {
    height: auto !important;
    min-height: 0 !important;
    max-height: none !important;
    margin-top: 0 !important;
}


#templatesPage .saved-templates-header input,
#templatesPage #templateSearch {
    flex: 0 0 140px;
    width: 140px;
    padding: 10px;
    border: 1px solid #dbe1ea;
    border-radius: 10px;
    outline: 0;
    background: #fff;
    font: inherit;
    font-size: 13px;
}

#templatesPage #templatesList {
    flex: 1 1 auto;
    width: 100%;
    height: auto;
    min-height: 0;
    max-height: none;
    padding-right: 5px;
    overflow-x: hidden;
    overflow-y: auto;
}

#templatesPage .template-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
        "heading heading"
        "meta actions";
    align-items: end;
    column-gap: 16px;
    row-gap: 14px;
    width: 100%;
    min-height: 158px;
    margin-bottom: 14px;
    padding: 18px 18px 17px;
    border: 1px solid #e2e8f0;
    border-radius: 18px;
    background: #fff;
    box-shadow: 0 1px 2px rgba(15, 23, 42, .02);
}

#templatesPage .template-item:hover {
    border-color: #cbd5e1;
    box-shadow: 0 8px 22px rgba(15, 23, 42, .06);
}

#templatesPage .template-item-heading {
    grid-area: heading;
    display: grid;
    grid-template-columns: 58px minmax(0, 1fr);
    align-items: center;
    gap: 16px;
    min-width: 0;
}

#templatesPage .template-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 58px;
    height: 58px;
    border-radius: 16px;
    background: #eef2ff;
    color: #111827;
}

#templatesPage .template-icon svg {
    display: block;
    width: 30px;
    height: 30px;
}

#templatesPage .template-item-heading h3 {
    min-width: 0;
    margin: 0;
    color: #111827;
    font-family: inherit;
    font-size: 20px;
    font-weight: 700;
    line-height: 1.2;
    letter-spacing: normal;
    white-space: normal;
    overflow-wrap: anywhere;
    word-break: normal;
    hyphens: none;
}

#templatesPage .template-meta {
    grid-area: meta;
    min-width: 0;
    padding-left: 2px;
    color: #64748b;
    font-size: 13px;
    line-height: 1.35;
}

#templatesPage .template-date-line,
#templatesPage .template-time-line {
    display: block;
}

#templatesPage .template-creator-line {
    display: flex;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 5px;
    color: #a16207;
}

#templatesPage .template-creator-line strong {
    color: #a16207;
    font-weight: 700;
}

#templatesPage .template-buttons {
    grid-area: actions;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 9px;
    align-self: end;
    flex: 0 0 auto;
}

#templatesPage .edit-btn,
#templatesPage .delete-btn {
    min-width: 68px;
    padding: 11px 14px;
    border-radius: 12px;
    color: #fff;
    font-size: 14px;
    font-weight: 700;
    line-height: 1;
    white-space: nowrap;
}

#templatesPage .edit-btn { background: #0f172a; }
#templatesPage .delete-btn { background: #ef4444; }

#templatesPage .edit-btn:hover { background: #1e293b; }
#templatesPage .delete-btn:hover { background: #dc2626; }

#templatesPage .templates-empty-state {
    padding: 36px 20px;
    color: #64748b;
    text-align: center;
}

@media (max-width: 1250px) {
    #templatesPage {
        grid-template-columns: minmax(0, 1fr) 420px;
    }

    #templatesPage #toolbar .ql-picker.ql-font {
        width: 132px;
    }
}

@media (max-width: 1040px) {
    #templatesPage {
        grid-template-columns: 1fr;
    }

    #templatesPage > .saved-templates-card {
        min-height: 620px;
    }
}

@media (max-width: 720px) {
    #templatesPage > .template-card {
        padding: 20px 16px;
    }

    #templatesPage .template-header,
    #templatesPage .saved-templates-header,
    #templatesPage .template-actions {
        align-items: stretch;
        flex-direction: column;
    }

    #templatesPage .saved-templates-header input,
    #templatesPage #templateSearch,
    #templatesPage #testEmailAddress {
        width: 100%;
        min-width: 0;
        flex-basis: auto;
    }

    #templatesPage .test-email-box {
        width: 100%;
        grid-template-columns: 1fr;
    }

    #templatesPage .test-template-btn {
        width: 100%;
    }

    #templatesPage .template-item {
        grid-template-columns: 1fr;
        grid-template-areas:
            "heading"
            "meta"
            "actions";
    }

    #templatesPage .template-buttons {
        justify-content: flex-start;
    }

    .conex-template-image-toolbar {
        max-width: 90vw;
        overflow-x: auto;
    }
}

/* =========================================================
   Desktop scrolling behavior — final
   - No scrollbar on the complete Email Templates card.
   - Only the Quill message body scrolls when the email is long.
   - Saved Templates keeps an independent list scrollbar.
   - Selectors support both the old generic page markup and the
     newer named card classes.
   ========================================================= */
@media (min-width: 1041px) {
    #templatesPage {
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        overflow: visible !important;
        align-items: stretch !important;
    }

    /* LEFT CARD: never scroll the complete card. */
    #templatesPage > .template-card:first-child,
    #templatesPage > .template-editor-card {
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        overflow: visible !important;
        overscroll-behavior: auto !important;
        scrollbar-gutter: auto !important;
    }

    /* The editor shell and toolbar remain fixed in the normal card flow. */
    #templatesPage > .template-card:first-child #editorContainer,
    #templatesPage > .template-editor-card #editorContainer {
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        overflow: visible !important;
    }

    /* Only the message viewport is fixed-height. */
    #templatesPage #templateEditor.ql-container.ql-snow {
        height: 520px !important;
        min-height: 520px !important;
        max-height: 520px !important;
        overflow: hidden !important;
    }

    /* The vertical scrollbar belongs here only. */
    #templatesPage #templateEditor .ql-editor {
        height: 100% !important;
        min-height: 0 !important;
        max-height: 100% !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior: auto;
        scrollbar-gutter: stable;
    }

    /* RIGHT CARD: stretch beside the editor card; only its list scrolls. */
    #templatesPage > .template-card:last-child,
    #templatesPage > .saved-templates-card {
        display: flex !important;
        flex-direction: column !important;
        align-self: stretch !important;
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        overflow: hidden !important;
    }

    #templatesPage > .template-card:last-child #templatesList,
    #templatesPage > .saved-templates-card #templatesList {
        flex: 1 1 0 !important;
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior: auto;
        scrollbar-gutter: stable;
    }
}
