:root {
    --brown: #71643C;
    --teal: #006F7A;
    --teal-hover: #00555E;   
    --green: #006F7A;        
    --slate: #3E4E59;
    --sage: #F4F5F0;
    --cream: #FDFCF8;
    --active-blue: #2563eb;  
}

.calendar-wrapper {
    max-width: 1200px;
    margin: 28px auto;
    padding: 0 16px 40px;
}

/* Controls interface block isolated to calendar area */
.controls {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
    padding: 20px;
    background: var(--sage);
    flex-wrap: wrap;
    border-radius: 8px 8px 0 0;
}

.controls button {
    background-color: var(--teal);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.controls button:hover { background-color: var(--teal-hover); }
.controls button:disabled { background-color: #ccc; cursor: not-allowed; opacity: 0.6; }
.controls .btn-alt { background-color: var(--slate); }

.controls a { 
    text-decoration: none; 
    color: var(--teal); 
    word-break: break-word; 
    font-weight: bold;
}
.controls a:hover {
    color: var(--brown);
    background-color: rgba(0, 111, 122, 0.1);
    border-radius: 3px;
    padding: 1px 3px;
}

#currentMonthDisplay {
    font-size: 1.4em;
    font-weight: bold;
    color: var(--brown);
    min-width: 200px;
    text-align: center;
}

/* Calendar structure configuration */
.calendar-container {
    background: #fff;
    border: 2px solid var(--brown);
    border-top: none;
    border-radius: 0 0 10px 10px;
    box-shadow: 0 6px 24px rgba(62,78,89,0.12);
    overflow: hidden;
}

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 1px;
    background-color: #D1D1D1;
    width: 100%;
}

.weekday-header {
    background-color: var(--green);
    color: white;
    text-align: center;
    padding: 10px 0;
    font-weight: bold;
    font-size: 0.9em;
}

.day-cell {
    background-color: white;
    min-height: 110px;
    padding: 8px;
    position: relative; 
}

.day-cell.other-month { background-color: #F9F9F9; color: #CCC; }
.day-number { font-weight: bold; margin-bottom: 8px; display: block; }

.day-cell.past-day {
    background-color: #FDFDFD;
}
.day-cell.past-day .day-number {
    opacity: 0.45;
}
.day-cell.past-day::after {
    content: "";
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    pointer-events: none; 
    background: linear-gradient(to top right, transparent 48%, rgba(180, 180, 180, 0.25) 49%, rgba(180, 180, 180, 0.25) 51%, transparent 52%);
}

.event-link {
    font-size: 0.75em;
    background-color: var(--sage);
    border-left: 3px solid var(--teal);
    margin-bottom: 4px;
    padding: 4px 6px;
    cursor: pointer;
    white-space: normal;
    word-wrap: break-word;
    overflow-wrap: break-word;
    line-height: 1.2;
    display: block;
    color: var(--slate);
    position: relative;
    z-index: 2; 
}

.event-link:hover { background-color: #E2E6D5; }

/* List layout styling rules */
.list-view {
    max-width: 800px;
    margin: 20px auto;
    display: none;
    padding: 0 20px;
}

.list-item {
    padding: 15px;
    border-bottom: 1px solid #EEE;
    cursor: pointer;
    display: flex;
    align-items: flex-start;
}

.list-item:hover { background-color: var(--sage); }

.list-date {
    color: var(--green);
    font-weight: bold;
    min-width: 80px;
    margin-right: 10px;
    flex-shrink: 0;
}

.list-title { display: inline-block; word-wrap: break-word; }

/* Modal popups */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0; top: 0;
    width: 100%; height: 100%;
    background-color: rgba(62, 78, 89, 0.7);
}

.modal-content {
    background-color: white;
    margin: 10% auto;
    padding: 30px;
    border-left: 10px solid var(--teal);
    width: 90%;
    max-width: 500px;
    border-radius: 8px;
    position: relative;
}

.close-btn {
    position: absolute;
    right: 20px; top: 10px;
    font-size: 28px;
    cursor: pointer;
}

#modalTitle { color: var(--brown); margin: 0 0 10px 0; }
#modalDate { color: var(--teal); font-weight: bold; margin-bottom: 20px; }
#modalDetail { line-height: 1.6; }

.external-link {
    text-align: center;
    margin-top: 14px;
    font-size: 0.88em;
    color: #888;
}

.external-link a {
    color: var(--teal);
    font-weight: 600;
    text-decoration: none;
    border-bottom: 1px dotted var(--teal);
    transition: color 0.2s ease, border-color 0.2s ease;
}

.external-link a:hover {
    color: var(--active-blue);
    border-bottom: 1px solid var(--active-blue);
}

/* Master Root Print Output Elements Setup */
#printListContainer {
    display: none;
    width: 100%;
    max-width: 100%;
    margin: 0;
    padding: 0;
    background: #fff;
}

.print-qr {
    display: none;
}

.print-only-header {
    display: none;
    text-align: center;
    font-size: 2.8em;          /* Increased font size for higher prominence */
    font-weight: 800;          /* Ultra-bold weighting */
    color: var(--teal);
    margin: 0 auto 30px auto;  /* Centers the bounding box and adds spacing below */
    max-width: 450px;          /* Restricts width to cleanly force a two-line wrap */
    line-height: 1.2;          /* Tightens up the space between the two lines */
}

/* Print Rules mirroring reference layout formatting exactly */
.print-month-section {
    page-break-inside: avoid;
    margin-bottom: 25px;
    font-family: Arial, sans-serif;
}

/* Month heading in brown color with a line underneath */
.print-month-heading {
    font-size: 22px;
    font-weight: bold;
    color: #71643C; 
    border-bottom: 2px solid #71643C;
    margin: 0 0 14px 0;
    padding-bottom: 4px;
}

/* Cleaned Event Row spacing configuration (Duplicates Removed) */
.print-event-row {
    padding: 8px 0; 
    border: none;
    margin: 0;
    font-family: Arial, sans-serif;
}

.print-row-main {
    display: flex;
    align-items: flex-start;
    font-size: 16px;
    line-height: 1.4;
}

/* Dates display styled with a wider 150px layout and Teal color */
.print-row-date {
    width: 150px;
    min-width: 150px;
    font-weight: bold;
    color: #006F7A; 
    flex-shrink: 0;
    padding-right: 20px; 
    box-sizing: border-box;
}

/* Title text strictly bolded */
.print-row-title {
    font-weight: bold;
    color: #1e293b;
    flex-grow: 1;
}

/* Details indented cleanly to line up under titles */
.print-row-detail {
    margin-left: 150px;
    font-size: 15px;
    color: #475569;
    margin-top: 4px;
    line-height: 1.4;
    white-space: pre-line;
}

@media print {
    /* ==========================================================================
       STATE 1: MULTI-MONTH LIST PRINTING (Margins Restored)
       ========================================================================== */
    body.executing-multi-print {
        margin: 0.5in !important; /* Restores clean page margins for list view */
        padding: 0 !important;
        background: #fff !important;
    }

    body.executing-multi-print #printListContainer { 
        display: block !important; 
    }
    body.executing-multi-print #pageContentWrapper { 
        display: none !important; 
    }

    /* ==========================================================================
       STATE 2: OPTIMIZED SINGLE-MONTH CALENDAR GRID PRINTING
       ========================================================================== */
    /* Target only the grid view print state to drop margins and kill top/bottom browser shading */
    body:not(.executing-multi-print) {
        margin: 0 !important;
        padding: 0 !important;
        background: #fff !important;
    }

    /* Standardizes the print canvas area specifically for grid view to override browser frames */
    @page {
        size: auto;
        margin: 0 !important; /* Removes the subtle gray headers/footers and text blocks */
    }

    /* Display the grid view's custom ribbon banner cleanly */
    body:not(.executing-multi-print) .print-only-header { 
        display: block !important; 
        text-align: center !important;
        margin: 25px auto 25px auto !important; /* Places balanced white space above the ribbon */
        max-width: 600px !important;
        position: relative !important;
    }
    
    .print-grid-church-title {
        font-family: Arial, sans-serif !important;
        font-size: 28px !important;
        font-weight: 800 !important;
        color: #006F7A !important; 
        text-transform: uppercase;
        letter-spacing: 2px;
        margin-bottom: 8px;
    }
    
    .print-grid-month-title {
        display: inline-block !important;
        font-family: Arial, sans-serif !important;
        font-size: 20px !important;
        font-weight: 700 !important;
        color: #fff !important; 
        background-color: #71643C !important; 
        padding: 6px 40px !important;
        text-transform: uppercase;
        letter-spacing: 1px;
        position: relative !important;
        box-shadow: 0 2px 4px rgba(0,0,0,0.15);
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .print-grid-month-title::before {
        content: "" !important;
        position: absolute !important;
        left: -12px !important;
        top: 0 !important;
        width: 0 !important;
        height: 0 !important;
        border-style: solid !important;
        border-width: 17px 12px 17px 0 !important;
        border-color: #71643C #71643C #71643C transparent !important;
    }

    .print-grid-month-title::after {
        content: "" !important;
        position: absolute !important;
        right: -12px !important;
        top: 0 !important;
        width: 0 !important;
        height: 0 !important;
        border-style: solid !important;
        border-width: 17px 0 17px 12px !important;
        border-color: #71643C transparent #71643C #71643C !important;
    }
    
    /* Hide all web interface navigation elements */
    body:not(.executing-multi-print) .controls, 
    body:not(.executing-multi-print) header, 
    body:not(.executing-multi-print) #header, 
    body:not(.executing-multi-print) .external-link, 
    body:not(.executing-multi-print) .btn-alt, 
    body:not(.executing-multi-print) .navbar, 
    body:not(.executing-multi-print) #logobar,
    body:not(.executing-multi-print) #listView,
    body:not(.executing-multi-print) .header-support,
    body:not(.executing-multi-print) hr { 
        display: none !important; 
    }
    
    /* Grid stretches edge-to-edge horizontally while maintaining a clean safety padding */
    body:not(.executing-multi-print) .calendar-wrapper {
        max-width: 100% !important;
        width: 100% !important;
        margin: 0 !important;
        padding: 0 20px 20px 20px !important; 
    }

    body:not(.executing-multi-print) .calendar-container { 
        display: block !important; 
        border: 2px solid #000000 !important; 
        box-shadow: none !important; 
        background: #fff !important;
        width: 100% !important;
    }

    /* Strong black gridline gaps */
    body:not(.executing-multi-print) .calendar-grid {
        background-color: #000000 !important; 
        gap: 2px !important; 
        width: 100% !important;
    }

    body:not(.executing-multi-print) .weekday-header {
        background-color: #006F7A !important;
        color: #fff !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
        border-bottom: 2px solid #000000 !important;
        font-size: 14px !important;
        padding: 8px 0 !important;
    }

    /* Balanced cell sizing down the page */
    body:not(.executing-multi-print) .day-cell { 
        min-height: 115px !important; 
        background-color: #fff !important;
        border: none !important;
        padding: 6px !important;
        page-break-inside: avoid;
    }

    body:not(.executing-multi-print) .day-cell.other-month { 
        background-color: #ffffff !important; 
    }
    body:not(.executing-multi-print) .day-cell.other-month .day-number { 
        color: #888888 !important; 
    }

    body:not(.executing-multi-print) .day-cell.past-day::after { 
        display: none !important; 
    } 

    body:not(.executing-multi-print) .event-link {
        font-size: 11px !important; 
        line-height: 1.3 !important;
        background: #f1f5f9 !important; 
        border-left: 3px solid #006F7A !important; 
        color: #000 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
        margin-bottom: 4px !important;
        padding: 3px 5px !important;
    }
    
    .print-qr {
        display: block !important;
        position: fixed !important;
        bottom: 15px !important;
        right: 15px !important;
        width: 85px !important;
        height: 85px !important;
        z-index: 99999 !important;
    }
}
/* Screen constraints adaptation features */
@media (max-width: 768px) {
    .calendar-wrapper { margin: 16px auto; padding: 0 10px 30px; }
    .day-cell { min-height: 70px; padding: 4px; }
    .weekday-header { font-size: 0.7em; }
}