/* ============================================================
   calendar.css — 黄历  ·  Main calendar page (index.astro)
   Includes: app-card, topbar, view-toggle, calendar grids,
             day-detail panel, knowledge sidebar, install banner,
             description cards, animations, responsive, print
   ============================================================ */

/* ============================================================
   APP CARD  — centered max-width container
   ============================================================ */
.app-card {
  max-width: var(--card-w);
  margin: 0 auto;
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow:
    0 0 0 1px rgba(212,172,13,0.45),
    0 20px 60px rgba(50,15,0,0.38),
    0 4px 14px  rgba(50,15,0,0.22);
}

/* ============================================================
   TOPBAR
   ============================================================ */
.topbar {
  background: var(--red);
  border-bottom: 3px solid var(--gold);
  display: flex;
  align-items: center;
  padding: 0 22px;
  height: 56px;
  gap: 12px;
}

.topbar-brand {
  margin-right: auto;
  display: flex;
  align-items: baseline;
  gap: 1px;
}
.brand-hl {
  font-size: 26px;
  font-weight: 700;
  color: var(--gold);
  letter-spacing: -1px;
  line-height: 1;
  text-shadow: 0 1px 4px rgba(0,0,0,0.3);
}
.brand-li {
  font-size: 18px;
  font-weight: 600;
  color: rgba(255,255,255,0.92);
  letter-spacing: 3px;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 14px;
}

.today-btn {
  background: rgba(255,255,255,0.14);
  border: 1px solid rgba(255,255,255,0.32);
  color: #fff;
  padding: 5px 16px;
  border-radius: 20px;
  font-family: var(--font);
  font-size: 13px;
  cursor: pointer;
  letter-spacing: 0.5px;
  transition: background 0.2s, border-color 0.2s;
  white-space: nowrap;
}
.today-btn:hover  { background: rgba(255,255,255,0.26); border-color: rgba(255,255,255,0.5); }
.today-btn:active { background: rgba(255,255,255,0.18); }

/* ============================================================
   VIEW TOGGLE  (农历 ⟷ 西历)
   ============================================================ */
.view-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  user-select: none;
}
.tgl-label {
  font-size: 12px;
  font-weight: 600;
  color: rgba(255,255,255,0.45);
  letter-spacing: 0.5px;
  transition: color 0.2s;
  white-space: nowrap;
}
.tgl-label.active { color: #fff; }
.tgl-track {
  position: relative;
  width: 36px; height: 20px;
  background: rgba(255,255,255,0.18);
  border-radius: 10px;
  flex-shrink: 0;
  transition: background 0.2s;
}
.tgl-track.active { background: rgba(255,255,255,0.40); }
.tgl-thumb {
  position: absolute;
  top: 2px; left: 2px;
  width: 16px; height: 16px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
  transition: transform 0.2s cubic-bezier(.4,0,.2,1);
}
.tgl-track.active .tgl-thumb { transform: translateX(16px); }

/* ============================================================
   APP BODY — flex row: sidebar + main layout
   ============================================================ */
.app-body {
  display: flex;
  align-items: stretch;
  background: var(--paper);
  position: relative;
}

/* Main layout column */
.layout {
  display: flex;
  flex-direction: column;
  background: var(--paper);
}
.app-body > .layout {
  flex: 1;
  min-width: 0;
}

/* ============================================================
   KNOWLEDGE NAV SIDEBAR  (desktop only, left of calendar)
   ============================================================ */
.knowledge-nav {
  width: 172px;
  flex-shrink: 0;
  background: var(--paper-dark);
  border-right: 1px solid var(--border);
  padding: 18px 0 32px;
  position: sticky;
  top: 56px; /* height of topbar */
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--paper-mid) transparent;
}
.knowledge-nav::-webkit-scrollbar { width: 4px; }
.knowledge-nav::-webkit-scrollbar-track { background: transparent; }
.knowledge-nav::-webkit-scrollbar-thumb { background: var(--paper-mid); border-radius: 2px; }

/* ============================================================
   CALENDAR PANEL
   ============================================================ */
.cal-panel {
  padding: 18px 20px 16px;
  border-bottom: 2px solid var(--border);
  background: var(--paper);
}

.cal-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.cal-nav-center { flex: 1; text-align: center; }
.cal-month-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--red);
  letter-spacing: 2px;
}
.cal-month-sub {
  font-size: 13px;
  color: #888;
  margin-top: 3px;
}
.cal-nav-btn {
  flex-shrink: 0;
  width: 36px; height: 36px;
  background: var(--paper-dark);
  border: 1px solid var(--border);
  border-radius: 50%;
  font-size: 22px;
  color: var(--ink);
  cursor: pointer;
  font-family: var(--font);
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.cal-nav-btn:hover { background: var(--red); color: #fff; border-color: var(--red); }

.cal-jump {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}
.cal-jump select {
  flex: 1;
  font-family: var(--font);
  font-size: 13px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  outline: none;
  transition: border-color 0.15s;
}
.cal-jump select:focus,
.cal-jump select:hover { border-color: var(--red); }

/* ============================================================
   CALENDAR GRIDS
   ============================================================ */

/* Western (7-column) */
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

/* Lunar (xun rows) */
.cal-grid-lunar {
  display: flex !important;
  flex-direction: column;
  gap: 5px;
}

/* Weekday headers */
.cal-weekday {
  text-align: center;
  font-size: 12px;
  color: #999;
  padding: 4px 0 8px;
  font-weight: 700;
  letter-spacing: 0.3px;
}
.cal-weekday:first-child,
.cal-weekday:last-child { color: var(--red); }

/* Day cell */
.cal-day {
  background: #fff;
  border: 1px solid var(--paper-mid);
  border-radius: 6px;
  padding: 7px 4px 5px;
  min-height: 72px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
.cal-day:hover    { border-color: var(--red); background: var(--red-light); }
.cal-day:active   { background: #f0a09a; }
.cal-day.today    { background: var(--red); border-color: var(--red); }
.cal-day.selected { border-color: var(--gold); box-shadow: 0 0 0 2px var(--gold); }
.cal-day.other-month { opacity: 0.28; cursor: default; pointer-events: none; }

/* Western cell text */
.day-solar    { font-size: 19px; font-weight: 700; text-align: center; line-height: 1; margin-bottom: 3px; }
.day-lunar    { font-size: 12px; text-align: center; color: #777; }
.day-term     { font-size: 11px; text-align: center; color: var(--green); font-weight: 700; margin-top: 2px; }
.day-festival { font-size: 11px; text-align: center; color: var(--red);   font-weight: 700; margin-top: 2px; }

.cal-day.today .day-solar,
.cal-day.today .day-lunar    { color: #fff; }
.cal-day.today .day-term,
.cal-day.today .day-festival { color: var(--gold-light); }

/* Lunar xun layout */
.xun-row {
  display: grid;
  grid-template-columns: 26px repeat(10, 1fr);
  gap: 4px;
}
.xun-label {
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; color: var(--red);
  writing-mode: vertical-rl; letter-spacing: 2px;
}
.xun-day   { min-width: 0; }
.xun-blank { visibility: hidden; pointer-events: none; }

/* Quadrant wrappers for lunar cell layout */
.cell-q { display: block; }
.cell-q:empty { display: none; }

/* Lunar cell text */
.day-lunar-main { font-size: 15px; font-weight: 700; text-align: center; line-height: 1; margin-bottom: 3px; }
.day-solar-sub  { font-size: 11px; text-align: center; color: #777; }
.day-weekday    { color: #999; }
.day-weekday.weekend { color: var(--red); }

.cal-day.today .day-lunar-main,
.cal-day.today .day-solar-sub       { color: #fff; }
.cal-day.today .day-weekday         { color: rgba(255,255,255,0.7); }
.cal-day.today .day-weekday.weekend { color: var(--gold-light); }

/* ============================================================
   DETAIL PANEL
   ============================================================ */
.detail-panel { background: var(--paper); }
#day-detail   { width: 100%; }

.cal-links {
  padding: 10px 16px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  border-top: 1px solid var(--paper-mid);
  background: var(--paper-dark);
}
.cal-links:empty { display: none; }

/* ============================================================
   DAY DETAIL COMPONENTS  (rendered by calendar.js)
   ============================================================ */
.date-bar {
  display: flex;
  background: var(--red);
  border-bottom: 2px solid var(--gold);
}
.date-lunar {
  flex: 1;
  padding: 14px 16px;
  display: flex; flex-direction: column; justify-content: center;
}
.date-lunar-title { font-size: 22px; font-weight: 700; color: #fff; letter-spacing: 1px; }
.date-lunar-gz    { font-size: 12px; color: #FFE88A; margin-top: 4px; line-height: 1.5; }
.date-solar {
  display: flex; align-items: center; gap: 7px;
  padding: 14px 16px;
  background: rgba(0,0,0,0.12);
  min-width: 140px;
}
.date-solar-num  { font-size: 54px; font-weight: 700; color: #fff; line-height: 1; }
.date-solar-info { display: flex; flex-direction: column; }
.date-solar-ym   { font-size: 14px; color: rgba(255,255,255,0.92); font-weight: 600; }
.date-solar-wd   { font-size: 13px; color: rgba(255,255,255,0.82); margin-top: 3px; }

.tags-row { background: var(--red); padding: 2px 16px 8px; }
.tags-row:empty { padding: 0; }
.festival-tag {
  display: inline-block;
  background: var(--gold); color: #1A1A1A;
  font-size: 12px; font-weight: 700;
  padding: 2px 9px; border-radius: 10px;
  margin-right: 5px; letter-spacing: 0.5px; line-height: 1.8;
}
.term-tag {
  display: inline-block;
  background: rgba(255,255,255,0.18); color: #fff;
  font-size: 12px; padding: 2px 9px; border-radius: 10px;
  letter-spacing: 0.5px; line-height: 1.8;
}

/* Clickable tag links */
a.festival-tag,
a.term-tag {
  text-decoration: none;
  cursor: pointer;
  transition: opacity 0.15s, box-shadow 0.15s;
}
a.festival-tag:hover { opacity: 0.82; box-shadow: 0 1px 6px rgba(192,57,43,0.25); }
a.term-tag:hover     { opacity: 0.82; box-shadow: 0 1px 6px rgba(0,0,0,0.15); }

.yiji-row { display: grid; grid-template-columns: 1fr 1fr; border-bottom: 1px solid var(--paper-mid); }
.yiji-cell { padding: 10px 16px; }
.yiji-cell:first-child { border-right: 1px solid var(--paper-mid); }
.yiji-label { display: flex; align-items: center; gap: 6px; font-size: 14px; font-weight: 700; margin-bottom: 5px; }
.yiji-dot  { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.yi-dot    { background: var(--green); }
.ji-dot    { background: var(--red); }
.yi-label  { color: var(--green); }
.ji-label  { color: var(--red); }
.yiji-items { font-size: 13px; color: var(--ink-soft); line-height: 1.9; }

.info-row { display: flex; border-bottom: 1px solid var(--paper-mid); }
.info-col {
  flex: 1; padding: 7px 4px; text-align: center;
  border-right: 1px solid var(--paper-mid); min-width: 0;
}
.info-col:last-child { border-right: none; }
.info-label { font-size: 11px; color: #888; letter-spacing: 0.3px; }
.info-value { font-size: 13px; font-weight: 600; color: var(--ink); word-break: keep-all; overflow-wrap: break-word; }
.info-sub   { font-size: 11px; white-space: nowrap; }
.luck-ji    { color: var(--red)   !important; }
.luck-good  { color: var(--green) !important; }

.hours-header { font-size: 12px; color: #888; font-weight: 700; padding: 7px 16px 3px; letter-spacing: 0.5px; }
.hours-grid {
  display: grid; grid-template-columns: repeat(13, 1fr);
  border-bottom: 1px solid var(--paper-mid);
}
.hour-cell {
  padding: 5px 1px; text-align: center;
  border-right: 1px solid var(--paper-mid);
}
.hour-cell:nth-child(13n) { border-right: none; }
.hour-cell.current { background: var(--red); }
.hour-cell.current .hour-gz,
.hour-cell.current .hour-luck { color: #fff !important; }
.hour-gz   { font-size: 12px; font-weight: 700; color: var(--ink); }
.hour-luck { font-size: 11px; font-weight: 600; }
.hour-good { color: var(--green); }
.hour-bad  { color: var(--red); }

.gods-section { display: flex; border-bottom: 1px solid var(--paper-mid); }
.gods-half { flex: 1; padding: 6px 12px; line-height: 1.7; }
.gods-half:first-child { border-right: 1px solid var(--paper-mid); }
.gods-label { font-size: 12px; color: #666; font-weight: 700; margin-bottom: 2px; }
.gods-items { font-size: 12px; }
.gods-items .g { margin-right: 4px; white-space: nowrap; }
.god-good  { color: var(--green); }
.god-ji    { color: var(--red); }

.pengzu {
  padding: 7px 16px 10px;
  font-size: 12px; color: var(--ink-soft); line-height: 2;
  background: var(--paper-dark);
  border-bottom: 1px solid var(--paper-mid);
}
.pengzu-title { font-size: 12px; font-weight: 700; color: #666; letter-spacing: 0.5px; margin-bottom: 2px; }

/* ============================================================
   DESCRIPTION CARDS  (solar term & festival in day detail)
   ============================================================ */
.desc-cards {
  border-bottom: 1px solid var(--paper-mid);
}

.desc-card {
  padding: 11px 16px 12px;
  border-left: 3px solid var(--green);
  background: linear-gradient(90deg, rgba(30, 132, 73, 0.07) 0%, transparent 55%);
  position: relative;
}
.desc-card + .desc-card {
  border-top: 1px solid var(--paper-mid);
}
.desc-card.desc-festival {
  border-left-color: var(--red);
  background: linear-gradient(90deg, rgba(192, 57, 43, 0.07) 0%, transparent 55%);
}

.desc-card-header {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 5px;
}
.desc-card-icon  { font-size: 14px; line-height: 1; flex-shrink: 0; }
.desc-card-name  { font-size: 13px; font-weight: 700; color: var(--ink); letter-spacing: 0.5px; flex: 1; }
.desc-card-badge {
  font-size: 10px;
  padding: 1px 7px;
  border-radius: 8px;
  font-weight: 600;
  letter-spacing: 0.3px;
  flex-shrink: 0;
}
.desc-card-badge.badge-term     { background: rgba(30,132,73,0.13); color: var(--green); }
.desc-card-badge.badge-festival { background: rgba(192,57,43,0.12); color: var(--red); }

.desc-card-text {
  font-size: 12px;
  color: var(--ink-soft);
  line-height: 1.9;
  margin: 0 0 7px;
}
.desc-customs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}
.desc-custom-tag {
  font-size: 11px;
  padding: 2px 9px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.055);
  color: var(--ink-soft);
  border: 1px solid rgba(0, 0, 0, 0.07);
  white-space: nowrap;
}
.desc-card-empty { font-size: 12px; color: #bbb; font-style: italic; }

/* ============================================================
   MOBILE KNOWLEDGE SECTION  (in footer, hidden on desktop)
   ============================================================ */
.mobile-knowledge {
  display: none;
  margin-bottom: 20px;
}

.mobile-kn-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--red);
  letter-spacing: 3px;
  text-align: center;
  margin-bottom: 12px;
  text-transform: uppercase;
}

.mobile-kn-grid {
  display: grid !important;
  grid-template-columns: repeat(3, 1fr) !important;
  gap: 0 !important;
}
.mobile-kn-grid a {
  display: block;
  padding: 0.6rem 0.25rem;
  color: #6E5A38;
  text-decoration: none;
  font-size: 0.8rem;
  letter-spacing: 0.03em;
  text-align: center;
  border-bottom: 1px solid rgba(255,255,255,0.15);
  border-right: 1px solid rgba(255,255,255,0.15);
  background: none !important;
  border-radius: 0 !important;
  transition: color 0.15s, background 0.15s;
}
.mobile-kn-grid a:nth-child(3n)      { border-right: none; }
.mobile-kn-grid a:nth-last-child(-n+3) { border-bottom: none; }
.mobile-kn-grid a:active {
  color: #fff;
  background: rgba(255,255,255,0.15) !important;
}

/* ============================================================
   ANIMATIONS
   ============================================================ */
@keyframes detailIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.detail-anim { animation: detailIn 0.22s ease-out; }

@keyframes gridIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.grid-anim { animation: gridIn 0.18s ease-out; }

/* ============================================================
   PWA INSTALL BANNER
   ============================================================ */
.install-banner {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom, 16px));
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 11px 18px;
  border-radius: 12px;
  display: flex; align-items: center; gap: 10px;
  font-size: 13px; font-family: var(--font);
  box-shadow: 0 4px 24px rgba(0,0,0,0.4);
  z-index: 500;
  white-space: nowrap;
  max-width: calc(100vw - 32px);
}
.install-banner[hidden] { display: none; }
.install-yes {
  background: var(--red); border: none; color: #fff;
  padding: 6px 14px; border-radius: 7px;
  font-family: var(--font); font-size: 13px; cursor: pointer; flex-shrink: 0;
  transition: background 0.15s;
}
.install-yes:hover { background: var(--red-dark); }
.install-no {
  background: none; border: none; color: rgba(255,255,255,0.5);
  font-size: 16px; cursor: pointer; flex-shrink: 0; padding: 2px 4px;
  transition: color 0.15s;
}
.install-no:hover { color: #fff; }

/* ============================================================
   RESPONSIVE — sidebar hidden, mobile knowledge shown (≤ 900px)
   ============================================================ */
@media (max-width: 900px) {
  .knowledge-nav { display: none; }
  .mobile-knowledge { display: block; }
  /* Separate the footer wordmark visually from the mobile nav grid */
  .about-footer-wordmark { border-top: 1px solid var(--border); padding-top: 16px; }
}

/* ============================================================
   RESPONSIVE — mobile  (≤ 599px)
   Full-width card, no desktop frame, sticky topbar
   ============================================================ */
@media (max-width: 599px) {
  body {
    background: var(--paper);
    padding: 0;
  }
  .app-card {
    border-radius: 0;
    box-shadow: none;
    max-width: 100%;
  }
  .topbar {
    position: sticky;
    top: 0;
    z-index: 100;
    height: 52px;
    padding: 0 14px;
  }
  .brand-hl { font-size: 22px; }
  .brand-li { font-size: 15px; letter-spacing: 2px; }
  .today-btn { padding: 4px 12px; font-size: 12px; }
  .tgl-label { font-size: 11px; }

  .cal-panel { padding: 12px 10px 10px; }
  .cal-month-title { font-size: 17px; }
  .cal-nav-btn { width: 32px; height: 32px; font-size: 19px; }

  /* Compact day cells */
  .cal-day { min-height: 52px; padding: 5px 2px; }
  .day-solar { font-size: 15px; }
  .day-lunar, .day-term, .day-festival { font-size: 10px; }

  .xun-row { grid-template-columns: 18px repeat(10, 1fr); gap: 2px; }
  .xun-label { font-size: 9px; }
  .day-lunar-main { font-size: 12px; }
  .day-solar-sub  { font-size: 9px; }

  /* ── Lunar view: 4-quadrant vertical layout on mobile ──────
     Note: writing-mode:vertical-rl swaps CSS Grid row/col meanings.
     In vertical-rl: row-axis = horizontal (right→left), col-axis = vertical (top→bottom).
     So grid-area: row/col maps as:
       1/1=right-top(右上), 1/2=right-bottom(右下)
       2/1=left-top(左上),  2/2=left-bottom(左下)            */
  .cal-grid-lunar .xun-day {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    writing-mode: vertical-rl;
    text-orientation: mixed;
    height: auto;
    min-height: 75px;
    padding: 4px 2px;
    gap: 1px;
    overflow: hidden;
  }
  .cal-grid-lunar .cell-q       { display: flex; }
  .cal-grid-lunar .cell-q:empty { display: flex; }

  /* Quadrant positions — spread to four corners */
  .cal-grid-lunar .cell-q-festival { grid-area: 2 / 1; justify-content: flex-start; align-items: flex-end; }
  .cal-grid-lunar .cell-q-lunar    { grid-area: 1 / 1; justify-content: flex-start; align-items: flex-start; }
  .cal-grid-lunar .cell-q-term     { grid-area: 2 / 2; justify-content: flex-end;   align-items: flex-end; }
  .cal-grid-lunar .cell-q-solar    { grid-area: 1 / 2; justify-content: flex-end;   align-items: flex-start; }

  .cal-grid-lunar .day-lunar-main { font-size: 13px; font-weight: 700; margin: 0; line-height: 1.1; }
  .cal-grid-lunar .day-solar-sub  { font-size: 9px; }
  .cal-grid-lunar .day-festival   { font-size: 9px; color: var(--red); }
  .cal-grid-lunar .day-term       { font-size: 9px; color: var(--green); }
  .cal-grid-lunar .day-num {
    -webkit-text-combine-upright: all;
    text-combine-upright: all;
  }

  .date-solar-num   { font-size: 44px; }
  .date-lunar-title { font-size: 20px; white-space: nowrap; }
  .yiji-cell        { padding: 8px 12px; }
  .yiji-items       { font-size: 12px; }

  .desc-card      { padding: 9px 12px 10px; }
  .desc-card-text { font-size: 11px; }
}

/* ── Safe area for notched phones in standalone PWA mode ───── */
@supports (padding-top: env(safe-area-inset-top)) {
  /* Mobile portrait */
  @media (display-mode: standalone) and (max-width: 599px) {
    .topbar {
      padding-top: env(safe-area-inset-top);
      height: calc(52px + env(safe-area-inset-top));
    }
  }
  /* Desktop — adjust sidebar top offset */
  @media (display-mode: standalone) and (min-width: 600px) {
    .knowledge-nav { top: calc(56px + env(safe-area-inset-top)); }
  }
}

/* ============================================================
   PRINT
   ============================================================ */
@media print {
  body { background: #fff; padding: 0; }
  .app-card { box-shadow: none; border-radius: 0; }
  .topbar, .cal-panel, .install-banner { display: none !important; }
}
