:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --line: #e2e5ea;
  --text: #1b1e24;
  --muted: #69707c;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --free: #1f9d55;
  --busy: #5b6577;
  --over: #e39a2d;
  --turn: #2f6fed;
  --danger: #c93c3c;
  --radius: 10px;
  --label: 250px;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1c1f25;
    --line: #2d3139;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --accent: #5b8dff;
    --busy: #56607a;
    --over: #f0ad4e;
    --danger: #ff6b6b;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }

.wrap { max-width: 1400px; margin: 0 auto; padding: 20px 24px 48px; }

/* ---------- header ---------- */
header.top { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
header.top h1 { font-size: 19px; margin: 0; letter-spacing: -0.01em; display: flex; align-items: center; gap: 10px; white-space: nowrap; }
header.top h1 img { width: 28px; height: 28px; }
header.top .spacer { flex: 1; }
.who { color: var(--muted); font-size: 14px; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
.who b { color: var(--text); font-weight: 600; }
.who .team, .tag {
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--bg) 50%, var(--line));
  color: var(--muted);
  white-space: nowrap;
}
.tag.warn { background: color-mix(in srgb, var(--over) 22%, var(--panel)); color: var(--text); }
nav.seg { display: flex; background: color-mix(in srgb, var(--bg) 40%, var(--line)); border-radius: 9px; padding: 3px; margin-left: 8px; }
nav.seg a { padding: 5px 14px; border-radius: 7px; color: var(--muted); text-decoration: none; font-size: 14px; font-weight: 500; transition: background-color 120ms ease; }
nav.seg a.on { background: var(--panel); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / 0.12); }
nav.seg a:active { transform: scale(0.97); }

/* ---------- buttons ---------- */
button, a.button {
  font: inherit;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 7px 14px;
  min-height: 36px;
  cursor: pointer;
  transition: transform 80ms ease, background-color 120ms ease, opacity 120ms ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  white-space: nowrap;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
button:hover, a.button:hover { background: color-mix(in srgb, var(--panel) 88%, var(--text)); }
button:active, a.button:active { transform: scale(0.96); }
button:focus-visible, a:focus-visible, [tabindex]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary, a.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover, a.button.primary:hover { background: color-mix(in srgb, var(--accent) 86%, black); }
button.danger { color: var(--danger); }
button.danger-fill { background: var(--danger); border-color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; color: var(--muted); }
button.ghost:hover { color: var(--text); background: color-mix(in srgb, var(--bg) 70%, var(--line)); }
button.ghost.danger { color: var(--danger); }
button.small { padding: 4px 11px; min-height: 32px; font-size: 13.5px; }
button.link { border: 0; background: none; padding: 0; min-height: 0; color: var(--accent); font-weight: 600; text-decoration: underline; text-underline-offset: 2px; white-space: normal; text-align: left; }
button.ok { background: var(--free) !important; border-color: var(--free) !important; color: #fff !important; }
button[aria-busy="true"] { opacity: 0.65; pointer-events: none; }
button[aria-busy="true"]::after {
  content: "";
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  margin-left: 8px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-btn { min-height: 30px; padding: 3px 12px; border-radius: 999px; font-size: 13.5px; }

/* ---------- banners ---------- */
.banner {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
}
.banner .grow { flex: 1; min-width: 0; }
.banner .sub { color: var(--muted); font-size: 14px; }
.banner .acts { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.banner.turn { border-color: var(--turn); background: color-mix(in srgb, var(--turn) 9%, var(--panel)); }
.banner.turn strong { color: var(--turn); }
.banner.mine { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.banner.over, .banner.late { border-color: color-mix(in srgb, var(--over) 70%, var(--line)); background: color-mix(in srgb, var(--over) 9%, var(--panel)); }
.banner.muted { color: var(--muted); }

/* ---------- toolbar ---------- */
.toolbar { display: flex; align-items: center; gap: 12px; margin: 6px 0 10px; flex-wrap: wrap; }
.toolbar h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0; font-weight: 600; }
.toolbar .spacer { flex: 1; }
.legend { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); }
.legend .lg { display: inline-block; width: 14px; height: 10px; border-radius: 3px; margin-left: 6px; }
.lg.active { background: var(--busy); }
.lg.over { background: repeating-linear-gradient(135deg, var(--over) 0 4px, color-mix(in srgb, var(--over) 55%, var(--panel)) 4px 8px); }
.lg.waiting { background: color-mix(in srgb, var(--busy) 25%, var(--panel)); border: 1px solid color-mix(in srgb, var(--busy) 50%, var(--panel)); }
.lg.plan { border: 1.5px dashed var(--muted); }

/* ---------- timeline ---------- */
.tl {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow-x: auto;
  overscroll-behavior-x: contain;
  container-type: inline-size;
  scrollbar-width: thin;
}
.tl-inner { width: max-content; min-width: 100%; }
.tl-row { display: flex; border-top: 1px solid var(--line); min-height: 78px; }
.tl-row.axis { border-top: 0; min-height: 30px; }
.tl-label {
  position: sticky;
  left: 0;
  z-index: 3;
  width: var(--label);
  flex: none;
  padding: 10px 14px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 4px;
  justify-content: center;
}
.axis .tl-label { padding: 4px 14px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; }
.tl-row.is-mine .tl-label { box-shadow: inset 3px 0 0 var(--accent); }
.tl-row.is-turn .tl-label { box-shadow: inset 3px 0 0 var(--turn); background: color-mix(in srgb, var(--turn) 7%, var(--panel)); }
.dname { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.dname > span:first-child { font-weight: 650; font-size: 15.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dname .note { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.st { font-size: 13px; color: var(--muted); }
.st.free::before, .st.busy::before, .st.over::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.st.free::before { background: var(--free); }
.st.busy::before { background: var(--busy); }
.st.over::before { background: var(--over); }
.tl-label .acts { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 2px; }
.tl-label .teams { display: flex; gap: 4px; flex-wrap: wrap; }

.tl-track { position: relative; flex: none; }
.tl-track.pickable { cursor: crosshair; }
.axis .tl-track { height: 30px; }
.tick, .day { position: absolute; top: 0; bottom: 0; font-size: 11.5px; color: var(--muted); padding: 7px 0 0 4px; border-left: 1px solid color-mix(in srgb, var(--line) 70%, transparent); font-variant-numeric: tabular-nums; }
.tick.night { opacity: 0.55; }
.day { color: var(--text); font-weight: 650; border-left: 2px solid var(--line); padding-left: 6px; white-space: nowrap; }
.now-pill { position: absolute; top: 5px; transform: translateX(-50%); background: #e5484d; color: #fff; font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 999px; z-index: 2; font-variant-numeric: tabular-nums; }
.tl-track .past { position: absolute; left: 0; top: 0; bottom: 0; background: repeating-linear-gradient(135deg, transparent 0 6px, color-mix(in srgb, var(--line) 45%, transparent) 6px 7px); pointer-events: none; }
.tl-track .now { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: #e5484d; z-index: 2; pointer-events: none; }

.blk {
  position: absolute;
  top: 12px;
  bottom: 12px;
  border-radius: 7px;
  padding: 5px 8px;
  font-size: 12.5px;
  line-height: 1.25;
  overflow: hidden;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 4px;
  transition: filter 120ms ease, transform 80ms ease;
  z-index: 1;
}
.blk:hover { filter: brightness(1.06); }
.blk:active { transform: scale(0.98); }
.blk span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; position: relative; }
.blk .who { font-weight: 650; }
.blk .when { opacity: 0.85; font-variant-numeric: tabular-nums; }
.blk.active { background: var(--busy); color: #fff; }
.blk.active.mine { background: var(--accent); }
.blk.waiting { background: color-mix(in srgb, var(--busy) 18%, var(--panel)); border: 1px solid color-mix(in srgb, var(--busy) 45%, var(--panel)); color: var(--text); }
.blk.waiting.mine { background: color-mix(in srgb, var(--accent) 16%, var(--panel)); border-color: var(--accent); }
.blk.waiting.shifted { border-style: solid; box-shadow: inset 3px 0 0 var(--over); }
.blk.done { background: color-mix(in srgb, var(--line) 70%, var(--panel)); color: var(--muted); top: 22px; bottom: 22px; padding: 0 6px; font-size: 11.5px; }
/* The overrun: the part of an active block past its planned end, in stripes. */
.blk .overrun { position: absolute; top: 0; bottom: 0; right: 0; background: repeating-linear-gradient(135deg, var(--over) 0 6px, color-mix(in srgb, var(--over) 60%, #fff) 6px 12px); }
.plan-ghost { position: absolute; top: 9px; bottom: 9px; border: 1.5px dashed color-mix(in srgb, var(--muted) 70%, transparent); border-radius: 8px; pointer-events: none; }
.sel { position: absolute; top: 8px; bottom: 8px; background: color-mix(in srgb, var(--accent) 22%, transparent); border: 2px solid var(--accent); border-radius: 8px; z-index: 2; font-size: 12px; font-weight: 600; padding: 4px 6px; color: var(--accent); pointer-events: none; white-space: nowrap; }
.fine { color: var(--muted); font-size: 13px; margin: 6px 0 10px; }
.fine.below { margin-top: 8px; }

/* ---------- empty + skeleton ---------- */
.empty-state { text-align: center; padding: 44px 20px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.empty-state h3 { color: var(--text); margin: 0 0 6px; font-size: 17px; }
.empty-state p { margin: 0 auto 16px; max-width: 420px; }
.sk { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-top: 60px; }
.sk-row { display: flex; gap: 20px; padding: 26px 16px; border-top: 1px solid var(--line); }
.sk-row:first-child { border-top: 0; }
.skeleton { height: 22px; border-radius: 6px; background: linear-gradient(90deg, var(--line) 25%, color-mix(in srgb, var(--line) 45%, var(--panel)) 50%, var(--line) 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
.skeleton.grow { flex: 1; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- login ---------- */
.login { max-width: 360px; margin: 11vh auto 0; padding: 0 20px 40px; }
.login .logo { width: 56px; height: 56px; display: block; margin-bottom: 14px; }
.login h1 { font-size: 24px; margin: 0 0 4px; }
.login p.lead { color: var(--muted); margin: 0 0 22px; }
.login form > button { width: 100%; min-height: 44px; }
.login .names { margin-bottom: 8px; }
.login .switch { margin-top: 18px; font-weight: 500; font-size: 14px; color: var(--muted); }
label { display: block; font-size: 13px; color: var(--muted); margin: 0 0 4px; }
input, select {
  font: inherit;
  font-size: 16px; /* no iOS zoom on focus */
  width: 100%;
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  margin-bottom: 14px;
}
select { appearance: auto; min-height: 42px; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.error { color: var(--danger); font-size: 14px; margin: -4px 0 12px; min-height: 1em; }
.hint { color: var(--muted); font-size: 13px; margin-top: 16px; }
label.check { display: flex; align-items: center; gap: 6px; margin: -6px 0 12px; cursor: pointer; }
label.check input { width: auto; margin: 0; }
label.pill { margin: 0; padding: 5px 12px; border: 1px solid var(--line); border-radius: 999px; color: var(--text); font-size: 14px; }
label.pill:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--panel)); }
.chips.boxes { margin-bottom: 12px; }

/* ---------- admin ---------- */
.admin-grid { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 28px; align-items: start; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 14px; overflow: hidden; }
.card-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px 8px; }
.card-head h3 { margin: 0; font-size: 16px; flex: 1; display: flex; align-items: baseline; gap: 8px; }
.card-head .count { color: var(--muted); font-size: 13px; font-weight: 400; }
.card-head .acts { display: flex; gap: 2px; }
ul.people { list-style: none; margin: 0; padding: 0; }
ul.people li { display: flex; align-items: center; gap: 8px; padding: 8px 14px; border-top: 1px solid var(--line); }
ul.people .pname { flex: 1; font-weight: 550; }
ul.people .grow { flex: 1; min-width: 0; }
ul.people .note { color: var(--muted); font-size: 13px; }
ul.people .teams { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
ul.devices li { padding: 10px 14px; }
ul.devices li:first-child { border-top: 0; }
.pad { padding: 0 14px; }
form.inline { display: flex; gap: 8px; padding: 10px 14px 14px; border-top: 1px solid var(--line); }
form.inline input { margin: 0; }

/* ---------- dialog + toast ---------- */
dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  padding: 20px;
  width: min(440px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
}
dialog::backdrop { background: rgb(0 0 0 / 0.35); }
dialog h3 { margin: 0 0 12px; font-size: 17px; }
dialog p { margin: 0 0 12px; color: var(--muted); }
dialog .buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
dialog .buttons.wrap { flex-wrap: wrap; }
dialog .buttons .spacer { flex: 1; }
.range-grid { display: grid; grid-template-columns: 1.3fr 1fr 1fr; gap: 10px; }
.range-grid select { margin-bottom: 10px; min-width: 0; }
dialog select { margin-bottom: 10px; }
.range-info { font-weight: 600; margin: 12px 0 8px; font-variant-numeric: tabular-nums; }
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  width: max-content;
  max-width: calc(100vw - 32px);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.2);
  z-index: 10;
}

/* ---------- phone ---------- */
@media (max-width: 720px) {
  .wrap { padding: 12px 12px 40px; }
  header.top { flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
  header.top h1 { font-size: 17px; }
  header.top h1 span { display: none; }
  nav.seg { margin-left: 0; }
  .who .team { display: none; }
  .legend, .hide-phone { display: none; }
  .chips.days { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; width: 100%; margin: 0 -12px; padding: 0 12px; }
  .chips.days .chip-btn { flex: none; }
  .banner { flex-direction: column; align-items: stretch; gap: 10px; }
  .banner .acts { justify-content: flex-start; }
  /* Each device: its name, status and buttons across the full width, the track below it. */
  .tl-row { flex-direction: column; min-height: 0; }
  .tl-row.axis .tl-label { display: none; }
  .tl-label { width: 100cqw; border-right: 0; border-bottom: 1px dashed var(--line); padding: 10px 12px; }
  .tl-label .acts button { flex: 1; }
  .tl-row:not(.axis) .tl-track { height: 64px; }
  .blk { top: 9px; bottom: 9px; }
  .blk.done { top: 18px; bottom: 18px; }
  button, button.small { min-height: 40px; }
  .chip-btn { min-height: 34px; }
  .admin-grid { grid-template-columns: 1fr; gap: 10px; }
  ul.people li { flex-wrap: wrap; }
  ul.devices .teams { width: 100%; justify-content: flex-start; order: 3; }
  .range-grid { grid-template-columns: 1fr 1fr; }
  .range-grid > div:first-child { grid-column: 1 / -1; }
  dialog { padding: 18px 16px; }
}
@media (min-width: 721px) {
  .tl-row:not(.axis) .tl-track { min-height: 78px; }
}
