:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-2: #fafaf8;
  --border: #e3e3df;
  --border-strong: #cfcfca;
  --text: #1f1f1e;
  --text-muted: #6b6b67;
  --accent: #3b6dd6;
  --accent-soft: #e6edfb;
  --weekend: #faf6f0;
  --today: #fff5d6;
  --pill-bg: #eef0ee;
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.08);

  /* per-project palette (assigned by JS via CSS vars on the pill) */
  --p1: #3b6dd6;
  --p2: #c4502a;
  --p3: #2f8f5e;
  --p4: #8a4ca6;
  --p5: #b78a1f;
  --p6: #2d8aa8;
  --p7: #c34c7a;
  --p8: #6b6b67;
  --p9: #4a8c3a;
  --p10: #b3543a;
  --p11: #5a6cc7;
  --p12: #8d6e3b;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15161a;
    --surface: #1d1f24;
    --surface-2: #22242a;
    --border: #2e3038;
    --border-strong: #3c3f48;
    --text: #ececec;
    --text-muted: #9b9ea6;
    --accent: #6e9bff;
    --accent-soft: #2a335a;
    --weekend: #1a1c20;
    --today: #3a331c;
    --pill-bg: #2a2c33;
    --shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.45;
  font-size: 14px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
  flex-wrap: wrap;
}
.brand h1 { margin: 0; font-size: 18px; font-weight: 600; letter-spacing: -0.01em; }
.muted { color: var(--text-muted); font-size: 12px; margin-left: 8px; }

.controls {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.view-switch, .nav-buttons {
  display: inline-flex;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface-2);
}
.view-switch button, .nav-buttons button {
  background: transparent;
  border: none;
  border-right: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  font: inherit;
  cursor: pointer;
}
.view-switch button:last-child, .nav-buttons button:last-child { border-right: none; }
.view-switch button.active { background: var(--accent); color: white; }
.view-switch button:not(.active):hover, .nav-buttons button:hover { background: var(--accent-soft); }

.period-label {
  font-weight: 600;
  font-size: 15px;
  min-width: 8em;
  text-align: center;
}

#project-filter {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 6px 8px;
  border-radius: 8px;
  font: inherit;
}

main#calendar {
  padding: 20px;
}

/* ==== Month grid ==== */
.month-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.dow-cell {
  background: var(--surface-2);
  padding: 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  text-align: center;
  font-weight: 600;
}
.day-cell {
  background: var(--surface);
  min-height: 110px;
  padding: 6px 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: relative;
  min-width: 0;
  overflow: hidden;
}
.month-grid .day-cell { cursor: pointer; }
.day-cell.weekend { background: var(--weekend); }
.day-cell.outside { opacity: 0.45; }
.day-cell.today { background: var(--today); }
.day-cell .daynum {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
}
.day-cell .daynum .count { font-weight: 500; }

/* ==== Week grid (single column, one row per day) ==== */
.week-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.week-grid .day-cell {
  min-height: 90px;
  display: grid;
  grid-template-columns: 140px minmax(0, 1fr);
  gap: 12px;
  align-items: start;
  padding: 10px 14px;
}
.week-grid .day-cell .daynum {
  font-size: 13px;
  color: var(--text);
  font-weight: 600;
  white-space: nowrap;
}
.week-grid .day-cell .daynum .dow-name {
  display: block;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.week-grid .day-cell .daynum .daynum-big {
  font-size: 22px;
  font-weight: 600;
  line-height: 1.1;
}
.week-grid .day-pills {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

/* ==== Day view ==== */
.day-view {
  max-width: 720px;
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  box-shadow: var(--shadow);
}
.day-view h2 { margin-top: 0; }

/* ==== Pill ==== */
.pill {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  color: var(--text);
  background: var(--pill-bg);
  border: 1px solid transparent;
  border-left: 3px solid var(--pill-color, var(--accent));
  padding: 4px 6px;
  border-radius: 6px;
  font-size: 11.5px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}
.pill:hover { background: var(--accent-soft); border-color: var(--border-strong); }
.pill-compact { border-left-width: 4px; padding-left: 8px; }
.pill .proj {
  display: inline-block;
  font-size: 9.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--pill-color, var(--accent));
  margin-right: 4px;
  font-weight: 700;
}

/* ==== List view ==== */
.list-view {
  max-width: 880px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.list-month { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px 20px; box-shadow: var(--shadow); }
.list-month h2 { margin: 0 0 10px 0; font-size: 16px; }
.list-row {
  display: grid;
  grid-template-columns: 96px 1fr;
  gap: 12px;
  padding: 6px 0;
  border-top: 1px solid var(--border);
}
.list-row:first-of-type { border-top: none; }
.list-row .when { color: var(--text-muted); font-size: 12px; }
.list-row .what { font-size: 13.5px; }
.list-row .what .proj {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  color: var(--pill-color, var(--accent));
  text-transform: uppercase;
  margin-right: 6px;
  letter-spacing: 0.04em;
}
.list-row .what a { color: inherit; text-decoration: none; }
.list-row .what a:hover { color: var(--accent); text-decoration: underline; }

/* ==== Viewer ==== */
.viewer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(900px, 100%);
  background: var(--surface);
  z-index: 100;
  box-shadow: -10px 0 40px rgba(0, 0, 0, 0.18);
  display: flex;
  flex-direction: column;
  transform: translateX(0);
  transition: transform 0.18s ease;
}
.viewer.hidden { transform: translateX(100%); pointer-events: none; }
.viewer-inner { display: flex; flex-direction: column; height: 100%; overflow: hidden; }
.viewer-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.viewer-head .tag {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--accent);
  margin-right: 8px;
}
.viewer-actions { display: flex; gap: 12px; align-items: center; }
.viewer-actions a { color: var(--text-muted); text-decoration: none; font-size: 13px; }
.viewer-actions a:hover { color: var(--accent); }
#viewer-close {
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
}
#viewer-body { padding: 24px 32px 60px; overflow: auto; }

.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 90; }
.backdrop.hidden { display: none; }

.tt {
  position: fixed;
  z-index: 9999;
  pointer-events: none;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  padding: 3px 7px;
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.35;
  max-width: 360px;
  white-space: pre-wrap;
  display: none;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
  left: 0;
  top: 0;
}
.tt.on { display: block; }

/* ==== Markdown ==== */
.markdown-body { font-size: 14.5px; line-height: 1.65; }
.markdown-body h1, .markdown-body h2, .markdown-body h3 { line-height: 1.3; margin-top: 1.6em; }
.markdown-body h1 { font-size: 1.6em; margin-top: 0; }
.markdown-body h2 { font-size: 1.25em; padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.markdown-body h3 { font-size: 1.05em; }
.markdown-body code {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.92em;
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 4px;
}
.markdown-body pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 12px 14px;
  border-radius: 8px;
  overflow-x: auto;
}
.markdown-body pre code { background: transparent; padding: 0; }
.markdown-body blockquote {
  border-left: 3px solid var(--border-strong);
  margin: 0;
  padding: 4px 14px;
  color: var(--text-muted);
}
.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 12px 0;
}
.markdown-body th, .markdown-body td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.markdown-body th { background: var(--surface-2); }
.markdown-body img { max-width: 100%; height: auto; }
.markdown-body a { color: var(--accent); }

.mermaid-block {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  justify-content: center;
  overflow-x: auto;
}
.mermaid-error {
  color: #b00020;
  background: #fbeaea;
  border-radius: 6px;
  padding: 8px;
  font-family: monospace;
  white-space: pre-wrap;
}

@media (max-width: 720px) {
  .day-cell { min-height: 70px; }
  .week-grid .day-cell { min-height: 200px; }
  .topbar { gap: 8px; padding: 10px 12px; }
  main#calendar { padding: 10px; }
  .pill { font-size: 11px; }
  .viewer { width: 100%; }
  #viewer-body { padding: 16px 18px 60px; }
}
