/* ── DividendIQ — Calendar Surface ──
   Phase A — calendar tab grid + list + day flyout.
   Tokens come from app.css (:root) + sectors.css. No hardcoded hex
   values here except WCAG-AA-validated chip text fallbacks for the
   light themes (per design § 5.4).

   Responsive breakpoints (design § 6.2):
     ≥ 1024 px       — 7-column grid, max 5 chips per cell
     640 – 1023 px   — 7-column grid, single-line chips
     <  640 px       — vertical week-stack (rows = weeks; horizontal scroll)

   Chip tap target (design § 5.4 NB-7):
     <  640 px       — min-width 44 px AND min-height 44 px (WCAG 2.5.5)
                       max-width 100% + ellipsis (NB-2 R4 — overflow ceiling)
   ────────────────────────────────────────── */

/* ── Calendar event tokens (consumed by chips). These mirror what
       app.css declares; declared here too so the file can ship standalone. */
.cal-view {
  --cal-grid-gap: 6px;
  --cal-cell-pad: 10px;
  --cal-cell-radius: 8px;
  --cal-chip-radius: 6px;
}

/* ── How-it-works panel (FR-C14) ── */
#calendar-view .diq-how-it-works {
  margin: 0 0 16px;
}

/* ── Toolbar ── */
.cal-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  margin: 12px 0 16px;
}

.cal-scope-segment {
  display: inline-flex;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}

.cal-scope-btn {
  border: 0;
  background: transparent;
  color: var(--text2);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 32px;
}

@media (hover: hover) {
  .cal-scope-btn:hover { color: var(--text); }
}

.cal-scope-btn[aria-selected="true"] {
  /* engagement fix-ticker-a11y-color-contrast-h1: switch hard-coded #fff
     to the canonical --on-accent token. White-on-accent fails WCAG AA
     for normal text on most accent colours (e.g. Obsidian #cc6b28 →
     3.68:1). --on-accent is per-theme-tuned for AA pass on var(--accent).
     #fff fallback preserves prior behaviour for any theme not yet
     defining the token (defensive; backfilled in :root + light defaults
     by this PR). */
  background: var(--accent);
  color: var(--on-accent, #fff);
}

.cal-month-nav {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.cal-month-nav button {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  min-width: 44px;
  min-height: 44px;
  padding: 0 14px;
  font: inherit;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}

.cal-prev,
.cal-next {
  width: 44px;
  padding: 0;
  flex: 0 0 44px;
}

@media (hover: hover) {
  .cal-month-nav button:hover { background: var(--surface3); }
}

.cal-month-label {
  font-weight: 700;
  font-size: 16px;
  min-width: 8em;
  text-align: center;
  color: var(--text);
}

.cal-today-btn {
  min-width: 72px;
  font-weight: 600;
}

@media (hover: hover) {
  .cal-today-btn:hover {
    color: var(--text);
    border-color: var(--border2);
  }
}

.cal-view-toggle {
  display: inline-flex;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
}

.cal-view-toggle button {
  border: 0;
  background: transparent;
  color: var(--text2);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 12px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 28px;
}

.cal-view-toggle button[aria-pressed="true"] {
  /* engagement fix-ticker-a11y-color-contrast-h1: see .cal-scope-btn[aria-selected="true"]
     rationale above — same WCAG AA fix via --on-accent token. */
  background: var(--accent);
  color: var(--on-accent, #fff);
}

/* ── Search row ── */
.cal-search-row {
  margin: 8px 0 16px;
}

.cal-search-row input {
  width: 100%;
  max-width: 320px;
  padding: 8px 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 14px;
}

.cal-search-row input:focus {
  outline: none;
  border-color: var(--accent);
}

.cal-search-row[hidden] { display: none; }

.cal-grid[hidden],
.cal-list[hidden] { display: none; }

/* ── Status / freshness row ── */
.cal-status-row {
  display: flex;
  gap: 12px;
  align-items: center;
  font-size: 12px;
  color: var(--text3);
  margin: 4px 0 12px;
}

.cal-freshness-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--pos);
  display: inline-block;
}

.cal-freshness-dot.stale { background: var(--yellow); }

/* ── Grid ── */
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: var(--cal-grid-gap);
  background: transparent;
}

.cal-weekday-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text3);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 4px 6px;
  text-align: left;
}

.cal-day {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--cal-cell-radius);
  padding: var(--cal-cell-pad);
  min-height: 96px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: relative;
}

.cal-day.is-other-month {
  background: var(--surface2);
  opacity: 0.55;
}

.cal-day.is-today {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent-glow);
}

.cal-day:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.cal-day-num {
  font-size: 12px;
  font-weight: 700;
  color: var(--text2);
  line-height: 1;
}

.cal-day.is-today .cal-day-num { color: var(--accent); }

.cal-day-chips {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* ── Chip ── */
.cal-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 0;
  background: color-mix(in srgb, var(--cal-event-ex) 18%, transparent);
  color: var(--text);
  border-left: 3px solid var(--cal-event-ex);
  border-radius: var(--cal-chip-radius);
  padding: 4px 8px;
  font: inherit;
  font-size: 11px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
}

.cal-chip[data-event-type="pay_date"] {
  background: color-mix(in srgb, var(--cal-event-pay) 18%, transparent);
  border-left-color: var(--cal-event-pay);
}

.cal-chip[data-event-type="div_change"] {
  background: color-mix(in srgb, var(--cal-event-cut) 22%, transparent);
  border-left-color: var(--cal-event-cut);
}

@media (hover: hover) {
  .cal-chip:hover { filter: brightness(1.12); }
}

.cal-chip:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.cal-chip-ticker { font-weight: 700; }
.cal-chip-amount { color: var(--text2); font-weight: 500; }
.cal-chip-badge {
  font-size: 9px;
  font-weight: 800;
  padding: 1px 4px;
  border-radius: 3px;
  background: var(--surface3);
  color: var(--text2);
  letter-spacing: 0.05em;
}

.cal-source-badge {
  display: inline-flex;
  align-items: center;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 999px;
  border: 1px solid var(--border2);
  background: var(--surface3);
  color: var(--text2);
  font-size: 10px;
  font-weight: 600;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* "+N more" overflow chip */
.cal-more-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--border2);
  background: transparent;
  color: var(--text2);
  border-radius: var(--cal-chip-radius);
  padding: 3px 8px;
  font: inherit;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}

@media (hover: hover) {
  .cal-more-chip:hover { color: var(--text); border-color: var(--accent); }
}

/* ── List view ── */
.cal-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.cal-list-row {
  display: grid;
  grid-template-columns: 80px 80px 1fr 90px 90px;
  gap: 12px;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
  cursor: pointer;
  text-align: left;
  color: var(--text);
}

@media (hover: hover) {
  .cal-list-row:hover { background: var(--surface2); }
}
.cal-list-row:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.cal-list-date  { color: var(--text2); font-weight: 600; }
.cal-list-type {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--cal-event-ex) 22%, transparent);
  color: var(--text);
  font-size: 11px;
  font-weight: 700;
  width: fit-content;
}

.cal-list-row[data-event-type="pay_date"] .cal-list-type {
  background: color-mix(in srgb, var(--cal-event-pay) 22%, transparent);
}

.cal-list-ticker { font-weight: 700; display: inline-flex; align-items: center; gap: 6px; min-width: 0; }
.cal-list-amount { color: var(--text2); text-align: right; }
.cal-list-yield  { color: var(--text2); text-align: right; }

/* ── Empty / stale states ── */
.cal-empty {
  background: var(--surface);
  border: 1px dashed var(--border2);
  border-radius: var(--cal-cell-radius);
  padding: 32px 24px;
  text-align: center;
  color: var(--text2);
}

.cal-empty strong { color: var(--text); font-size: 16px; display: block; margin-bottom: 6px; }

.cal-banner {
  background: var(--yellow-bg);
  border: 1px solid var(--yellow-border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 12px;
  margin-bottom: 12px;
}

/* ── Day flyout (native <dialog>) ── */
.cal-day-flyout {
  border: 1px solid var(--border2);
  border-radius: var(--cal-cell-radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px 24px;
  max-width: 480px;
  width: 90vw;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.4);
}

.cal-day-flyout::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

.cal-day-flyout-title {
  margin: 0 0 12px;
  font-size: 16px;
  font-weight: 700;
}

.cal-day-flyout-close {
  position: absolute;
  top: 12px;
  right: 12px;
  background: transparent;
  border: 0;
  color: var(--text2);
  font-size: 20px;
  cursor: pointer;
  line-height: 1;
}

.cal-day-flyout-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.cal-day-flyout-item {
  border-top: 1px solid var(--border);
  padding-top: 12px;
  border-radius: var(--radius-sm);
}

/* The [data-focused-event="true"] attribute is still set by JS for a11y
 * (aria-current, keyboard focus + scrollIntoView on the tapped event), but
 * we intentionally render it with NO visual emphasis: a persistent orange
 * ring on the entry-point row made it look like a click-selection that
 * users expected to follow subsequent clicks, while peer rows looked
 * "second class". All flyout items now render as visual peers. */

.cal-day-flyout-item:first-child { border-top: 0; padding-top: 0; }

.cal-day-flyout-item-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 8px;
}

.cal-day-flyout-item-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--text);
  font-size: 12px;
  margin: 4px 0;
}

.cal-kv-label {
  color: var(--text2);
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.cal-kv-value {
  color: var(--text);
  text-align: right;
}

.cal-kv-note {
  color: var(--text2);
}

.cal-day-flyout-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.cal-day-flyout-actions button {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

@media (hover: hover) {
  .cal-day-flyout-actions button:hover { background: var(--surface3); border-color: var(--accent); }
}
.cal-day-flyout-actions button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── Toast ── */
.cal-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface3);
  color: var(--text);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  padding: 12px 18px;
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  z-index: 9999;
  max-width: 90vw;
}

/* ── Skeleton (loading) ── */
.cal-grid.is-loading .cal-day-chips::before {
  content: '';
  display: block;
  background: linear-gradient(90deg, var(--surface2) 0%, var(--surface3) 50%, var(--surface2) 100%);
  background-size: 200% 100%;
  animation: cal-shimmer 1.4s linear infinite;
  height: 18px;
  border-radius: var(--cal-chip-radius);
}

@keyframes cal-shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .cal-grid.is-loading .cal-day-chips::before { animation: none; }
}

/* ── Breakpoints (design § 6.2) ── */

/* 640–1023 px — keep grid, single-line chips. */
@media (max-width: 1023px) and (min-width: 641px) {
  .cal-day {
    min-height: 84px;
    padding: 8px;
  }
  .cal-list-row {
    grid-template-columns: 80px 70px 1fr 80px;
  }
  .cal-list-yield { display: none; }
}

/* < 640 px — vertical week stack + WCAG 2.5.5 tap targets. */
@media (max-width: 640px) {
  .cal-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  /* The weekday-label row hides on mobile — each week stack carries
     its day labels inline via the chip. */
  .cal-weekday-label { display: none; }
  .cal-day {
    min-height: auto;
    padding: 10px 12px;
  }
  .cal-day-chips {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 6px;
  }
  .cal-chip {
    /* WCAG 2.5.5 — 44 × 44 minimum on both axes (design § 5.4 NB-7). */
    min-width: 44px;
    min-height: 44px;
    /* NB-2 R4 — overflow ceiling so a chip cannot exceed its cell. */
    max-width: 100%;
    padding: 8px 12px;
    font-size: 13px;
  }
  .cal-more-chip {
    min-width: 44px;
    min-height: 44px;
    padding: 8px 12px;
  }
  .cal-toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .cal-toolbar > * { width: 100%; justify-content: center; }
  .cal-list-row {
    grid-template-columns: 80px 1fr 80px;
    gap: 8px;
  }
  .cal-list-yield, .cal-list-type { display: none; }
  .cal-day-flyout {
    width: 100vw;
    max-width: 100vw;
    height: 100vh;
    border-radius: 0;
  }
}

/* ── Phase E E2 — share + iCal export ── */
.cal-share-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.cal-share-actions button {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  min-height: 32px;
}
@media (hover: hover) {
  .cal-share-actions button:hover { background: var(--surface3); border-color: var(--accent); }
}
.cal-share-actions button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.cal-share-actions button:disabled { opacity: 0.6; cursor: progress; }

.cal-share-modal {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 24px;
  min-width: 320px;
  max-width: min(520px, calc(100vw - 32px));
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
  /* Restore the `margin: auto` that <dialog> UA styles use for
     modal centering — the project-wide reset
     `*, *::before, *::after { margin: 0 }` in app.css zeroes it
     out, leaving showModal() dialogs pinned to the top-left. */
  margin: auto;
}
.cal-share-modal::backdrop {
  background: rgba(0,0,0,0.35);
}
.cal-share-modal-title { margin: 0 0 6px 0; font-size: 18px; color: var(--text); }
.cal-share-modal-body { margin: 0 0 12px 0; color: var(--muted); font-size: 14px; }
.cal-share-modal-meta { margin: 8px 0 0 0; color: var(--muted); font-size: 12px; }
.cal-share-modal-close {
  position: absolute;
  top: 8px;
  right: 12px;
  background: none;
  border: 0;
  font-size: 22px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
}
@media (hover: hover) {
  .cal-share-modal-close:hover { color: var(--text); }
}
.cal-share-modal-row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.cal-share-url {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface2);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.cal-share-url:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.cal-share-copy {
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 8px 14px;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  min-height: 32px;
}
@media (hover: hover) {
  .cal-share-copy:hover { filter: brightness(1.05); }
}
.cal-share-copy:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (max-width: 640px) {
  .cal-share-modal {
    width: 100vw;
    max-width: 100vw;
    height: auto;
    border-radius: 0;
    margin: 0;
  }
  .cal-share-modal-row { flex-direction: column; align-items: stretch; }
}
