:root {
  color-scheme: dark;
  --bg: #0e1117;
  --panel: #161b22;
  --panel-2: #1c232c;
  --line: #2a313c;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #4f8cff;
  --google: #14b8a6;
  --meeting: #a371f7;
  --leave-draft: #8b949e;
  --leave-confirm: #d29922;
  --leave-validate: #2ea043;
  --leave-refuse: #f85149;
  --leave-mirror: #6e7bd6;
  --missing-late: #f0883e;
  --missing-early: #b87fff;
  --err: #f85149;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif; }

.topbar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; letter-spacing: 0.3px; }

.loading { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.loading[hidden] { display: none; }
.spinner {
  display: inline-block; width: 12px; height: 12px;
  border: 2px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.spacer { flex: 1; }
.month-nav { display: flex; align-items: center; gap: 6px; }
.month-nav span { min-width: 160px; text-align: center; font-weight: 600; }
.conn { display: flex; align-items: center; gap: 8px; }
.filters { display: inline-flex; align-items: stretch; margin-right: 8px;
  border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.filters .seg { background: transparent; border: none; border-radius: 0;
  padding: 4px 10px; color: var(--muted); border-left: 1px solid var(--line); }
.filters .seg:first-child { border-left: none; }
.filters .seg:hover { color: var(--text); }
.filters .seg.active { background: #2ea04322; color: #d2f7d4; }

button { font: inherit; color: inherit; background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; cursor: pointer; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: white; }
button.ghost { background: transparent; }
button.danger { background: transparent; border-color: var(--err); color: var(--err); }
button:disabled,
button:disabled:hover {
  background: transparent; color: var(--muted);
  border-color: var(--line);
  cursor: not-allowed; opacity: 0.6;
}

.conn button[data-state="on"] { color: #d2f7d4; border-color: #2ea04388; background: #2ea04322; }
.conn button[data-state="on"]:hover { border-color: #2ea043; }

.legend { display: flex; gap: 16px; flex-wrap: wrap; padding: 8px 16px; color: var(--muted); font-size: 12px; border-bottom: 1px solid var(--line); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
.dot-google { background: var(--google); }
.dot-meeting { background: var(--meeting); }
.dot-leave-draft { background: var(--leave-draft); }
.dot-leave-confirm { background: var(--leave-confirm); }
.dot-leave-validate { background: var(--leave-validate); }
.dot-leave-refuse { background: var(--leave-refuse); }
.dot-leave-mirror { background: var(--leave-mirror); }

main { padding: 12px 16px 16px; position: relative; }

.today-float {
  position: absolute;
  top: 12px; height: 24px;
  z-index: 5;
  background: rgba(79, 140, 255, 0.5);
  color: white;
  border: 1px solid rgba(79, 140, 255, 0.5); border-radius: 4px;
  padding: 0 8px; font-size: 12px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 4px;
}
.today-float:hover { background: var(--accent); border-color: var(--accent); }
.today-float.on-left { left: 20px; }
.today-float.on-right { right: 20px; }
body.rotated .today-float {
  /* Float in the dedicated 24px strip on the right edge. translateX(50%)
     shifts the layout box right by half its width so its post-rotation
     visual center sits at main_w - 12 (mid-strip). */
  top: auto; bottom: auto; left: 0%; right: auto;
  transform: translateX(50%) rotate(90deg);
  transform-origin: center center;
}
body.rotated .today-float.on-left  { top: 40px; }
body.rotated .today-float.on-right { bottom: 40px; }
.today-float[hidden] { display: none; }

/* shared color/typography for event bars */
.evt {
  font-size: 12px; padding: 2px 8px; border-radius: 4px;
  cursor: pointer; color: white; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.evt.google { background: var(--google); }
.evt.meeting { background: var(--meeting); }
.evt.leave-draft { background: var(--leave-draft); color: #111; }
.evt.leave-confirm { background: var(--leave-confirm); color: #111; }
.evt.leave-validate { background: var(--leave-validate); }
.evt.leave-refuse { background: var(--leave-refuse); text-decoration: line-through; }
/* Separated strip ABOVE the timeline that hosts missing-time arrow markers.
   Sized & translated in JS to match tl-inner so markers stay aligned with
   the timeline as the user pans/zooms. */
.miss-strip {
  position: relative;
  height: 24px;
  margin-bottom: 4px;
  overflow: hidden;
}
.miss-inner { position: relative; height: 100%; will-change: transform; }
/* Late arrival → "→" anchored left-aligned with the start of the working
   day. Left early → "←" anchored right-aligned with the end of the day.
   Sized with a generous tap target. */
.tl-miss {
  position: absolute; top: 0; bottom: 0; z-index: 4;
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 28px;
  font-size: 16px; line-height: 1; font-weight: 700;
  cursor: pointer; user-select: none;
  padding: 0 6px; border-radius: 4px;
}
.tl-miss.miss-late { color: var(--missing-late); }
.tl-miss.miss-late:hover { background: var(--missing-late); color: var(--bg); }
.tl-miss.miss-early { color: var(--missing-early); transform: translateX(-100%); }
.tl-miss.miss-early:hover { background: var(--missing-early); color: var(--bg); }

/* leave & google bars: title is allowed to spill past the box width when
   short, and is vertically centered regardless of bar height. */
.evt[class*="leave-"],
.evt.google {
  display: flex; align-items: center;
  overflow: visible;
}
/* calendar.event mirror of an hr.leave we cannot read directly — limited info */
.evt.leave-mirror { background: var(--leave-mirror); }

/* zoom control in topbar */
.zoom-ctrl { display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--line); border-radius: 6px; padding: 0 4px; }
.zoom-ctrl button { background: transparent; border: none; padding: 4px 10px; font-size: 14px; }
.zoom-ctrl button:hover { color: var(--accent); }
#zoom-label { font-size: 12px; color: var(--muted); min-width: 28px; text-align: center; font-variant-numeric: tabular-nums; }

/* windowed / zoomable timeline (no scrollbar — pan with [<]/[>] or drag) */
.timeline {
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  overflow: hidden;
  position: relative;
  cursor: grab;
  user-select: none;
  touch-action: none;
}
/* per-day full-height background band; gaps between weeks (and Sundays)
   intentionally have no band so the darker timeline bg shows through. */
.tl-day-bg {
  position: absolute; top: 0; bottom: 0;
  background: var(--panel);
  z-index: 0;
}
.tl-day-bg.weekend { background: #131922; }
.tl-day-bg.today { background: #1f2937; }
.timeline.dragging { cursor: grabbing; }
.timeline .tl-inner { will-change: transform; }
.tl-inner { position: relative; min-height: 200px; }

.tl-day {
  position: absolute; top: 0;
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 11px; color: var(--muted);
  cursor: default; user-select: none;
  overflow: hidden;
}
.tl-day.today { background: #1f2937; }
.tl-day.weekend { background: #131922; }
.tl-day.month-start { border-left: 2px solid var(--accent); }

/* full-height vertical line at every day boundary; Mondays are emphasized */
.tl-day-sep {
  position: absolute; top: 0; bottom: 0; width: 1px;
  background: var(--line); pointer-events: none; z-index: 0;
}
.tl-day-sep.week-start { background: var(--muted); }
.tl-day .tl-dow { font-size: 11px; font-weight: 700; color: #22d3ee; letter-spacing: 0.5px; }
.tl-day .tl-num { font-size: 13px; font-weight: 600; color: var(--text); }
.tl-day.today .tl-num { color: var(--accent); }
.tl-day .tl-month { font-size: 11px; font-weight: 700; color: var(--accent); }

.tl-hour {
  position: absolute; top: 0; width: 1px;
  background: var(--line); opacity: 0.5;
  pointer-events: none;
}
.tl-hour.noon { opacity: 0.85; }
.tl-hour .tl-hnum {
  position: absolute; top: 32px; left: 2px;
  font-size: 9px; color: var(--muted);
  background: var(--panel); padding: 0 2px;
  font-variant-numeric: tabular-nums;
}

.tl-now {
  position: absolute; top: 0; bottom: 0;
  width: 2px; background: var(--err);
  pointer-events: none; z-index: 5;
  box-shadow: 0 0 6px rgba(248, 81, 73, 0.6);
}
.tl-now::before {
  content: ""; position: absolute; top: -3px; left: -4px;
  width: 10px; height: 10px; border-radius: 50%; background: var(--err);
}

.tl-bar {
  position: absolute; height: 22px; line-height: 22px;
  border-radius: 3px;
  z-index: 1;
}
.tl-bar:hover { z-index: 4; outline: 1px solid white; outline-offset: -1px; }

dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px;
  padding: 18px; min-width: 380px; max-width: 540px;
}
dialog::backdrop { background: rgba(0,0,0,0.5); }
dialog h2 { margin: 0 0 12px; font-size: 16px; }
dialog label { display: block; margin: 8px 0; }
dialog label small { display: block; color: var(--muted); font-weight: normal; margin-top: 2px; }
.dow-hint { color: var(--muted); font-size: 12px; margin-left: 6px; font-weight: normal; }
.dow-hint:empty { display: none; }
dialog input[type=text], dialog input[type=password], dialog input[type=email], dialog input[type=url],
dialog input[type=date], dialog input[type=datetime-local], dialog input[type=time],
dialog input[type=number], dialog select, dialog textarea {
  width: 100%; margin-top: 4px; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; font: inherit;
  color-scheme: dark;
}
dialog .row { display: flex; gap: 10px; }
dialog .row label { flex: 1; }
dialog menu { display: flex; gap: 8px; justify-content: flex-end; padding: 0; margin: 12px 0 0; }
dialog .hint { color: var(--muted); font-size: 12px; }
dialog code { background: var(--panel-2); padding: 1px 4px; border-radius: 4px; }
.err { color: var(--err); font-size: 12px; min-height: 1em; margin-top: 6px; }

.approvals-btn { position: relative; display: inline-flex; align-items: center; gap: 6px; }
.approvals-btn.has-pending {
  background: var(--missing-late); border-color: var(--missing-late);
  color: var(--bg); font-weight: 700;
}
.approvals-btn.has-pending:hover { border-color: var(--missing-late); filter: brightness(1.1); }
.approvals-btn.busy {
  background: transparent; color: var(--muted);
  border-color: var(--line);
  pointer-events: none;
}
.approvals-btn.busy::after {
  content: ""; display: inline-block;
  width: 12px; height: 12px; margin-left: 6px;
  border: 2px solid var(--line); border-top-color: var(--missing-late);
  border-radius: 50%; animation: spin 0.8s linear infinite;
  vertical-align: middle;
}
.approvals-list { max-height: 60vh; overflow: auto; display: flex; flex-direction: column; gap: 6px; }
.approvals-list .approvals-empty { color: var(--muted); font-size: 13px; padding: 8px 0; }
.approvals-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 6px;
}
.approvals-row .who { font-weight: 600; }
.approvals-row .meta { color: var(--muted); font-size: 12px; }
.approvals-row .grow { flex: 1; min-width: 0; }
.approvals-row .grow > .who { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.approvals-row .grow > .meta { word-break: break-word; }
.approvals-row .tag {
  display: inline-block; padding: 1px 6px; border-radius: 4px;
  font-size: 11px; font-weight: 700; margin-right: 6px;
}
.approvals-row .tag.late { background: var(--missing-late); color: var(--bg); }
.approvals-row .tag.early { background: var(--missing-early); color: var(--bg); }
#dlg-approvals,
#dlg-leave-approvals { min-width: min(480px, 100vw - 24px); max-width: 640px; }
@media (max-width: 480px) {
  #dlg-approvals,
  #dlg-leave-approvals { min-width: 0; width: calc(100vw - 24px); max-width: none; padding: 14px; }
  .approvals-row { flex-wrap: wrap; gap: 8px; }
  .approvals-row .grow { flex-basis: 100%; min-width: 0; }
  .approvals-row > button { margin-left: auto; }
  .approvals-list { max-height: 70vh; }
}
.check { display: flex !important; align-items: center; gap: 6px; }
.check input { width: auto !important; margin: 0 !important; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 8px; }
.tab { background: transparent; border: none; border-bottom: 2px solid transparent; padding: 6px 10px; border-radius: 0; }
.tab.active { border-bottom-color: var(--accent); color: var(--text); }

/* rotate button: mobile-only, pinned left within .conn */
.btn-rotate { display: none; padding: 6px 8px; }
.btn-rotate svg { display: block; }

/* When the timeline is rotated 90° clockwise (mobile only), only the
   calendar element rotates. The topbar/legend/dialogs stay upright. The
   calendar's layout width becomes the visual height; layout height becomes
   visual width. */
@media (max-width: 700px) {
  body.rotated { display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; }
  body.rotated main { padding: 0; position: relative; overflow: hidden; flex: 1 1 auto; min-height: 0; }
  body.rotated #calendar { position: absolute; top: 0; left: 0; transform-origin: 0 0; border-radius: 0; border: none; }
  /* Dialogs and toast must render upright at viewport center, never affected
     by the body's flex layout or the calendar's transform. */
  body.rotated dialog[open] {
    position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
    margin: 0; max-height: calc(100dvh - 24px); max-width: calc(100vw - 24px);
    overflow-y: auto;
  }
  body.rotated #toast { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); }
  /* The miss-strip wraps the Late/Early arrow markers. In rotated mode the
     timeline runs top→bottom, so the strip rotates with cal: a 24px-wide
     vertical band overlaying the timeline header on the right edge of main.
     The width and transform are set inline by applyRotation(). */
  body.rotated .miss-strip {
    position: absolute; top: 0; left: 0;
    height: 24px; margin: 0;
    transform-origin: 0 0;
    z-index: 6;
  }
}

/* ---- responsive tweaks ---- */
@media (max-width: 700px) {
  .btn-rotate { display: inline-flex; align-items: center; justify-content: center; margin-right: auto; }
  .topbar { padding: 8px 10px; gap: 8px; }
  .brand { display: none; }
  .spacer { display: none; }
  .month-nav span { min-width: 110px; font-size: 13px; }
  .legend { padding: 6px 10px; gap: 10px; font-size: 11px; }
  main { padding: 8px 10px 12px; }
  .conn { gap: 6px; flex: 1 0 100%; order: -1; justify-content: flex-end; }
  .conn button { padding: 6px 8px; font-size: 12px; }
  .filters { margin-left: auto; }
  /* hide "Loading…" text on small screens; spinner alone is enough */
  .loading { font-size: 0; gap: 0; }
}

@media (max-width: 480px) {
  .topbar { gap: 6px; }
  .month-nav { gap: 4px; }
  .month-nav span { min-width: 70px; font-size: 12px; }
  .filters { margin-right: 0; }
  .filters .seg { padding: 4px 6px; font-size: 12px; }
  .conn button { padding: 6px 6px; font-size: 12px; }
  .zoom-ctrl button { padding: 4px 8px; }
  dialog { min-width: 0; width: calc(100vw - 24px); margin: 12px; }
  dialog .row { flex-direction: column; gap: 4px; }
  /* Time-pair rows (From/To, Comp From/To) stay horizontal even on narrow
     screens — time inputs are compact enough to share a line. */
  dialog .row.times { flex-direction: row; gap: 8px; }
}

#toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  padding: 8px 14px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 999;
}
#toast.show { opacity: 1; }
#toast.err { border-color: var(--err); color: var(--err); }

.ev-row { display: flex; gap: 8px; margin: 4px 0; }
.ev-row .k { width: 90px; color: var(--muted); }
.ev-row .v { flex: 1; }
