:root {
  --bg-0: #0b1020;
  --bg-1: #121a30;
  --card: rgba(24, 33, 58, 0.72);
  --card-solid: #18213a;
  --card-border: rgba(255, 255, 255, 0.08);
  --text: #eef3ff;
  --muted: #93a3c4;
  --faint: #64749a;
  --accent: #5b8cff;
  --accent-2: #7c5cff;
  --open: #ffb038;
  --open-soft: rgba(255, 176, 56, 0.14);
  --closed: #33d18f;
  --closed-soft: rgba(51, 209, 143, 0.14);
  --danger: #ff6b6b;
  --radius: 20px;
  --shadow: 0 20px 50px -24px rgba(0, 0, 0, 0.75);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg-0: #eef2fb;
    --bg-1: #dfe6f6;
    --card: rgba(255, 255, 255, 0.86);
    --card-solid: #ffffff;
    --card-border: rgba(19, 32, 62, 0.1);
    --text: #14213f;
    --muted: #566588;
    --faint: #8493b3;
    --shadow: 0 18px 44px -26px rgba(20, 33, 62, 0.5);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 700px at 15% -10%, var(--bg-1), transparent 60%),
    radial-gradient(1000px 600px at 100% 0%, rgba(124, 92, 255, 0.16), transparent 55%),
    var(--bg-0);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

.glow {
  position: fixed;
  inset: -30% -10% auto -10%;
  height: 60vh;
  background: radial-gradient(50% 60% at 50% 0%, rgba(91, 140, 255, 0.18), transparent 70%);
  pointer-events: none;
  z-index: 0;
}

/* ---------- Top bar ---------- */
.topbar {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  padding: calc(22px + env(safe-area-inset-top)) 22px 10px;
}

.brand { display: flex; align-items: center; gap: 13px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 13px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 8px 20px -8px var(--accent);
}
.brand-text h1 { font-size: 1.2rem; margin: 0; letter-spacing: 0.2px; font-weight: 700; }
.brand-sub { margin: 1px 0 0; font-size: 0.76rem; color: var(--faint); letter-spacing: 0.3px; }

.user { display: flex; align-items: center; gap: 10px; font-size: 0.82rem; }
.user-name { color: var(--muted); max-width: 42vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logout {
  color: var(--text);
  text-decoration: none;
  padding: 7px 13px;
  border-radius: 999px;
  border: 1px solid var(--card-border);
  background: var(--card);
  transition: border-color 0.15s ease, transform 0.05s ease;
}
.logout:hover { border-color: var(--accent); }
.logout:active { transform: translateY(1px); }

/* ---------- Doors grid ---------- */
.doors {
  position: relative;
  z-index: 1;
  flex: 1;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  padding: 14px 22px 8px;
  display: grid;
  gap: 20px;
  grid-template-columns: 1fr;
}
@media (min-width: 620px) {
  .doors { grid-template-columns: 1fr 1fr; }
}

.card {
  position: relative;
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  overflow: hidden;
}
.card::after {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 3px;
  background: linear-gradient(90deg, transparent, var(--card-border), transparent);
}

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card-title { font-size: 1.06rem; font-weight: 650; }

/* ---------- Status badge ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px 6px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 650;
  letter-spacing: 0.2px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--muted);
  border: 1px solid var(--card-border);
  transition: color 0.25s ease, background 0.25s ease, border-color 0.25s ease;
}
.badge .dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--faint);
  box-shadow: 0 0 0 0 currentColor;
}
.card.state-open .badge { color: var(--open); background: var(--open-soft); border-color: transparent; }
.card.state-open .badge .dot { background: var(--open); animation: pulse 2s infinite; }
.card.state-closed .badge { color: var(--closed); background: var(--closed-soft); border-color: transparent; }
.card.state-closed .badge .dot { background: var(--closed); }

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(255, 176, 56, 0.5); }
  70% { box-shadow: 0 0 0 7px rgba(255, 176, 56, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 176, 56, 0); }
}

/* ---------- Garage door illustration ---------- */
.stage {
  position: relative;
  border-radius: 14px;
  overflow: hidden;
  background: linear-gradient(180deg, rgba(255,255,255,0.03), rgba(0,0,0,0.15));
}
.stage svg { display: block; width: 100%; height: auto; }

.door-panel {
  transform: translateY(0);
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 0.9s cubic-bezier(0.16, 1, 0.3, 1);
}
.card.state-open .door-panel { transform: translateY(-101%); }
.card.moving .door-panel { transition-duration: 1.1s; }

.offline-tag {
  position: absolute;
  top: 10px; left: 10px;
  display: none;
  align-items: center;
  gap: 6px;
  padding: 4px 9px;
  font-size: 0.68rem;
  font-weight: 650;
  border-radius: 999px;
  color: #fff;
  background: rgba(255, 107, 107, 0.92);
}
.card.is-offline .offline-tag { display: inline-flex; }

/* ---------- Trigger button ---------- */
.btn {
  appearance: none;
  border: 0;
  border-radius: 14px;
  padding: 16px 18px;
  font-size: 1rem;
  font-weight: 650;
  letter-spacing: 0.2px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  box-shadow: 0 12px 26px -14px var(--accent);
  transition: filter 0.15s ease, transform 0.06s ease, opacity 0.15s ease;
}
.btn svg { width: 18px; height: 18px; }
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px) scale(0.995); }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn.busy .btn-label::after {
  content: "";
  display: inline-block;
  width: 14px; height: 14px;
  margin-left: 2px;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Skeleton loading ---------- */
.card.skeleton { pointer-events: none; }
.card.skeleton .badge,
.card.skeleton .card-title,
.card.skeleton .stage,
.card.skeleton .btn {
  color: transparent;
  background: linear-gradient(100deg, rgba(255,255,255,0.05) 30%, rgba(255,255,255,0.12) 50%, rgba(255,255,255,0.05) 70%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite;
  box-shadow: none;
}
.card.skeleton .card-title { border-radius: 6px; width: 42%; height: 1.1em; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  z-index: 5;
  max-width: min(92vw, 460px);
  padding: 13px 18px;
  border-radius: 14px;
  font-size: 0.9rem;
  color: var(--text);
  background: var(--card-solid);
  border: 1px solid var(--card-border);
  box-shadow: 0 22px 44px -18px rgba(0,0,0,0.7);
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { border-color: rgba(255, 107, 107, 0.6); }
.toast.success { border-color: rgba(51, 209, 143, 0.6); }

/* ---------- Proximity gate banner ---------- */
.geo-banner {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 860px;
  margin: 4px auto 0;
  padding: 13px 18px;
  border-radius: 14px;
  font-size: 0.9rem;
  color: var(--open);
  background: var(--open-soft);
  border: 1px solid rgba(255, 176, 56, 0.35);
}
.doors.gated { opacity: 0.5; filter: grayscale(0.4); pointer-events: none; }

/* ---------- Footer ---------- */
.foot {
  position: relative;
  z-index: 1;
  text-align: center;
  color: var(--faint);
  font-size: 0.74rem;
  padding: 10px 20px calc(22px + env(safe-area-inset-bottom));
}

@media (prefers-reduced-motion: reduce) {
  .door-panel, .btn, .toast, .badge { transition: none; }
  .card.state-open .badge .dot { animation: none; }
}

/* Phone portrait: fill the viewport, no page scroll — the door images shrink to
   fit the space left after the header/buttons/footer. Placed last so these
   overrides win over the base .card / .stage svg rules above. */
@media (max-width: 619px) {
  body { height: 100dvh; overflow: hidden; }
  .topbar { padding: calc(14px + env(safe-area-inset-top)) 18px 8px; }
  .doors {
    flex: 1;
    min-height: 0;
    gap: 12px;
    padding: 10px 14px;
    grid-auto-rows: 1fr; /* two equal-height rows share the leftover space */
  }
  .card { min-height: 0; padding: 14px; gap: 10px; }
  .stage { flex: 1; min-height: 0; }
  .stage svg { width: 100%; height: 100%; } /* viewBox + preserveAspectRatio => scale to fit */
  .btn { padding: 13px; }
  .foot { padding: 6px 20px calc(10px + env(safe-area-inset-bottom)); }
}
