:root {
  --home-max: 1200px;
  --home-radius: 20px;
  --home-card-radius: 16px;
  --nav-height: 64px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  min-height: 100%;
  font-family: "Google Sans", "Roboto", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
}

m3e-theme { display: block; min-height: 100vh; }

/* ===== App Shell ===== */
.app-shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: var(--md-sys-color-surface, #f5f5f5);
}

/* ===== Pages ===== */
.page { display: none; }
.page--active { display: block; }

/* ===== Top Bar ===== */
.top-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--md-sys-color-surface, #f5f5f5);
  padding: 12px 16px 0;
}
.top-bar__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: var(--home-max);
  margin: 0 auto;
  padding-bottom: 12px;
}
.top-bar__brand { display: flex; align-items: center; gap: 10px; }
.top-bar__logo { font-size: 28px; color: var(--md-sys-color-primary, #8AB4F8); }
.top-bar__title {
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--md-sys-color-on-surface, #1a1a1a);
}
.top-bar__actions { display: flex; align-items: center; gap: 8px; }

/* ===== Connection Dot ===== */
.conn-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.conn-dot--online { background: #34a853; box-shadow: 0 0 6px #34a85380; }
.conn-dot--offline { background: #9aa0a6; }
.conn-dot--demo { background: #fbbc04; box-shadow: 0 0 6px #fbbc0480; }

/* ===== Search Bar ===== */
.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: var(--home-max);
  margin: 0 auto;
  padding: 0 16px;
  height: 48px;
  background: var(--md-sys-color-surface-container-high, #e8eaed);
  border-radius: 28px;
  transition: background 0.2s;
}
.search-bar:focus-within { background: var(--md-sys-color-surface-container-highest, #d2d5d9); }
.search-bar__icon { font-size: 22px; color: var(--md-sys-color-on-surface-variant, #5f6368); flex-shrink: 0; }
.search-bar__input {
  flex: 1; border: none; outline: none; background: transparent;
  font: inherit; font-size: 0.95rem; color: var(--md-sys-color-on-surface, #1a1a1a); min-width: 0;
}
.search-bar__input::placeholder { color: var(--md-sys-color-on-surface-variant, #5f6368); opacity: 0.7; }
.search-bar__clear {
  display: flex; align-items: center; justify-content: center;
  background: none; border: none; cursor: pointer; padding: 4px;
  color: var(--md-sys-color-on-surface-variant, #5f6368);
}
.search-bar__clear m3e-icon { font-size: 20px; }

/* ===== Content ===== */
.content {
  flex: 1;
  max-width: var(--home-max);
  margin: 0 auto;
  width: 100%;
  padding: 20px 16px calc(var(--nav-height) + 32px);
}
.page-desc {
  font-size: 0.9rem;
  color: var(--md-sys-color-on-surface-variant, #5f6368);
  margin: -8px 0 20px;
  line-height: 1.5;
}

/* ===== Stats Row ===== */
.stats-row { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.stat-chip {
  display: flex; align-items: center; gap: 6px; padding: 6px 14px;
  border-radius: 20px; font-size: 0.8rem; font-weight: 500;
  background: var(--md-sys-color-surface-container, #e8eaed);
  color: var(--md-sys-color-on-surface-variant, #5f6368);
}
.stat-chip__icon { font-size: 18px; }
.stat-chip--online { color: #34a853; }
.stat-chip--offline { color: #9aa0a6; }

/* ===== Category Chips ===== */
.chip-bar {
  display: flex; gap: 8px; overflow-x: auto; padding-bottom: 16px;
  scrollbar-width: none; -ms-overflow-style: none;
}
.chip-bar::-webkit-scrollbar { display: none; }
.cat-chip {
  display: flex; align-items: center; gap: 6px; padding: 8px 16px;
  border-radius: 20px; border: 1px solid var(--md-sys-color-outline-variant, #dadce0);
  background: transparent; color: var(--md-sys-color-on-surface-variant, #5f6368);
  font: inherit; font-size: 0.82rem; font-weight: 500; cursor: pointer;
  white-space: nowrap; transition: all 0.2s; flex-shrink: 0;
}
.cat-chip m3e-icon { font-size: 18px; }
.cat-chip:hover { background: var(--md-sys-color-surface-container-highest, #d2d5d9); }
.cat-chip--active {
  background: var(--md-sys-color-primary, #8AB4F8);
  color: var(--md-sys-color-on-primary, #003063);
  border-color: transparent;
}
.cat-chip--active m3e-icon { color: var(--md-sys-color-on-primary, #003063); }

/* ===== Device Grid ===== */
.device-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px;
}

/* ===== Device Card ===== */
.dev-card {
  background: var(--md-sys-color-surface-container-low, #f1f3f4);
  border-radius: var(--home-card-radius); padding: 20px;
  display: flex; flex-direction: column; gap: 12px;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
  cursor: default; position: relative; overflow: hidden;
}
.dev-card:hover { transform: translateY(-2px); box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
.dev-card--flash { background: var(--md-sys-color-primary-container, #d3e3fd); }

.dev-card__top {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
}
.dev-card__icon-wrap {
  width: 48px; height: 48px; border-radius: 14px;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.dev-card__icon-wrap--online {
  background: var(--md-sys-color-primary-container, #d3e3fd);
  color: var(--md-sys-color-on-primary-container, #001d36);
}
.dev-card__icon-wrap--offline {
  background: var(--md-sys-color-surface-container-highest, #d2d5d9);
  color: var(--md-sys-color-on-surface-variant, #5f6368);
}
.dev-card__icon { font-size: 26px; }

.dev-card__status {
  font-size: 0.7rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.06em; padding: 3px 10px; border-radius: 12px;
}
.dev-card__status--online { background: #e6f4ea; color: #137333; }
.dev-card__status--offline {
  background: var(--md-sys-color-surface-container-highest, #d2d5d9);
  color: var(--md-sys-color-on-surface-variant, #5f6368);
}

.dev-card__info { flex: 1; }
.dev-card__name {
  font-size: 1.05rem; font-weight: 600; color: var(--md-sys-color-on-surface, #1a1a1a);
  margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.dev-card__meta {
  display: flex; flex-wrap: wrap; gap: 6px 14px;
  font-size: 0.78rem; color: var(--md-sys-color-on-surface-variant, #5f6368);
}
.dev-card__meta-item { display: flex; align-items: center; gap: 4px; }
.dev-card__meta-item m3e-icon { font-size: 14px; opacity: 0.7; }
.dev-card__meta-val { font-family: "Roboto Mono", ui-monospace, monospace; }

.dev-card__cat {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 0.72rem; font-weight: 500; padding: 2px 10px; border-radius: 10px;
  background: var(--md-sys-color-surface-container, #e8eaed);
  color: var(--md-sys-color-on-surface-variant, #5f6368);
  width: fit-content;
}
.dev-card__cat m3e-icon { font-size: 14px; }

/* ===== Device Controls (on card) ===== */
.dev-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0 0;
  border-top: 1px solid var(--md-sys-color-outline-variant, #dadce0);
}
.dev-controls__toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--md-sys-color-on-surface-variant, #5f6368);
}
.toggle-switch {
  position: relative;
  width: 44px;
  height: 24px;
  background: var(--md-sys-color-surface-container-highest, #d2d5d9);
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.2s;
  border: none;
  padding: 0;
}
.toggle-switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: white;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.toggle-switch--on {
  background: var(--md-sys-color-primary, #8AB4F8);
}
.toggle-switch--on::after {
  transform: translateX(20px);
}

.dev-controls__slider {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
}
.slider-track {
  flex: 1;
  height: 4px;
  background: var(--md-sys-color-surface-container-highest, #d2d5d9);
  border-radius: 2px;
  position: relative;
  cursor: pointer;
}
.slider-fill {
  height: 100%;
  background: var(--md-sys-color-primary, #8AB4F8);
  border-radius: 2px;
  transition: width 0.1s;
}
.slider-thumb {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--md-sys-color-primary, #8AB4F8);
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
  cursor: grab;
}
.slider-label {
  font-size: 0.72rem;
  font-family: "Roboto Mono", monospace;
  color: var(--md-sys-color-on-surface-variant, #5f6368);
  min-width: 28px;
  text-align: right;
}

.dev-card__actions {
  display: flex; align-items: center; gap: 8px; margin-top: 2px;
}
.dev-card__actions m3e-button { flex: 1; }
.dev-card__actions m3e-icon-button { flex-shrink: 0; }

/* ===== Empty State ===== */
.empty-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 80px 24px; text-align: center; gap: 12px;
}
.empty-state__icon { font-size: 64px; color: var(--md-sys-color-primary, #8AB4F8); opacity: 0.5; }
.empty-state__title { font-size: 1.2rem; font-weight: 600; color: var(--md-sys-color-on-surface, #1a1a1a); }
.empty-state__desc {
  font-size: 0.9rem; color: var(--md-sys-color-on-surface-variant, #5f6368);
  max-width: 320px; line-height: 1.5;
}

/* ===== Rooms Page ===== */
.rooms-container { display: flex; flex-direction: column; gap: 20px; }
.room-group__title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--md-sys-color-on-surface-variant, #5f6368);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.room-group__title m3e-icon { font-size: 20px; }
.room-devices {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 10px;
}
.room-dev {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--md-sys-color-surface-container-low, #f1f3f4);
  border-radius: 14px;
  transition: background 0.15s;
}
.room-dev:hover { background: var(--md-sys-color-surface-container, #e8eaed); }
.room-dev__icon {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.room-dev__icon--online { background: var(--md-sys-color-primary-container, #d3e3fd); color: #001d36; }
.room-dev__icon--offline { background: var(--md-sys-color-surface-container-highest, #d2d5d9); color: #5f6368; }
.room-dev__icon m3e-icon { font-size: 22px; }
.room-dev__info { flex: 1; min-width: 0; }
.room-dev__name { font-weight: 600; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-dev__status { font-size: 0.72rem; color: var(--md-sys-color-on-surface-variant, #5f6368); }
.room-dev__pulse-btn { flex-shrink: 0; }

/* ===== Pulse Page ===== */
.pulse-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.pulse-card {
  background: var(--md-sys-color-surface-container-low, #f1f3f4);
  border-radius: var(--home-card-radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s, background 0.15s;
  border: 2px solid transparent;
}
.pulse-card:hover { background: var(--md-sys-color-surface-container, #e8eaed); transform: scale(1.02); }
.pulse-card--active { border-color: var(--md-sys-color-primary, #8AB4F8); background: var(--md-sys-color-primary-container, #d3e3fd); }
.pulse-card__icon {
  width: 52px;
  height: 52px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--md-sys-color-primary, #8AB4F8);
  color: var(--md-sys-color-on-primary, #003063);
}
.pulse-card__icon m3e-icon { font-size: 28px; }
.pulse-card__name { font-weight: 600; font-size: 0.95rem; }
.pulse-card__side { font-size: 0.78rem; color: var(--md-sys-color-on-surface-variant, #5f6368); }

/* ===== Settings Page ===== */
.settings-section {
  background: var(--md-sys-color-surface-container-low, #f1f3f4);
  border-radius: var(--home-card-radius);
  padding: 4px 0;
  margin-bottom: 16px;
}
.settings-section__title {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--md-sys-color-primary, #8AB4F8);
  padding: 12px 16px 4px;
}
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  gap: 12px;
}
.setting-row + .setting-row { border-top: 1px solid var(--md-sys-color-outline-variant, #dadce0); }
.setting-info { flex: 1; min-width: 0; }
.setting-label { font-size: 0.9rem; font-weight: 500; color: var(--md-sys-color-on-surface, #1a1a1a); }
.setting-value {
  font-size: 0.78rem;
  color: var(--md-sys-color-on-surface-variant, #5f6368);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: "Roboto Mono", ui-monospace, monospace;
  margin-top: 2px;
}

/* ===== FAB ===== */
m3e-fab#add-fab {
  position: fixed;
  bottom: calc(var(--nav-height) + 20px);
  right: 24px;
  z-index: 90;
}

/* ===== Bottom Nav ===== */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 100;
  height: var(--nav-height);
  background: var(--md-sys-color-surface-container, #e8eaed);
  display: flex; align-items: center; justify-content: space-around;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.nav-item {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 16px; border: none; background: none; cursor: pointer;
  color: var(--md-sys-color-on-surface-variant, #5f6368);
  font: inherit; font-size: 0.7rem; font-weight: 500; border-radius: 16px;
  transition: all 0.2s; position: relative;
}
.nav-item m3e-icon { font-size: 24px; }
.nav-item--active { color: var(--md-sys-color-primary, #8AB4F8); }
.nav-item--active::before {
  content: ""; position: absolute; top: 4px; left: 50%; transform: translateX(-50%);
  width: 32px; height: 28px; border-radius: 14px;
  background: var(--md-sys-color-primary-container, #d3e3fd); z-index: -1;
}
.nav-item:hover:not(.nav-item--active) {
  background: var(--md-sys-color-surface-container-highest, #d2d5d9); border-radius: 16px;
}

/* ===== Dialog ===== */
.dialog-form {
  display: flex; flex-direction: column; gap: 14px;
  min-width: min(80vw, 440px); padding-top: 4px;
}
.form-row { display: flex; gap: 14px; }
.form-row m3e-form-field { flex: 1; }
.dialog-form select,
.dialog-form input {
  width: 100%; border: none; outline: none; background: transparent;
  color: inherit; font: inherit; padding: 6px 0;
}

/* ===== Hide ===== */
[hidden] { display: none !important; }

/* ===== Gate Controls ===== */
.dev-controls__gate {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 8px 0 0;
  border-top: 1px solid var(--md-sys-color-outline-variant, #dadce0);
}
.gate-btn {
  width: 100%;
  padding: 14px;
  border-radius: 14px;
  border: none;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
  overflow: hidden;
}
.gate-btn--ready {
  background: var(--md-sys-color-primary, #8AB4F8);
  color: var(--md-sys-color-on-primary, #003063);
}
.gate-btn--ready:hover:not(:disabled) {
  filter: brightness(1.1);
  transform: scale(1.01);
}
.gate-btn--ready:active:not(:disabled) {
  transform: scale(0.98);
}
.gate-btn--open {
  background: var(--md-sys-color-error, #ea4335);
  color: white;
  cursor: not-allowed;
}
.gate-btn__fill {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  background: rgba(0,0,0,0.15);
  transition: width 1s linear;
  pointer-events: none;
}
.gate-btn__label {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.gate-btn:disabled {
  opacity: 0.85;
}

/* ===== Responsive ===== */
@media (max-width: 540px) {
  .form-row { flex-direction: column; }
  .device-grid { grid-template-columns: 1fr; }
  .pulse-grid { grid-template-columns: repeat(2, 1fr); }
  .room-devices { grid-template-columns: 1fr; }
  .stats-row { gap: 8px; }
  m3e-fab#add-fab { right: 16px; bottom: calc(var(--nav-height) + 16px); }
}
@media (min-width: 541px) and (max-width: 900px) {
  .device-grid { grid-template-columns: repeat(2, 1fr); }
}
