:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1c2129;
  --muted: #6b7380;
  --border: #dfe3e8;
  --grid-line: #eceff3;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --today: #2563eb;
  --now: #ef4444;
  --live: #16a34a;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(15, 23, 42, 0.12);
  --hour-height: 48px;
  --gutter: 56px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1f242c;
    --text: #e6e9ee;
    --muted: #8d95a3;
    --border: #2b313b;
    --grid-line: #222831;
    --primary: #3b82f6;
    --danger: #f87171;
    --today: #60a5fa;
    --now: #f87171;
    --live: #4ade80;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

[hidden] { display: none !important; }

/* ---------- layout ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { margin: 0; font-size: 1.15rem; }
.user-box { display: flex; align-items: center; gap: 10px; min-width: 0; }

/* ---------- account menu ---------- */

.account { position: relative; }
.account-btn { display: inline-flex; align-items: center; gap: 8px; padding: 5px 10px 5px 5px; max-width: 260px; }
.avatar {
  flex: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  font-weight: 700;
  background: var(--primary);
  color: var(--primary-text);
}
.user-name {
  font-size: 0.9rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.account-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 230px;
  max-width: calc(100vw - 24px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
  z-index: 20;
  display: flex;
  flex-direction: column;
}
.account-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 10px 10px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--border);
  overflow-wrap: anywhere;
}
.account-info span { font-size: 0.8rem; color: var(--muted); }
.menu-item {
  font: inherit;
  font-size: 0.9rem;
  text-align: left;
  padding: 8px 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--surface-2); }

.link-btn {
  font: inherit;
  padding: 0;
  border: 0;
  background: none;
  color: var(--primary);
  cursor: pointer;
  text-decoration: underline;
}
.auth-switch { text-align: center; }

.live-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
}
.live-status::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}
.live-status[data-state="live"]::before { background: var(--live); }
.live-status[data-state="offline"] { color: var(--danger); }
.live-status[data-state="offline"]::before { background: var(--danger); }

main { max-width: 1200px; margin: 0 auto; padding: 16px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.narrow { max-width: 420px; margin: 40px auto; }
.card h2 { margin-top: 0; }

.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row.two { align-items: flex-end; }
.row.two > * { flex: 1 1 160px; }
.spacer { flex: 1; }
.row.nowrap { flex-wrap: nowrap; }
.row.nowrap input { min-width: 0; font-size: 0.8rem; }
.hint { margin: 0; font-size: 0.85rem; color: var(--muted); }
.note.warn { border-left: 3px solid var(--danger); }
.note {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
  background: var(--surface-2);
  padding: 8px 10px;
  border-radius: 8px;
}

/* ---------- forms ---------- */

.turnstile { min-height: 65px; } /* reserve space so the form doesn't jump when the widget appears */

label { display: flex; flex-direction: column; gap: 4px; font-size: 0.875rem; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
label.color-label { flex-direction: row; align-items: center; gap: 10px; }

input, textarea, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  width: 100%;
}
input[type="checkbox"] { width: auto; }
input[type="color"] { width: 48px; height: 32px; padding: 2px; }
textarea { resize: vertical; }

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- buttons ---------- */

.btn {
  font: inherit;
  font-size: 0.9rem;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
a.btn { display: inline-flex; justify-content: center; align-items: center; text-decoration: none; }
a.btn[aria-disabled="true"] { opacity: 0.6; pointer-events: none; }
.btn.icon { width: 38px; padding: 6px 0; font-size: 1.25rem; line-height: 1; }

.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.seg-btn {
  font: inherit;
  font-size: 0.875rem;
  padding: 7px 12px;
  border: 0;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.seg-btn + .seg-btn { border-left: 1px solid var(--border); }
.seg-btn:hover { background: var(--surface-2); }
.seg-btn[aria-pressed="true"] { background: var(--primary); color: var(--primary-text); }

/* ---------- toolbar ---------- */

.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.nav { display: flex; gap: 6px; }
.range-title { margin: 0; font-size: 1.3rem; flex: 1; min-width: 140px; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- search ---------- */

.search { flex: 0 1 240px; min-width: 160px; }
.search input {
  padding: 7px 10px 7px 32px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%238d95a3' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='7' cy='7' r='5'/%3E%3Cpath d='M11 11l3.5 3.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 10px center;
}

.search-results {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px 16px;
}
.search-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.search-status { margin: 0; color: var(--muted); font-size: 0.9rem; }
.search-group-title {
  margin: 16px 0 6px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.search-items { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }

.result {
  --ev-color: #3b82f6;
  width: 100%;
  display: flex;
  gap: 14px;
  align-items: flex-start;
  text-align: left;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--ev-color);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
}
.result:hover { background: var(--surface-2); }
.result-date {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 44px;
  line-height: 1.15;
}
.result-month { font-size: 0.7rem; text-transform: uppercase; color: var(--ev-color); font-weight: 700; }
.result-day { font-size: 1.35rem; font-weight: 700; }
.result-dow, .result-year { font-size: 0.7rem; color: var(--muted); }
.result-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.result-title { font-weight: 600; overflow-wrap: anywhere; }
.result-meta, .result-extra { font-size: 0.85rem; color: var(--muted); overflow-wrap: anywhere; }
mark {
  background: color-mix(in srgb, #facc15 55%, transparent);
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
}

/* ---------- month view ---------- */

.weekdays, .grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
}
.weekdays span {
  padding: 6px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  text-align: center;
}
.grid {
  background: var(--border);
  gap: 1px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.day {
  background: var(--surface);
  min-height: 110px;
  max-height: 140px;
  padding: 4px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  cursor: pointer;
  min-width: 0;
}
.day:hover { background: var(--surface-2); }
.day.outside { background: var(--bg); }
.day.outside .day-num { color: var(--muted); }

.day-num {
  align-self: flex-end;
  font: inherit;
  font-size: 0.8rem;
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 0;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}
.day-num:hover { background: var(--border); }
.day.today .day-num { background: var(--today); color: #fff; font-weight: 700; }

.events {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
  min-height: 0;
}

.event {
  --ev-color: #3b82f6;
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  font-size: 0.75rem;
  padding: 2px 6px;
  border: 0;
  border-left: 3px solid var(--ev-color);
  border-radius: 4px;
  background: color-mix(in srgb, var(--ev-color) 16%, var(--surface));
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.event:hover { background: color-mix(in srgb, var(--ev-color) 28%, var(--surface)); }
.event.all-day { background: var(--ev-color); color: #fff; }
.event-time { color: var(--muted); font-variant-numeric: tabular-nums; }
.event.all-day .event-time { color: inherit; }
.repeat-icon { opacity: 0.75; }

/* ---------- week / day time grid ---------- */

.timegrid {
  --days: 7;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}
.tg-row {
  display: grid;
  grid-template-columns: var(--gutter) repeat(var(--days), minmax(0, 1fr));
}
.tg-head { border-bottom: 1px solid var(--border); }
.tg-day-head {
  font: inherit;
  border: 0;
  border-left: 1px solid var(--grid-line);
  background: transparent;
  color: var(--text);
  padding: 6px 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
}
.tg-day-head:hover { background: var(--surface-2); }
.tg-dow { font-size: 0.7rem; font-weight: 600; text-transform: uppercase; color: var(--muted); }
.tg-date {
  font-size: 1.1rem;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 50%;
}
.tg-day-head.today .tg-date { background: var(--today); color: #fff; font-weight: 700; }
.tg-day-head.today .tg-dow { color: var(--today); }

.tg-allday { border-bottom: 1px solid var(--border); }
.tg-allday-label {
  font-size: 0.7rem;
  color: var(--muted);
  padding: 6px 6px 0 0;
  text-align: right;
}
.tg-allday-cell {
  border-left: 1px solid var(--grid-line);
  min-height: 28px;
  padding: 2px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  cursor: pointer;
}
.tg-allday-cell:hover { background: var(--surface-2); }

.tg-scroll {
  max-height: calc(100vh - 260px);
  min-height: 320px;
  overflow-y: auto;
}
.tg-body { height: calc(var(--hour-height) * 24); }
.tg-hours { position: relative; }
.tg-hour {
  --h: 0;
  position: absolute;
  right: 6px;
  top: calc(var(--hour-height) * var(--h));
  transform: translateY(-50%);
  font-size: 0.7rem;
  color: var(--muted);
  white-space: nowrap;
}

.tg-col {
  position: relative;
  border-left: 1px solid var(--grid-line);
  background-image: repeating-linear-gradient(
    to bottom,
    var(--grid-line) 0,
    var(--grid-line) 1px,
    transparent 1px,
    transparent var(--hour-height)
  );
  cursor: pointer;
}
.tg-col.today { background-color: color-mix(in srgb, var(--today) 5%, transparent); }

.tg-event {
  --ev-color: #3b82f6;
  --top: 0;
  --height: 0.04;
  --left: 0;
  --width: 1;
  position: absolute;
  top: calc(var(--top) * 100%);
  height: calc(var(--height) * 100% - 2px);
  left: calc(var(--left) * (100% - 6px) + 2px);
  width: calc(var(--width) * (100% - 6px) - 2px);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  overflow: hidden;
  padding: 2px 6px;
  font: inherit;
  font-size: 0.75rem;
  text-align: left;
  border: 0;
  border-left: 3px solid var(--ev-color);
  border-radius: 4px;
  background: color-mix(in srgb, var(--ev-color) 22%, var(--surface));
  color: var(--text);
  cursor: pointer;
  z-index: 1;
}
.tg-event:hover { background: color-mix(in srgb, var(--ev-color) 34%, var(--surface)); z-index: 2; }
.tg-event-title { font-weight: 600; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tg-event-time { color: var(--muted); font-size: 0.7rem; white-space: nowrap; }
.tg-event.short { flex-direction: row; gap: 6px; align-items: center; }
.tg-event.short .tg-event-time { display: none; }

.now-line {
  --top: 0;
  position: absolute;
  left: 0;
  right: 0;
  top: calc(var(--top) * 100%);
  height: 2px;
  background: var(--now);
  z-index: 3;
  pointer-events: none;
}
.now-line::before {
  content: "";
  position: absolute;
  left: -5px;
  top: -4px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--now);
}

/* ---------- dialog ---------- */

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  width: min(480px, calc(100vw - 32px));
  padding: 20px;
}
dialog::backdrop { background: rgba(15, 23, 42, 0.45); }
dialog h2 { margin: 0; font-size: 1.15rem; }
.actions { margin-top: 4px; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- small screens ---------- */

@media (max-width: 640px) {
  :root { --gutter: 40px; --hour-height: 44px; }
  main { padding: 8px; }
  .toolbar { gap: 8px; }
  .range-title { font-size: 1.05rem; order: -1; flex-basis: 100%; }
  .seg-btn { padding: 7px 9px; }
  .search { order: 5; flex: 1 1 100%; }
  .search-results { padding: 10px; }
  .result { gap: 10px; padding: 8px 10px; }
  .user-box { gap: 8px; }
  .account-btn { max-width: 150px; }

  .day { min-height: 72px; max-height: 96px; padding: 2px; }
  .day-num { width: 22px; height: 22px; font-size: 0.7rem; }
  .event { font-size: 0.65rem; padding: 1px 3px; }
  .event-time { display: none; }
  .weekdays span { font-size: 0.65rem; padding: 4px 0; }

  .tg-hour { font-size: 0.6rem; right: 3px; }
  .tg-date { width: 26px; height: 26px; font-size: 0.95rem; }
  .tg-event { font-size: 0.65rem; padding: 1px 3px; }
  .tg-scroll { max-height: calc(100vh - 230px); }
}
