:root {
  --bg: #f6f7f4;
  --surface: #ffffff;
  --surface-soft: #fbfcfa;
  --input-bg: #ffffff;
  --line: #d9ded6;
  --text: #17211b;
  --muted: #66706b;
  --accent: #126d63;
  --accent-strong: #0a4f49;
  --accent-soft: #e6f3ef;
  --warn: #b76e21;
  --danger: #a33a3a;
  --topbar-bg: #1f2a24;
  --topbar-text: #ffffff;
  --topbar-muted: #cdd8d2;
  --nav-bg: #e8ece4;
  --shadow: rgba(31, 42, 36, .18);
  --button-shine: rgba(255, 255, 255, .32);
  --ok: #1f7a4d;
}

body[data-theme="blocks"] {
  --bg: #f7f0ff;
  --surface: #fffaff;
  --surface-soft: #fff4fb;
  --input-bg: #ffffff;
  --line: #e3d5f6;
  --text: #231d34;
  --muted: #6b6078;
  --accent: #2f80ed;
  --accent-strong: #8f2ee8;
  --accent-soft: #ecdcff;
  --topbar-bg: #7027c3;
  --topbar-text: #ffffff;
  --topbar-muted: #ffe4f7;
  --nav-bg: #dff8ed;
  --shadow: rgba(112, 39, 195, .20);
}

body[data-theme="studio"] {
  --bg: #f2f4f7;
  --surface: #ffffff;
  --surface-soft: #f7f9fb;
  --input-bg: #ffffff;
  --line: #d6dce4;
  --text: #111827;
  --muted: #5e6878;
  --accent: #0f766e;
  --accent-strong: #111827;
  --accent-soft: #dff7f2;
  --topbar-bg: #111827;
  --topbar-text: #ffffff;
  --topbar-muted: #cbd5e1;
  --nav-bg: #e8edf3;
  --shadow: rgba(17, 24, 39, .16);
}

body[data-theme="night"] {
  --bg: #101816;
  --surface: #17211f;
  --surface-soft: #1d2a27;
  --input-bg: #101816;
  --line: #30413c;
  --text: #eef8f3;
  --muted: #a9bbb3;
  --accent: #2dd4bf;
  --accent-strong: #5eead4;
  --accent-soft: #123c37;
  --topbar-bg: #08110f;
  --topbar-text: #ffffff;
  --topbar-muted: #a9bbb3;
  --nav-bg: #13201d;
  --shadow: rgba(0, 0, 0, .34);
  --button-shine: rgba(255, 255, 255, .16);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Arial, sans-serif;
  font-size: 14px;
}

body.locked {
  overflow: hidden;
}

[hidden] {
  display: none !important;
}

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
}

body.locked .auth-gate {
  display: flex;
}

.auth-card {
  width: min(460px, 100%);
  max-height: calc(100vh - 32px);
  overflow: auto;
  display: grid;
  gap: 18px;
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 45px var(--shadow);
}

.auth-card h1 {
  margin: 0;
  font-size: 24px;
}

.auth-card p {
  margin: 4px 0 0;
  color: var(--muted);
}

.auth-card details {
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.auth-card summary {
  cursor: pointer;
  font-weight: 700;
  color: var(--accent-strong);
}

.auth-message {
  display: none;
  padding: 10px 12px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--text);
  font-weight: 600;
}

.auth-message.show {
  display: block;
}

.auth-message.error {
  border-color: #e1b5b5;
  background: #fff3f3;
  color: var(--danger);
}

.auth-card details form {
  margin-top: 12px;
}

.topbar {
  min-height: 72px;
  display: flex;
  justify-content: space-between;
  gap: 24px;
  align-items: center;
  padding: 14px 24px;
  background: var(--topbar-bg);
  color: var(--topbar-text);
  transition: background-color .2s ease, color .2s ease;
}

.topbar h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 650;
}

.topbar p {
  margin: 3px 0 0;
  color: var(--topbar-muted);
}

.session {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.session span {
  border: 1px solid rgba(255,255,255,.24);
  padding: 6px 10px;
  border-radius: 6px;
  white-space: nowrap;
}

.session button {
  min-height: 32px;
  padding: 5px 10px;
  background: transparent;
  color: #fff;
  border-color: rgba(255,255,255,.28);
}

.mobile-menu-button {
  display: none;
}

.session-warning {
  border-color: color-mix(in srgb, var(--warn) 70%, #fff 30%) !important;
  color: #fff5df;
}

.shell {
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr);
  min-height: calc(100vh - 72px);
}

.tabs {
  background: var(--nav-bg);
  border-right: 1px solid var(--line);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
}

.nav-group {
  display: grid;
  gap: 6px;
}

.nav-group + .nav-group {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
}

.nav-group > span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  padding: 2px 8px;
}

.tab, button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  min-height: 36px;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  touch-action: manipulation;
  position: relative;
  overflow: hidden;
  transition: transform .14s ease, box-shadow .14s ease, background-color .18s ease, border-color .18s ease, color .18s ease;
}

.tab {
  text-align: left;
}

.tab.active, button[type="submit"], .primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button:hover {
  border-color: var(--accent-strong);
  box-shadow: 0 8px 18px var(--shadow);
  transform: translateY(-1px);
}

button:active {
  box-shadow: none;
  transform: translateY(1px) scale(.99);
}

button[type="submit"]::after,
.metric-grid button::after,
.theme-option::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(120deg, transparent 0%, transparent 38%, var(--button-shine) 50%, transparent 62%, transparent 100%);
  transform: translateX(-120%);
  pointer-events: none;
}

button[type="submit"]:hover::after,
.metric-grid button:hover::after,
.theme-option:hover::after {
  animation: shine .7s ease;
}

@keyframes shine {
  to { transform: translateX(120%); }
}

.workspace {
  min-width: 0;
  padding: 18px;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: end;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.toolbar label {
  max-width: 360px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.grid {
  display: grid;
  gap: 16px;
}

.grid.two {
  grid-template-columns: repeat(2, minmax(280px, 1fr));
}

.split {
  display: grid;
  grid-template-columns: minmax(320px, 520px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.split.wide {
  grid-template-columns: minmax(420px, 640px) minmax(0, 1fr);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

.panel h2 {
  margin: 0 0 14px;
  font-size: 17px;
}

form {
  display: grid;
  gap: 12px;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

input, select, textarea {
  width: 100%;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  color: var(--text);
  background: var(--input-bg);
  font: inherit;
}

input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 70%, #fff 30%);
  outline-offset: 2px;
}

.dashboard {
  display: grid;
  gap: 16px;
}

.dashboard-head {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 16px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(135deg, var(--surface) 0%, var(--surface-soft) 100%);
}

.dashboard-head h2 {
  margin: 0;
  font-size: 22px;
}

.dashboard-head p {
  margin: 4px 0 0;
  color: var(--muted);
}

.quick-actions,
.action-list,
.status-list {
  display: grid;
  gap: 8px;
}

.quick-actions {
  grid-template-columns: repeat(3, minmax(120px, 1fr));
}

.dashboard-grid {
  display: grid;
  grid-template-columns: minmax(260px, 420px) minmax(0, 1fr);
  gap: 16px;
}

textarea {
  min-height: 76px;
  resize: vertical;
}

.row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.row label:nth-child(3) {
  grid-column: auto;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
}

.check input {
  width: auto;
  min-height: auto;
}

.inline-filter, .add-line {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}

.button-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}

.metric-grid button {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: var(--surface-soft);
  color: var(--text);
  text-align: left;
  min-height: 88px;
  box-shadow: inset 0 3px 0 color-mix(in srgb, var(--accent) 55%, transparent);
}

.metric-grid strong {
  display: block;
  font-size: 24px;
  margin-bottom: 4px;
}

.metric-grid span {
  color: var(--muted);
  font-weight: 700;
}

.suggestions {
  display: grid;
  gap: 6px;
  margin-top: -6px;
}

.suggestion {
  text-align: left;
  background: var(--surface-soft);
}

.suggestion strong,
.suggestion small {
  display: block;
}

.suggestion small {
  color: var(--muted);
  margin-top: 3px;
}

.inline-filter input, .add-line select {
  flex: 1;
}

.list, .editable-list {
  display: grid;
  gap: 8px;
}

.item {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  background: var(--surface-soft);
  text-align: left;
  min-width: 0;
}

.item:hover {
  background: color-mix(in srgb, var(--surface-soft) 82%, var(--accent) 18%);
}

.status-item {
  text-align: left;
  display: grid;
  gap: 4px;
  background: var(--surface-soft);
}

.status-item strong,
.status-item small {
  display: block;
}

.status-item small {
  color: var(--muted);
  line-height: 1.35;
}

.item strong {
  display: block;
  margin-bottom: 3px;
}

.item small {
  color: var(--muted);
  overflow-wrap: anywhere;
}

.item.selected {
  border-color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}

.item-row {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) 120px 120px 80px;
  gap: 8px;
  align-items: end;
}

.dispatch-board {
  display: grid;
  gap: 14px;
}

.dispatch-lab {
  display: grid;
  gap: 8px;
}

.dispatch-lab h3 {
  margin: 0;
  font-size: 1rem;
}

.table-scroll {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.dispatch-table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
  background: var(--surface);
}

.dispatch-table th,
.dispatch-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.dispatch-table th {
  background: var(--surface-soft);
  font-size: .82rem;
  color: var(--muted);
}

.dispatch-table td {
  font-size: .9rem;
}

.audit-list {
  display: grid;
  gap: 8px;
}

.audit-row {
  display: grid;
  gap: 3px;
  padding: 10px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--ok);
  border-radius: 6px;
  background: var(--surface-soft);
}

.audit-row.failed {
  border-left-color: var(--danger);
}

.audit-row small,
.audit-row span {
  color: var(--muted);
  overflow-wrap: anywhere;
}

.compact {
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}

pre {
  min-height: 260px;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  color: var(--text);
}

#toast {
  position: fixed;
  z-index: 40;
  right: 18px;
  bottom: 18px;
  max-width: 420px;
  padding: 12px 14px;
  border-radius: 8px;
  background: #1f2a24;
  color: #fff;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity .18s ease, transform .18s ease;
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}

.theme-option {
  display: grid;
  gap: 8px;
  min-height: 132px;
  padding: 14px;
  text-align: left;
  background: var(--surface-soft);
}

.theme-option.active {
  border-color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent), 0 8px 18px var(--shadow);
}

.theme-option strong,
.theme-option small {
  display: block;
}

.theme-option small {
  color: var(--muted);
  line-height: 1.35;
}

.theme-swatch {
  display: block;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.theme-swatch.clinical {
  background: linear-gradient(90deg, #f6f7f4 0 25%, #ffffff 25% 50%, #126d63 50% 75%, #1f2a24 75%);
}

.theme-swatch.blocks {
  background: linear-gradient(90deg, #8f2ee8 0 25%, #ff5ca8 25% 50%, #2f80ed 50% 75%, #20c997 75%);
}

.theme-swatch.studio {
  background: linear-gradient(90deg, #ffffff 0 25%, #f2f4f7 25% 50%, #0f766e 50% 75%, #111827 75%);
}

.theme-swatch.night {
  background: linear-gradient(90deg, #08110f 0 25%, #17211f 25% 50%, #2dd4bf 50% 75%, #eef8f3 75%);
}

.status-box {
  display: grid;
  gap: 4px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-soft);
}

.status-box strong,
.status-box small {
  display: block;
}

.status-box small {
  color: var(--muted);
}

.help-box {
  display: grid;
  gap: 4px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: color-mix(in srgb, var(--surface-soft) 82%, var(--accent) 18%);
}

.help-box strong,
.help-box small {
  display: block;
}

.help-box small {
  color: var(--muted);
  line-height: 1.4;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
  }
}

#toast.show {
  opacity: 1;
  transform: translateY(0);
}

#toast.error {
  background: var(--danger);
}

@media (max-width: 920px) {
  body {
    font-size: 15px;
  }

  .shell {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .mobile-menu-button {
    display: inline-flex;
    justify-content: center;
    align-items: center;
  }

  .tabs {
    position: fixed;
    z-index: 30;
    inset: 72px auto 0 0;
    width: min(320px, 88vw);
    transform: translateX(-105%);
    transition: transform .18s ease;
    border-right: 1px solid var(--line);
    box-shadow: 14px 0 28px var(--shadow);
  }

  body.nav-open .tabs {
    transform: translateX(0);
  }

  .tab {
    text-align: left;
  }

  .grid.two, .split, .split.wide {
    grid-template-columns: 1fr;
  }

  .dashboard-head,
  .dashboard-grid {
    grid-template-columns: 1fr;
  }

  .dashboard-head,
  .toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .quick-actions {
    grid-template-columns: 1fr;
  }

  .panel {
    padding: 14px;
  }
}

@media (max-width: 620px) {
  .topbar {
    min-height: auto;
    gap: 10px;
    padding: 12px;
  }

  .topbar h1 {
    font-size: 18px;
  }

  .topbar p {
    font-size: 12px;
  }

  .topbar, .toolbar, .inline-filter, .add-line, .session, .button-row {
    align-items: stretch;
    flex-direction: column;
  }

  .session {
    justify-content: flex-start;
  }

  .session span,
  .session button,
  .button-row button,
  .inline-filter button,
  .add-line button {
    width: 100%;
  }

  .row, .item-row {
    grid-template-columns: 1fr;
  }

  .workspace {
    padding: 10px;
  }

  .toolbar {
    margin-bottom: 12px;
  }

  .toolbar label {
    max-width: none;
  }

  input, select, textarea, .tab, button {
    min-height: 42px;
  }

  .auth-gate {
    align-items: stretch;
    padding: 12px;
  }

  .auth-card {
    align-self: center;
    padding: 16px;
  }

  .metric-grid {
    grid-template-columns: 1fr 1fr;
  }

  .theme-grid {
    grid-template-columns: 1fr;
  }

  .metric-grid button {
    min-height: 78px;
    padding: 12px;
  }

  .metric-grid strong {
    font-size: 21px;
  }

  #toast {
    right: 10px;
    bottom: 10px;
    left: 10px;
    max-width: none;
  }
}
