:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-soft: #f9fbfc;
  --control-bg: #ffffff;
  --text: #17202a;
  --label: #334553;
  --muted: #607080;
  --line: #d9e0e7;
  --accent: #146c8f;
  --accent-dark: #0f526d;
  --accent-soft: #e7f2f6;
  --focus: #d9edf4;
  --event-bg: #dceef6;
  --event-bg-hover: #c8e4ef;
  --event-text: #153f52;
  --event-text-hover: #0f3445;
  --review-bg: #fff1c9;
  --review-bg-hover: #ffe5a3;
  --review-text: #6a4a00;
  --review-text-hover: #4f3700;
  --review-pill-bg: #fff3d8;
  --review-pill-text: #825900;
  --reviewed-pill-bg: #e7f4ec;
  --reviewed-pill-text: #1e6b3a;
  --highlight: #f5c542;
  --shadow: rgba(15, 35, 50, 0.25);
  --backdrop: rgba(23, 32, 42, 0.38);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #11161c;
  --panel: #171f27;
  --panel-soft: #1d2731;
  --control-bg: #121920;
  --text: #e7edf3;
  --label: #c8d3dc;
  --muted: #9eacb8;
  --line: #2e3a46;
  --accent: #70c7e8;
  --accent-dark: #9bdcf3;
  --accent-soft: #143545;
  --focus: #16465b;
  --event-bg: #143545;
  --event-bg-hover: #1a4559;
  --event-text: #d8f4ff;
  --event-text-hover: #ffffff;
  --review-bg: #4b3610;
  --review-bg-hover: #654817;
  --review-text: #ffe2a3;
  --review-text-hover: #fff2d0;
  --review-pill-bg: #4b3610;
  --review-pill-text: #ffe2a3;
  --reviewed-pill-bg: #183a29;
  --reviewed-pill-text: #a8e7bf;
  --highlight: #f2cf63;
  --shadow: rgba(0, 0, 0, 0.55);
  --backdrop: rgba(0, 0, 0, 0.58);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
}

.page {
  width: min(1260px, calc(100% - 32px));
  margin: 32px auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 16px;
}

h1 {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
}

p {
  margin: 4px 0 0;
  color: var(--muted);
}

.scan-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.filter-links {
  display: flex;
  gap: 6px;
}

.filter-links a {
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--accent);
  font-weight: 700;
  padding: 8px 10px;
  text-decoration: none;
}

.filter-links a.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-dark);
}

button {
  min-height: 38px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 650;
  padding: 0 14px;
  cursor: pointer;
}

button:hover {
  background: var(--accent-dark);
}

button[name="dry_run"] {
  background: var(--control-bg);
  color: var(--accent);
}

.theme-toggle {
  border-color: var(--line);
  background: var(--control-bg);
  color: var(--label);
}

.theme-toggle:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-dark);
}

.notice {
  margin-bottom: 16px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  padding: 12px 14px;
}

.calendar-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 14px;
}

.month-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.month-actions h2,
.undated-events h2,
.pdf-dialog h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 650;
}

.calendar-nav,
.calendar-icon-nav {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--control-bg);
  color: var(--label);
  font-weight: 700;
  text-decoration: none;
}

.calendar-nav {
  padding: 0 14px;
}

.calendar-icon-nav {
  width: 38px;
  font-size: 24px;
  line-height: 1;
}

.calendar-nav:hover,
.calendar-icon-nav:hover {
  border-color: var(--accent);
  color: var(--accent-dark);
}

.search-box {
  display: grid;
  grid-template-columns: auto minmax(220px, 340px);
  align-items: center;
  gap: 10px;
  color: var(--label);
}

.search-control {
  position: relative;
}

input[type="search"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--control-bg);
  color: var(--text);
  font: inherit;
  padding: 9px 14px;
}

.search-results {
  position: absolute;
  z-index: 20;
  top: calc(100% + 6px);
  right: 0;
  width: min(420px, 90vw);
  max-height: 340px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 18px 45px var(--shadow);
  padding: 6px;
}

.search-result {
  display: grid;
  gap: 3px;
  width: 100%;
  min-height: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  padding: 9px 10px;
  text-align: left;
}

.search-result:hover,
.search-result.active {
  background: var(--accent-soft);
  color: var(--text);
}

.search-result.empty {
  color: var(--muted);
  cursor: default;
}

.search-result-name {
  font-weight: 800;
  line-height: 1.25;
}

.search-result-date {
  color: var(--muted);
  font-size: 12px;
}

input[type="search"]:focus,
input[type="text"]:focus,
input[type="date"]:focus,
textarea:focus {
  border-color: var(--accent);
  outline: 3px solid var(--focus);
}

.calendar-shell {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.calendar-weekdays,
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
}

.calendar-weekdays {
  border-bottom: 1px solid var(--line);
  background: var(--panel-soft);
}

.calendar-weekdays span {
  padding: 10px 12px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.calendar-day {
  min-height: 142px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  padding: 8px;
}

.calendar-day:nth-child(7n) {
  border-right: 0;
}

.calendar-day:nth-last-child(-n + 7) {
  border-bottom: 0;
}

.calendar-day.outside-month {
  background: var(--panel-soft);
}

.calendar-day.day-highlight {
  box-shadow: inset 0 0 0 2px var(--highlight);
}

.day-number {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  color: var(--label);
  font-size: 13px;
  font-weight: 700;
}

.outside-month .day-number {
  color: var(--muted);
}

.day-number.today {
  background: var(--accent);
  color: #fff;
}

.day-events {
  display: grid;
  gap: 5px;
  margin-top: 6px;
}

.calendar-event {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 6px;
  min-height: 26px;
  min-width: 0;
  border-radius: 5px;
  background: var(--event-bg);
  color: var(--event-text);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.25;
  padding: 5px 7px;
  text-decoration: none;
}

.calendar-event:hover {
  background: var(--event-bg-hover);
  color: var(--event-text-hover);
  text-decoration: none;
}

.calendar-event.needs-review-event {
  background: var(--review-bg);
  color: var(--review-text);
}

.calendar-event.needs-review-event:hover {
  background: var(--review-bg-hover);
  color: var(--review-text-hover);
}

.calendar-event.search-highlight {
  outline: 3px solid var(--highlight);
  outline-offset: 2px;
}

.event-title {
  min-width: 0;
  overflow-wrap: anywhere;
  white-space: normal;
}

.event-version-count {
  flex: 0 0 auto;
  min-width: 18px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.7);
  color: inherit;
  font-size: 11px;
  line-height: 18px;
  text-align: center;
}

.undated-events {
  margin-top: 18px;
}

.undated-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.calendar-empty {
  margin-top: 14px;
  color: var(--muted);
  font-weight: 700;
}

.pdf-dialog {
  width: min(560px, calc(100% - 28px));
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 24px 70px var(--shadow);
  padding: 0;
}

.pdf-dialog::backdrop {
  background: var(--backdrop);
}

.dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding: 16px;
}

.dialog-close {
  width: 34px;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--control-bg);
  color: var(--label);
  font-size: 22px;
  line-height: 1;
  padding: 0;
}

.dialog-close:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-dark);
}

.pdf-version-list {
  display: grid;
  gap: 8px;
  padding: 16px;
}

.pdf-version {
  display: grid;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  padding: 11px 12px;
  text-decoration: none;
}

.pdf-version:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.pdf-version-name {
  color: var(--accent);
  font-weight: 800;
}

.pdf-version-date {
  color: var(--muted);
  font-size: 13px;
}

.pdf-dialog > .review-link {
  display: inline-flex;
  margin: 0 16px 16px;
}

.edit-panel {
  margin-bottom: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 16px;
}

.form-grid {
  display: grid;
  grid-template-columns: minmax(240px, 1fr) minmax(180px, 240px);
  gap: 14px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--label);
  font-weight: 700;
}

input[type="text"],
input[type="date"],
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--control-bg);
  color: var(--text);
  font: inherit;
  padding: 9px 10px;
}

textarea {
  resize: vertical;
}

.form-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 980px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 12px 14px;
  text-align: left;
  vertical-align: top;
  line-height: 1.35;
}

th {
  background: var(--panel-soft);
  color: var(--label);
  font-size: 13px;
  text-transform: uppercase;
}

tr:last-child td {
  border-bottom: 0;
}

.date {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  white-space: nowrap;
}

.pdf-link {
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}

.event-link,
.review-link,
.back-link {
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
}

.count {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.status {
  display: inline-block;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  padding: 6px 8px;
  white-space: nowrap;
}

.status.reviewed {
  background: var(--reviewed-pill-bg);
  color: var(--reviewed-pill-text);
}

.status.needs-review {
  background: var(--review-pill-bg);
  color: var(--review-pill-text);
}

.pdf-link:hover,
.event-link:hover,
.review-link:hover,
.back-link:hover {
  color: var(--accent-dark);
  text-decoration: underline;
}

.empty {
  color: var(--muted);
  text-align: center;
}

.ai-cell {
  display: grid;
  gap: 3px;
  min-width: 220px;
}

.ai-cell small,
.muted {
  color: var(--muted);
}

@media (max-width: 640px) {
  .page {
    width: min(100% - 20px, 1180px);
    margin: 18px auto;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .scan-actions {
    width: 100%;
    justify-content: flex-start;
  }

  .top-actions {
    justify-content: flex-start;
  }

  .calendar-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .month-actions {
    flex-wrap: wrap;
  }

  .month-actions h2 {
    width: 100%;
    margin-top: 4px;
  }

  .search-box {
    grid-template-columns: 1fr;
  }

  .calendar-shell {
    overflow-x: auto;
  }

  .calendar-weekdays,
  .calendar-grid {
    min-width: 760px;
  }

  .calendar-day {
    min-height: 126px;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }
}
