/* ==========================================
   BENEFIQ — CALENDAR PAGE
   Month grid, stat header, deadline timeline
   ========================================== */

@layer pages {

  /* ==========================================
     DEADLINE CARDS
     ========================================== */
  .deadline-cards {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    margin-bottom: var(--space-6);
  }

  .deadline-card {
    display: flex;
    align-items: center;
    gap: var(--space-4);
    padding: var(--space-4) var(--space-5);
    background: rgba(0, 0, 0, 0.03);
    border-radius: var(--radius-card);
    border: none;
    transition: background var(--transition-base);
  }

  .deadline-card:hover {
    background: rgba(0, 0, 0, 0.05);
  }

  .deadline-card--urgent {
    background: rgba(245, 158, 11, 0.06);
    border-left: 3px solid var(--color-warning);
  }

  .deadline-card--soon {
    background: rgba(249, 115, 22, 0.05);
    border-left: 3px solid var(--brand-500);
  }

  .deadline-card-icon {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    background: rgba(0, 0, 0, 0.04);
    color: var(--text-muted);
  }

  .deadline-card--urgent .deadline-card-icon {
    background: rgba(245, 158, 11, 0.1);
    color: var(--color-warning);
  }

  .deadline-card--soon .deadline-card-icon {
    background: rgba(249, 115, 22, 0.08);
    color: var(--brand-500);
  }

  .deadline-card-swatch {
    width: 36px;
    height: 24px;
    border-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
  }

  .deadline-card-body {
    flex: 1;
    min-width: 0;
  }

  .deadline-card-name {
    font-size: var(--text-sm);
    font-weight: var(--weight-semibold);
    color: var(--color-text);
    margin-bottom: 2px;
  }

  .deadline-card-meta {
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  .deadline-card-countdown {
    flex-shrink: 0;
    text-align: right;
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .deadline-card-days {
    font-size: var(--text-sm);
    font-weight: 800;
    color: var(--text-secondary);
  }

  .deadline-card--urgent .deadline-card-days { color: var(--color-warning); }
  .deadline-card--soon .deadline-card-days { color: var(--brand-500); }

  .deadline-card-date {
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  /* ==========================================
     CALENDAR MONTH GRID
     ========================================== */
  .calendar-grid-container {
    background: rgba(255, 255, 255, 0.65);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.5);
    border-radius: var(--radius-card);
    padding: var(--space-5);
    margin-bottom: var(--space-6);
    box-shadow: var(--shadow-card), inset 0 1px 0 rgba(255, 255, 255, 0.6);
  }

  .calendar-grid-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-4);
  }

  .calendar-grid-month {
    font-size: var(--text-lg);
    font-weight: 800;
    color: var(--text);
  }

  .calendar-nav-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-base);
  }

  .calendar-nav-btn:hover {
    background: var(--bg-sunken);
    color: var(--text);
  }

  .calendar-grid-days {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    text-align: center;
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: var(--space-4);
    padding-bottom: var(--space-3);
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  }

  .calendar-grid-cells {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    grid-auto-rows: 1fr;
    min-height: 792px; /* 6 rows × 130px + 12px gaps — consistent across all months */
    gap: 2px;
  }

  .cal-cell {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    min-height: 130px;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    cursor: default;
    transition: background var(--transition-base);
    overflow: hidden;
  }

  .cal-cell--empty {
    visibility: hidden;
    min-height: 0;
  }

  .cal-day {
    font-size: var(--text-sm);
    font-weight: var(--weight-semibold);
    color: var(--text-secondary);
    line-height: 1;
    margin-bottom: var(--space-2);
  }

  /* Event labels inside cells */
  .cal-event {
    display: block;
    font-size: var(--text-xs);
    line-height: 1.3;
    font-weight: var(--weight-medium);
    padding: 2px 5px;
    border-radius: var(--radius-sm);
    margin-bottom: 3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .cal-event--benefit {
    background: rgba(59, 130, 246, 0.1);
    color: var(--accent-blue-500);
  }

  .cal-event--benefit.cal-event--urgent {
    background: rgba(239, 68, 68, 0.1);
    color: var(--color-danger);
  }

  .cal-event--deal {
    background: rgba(249, 115, 22, 0.1);
    color: var(--brand-500);
    cursor: pointer;
  }

  .cal-event--deal:hover {
    background: rgba(249, 115, 22, 0.2);
  }

  .cal-event--credit {
    background: rgba(16, 185, 129, 0.1);
    color: #10b981;
  }

  .cal-event--credit.cal-event--urgent {
    background: rgba(239, 68, 68, 0.1);
    color: var(--color-danger);
  }

  .cal-event--expired {
    opacity: 0.45;
  }

  .cal-event--more {
    color: var(--text-muted);
    cursor: pointer;
    font-size: var(--text-xs);
    padding: 2px 5px;
  }

  .cal-event--more:hover {
    color: var(--text-secondary);
  }

  /* Day popover for "+N more" */
  .cal-day-popover-backdrop {
    position: fixed;
    inset: 0;
    z-index: 99;
  }

  .cal-day-popover {
    z-index: 100;
    width: 312px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-card);
    box-shadow: var(--shadow-card);
    overflow: hidden;
  }

  .cal-popover-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border);
    font-size: var(--text-sm);
    font-weight: var(--weight-semibold);
    color: var(--color-text);
  }

  .cal-popover-close {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: var(--text-base);
  }

  .cal-popover-close:hover {
    background: var(--bg-sunken);
  }

  .cal-popover-list {
    padding: var(--space-2) 0;
    max-height: 200px;
    overflow-y: auto;
  }

  .cal-popover-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-4);
  }

  .cal-popover-item--deal {
    cursor: pointer;
  }

  .cal-popover-item--deal:hover {
    background: var(--bg-sunken);
  }

  .cal-popover-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
  }

  .cal-popover-label {
    flex: 1;
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--color-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .cal-popover-meta {
    font-size: var(--text-xs);
    color: var(--text-muted);
    flex-shrink: 0;
  }

  .cal-cell--event {
    background: transparent;
  }

  .cal-cell--today {
    background: var(--brand-500) !important;
    border-radius: var(--radius-md);
  }

  .cal-cell--today .cal-day {
    color: var(--surface-50) !important;
    font-weight: 800;
  }

  /* Selected day (desktop) — coral outline */
  .cal-cell--selected {
    outline: 2px solid var(--brand-500);
    outline-offset: -2px;
    border-radius: var(--radius-md);
  }

  /* Today + selected doesn't need double emphasis */
  .cal-cell--today.cal-cell--selected {
    outline: none;
  }

  /* Event labels on today's coral cell — solid white bubble with coral text */
  .cal-cell--today .cal-event--deal {
    background-color: #ffffff !important;
    background: #ffffff !important;
    color: var(--brand-500) !important;
    font-weight: var(--weight-semibold) !important;
    opacity: 1 !important;
  }
  .cal-cell--today .cal-event--benefit {
    background-color: #ffffff !important;
    background: #ffffff !important;
    color: var(--brand-500) !important;
    font-weight: var(--weight-semibold) !important;
    opacity: 1 !important;
  }
  .cal-cell--today .cal-event--credit {
    background-color: #ffffff !important;
    background: #ffffff !important;
    color: var(--brand-500) !important;
    font-weight: var(--weight-semibold) !important;
    opacity: 1 !important;
  }

  /* ==========================================
     MOBILE COMPACT CALENDAR
     Small cells with dots, tap day to see events
     ========================================== */

  @media (max-width: 768px) {
    .calendar-grid-cells {
      min-height: auto;
    }
    .cal-cell {
      min-height: 52px;
      padding: var(--space-1);
      align-items: center;
      cursor: pointer;
    }
    .cal-cell:active {
      background: var(--surface-100);
    }
    .cal-cell--selected {
      background: var(--surface-100);
      outline: 2px solid var(--brand-500);
      outline-offset: -2px;
    }
    /* Today styling on mobile — coral ring, not filled */
    .cal-cell--today {
      background: transparent;
      outline: 2px solid var(--brand-500);
      outline-offset: -2px;
    }
    .cal-cell--today .cal-day {
      color: var(--brand-500);
      font-weight: 800;
    }
    .cal-cell--today.cal-cell--selected {
      background: var(--surface-100);
    }
    .cal-cell--today.cal-cell--selected .cal-day {
      color: var(--brand-500);
    }
    .cal-day { font-size: var(--text-xs); margin-bottom: 2px; }

    /* Hide text labels — show dots only (non-interactive on mobile) */
    .cal-event {
      font-size: 0;
      padding: 0;
      width: 6px;
      height: 6px;
      min-height: 0;
      border-radius: 50%;
      display: inline-block;
      margin: 0 1px;
      overflow: hidden;
      line-height: 0;
      flex-shrink: 0;
      pointer-events: none;
      cursor: default;
    }
    .cal-event--deal { background: var(--brand-500); color: transparent; }
    .cal-event--benefit { background: var(--accent-blue-500); color: transparent; }
    .cal-event--urgent { background: var(--color-danger); color: transparent; }
    .cal-event--expired { background: var(--surface-400); color: transparent; }
    
    /* White dots on today's coral cell for contrast */
    .cal-cell--today .cal-event--deal,
    .cal-cell--today .cal-event--benefit,
    .cal-cell--today .cal-event--credit,
    .cal-cell--today .cal-event--expired {
      background: rgba(255, 255, 255, 0.9) !important;
    }
    .cal-event--more {
      background: var(--surface-300);
      color: transparent;
    }

    /* Dot row — horizontal center */
    .cal-cell .cal-day ~ .cal-event {
      vertical-align: middle;
    }

    /* Selected day event list below grid */
    .cal-day-detail {
      display: flex;
      flex-direction: column;
      gap: var(--space-2);
      padding: var(--space-4) 0;
      border-top: 1px solid var(--border);
      margin-top: var(--space-3);
    }
    .cal-day-detail-header {
      font-size: var(--text-sm);
      font-weight: var(--weight-bold);
      color: var(--text);
      margin-bottom: var(--space-1);
    }
    .cal-day-detail-item {
      display: flex;
      align-items: center;
      gap: var(--space-3);
      padding: var(--space-3);
      background: var(--bg-elevated);
      border-radius: var(--radius-md);
    }
    .cal-day-detail-dot {
      width: 8px;
      height: 8px;
      border-radius: 50%;
      flex-shrink: 0;
    }
    .cal-day-detail-text {
      flex: 1;
      min-width: 0;
    }
    .cal-day-detail-title {
      font-size: var(--text-sm);
      font-weight: var(--weight-medium);
      color: var(--text);
      line-height: 1.3;
    }
    .cal-day-detail-meta {
      font-size: var(--text-xs);
      color: var(--text-muted);
    }
    .cal-day-detail-empty {
      font-size: var(--text-sm);
      color: var(--text-muted);
      text-align: center;
      padding: var(--space-4);
    }
  }

  /* Force calendar stat widgets to stay on one row on mobile */
  #calendar-content .widget-grid-3 {
    grid-template-columns: repeat(3, 1fr);
    gap: var(--space-3);
  }

  @media (max-width: 480px) {
    .cal-cell {
      min-height: 46px;
    }
    #calendar-content .widget-grid-3 {
      grid-template-columns: repeat(3, 1fr);
      gap: var(--space-2);
    }
    #calendar-content .stat-widget {
      padding: var(--space-3);
    }
    #calendar-content .stat-widget-label {
      font-size: 0.6rem;
    }
    #calendar-content .stat-widget-value {
      font-size: var(--text-lg);
    }
    .calendar-timeline-item {
      padding: var(--space-3);
    }
    .calendar-timeline-date {
      font-size: var(--text-xs);
      min-width: 48px;
    }
    .calendar-header {
      flex-direction: column;
      align-items: flex-start;
      gap: var(--space-3);
    }
  }

  /* ==========================================
     TIMELINE
     ========================================== */
  .calendar-timeline {
    position: relative;
    padding-left: var(--space-12);
  }

  .calendar-timeline::before {
    content: '';
    position: absolute;
    left: 27px;
    top: 0;
    bottom: 0;
    width: 2px;
    background: var(--border);
  }

  .timeline-event {
    position: relative;
    margin-bottom: var(--space-5);
  }

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

  .timeline-date {
    position: absolute;
    left: calc(var(--space-12) * -1);
    width: 56px;
    text-align: center;
  }

  .timeline-date-badge {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    background: var(--bg-elevated);
    border-radius: var(--radius-card);
    box-shadow: var(--shadow-sm);
    position: relative;
    z-index: 1;
  }

  .timeline-date-month {
    font-size: 10px;
    font-weight: var(--weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
  }

  .timeline-date-day {
    font-family: var(--font-body);
    font-size: var(--text-xl);
    font-weight: 800;
    color: var(--text);
    line-height: 1;
  }

  .timeline-event.urgent .timeline-date-badge { background: var(--warning); }
  .timeline-event.urgent .timeline-date-month,
  .timeline-event.urgent .timeline-date-day { color: var(--text-inverse); }
  .timeline-event.overdue .timeline-date-badge { background: var(--danger); }
  .timeline-event.overdue .timeline-date-month,
  .timeline-event.overdue .timeline-date-day { color: var(--text-inverse); }

  .timeline-content {
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.5);
    border-radius: var(--radius-card);
    padding: var(--space-5);
    box-shadow: var(--shadow-card), inset 0 1px 0 rgba(255, 255, 255, 0.6);
    margin-left: var(--space-3);
  }

  .timeline-event.urgent .timeline-content { border-left: 4px solid var(--warning); }
  .timeline-event.overdue .timeline-content { border-left: 4px solid var(--danger); }

  .timeline-title {
    font-size: var(--text-base);
    font-weight: var(--weight-bold);
    color: var(--text);
    margin-bottom: var(--space-2);
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }

  .timeline-description {
    font-size: var(--text-sm);
    color: var(--text-secondary);
    line-height: var(--leading-relaxed);
    margin-bottom: var(--space-3);
  }

  .timeline-card-chip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: var(--bg-sunken);
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    color: var(--text-secondary);
  }

  .timeline-card-swatch {
    width: 20px;
    height: 12px;
    border-radius: 2px;
  }

  /* ==========================================
     DEAL TIMELINE EVENTS
     ========================================== */
  .timeline-event--deal .timeline-content {
    border-left: 4px solid var(--brand-500);
  }

  .timeline-event--expired {
    opacity: 0.45;
  }

  /* ==========================================
     DEAL DEADLINE CARDS
     ========================================== */
  .deadline-card--deal {
    background: rgba(235, 68, 37, 0.05);
    border-left: 3px solid var(--brand-500);
    cursor: pointer;
  }

  .deadline-card--deal:hover {
    background: rgba(235, 68, 37, 0.08);
  }

  .deadline-card--deal .deadline-card-icon {
    background: rgba(235, 68, 37, 0.1);
    color: var(--brand-500);
  }

  .deadline-card--deal .deadline-card-days {
    color: var(--brand-500);
  }

  .deadline-card--expired {
    opacity: 0.45;
  }

  /* ==========================================
     DEAL DEADLINE MODAL
     ========================================== */
  .deal-modal-card-header {
    padding: var(--space-5) var(--space-5) var(--space-4);
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-height: 72px;
    justify-content: flex-end;
    position: relative;
    overflow: hidden;
  }

  .deal-modal-card-name {
    font-size: var(--text-base);
    font-weight: var(--weight-bold);
    color: rgba(255, 255, 255, 0.95);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    position: relative;
    z-index: 1;
  }

  .deal-modal-card-issuer {
    font-size: var(--text-xs);
    color: rgba(255, 255, 255, 0.65);
    font-weight: var(--weight-medium);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
    position: relative;
    z-index: 1;
  }

  .deal-modal .modal-body {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
  }

  .deal-modal-type-label {
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--brand-500);
  }

  .deal-modal-title {
    font-size: var(--text-lg);
    font-weight: var(--weight-bold);
    color: var(--color-text);
    line-height: var(--leading-tight);
  }

  .deal-modal-rate {
    font-family: var(--font-display);
    font-size: var(--text-3xl);
    font-weight: var(--weight-extrabold);
    color: var(--color-text);
    line-height: 1;
  }

  .deal-modal-deadline {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--text-secondary);
    padding: var(--space-3) 0;
    border-top: 1px solid var(--border);
  }

  .deal-modal-cta {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-3) var(--space-5);
    background: var(--brand-500);
    color: var(--text-inverse);
    border-radius: var(--radius-md);
    font-size: var(--text-sm);
    font-weight: var(--weight-semibold);
    text-decoration: none;
    transition: background var(--transition-base);
    margin-top: var(--space-1);
  }

  .deal-modal-cta:hover {
    background: var(--brand-600);
  }

  .deal-modal-close {
    position: absolute;
    top: var(--space-3);
    right: var(--space-3);
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: rgba(255, 255, 255, 0.9);
    cursor: pointer;
    transition: background var(--transition-base);
    z-index: 1;
  }

  .deal-modal-close:hover {
    background: rgba(255, 255, 255, 0.3);
  }

  /* ==========================================
     EXPORT BUTTON
     ========================================== */
  .calendar-export {
    margin-top: var(--space-6);
    padding: var(--space-5);
    background: var(--bg-elevated);
    border-radius: var(--radius-card);
    text-align: center;
  }

  .calendar-export p {
    margin-bottom: var(--space-4);
  }

  /* ==========================================
     EMPTY STATE
     ========================================== */
  .calendar-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: var(--space-12) var(--space-5);
  }

  .calendar-empty-icon {
    display: flex;
    justify-content: center;
    margin-bottom: var(--space-4);
  }

  .calendar-empty h3 {
    font-size: var(--text-lg);
    margin-bottom: var(--space-2);
  }

  .calendar-empty p {
    color: var(--text-muted);
    margin-bottom: var(--space-5);
  }

  /* ==========================================
     DAYS UNTIL BADGE
     ========================================== */
  .days-until {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
  }

  .days-until.normal { background: var(--bg-sunken); color: var(--text-muted); }
  .days-until.soon { background: var(--warning-light); color: var(--warning); }
  .days-until.urgent { background: var(--danger-light); color: var(--danger); }

  /* Touch target compliance (44px minimum) */
  @media (pointer: coarse) {
    .calendar-day {
      min-height: 44px;
    }
    .deal-modal-close {
      min-width: 44px;
      min-height: 44px;
    }
  }

}
