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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: #f5f5f5;
}

.view-toggle {
    display: flex;
    gap: 0.5rem;
    background: #f0f0f0;
    border-radius: 6px;
    padding: 0.25rem;
}

.calendar-title {
    font-size: 1.5rem;
    color: #2c3e50;
}

.view-toggle {
    display: flex;
    gap: 0.5rem;
    background: #f0f0f0;
    border-radius: 6px;
    padding: 0.25rem;
}

.view-toggle a {
    padding: 0.5rem 1rem;
    text-decoration: none;
    color: #666;
    border-radius: 4px;
    transition: all 0.2s;
}

.view-toggle a.active {
    background: white;
    color: #2c3e50;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.view-toggle a:hover:not(.active) {
    color: #2c3e50;
}

.navigation {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.nav-button {
    background: white;
    border: 1px solid #ddd;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    cursor: pointer;
    text-decoration: none;
    color: #2c3e50;
    transition: all 0.2s;
}

.nav-button:hover {
    background: #f8f8f8;
    border-color: #999;
}

.current-month {
    font-size: 1.25rem;
    font-weight: 600;
    color: #2c3e50;
    min-width: 200px;
    text-align: center;
}

.container {
    max-width: 1400px;
    margin: 2rem auto;
    padding: 0 2rem;
}

/* Responsive View Switching Logic */
.calendar-view-section { display: none; }
.list-view-section { display: none; }

/* Default based on active_view context */
body.active-calendar .calendar-view-section { display: block; }
body.active-list .list-view-section { display: block; }
body.active-list .navigation { display: none; }

/* Orientation and behavior for mobiles and tablets */
@media screen and (max-width: 1024px) {
    /* Hide selection toggle on mobiles/tablets */
    .view-toggle {
        display: none !important;
    }

    /* For mobile/tablet, force view based on orientation regardless of active_view */
    @media (orientation: portrait) {
        .calendar-view-section { display: none !important; }
        .list-view-section { display: block !important; }
        .navigation { display: none !important; }
    }

    @media (orientation: landscape) {
        .calendar-view-section { display: block !important; }
        .list-view-section { display: none !important; }
        .navigation { display: flex !important; }
    }
}

/* Calendar Grid Styles */
.calendar-grid {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
}

.calendar-header {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    background: white;
    color: #333;
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
}

.calendar-header div {
    padding: 1rem;
    text-align: center;
    font-weight: 600;
    border-bottom: 1px solid #e0e0e0;
    border-right: 1px solid #e0e0e0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.calendar-header div:last-child {
    border-right: none;
}

.calendar-body {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    grid-auto-rows: minmax(140px, auto);
    flex: 1;
}

.calendar-day {
    border: 1px solid #e0e0e0;
    padding: 0.75rem;
    background: white;
    position: relative;
    display: flex;
    flex-direction: column;
}

.calendar-day.other-month {
    background: #f9f9f9;
    color: #999;
}

.calendar-day.today {
    background: #fff9e6;
}

.day-number {
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 0.5rem;
    flex-shrink: 0;
}

.calendar-day.today .day-number {
    background: #e74c3c;
    color: white;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.event-item {
    background: white;
    color: #111;
    padding: 0.5rem;
    margin-bottom: 0.5rem;
    border-radius: 6px;
    font-size: 0.9rem;
    cursor: default;
    transition: background 0.15s, box-shadow 0.15s, transform 0.08s;
    word-wrap: break-word;
    overflow-wrap: break-word;
    flex-shrink: 0;
    position: relative;
    border: 1px solid #e6e6e6;
}

.event-item.multi-day-event {
    border-left: 4px solid #007bff;
    background: linear-gradient(90deg, #e8f4ff 0%, white 100%);
}

.multi-day-indicator {
    font-size: 0.75rem;
    margin-right: 0.25rem;
    cursor: help;
}

/* Multi-day event continuation bar for calendar grid */
.event-continuation {
    display: block;
    position: relative;
    height: 10px;
    margin: 2px 0;
    border-radius: 5px;
    transition: background 0.2s ease;
    cursor: pointer;
    background-color: transparent !important;
}

/* Apply lightened color via pseudo-element so popup is unaffected */
.event-continuation::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 5px;
    background-color: var(--bar-color, #666);
    opacity: 0.4;
    transition: opacity 0.2s ease;
}

.event-continuation:hover::before {
    opacity: 0.8;
}

.event-continuation:hover {
    transform: scaleY(1.3);
    z-index: 1001;
}

.event-continuation .event-popup {
    display: none;
    position: absolute;
    bottom: 100%;
    left: 0;
    min-width: 250px;
    max-width: 350px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
    z-index: 2000;
    overflow: hidden;
    text-decoration: none;
    color: inherit;
}

@media (hover: hover) {
    .event-continuation:hover .event-popup {
        display: block;
        transform: scaleY(0.769) !important; /* Counter the parent's scaleY(1.3) to prevent stretched popup */
        transform-origin: bottom center;
        transition: none !important; /* Remove animation/transition effects */
    }
    
    .event-continuation:hover .event-popup.has-poster {
        display: flex;
    }
}

.popup-dates {
    font-size: 0.85rem;
    color: #007bff;
    margin-bottom: 0.5rem;
    padding: 0.25rem 0.5rem;
    background: #e8f4ff;
    border-radius: 4px;
}

@media (hover: hover) {
    .event-item:hover {
        background: #fafafa;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
        transform: translateY(-2px);
        z-index: 1001; /* Ensure it's above other cells and the header (z-index 100) */
    }

    .event-item:hover .event-popup {
        display: block;
    }

    .event-item:hover .event-popup.has-poster {
        display: flex;
    }
}

.event-popup {
    display: none;
    position: absolute;
    bottom: 70%; /* Overlap slightly to allow easy mouse transition */
    left: 0;
    background: white;
    color: #333;
    border: 1px solid #ddd;
    border-radius: 6px;
    padding: 1rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    z-index: 1000;
    min-width: 280px;
    max-width: 350px;
    text-decoration: none;
    cursor: pointer;
}

.event-popup.has-poster {
    flex-direction: row;
    max-width: 550px;
    gap: 1rem;
}

.popup-poster {
    flex-shrink: 0;
    width: 180px;
}

.popup-poster img {
    width: 100%;
    height: auto;
    border-radius: 4px;
    object-fit: cover;
}

.popup-content {
    flex: 1;
    min-width: 280px;
    max-width: 350px;
}

/* Adjust popup position for days on the right side of the calendar */
.calendar-day:nth-child(7n) .event-popup,
.calendar-day:nth-child(7n-1) .event-popup {
    left: auto;
    right: 0;
}

/* Position popup below for first-row events to avoid cutoff at top */
.calendar-day.first-row .event-popup {
    bottom: auto;
    top: 70%; /* Overlap slightly from the top */
}

.popup-title {
    font-size: 1rem;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 0.5rem;
    word-wrap: break-word;
}

.popup-type {
    display: inline-block;
    background: #3498db;
    color: white;
    padding: 0.2rem 0.5rem;
    border-radius: 3px;
    font-size: 0.75rem;
    margin-bottom: 0.5rem;
}

.popup-time {
    font-size: 0.85rem;
    color: #555;
    margin-bottom: 0.3rem;
}

.popup-venue {
    font-size: 0.85rem;
    color: #666;
    margin-bottom: 0.3rem;
}

.popup-organiser {
    font-size: 0.85rem;
    color: #666;
    margin-bottom: 0.5rem;
}

.popup-description {
    font-size: 0.85rem;
    line-height: 1.4;
    color: #555;
    border-top: 1px solid #e0e0e0;
    padding-top: 0.5rem;
    margin-bottom: 0.5rem;
}

.popup-link-hint {
    margin-top: 0.75rem;
    padding-top: 0.5rem;
    border-top: 1px solid #e0e0e0;
    color: #3498db;
    font-size: 0.85rem;
    font-weight: 600;
}

/* List View Styles */
.events-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.date-group {
    background: white;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.date-header {
    background: #2c3e50;
    color: white;
    padding: 1rem 1.5rem;
    font-size: 1.1rem;
    font-weight: 600;
}

.event-card {
    padding: 1.5rem;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    justify-content: center;
    gap: 1.5rem;
    transition: background 0.2s, transform 0.2s;
    cursor: pointer;
    align-items: flex-start;
}

/* Center container for time + details */
.event-card-center {
    display: flex;
    gap: 1.5rem;
    max-width: 600px;
    flex: 0 1 600px;
}

/* Poster columns - fixed width for symmetry */
.event-poster {
    flex: 0 0 200px;
    display: flex;
    align-items: center;
}

.event-card.has-poster.poster-left .event-poster {
    order: -1;
}

.event-card.has-poster.poster-left .event-card-center {
    order: 0;
}

.event-card.has-poster.poster-left::after {
    content: '';
    flex: 0 0 200px;
    order: 1;
}

.event-card.has-poster.poster-right .event-poster {
    order: 1;
}

.event-card.has-poster.poster-right .event-card-center {
    order: 0;
}

.event-card.has-poster.poster-right::before {
    content: '';
    flex: 0 0 200px;
    order: -1;
}

.event-poster img {
    width: 100%;
    height: auto;
    border-radius: 6px;
    object-fit: contain;
    max-height: 250px;
}

.event-link:last-child .event-card {
    border-bottom: none;
}

.event-card:hover {
    background: #f0f7ff;
    transform: translateX(4px);
    padding: 1.25rem;
}

/* Multi-day event styles for list view */
.event-card.multi-day-event {
    border-left: 4px solid #007bff;
    background: linear-gradient(90deg, #e8f4ff 0%, white 100%);
}

.event-card.multi-day-event:hover {
    background: linear-gradient(90deg, #d0e8ff 0%, #f0f7ff 100%);
}

/* Continuation card styles for list view (multi-day events on subsequent days) */
.event-card.continuation-card {
    padding: 0.75rem 1rem;
    background: #fafafa;
    min-height: auto;
}

.event-card.continuation-card:hover {
    background: #f0f0f0;
}

.event-card.continuation-card .event-title.continuation-title {
    font-size: 0.95rem;
    margin-bottom: 0;
    display: inline;
}

.event-card.continuation-card .continuation-dates {
    font-size: 0.85rem;
    color: #666;
    margin-left: 0.75rem;
}

.event-dates {
    font-size: 0.9rem;
    font-weight: 600;
    color: #007bff;
    white-space: nowrap;
}

.event-link {
    text-decoration: none;
    color: inherit;
    display: block;
}

.event-time-block {
    text-align: center;
}

.event-time {
    font-size: 1rem;
    font-weight: 600;
    color: #111;
    display: block;
}

.event-time-end {
    font-size: 0.9rem;
    color: #666;
    display: block;
    margin-top: 0.25rem;
}

.event-details {
    flex: 1;
}

.event-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 0.5rem;
}

.event-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    margin-top: 0.75rem;
    font-size: 0.9rem;
    color: #666;
}

.event-meta-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.event-meta-item::before {
    content: "•";
    color: #3498db;
    font-weight: bold;
}

.event-description {
    color: #555;
    line-height: 1.6;
    margin-top: 0.5rem;
}

.event-description p {
    display: block;
    margin: 0 0 0.5rem 0;
}

.event-description p:last-child {
    margin-bottom: 0;
}

.event-type {
    display: inline-block;
    background: #3498db;
    color: white;
    padding: 0.25rem 0.75rem;
    border-radius: 12px;
    font-size: 0.85rem;
    margin-bottom: 0.5rem;
}

.no-events {
    background: white;
    border-radius: 8px;
    padding: 3rem;
    text-align: center;
    color: #999;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.day-short { display: none; }

@media (max-width: 1024px) {
    /* Mobile/Tablet Calendar Grid View: Hide titles, show time as colored tag */
    .calendar-view-section .event-item .event-title-text {
        display: none;
    }
    
    .calendar-view-section .event-item .multi-day-indicator {
        display: none;
    }
    
    .calendar-view-section .event-item .event-time {
        display: inline-block;
        background-color: var(--event-type-color, #666);
        color: var(--event-type-text-color, white);
        padding: 0.25rem 0.5rem;
        border-radius: 4px;
        font-size: 0.75rem;
        font-weight: 600;
    }
    
    .calendar-view-section .event-item {
        padding: 0.25rem;
        margin-bottom: 0.25rem;
        border: none;
        background: transparent;
    }

    /* Force hide all popups on mobile/tablet unless they have the .show-popup class */
    .event-popup {
        display: none !important;
    }
    
    .show-popup .event-popup {
        display: flex !important;
    }
    
    /* Center screen fix: disable transforms on parents when popup is shown */
    /* as transforms create a new coordinate system for fixed children */
    .event-item.show-popup,
    .event-continuation.show-popup {
        transform: none !important;
        z-index: 10000 !important;
    }
    
    /* Mobile/Tablet: Use .show-popup class instead of :hover */
    .event-item.show-popup .event-popup,
    .event-continuation.show-popup .event-popup {
        display: flex;
    }
    
    /* Mobile/Tablet: Center popups and make them fill screen */
    .event-item.show-popup .event-popup,
    .event-continuation.show-popup .event-popup {
        position: fixed !important;
        top: 50% !important;
        left: 50% !important;
        right: auto !important;
        bottom: auto !important;
        transform: translate(-50%, -50%) !important;
        min-width: 90vw !important;
        max-width: 90vw !important;
        width: 90vw !important;
        max-height: 90vh !important;
        overflow-y: auto !important;
        z-index: 10001 !important; /* Above backdrop */
        padding: 1.5rem !important;
        margin: 0 !important;
        background: white !important;
        border-radius: 12px !important;
        box-shadow: 0 10px 30px rgba(0,0,0,0.3) !important;
    }
    
    .event-item.show-popup .event-popup.has-poster,
    .event-continuation.show-popup .event-popup.has-poster {
        flex-direction: row !important; /* Desktop-like side-by-side layout */
        max-width: 90vw !important;
        display: flex !important;
        gap: 1rem !important;
        align-items: flex-start !important;
    }
    
    .event-item.show-popup .popup-poster,
    .event-continuation.show-popup .popup-poster {
        flex: 0 0 120px !important; /* Fixed width for poster on left */
        max-width: 120px !important;
        margin: 0 !important;
    }
    
    .event-item.show-popup .popup-poster img,
    .event-continuation.show-popup .popup-poster img {
        width: 100% !important;
        height: auto !important;
        max-height: 180px !important;
        object-fit: contain !important;
    }
    
    .event-item.show-popup .popup-content,
    .event-continuation.show-popup .popup-content {
        flex: 1 !important;
        min-width: 0 !important;
        max-width: none !important;
    }
    
    /* Add backdrop overlay for mobile/tablet popups */
    .event-item.show-popup::after,
    .event-continuation.show-popup::after {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 9999;
    }
    
    /* Keep continuation hover transform countered */
    .event-continuation.show-popup .event-popup {
        transform: translate(-50%, -50%) !important;
    }
}

/* Touch device popup handling for larger screens (e.g., iPads in landscape).
   These devices show the desktop calendar grid but can't use hover for popups. */
@media (pointer: coarse) and (min-width: 1025px) {
    /* Force hide hover-triggered popups — touch can't hover reliably */
    .event-popup {
        display: none !important;
    }

    .event-item.show-popup .event-popup,
    .event-continuation.show-popup .event-popup {
        display: flex !important;
    }

    .event-item.show-popup,
    .event-continuation.show-popup {
        transform: none !important;
        z-index: 10000 !important;
    }

    .event-item.show-popup .event-popup,
    .event-continuation.show-popup .event-popup {
        position: fixed !important;
        top: 50% !important;
        left: 50% !important;
        right: auto !important;
        bottom: auto !important;
        transform: translate(-50%, -50%) !important;
        min-width: 350px !important;
        max-width: 550px !important;
        width: auto !important;
        max-height: 80vh !important;
        overflow-y: auto !important;
        z-index: 10001 !important;
        padding: 1.5rem !important;
        margin: 0 !important;
        background: white !important;
        border-radius: 12px !important;
        box-shadow: 0 10px 30px rgba(0,0,0,0.3) !important;
    }

    .event-item.show-popup .event-popup.has-poster,
    .event-continuation.show-popup .event-popup.has-poster {
        flex-direction: row !important;
        max-width: 600px !important;
        gap: 1rem !important;
        align-items: flex-start !important;
    }

    .event-item.show-popup .popup-poster,
    .event-continuation.show-popup .popup-poster {
        flex: 0 0 160px !important;
        max-width: 160px !important;
        margin: 0 !important;
    }

    .event-item.show-popup .popup-poster img,
    .event-continuation.show-popup .popup-poster img {
        width: 100% !important;
        height: auto !important;
        max-height: 220px !important;
        object-fit: contain !important;
    }

    .event-item.show-popup .popup-content,
    .event-continuation.show-popup .popup-content {
        flex: 1 !important;
        min-width: 0 !important;
        max-width: none !important;
    }

    /* Backdrop overlay */
    .event-item.show-popup::after,
    .event-continuation.show-popup::after {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 9999;
    }

    .event-continuation.show-popup .event-popup {
        transform: translate(-50%, -50%) !important;
    }

    /* Indicate tappable events */
    .event-item,
    .event-continuation {
        cursor: pointer;
    }
}

@media (max-width: 768px) {
    .day-full { display: none; }
    .day-short { display: inline; }

    /* Mobile list view: stack vertically */
    .event-card,
    .event-card.has-poster {
        display: flex;
        flex-direction: column;
        gap: 1rem;
        padding: 1rem;
    }
    
    .event-card-center,
    .event-details {
        display: contents; /* Flatten wrapper for reordering */
    }
    
    /* Reorder elements: time, event-type, poster, then rest */
    .event-time-block {
        order: -3 !important;
        text-align: left;
        flex-shrink: 0;
        width: auto; /* allow time to sit inline with type */
        display: inline-flex;
        align-items: center;
        margin-right: 0.5rem;
    }
    
    .event-type {
        order: -2 !important;
        margin-bottom: 0;
        display: inline-flex;
        align-items: center;
        padding: 0.15rem 0.5rem;
        border-radius: 12px;
        font-size: 0.85rem;
        vertical-align: middle;
    }
    
    /* Force poster after time and type */
    .event-card .event-poster,
    .event-card.has-poster .event-poster,
    .event-card.has-poster.poster-left .event-poster,
    .event-card.has-poster.poster-right .event-poster {
        order: -1 !important;
        flex: 0 0 auto;
        width: 100%;
        max-width: 250px;
        margin: 0 auto 1rem auto;
    }
    
    .event-title {
        order: 0 !important;
        width: 100%;
    }
    
    .event-description {
        order: 1 !important;
        width: 100%;
    }
    
    .event-description p,
    .event-description ul,
    .event-description ol {
        display: block !important;
        width: 100% !important;
    }
    
    .event-meta {
        order: 2 !important;
        width: 100%;
    }
    
    .event-card .event-poster img,
    .event-card.has-poster .event-poster img {
        object-fit: contain;
        max-height: none;
        width: auto;
        max-width: 100%;
    }

    /* Mobile grid: place time and type on same row, poster below */
    .event-card {
        display: grid !important;
        grid-template-columns: auto 1fr;
        grid-auto-rows: auto;
        gap: 0.5rem 1rem;
        align-items: start;
    }

    .event-time-block {
        grid-column: 1;
        grid-row: 1;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .event-type {
        grid-column: 2;
        grid-row: 1;
        align-self: center;
        justify-self: start;
    }

    .event-card .event-poster,
    .event-card.has-poster .event-poster,
    .event-card.has-poster.poster-left .event-poster,
    .event-card.has-poster.poster-right .event-poster {
        grid-column: 1 / -1;
        grid-row: 2;
        width: 100%;
        max-width: none;
        margin: 0 auto 1rem auto;
    }

    .event-title,
    .event-description,
    .event-meta {
        grid-column: 1 / -1;
    }

    /* Hide the symmetry spacers on mobile */
    .event-card.has-poster.poster-left::after,
    .event-card.has-poster.poster-right::before {
        display: none !important;
    }
    
    .header-content {
        flex-direction: column;
        align-items: stretch;
    }
    
    .view-toggle {
        justify-content: center;
    }

    .calendar-day {
        min-height: 80px;
        padding: 0.25rem;
    }
    
    .day-number {
        font-size: 0.85rem;
    }
    
    .event-item {
        font-size: 0.75rem;
        padding: 0.2rem 0.3rem;
    }
    
    .calendar-header div {
        padding: 0.5rem;
        font-size: 0.85rem;
    }
    
    /* Stronger border on mobile */
    .event-card {
        border-bottom: 1px solid #ccc;
    }
}

/* Footer: match leftlist footer colors and center text */
footer {
  background-color: #1f2937;
  color: #e5e7eb;
  text-align: center;
  padding: 1rem;
  margin-top: 2rem;
}
