:root {
  color-scheme: light;
  --bg: #f6f7f4;
  --panel: #ffffff;
  --ink: #17201a;
  --muted: #667067;
  --line: #dfe5dc;
  --accent: #176a5e;
  --accent-strong: #0f4c43;
  --danger: #a33b2e;
  --shadow: 0 18px 42px rgba(23, 32, 26, 0.1);
  --entry: #1677ff;
  --climate: #f59e0b;
  --power: #16a34a;
  --water: #0891b2;
  --security: #dc2626;
  --motion: #7c3aed;
  --hubs: #475569;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111613;
  --panel: #1a211d;
  --ink: #edf3ee;
  --muted: #a2aea5;
  --line: #324038;
  --accent: #5dc9ad;
  --accent-strong: #8ee0cb;
  --danger: #ff8d7f;
  --shadow: 0 18px 42px rgba(0, 0, 0, 0.32);
  --entry: #2f8cff;
  --climate: #f59e0b;
  --power: #22c55e;
  --water: #06b6d4;
  --security: #ef4444;
  --motion: #8b5cf6;
  --hubs: #94a3b8;
}

* {
  box-sizing: border-box;
}

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

button,
input,
textarea {
  font: inherit;
}

form {
  margin: 0;
}

.app-shell {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 32px 0;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
  padding-bottom: 22px;
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2rem, 4vw, 4rem);
  line-height: 1;
}

.toolbar,
.actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.button,
.icon-button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  min-height: 40px;
  border-radius: 7px;
  padding: 0 14px;
  cursor: pointer;
}

.theme-button {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
}

.theme-button:hover {
  border-color: var(--accent);
}

.theme-icon {
  position: relative;
  display: block;
  width: 16px;
  height: 16px;
}

:root:not([data-theme="dark"]) .theme-icon {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
}

:root:not([data-theme="dark"]) .theme-icon::after {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  left: 5px;
  top: -3px;
  border-radius: 50%;
  background: var(--panel);
}

:root[data-theme="dark"] .theme-icon {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: #f1e8c8;
  box-shadow:
    0 -7px 0 -5px #f1e8c8,
    0 7px 0 -5px #f1e8c8,
    7px 0 0 -5px #f1e8c8,
    -7px 0 0 -5px #f1e8c8,
    5px 5px 0 -5px #f1e8c8,
    -5px -5px 0 -5px #f1e8c8,
    5px -5px 0 -5px #f1e8c8,
    -5px 5px 0 -5px #f1e8c8;
}

:root[data-theme="dark"] .theme-icon::before,
:root[data-theme="dark"] .theme-icon::after {
  content: none;
}

.button:hover,
.icon-button:hover {
  border-color: var(--accent);
}

.button:disabled {
  cursor: wait;
  opacity: 0.65;
}

.button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--bg);
}

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

.status {
  color: var(--muted);
  font-size: 0.92rem;
  max-width: 520px;
}

.status.error {
  color: var(--danger);
}

.summary {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 12px;
  margin: 24px 0;
}

.summary-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  min-height: 112px;
  padding: 16px;
}

.metric-card {
  display: grid;
  align-content: center;
}

.latest-card {
  grid-column: span 2;
  display: grid;
  align-content: center;
  border-left: 4px solid var(--entry);
  background:
    linear-gradient(135deg, color-mix(in srgb, var(--entry), transparent 88%), transparent 58%),
    var(--panel);
}

.temps-card {
  grid-column: span 2;
  display: grid;
  gap: 10px;
  align-content: start;
  border-left: 4px solid var(--climate);
}

.activity-summary-card {
  grid-column: span 4;
  display: grid;
  gap: 10px;
  align-content: start;
  border-left: 4px solid var(--motion);
}

.summary-kicker {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.metric {
  display: block;
  font-size: 2rem;
  font-weight: 800;
}

.small-metric {
  color: var(--ink);
  font-size: 1.18rem;
  line-height: 1.2;
}

.metric-label {
  color: var(--muted);
}

.temp-overview {
  display: grid;
  gap: 6px;
  font-size: 0.84rem;
}

.temp-overview div {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.activity-summary div {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: baseline;
  gap: 8px;
}

.temp-overview span,
.activity-summary span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.temp-overview strong,
.activity-summary strong {
  color: var(--climate);
  white-space: nowrap;
}

.activity-summary {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px 16px;
  font-size: 0.84rem;
}

.activity-summary strong {
  color: var(--motion);
}

.activity-summary small {
  color: var(--muted);
  white-space: nowrap;
}

.mqtt-metric {
  color: var(--climate);
}

.mqtt-metric[data-status="connected"] {
  color: var(--power);
}

.mqtt-metric[data-status="error"],
.mqtt-metric[data-status="subscribe-error"] {
  color: var(--security);
}

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

.category-tabs {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin: 0 0 16px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 12px;
}

.category-tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  padding: 0 10px 0 12px;
  cursor: pointer;
}

.category-tab[data-active="true"] {
  border-color: var(--accent);
  color: var(--ink);
}

.category-tab strong {
  display: inline-grid;
  place-items: center;
  min-width: 24px;
  height: 22px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 750;
}

.category-tab[data-active="true"] strong {
  background: var(--accent);
  color: var(--bg);
}

.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  align-items: start;
  gap: 12px;
}

.device-card {
  --card-accent: var(--accent);
  display: grid;
  gap: 10px;
  padding: 14px;
  border-left: 4px solid var(--card-accent);
  transition:
    border-color 0.15s ease,
    opacity 0.15s ease,
    transform 0.15s ease;
}

.device-card.entry {
  --card-accent: var(--entry);
}

.device-card.climate {
  --card-accent: var(--climate);
}

.device-card.power {
  --card-accent: var(--power);
}

.device-card.water {
  --card-accent: var(--water);
}

.device-card.security {
  --card-accent: var(--security);
}

.device-card.motion {
  --card-accent: var(--motion);
}

.device-card.hubs {
  --card-accent: var(--hubs);
}

.device-card.dragging {
  opacity: 0.5;
}

.device-card.drop-target {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.device-card.hidden-card {
  opacity: 0.72;
  border-style: dashed;
}

.card-header {
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr) auto;
  align-items: start;
  gap: 10px;
}

.card-header h2 {
  color: var(--ink);
  font-size: 1.08rem;
  line-height: 1.25;
  overflow-wrap: anywhere;
}

.drag-handle {
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  color: var(--muted);
  cursor: grab;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-weight: 800;
  line-height: 1;
}

.drag-handle:active {
  cursor: grabbing;
}

.card-tools {
  display: flex;
  align-items: center;
  gap: 6px;
}

.tool-button {
  min-height: 30px;
  padding: 0 8px;
  font-size: 0.8rem;
}

.device-meta {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.device-meta > span {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  padding: 0 10px;
  font-size: 0.84rem;
}

.device-meta .type-chip {
  color: var(--muted);
}

.device-meta .status-chip {
  gap: 6px;
  min-height: 22px;
  padding: 0 8px;
  font-size: 0.74rem;
}

.device-meta .battery-indicator {
  margin-left: auto;
  border: 0;
  min-height: 22px;
  padding: 0;
  color: var(--battery-color);
}

.status-chip i {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: currentColor;
}

.device-meta .status-chip.good,
.device-meta .status-chip.stale,
.device-meta .status-chip.bad {
  border-color: currentColor;
}

.status-chip.good {
  color: var(--power);
}

.status-chip.stale {
  color: var(--climate);
}

.status-chip.bad {
  color: var(--security);
}

.status-chip.neutral {
  color: var(--muted);
}

.device-id {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 0.78rem;
  overflow-wrap: anywhere;
}

.state-panel {
  display: grid;
  gap: 6px;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 10px 0;
}

.primary-state {
  display: grid;
  gap: 2px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 8px;
}

.primary-state strong {
  color: var(--card-accent);
  font-size: 1.36rem;
  line-height: 1.15;
}

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

.primary-state.good strong {
  color: var(--power);
}

.primary-state.bad strong {
  color: var(--security);
}

.state-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  min-height: 22px;
  color: var(--muted);
  font-size: 0.88rem;
}

.state-row strong {
  color: var(--ink);
  font-weight: 750;
  text-align: right;
}

.state-row.good strong {
  color: var(--accent);
}

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

.state-row.warn strong {
  color: var(--climate);
}

.state-row.dead strong {
  color: var(--muted);
}

.state-row.stale strong {
  color: var(--climate);
}

.battery-indicator {
  --battery-color: var(--power);
  display: inline-flex;
  align-items: center;
  justify-content: end;
  gap: 7px;
  color: var(--battery-color);
}

.battery-indicator.warn {
  --battery-color: var(--climate);
}

.battery-indicator.bad {
  --battery-color: var(--danger);
}

.battery-indicator.dead {
  --battery-color: var(--muted);
}

.battery-icon {
  position: relative;
  display: inline-block;
  width: 28px;
  height: 14px;
  border: 2px solid currentColor;
  border-radius: 4px;
  background: color-mix(in srgb, currentColor, transparent 92%);
}

.battery-icon::after {
  content: "";
  position: absolute;
  top: 3px;
  right: -5px;
  width: 3px;
  height: 6px;
  border-radius: 0 2px 2px 0;
  background: currentColor;
}

.battery-fill {
  position: absolute;
  inset: 2px;
  right: auto;
  width: calc(var(--battery-fill) - 4px);
  min-width: 0;
  max-width: calc(100% - 4px);
  border-radius: 2px;
  background: currentColor;
}

.battery-icon[data-level="0"] .battery-fill {
  display: none;
}

.battery-icon[data-level="0"]::before {
  content: "";
  position: absolute;
  left: 6px;
  top: 50%;
  width: 12px;
  height: 2px;
  border-radius: 999px;
  background: currentColor;
  transform: translateY(-50%) rotate(-35deg);
  opacity: 0.9;
}

.state-empty {
  color: var(--muted);
  font-size: 0.88rem;
}

.telemetry {
  display: grid;
  gap: 6px;
  border-top: 1px solid var(--line);
  padding-top: 8px;
}

.telemetry summary {
  color: var(--muted);
  cursor: pointer;
  font-size: 0.86rem;
}

.telemetry[open] summary {
  margin-bottom: 4px;
}

.actions {
  gap: 8px;
}

.actions .button {
  min-height: 34px;
  padding: 0 10px;
  font-size: 0.88rem;
}

.icon-action {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 850;
  line-height: 1;
  padding: 0;
}

.icon-action:hover {
  border-color: var(--card-accent);
  color: var(--ink);
}

.icon-action.power-action {
  font-size: 1.05rem;
}

.icon-action.power-action.active {
  border-color: color-mix(in srgb, var(--power), transparent 28%);
  background: color-mix(in srgb, var(--power), transparent 86%);
  color: var(--power);
}

.icon-action.power-action.inactive {
  color: var(--muted);
}

.icon-action.toggle-action {
  color: var(--motion);
}

.icon-action.open-action {
  color: var(--entry);
}

.icon-action.close-action {
  color: var(--muted);
}

.empty {
  grid-column: 1 / -1;
  padding: 24px;
  color: var(--muted);
}

dialog {
  width: min(560px, calc(100% - 24px));
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 0;
}

dialog::backdrop {
  background: rgba(23, 32, 26, 0.35);
}

.dialog-body {
  display: grid;
  gap: 16px;
  padding: 18px;
}

.dialog-body header,
.dialog-body footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.icon-button {
  width: 40px;
  padding: 0;
}

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

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  color: var(--ink);
  padding: 10px;
}

textarea {
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
}

.check-row {
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
}

.check-row input {
  width: auto;
}

.activity-list {
  display: grid;
  gap: 8px;
  max-height: min(62vh, 560px);
  overflow: auto;
}

.activity-row {
  display: grid;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.activity-row strong {
  color: var(--ink);
}

.activity-row span {
  color: var(--muted);
  font-size: 0.84rem;
}

.activity-row span:first-of-type {
  color: var(--ink);
}

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

.automation-form .button {
  grid-column: 1 / -1;
}

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

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

.login-panel h1 {
  font-size: 1.6rem;
  line-height: 1.1;
}

.login-panel .button {
  justify-content: center;
}

.login-error {
  border: 1px solid color-mix(in srgb, var(--danger), transparent 50%);
  border-radius: 7px;
  color: var(--danger);
  padding: 10px;
}

@media (max-width: 720px) {
  .app-shell {
    width: min(100% - 20px, 1180px);
    padding: 20px 0;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

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

  .latest-card,
  .temps-card,
  .activity-summary-card {
    grid-column: auto;
  }

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