/* ── Reset y base ───────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #f0f2f5;
  --surface:      #ffffff;
  --surface2:     #f8f9fa;
  --border:       #dee2e6;
  --text:         #1a1a2e;
  --text-muted:   #6c757d;
  --primary:      #2563eb;
  --primary-h:    #1d4ed8;
  --primary-light:#dbeafe;
  --danger:       #dc2626;
  --danger-light: #fee2e2;
  --success:      #16a34a;
  --success-light:#dcfce7;
  --warning:      #d97706;
  --radius:       12px;
  --radius-sm:    8px;
  --shadow:       0 1px 4px rgba(0,0,0,.08);
  --shadow-md:    0 4px 16px rgba(0,0,0,.14);
  --font:         system-ui, -apple-system, 'Segoe UI', sans-serif;
  --nav-h:        60px;
}

html { scroll-behavior: smooth; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  min-height: 100dvh;
}

/* ── Tipografía ─────────────────────────────────────────── */
h2 { font-size: 1.2rem; font-weight: 700; }
h3 { font-size: 1rem;   font-weight: 600; margin-bottom: .5rem; }

label {
  display: flex;
  flex-direction: column;
  gap: .3rem;
  font-size: .875rem;
  font-weight: 500;
  margin-bottom: .6rem;
  color: var(--text);
}

.text-muted { font-size: .8rem; color: var(--text-muted); }
.hint       { font-size: .8rem; color: var(--text-muted); margin-top: .25rem; }
.empty      { text-align: center; color: var(--text-muted); padding: 2rem 1rem; }
.hidden     { display: none !important; }
.error      { color: var(--danger); }
.page-center { padding: 2rem 1rem; }

/* ── Inputs ─────────────────────────────────────────────── */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="url"],
input[type="number"],
select, textarea {
  width: 100%;
  padding: .65rem .875rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: .9375rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
  transition: border-color .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
}
input[readonly] { background: var(--surface2); cursor: default; }

/* ── Botones ─────────────────────────────────────────────── */
button {
  padding: .65rem 1.25rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: .9375rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--primary);
  color: #fff;
  transition: background .15s, opacity .15s;
  user-select: none;
  white-space: nowrap;
}
button:active    { opacity: .85; }
button:disabled  { opacity: .5; cursor: not-allowed; }
button:hover:not(:disabled) { background: var(--primary-h); }

button.danger       { background: var(--danger); }
button.danger:hover { background: #b91c1c; }

button.ghost  {
  background: transparent;
  color: var(--primary);
  border: 1.5px solid var(--primary);
}
button.ghost:hover { background: var(--primary-light); }

.btn-full  { width: 100%; text-align: center; }
.btn-sm    { padding: .35rem .75rem; font-size: .8125rem; border-radius: 6px; }
.btn-icon  { padding: .4rem .65rem; font-size: 1rem; min-width: 36px; border-radius: 8px; }
.btn-back  { background: transparent; color: var(--text); border: none; font-size: 1.1rem; padding: .3rem .4rem; }

.actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .75rem; }

/* ── Cards ──────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
  margin-bottom: .75rem;
}
.form-card { padding: 1.25rem; }

/* ── Page header ────────────────────────────────────────── */
.page-header {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: 1rem 1rem .5rem;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}
.page-header h2  { flex: 1; }

/* ── Lista genérica ─────────────────────────────────────── */
.list { padding: 0 1rem 1rem; }

.list-item {
  display: flex;
  align-items: center;
  gap: .75rem;
  cursor: pointer;
  padding: .875rem 1rem;
}
.list-item.danger-item { color: var(--danger); }

.list-item-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: .15rem;
  min-width: 0;
}
.list-item-text strong { font-size: .9375rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.row-actions { display: flex; align-items: center; gap: .4rem; flex-shrink: 0; }

.row-inline  { display: flex; gap: .5rem; }
.row-inline label { flex: 1; }

.chevron {
  width: 18px; height: 18px;
  stroke: var(--text-muted); fill: none;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

/* ── Badge ──────────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: .15rem .55rem;
  border-radius: 999px;
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .03em;
}
.badge-owner  { background: #fef3c7; color: #92400e; }
.badge-admin  { background: var(--primary-light); color: #1e40af; }
.badge-member { background: var(--surface2); color: var(--text-muted); }

/* ── Status dot ─────────────────────────────────────────── */
.status-dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-dot.online  { background: var(--success); }
.status-dot.offline { background: var(--text-muted); }

/* ── Bottom nav ─────────────────────────────────────────── */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
  max-width: 480px;
  margin: 0 auto;
  box-shadow: 0 -2px 12px rgba(0,0,0,.06);
}
.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .2rem;
  text-decoration: none;
  color: var(--text-muted);
  font-size: .7rem;
  font-weight: 500;
  padding: .4rem .75rem;
  border-radius: var(--radius-sm);
  transition: color .15s;
  min-width: 60px;
}
.nav-item span { line-height: 1; }
.nav-item.active { color: var(--primary); }

/* ── Modal ──────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 200;
  padding: 0;
  animation: fadeIn .15s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal-sheet {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 90dvh;
  display: flex;
  flex-direction: column;
  animation: slideUp .2s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }

.modal-header {
  display: flex;
  align-items: center;
  padding: 1rem 1rem .75rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-header h3 { flex: 1; font-size: 1rem; font-weight: 700; }

.modal-body {
  padding: 1rem;
  overflow-y: auto;
  flex: 1;
}
.modal-footer {
  padding: .75rem 1rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* ── Toast ──────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + 1rem + env(safe-area-inset-bottom, 0px));
  left: 50%; transform: translateX(-50%) translateY(1rem);
  background: var(--text);
  color: #fff;
  padding: .65rem 1.2rem;
  border-radius: 999px;
  font-size: .875rem;
  font-weight: 500;
  z-index: 300;
  opacity: 0;
  transition: opacity .2s, transform .2s;
  white-space: nowrap;
  pointer-events: none;
  max-width: 90vw;
  text-align: center;
}
.toast.visible { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.toast-error   { background: var(--danger); }
.toast.toast-success { background: var(--success); }

/* ── Logo ───────────────────────────────────────────────── */
.logo {
  font-size: 2rem;
  font-weight: 800;
  color: var(--primary);
  text-align: center;
  letter-spacing: -.03em;
}

/* ── Login / Auth ───────────────────────────────────────── */
.page-auth {
  padding: 2rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  min-height: 100dvh;
  justify-content: center;
}
.login-logo-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .75rem;
  width: 100%;
  max-width: 380px;
  margin-bottom: .25rem;
}
.login-logo-row .logo { margin-bottom: 0; }

.page-auth h1 {
  font-size: 1.75rem;
  font-weight: 800;
  color: var(--primary);
  text-align: center;
  margin-bottom: .5rem;
}
.page-auth .card { width: 100%; max-width: 380px; }
.page-auth a { color: var(--primary); font-size: .875rem; text-decoration: none; }

.auth-remember {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: .5rem;
  font-size: .875rem;
  font-weight: 400;
  margin-bottom: .25rem;
}
.auth-remember input { width: auto; }

/* ── Homes list ─────────────────────────────────────────── */
.page-homes { padding: 1rem; }
.page-homes .page-header { padding: 1rem 0 .75rem; }

.home-card {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: 1rem;
  cursor: pointer;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  margin-bottom: .75rem;
  transition: box-shadow .15s;
}
.home-card:active { box-shadow: var(--shadow-md); }

.home-icon {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--primary-light);
  color: var(--primary);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.3rem;
  flex-shrink: 0;
}
.home-info { flex: 1; min-width: 0; }
.home-info strong { display: block; font-size: .9375rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.home-role { font-size: .775rem; color: var(--text-muted); }

/* ── Device grid ────────────────────────────────────────── */
.device-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: .75rem;
  padding: 0 1rem 1rem;
}

.device-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .875rem;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: .5rem;
  transition: box-shadow .15s;
}

.device-header {
  display: flex;
  align-items: flex-start;
  gap: .4rem;
}
.device-icon { font-size: 1.25rem; flex-shrink: 0; }
.device-info { flex: 1; min-width: 0; }
.device-info strong { font-size: .8125rem; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.device-model { font-size: .7rem; color: var(--text-muted); }

.device-controls { display: flex; flex-direction: column; gap: .35rem; }

.device-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .3rem;
  padding: .45rem .65rem;
  background: var(--surface2);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .78rem;
  font-weight: 500;
  width: 100%;
  text-align: left;
  transition: background .15s, color .15s;
}
.device-toggle.on {
  background: var(--primary-light);
  color: var(--primary);
  border-color: #bfdbfe;
}

.toggle-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--border);
  flex-shrink: 0;
}
.device-toggle.on .toggle-dot { background: var(--primary); }

.device-sensor {
  font-size: .875rem;
  font-weight: 700;
  color: var(--text-muted);
  text-align: right;
}

/* ── Switch / slider ─────────────────────────────────────── */
.switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 40px; height: 24px;
  flex-direction: row;
  margin-bottom: 0;
}
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background .2s;
}
.slider::before {
  content: '';
  position: absolute;
  width: 18px; height: 18px;
  left: 3px; top: 3px;
  background: white;
  border-radius: 50%;
  transition: transform .2s;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.switch input:checked + .slider { background: var(--primary); }
.switch input:checked + .slider::before { transform: translateX(16px); }

/* ── Scene list ─────────────────────────────────────────── */
.action-row {
  margin-bottom: .5rem;
  padding: .875rem;
  background: var(--surface2);
  border: 1px solid var(--border);
}
.action-row-header {
  display: flex;
  align-items: center;
  margin-bottom: .5rem;
}
.action-row-header strong { flex: 1; font-size: .875rem; }

/* ── Log de actividad ────────────────────────────────────── */
.log-entry {
  padding: .75rem 1rem;
  margin-bottom: .5rem;
}
.log-type {
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--primary);
  margin-bottom: .2rem;
}
.log-desc { font-size: .875rem; margin-bottom: .25rem; }
.log-meta { font-size: .75rem; color: var(--text-muted); }

/* ── Paginación ─────────────────────────────────────────── */
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: .75rem 1rem;
  font-size: .875rem;
  color: var(--text-muted);
}

/* ── Profile ─────────────────────────────────────────────── */
.page-profile { padding: 1rem; }

.profile-card {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1.25rem;
}
.avatar {
  width: 52px; height: 52px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 1.4rem;
  font-weight: 800;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.profile-card strong { display: block; font-size: 1rem; }
.profile-card .text-muted { font-size: .825rem; }

/* ── Session list ───────────────────────────────────────── */
.list-item .btn-sm.danger { margin-left: auto; flex-shrink: 0; }

/* ── Coordinator status ─────────────────────────────────── */
.list-item-text .status-dot { margin-top: .2rem; }

/* ── Responsive helpers ─────────────────────────────────── */
@media (max-width: 360px) {
  .device-grid { grid-template-columns: 1fr; }
}
