:root {
  --bg0: #0c1218;
  --bg1: #15202b;
  --accent: #4cc2ff;
  --accent-dim: #2a7fb8;
  --danger: #e81123;
  --ok: #2ecc71;
  --muted: #9aa7b5;
  --text: #f3f6f9;
  --glass: rgba(255, 255, 255, 0.08);
  --glass-border: rgba(255, 255, 255, 0.16);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --radius: 14px;
  --font: "Outfit", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg0);
  overflow: hidden;
}

#bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(900px 600px at 12% 8%, #2a455c 0%, transparent 55%),
    radial-gradient(800px 500px at 88% 18%, #1a3348 0%, transparent 50%),
    linear-gradient(160deg, #0b1016, #152433 55%, #0e1620);
}
#bg::after {
  content: "";
  position: absolute;
  inset: 0;
  backdrop-filter: blur(28px) saturate(1.2);
  background: rgba(12, 18, 24, 0.25);
}

#app {
  position: relative;
  z-index: 1;
  height: 100%;
}

.mica {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(40px) saturate(1.4);
  box-shadow: var(--shadow);
  border-radius: var(--radius);
}

/* Login */
.login-wrap {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(400px, 100%);
  padding: 36px 32px 28px;
  text-align: center;
}
.brand {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  margin: 0 0 6px;
}
.brand span { color: var(--accent); }
.login-sub {
  margin: 0 0 28px;
  color: var(--muted);
  font-size: 0.95rem;
}
.field {
  text-align: left;
  margin-bottom: 14px;
}
.field label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 6px;
}
.field input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.14);
  background: rgba(0,0,0,0.28);
  color: var(--text);
  font: inherit;
  outline: none;
}
.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(76, 194, 255, 0.25);
}
.btn {
  appearance: none;
  border: none;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  border-radius: 10px;
  padding: 11px 16px;
  color: #041018;
  background: linear-gradient(180deg, #6ad0ff, #3aa8e0);
  box-shadow: 0 4px 0 #1f6f99, 0 8px 20px rgba(0,0,0,0.25);
  transition: transform 80ms ease, box-shadow 80ms ease, filter 120ms ease;
}
.btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #1f6f99, 0 2px 8px rgba(0,0,0,0.3);
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-block { width: 100%; margin-top: 8px; }
.btn-ghost {
  background: rgba(255,255,255,0.08);
  color: var(--text);
  box-shadow: 0 3px 0 rgba(0,0,0,0.35);
}
.btn-ghost:active { box-shadow: 0 1px 0 rgba(0,0,0,0.35); }
.btn-danger {
  background: linear-gradient(180deg, #ff5a67, #c20f20);
  color: #fff;
  box-shadow: 0 4px 0 #7a0a14, 0 8px 20px rgba(0,0,0,0.25);
}
.btn-danger:active { box-shadow: 0 1px 0 #7a0a14; }
.err {
  color: #ff8f9a;
  min-height: 1.2em;
  font-size: 0.85rem;
  margin: 8px 0 0;
}

/* Hub */
.shell {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 18px 22px 22px;
  gap: 16px;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.topbar h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
}
.topbar .meta { color: var(--muted); font-size: 0.85rem; }
.device-grid {
  flex: 1;
  overflow: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
  align-content: start;
  padding-right: 4px;
}
.device-card {
  position: relative;
  padding: 18px 16px 14px;
  cursor: pointer;
  transition: transform 120ms ease, border-color 120ms ease;
  min-height: 120px;
}
.device-card:hover {
  transform: translateY(-2px);
  border-color: rgba(76,194,255,0.45);
}
.device-card .name {
  font-weight: 650;
  font-size: 1.05rem;
  margin-bottom: 8px;
  padding-right: 28px;
  word-break: break-all;
}
.status-row {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.82rem;
}
.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #6b7785;
  flex: none;
}
.dot.on { background: var(--ok); box-shadow: 0 0 10px rgba(46,204,113,0.55); }
.battery { margin-top: 10px; font-size: 0.82rem; color: var(--muted); }
.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}
.btn-chip {
  border: 1px solid rgba(255,255,255,0.14);
  background: rgba(255,255,255,0.06);
  color: var(--muted);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-chip:hover {
  border-color: rgba(76,194,255,0.45);
  color: #dff4ff;
}
.btn-chip.on {
  background: rgba(46,204,113,0.18);
  border-color: rgba(46,204,113,0.45);
  color: #9dffc0;
}
.btn-chip:disabled {
  opacity: 0.55;
  cursor: wait;
}
.trash {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
}
.trash:hover {
  background: rgba(232,17,35,0.15);
  border-color: rgba(232,17,35,0.35);
  color: #ff8f9a;
}
.empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 60px 20px;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0,0,0,0.45);
  display: grid;
  place-items: center;
  padding: 20px;
}
.modal {
  width: min(420px, 100%);
  padding: 22px;
}
.modal h2 { margin: 0 0 8px; font-size: 1.1rem; }
.modal p { margin: 0 0 18px; color: var(--muted); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }

/* Control room */
.room {
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  padding: 16px 18px 18px;
  padding-bottom: max(18px, env(safe-area-inset-bottom));
  overflow: hidden;
}
.room-top {
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: space-between;
  flex-wrap: wrap;
}
.room-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.room-top .title { font-weight: 700; font-size: 1.15rem; }
/* Desktop: video | status side-by-side. Mobile: status stacks above Front Cam. */
.room-mid {
  display: grid;
  grid-template-columns: 1fr minmax(180px, 240px);
  gap: 12px;
  min-height: 0;
  align-items: stretch;
}
.status-box {
  padding: 12px 14px;
  font-size: 0.8rem;
  line-height: 1.35;
  color: var(--muted);
}
.status-box .status-title {
  font-weight: 700;
  color: #e8f4ff;
  margin-bottom: 8px;
  font-size: 0.85rem;
}
.status-box .status-cam {
  margin: 8px 0;
  font-weight: 650;
  color: #c5d4e0;
}
.status-box .status-cam.ok { color: #9dffc0; }
.status-box .status-cam.bad { color: #ff8f9a; }
.status-box .status-detail {
  margin-top: 6px;
  color: #ffb4a8;
  word-break: break-word;
}
.status-box .status-data {
  margin-top: 10px;
  font-size: 0.75rem;
  opacity: 0.9;
}
.video-stage {
  position: relative;
  min-height: 280px;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.video-frame {
  position: relative;
  width: min(100%, 960px);
  aspect-ratio: 4 / 3;
  max-height: 100%;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--glass-border);
  resize: both;
  min-width: 240px;
  min-height: 180px;
}
.video-frame video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  display: block;
}
.video-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  pointer-events: none;
  padding: 12px;
  text-align: center;
}
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}
.rec-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
  margin-right: 6px;
  animation: blink 1s infinite;
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

/* Media Vault Gallery */
.gallery-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, 0.55);
  display: grid;
  place-items: center;
  padding: 16px;
}
.gallery-panel {
  width: min(960px, 100%);
  height: min(860px, 92vh);
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}
.gallery-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--glass-border);
}
.gallery-title { font-weight: 700; font-size: 1.1rem; }
.gallery-sub { color: var(--muted); font-size: 0.8rem; margin-top: 2px; }
.gallery-top-actions { display: flex; gap: 8px; }
.gallery-body {
  overflow: auto;
  padding: 14px 16px 18px;
}
.gallery-meta {
  color: var(--muted);
  font-size: 0.78rem;
  margin-bottom: 10px;
}
.gallery-empty {
  text-align: center;
  color: var(--muted);
  padding: 48px 16px;
  line-height: 1.5;
  font-size: 0.92rem;
}
.gallery-empty b { color: var(--accent); }
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.gallery-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 0;
  border: 1px solid var(--glass-border);
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.28);
  color: inherit;
  font: inherit;
  cursor: pointer;
  overflow: hidden;
  text-align: left;
}
.gallery-card:hover {
  border-color: rgba(76, 194, 255, 0.45);
}
.gallery-card-wrap {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}
.gallery-dl-btn {
  width: 100%;
  justify-content: center;
}
.gallery-folder-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}
.gallery-folder-name {
  font-weight: 600;
  color: #e8f4ff;
  font-size: 0.9rem;
  flex: 1;
  min-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.gallery-thumb-folder {
  display: grid;
  place-items: center;
  font-size: 2rem;
  background: rgba(76, 194, 255, 0.08);
}
.btn-active {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  box-shadow: 0 0 0 3px rgba(76, 194, 255, 0.2);
}
.gallery-thumb {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  background: #0a0e14;
  display: block;
}
.gallery-thumb-video,
.gallery-thumb-file {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 1.6rem;
  font-weight: 700;
}
.gallery-thumb-file { font-size: 0.85rem; letter-spacing: 0.06em; }
.gallery-thumb-wrap {
  position: relative;
  width: 100%;
}
.gallery-kind {
  position: absolute;
  left: 6px;
  top: 6px;
  z-index: 2;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 3px 6px;
  border-radius: 6px;
  color: #041018;
  background: rgba(255, 255, 255, 0.88);
  pointer-events: none;
}
.gallery-kind.video {
  color: #fff;
  background: rgba(232, 17, 35, 0.92);
}
.gallery-kind.photo {
  background: rgba(76, 194, 255, 0.92);
}
.gallery-play-fab {
  position: absolute;
  right: 8px;
  bottom: 8px;
  z-index: 2;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  color: #fff;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.35);
  pointer-events: none;
}
.gallery-card-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0 8px 8px;
  font-size: 0.72rem;
  color: var(--muted);
  min-width: 0;
}
.gallery-card-meta span:first-child {
  color: #e8f4ff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.viewer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(0, 0, 0, 0.82);
  display: grid;
  place-items: center;
  padding: 12px;
}
.viewer-backdrop[hidden] { display: none !important; }
.viewer-panel {
  width: min(1100px, 100%);
  max-height: 94vh;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 8px;
}
.viewer-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.viewer-name {
  flex: 1;
  min-width: 120px;
  font-size: 0.85rem;
  color: #e8f4ff;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.viewer-stage {
  min-height: 200px;
  max-height: calc(94vh - 56px);
  display: grid;
  place-items: center;
  background: #000;
  border-radius: 12px;
  border: 1px solid var(--glass-border);
  overflow: auto;
}
.viewer-media {
  max-width: 100%;
  max-height: min(70vh, 720px);
  object-fit: contain;
  border-radius: 8px;
}
.viewer-video {
  width: 100%;
  background: #000;
  max-height: min(68vh, 720px);
}
.viewer-hint {
  margin-top: 8px;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
}
.viewer-kind {
  display: inline-block;
  margin-right: 6px;
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #fff;
  vertical-align: middle;
}
.viewer-kind.video { background: rgba(232, 17, 35, 0.9); }
.viewer-kind.photo { background: rgba(42, 127, 184, 0.95); }
  max-width: 100%;
  max-height: calc(94vh - 100px);
  object-fit: contain;
  display: block;
  transition: transform 0.2s ease;
  user-select: none;
  -webkit-user-drag: none;
}
.viewer-mobile .viewer-stage {
  touch-action: pan-y;
  cursor: grab;
  min-height: 280px;
}
.viewer-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 4px 0 2px;
}
.viewer-nav .btn {
  font-size: 1.6rem;
  line-height: 1;
  min-width: 48px;
  padding: 6px 14px;
}
.viewer-count {
  color: var(--muted);
  font-size: 0.85rem;
  min-width: 64px;
  text-align: center;
}

@media (max-width: 720px) {
  html, body { overflow: auto; }
  #app { min-height: 100%; min-height: 100dvh; }
  .room {
    height: auto;
    min-height: 100dvh;
    max-height: none;
    grid-template-rows: auto minmax(160px, 42vh) auto auto;
    overflow: visible;
    gap: 10px;
  }
  /* Flatten so order is: top → video → status → Front Cam controls */
  .room-mid {
    display: contents;
  }
  .video-stage {
    min-height: 160px;
    max-height: 42vh;
  }
  .video-frame {
    resize: none;
    width: 100%;
    max-height: 42vh;
    aspect-ratio: 9 / 16;
    min-width: 0;
    min-height: 140px;
  }
  .status-box {
    order: unset;
    flex: none;
  }
  .controls {
    position: sticky;
    bottom: 0;
    z-index: 5;
    padding: 10px 4px max(10px, env(safe-area-inset-bottom));
    background: linear-gradient(180deg, transparent, rgba(8, 12, 18, 0.92) 28%);
  }
  .controls .btn {
    flex: 1 1 calc(50% - 10px);
    min-width: 132px;
    font-size: 0.92rem;
    padding: 12px 10px;
  }
  .room-tools {
    width: 100%;
    justify-content: flex-start;
  }
}

@media (max-width: 640px) {
  .shell, .room { padding: 12px; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
  .gallery-panel { height: 96vh; height: 96dvh; }
  .gallery-grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; }
  .viewer-panel { width: 100%; max-height: 96vh; max-height: 96dvh; }
}
