:root {
  color-scheme: light;
  --bg: #f5f7f2;
  --panel: #ffffff;
  --ink: #18221c;
  --muted: #66736b;
  --line: #dce4dd;
  --brand: #217346;
  --brand-dark: #165832;
  --accent: #1e6aa8;
  --warn: #9b5c13;
  --danger: #a72c2c;
  --ok: #1f7a4d;
  --shadow: 0 8px 24px rgba(16, 34, 23, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  min-height: 40px;
  border: 0;
  border-radius: 6px;
  background: var(--brand);
  color: #fff;
  padding: 0 14px;
  cursor: pointer;
}

button:hover {
  background: var(--brand-dark);
}

button.ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
}

button.ghost:hover {
  border-color: var(--brand);
}

input {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
  color: var(--ink);
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.hidden {
  display: none !important;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

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

.login-logo {
  width: 148px;
  height: auto;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--brand);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0;
  font-size: 12px;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
}

h2 {
  font-size: 20px;
}

h3 {
  font-size: 15px;
}

.error {
  min-height: 20px;
  color: var(--danger);
}

.app {
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(245, 247, 242, 0.94);
  backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand img {
  width: 72px;
  height: auto;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.role-badge {
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  border: 1px solid var(--line);
  padding: 0 12px;
  color: var(--muted);
  background: #fff;
}

.layout {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 18px;
  padding: 18px 24px 28px;
}

.unit-panel,
.chart-card,
.camera-card,
.settings-card,
.empty-state,
.metric {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.unit-panel {
  height: calc(100vh - 118px);
  position: sticky;
  top: 90px;
  display: grid;
  grid-template-rows: auto minmax(120px, 1fr) auto;
  overflow: hidden;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.unit-list {
  overflow: auto;
  padding: 8px;
}

.controller-admin {
  display: grid;
  gap: 10px;
  padding: 14px;
  border-top: 1px solid var(--line);
  background: #fbfcfa;
}

.controller-provision-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 8px;
}

.controller-secret {
  display: grid;
  gap: 8px;
  border: 1px solid #c7dccd;
  border-radius: 6px;
  padding: 10px;
  background: #edf5ef;
}

.controller-secret code {
  display: block;
  overflow-wrap: anywhere;
  color: var(--ink);
  font-size: 12px;
}

.controller-list {
  display: grid;
  gap: 8px;
  max-height: 180px;
  overflow: auto;
}

.controller-row {
  display: grid;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px;
  background: #fff;
}

.controller-row strong,
.controller-row span,
.controller-row em {
  display: block;
  overflow-wrap: anywhere;
}

.controller-row strong {
  font-size: 13px;
}

.controller-row span,
.controller-row em {
  color: var(--muted);
  font-size: 12px;
  font-style: normal;
}

.unit-button {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  text-align: left;
  color: var(--ink);
  background: transparent;
  border: 1px solid transparent;
  padding: 12px;
  min-height: 72px;
}

.unit-button:hover,
.unit-button.active {
  background: #edf5ef;
  border-color: #c7dccd;
}

.unit-button strong {
  font-size: 15px;
}

.unit-button span {
  color: var(--muted);
  font-size: 12px;
}

.unit-pill {
  align-self: start;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  background: #e9edf0;
  color: var(--muted);
}

.unit-pill.online {
  background: #daf0e3;
  color: var(--ok);
}

.workspace {
  min-width: 0;
}

.empty-state {
  min-height: 260px;
  display: grid;
  place-items: center;
  color: var(--muted);
}

.unit-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.status-line {
  color: var(--muted);
  font-size: 13px;
}

.unit-meta {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: end;
  color: var(--muted);
  font-size: 13px;
}

.unit-meta span {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 10px;
}

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

.metric {
  min-height: 94px;
  padding: 14px;
  display: grid;
  align-content: space-between;
}

.metric span {
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  font-size: 24px;
}

.chart-grid,
.lower-grid,
.admin-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-top: 14px;
}

.camera-card {
  margin-top: 14px;
  padding: 16px;
}

.camera-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(220px, 0.65fr);
  gap: 14px;
}

.camera-frame {
  position: relative;
  min-height: 230px;
  border-radius: 8px;
  overflow: hidden;
  background: #10261d;
}

.camera-frame img {
  width: 100%;
  height: 100%;
  min-height: 230px;
  object-fit: cover;
  display: block;
}

.camera-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #dce4dd;
}

.camera-note {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 12px;
}

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

.camera-timeline img {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--line);
  cursor: pointer;
}

.chart-card,
.settings-card {
  padding: 16px;
}

.chart-head,
.section-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.chart-head span,
.section-title span {
  color: var(--muted);
  font-size: 12px;
}

canvas {
  width: 100%;
  max-width: 100%;
  height: 220px;
  display: block;
}

.actuator-list,
.alarm-list,
.manual-controls {
  display: grid;
  gap: 10px;
}

.actuator-row,
.alarm-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 10px;
}

.actuator-row:last-child,
.alarm-row:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.state-dot {
  width: 12px;
  height: 12px;
  border-radius: 999px;
  background: #c8ced1;
}

.state-dot.on {
  background: var(--ok);
}

.alarm-row strong {
  color: var(--danger);
}

.admin-panel {
  margin-top: 18px;
}

.settings-drawer {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
  padding: 0;
}

.settings-drawer summary {
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  cursor: pointer;
  list-style: none;
}

.settings-drawer summary::-webkit-details-marker {
  display: none;
}

.settings-drawer summary span {
  font-weight: 800;
}

.settings-drawer summary small {
  color: var(--muted);
}

.settings-drawer[open] {
  padding-bottom: 16px;
}

.settings-drawer[open] summary {
  border-bottom: 1px solid var(--line);
  margin-bottom: 14px;
}

.settings-drawer .settings-card {
  box-shadow: none;
}

.settings-drawer > .settings-card {
  margin: 14px;
}

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

.manual-controls {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.manual-controls button {
  background: #ecf4ef;
  color: var(--brand);
  border: 1px solid #c7dccd;
}

.manual-controls button.on {
  background: var(--brand);
  color: #fff;
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
    padding: 14px;
  }

  .unit-panel {
    position: static;
    height: auto;
    max-height: 320px;
  }

  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .chart-grid,
  .lower-grid,
  .admin-grid,
  .camera-layout {
    grid-template-columns: 1fr;
  }

  .camera-timeline {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 620px) {
  .topbar {
    padding: 12px 14px;
  }

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

  .brand img {
    width: 54px;
  }

  .top-actions {
    gap: 6px;
  }

  .role-badge {
    display: none;
  }

  .unit-header {
    display: grid;
  }

  .unit-meta {
    justify-content: start;
  }

  .metric-grid,
  .camera-timeline,
  .form-grid,
  .manual-controls {
    grid-template-columns: 1fr;
  }
}
