/* === Reset & Variables === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --white:      #ffffff;
  --gray-50:    #F9FAFB;
  --gray-100:   #F3F4F6;
  --gray-200:   #E5E7EB;
  --gray-400:   #9CA3AF;
  --gray-600:   #4B5563;
  --gray-900:   #111827;

  /* Feed colors */
  --fred-custody-bg:      #DBEAFE;
  --fred-custody-chip:    #1D4ED8;
  --charissa-custody-bg:  #DCFCE7;
  --charissa-custody-chip:#15803D;
  --outlook-bg:           #1E3A5F;
  --outlook-text:         #ffffff;
  --tentative-bg:         #FEF3C7;
  --tentative-accent:     #B45309;

  /* Today highlight */
  --today-ring:   #EF4444;
  --today-dot-bg: #EF4444;
  --today-dot-fg: #ffffff;

  --radius:    6px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.07);
  --shadow:    0 1px 3px rgba(0,0,0,0.10), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.14);
}

/* === Base === */
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--gray-50);
  color: var(--gray-900);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* === Header === */
header {
  background: var(--white);
  border-bottom: 1px solid var(--gray-200);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow-sm);
}

.header-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0.625rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.header-title {
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--gray-900);
  flex: 1;
  min-width: 100px;
  letter-spacing: -0.02em;
}

.nav-controls {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

#current-period {
  font-size: 0.9375rem;
  font-weight: 600;
  min-width: 170px;
  text-align: center;
  color: var(--gray-900);
}

/* === Buttons === */
button { font-family: inherit; cursor: pointer; border: none; background: none; }

.btn {
  padding: 0.375rem 0.75rem;
  border-radius: var(--radius);
  font-size: 0.8125rem;
  font-weight: 500;
  border: 1px solid var(--gray-200);
  background: var(--white);
  color: var(--gray-600);
  transition: background 0.12s, border-color 0.12s;
  line-height: 1.5;
}
.btn:hover { background: var(--gray-100); border-color: var(--gray-400); }

.btn-nav { padding: 0.375rem 0.6rem; font-size: 1.125rem; line-height: 1; }

.view-toggle {
  display: flex;
  background: var(--gray-100);
  border-radius: var(--radius);
  padding: 2px;
  gap: 2px;
}

.view-btn {
  padding: 0.3rem 0.7rem;
  border-radius: 4px;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--gray-600);
  transition: background 0.12s, color 0.12s, box-shadow 0.12s;
}
.view-btn.active {
  background: var(--white);
  color: var(--gray-900);
  box-shadow: var(--shadow-sm);
}

/* === Loading bar === */
#loading-bar {
  height: 3px;
  background: linear-gradient(90deg, #1D4ED8 0%, #60A5FA 50%, #1D4ED8 100%);
  background-size: 200% 100%;
  animation: shimmer 1.2s linear infinite;
}
@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* === Calendar container === */
#calendar-container {
  max-width: 1280px;
  margin: 1rem auto 0;
  padding: 0 1rem;
}

/* === Month grid === */
.month-grid {
  display: grid;
  /* Fix #1: minmax(0,1fr) lets columns shrink below content width so they
     never overflow the container and get clipped by border-radius */
  grid-template-columns: repeat(7, minmax(0, 1fr));
  background: var(--gray-200);
  gap: 1px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  width: 100%;
}

.dow-header {
  background: var(--white);
  padding: 0.4rem 0.25rem;
  text-align: center;
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--gray-400);
}

.day-cell {
  background: var(--white);
  min-height: 108px;
  padding: 4px;
  position: relative;
  transition: background 0.1s;
}
.day-cell.other-month { background: var(--gray-50); }
.day-cell.today       { outline: 2px solid var(--today-ring); outline-offset: -2px; }

/* Custody day-cell background tints */
.day-cell.bg-fred_custody     { background-color: var(--fred-custody-bg); }
.day-cell.bg-charissa_custody { background-color: var(--charissa-custody-bg); }

.cell-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 3px;
}

.day-num {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--gray-600);
  width: 1.5rem;
  height: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  flex-shrink: 0;
}
.day-cell.today .day-num {
  background: var(--today-dot-bg);
  color: var(--today-dot-fg);
  font-weight: 700;
}
.day-cell.other-month .day-num { color: var(--gray-400); }

/* + add button (appears on hover / always on touch) */
.add-btn {
  opacity: 0;
  width: 1.375rem;
  height: 1.375rem;
  border-radius: 50%;
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--gray-400);
  transition: opacity 0.12s, background 0.12s, color 0.12s;
  flex-shrink: 0;
  line-height: 1;
}
.day-cell:hover .add-btn,
.week-col:hover .add-btn  { opacity: 1; }
.add-btn:hover { background: var(--gray-200); color: var(--gray-900); }

@media (hover: none) { .add-btn { opacity: 1; } }

.cell-events {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* === Event chips === */
.event-chip {
  font-size: 0.6875rem;
  font-weight: 500;
  padding: 2px 5px;
  border-radius: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: default;
  display: flex;
  align-items: center;
  gap: 3px;
  user-select: none;
}

.event-chip.feed-fred_custody {
  background: var(--fred-custody-chip);
  color: var(--white);
}
.event-chip.feed-charissa_custody {
  background: var(--charissa-custody-chip);
  color: var(--white);
}
.event-chip.feed-fred_outlook {
  background: var(--outlook-bg);
  color: var(--outlook-text);
}
.event-chip.feed-tentative {
  background: var(--tentative-bg);
  color: var(--tentative-accent);
  border: 1.5px dashed var(--tentative-accent);
  cursor: pointer;
}
.event-chip.feed-tentative:hover { opacity: 0.8; }

.chip-time {
  font-weight: 400;
  font-size: 0.625rem;
  opacity: 0.8;
  flex-shrink: 0;
}

.more-badge {
  font-size: 0.625rem;
  color: var(--gray-400);
  padding-left: 4px;
  cursor: pointer;
  user-select: none;
}
.more-badge:hover { color: var(--gray-600); text-decoration: underline; }

/* === Week grid === */
.week-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  background: var(--gray-200);
  gap: 1px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 420px;
  box-shadow: var(--shadow-sm);
}

.week-col {
  background: var(--white);
  padding: 0.5rem 0.375rem;
  min-height: 200px;
}
.week-col.today       { outline: 2px solid var(--today-ring); outline-offset: -2px; }
.week-col.bg-fred_custody     { background-color: var(--fred-custody-bg); }
.week-col.bg-charissa_custody { background-color: var(--charissa-custody-bg); }

.week-col-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding-bottom: 0.5rem;
  margin-bottom: 0.375rem;
  border-bottom: 1px solid var(--gray-200);
}

.week-dow {
  font-size: 0.625rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--gray-400);
}

.week-day-num {
  font-size: 1.25rem;
  font-weight: 400;
  color: var(--gray-600);
  width: 2rem;
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}
.week-day-num.today-badge {
  background: var(--today-dot-bg);
  color: var(--today-dot-fg);
  font-weight: 700;
}

.week-events {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.week-col .event-chip {
  font-size: 0.75rem;
  padding: 3px 6px;
  white-space: normal;
  word-break: break-word;
}

/* Custody label in week view */
.custody-label {
  font-size: 0.6875rem;
  font-weight: 600;
  padding: 3px 6px;
  border-radius: 3px;
  margin-bottom: 2px;
}
.custody-label-fred_custody {
  background: var(--fred-custody-chip);
  color: var(--white);
}
.custody-label-charissa_custody {
  background: var(--charissa-custody-chip);
  color: var(--white);
}

/* === Legend === */
.legend {
  max-width: 1280px;
  margin: 0.875rem auto 2rem;
  padding: 0 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.875rem;
  align-items: center;
}

.legend-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--gray-400);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.8125rem;
  color: var(--gray-600);
}

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 2px;
  flex-shrink: 0;
}
.swatch-fred-custody     { background: var(--fred-custody-chip); }
.swatch-charissa-custody { background: var(--charissa-custody-chip); }
.swatch-outlook          { background: var(--outlook-bg); }
.swatch-tentative        { background: var(--tentative-bg); border: 1.5px dashed var(--tentative-accent); }

/* === Modal === */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
#modal-overlay.hidden { display: none !important; }

#modal {
  background: var(--white);
  border-radius: 12px;
  padding: 1.5rem;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow-lg);
}

#modal h2 {
  font-size: 1.125rem;
  font-weight: 700;
  margin-bottom: 1.25rem;
  color: var(--gray-900);
}

.form-group { margin-bottom: 1rem; }

.form-group label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--gray-600);
  margin-bottom: 0.3rem;
}
.label-hint { font-weight: 400; color: var(--gray-400); }

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  font-size: 0.9375rem;
  font-family: inherit;
  color: var(--gray-900);
  background: var(--white);
  transition: border-color 0.12s, box-shadow 0.12s;
}
.form-group input:focus,
.form-group textarea:focus {
  outline: none;
  border-color: #1D4ED8;
  box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.15);
}
.form-group textarea { resize: vertical; min-height: 76px; }

.modal-footer {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 1.25rem;
  flex-wrap: wrap;
  align-items: center;
}

.btn-primary {
  padding: 0.5rem 1.25rem;
  background: #1D4ED8;
  color: var(--white);
  border-radius: var(--radius);
  font-size: 0.9375rem;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition: background 0.12s;
}
.btn-primary:hover { background: #1E40AF; }

.btn-ghost {
  padding: 0.5rem 1rem;
  background: none;
  color: var(--gray-600);
  border-radius: var(--radius);
  font-size: 0.9375rem;
  border: 1px solid var(--gray-200);
  cursor: pointer;
  transition: background 0.12s;
}
.btn-ghost:hover { background: var(--gray-100); }

.btn-danger {
  padding: 0.5rem 1rem;
  background: none;
  color: #DC2626;
  border-radius: var(--radius);
  font-size: 0.9375rem;
  border: 1px solid #FCA5A5;
  cursor: pointer;
  margin-right: auto;
  transition: background 0.12s;
}
.btn-danger:hover { background: #FEF2F2; }

/* === Day detail modal (Fix #4) === */
#day-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
#day-modal-overlay.hidden { display: none !important; }

#day-modal {
  background: var(--white);
  border-radius: 12px;
  padding: 1.5rem;
  width: 100%;
  max-width: 420px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}

.dm-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--gray-200);
}

#day-modal-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--gray-900);
  line-height: 1.3;
}

.btn-icon-close {
  font-size: 0.875rem;
  color: var(--gray-400);
  padding: 0.25rem 0.375rem;
  border-radius: 4px;
  flex-shrink: 0;
  transition: background 0.12s, color 0.12s;
}
.btn-icon-close:hover { background: var(--gray-100); color: var(--gray-900); }

.dm-event {
  display: flex;
  align-items: flex-start;
  gap: 0.625rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--gray-100);
}
.dm-event:last-child { border-bottom: none; }

.dm-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  margin-top: 4px;
}
.dm-dot-fred_custody     { background: var(--fred-custody-chip); }
.dm-dot-charissa_custody { background: var(--charissa-custody-chip); }
.dm-dot-fred_outlook     { background: var(--outlook-bg); }
.dm-dot-tentative        { background: var(--tentative-accent); }

.dm-info { flex: 1; min-width: 0; }

.dm-title {
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--gray-900);
  word-break: break-word;
}

.dm-time {
  font-size: 0.8125rem;
  color: var(--gray-400);
  margin-top: 1px;
}

.dm-note {
  font-size: 0.8125rem;
  color: var(--gray-600);
  font-style: italic;
  margin-top: 3px;
}

.dm-empty {
  text-align: center;
  color: var(--gray-400);
  font-size: 0.9375rem;
  padding: 1.5rem 0;
}

/* === Utilities === */
.hidden { display: none !important; }

/* === Mobile === */
@media (max-width: 768px) {
  .header-title { font-size: 1rem; }
  #current-period { min-width: 130px; font-size: 0.875rem; }

  .day-cell { min-height: 72px; padding: 2px; }
  .day-num  { font-size: 0.75rem; width: 1.25rem; height: 1.25rem; }
  .event-chip { font-size: 0.5625rem; padding: 1px 3px; }
  .more-badge { font-size: 0.5625rem; }

  /* Week grid scrolls horizontally on small screens */
  .week-grid {
    overflow-x: auto;
    grid-template-columns: repeat(7, minmax(90px, 1fr));
  }

  .legend { gap: 0.5rem; }
  .legend-item { font-size: 0.75rem; }
}

@media (max-width: 480px) {
  .header-inner { gap: 0.5rem; }
  .btn { padding: 0.3rem 0.5rem; font-size: 0.75rem; }
  .btn-nav { padding: 0.3rem 0.5rem; }
}
