/* ---------- theme tokens ----------
 * Light is the default. Dark applies when the OS asks for it *and* the user
 * hasn't forced light, or when the user explicitly picks dark.
 * The two dark blocks below must stay in sync.
 */
:root {
  color-scheme: light;
  --bg: #e8eaef;          /* soft grey, deliberately not bright white */
  --surface: #f9fafb;     /* cards: soft off-white */
  --surface-2: #eceef2;
  --border: #d3d8e0;
  --text: #1c242f;
  --muted: #59616f;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --shadow: 0 6px 24px rgba(17, 24, 39, 0.12);
  --radius: 12px;
  --topbar-h: 56px;
  --slash-color: rgba(15, 20, 30, 0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="pink"]) {
    color-scheme: dark;
    --bg: #0f1420;
    --surface: #161d2b;
    --surface-2: #1e2736;
    --border: #2a3446;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --primary: #3b82f6;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    --slash-color: rgba(255, 255, 255, 0.34);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1420;
  --surface: #161d2b;
  --surface-2: #1e2736;
  --border: #2a3446;
  --text: #e6e9ef;
  --muted: #98a2b3;
  --primary: #3b82f6;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  --slash-color: rgba(255, 255, 255, 0.34);
}

:root[data-theme="light"] { color-scheme: light; }

:root[data-theme="pink"] {
  color-scheme: light;
  --bg: #fbe3ec;
  --surface: #fff5f8;
  --surface-2: #fde8f0;
  --border: #f2c4d6;
  --text: #4a1f33;
  --muted: #8a5a70;
  --primary: #d6336c;
  --primary-text: #ffffff;
  --danger: #c2255c;
  --shadow: 0 6px 24px rgba(160, 40, 90, 0.16);
  --slash-color: rgba(120, 30, 70, 0.4);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}
button, select, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.link {
  background: none; border: 0; color: var(--primary); padding: 2px 4px;
  text-decoration: underline;
}
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: var(--topbar-h);
  flex-wrap: wrap;
}
.brand { font-weight: 700; display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.brand span { font-size: 15px; }

/* Collapse toggle for the tabs + view options. */
.bar-toggle {
  flex: none; border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); border-radius: 8px; padding: 6px 11px; font-size: 15px;
  line-height: 1;
}
.bar-toggle:hover { border-color: var(--muted); }
.bar-summary {
  display: none; color: var(--muted); font-size: 13px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.bar-collapsed #tabs,
.bar-collapsed .controls .view-switch { display: none; }
.bar-collapsed .bar-summary { display: inline-block; max-width: 45vw; vertical-align: middle; }
.bar-collapsed .controls { margin-left: auto; }
body.kiosk .bar-toggle, body.kiosk .bar-summary { display: none; }

.tabs {
  display: flex; gap: 4px; overflow-x: auto; flex: 1 1 260px;
  scrollbar-width: thin;
}
.tabs button {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 6px 14px;
  white-space: nowrap;
  border-left: 4px solid var(--tab-color, var(--border));
}
.tabs button.active {
  background: var(--primary);
  color: var(--primary-text);
  border-color: var(--primary);
}

.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-left: auto; }
.view-switch { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.view-switch button { border: 0; background: var(--surface-2); padding: 6px 12px; }
.view-switch button.active { background: var(--primary); color: var(--primary-text); }

button.primary { background: var(--primary); color: var(--primary-text); border: 0; border-radius: 8px; padding: 8px 14px; font-weight: 600; }
button.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); border-radius: 8px; padding: 7px 12px; }

.usermenu { position: relative; }
.usermenu > summary { list-style: none; cursor: pointer; padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.usermenu > summary::-webkit-details-marker { display: none; }
.usermenu .menu {
  position: absolute; right: 0; max-width: calc(100vw - 16px); top: calc(100% + 6px);
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); padding: 8px; display: grid; gap: 6px; min-width: 160px; z-index: 50;
}
.usermenu .menu a, .usermenu .menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 8px; border-radius: 6px; text-decoration: none; color: var(--text); }
.usermenu.align-left .menu { right: auto; left: 0; }
.usermenu .menu a:hover, .usermenu .menu button:hover { background: var(--surface-2); }

/* ---------- theme picker ---------- */
.theme-row { flex-wrap: wrap; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 8px; font-size: 13px; color: var(--muted); }
.theme-pick { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.theme-pick button {
  border: 0; background: var(--surface-2); color: var(--text);
  padding: 5px 10px; font-size: 12px; width: auto; text-align: center;
}
.theme-pick button + button { border-left: 1px solid var(--border); }
.theme-pick button[aria-pressed="true"],
.usermenu .menu .theme-pick button[aria-pressed="true"]:hover { background: var(--primary); color: var(--primary-text); }

.theme-row select { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 4px 6px; font-size: 12px; }

/* Compact theme toggle shown in the status bar — the only theme control the
   kiosk view can reach (kiosk hides the top-bar user menu). */
.theme-cycle {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 999px; padding: 3px 10px; font-size: 12px; line-height: 1.4;
  white-space: nowrap;
}
.theme-cycle:hover { border-color: var(--muted); }
body:not(.kiosk) .theme-cycle { display: none; }
body.kiosk .theme-cycle { font-size: 14px; padding: 5px 14px; }

/* ---------- calendar ---------- */
#calendar {
  flex: 1 1 auto;
  min-height: 0;
  padding: 10px 12px;
  overflow-x: hidden;   /* never scroll sideways */
  overflow-y: auto;
}
.fc {
  --fc-border-color: var(--border);
  --fc-page-bg-color: transparent;
  --fc-neutral-bg-color: var(--surface-2);
  --fc-list-event-hover-bg-color: var(--surface-2);
  height: 100%;
}

/* Thin, reserved scrollbars so a busy month's scroller never sits on top of the
   day numbers. */
.fc-scroller { scrollbar-width: thin; scrollbar-gutter: stable; }
.fc-scroller::-webkit-scrollbar { width: 9px; height: 9px; }
.fc-scroller::-webkit-scrollbar-track { background: transparent; }
.fc-scroller::-webkit-scrollbar-thumb {
  background: var(--border); border-radius: 999px;
  border: 2px solid var(--surface);
}
.fc .fc-daygrid-day-top { padding-right: 4px; }
.fc .fc-toolbar-title { font-size: 1.05rem; }
.fc .fc-toolbar.fc-header-toolbar { margin-bottom: 0.5em; }
.fc .fc-button {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  text-transform: capitalize; box-shadow: none;
  padding: 5px 9px; font-size: 0.86rem;
}
.fc .fc-button-primary:not(:disabled).fc-button-active,
.fc .fc-button-primary:not(:disabled):active { background: var(--primary); border-color: var(--primary); color: #fff; }
.fc .fc-daygrid-day.fc-day-today, .fc .fc-timegrid-col.fc-day-today { background: color-mix(in srgb, var(--primary) 10%, transparent); }

/* Bold diagonal slash across a day that's over, on calendars that opt in.
   Fixed-angle CSS gradient stripe, not a per-cell computed line — 45deg
   reads as a top-left-to-bottom-right "\" on screen (CSS gradient angles
   are true screen-pixel angles regardless of the box's own aspect ratio,
   so every cell gets the same visual angle even though rows can differ in
   height — see equalizeRowHeights). Doesn't reach exact corners on
   non-square cells; that's the accepted tradeoff for a consistent angle. */
.fc-daygrid-day.past-marked .fc-daygrid-day-frame { position: relative; }
.fc-daygrid-day.past-marked .fc-daygrid-day-frame::after {
  content: "";
  display: block;   /* FC's own clearfix puts `display:table` on this pseudo-
                        element, which collapses it to a zero-size box — this
                        override is what actually makes the slash paint. */
  position: absolute;
  inset: 0;
  z-index: 8;   /* above FC's own event chips (z-index:6) and drag-mirror (7) */
  pointer-events: none;
  background-image: linear-gradient(
    45deg,
    transparent calc(50% - 3px),
    var(--slash-color) calc(50% - 3px),
    var(--slash-color) calc(50% + 3px),
    transparent calc(50% + 3px)
  );
}
.fc-event { cursor: pointer; border-radius: 6px; padding: 1px 3px; }
.fc-event.is-feed { opacity: 0.92; border-style: dashed; }

/* Long titles get cut off with an ellipsis instead of wrapping into (or
   overlapping) the start/end time — FC's own title rule is overflow:hidden
   but never sets white-space/text-overflow, so long text just wraps. */
.fc-daygrid-event .fc-event-title-container { min-width: 0; }
.fc-daygrid-event .fc-event-title {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.fc-daygrid-event .fc-event-time { flex-shrink: 0; white-space: nowrap; }

/* Month/day-grid timed events: an outlined chip in the event's own colour, so
   several landing on one day read as distinct. All-day events, and week-view
   time-slot events, keep their solid fill. */
.fc-daygrid-event.is-timed {
  background: var(--surface);
  background: color-mix(in srgb, var(--ev, var(--primary)) 16%, var(--surface)) !important;
  border: 1px solid var(--ev, var(--primary)) !important;
  border-left-width: 4px !important;
}
/* FullCalendar puts the event's text colour inline on .fc-event-main, so the
   contrast override has to target the children, not just the root. */
.fc-daygrid-event.is-timed,
.fc-daygrid-event.is-timed .fc-event-main,
.fc-daygrid-event.is-timed .fc-event-time,
.fc-daygrid-event.is-timed .fc-event-title {
  color: var(--text) !important;
}
.fc-daygrid-event.is-timed .fc-event-time { font-weight: 700; }
.fc-daygrid-event.is-timed .fc-daygrid-event-dot { display: none; }

/* "+N more" link and its day popover (shown when a day is full). */
.fc .fc-daygrid-more-link { color: var(--muted); font-weight: 600; }
.fc .fc-popover { background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); }
.fc .fc-popover-header { background: var(--surface-2); color: var(--text); }

/* ---------- status bar ---------- */
.statusbar {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 14px; font-size: 12px; color: var(--muted);
  border-top: 1px solid var(--border); background: var(--surface);
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: #9ca3af; flex: none; }
.dot.live { background: #22c55e; }
.dot.stale { background: #f59e0b; }

/* ---------- dialog ---------- */
dialog#eventDialog {
  border: 0; border-radius: 16px; padding: 0; width: min(620px, 94vw);
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
dialog#eventDialog::backdrop { background: rgba(10, 14, 22, 0.55); }
#eventForm > header, #eventForm > footer { display: flex; align-items: center; gap: 8px; padding: 14px 18px; }
#eventForm > header { border-bottom: 1px solid var(--border); }
#eventForm > header h2 { margin: 0; font-size: 1.05rem; flex: 1; }
#eventForm > footer { border-top: 1px solid var(--border); }
#eventForm .x { background: none; border: 0; font-size: 16px; color: var(--muted); }
#eventForm .spacer { flex: 1; }
#eventForm footer button:not(.primary):not(.danger) { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 14px; }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; padding: 16px 18px; }
.grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.grid label input, .grid label select, .grid label textarea {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font-size: 14px;
}
.grid .col-2 { grid-column: 1 / -1; }
.grid .allday-row { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.grid .allday-row input { width: auto; }
.color-field { display: flex; align-items: center; gap: 8px; }
.color-field input[type=color] { width: 44px; height: 34px; padding: 2px; }
.reminders, .repeat, .calendars-field { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px 12px; }
.reminders legend, .repeat legend, .calendars-field legend { font-size: 12px; color: var(--muted); padding: 0 4px; }
.checkbox-grid { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.checkbox-grid .chk { flex-direction: row !important; align-items: center; gap: 6px; color: var(--text) !important; font-size: 14px !important; }
.checkbox-grid .chk input { width: auto; }
.reminder-row { display: flex; align-items: center; gap: 6px; margin-top: 6px; }
.reminder-row input[type=number] { width: 70px; }
.reminder-row select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.reminder-row .rm { background: none; border: 0; color: var(--danger); font-size: 16px; }

/* ---------- repeat builder ---------- */
.repeat-modes { display: flex; flex-wrap: wrap; gap: 6px; }
.repeat-modes button {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 999px; padding: 6px 12px; font-size: 13px;
}
.repeat-modes button.on { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.repeat-panel { margin-top: 10px; display: grid; gap: 8px; }
.weekday-row { display: flex; flex-wrap: wrap; gap: 5px; }
.weekday-row button {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: 7px 0; width: 46px; font-size: 13px;
}
.weekday-row button.on { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.repeat-line { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; font-size: 13px; color: var(--muted); }
.repeat-line input[type=number] { width: 58px; }
.repeat-line input[type=number], .repeat-line input[type=date] {
  padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px;
  padding: 3px 4px 3px 10px; font-size: 12.5px;
}
.chip button { background: none; border: 0; color: var(--muted); font-size: 13px; padding: 0 4px; line-height: 1; }
.chip button:hover { color: var(--danger); }
.repeat-hint { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.repeat-preview { margin: 8px 0 0; font-size: 13px; font-weight: 600; }
.series-note {
  grid-column: 1 / -1; margin: 0; font-size: 13px; color: var(--muted);
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px;
}

.form-error { color: var(--danger); margin: 0 18px 12px; font-size: 13px; }

/* ---------- popover ---------- */
#eventPopover {
  position: fixed; z-index: 60; width: min(300px, 88vw);
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 12px 14px;
}
#eventPopover .pop-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
#eventPopover .pop-swatch { width: 12px; height: 12px; border-radius: 3px; flex: none; }
#eventPopover dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; font-size: 13px; }
#eventPopover dt { color: var(--muted); }
#eventPopover dd { margin: 0; }
#eventPopover .pop-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
#eventPopover .pop-edit { background: var(--primary); color: #fff; border: 0; border-radius: 7px; padding: 6px 12px; }

/* ---------- auth pages ---------- */
.auth-page { align-items: center; justify-content: center; }
.auth-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow); padding: 28px; width: min(380px, 92vw); margin: auto;
}
.auth-card h1 { margin: 0 0 4px; font-size: 1.2rem; }
.auth-card form { display: grid; gap: 12px; margin-top: 16px; }
.auth-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.auth-card input { padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); }
.auth-card button { background: var(--primary); color: #fff; border: 0; border-radius: 8px; padding: 10px; font-weight: 600; }

/* ---------- responsive ---------- */
@media (max-width: 720px) {
  .topbar { gap: 8px; }
  .brand span { display: none; }
  .controls { width: 100%; justify-content: space-between; }
  .grid { grid-template-columns: 1fr; }
  dialog#eventDialog { width: 100vw; height: 100dvh; max-height: 100dvh; border-radius: 0; }
  #eventForm { display: flex; flex-direction: column; height: 100%; }
  #eventForm .grid { flex: 1; overflow: auto; }
  .fc .fc-toolbar.fc-header-toolbar { flex-direction: column; gap: 6px; }
}

/* Short viewports (phone landscape, small windows) — reclaim vertical space. */
@media (max-height: 680px) {
  :root { --topbar-h: 42px; }
  .topbar { padding: 4px 8px; gap: 6px; min-height: 0; }
  .brand span { display: none; }
  button.primary { padding: 6px 11px; font-size: 13px; }
  .usermenu > summary { padding: 5px 9px; font-size: 13px; }
  .bar-toggle { padding: 5px 9px; }
  #calendar { padding: 2px 6px; }
  .fc .fc-toolbar.fc-header-toolbar { flex-direction: row; gap: 4px; margin-bottom: 3px; }
  .fc .fc-toolbar-title { font-size: 0.95rem; }
  .fc .fc-button { padding: 3px 7px; font-size: 0.78rem; }
  .fc .fc-col-header-cell-cushion { padding: 2px 0; font-size: 11px; }
  .fc .fc-daygrid-day-number { padding: 1px 4px; font-size: 11px; }
  .fc-daygrid-event { font-size: 11px; margin-top: 1px; }
  .fc-daygrid-event.is-timed { border-left-width: 3px !important; }
  .fc .fc-daygrid-more-link { font-size: 11px; }
  .statusbar { padding: 2px 10px; font-size: 11px; }
}

/* ---------- compact density ----------
   Squeeze more events per day into the month grid. Toggled from the kiosk status
   bar, or forced with ?compact=1 / ?density=compact. */
.density-compact #calendar { padding: 1px 4px; }
.density-compact .fc .fc-toolbar.fc-header-toolbar { margin-bottom: 2px; }
.density-compact .fc .fc-toolbar-title { font-size: 0.95rem; }
.density-compact .fc .fc-button { padding: 2px 7px; font-size: 0.75rem; }
.density-compact .fc .fc-col-header-cell-cushion { padding: 1px 0; font-size: 10px; }
.density-compact .fc .fc-daygrid-day-number { padding: 0 3px; font-size: 10px; line-height: 1.3; }
.density-compact .fc-daygrid-event {
  font-size: 10px; line-height: 1.35; padding: 0 3px; margin-top: 0.5px;
  border-radius: 4px;
}
.density-compact .fc-daygrid-event .fc-event-time { font-weight: 700; margin-right: 2px; }
.density-compact .fc-daygrid-event.is-timed { border-width: 1px !important; border-left-width: 3px !important; }
.density-compact .fc .fc-daygrid-more-link { font-size: 10px; }
.density-compact .fc .fc-daygrid-day-bottom { padding: 0 3px; }
.density-compact .statusbar { padding: 1px 8px; font-size: 10px; }

/* In kiosk, compact also drops the room-readability upsizing. */
.density-compact body.kiosk { font-size: 14px; }
.density-compact body.kiosk .fc .fc-toolbar-title { font-size: 1.05rem; }
.density-compact body.kiosk .fc-daygrid-event { font-size: 11px; padding: 1px 4px; }
.density-compact body.kiosk .statusbar { font-size: 11px; }

/* Density toggle button — kiosk status bar only, next to the theme cycle. */
.density-toggle {
  flex: none; border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); border-radius: 999px; padding: 3px 10px; font-size: 12px;
  white-space: nowrap;
}
.density-toggle:hover { border-color: var(--muted); }
body:not(.kiosk) .density-toggle { display: none; }
body.kiosk .density-toggle { font-size: 13px; padding: 5px 12px; }

/* ---------- kiosk ---------- */
body.kiosk { font-size: 17px; overflow: hidden; }
body.kiosk .controls, body.kiosk .brand { display: none; }
body.kiosk .topbar { justify-content: center; }
body.kiosk.locked .tabs { display: none; }
body.kiosk .fc .fc-toolbar-title { font-size: 1.5rem; }
body.kiosk .fc-event { font-size: 0.95rem; padding: 2px 5px; }
body.kiosk .statusbar { font-size: 13px; }
body.kiosk #calendar { overflow-x: hidden; }
body.kiosk .fc-scroller::-webkit-scrollbar { width: 6px; height: 6px; }

/* Compact view switcher — only shown in kiosk (the top toolbar is hidden there). */
.kiosk-views { display: none; }
body.kiosk .kiosk-views { display: inline-flex; }
.kiosk-views { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; margin-left: auto; }
.kiosk-views button {
  border: 0; background: var(--surface-2); color: var(--text);
  padding: 5px 12px; font-size: 13px;
}
.kiosk-views button + button { border-left: 1px solid var(--border); }
.kiosk-views button.active { background: var(--primary); color: var(--primary-text); }

/* Phones: keep day cells tall enough to read, and let the month grid scroll. */
@media (max-width: 720px) {
  .fc .fc-daygrid-day-frame { min-height: 64px; }
  .fc .fc-scroller-liquid-absolute { overflow-y: auto !important; -webkit-overflow-scrolling: touch; }
}
