* {
  box-sizing: border-box;
}

:root {
  --bg: #0b0f16;
  --panel: #121a27;
  --panel-alt: #182230;
  --text: #f8fafc;
  --muted: #94a3b8;
  --line: rgba(255, 255, 255, 0.12);
  --primary: #01b2b5;
  --primary-hover: #00989e;
  --green: #22c55e;
  --amber: #facc15;
  --red: #ef4444;
  --unknown: #38bdf8;
  --shadow: 0 10px 30px rgb(2 6 23 / 0.25);
  color-scheme: dark;
}

html,
body,
#app {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

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

button,
a.button {
  border: 1px solid var(--line);
  background: var(--panel-alt);
  color: var(--text);
  border-radius: 4px;
  padding: 8px 12px;
  cursor: pointer;
  text-decoration: none;
  transition: transform 0.14s ease, border-color 0.14s ease, background 0.14s ease;
}

button:hover,
a.button:hover {
  transform: translateY(-1px);
  border-color: rgba(0, 179, 186, 0.55);
}

button.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #031417;
  font-weight: 700;
}

button.primary:hover {
  background: var(--primary-hover);
}

button.danger {
  border-color: rgba(239, 68, 68, 0.45);
  color: #fecaca;
}

button.compact {
  padding: 5px 8px;
  font-size: 0.82rem;
}

.inline-actions {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

input,
select,
textarea {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #081019;
  color: var(--text);
  padding: 8px 10px;
}

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

.loading-screen,
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 24px;
  box-shadow: var(--shadow);
}

.login-card h1,
.login-card p {
  margin: 0 0 16px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(2, 6, 23, 0.72);
}

.modal-panel {
  width: min(1040px, 100%);
  max-height: min(86vh, 900px);
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.modal-header h2 {
  margin: 0;
}

.shell {
  min-height: 100vh;
  padding: 16px;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 16px;
  align-items: center;
  margin-bottom: 16px;
}

.title {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--muted);
}

.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(130px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.summary-card,
.panel,
.room-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.summary-card {
  padding: 14px;
  text-align: center;
  transition: transform 0.16s ease, border-color 0.16s ease;
}

button.summary-card {
  text-align: center;
  cursor: pointer;
}

.summary-card:hover {
  transform: translateY(-2px);
  border-color: rgba(0, 179, 186, 0.35);
}

.summary-card span {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
}

.summary-card strong {
  display: block;
  font-size: 1.6rem;
  margin-top: 6px;
}

.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.tabs button[aria-selected="true"] {
  background: var(--primary);
  border-color: var(--primary);
  color: #031417;
}

#main.dashboard-main {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.dashboard-fixed {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg);
  padding-bottom: 8px;
}

.room-list {
  display: grid;
  align-content: start;
  gap: 16px;
  min-height: 0;
  overflow-y: auto;
  padding-right: 4px;
}

.filterbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto;
  gap: 8px;
  margin-bottom: 16px;
}

.site-group {
  display: grid;
  gap: 10px;
}

.site-heading {
  display: grid;
  grid-template-columns: 18px auto 1fr auto auto;
  align-items: center;
  gap: 12px;
  padding: 8px 2px;
  cursor: pointer;
  list-style: none;
}

.site-heading::-webkit-details-marker {
  display: none;
}

.site-heading h2 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
}

.site-heading span {
  color: var(--muted);
  font-size: 0.86rem;
}

.room-card {
  overflow: hidden;
  transition: border-color 0.18s ease, transform 0.18s ease;
}

.room-card:hover {
  border-color: rgba(0, 179, 186, 0.35);
}

.room-header {
  width: 100%;
  display: grid;
  grid-template-columns: 18px auto 1fr auto auto;
  gap: 12px;
  align-items: center;
  padding: 14px;
  background: transparent;
  border: 0;
  text-align: left;
  list-style: none;
}

.room-header::-webkit-details-marker {
  display: none;
}

.twirl {
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
  transition: transform 0.16s ease;
}

.room-card[open] .twirl {
  transform: rotate(90deg);
}

.site-group[open] > .site-heading .twirl {
  transform: rotate(90deg);
}

.room-name {
  font-weight: 700;
}

.room-meta,
.device-meta,
.small {
  color: var(--muted);
  font-size: 0.86rem;
}

.status-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  box-shadow: 0 0 16px currentColor;
}

.site-status-dot {
  width: 12px;
  height: 12px;
}

.status-green { color: var(--green); background: var(--green); }
.status-amber { color: var(--amber); background: var(--amber); }
.status-red { color: var(--red); background: var(--red); }
.status-unknown { color: var(--unknown); background: var(--unknown); }

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.78rem;
}

.device-list {
  border-top: 1px solid var(--line);
  padding: 8px 14px 14px;
  animation: reveal 0.22s cubic-bezier(.2,.8,.2,1);
}

.device-row {
  display: grid;
  grid-template-columns: auto minmax(160px, 1fr) minmax(160px, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 9px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  transition: background 0.14s ease, padding-left 0.14s ease;
}

.dashboard-device-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 6px;
}

.dashboard-device-actions button {
  padding: 5px 8px;
  font-size: 0.82rem;
}

.device-row:hover {
  background: rgba(255, 255, 255, 0.035);
  padding-left: 6px;
}

.device-row.focus-device {
  animation: focus-device 1.3s ease;
}

.device-row:last-child {
  border-bottom: 0;
}

.panel {
  padding: 16px;
  margin-bottom: 16px;
}

.admin-shell {
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr);
  gap: 16px;
}

.admin-nav {
  display: grid;
  align-content: start;
  gap: 8px;
}

.admin-nav button {
  text-align: left;
}

.admin-nav button[aria-selected="true"] {
  background: var(--primary);
  border-color: var(--primary);
  color: #031417;
  font-weight: 700;
}

.admin-content {
  min-width: 0;
}

.admin-import {
  margin: 14px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.03);
}

.admin-import summary {
  cursor: pointer;
  font-weight: 700;
}

.admin-device-site,
.admin-device-room {
  margin-top: 12px;
}

.admin-device-site > summary,
.admin-device-room > summary {
  display: grid;
  grid-template-columns: 18px auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  list-style: none;
  padding: 8px 0;
}

.admin-device-site > summary::-webkit-details-marker,
.admin-device-room > summary::-webkit-details-marker {
  display: none;
}

.admin-device-room {
  margin-left: 18px;
}

.admin-device-site[open] > summary .twirl,
.admin-device-room[open] > summary .twirl {
  transform: rotate(90deg);
}

.order-cell {
  white-space: nowrap;
}

.admin-status-dot {
  width: 10px;
  height: 10px;
  margin: 0 6px 0 2px;
  vertical-align: middle;
}

.order-actions {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}

.order-actions button {
  padding: 5px 8px;
}

.drag-handle {
  color: var(--muted);
  cursor: grab;
  margin-right: 6px;
}

tr.ignored-row {
  opacity: 0.62;
}

tr.ignored-row td {
  background: rgba(255, 255, 255, 0.02);
}

tr.dragging {
  opacity: 0.48;
  outline: 1px dashed var(--primary);
}

.progress-panel {
  display: grid;
  gap: 8px;
}

.progress-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

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

.progress-actions button {
  padding: 5px 9px;
}

progress {
  width: 100%;
  height: 12px;
  accent-color: var(--primary);
}

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

.grid.three {
  grid-template-columns: repeat(3, minmax(140px, 1fr));
}

.field.wide {
  grid-column: 1 / -1;
}

.field label {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
  margin-bottom: 5px;
}

.table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
}

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

.table th {
  color: var(--muted);
  font-weight: 500;
}

.error {
  color: #fecaca;
  min-height: 20px;
}

.hidden {
  display: none !important;
}

.report-page {
  background: #ffffff;
  color: #17202a;
}

.report-page .small,
.report-page .table th {
  color: #55616e;
}

.report-page .summary-card {
  background: #f6f8fa;
  color: #17202a;
  border-color: #d9e0e7;
  box-shadow: none;
}

.report-summary-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(90px, 1fr));
  gap: 8px;
  margin: 16px 0;
}

.report-summary-row .summary-card {
  padding: 10px;
}

.report-summary-row .summary-card strong {
  font-size: 1.25rem;
}

.report-site {
  margin-top: 18px;
}

.report-table caption {
  caption-side: top;
  text-align: left;
  font-weight: 700;
  padding: 8px 0;
}

@keyframes reveal {
  from {
    opacity: 0;
    transform: translateY(-4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes focus-device {
  0% {
    background: rgba(239, 68, 68, 0.32);
    box-shadow: inset 3px 0 0 var(--red);
  }
  100% {
    background: transparent;
    box-shadow: inset 0 0 0 transparent;
  }
}

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

@media print {
  body {
    background: #ffffff;
    color: #17202a;
  }

  .topbar,
  .tabs,
  .report-actions {
    display: none !important;
  }

  .shell {
    display: block;
    padding: 0;
  }

  #main.dashboard-main {
    display: block;
  }

  .dashboard-fixed {
    position: static;
  }

  .room-list {
    overflow: visible;
  }

  .panel,
  .summary-card {
    box-shadow: none;
  }

  .report-summary-row {
    display: grid !important;
    grid-template-columns: repeat(5, minmax(0, 1fr)) !important;
    gap: 8px;
    page-break-inside: avoid;
  }

  .report-page {
    border: 0;
  }
}

@media (max-width: 860px) {
  .topbar,
  .device-row,
  .grid,
  .grid.three,
  .filterbar,
  .admin-shell {
    grid-template-columns: 1fr;
  }

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

  .report-summary-row {
    grid-template-columns: repeat(5, minmax(70px, 1fr));
  }

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