:root {
  --bg: #ffffff;
  --ink: #111111;
  --muted: #666666;
  --line: #e5e5e5;
  --card: #ffffff;
  --accent: #111111;
  --accent-bg: #f5f5f5;
  --danger: #b00020;
  --ok: #0a7a32;
  --radius: 8px;
  --font: "DM Sans", system-ui, sans-serif;
  --display: "DM Sans", system-ui, sans-serif;
}

body.theme-level-8 {
  --ink: #3b1f5e;
  --accent: #3b1f5e;
  --muted: #6b5a7a;
  --accent-bg: #f3eef8;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
}

a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.85rem 1.25rem;
  border-bottom: 1px solid var(--line);
  background: #fff;
  position: sticky;
  top: 0;
  z-index: 40;
}

.topbar-brand { display: flex; align-items: center; gap: 0.7rem; min-width: 180px; }
.brand-mark {
  width: 2.2rem; height: 2.2rem; border-radius: 6px;
  display: grid; place-items: center;
  background: var(--ink); color: #fff;
  font-weight: 700; font-size: 0.8rem;
}
body.theme-level-8 .shift-block {
  background: #3b1f5e;
}
body.theme-level-8 .btn:not(.btn-ghost):not(.btn-danger) {
  background: #3b1f5e;
}
body.theme-level-8 .week-day.is-active {
  border-color: #3b1f5e;
}
.brand-name { font-weight: 700; font-size: 0.95rem; line-height: 1.1; }
.brand-sub { font-size: 0.7rem; color: var(--muted); letter-spacing: 0.04em; text-transform: uppercase; }

.business-switcher { position: relative; }
.switcher-trigger, .switcher-item {
  display: flex; align-items: center; gap: 0.65rem;
  border: 1px solid var(--line); background: #fff;
  border-radius: 8px; padding: 0.4rem 0.8rem 0.4rem 0.45rem;
  cursor: pointer;
}
.switcher-trigger:hover, .switcher-item:hover { border-color: #bbb; }
.switcher-logo, .switcher-initials {
  width: 1.8rem; height: 1.8rem; border-radius: 50%;
  object-fit: cover; flex-shrink: 0;
}
.switcher-initials {
  display: grid; place-items: center;
  background: var(--accent-bg); color: var(--ink);
  font-size: 0.7rem; font-weight: 700;
}
.switcher-label { font-weight: 600; }
.switcher-chevron {
  width: 0.45rem; height: 0.45rem;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg); margin-left: 0.2rem; margin-bottom: 0.2rem;
}
.switcher-menu {
  position: absolute; top: calc(100% + 0.4rem); left: 0;
  min-width: 240px; padding: 0.4rem;
  background: #fff; border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,0.08); z-index: 50;
}
.switcher-item-form { margin: 0; }
.switcher-item {
  width: 100%; border-radius: 6px; border: none; background: transparent;
  padding: 0.55rem 0.65rem; text-align: left;
}
.switcher-item.is-active { background: var(--accent-bg); }

.topnav { display: flex; gap: 0.25rem; margin-left: auto; }
.topnav a {
  padding: 0.45rem 0.75rem; border-radius: 6px;
  color: var(--muted); font-weight: 500; font-size: 0.92rem;
}
.topnav a:hover { color: var(--ink); background: var(--accent-bg); }
.topnav a.is-active { color: var(--ink); background: var(--accent-bg); font-weight: 600; }

.topbar-user { display: flex; align-items: center; gap: 0.6rem; margin-left: 0.5rem; }
.user-name { font-size: 0.85rem; color: var(--muted); }

.page { padding: 1.4rem 1.25rem 3rem; max-width: 1400px; margin: 0 auto; }

.flash {
  padding: 0.75rem 1rem; border-radius: 8px; margin-bottom: 1rem;
  border: 1px solid var(--line); background: #fff;
}
.flash-ok { border-color: #86efac; background: #f0fdf4; color: var(--ok); }
.flash-error { border-color: #fecdd3; background: #fff1f2; color: var(--danger); }
.flash-info { border-color: var(--line); background: var(--accent-bg); }

.page-head {
  display: flex; flex-wrap: wrap; align-items: flex-end;
  justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem;
}
.page-head h1 {
  margin: 0; font-weight: 700;
  font-size: clamp(1.5rem, 2.5vw, 1.9rem);
}
.page-head p { margin: 0.25rem 0 0; color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  border: 1px solid transparent; border-radius: 6px;
  padding: 0.55rem 1rem; cursor: pointer; font-weight: 600;
  background: var(--ink); color: #fff;
}
.btn:hover { opacity: 0.9; }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn-accent { background: var(--ink); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { padding: 0.35rem 0.75rem; font-size: 0.85rem; }

.panel {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
}

.form-grid {
  display: grid; gap: 0.9rem;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}
.field { display: flex; flex-direction: column; gap: 0.35rem; }
.field label { font-size: 0.8rem; color: var(--muted); font-weight: 600; letter-spacing: 0.02em; }
.field input, .field select, .field textarea {
  border: 1px solid var(--line); border-radius: 6px;
  padding: 0.55rem 0.7rem; background: #fff;
  width: 100%;
  max-width: 100%;
  min-height: 2.6rem;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid rgba(0, 0, 0, 0.15); border-color: #111;
}

.table-wrap { overflow-x: auto; }
table.data {
  width: 100%; border-collapse: collapse; font-size: 0.92rem;
}
table.data th, table.data td {
  text-align: left; padding: 0.7rem 0.6rem;
  border-bottom: 1px solid var(--line);
}
table.data th { color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; }
table.data tr:hover td { background: #fafafa; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.wage { color: var(--ink); font-weight: 700; font-variant-numeric: tabular-nums; }

.auth-shell {
  min-height: 100vh; display: grid; place-items: center; padding: 1.5rem;
  background: #fff;
}
.auth-card {
  width: min(420px, 100%);
  background: #fff; border: 1px solid var(--line);
  border-radius: 10px; padding: 2rem;
}
.auth-card h1 {
  margin: 0 0 0.35rem; font-size: 1.6rem; font-weight: 700;
}
.auth-card .lede { color: var(--muted); margin: 0 0 1.4rem; }
.auth-card .field { margin-bottom: 0.9rem; }
.auth-card .btn { width: 100%; margin-top: 0.4rem; }

.week-strip {
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 0.4rem; margin-bottom: 1rem;
}
.week-day {
  display: flex; flex-direction: column; gap: 0.15rem;
  padding: 0.55rem 0.45rem; border-radius: 6px;
  border: 1px solid var(--line); background: #fff;
  text-align: center; color: inherit;
}
.week-day:hover { border-color: #bbb; }
.week-day.is-active { border-color: #111; background: var(--accent-bg); }
.week-day .d { font-size: 0.78rem; color: var(--muted); }
.week-day .w { font-weight: 700; font-size: 0.9rem; }

.rota-toolbar {
  display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center;
  margin-bottom: 1rem;
}
.rota-toolbar .spacer { flex: 1; }
.day-total {
  font-size: 1.35rem; font-weight: 700; color: var(--ink);
}

.rota-board-wrap {
  overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius);
  background: #fff;
}
.rota-board {
  display: grid;
  min-width: max-content;
  --label-w: 140px;
  --hour-w: 72px;
}
.rota-hours, .rota-row {
  display: grid;
  grid-template-columns: var(--label-w) repeat(var(--hours), var(--hour-w));
}
.rota-hours {
  position: sticky; top: 0; z-index: 5;
  background: #fafafa; border-bottom: 1px solid var(--line);
}
.rota-hours .cell, .rota-row .label {
  padding: 0.55rem 0.5rem; font-size: 0.78rem; color: var(--muted);
  border-right: 1px solid var(--line);
}
.rota-hours .cell { text-align: center; font-weight: 600; color: var(--ink); }
.rota-row { border-bottom: 1px solid var(--line); min-height: 64px; }
.rota-row .label {
  position: sticky; left: 0; z-index: 3;
  background: #fafafa; font-weight: 600; color: var(--ink);
  display: flex; align-items: center;
}
.rota-track {
  position: relative;
  grid-column: 2 / -1;
  background-image: repeating-linear-gradient(
    to right,
    transparent 0,
    transparent calc(var(--hour-w) - 1px),
    var(--line) calc(var(--hour-w) - 1px),
    var(--line) var(--hour-w)
  );
  min-height: 64px;
}
.shift-block {
  position: absolute;
  top: 6px;
  height: calc(100% - 12px);
  min-height: 44px;
  border-radius: 6px;
  background: #111;
  color: #fff;
  padding: 0.35rem 0.55rem;
  cursor: grab;
  user-select: none;
  overflow: hidden;
  z-index: 2;
}
.shift-block:active { cursor: grabbing; }
.shift-block .name { font-weight: 600; font-size: 0.82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.shift-block .meta { font-size: 0.7rem; opacity: 0.8; }
.shift-block .handle {
  position: absolute; top: 0; bottom: 0; width: 8px;
  cursor: ew-resize;
}
.shift-block .handle-left { left: 0; }
.shift-block .handle-right { right: 0; }
.shift-block .del {
  position: absolute; top: 2px; right: 4px;
  border: none; background: transparent; color: #fff;
  opacity: 0.55; cursor: pointer; font-size: 0.9rem; line-height: 1;
}
.shift-block .del:hover { opacity: 1; }

.rota-closed {
  padding: 3rem 1.5rem; text-align: center; color: var(--muted);
}
.rota-error {
  margin-top: 0.75rem; color: var(--danger); font-weight: 600; min-height: 1.2em;
}

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: grid; place-items: center; z-index: 100; padding: 1rem;
}
.modal-backdrop[hidden] {
  display: none !important;
}
.modal {
  width: min(420px, 100%); background: #fff;
  border-radius: 10px; border: 1px solid var(--line);
  box-shadow: 0 12px 40px rgba(0,0,0,0.15); padding: 1.25rem;
}
.modal h2 { margin: 0 0 0.75rem; font-size: 1.2rem; font-weight: 700; }
.modal-wide { width: min(520px, 100%); }
.modal-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 1rem; }

.position-row {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  margin-bottom: 0.65rem;
  padding: 0.55rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.position-row.is-dragging { opacity: 0.65; background: var(--accent-bg); }
.position-row.drag-over { border-color: var(--ink); }
.drag-handle {
  border: 1px solid var(--line);
  background: var(--accent-bg);
  border-radius: 6px;
  width: 2rem;
  height: 2.4rem;
  cursor: grab;
  color: var(--muted);
  font-size: 0.85rem;
  letter-spacing: -0.1em;
  flex-shrink: 0;
  margin-bottom: 0.1rem;
}
.drag-handle:active { cursor: grabbing; }

.inline-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.badge {
  display: inline-block; padding: 0.15rem 0.5rem; border-radius: 999px;
  font-size: 0.75rem; font-weight: 600;
}
.badge-ok { background: #dcfce7; color: var(--ok); }
.badge-off { background: #f4f4f5; color: var(--muted); }

.settings-section { margin-bottom: 1.5rem; }
.settings-section h2 {
  margin: 0 0 0.75rem; font-size: 1.1rem; font-weight: 700;
}

.form-grid-users {
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
}
.rota-hint { display: none; }

@media (max-width: 900px) {
  .topbar {
    flex-wrap: wrap;
    gap: 0.65rem;
    padding: 0.7rem 0.85rem;
  }
  .topbar-brand { min-width: 0; flex: 1; }
  .brand-sub { display: none; }
  .topnav {
    order: 4;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 0.15rem;
    gap: 0.15rem;
  }
  .topnav::-webkit-scrollbar { display: none; }
  .topnav a {
    flex: 0 0 auto;
    white-space: nowrap;
    padding: 0.55rem 0.8rem;
    font-size: 0.88rem;
  }
  .topbar-user { margin-left: 0; }
  .user-name { display: none; }
  .business-switcher { max-width: min(58vw, 220px); }
  .switcher-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 9rem;
  }
  .switcher-menu {
    left: auto;
    right: 0;
    min-width: min(260px, 92vw);
  }
  .page { padding: 1rem 0.85rem 5rem; }
  .panel { padding: 0.9rem; }
  .week-strip {
    gap: 0.3rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.2rem;
  }
  .week-day {
    min-width: 3.4rem;
    padding: 0.45rem 0.25rem;
  }
  .week-day .d { font-size: 0.68rem; }
  .week-day .w { font-size: 0.72rem; }
  .rota-toolbar {
    gap: 0.45rem;
  }
  .rota-toolbar .spacer { display: none; }
  .rota-hint {
    display: block;
    font-size: 0.8rem;
    color: var(--muted);
    margin: -0.35rem 0 0.75rem;
  }
  .rota-board {
    --label-w: 72px;
    --hour-w: 48px;
  }
  .rota-hours .cell,
  .rota-row .label {
    padding: 0.4rem 0.3rem;
    font-size: 0.68rem;
  }
  .shift-block {
    padding: 0.25rem 0.4rem;
    min-height: 40px;
    touch-action: none;
  }
  .shift-block .name { font-size: 0.74rem; }
  .shift-block .meta { font-size: 0.62rem; }
  .shift-block .handle { width: 12px; }
  .position-row {
    flex-wrap: wrap;
    align-items: stretch;
  }
  .position-row .field { flex: 1 1 120px; }
  .position-row .drag-handle {
    align-self: center;
    width: 2.4rem;
    height: 2.6rem;
  }
  .modal-backdrop {
    align-items: end;
    padding: 0;
  }
  .modal,
  .modal-wide {
    width: 100%;
    max-width: 100%;
    border-radius: 14px 14px 0 0;
    max-height: min(92vh, 900px);
    overflow-y: auto;
    padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom));
  }
  .modal-actions {
    flex-wrap: wrap;
  }
  .modal-actions .btn {
    min-height: 2.6rem;
  }
  .auth-card {
    padding: 1.4rem;
    border: none;
  }
  .auth-shell { padding: 1rem; align-items: start; padding-top: 12vh; }
  .day-total { font-size: 1.15rem; }
  table.data { font-size: 0.85rem; }
  table.data th, table.data td { padding: 0.55rem 0.4rem; }
}

@media (max-width: 560px) {
  .page-head h1 { font-size: 1.35rem; }
  .page-head p { font-size: 0.85rem; }
  .form-grid {
    grid-template-columns: 1fr;
  }
  .week-day .w {
    font-size: 0.65rem;
  }
  .rota-hint {
    font-size: 0.8rem;
    color: var(--muted);
    margin: 0 0 0.6rem;
  }
}

@media print {
  body { background: #fff; }
  .topbar, .rota-toolbar .btn, .week-strip, .flash { display: none !important; }
  .rota-board-wrap { box-shadow: none; }
}
