:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f0efec;
  --border: #e2e0dc;
  --text: #1c1b1a;
  --muted: #6b6864;
  --accent: #e00122; /* UC red */
  --accent-ink: #ffffff;
  --accent-soft: #fde8eb;
  --free: #e7f4ec;
  --free-ink: #1d6b3b;
  --busy: #c9c6c1;
  --busy-ink: #3d3b38;
  --closed: #e9e7e3;
  --sel: #2563eb;
  --sel-soft: rgba(37, 99, 235, 0.18);
  --warn-bg: #fff5e0;
  --warn-ink: #7a4b00;
  --ok-bg: #e7f4ec;
  --ok-ink: #14532d;
  --err-bg: #fdecec;
  --err-ink: #8a1c1c;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 4px 16px rgba(0, 0, 0, 0.06);
  --tabbar: 0px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151514;
    --surface: #1e1e1c;
    --surface-2: #2a2a27;
    --border: #353431;
    --text: #ecebe8;
    --muted: #9c9993;
    --accent: #ff4d63;
    --accent-ink: #1a0005;
    --accent-soft: #3a1a20;
    --free: #18291f;
    --free-ink: #8fd6a8;
    --busy: #4a4844;
    --busy-ink: #e6e4e0;
    --closed: #242422;
    --sel: #60a5fa;
    --sel-soft: rgba(96, 165, 250, 0.22);
    --warn-bg: #3a2c10;
    --warn-ink: #f6d38d;
    --ok-bg: #16301f;
    --ok-ink: #a7e3bd;
    --err-bg: #3a1717;
    --err-ink: #f4b4b4;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font: inherit; color: inherit; }

input, select, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 0;
  min-height: 38px;
}
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--sel);
  outline-offset: 1px;
}

button {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 14px;
  min-height: 38px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { background: var(--surface-2); }
button.primary, a.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover { filter: brightness(1.06); }
button:disabled { opacity: 0.55; cursor: default; }
button.ghost { background: none; border-color: transparent; padding: 6px 8px; }
button.ghost:hover { background: var(--surface-2); }

a { color: var(--sel); }
a.button { display: inline-block; padding: 9px 14px; border-radius: 8px; text-decoration: none; }
.muted { color: var(--muted); }
small { color: var(--muted); font-size: 12px; }

/* ---------- Header + tabs ---------- */

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 20px;
  padding: 10px 24px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 16px; }
.logo {
  display: grid; place-items: center;
  width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); font-weight: 800; font-size: 14px;
}
.tabs { display: flex; gap: 4px; }
.tabs button { display: flex; align-items: center; gap: 6px; border: none; background: none; padding: 8px 12px; color: var(--muted); }
.tabs button:hover { background: var(--surface-2); color: var(--text); }
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.tab-icon { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabs button.active .tab-icon path[d^="m12"] { fill: currentColor; }
.count:not(:empty) {
  display: inline-block; min-width: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--surface-2); color: var(--muted); font-size: 12px; text-align: center;
}
.account { margin-left: auto; display: flex; align-items: center; gap: 6px; color: var(--muted); }
.who-am-i { display: flex; align-items: center; gap: 6px; color: var(--text); font-weight: 500; }
.who-am-i .dot { width: 8px; height: 8px; border-radius: 50%; background: #22c55e; }

main { max-width: 1200px; margin: 0 auto; padding: 18px 24px 60px; }
.view { display: none; }
.view.active { display: block; }

.panel {
  padding: 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}

/* ---------- Day chips ---------- */

.days {
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
  margin: -2px -14px 0; padding: 2px 14px 2px;
  scroll-padding-inline: 14px;
}
.days::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  min-height: 36px; padding: 6px 14px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-size: 13.5px; cursor: pointer; white-space: nowrap;
}
.chip:hover { background: var(--surface-2); }
.chip.on { background: var(--text); border-color: var(--text); color: var(--surface); font-weight: 600; }
.days .chip-other { position: relative; overflow: hidden; flex-direction: row; color: var(--muted); font-size: 13.5px; font-weight: 400; }
.days .chip-other.on { color: var(--surface); font-weight: 600; }
.chip-other input { position: absolute; inset: 0; opacity: 0; cursor: pointer; min-height: 0; padding: 0; border: 0; }

/* ---------- Find ---------- */

.find-form { display: flex; flex-direction: column; gap: 12px; }
.find-form label, .more-filters label, #bookForm label {
  display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500;
}
.find-form label > *, #bookForm label > input { color: var(--text); font-size: 14px; }
.when { display: grid; grid-template-columns: 1fr 1fr 110px; gap: 10px; max-width: 520px; }
.search-row { display: flex; gap: 8px; }
.search-row input { flex: 1; }
.more-filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; align-items: end; }
.more-filters[hidden] { display: none; }
.more-filters .check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; min-height: 38px; }
.more-filters .check input { min-height: 0; width: 18px; height: 18px; }

.status { display: flex; align-items: center; gap: 8px; margin: 16px 2px 10px; color: var(--muted); min-height: 20px; }
.status strong { color: var(--text); }
.spinner {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.results { display: flex; flex-direction: column; gap: 8px; }

.room {
  display: grid; grid-template-columns: auto minmax(200px, 300px) 1fr auto; align-items: center; gap: 6px 14px;
  padding: 12px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.room .who, .grid .who { min-width: 0; }
.name { font-weight: 600; }
.meta { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.facts { font-size: 12.5px; color: var(--muted); margin-top: 1px; }
.free { color: var(--free-ink); font-weight: 500; }
.badge { color: var(--warn-ink); font-weight: 500; }
.room .book { min-width: 76px; }

.star {
  font-size: 19px; line-height: 1; width: 34px; height: 34px; min-height: 0; padding: 0;
  color: var(--border); border: none; background: none; border-radius: 50%;
}
.star.on { color: #f5a524; }
.star:hover { background: var(--surface-2); color: #f5a524; }

.more { align-self: center; margin-top: 8px; }
.empty {
  padding: 32px 20px; text-align: center; color: var(--muted);
  border: 1px dashed var(--border); border-radius: var(--radius);
}
.empty p { margin: 0 0 12px; }
.tips { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }

/* ---------- Timeline ---------- */

.timeline { position: relative; min-width: 0; }
.track {
  position: relative; height: 28px; border-radius: 6px;
  background: var(--free); overflow: hidden; touch-action: pan-y;
}
.track.pickable { cursor: crosshair; }
.track .tick { position: absolute; top: 0; bottom: 0; width: 1px; background: var(--border); opacity: 0.7; pointer-events: none; }
.track .closed {
  position: absolute; top: 0; bottom: 0;
  background: var(--closed) repeating-linear-gradient(135deg, transparent 0 5px, var(--border) 5px 6px);
  cursor: not-allowed;
}
.track .busy {
  position: absolute; top: 3px; bottom: 3px; border-radius: 4px;
  background: var(--busy); color: var(--busy-ink); font-size: 11px; line-height: 22px;
  padding: 0 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer;
}
.track .sel, .track .draft {
  position: absolute; top: 0; bottom: 0; border: 2px solid var(--sel); background: var(--sel-soft);
  border-radius: 6px; pointer-events: none;
}
.track .now { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--accent); pointer-events: none; }
.axis { position: relative; height: 16px; font-size: 10.5px; color: var(--muted); }
.axis span { position: absolute; transform: translateX(-50%); top: 2px; }
.axis span:first-child { transform: none; }
.axis span:last-child { transform: translateX(-100%); }

/* ---------- Saved ---------- */

.saved-bar { display: flex; flex-direction: column; gap: 12px; }
.addroom { position: relative; max-width: 420px; }
.addroom input { width: 100%; }
.menu {
  position: absolute; z-index: 10; left: 0; right: 0; top: calc(100% + 4px);
  margin: 0; padding: 4px; list-style: none; max-height: 320px; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.menu li { padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu li.active, .menu li:hover { background: var(--surface-2); }
.menu li .meta { color: var(--muted); font-size: 12px; }
.hint { color: var(--muted); margin: 12px 2px; font-size: 13px; }

.grid {
  display: grid; grid-template-columns: minmax(200px, 300px) 1fr;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
}
.grid > .cell { padding: 10px 14px; border-top: 1px solid var(--border); min-width: 0; }
.grid > .head { border-top: none; padding-top: 10px; padding-bottom: 0; }
.grid .label { display: flex; align-items: center; gap: 6px; padding-left: 6px; }
.grid .cell.tl { display: flex; align-items: center; }
.grid .cell.tl .timeline { flex: 1; }

/* ---------- Dialogs ---------- */

dialog {
  width: min(560px, calc(100vw - 32px)); padding: 0;
  border: 1px solid var(--border); border-radius: 16px;
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
#bookForm, #infoBody { display: flex; flex-direction: column; gap: 12px; padding: 20px 22px 6px; }
#bookForm header h2, #infoBody h2 { margin: 0; font-size: 19px; }
#bookForm header p { margin: 2px 0 0; }
.book-when { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 10px; }
dialog menu { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin: 0; padding: 12px 22px 18px; }
#bookForm menu { padding: 6px 0 14px; }
dialog menu .quiet { margin-right: auto; font-size: 13px; color: var(--muted); }
#bookSubmit { min-width: 110px; }

.note { padding: 10px 12px; border-radius: 8px; font-size: 13px; }
.limits { margin: -4px 2px 0; font-size: 12.5px; color: var(--muted); }
.note.warn { background: var(--warn-bg); color: var(--warn-ink); }
.note.ok { background: var(--ok-bg); color: var(--ok-ink); }
.note.err { background: var(--err-bg); color: var(--err-ink); }
.note.info { background: var(--surface-2); }
.note ul { margin: 2px 0 4px; padding-left: 18px; }
.note p { margin: 0 0 4px; }
.note p:last-child { margin-bottom: 0; }
.note button { margin-top: 6px; }
.note details { margin-top: 6px; }
.note pre, #infoBody pre { max-height: 220px; overflow: auto; font-size: 11px; white-space: pre-wrap; word-break: break-all; }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + var(--tabbar) + env(safe-area-inset-bottom)); z-index: 20;
  transform: translateX(-50%); max-width: calc(100vw - 32px);
  padding: 10px 16px; border-radius: 10px;
  background: var(--text); color: var(--surface); font-size: 13.5px; box-shadow: var(--shadow);
}
.toast[hidden] { display: none; }

/* Touch screens have no hover, so a tapped star shouldn't look half-starred. */
@media (hover: none) {
  .star:hover { background: none; }
  .star:not(.on):hover { color: var(--border); }
}

/* ---------- Phones ---------- */

@media (max-width: 760px) {
  :root { --tabbar: 60px; }
  .top { gap: 10px; padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top)); }
  .brand { font-size: 15px; }
  main { padding: 12px 12px calc(28px + var(--tabbar) + env(safe-area-inset-bottom)); }

  /* Tabs become a bottom bar, in thumb reach. */
  .tabs {
    position: fixed; z-index: 6; left: 0; right: 0; bottom: 0;
    height: calc(var(--tabbar) + env(safe-area-inset-bottom)); padding: 4px 8px env(safe-area-inset-bottom);
    background: var(--surface); border-top: 1px solid var(--border); gap: 0;
  }
  .tabs button { flex: 1; flex-direction: column; justify-content: center; gap: 2px; font-size: 11.5px; border-radius: 10px; }
  .tabs button.active { background: none; }
  .tab-icon { width: 22px; height: 22px; }
  .tabs .count:not(:empty) { position: absolute; margin: -2px 0 0 26px; font-size: 10.5px; }
  .tabs button { position: relative; }

  /* 16px stops iOS Safari from zooming in when a field is focused. */
  input, select, textarea { font-size: 16px !important; }

  .panel { padding: 12px; border-radius: 14px; }
  .days { margin-inline: -12px; padding-inline: 12px; scroll-padding-inline: 12px; }
  .when { grid-template-columns: 1fr 1fr 84px; gap: 8px; max-width: none; }

  .room { grid-template-columns: auto 1fr auto; gap: 8px 8px; padding: 10px 12px 12px 6px; }
  .room .timeline { grid-column: 1 / -1; grid-row: 2; padding-left: 6px; }
  .room .meta { white-space: normal; display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; }

  .grid { grid-template-columns: 1fr; }
  .grid > .head:first-child { display: none; }
  .grid > .head { padding-top: 8px; }
  .grid .cell.label { padding-bottom: 0; }
  .grid .cell.tl { border-top: none; padding-top: 6px; }

  /* The booking dialog slides up from the bottom like a sheet. */
  dialog {
    width: 100%; max-width: 100%; margin: auto 0 0; max-height: 92vh;
    border-radius: 18px 18px 0 0; border-bottom: none;
    padding-bottom: env(safe-area-inset-bottom);
  }
  #bookForm, #infoBody { padding: 18px 16px 4px; }
  .book-when { grid-template-columns: 1fr 1fr; }
  .book-when .date { grid-column: 1 / -1; }
  dialog menu { flex-wrap: wrap; padding: 10px 16px 16px; }
  #bookForm menu { padding: 4px 0 12px; }
  dialog menu button { flex: 1; min-height: 44px; }
  dialog menu .quiet { order: 3; flex-basis: 100%; text-align: center; margin: 6px 0 0; }
}
