:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f1f2f5;
  --ink: #14171f;
  --muted: #5f6675;
  --line: #e3e5ea;
  --primary: #14171f;
  --on-primary: #ffffff;
  --accent: #1d5fd1;
  --tile: #14171f;
  --on-tile: #ffffff;
  --ok: #0c7a4c;
  --ok-soft: #e2f5ec;
  --bad: #b42318;
  --bad-soft: #fdeceb;
  --warn: #8a5a00;
  --warn-soft: #fdf3dc;
  --shadow: 0 1px 2px rgb(20 23 31 / 5%), 0 6px 20px rgb(20 23 31 / 5%);
  --radius: 18px;
  --radius-sm: 12px;
  --bottom-nav: 68px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1014;
    --surface: #171a21;
    --surface-2: #20242d;
    --ink: #eff1f5;
    --muted: #a0a7b5;
    --line: #2a2f3a;
    --primary: #eff1f5;
    --on-primary: #14171f;
    --accent: #8db4ff;
    --tile: #262b36;
    --on-tile: #ffffff;
    --ok: #5fd6a2;
    --ok-soft: #14352a;
    --bad: #ff9a90;
    --bad-soft: #3f1f1d;
    --warn: #f2c66d;
    --warn-soft: #3a2f16;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

body.centered { display: grid; place-items: center; min-height: 100dvh; padding: 16px; }

h1, h2, h3 { line-height: 1.25; margin: 0; letter-spacing: -.01em; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
p { margin: 0; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }

button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Shell */
.topbar {
  position: sticky;
  top: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar img { width: 40px; height: 40px; border-radius: 11px; background: #fff; border: 1px solid var(--line); padding: 3px; }
.topbar strong { display: block; font-size: 1rem; line-height: 1.2; }
.topbar span { display: block; font-size: .8rem; color: var(--muted); }

.page {
  width: 100%;
  max-width: 680px;
  margin: 0 auto;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + var(--bottom-nav) + 24px);
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}
.page.wide { max-width: 1000px; }
.page.prose { padding-bottom: 32px; gap: 10px; }
.page.prose h2 { margin-top: 10px; }

.tabs {
  position: fixed;
  inset: auto 0 0;
  z-index: 3;
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding: 6px 6px calc(env(safe-area-inset-bottom) + 6px);
}
.tabs a {
  flex: 1 0 auto;
  min-width: 62px;
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 6px 4px;
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: var(--muted);
  font-size: .72rem;
  font-weight: 500;
}
.tabs a svg { width: 22px; height: 22px; }
.tabs a[aria-current="page"] { background: var(--surface-2); color: var(--ink); font-weight: 700; }

@media (min-width: 900px) {
  #app.shell {
    display: grid;
    grid-template-columns: 228px minmax(0, 1fr);
    grid-template-rows: auto 1fr;
    min-height: 100dvh;
  }
  #app.shell .topbar { grid-column: 1; grid-row: 1; position: static; border: 0; background: transparent; backdrop-filter: none; padding: 20px 16px 12px; }
  #app.shell .page { grid-column: 2; grid-row: 1 / span 2; padding: 28px 28px 40px; }
  #app.shell .tabs {
    grid-column: 1;
    grid-row: 2;
    position: sticky;
    top: 0;
    align-self: start;
    display: grid;
    gap: 4px;
    padding: 4px 12px;
    background: transparent;
    border: 0;
  }
  #app.shell .tabs a { display: flex; align-items: center; gap: 10px; padding: 10px 12px; font-size: .92rem; }
  #app.shell .tabs a[aria-current="page"] { background: var(--surface); box-shadow: var(--shadow); }
}

/* Surfaces */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 14px;
  min-width: 0;
}
.card.narrow { width: 100%; max-width: 420px; padding: 24px; }

.muted { color: var(--muted); font-size: .9rem; }
.notice, .message { padding: 10px 12px; border-radius: var(--radius-sm); font-size: .9rem; overflow-wrap: anywhere; }
.notice { background: var(--warn-soft); color: var(--warn); }
.message.bad { background: var(--bad-soft); color: var(--bad); }
.message.ok { background: var(--ok-soft); color: var(--ok); }

.list { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.item { display: grid; gap: 6px; padding: 12px 0; border-top: 1px solid var(--line); min-width: 0; overflow-wrap: anywhere; }
.item:first-child { border-top: 0; padding-top: 0; }
.item:last-child { padding-bottom: 0; }
.pre { white-space: pre-wrap; }

.tag { justify-self: start; padding: 2px 10px; border-radius: 999px; font-size: .76rem; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.tag.bad { background: var(--bad-soft); color: var(--bad); }
.tag.ok { background: var(--ok-soft); color: var(--ok); }

/* Gates */
.zone { display: grid; gap: 10px; }
.zone h2 { font-size: .95rem; color: var(--muted); font-weight: 600; }
.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.tile {
  min-height: 116px;
  border: 0;
  border-radius: var(--radius);
  background: var(--tile);
  color: var(--on-tile);
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 6px;
  padding: 12px;
  font-size: 1.05rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color .15s, transform .1s;
}
/* A gate left alone on its row takes the whole row. */
.tile:last-child:nth-child(odd) { grid-column: 1 / -1; }
.tile:active { transform: scale(.98); }
.tile svg { width: 30px; height: 30px; }
.tile small { font-size: .78rem; font-weight: 400; min-height: 1.2em; }
.tile.busy { opacity: .7; }
.tile.opened { background: #0c7a4c; color: #fff; }
.tile.failed { background: #b42318; color: #fff; }

/* Forms */
form { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
label { display: grid; gap: 5px; font-size: .88rem; font-weight: 500; min-width: 0; }
input, select, textarea {
  width: 100%;
  min-width: 0;
  min-height: 46px;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: 16px;
}
input:focus, select:focus, textarea:focus { background: var(--surface); border-color: var(--line); }
input[type="date"] { padding-inline: 8px; font-size: 14px; }
textarea { min-height: 110px; resize: vertical; }
.row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
form.inline { grid-template-columns: minmax(0, 1fr) auto; align-items: end; }
fieldset.checks { display: grid; gap: 10px; margin: 0; padding: 0; border: 0; min-width: 0; }
fieldset.checks legend { padding: 0 0 6px; font-size: .88rem; font-weight: 500; }
label.check { display: flex; align-items: center; gap: 10px; font-weight: 400; }
label.check input { width: 22px; min-height: 22px; flex: none; accent-color: var(--primary); }

button.btn {
  min-height: 46px;
  padding: 10px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: var(--on-primary);
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s;
}
button.btn:hover { opacity: .88; }
button.btn.quiet { background: transparent; color: var(--ink); border-color: var(--line); }
button.btn.danger { background: transparent; color: var(--bad); border-color: var(--line); }
button.btn.small { min-height: 40px; padding: 7px 12px; font-size: .86rem; }
button:disabled { opacity: .5; cursor: default; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.actions > button { flex: 1 1 auto; }
a.btn-link {
  flex: 1 1 auto;
  display: grid;
  place-items: center;
  min-height: 40px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  color: var(--ink);
  font-size: .86rem;
  font-weight: 600;
  text-decoration: none;
}

/* Collapsible days and roles */
.day { border-top: 1px solid var(--line); padding-top: 10px; }
.day:first-child { border-top: 0; padding-top: 0; }
.day summary { position: relative; padding: 4px 0 4px 22px; cursor: pointer; font-weight: 600; list-style: none; }
.day summary::-webkit-details-marker { display: none; }
.day summary::before { content: "›"; position: absolute; left: 4px; top: 0; font-size: 1.3rem; line-height: 1.4; transition: transform .15s; }
.day[open] summary::before { transform: rotate(90deg); }
.day summary span { display: block; }
.day summary span:first-child::first-letter { text-transform: uppercase; }
.day summary .muted { font-weight: 400; }
.day[open] summary { margin-bottom: 8px; }

/* Administration */
.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.stat {
  display: grid;
  gap: 2px;
  padding: 14px;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
}
.stat strong { font-size: 1.6rem; line-height: 1.1; letter-spacing: -.02em; }
.stat span { font-size: .82rem; color: var(--muted); }
.stat.alert strong { color: var(--bad); }

.segments {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 4px;
  background: var(--surface-2);
  border-radius: 14px;
}
.segments button {
  flex: 1 0 auto;
  min-height: 40px;
  padding: 6px 8px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: .84rem;
  font-weight: 600;
  cursor: pointer;
}
.segments button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
@media (max-width: 420px) {
  .segments button { padding: 6px 5px; font-size: .78rem; }
}
.count { display: inline-block; min-width: 20px; margin-left: 6px; padding: 0 6px; border-radius: 999px; background: var(--bad); color: #fff; font-size: .74rem; line-height: 20px; text-align: center; }

.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips button {
  flex: none;
  min-height: 36px;
  padding: 4px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: .84rem;
  font-weight: 500;
  cursor: pointer;
}
.chips button[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }

.panel { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.user { border-top: 1px solid var(--line); }
.user:first-child { border-top: 0; }
.user summary { display: flex; align-items: center; gap: 12px; padding: 12px 2px; cursor: pointer; list-style: none; }
.user summary::-webkit-details-marker { display: none; }
.user summary::after { content: "›"; flex: none; color: var(--muted); font-size: 1.3rem; line-height: 1; transition: transform .15s; }
.user[open] > summary::after { transform: rotate(90deg); }
.avatar { flex: none; display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%; background: var(--surface-2); font-size: .85rem; font-weight: 700; }
.who { flex: 1; min-width: 0; }
.who strong, .who span { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.who span { color: var(--muted); font-size: .84rem; }
.dot { flex: none; width: 10px; height: 10px; border-radius: 50%; background: var(--ok); }
.dot.bad { background: var(--bad); }
.user-body { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; padding: 2px 2px 16px; overflow-wrap: anywhere; }
.user-section { display: grid; gap: 8px; padding-top: 12px; border-top: 1px dashed var(--line); }
.user-section > h3 { font-size: .8rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.subs { margin-left: 8px; padding-left: 10px; border-left: 2px solid var(--line); }

@media (min-width: 720px) {
  .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

/* Sign-in */
.login-brand { text-align: center; display: grid; gap: 6px; justify-items: center; }
.login-brand img { width: 148px; height: 148px; padding: 8px; border-radius: 28px; background: #fff; }
