/* ==========================================================================
   Calendar Styles
   FullCalendar theming, event type colors, legend, popover, filters,
   mobile responsive, and dark mode support.
   All values reference --ds-* design tokens.
   ========================================================================== */

/* --------------------------------------------------------------------------
   Calendar event type color tokens (light mode)
   -------------------------------------------------------------------------- */
:root {
  --cal-delivery: var(--ds-success);
  --cal-delivery-subtle: var(--ds-success-subtle);
  --cal-installation: var(--ds-brand-primary);
  --cal-installation-subtle: var(--ds-brand-primary-subtle);
  --cal-estimated: var(--ds-gray-500);
  --cal-estimated-subtle: rgba(107, 114, 128, 0.1);
  --cal-po: var(--ds-warning);
  --cal-po-subtle: var(--ds-warning-subtle);
}

[data-bs-theme="dark"] {
  --cal-delivery: var(--ds-success);
  --cal-delivery-subtle: var(--ds-success-subtle);
  --cal-installation: var(--ds-brand-primary);
  --cal-installation-subtle: var(--ds-brand-primary-subtle);
  --cal-estimated: var(--ds-gray-500);
  --cal-estimated-subtle: rgba(156, 163, 175, 0.15);
  --cal-po: var(--ds-warning);
  --cal-po-subtle: var(--ds-warning-subtle);
}

/* --------------------------------------------------------------------------
   Calendar Container
   -------------------------------------------------------------------------- */
.cal-wrapper {
  display: flex;
  gap: var(--ds-space-4);
}

.cal-main {
  flex: 1;
  min-width: 0;
}

.cal-card {
  background: var(--ds-surface-card);
  border: 1px solid var(--ds-border-default);
  border-radius: var(--ds-radius-lg);
  box-shadow: var(--ds-shadow-sm);
  padding: var(--ds-space-4);
}

/* --------------------------------------------------------------------------
   FullCalendar Global Theme Overrides
   -------------------------------------------------------------------------- */
.fc {
  --fc-border-color: var(--ds-border-default);
  --fc-page-bg-color: var(--ds-surface-card);
  --fc-neutral-bg-color: var(--ds-surface-sunken);
  --fc-neutral-text-color: var(--ds-text-muted);
  --fc-today-bg-color: var(--ds-brand-primary-subtle);
  --fc-event-border-color: transparent;
  --fc-button-text-color: var(--ds-text-primary);
  --fc-button-bg-color: var(--ds-surface-raised);
  --fc-button-border-color: var(--ds-border-default);
  --fc-button-hover-bg-color: var(--ds-gray-100);
  --fc-button-hover-border-color: var(--ds-border-strong);
  --fc-button-active-bg-color: var(--ds-brand-primary);
  --fc-button-active-border-color: var(--ds-brand-primary);
  --fc-list-event-hover-bg-color: var(--ds-brand-primary-subtle);
  font-family: var(--ds-font-family);
  font-size: var(--ds-font-size-sm);
}

/* Header toolbar */
.fc .fc-toolbar-title {
  font-size: var(--ds-font-size-xl);
  font-weight: var(--ds-font-weight-semibold);
  color: var(--ds-text-primary);
}

/* Navigation and view buttons */
.fc .fc-button {
  font-size: var(--ds-font-size-sm);
  font-weight: var(--ds-font-weight-medium);
  border-radius: var(--ds-radius-md);
  padding: var(--ds-space-1-5) var(--ds-space-3);
  transition: var(--ds-transition-fast);
  text-transform: capitalize;
}

.fc .fc-button:focus {
  box-shadow: 0 0 0 0.2rem var(--ds-brand-primary-subtle);
}

.fc .fc-button-primary:not(:disabled).fc-button-active,
.fc .fc-button-primary:not(:disabled):active {
  background-color: var(--ds-brand-primary);
  border-color: var(--ds-brand-primary);
  color: var(--ds-text-on-primary);
}

/* Today button */
.fc .fc-today-button {
  background-color: var(--ds-brand-primary);
  border-color: var(--ds-brand-primary);
  color: var(--ds-text-on-primary);
  font-weight: var(--ds-font-weight-semibold);
}

.fc .fc-today-button:hover:not(:disabled) {
  background-color: var(--ds-brand-primary-hover);
  border-color: var(--ds-brand-primary-hover);
}

.fc .fc-today-button:disabled {
  opacity: 0.5;
}

/* Day headers */
.fc .fc-col-header-cell {
  background-color: var(--ds-surface-sunken);
  font-weight: var(--ds-font-weight-semibold);
  color: var(--ds-text-secondary);
  padding: var(--ds-space-2) 0;
  font-size: var(--ds-font-size-xs);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Day cells */
.fc .fc-daygrid-day-number {
  color: var(--ds-text-secondary);
  font-weight: var(--ds-font-weight-medium);
  padding: var(--ds-space-1) var(--ds-space-2);
  font-size: var(--ds-font-size-sm);
}

/* Today indicator */
.fc .fc-day-today {
  background-color: var(--ds-brand-primary-subtle) !important;
}

.fc .fc-day-today .fc-daygrid-day-number {
  color: var(--ds-brand-primary);
  font-weight: var(--ds-font-weight-bold);
  position: relative;
}

.fc .fc-day-today .fc-daygrid-day-number::after {
  content: '';
  display: block;
  width: 6px;
  height: 6px;
  background-color: var(--ds-brand-primary);
  border-radius: var(--ds-radius-full);
  margin: 2px auto 0;
}

/* More events popover */
.fc .fc-more-popover {
  background: var(--ds-surface-overlay);
  border: 1px solid var(--ds-border-default);
  border-radius: var(--ds-radius-lg);
  box-shadow: var(--ds-shadow-lg);
}

.fc .fc-more-popover .fc-popover-header {
  background: var(--ds-surface-sunken);
  color: var(--ds-text-primary);
  font-weight: var(--ds-font-weight-semibold);
  border-radius: var(--ds-radius-lg) var(--ds-radius-lg) 0 0;
  padding: var(--ds-space-2) var(--ds-space-3);
}

/* List view */
.fc .fc-list {
  border-color: var(--ds-border-default);
}

.fc .fc-list-day-cushion {
  background-color: var(--ds-surface-sunken);
  color: var(--ds-text-primary);
  font-weight: var(--ds-font-weight-semibold);
}

.fc .fc-list-event td {
  border-color: var(--ds-border-default);
}

/* --------------------------------------------------------------------------
   Event Type Colors
   -------------------------------------------------------------------------- */
.cal-event {
  border-radius: var(--ds-radius-sm);
  padding: 1px var(--ds-space-1);
  font-size: var(--ds-font-size-xs);
  font-weight: var(--ds-font-weight-medium);
  cursor: pointer;
  transition: var(--ds-transition-fast);
  border-left: 3px solid transparent;
}

.cal-event:hover {
  filter: brightness(1.1);
  box-shadow: var(--ds-shadow-sm);
}

.cal-event--delivery_date {
  background-color: var(--cal-delivery-subtle);
  color: var(--cal-delivery);
  border-left-color: var(--cal-delivery);
}

.cal-event--installation_date {
  background-color: var(--cal-installation-subtle);
  color: var(--cal-installation);
  border-left-color: var(--cal-installation);
}

.cal-event--estimated_completion {
  background-color: var(--cal-estimated-subtle);
  color: var(--cal-estimated);
  border-left-color: var(--cal-estimated);
}

.cal-event--po_delivery {
  background-color: var(--cal-po-subtle);
  color: var(--cal-po);
  border-left-color: var(--cal-po);
}

/* List view event dots */
.fc-list-event .fc-list-event-dot {
  border-radius: var(--ds-radius-full);
}

.cal-event--delivery_date .fc-list-event-dot,
.fc-list-event.cal-event--delivery_date .fc-list-event-dot {
  border-color: var(--cal-delivery);
}

.cal-event--installation_date .fc-list-event-dot,
.fc-list-event.cal-event--installation_date .fc-list-event-dot {
  border-color: var(--cal-installation);
}

.cal-event--estimated_completion .fc-list-event-dot,
.fc-list-event.cal-event--estimated_completion .fc-list-event-dot {
  border-color: var(--cal-estimated);
}

.cal-event--po_delivery .fc-list-event-dot,
.fc-list-event.cal-event--po_delivery .fc-list-event-dot {
  border-color: var(--cal-po);
}

/* --------------------------------------------------------------------------
   Event Type Legend
   -------------------------------------------------------------------------- */
.cal-legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--ds-space-3);
  padding: var(--ds-space-3) var(--ds-space-4);
  background: var(--ds-surface-card);
  border: 1px solid var(--ds-border-default);
  border-radius: var(--ds-radius-lg);
  margin-bottom: var(--ds-space-4);
}

.cal-legend-item {
  display: flex;
  align-items: center;
  gap: var(--ds-space-1-5);
  font-size: var(--ds-font-size-sm);
  color: var(--ds-text-secondary);
  cursor: pointer;
  padding: var(--ds-space-1) var(--ds-space-2);
  border-radius: var(--ds-radius-sm);
  transition: var(--ds-transition-fast);
  user-select: none;
}

.cal-legend-item:hover {
  background: var(--ds-surface-sunken);
}

.cal-legend-item.active {
  background: var(--ds-surface-sunken);
  font-weight: var(--ds-font-weight-semibold);
  color: var(--ds-text-primary);
}

.cal-legend-dot {
  width: 10px;
  height: 10px;
  border-radius: var(--ds-radius-full);
  flex-shrink: 0;
}

.cal-legend-dot--delivery_date { background-color: var(--cal-delivery); }
.cal-legend-dot--installation_date { background-color: var(--cal-installation); }
.cal-legend-dot--estimated_completion { background-color: var(--cal-estimated); }
.cal-legend-dot--po_delivery { background-color: var(--cal-po); }

/* --------------------------------------------------------------------------
   Filter Bar
   -------------------------------------------------------------------------- */
.cal-filters {
  display: flex;
  flex-wrap: wrap;
  gap: var(--ds-space-3);
  align-items: center;
  padding: var(--ds-space-3) var(--ds-space-4);
  background: var(--ds-surface-card);
  border: 1px solid var(--ds-border-default);
  border-radius: var(--ds-radius-lg);
  margin-bottom: var(--ds-space-4);
}

.cal-filter-label {
  font-size: var(--ds-font-size-sm);
  font-weight: var(--ds-font-weight-semibold);
  color: var(--ds-text-secondary);
  white-space: nowrap;
}

.cal-filter-label i {
  margin-right: var(--ds-space-1);
}

.cal-filters .form-select {
  max-width: 220px;
  font-size: var(--ds-font-size-sm);
  border-color: var(--ds-border-default);
  border-radius: var(--ds-radius-md);
}

.cal-filters .form-select:focus {
  border-color: var(--ds-brand-primary);
  box-shadow: 0 0 0 0.2rem var(--ds-brand-primary-subtle);
}

/* --------------------------------------------------------------------------
   Event Popover
   -------------------------------------------------------------------------- */
.cal-popover {
  position: absolute;
  z-index: var(--ds-z-popover);
  background: var(--ds-surface-overlay);
  border: 1px solid var(--ds-border-default);
  border-radius: var(--ds-radius-lg);
  box-shadow: var(--ds-shadow-xl);
  padding: 0;
  min-width: 280px;
  max-width: 340px;
  animation: calPopoverIn var(--ds-duration-normal) var(--ds-ease-out);
}

@keyframes calPopoverIn {
  from { opacity: 0; transform: translateY(-4px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.cal-popover-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--ds-space-3) var(--ds-space-4);
  border-bottom: 1px solid var(--ds-border-default);
}

.cal-popover-type {
  display: inline-flex;
  align-items: center;
  gap: var(--ds-space-1);
  font-size: var(--ds-font-size-xs);
  font-weight: var(--ds-font-weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: var(--ds-space-0-5) var(--ds-space-2);
  border-radius: var(--ds-radius-sm);
}

.cal-popover-type--delivery_date {
  background: var(--cal-delivery-subtle);
  color: var(--cal-delivery);
}
.cal-popover-type--installation_date {
  background: var(--cal-installation-subtle);
  color: var(--cal-installation);
}
.cal-popover-type--estimated_completion {
  background: var(--cal-estimated-subtle);
  color: var(--cal-estimated);
}
.cal-popover-type--po_delivery {
  background: var(--cal-po-subtle);
  color: var(--cal-po);
}

.cal-popover-close {
  background: none;
  border: none;
  color: var(--ds-text-muted);
  cursor: pointer;
  padding: var(--ds-space-1);
  border-radius: var(--ds-radius-sm);
  line-height: 1;
  font-size: 1.1rem;
  transition: var(--ds-transition-fast);
}

.cal-popover-close:hover {
  color: var(--ds-text-primary);
  background: var(--ds-surface-sunken);
}

.cal-popover-body {
  padding: var(--ds-space-3) var(--ds-space-4);
}

.cal-popover-title {
  font-size: var(--ds-font-size-md);
  font-weight: var(--ds-font-weight-semibold);
  color: var(--ds-text-primary);
  margin: 0 0 var(--ds-space-3) 0;
  line-height: var(--ds-line-height-tight);
}

.cal-popover-meta {
  display: flex;
  flex-direction: column;
  gap: var(--ds-space-2);
}

.cal-popover-row {
  display: flex;
  align-items: center;
  gap: var(--ds-space-2);
  font-size: var(--ds-font-size-sm);
  color: var(--ds-text-secondary);
}

.cal-popover-row i {
  width: 16px;
  text-align: center;
  color: var(--ds-text-muted);
  flex-shrink: 0;
}

.cal-popover-row .badge {
  font-size: var(--ds-font-size-xs);
}

.cal-popover-footer {
  padding: var(--ds-space-3) var(--ds-space-4);
  border-top: 1px solid var(--ds-border-default);
}

.cal-popover-footer .btn {
  font-size: var(--ds-font-size-sm);
  width: 100%;
}

/* --------------------------------------------------------------------------
   Quick-Create Modal Overrides
   -------------------------------------------------------------------------- */
.cal-quick-create .modal-header {
  border-bottom-color: var(--ds-border-default);
}

.cal-quick-create .modal-footer {
  border-top-color: var(--ds-border-default);
}

.cal-quick-create .form-label {
  font-size: var(--ds-font-size-sm);
  font-weight: var(--ds-font-weight-medium);
  color: var(--ds-text-secondary);
}

/* --------------------------------------------------------------------------
   View Switcher Enhancement
   -------------------------------------------------------------------------- */
.fc .fc-button-group {
  border-radius: var(--ds-radius-md);
  overflow: hidden;
}

.fc .fc-button-group .fc-button {
  border-radius: 0;
}

.fc .fc-button-group .fc-button:first-child {
  border-radius: var(--ds-radius-md) 0 0 var(--ds-radius-md);
}

.fc .fc-button-group .fc-button:last-child {
  border-radius: 0 var(--ds-radius-md) var(--ds-radius-md) 0;
}

/* --------------------------------------------------------------------------
   Dark Mode
   -------------------------------------------------------------------------- */
[data-bs-theme="dark"] .fc {
  --fc-page-bg-color: var(--ds-surface-card);
  --fc-neutral-bg-color: var(--ds-surface-sunken);
  --fc-neutral-text-color: var(--ds-text-muted);
  --fc-border-color: var(--ds-border-default);
  --fc-today-bg-color: var(--ds-brand-primary-subtle);
  --fc-button-text-color: var(--ds-text-primary);
  --fc-button-bg-color: var(--ds-surface-raised);
  --fc-button-border-color: var(--ds-border-default);
  --fc-button-hover-bg-color: var(--ds-surface-overlay);
  --fc-button-hover-border-color: var(--ds-border-strong);
  --fc-button-active-bg-color: var(--ds-brand-primary);
  --fc-button-active-border-color: var(--ds-brand-primary);
  --fc-list-event-hover-bg-color: var(--ds-brand-primary-subtle);
}

/* Ensure FC toolbar buttons (prev/next/today) have visible text and icon color in dark mode */
[data-bs-theme="dark"] .fc .fc-button {
  color: var(--ds-text-primary);
  background-color: var(--ds-surface-raised);
  border-color: var(--ds-border-default);
}

[data-bs-theme="dark"] .fc .fc-button:hover {
  color: var(--ds-text-primary);
  background-color: var(--ds-surface-overlay);
  border-color: var(--ds-border-strong);
}

[data-bs-theme="dark"] .fc .fc-button-primary:not(:disabled).fc-button-active,
[data-bs-theme="dark"] .fc .fc-button-primary:not(:disabled):active {
  color: var(--ds-text-on-primary);
  background-color: var(--ds-brand-primary);
  border-color: var(--ds-brand-primary);
}

[data-bs-theme="dark"] .fc .fc-today-button {
  color: var(--ds-text-on-primary);
  background-color: var(--ds-brand-primary);
  border-color: var(--ds-brand-primary);
}

[data-bs-theme="dark"] .fc .fc-toolbar-title {
  color: var(--ds-text-primary);
}

[data-bs-theme="dark"] .fc .fc-col-header-cell {
  background-color: var(--ds-surface-sunken);
  color: var(--ds-text-muted);
}

[data-bs-theme="dark"] .fc .fc-daygrid-day-number {
  color: var(--ds-text-secondary);
}

[data-bs-theme="dark"] .fc .fc-day-today .fc-daygrid-day-number {
  color: var(--ds-brand-primary);
}

[data-bs-theme="dark"] .fc .fc-list-day-cushion {
  background-color: var(--ds-surface-sunken);
  color: var(--ds-text-primary);
}

[data-bs-theme="dark"] .fc .fc-list-event td {
  border-color: var(--ds-border-default);
}

[data-bs-theme="dark"] .fc .fc-more-popover {
  background: var(--ds-surface-overlay);
  border-color: var(--ds-border-default);
}

[data-bs-theme="dark"] .fc .fc-more-popover .fc-popover-header {
  background: var(--ds-surface-sunken);
  color: var(--ds-text-primary);
}

[data-bs-theme="dark"] .cal-popover {
  background: var(--ds-surface-overlay);
  border-color: var(--ds-border-default);
}

[data-bs-theme="dark"] .cal-popover-title {
  color: var(--ds-text-primary);
}

[data-bs-theme="dark"] .cal-popover-row {
  color: var(--ds-text-secondary);
}

[data-bs-theme="dark"] .cal-popover-row i {
  color: var(--ds-text-muted);
}

[data-bs-theme="dark"] .cal-legend {
  background: var(--ds-surface-card);
  border-color: var(--ds-border-default);
}

[data-bs-theme="dark"] .cal-legend-item {
  color: var(--ds-text-secondary);
}

[data-bs-theme="dark"] .cal-legend-item:hover {
  background: var(--ds-surface-raised);
}

[data-bs-theme="dark"] .cal-legend-item.active {
  background: var(--ds-surface-raised);
  color: var(--ds-text-primary);
}

[data-bs-theme="dark"] .cal-filters {
  background: var(--ds-surface-card);
  border-color: var(--ds-border-default);
}

[data-bs-theme="dark"] .cal-card {
  background: var(--ds-surface-card);
  border-color: var(--ds-border-default);
}

/* --------------------------------------------------------------------------
   Mobile Responsive
   -------------------------------------------------------------------------- */
@media (max-width: 767.98px) {
  .cal-wrapper {
    flex-direction: column;
  }

  .cal-filters {
    flex-direction: column;
    align-items: stretch;
  }

  .cal-filters .form-select {
    max-width: 100%;
  }

  .cal-legend {
    gap: var(--ds-space-2);
    padding: var(--ds-space-2) var(--ds-space-3);
  }

  .cal-legend-item {
    font-size: var(--ds-font-size-xs);
    padding: var(--ds-space-0-5) var(--ds-space-1);
  }

  /* FullCalendar mobile adjustments */
  .fc .fc-toolbar {
    flex-direction: column;
    gap: var(--ds-space-2);
  }

  .fc .fc-toolbar-title {
    font-size: var(--ds-font-size-lg);
  }

  .fc .fc-button {
    padding: var(--ds-space-1) var(--ds-space-2);
    font-size: var(--ds-font-size-xs);
  }

  /* Make events more touch-friendly */
  .cal-event {
    padding: var(--ds-space-1) var(--ds-space-1-5);
    min-height: 28px;
    display: flex;
    align-items: center;
  }

  /* Popover mobile: full-width at bottom */
  .cal-popover {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    max-width: 100%;
    min-width: auto;
    border-radius: var(--ds-radius-xl) var(--ds-radius-xl) 0 0;
    animation: calPopoverSlideUp var(--ds-duration-normal) var(--ds-ease-out);
  }

  @keyframes calPopoverSlideUp {
    from { opacity: 0; transform: translateY(100%); }
    to   { opacity: 1; transform: translateY(0); }
  }

  .cal-quick-create .modal-dialog {
    margin: 0;
    max-width: 100%;
    min-height: auto;
  }
}

/* Touch-friendly event clicking */
@media (pointer: coarse) {
  .cal-event {
    min-height: 32px;
    padding: var(--ds-space-1) var(--ds-space-2);
  }

  .fc .fc-daygrid-event {
    margin-bottom: 2px;
  }
}
