.cal {
    --cal-border: rgba(16, 24, 40, 0.10);
    --cal-text: rgba(16, 24, 40, 0.92);
    --cal-muted: rgba(16, 24, 40, 0.52);
    --cal-radius: 12px;
    --cal-cell-h: 120px;
    --cal-shadow: 0 1px 2px rgba(16, 24, 40, 0.08), 0 12px 28px rgba(16, 24, 40, 0.12);

    border: 1px solid var(--cal-border);
    border-radius: var(--cal-radius);
    background: #fff;

    /* responsive: fill container + allow horizontal scroll on narrow widths */
    display: block;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* =========================================================
   CalendarV2 page wrapper fix (this removes the empty right side)
   Scoped to ONLY pages containing #cal-recurring-v2
   ========================================================= */
main.app-main:has(#cal-recurring-v2),
main.nw-main:has(#cal-recurring-v2),
.page-content:has(#cal-recurring-v2),
.content-stack:has(#cal-recurring-v2) {
    max-width: none !important;
    width: 100% !important;
}

.page-content:has(#cal-recurring-v2),
.content-stack:has(#cal-recurring-v2) {
    margin: 0 !important;
}

/* If your environment doesn’t support :has(), uncomment this fallback:
main.app-main,
main.nw-main,
.page-content,
.content-stack {
  max-width: none !important;
  width: 100% !important;
  margin: 0 !important;
}
*/

/* =========================================================
   Header
   ========================================================= */
.cal__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--cal-border);

    /* responsive */
    flex-wrap: wrap;
    gap: 10px;
}

.cal__title {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: var(--cal-text);
    min-width: 0;
}

.cal__nav {
    display: inline-flex;
    gap: 8px;
}

.cal__nav-btn {
    width: 30px;
    height: 30px;
    border-radius: 8px;
    border: 1px solid var(--cal-border);
    background: #fff;
    color: rgba(16, 24, 40, 0.55);
    cursor: pointer;
}

.cal__nav-btn:focus-visible {
    outline: 2px solid rgba(59, 130, 246, 0.35);
    outline-offset: 2px;
}

/* =========================================================
   Grid
   ========================================================= */

/* Weekday row: no vertical borders */
.cal__dow {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    border-bottom: 1px solid var(--cal-border);

    /* keep readable 7-col layout on narrow widths */
    min-width: 720px;
}

.cal__dow-cell {
    padding: 12px 8px;
    font-size: 14px;
    font-weight: 700;
    color: rgba(16, 24, 40, 0.70);
    text-align: center;
}

.cal__viewport {
    position: relative;
}

.cal__grid {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));

    /* keep readable 7-col layout on narrow widths */
    min-width: 720px;
}

.cal__cell {
    min-height: var(--cal-cell-h);
    padding: 10px;
    border-right: 1px solid var(--cal-border);
    border-bottom: 1px solid var(--cal-border);
    position: relative;
}

.cal__cell:nth-child(7n) {
    border-right: 0;
}

.cal__date-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.cal__date {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    border-radius: 7px;
    font-size: 12px;
    font-weight: 650;
    color: var(--cal-text);
}

.cal__cell.is-outside .cal__date {
    color: rgba(16, 24, 40, 0.30);
    font-weight: 550;
}

.cal__cell.is-selected .cal__date {
    border: 2px solid rgba(34, 197, 94, 0.45);
    background: rgba(34, 197, 94, 0.06);
    color: rgba(21, 128, 61, 0.95);
}

.cal__more-link {
    font-size: 11px;
    color: rgba(37, 99, 235, 0.95);
    text-decoration: none;
    cursor: pointer;
    user-select: none;
}

.cal__more-link:hover {
    text-decoration: underline;
}

.cal__events {
    margin-top: 10px;
    display: grid;
    gap: 8px;
    max-height: calc(var(--cal-cell-h) - 48px);
    overflow: hidden;
}

/* =========================================================
   Text-only rows (no pills)
   ========================================================= */
.cal__row {
    width: 100%;
    display: grid;
    grid-template-columns: 16px 1fr auto;
    gap: 8px;
    align-items: center;

    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;

    font-size: 12px;
    line-height: 1.1;
    text-align: left;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;

    cursor: pointer;
}

.cal__row:focus-visible {
    outline: 2px solid rgba(59, 130, 246, 0.35);
    outline-offset: 2px;
    border-radius: 8px;
}

.cal__row-icon {
    width: 16px;
    height: 16px;
    display: grid;
    place-items: center;
}

.cal__row-icon img {
    width: 16px;
    height: 16px;
    border-radius: 4px;
    object-fit: cover;
    display: block;
}

.cal__row-label {
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--cal-text);
}

.cal__row-amt {
    font-variant-numeric: tabular-nums;
    font-weight: 650;
}

/* amount color rules */
.cal__row.is-income .cal__row-amt {
    color: rgba(21, 128, 61, 0.95);
}

.cal__row.is-income.is-pastdue .cal__row-amt {
    color: rgba(185, 28, 28, 0.78);
}

.cal__row.is-income.is-pastdue .cal__row-label {
    color: rgba(16, 24, 40, 0.70);
}

.cal__row.is-expense.is-future .cal__row-amt {
    color: rgba(16, 24, 40, 0.60);
}

.cal__row.is-expense.is-paid .cal__row-amt {
    color: rgba(16, 24, 40, 0.52);
}

.cal__row.is-expense.is-pastdue .cal__row-amt {
    color: rgba(185, 28, 28, 0.78);
}

.cal__row.is-expense.is-paid .cal__row-label {
    color: rgba(16, 24, 40, 0.55);
}

.cal__row.is-expense.is-future .cal__row-label {
    color: rgba(16, 24, 40, 0.70);
}

/* =========================================================
   Popover (teleported to fixed layer)
   ========================================================= */
.cal__popover-layer {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 10000;
}

.cal__popover {
    position: absolute;
    width: min(460px, calc(100vw - 24px));
    background: #fff;
    border: 1px solid var(--cal-border);
    border-radius: 14px;
    box-shadow: var(--cal-shadow);
    pointer-events: auto;
}

.cal__popover-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    border-bottom: 1px solid var(--cal-border);
}

.cal__popover-date {
    font-size: 12px;
    font-weight: 650;
    color: rgba(16, 24, 40, 0.70);
}

.cal__popover-close {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    border: 1px solid var(--cal-border);
    background: #fff;
    cursor: pointer;
}

.cal__popover-main {
    padding: 14px;
}

/* popover content helpers */
.cal__pop-title {
    display: grid;
    grid-template-columns: 30px 1fr auto;
    gap: 12px;
    align-items: start;
}

.cal__pop-badge {
    width: 30px;
    height: 30px;
    border-radius: 10px;
    background: rgba(239, 68, 68, 0.12);
    border: 1px solid rgba(239, 68, 68, 0.25);
    display: grid;
    place-items: center;
    font-weight: 750;
    color: rgba(185, 28, 28, 0.95);
}

.cal__pop-badge.is-neutral {
    background: rgba(148, 163, 184, 0.16);
    border-color: rgba(148, 163, 184, 0.25);
    color: rgba(16, 24, 40, 0.70);
}

.cal__pop-vendor-img {
    width: 20px;
    height: 20px;
    border-radius: 6px;
    object-fit: cover;
    display: block;
}

.cal__pop-name-main {
    font-size: 14px;
    font-weight: 700;
    color: rgba(16, 24, 40, 0.92);
}

.cal__pop-name-sub {
    margin-top: 3px;
    font-size: 11px;
    color: rgba(16, 24, 40, 0.55);
}

.cal__pop-amt {
    font-size: 12px;
    font-weight: 750;
    font-variant-numeric: tabular-nums;
    color: rgba(16, 24, 40, 0.85);
}

.cal__pop-chip {
    display: inline-flex;
    align-items: center;
    height: 18px;
    padding: 0 8px;
    border-radius: 999px;
    border: 1px solid rgba(148, 163, 184, 0.35);
    background: rgba(148, 163, 184, 0.12);
    font-size: 11px;
    font-weight: 650;
    color: rgba(16, 24, 40, 0.62);
    white-space: nowrap;
}

.cal__pop-chip.is-pastdue {
    border-color: rgba(239, 68, 68, 0.28);
    background: rgba(239, 68, 68, 0.10);
    color: rgba(185, 28, 28, 0.82);
}

/* popover amount color scheme */
.cal__pop-amt.is-income {
    color: rgba(21, 128, 61, 0.95);
}

.cal__pop-amt.is-income.is-pastdue {
    color: rgba(185, 28, 28, 0.78);
}

.cal__pop-amt.is-expense.is-future {
    color: rgba(16, 24, 40, 0.60);
}

.cal__pop-amt.is-expense.is-paid {
    color: rgba(16, 24, 40, 0.52);
}

.cal__pop-amt.is-expense.is-pastdue {
    color: rgba(185, 28, 28, 0.78);
}

/* =========================================================
   Breakpoints
   ========================================================= */
@media (max-width: 900px) {
    .cal {
        --cal-cell-h: 110px;
    }

    .cal__header {
        padding: 12px 12px;
    }

    .cal__cell {
        padding: 8px;
    }

    .cal__events {
        gap: 6px;
        margin-top: 8px;
        max-height: calc(var(--cal-cell-h) - 44px);
    }

    .cal__row {
        font-size: 11px;
        gap: 6px;
    }
}

@media (max-width: 640px) {
    .cal {
        --cal-cell-h: 104px;
    }

    .cal__header {
        padding: 10px 10px;
    }

    .cal__dow-cell {
        padding: 10px 6px;
        font-size: 10px;
    }

    .cal__cell {
        padding: 6px;
    }

    .cal__date {
        width: 24px;
        height: 24px;
        border-radius: 6px;
    }

    .cal__events {
        gap: 5px;
        margin-top: 6px;
        max-height: calc(var(--cal-cell-h) - 42px);
    }

    .cal__row {
        font-size: 11px;
        grid-template-columns: 14px 1fr auto;
        gap: 6px;
    }

    .cal__row-icon,
    .cal__row-icon img {
        width: 14px;
        height: 14px;
        border-radius: 4px;
    }
}