/* HomeCam 웹 UI — 폰 세로 화면 우선, 넓은 화면에서 2열 */

:root {
  --bg: #0e1116;
  --surface: #171c24;
  --surface-2: #1e242e;
  --border: #262d39;
  --text: #e6eaf0;
  --muted: #8b95a5;
  --accent: #4a9eff;
  --ok: #3ecf8e;
  --warn: #f0b429;
  --err: #f2626b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Noto Sans KR", sans-serif;
  -webkit-text-size-adjust: 100%;
}

/* --- 상단바 --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 18px; margin: 0; letter-spacing: -0.01em; }
.topbar-actions { display: flex; gap: 8px; }

button, .button-like {
  font: inherit;
  font-size: 14px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}
button.ghost, .button-like.ghost {
  background: transparent; border-color: var(--border); color: var(--text);
}
button.ghost:hover { border-color: var(--accent); }
button.ghost.active { border-color: var(--ok); color: var(--ok); }
button.danger { background: var(--err); }
button.small, .button-like.small { padding: 5px 10px; font-size: 13px; }

/* --- 탭 --- */
.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 12px 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 57px;
  z-index: 9;
}
.tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  color: var(--muted);
  padding: 10px 14px;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

.panel { display: none; padding: 14px; padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
.panel.active { display: block; }

.panel-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.muted-text { color: var(--muted); font-size: 13px; }
.empty { color: var(--muted); text-align: center; padding: 40px 12px; line-height: 1.7; }

/* --- 스위치 --- */
.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }
.switch.row { padding: 8px 0; }

/* --- 라이브 그리드 --- */
.grid { display: grid; grid-template-columns: 1fr; gap: 14px; }
@media (min-width: 760px) { .grid { grid-template-columns: repeat(2, 1fr); } }

.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.tile-video { position: relative; aspect-ratio: 16 / 9; background: #000; }
.tile-video video { width: 100%; height: 100%; object-fit: contain; display: block; }

.tile-overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  padding: 12px; text-align: center;
  color: var(--muted); font-size: 14px;
  background: rgba(0, 0, 0, 0.55);
}
.tile-overlay[hidden] { display: none; }

.tile-badge {
  position: absolute; top: 10px; left: 10px;
  background: rgba(242, 98, 107, 0.9);
  color: #fff; font-size: 12px; font-weight: 600;
  padding: 4px 9px; border-radius: 6px;
}

.tile-bar { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-top: 1px solid var(--border); }
.tile-name { font-weight: 600; font-size: 15px; }
.tile-status { display: inline-flex; align-items: center; gap: 6px; margin-left: auto; color: var(--muted); font-size: 13px; }

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot[data-state="live"]    { background: var(--ok); }
.dot[data-state="pending"] { background: var(--warn); }
.dot[data-state="error"]   { background: var(--err); }
.dot[data-state="off"]     { background: var(--muted); }

/* --- 녹화 목록 --- */
.rec-list { display: grid; grid-template-columns: 1fr; gap: 10px; }
@media (min-width: 620px) { .rec-list { grid-template-columns: repeat(2, 1fr); } }
@media (min-width: 1000px) { .rec-list { grid-template-columns: repeat(3, 1fr); } }

.rec-card {
  display: flex; gap: 12px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; overflow: hidden; cursor: pointer;
}
.rec-card:hover { border-color: var(--accent); }

.rec-thumb { position: relative; width: 128px; flex: none; aspect-ratio: 16 / 9; background: #000; }
.rec-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.no-thumb { display: grid; place-items: center; height: 100%; color: var(--muted); font-size: 11px; }
.rec-duration {
  position: absolute; right: 4px; bottom: 4px;
  background: rgba(0, 0, 0, 0.75); color: #fff;
  font-size: 11px; padding: 2px 5px; border-radius: 4px;
}

.rec-info { display: flex; flex-direction: column; justify-content: center; gap: 6px; padding: 8px 10px 8px 0; min-width: 0; }
.rec-time { font-size: 14px; font-weight: 600; }
.rec-sub { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; flex-wrap: wrap; }
.node-badge { background: var(--surface-2); border: 1px solid var(--border); padding: 2px 7px; border-radius: 999px; }

.more-btn { display: block; margin: 16px auto 0; }

/* --- 설정 --- */
.settings-list { display: grid; grid-template-columns: 1fr; gap: 14px; }
@media (min-width: 760px) { .settings-list { grid-template-columns: repeat(2, 1fr); } }

.settings-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.settings-card.offline { opacity: 0.6; }
.settings-card h3 { margin: 0 0 6px; font-size: 16px; }
.settings-status { margin-bottom: 12px; line-height: 1.6; }

.field { display: block; margin: 14px 0; font-size: 14px; }
.field > span { display: block; margin-bottom: 6px; }
.field small { color: var(--muted); }
.field b { color: var(--accent); }
.field input[type=range] { width: 100%; accent-color: var(--accent); }

/* --- 재생 모달 --- */
dialog {
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text);
  padding: 0; width: min(860px, calc(100vw - 24px));
}
dialog::backdrop { background: rgba(0, 0, 0, 0.7); }

.player-body { display: grid; gap: 10px; padding: 14px; }
.player-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-weight: 600; }
#player { width: 100%; max-height: 65vh; background: #000; border-radius: 8px; }
.player-meta { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.player-buttons { display: flex; align-items: center; gap: 8px; }
.player-buttons select {
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 4px 6px; font: inherit; font-size: 13px;
}

/* --- 노드 다이얼로그 --- */
.dialog-body { padding: 18px; display: grid; gap: 12px; }
.dialog-body h2 { margin: 0; font-size: 17px; }
.hint { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.5; }

textarea {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; resize: vertical;
}
.error { margin: 0; color: var(--err); font-size: 13px; }
menu { display: flex; justify-content: flex-end; gap: 8px; margin: 0; padding: 0; }

/* --- 전체화면 뷰어 --- */
.fs {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: #000;
  display: flex;
  flex-direction: column;
}
.fs[hidden] { display: none; }

.fs-stage {
  flex: 1;
  overflow: hidden;
  display: grid;
  place-items: center;
  /* 브라우저 기본 핀치줌을 막아야 직접 계산한 확대가 정상 동작한다 */
  touch-action: none;
  cursor: zoom-in;
}
.fs-stage.zoomed { cursor: grab; }

.fs-stage video {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  display: block;
  transform-origin: center center;
  /* 확대 중 드래그가 텍스트 선택으로 새지 않게 */
  user-select: none;
  -webkit-user-select: none;
}

.fs-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: rgba(23, 28, 36, 0.92);
  border-top: 1px solid var(--border);
  font-size: 14px;
}
.fs-bar > span:first-child { font-weight: 600; margin-right: auto; }

.fs-hint {
  position: absolute;
  top: calc(16px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 999px;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
}
.fs-hint.show { opacity: 1; }

/* 타일 영상은 눌러서 전체화면으로 갈 수 있음을 표시 */
.tile-video { cursor: pointer; }

/* --- 소리 레벨 미터 --- */
.sep { border: none; border-top: 1px solid var(--border); margin: 16px 0 8px; }

.meter-wrap { margin: 10px 0 4px; }

.meter {
  position: relative;
  height: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  width: 0%;
  background: var(--ok);
  transition: width 0.15s linear;
}
/* 임계값을 넘는 순간 색으로 즉시 알려준다 */
.meter-fill.over { background: var(--err); }

.meter-threshold {
  position: absolute;
  top: -2px;
  bottom: -2px;
  width: 2px;
  background: var(--warn);
  left: 0%;
}

.meter-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
}
.meter-labels b { color: var(--text); }

/* --- 토스트 --- */
.toast {
  position: fixed;
  left: 50%; transform: translateX(-50%);
  bottom: calc(20px + env(safe-area-inset-bottom));
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 16px; font-size: 14px;
  z-index: 100; max-width: calc(100vw - 32px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
.toast.error-toast { border-color: var(--err); color: var(--err); }

/* --- 해상도 선택 (설정 카드) ------------------------------------------- */
/* 카메라 스위치 바로 아래 붙는 상태 한 줄 */
.camera-note { margin: -4px 0 10px 0; }

.res-group { margin-top: 4px; }
/* 라벨과 컨트롤을 한 줄에 두되, 좁은 화면에서는 자연스럽게 접히게 한다. */
.res-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 10px 0; }
.res-row > span:first-child { flex: 1 1 140px; margin-bottom: 0; }
.res-controls { display: flex; gap: 6px; flex: 0 0 auto; }
.res-controls select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font: inherit;
  font-size: 13px;
}
.res-controls select:disabled { opacity: 0.5; }

/* --- Kinect 전용 조작 --------------------------------------------------- */
.kinect-group { margin-top: 4px; }
.kinect-group .kinect-tilt-state { color: var(--muted); }

/* --- 카메라/마이크 선택 (설정 카드) ------------------------------------- */
.source-group { margin-bottom: 4px; }
.source-group .res-controls select { min-width: 0; flex: 1; }
.source-url-controls { display: flex; gap: 6px; flex: 1; }
.source-url-controls .source-url {
  flex: 1;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font: inherit;
  font-size: 13px;
  min-width: 0;
}
