/* ── Tokens ─────────────────────────────────────────────────────────────── */

:root {
  color-scheme: light;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --border: #d8dce3;
  --text: #1b1f24;
  --text-muted: #5d6672;
  --accent: #1a6fc4;
  --on-accent: #ffffff;
  --memo: #1a6fc4;
  --email: #128a76;
  --holiday: #d9791c;
  --danger: #c0392b;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 1px 6px rgb(0 0 0 / 0.04);
  --radius: 12px;
  --radius-sm: 7px;
  --max-w: 720px;
  --past-opacity: 0.6;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f1216;
    --surface: #181c22;
    --surface-2: #232932;
    --border: #2e3540;
    --text: #e7eaef;
    --text-muted: #9ba4b1;
    --accent: #6fb1f0;
    --on-accent: #0b1220;
    --memo: #6fb1f0;
    --email: #4fc8b0;
    --holiday: #f0a35c;
    --danger: #e57368;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.45);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1216;
  --surface: #181c22;
  --surface-2: #232932;
  --border: #2e3540;
  --text: #e7eaef;
  --text-muted: #9ba4b1;
  --accent: #6fb1f0;
  --on-accent: #0b1220;
  --memo: #6fb1f0;
  --email: #4fc8b0;
  --holiday: #f0a35c;
  --danger: #e57368;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.45);
}

/* ── Base ───────────────────────────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
}

a { color: var(--accent); }

h1, h2, h3, h4, p { margin: 0; }

button, input { font: inherit; color: inherit; }

.container {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 1.25rem 1rem 2rem;
}

.hint { color: var(--text-muted); font-size: 0.85rem; }
.error { color: var(--danger); font-size: 0.85rem; }
.label { font-size: 0.8rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }

/* ── Buttons ────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.5rem 0.95rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 500;
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.btn:hover { background: var(--surface-2); }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.btn.primary:hover { filter: brightness(1.08); }

.btn.ghost { background: transparent; }
.btn.small { padding: 0.35rem 0.7rem; font-size: 0.8rem; }

.btn[aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn.active {
  border-color: var(--accent);
  color: var(--accent);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.btn:focus-visible, .icon-btn:focus-visible, .chip:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#refresh-btn.spinning svg { animation: spin 0.8s linear infinite; }

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* ── Header ─────────────────────────────────────────────────────────────── */

.app-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.app-header h1 { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.01em; }
.subtitle { color: var(--text-muted); font-size: 0.9rem; }
.header-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }

/* ── Year group chips ───────────────────────────────────────────────────── */

.year-groups { margin-bottom: 1rem; }

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 1rem;
  margin-bottom: 0.5rem;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.chip {
  padding: 0.3rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 500;
  line-height: 1.3;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.chip:hover { border-color: var(--accent); }

.chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

/* ── Action buttons ─────────────────────────────────────────────────────── */

#action-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.push-hint { margin-bottom: 1rem; }

/* ── Timeline ───────────────────────────────────────────────────────────── */

#timeline { margin-top: 1rem; }

.loading, .empty-state {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-muted);
}

.empty-state p + p { margin-top: 0.5rem; }

.month-header {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: 0.6rem 0 0.4rem;
  margin: 1.25rem 0 0.6rem;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  font-size: 1.05rem;
  font-weight: 600;
}

.month-header:first-child { margin-top: 0; }

/* ── Event cards ────────────────────────────────────────────────────────── */

.event-card {
  --type: var(--memo);
  display: grid;
  grid-template-columns: 3.5rem 1fr;
  gap: 0.9rem;
  padding: 0.85rem 1rem 0.85rem 0.85rem;
  margin-bottom: 0.6rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--type);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.event-card[data-type="email"] { --type: var(--email); }
.event-card[data-type="holiday"] {
  --type: var(--holiday);
  background: color-mix(in srgb, var(--surface) 93%, var(--holiday));
}

.event-card[data-past] { opacity: var(--past-opacity); }

.date-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0.35rem 0;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  line-height: 1.1;
  text-align: center;
  align-self: start;
}

.date-block .dow { font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }
.date-block .day { font-size: 1.35rem; font-weight: 700; margin: 0.1rem 0; }
.date-block .mon { font-size: 0.7rem; text-transform: uppercase; color: var(--text-muted); }
.date-block.undated { opacity: 0.7; }
.date-block.undated .day { font-size: 1.1rem; }

.card-body { min-width: 0; }

.badges {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.3rem;
}

.badge {
  display: inline-block;
  padding: 0.08rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 500;
  color: var(--text-muted);
}

.badge.type {
  border-color: transparent;
  background: var(--type);
  color: var(--on-accent);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.event-card[data-type="holiday"] .badge.type,
.event-card[data-type="email"] .badge.type { color: #fff; }

.date-range {
  margin-left: auto;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.title { font-size: 1.05rem; font-weight: 600; line-height: 1.3; margin-bottom: 0.25rem; }
.title a { color: inherit; text-decoration: none; }
.title a:hover { text-decoration: underline; }

.external-link-icon {
  display: inline-block;
  vertical-align: middle;
  margin-left: 0.15em;
  margin-bottom: 0.2em;
  opacity: 0.4;
}

.title a:hover .external-link-icon { opacity: 0.8; }

.pdf-badge {
  display: inline-block;
  vertical-align: middle;
  margin-left: 0.4rem;
  padding: 0.05rem 0.4rem;
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.65rem;
  font-weight: 600;
}

.event-content {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 0.9rem;
  color: var(--text-muted);
  white-space: pre-line;
}

.event-content.expanded {
  display: block;
  -webkit-line-clamp: unset;
  overflow: visible;
}

.expand-toggle {
  margin-top: 0.2rem;
  padding: 0;
  border: none;
  background: none;
  color: var(--accent);
  font-size: 0.8rem;
  cursor: pointer;
}

.expand-toggle:hover { text-decoration: underline; }

.tag-list {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.3rem;
  margin-top: 0.5rem;
}

.tag-list small {
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.68rem;
}

.tag-list .published-date {
  margin-left: auto;
  background: none;
  padding: 0;
}

/* ── Dialogs ────────────────────────────────────────────────────────────── */

dialog {
  width: min(92vw, 440px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.25);
}

dialog::backdrop { background: rgb(0 0 0 / 0.5); }

.dialog-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.25rem;
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.dialog-body h2 { font-size: 1.3rem; }
.dialog-body h3 { font-size: 1.1rem; }

.dialog-body input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.subscribe-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.subscribe-actions .btn { flex: 1 1 10rem; }

.url-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.url-row code {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
  font-size: 0.72rem;
  color: var(--text-muted);
}

/* ── Footer ─────────────────────────────────────────────────────────────── */

footer {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  margin-top: 2.5rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* ── Responsive / preferences ───────────────────────────────────────────── */

@media (max-width: 480px) {
  .container { padding: 1rem 0.75rem 2rem; }
  .app-header h1 { font-size: 1.35rem; }
  .event-card { grid-template-columns: 3rem 1fr; gap: 0.7rem; padding: 0.7rem 0.75rem 0.7rem 0.7rem; }
  .date-block .day { font-size: 1.2rem; }
  #action-buttons .btn { flex: 1 1 auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
