:root {
  --bg: #f4efe6;
  --surface: #fffdf9;
  --raised: #efe6d8;
  --fg: #1e1a16;
  --muted: #6d645b;
  --faint: #8f867c;
  --line: #e3d9cb;
  --ontime: #157a45;
  --ontime-wash: #e5f6ec;
  --late: #c24b2c;
  --late-wash: #fde8e1;
  --onfill: #fffdf9;
  --card: 1.5rem;
  --font: "Noto Sans TC", "PingFang HK", ui-sans-serif, system-ui, sans-serif;
  --display: "Fraunces", "Iowan Old Style", "Palatino Linotype", ui-serif, serif;
  color: var(--fg);
  background: var(--bg);
  font-family: var(--font);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
}

button { font: inherit; color: inherit; }

.app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  width: min(100%, 72rem);
  margin: 0 auto;
  background: var(--bg);
}

.pending {
  margin: 4rem 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.875rem;
}

.header { padding: 1.25rem 1.25rem 0; }
.eyebrow {
  margin: 0;
  color: var(--faint);
  font-size: 0.875rem;
  letter-spacing: 0.1em;
}
.title {
  margin: 0;
  font-family: var(--display);
  font-size: 2.25rem;
  font-weight: 500;
  line-height: 1.25;
}
.title-row, .month-nav, .mark-head, .streak-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.75rem;
}
.month-nav { align-items: center; margin-top: 1.25rem; }
.month-copy { text-align: center; }
.month-label { margin: 0; font-size: 1.125rem; font-weight: 500; }
.month-counts { margin: 0.25rem 0 0; color: var(--muted); font-size: 0.875rem; }
.ontime { color: var(--ontime); }
.late { color: var(--late); }
.dot { margin: 0 0.5rem; color: var(--line); }

.icon-btn, .today-btn, .mark-btn, .clear-btn, .month-btn {
  border: 0;
  background: var(--raised);
  cursor: pointer;
}
.icon-btn {
  display: flex;
  width: 2.75rem;
  height: 2.75rem;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
}
.today-btn {
  margin-bottom: 0.25rem;
  border-radius: 999px;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  font-weight: 500;
}
button:disabled { cursor: default; opacity: 0.4; }
.icon-btn:disabled { opacity: 0.3; }

.stats {
  margin: 1rem 1rem 0;
  border: 1px solid var(--fg);
  background: var(--surface);
}
.stats-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  border-bottom: 1px solid var(--fg);
  padding: 0.5rem 0.75rem;
}
.kicker, .th-en, .row-en {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.1em;
}
.stats-title { margin: 0; font-size: 1.125rem; font-weight: 500; line-height: 1.25; }
.fine, .th-zh, .row-zh { color: var(--muted); font-size: 0.75rem; }
.fine { margin: 0; }
.formula { max-width: 10rem; margin: 0; color: var(--muted); font-size: 0.75rem; line-height: 1.375; text-align: right; }
.stats-scroll { overflow-x: auto; }
.stats-table {
  width: max-content;
  border-collapse: collapse;
  text-align: center;
}
.stats-table th, .stats-table td {
  border: 1px solid var(--fg);
  background: var(--surface);
}
.label-col {
  position: sticky;
  left: 0;
  z-index: 2;
  width: 6rem;
  min-width: 6rem;
  padding: 0.25rem 0.5rem;
  text-align: left;
}
.year-col {
  position: sticky;
  z-index: 1;
  min-width: 3rem;
  padding: 0.25rem;
  font-size: 0.875rem;
  font-weight: 600;
  box-shadow: 1px 0 0 0 var(--fg);
}
.month-col { min-width: 2.75rem; padding: 0; }
.month-col.is-active, td.is-active { background: var(--raised); }
.month-btn {
  display: flex;
  width: 100%;
  flex-direction: column;
  align-items: center;
  padding: 0.25rem;
  background: transparent;
}
.stat-num {
  padding: 0.5rem 0.25rem;
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.calendar { padding: 1rem 1rem 0; }
.weekdays, .days {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 0.25rem;
}
.weekdays { padding: 0 0.25rem; }
.weekday {
  padding: 0.25rem 0;
  text-align: center;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--muted);
}
.weekday.is-end { color: var(--faint); }
.day {
  display: flex;
  height: 3rem;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  border: 1px solid var(--line);
  border-radius: 1rem;
  background: var(--surface);
  font-size: 1rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}
.day:active:not(:disabled) { transform: scale(0.95); }
.day.is-ontime { border-color: transparent; background: var(--ontime); color: var(--onfill); }
.day.is-late { border-color: transparent; background: var(--late); color: var(--onfill); }
.day.is-weekend { border-color: transparent; background: transparent; color: var(--faint); }
.day.is-future { color: var(--faint); }
.day.is-selected { outline: 2px solid var(--fg); outline-offset: 2px; }
.day.is-today:not(.is-selected):not(.is-weekend) { outline: 2px solid var(--ontime); outline-offset: 2px; }
.pip { width: 0.375rem; height: 0.375rem; border-radius: 999px; background: transparent; }
.day.is-ontime .pip, .day.is-late .pip { background: var(--onfill); }

.mark {
  margin-top: 1rem;
  border: 1px solid var(--line);
  border-radius: var(--card);
  background: var(--surface);
  padding: 0.75rem;
}
.mark-head { align-items: baseline; }
.mark-date { margin: 0; font-size: 0.875rem; font-weight: 500; }
.mark-hint { margin: 0; color: var(--muted); font-size: 0.75rem; }
.mark-actions { display: flex; gap: 0.5rem; margin-top: 0.75rem; }
.mark-btn, .clear-btn {
  height: 2.75rem;
  border-radius: 1rem;
  font-size: 0.875rem;
  font-weight: 500;
}
.mark-btn { flex: 1; }
.mark-btn.is-ontime { background: var(--ontime-wash); color: var(--ontime); }
.mark-btn.is-ontime.is-on { background: var(--ontime); color: var(--onfill); }
.mark-btn.is-late { background: var(--late-wash); color: var(--late); }
.mark-btn.is-late.is-on { background: var(--late); color: var(--onfill); }
.clear-btn { padding: 0 1rem; }
.save-error { margin: 0.5rem 0 0; color: var(--late); font-size: 0.75rem; }

.streak {
  margin-top: auto;
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding: 1.25rem 1.25rem max(1.25rem, env(safe-area-inset-bottom));
}
.streak.is-ontime { background: var(--ontime-wash); }
.streak.is-late { background: var(--late-wash); }
.streak-label { margin: 0; color: var(--muted); font-size: 0.875rem; font-weight: 500; letter-spacing: 0.1em; }
.streak-since { margin: 0; color: var(--muted); font-size: 0.875rem; }
.streak-count {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  margin-top: 0.25rem;
}
.streak-num {
  margin: 0;
  font-family: var(--display);
  font-size: 3.75rem;
  font-weight: 500;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--faint);
}
.streak-num.is-ontime { color: var(--ontime); }
.streak-num.is-late { color: var(--late); }
.streak-unit { margin: 0 0 0.5rem; color: var(--muted); font-size: 1.125rem; }
.recent-label { margin: 1.25rem 0 0.5rem; color: var(--muted); font-size: 0.75rem; }
.recent { display: flex; align-items: flex-end; justify-content: space-between; gap: 0.25rem; }
.bead {
  display: block;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 999px;
  background: var(--line);
}
.bead.is-today { width: 0.5rem; height: 0.75rem; }
.bead.is-ontime { background: var(--ontime); }
.bead.is-late { background: var(--late); }
.bead.is-weekend { border: 1px solid var(--line); background: transparent; }

@media (min-width: 1024px) {
  .app {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 22rem;
    align-content: start;
    column-gap: 2.5rem;
    min-height: 100dvh;
    padding: 2rem;
  }
  .header, .stats { grid-column: 1 / -1; }
  .header, .calendar, .stats { padding-left: 0; padding-right: 0; }
  .stats { margin-left: 0; margin-right: 0; }
  .stats-table { width: 100%; }
  .month-col { min-width: 0; }
  .streak {
    position: sticky;
    top: 1.5rem;
    align-self: start;
    margin-top: 1rem;
    border: 1px solid var(--line);
    border-radius: var(--card);
  }
}

@media (prefers-reduced-motion: reduce) {
  .day:active:not(:disabled) { transform: none; }
}
