/* ── Reset & custom properties ───────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #0d0f18;
  --surface:     #161927;
  --surface-2:   #1e2235;
  --border:      #2b2f4a;
  --accent:      #6c63ff;
  --accent-h:    #574fd6;
  --danger:      #e05555;
  --danger-h:    #c04040;
  --green:       #3dd68c;
  --yellow:      #f0c040;
  --text:        #e6e8f5;
  --text-muted:  #7a7da8;
  --radius:      14px;
  --radius-sm:   8px;
  --shadow:      0 6px 28px rgba(0,0,0,.55);

  --header-h:    48px;
  --banner-h:    36px;
}

html { height: 100%; }
body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ── App shell ───────────────────────────────────────────────────────────── */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

/* ── Header ──────────────────────────────────────────────────────────────── */
.header {
  flex-shrink: 0;
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.logo { font-weight: 700; font-size: 17px; letter-spacing: .4px; white-space: nowrap; }
.logo span { color: var(--accent); }

/* ── Header nav ──────────────────────────────────────────────────────────── */
.header-nav {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  overflow: hidden;
}
.header-nav a {
  font-size: 13px;
  color: var(--text-muted);
  padding: 5px 9px;
  border-radius: 6px;
  white-space: nowrap;
  text-decoration: none;
  transition: color .15s, background .15s;
}
.header-nav a:hover { color: var(--text); background: rgba(255,255,255,.06); }

/* hide the longest nav item on narrower windows */
@media (max-width: 900px) { .nav-hidden-sm { display: none; } }
@media (max-width: 680px) { .header-nav { display: none; } }

/* ── Language switcher ───────────────────────────────────────────────────── */
.btn-lang {
  flex-shrink: 0;
  background: rgba(108,99,255,.12);
  border: 1px solid rgba(108,99,255,.25);
  color: var(--accent);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .06em;
  cursor: pointer;
  transition: background .15s, color .15s;
  line-height: 1;
}
.btn-lang:hover { background: rgba(108,99,255,.22); color: var(--text); }

/* ── Status bar ──────────────────────────────────────────────────────────── */
.status-bar {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text-muted);
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
}

#status-text {
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}

.dot {
  flex-shrink: 0;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: background .3s;
}
.dot.idle       { background: var(--text-muted); }
.dot.searching  { background: var(--yellow); animation: pulse 1.4s ease-in-out infinite; }
.dot.connected  { background: var(--green); }
.dot.error      { background: var(--danger); }

@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:.2; } }

/* ── Admin banner ────────────────────────────────────────────────────────── */
.admin-banner {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 16px;
  height: var(--banner-h);
  background: rgba(240,192,64,.1);
  border-bottom: 1px solid rgba(240,192,64,.28);
  color: var(--yellow);
  font-size: 12.5px;
  font-weight: 500;
  animation: fade-slide-in .35s ease;
}

@keyframes fade-slide-in {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: none; }
}

/* ── Main area ───────────────────────────────────────────────────────────── */
/*
  Mobile default: flex column.
  .video-grid takes flex:1, .chat-wrapper sits below it in the flow.
  No position:relative needed here — video-cell has its own position:relative
  for the overlay and labels.
*/
.main-area {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* ── Video grid ──────────────────────────────────────────────────────────── */
.video-grid {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;  /* mobile: stacked vertically */
  gap: 3px;
  background: #000;
  padding: 3px;
}

.video-cell {
  flex: 1;
  min-height: 0;
  position: relative;
  background: #0a0b12;
  border-radius: var(--radius);
  overflow: hidden;
}

.video-cell video {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}

/* Remote video: invisible by default, fades in when stream arrives */
#remote-video {
  opacity: 0;
  transition: opacity .45s ease;
}
#remote-video.stream-active { opacity: 1; }

#local-video { transform: scaleX(-1); }
#local-video.camera-environment { transform: none; }

.video-label {
  position: absolute;
  top: 10px; left: 10px;
  background: rgba(0,0,0,.52);
  color: rgba(255,255,255,.85);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .4px;
  padding: 3px 9px;
  border-radius: 20px;
  backdrop-filter: blur(4px);
  pointer-events: none;
}
.you-label { left: auto; right: 10px; }

/* ── Video cell overlay buttons ──────────────────────────────────────────── */
/*
  Positioned absolutely within their .video-cell (position:relative).
  48×48px meets both Apple HIG and Material Design minimum touch target.
  z-index:6 keeps them above the spinner overlay (z-index:5).
*/
.btn-video-overlay {
  position: absolute;
  z-index: 6;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: rgba(0, 0, 0, .52);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  border: none;
  color: rgba(255, 255, 255, .9);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background .15s, transform .1s;
}
.btn-video-overlay:active:not(:disabled) { transform: scale(.88); }
.btn-video-overlay:disabled { opacity: .4; cursor: not-allowed; }

/* Flip camera — bottom-right of local video (thumb-zone) */
.btn-flip-cam {
  bottom: 10px;
  right: 10px;
}

/* Report — top-right of remote video (deliberate tap, Instagram/TikTok pattern) */
.btn-report-overlay {
  top: 10px;
  right: 10px;
  color: #ff6b6b;
}
.btn-report-overlay:active:not(:disabled) {
  background: rgba(224, 85, 85, .35);
  transform: scale(.88);
}

/* ── Overlay (inside video-cell) ─────────────────────────────────────────── */
.overlay {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 14px;
  background: rgba(10,11,18,.85);
  backdrop-filter: blur(5px);
  z-index: 5;
  transition: opacity .4s ease;
}
.overlay.hidden    { display: none; }
.overlay.overlay-out { opacity: 0; pointer-events: none; }

/* ── Searching pulse: sonar rings around spinner ──────────────────────────── */
.overlay.overlay-searching .spinner { position: relative; }

.overlay.overlay-searching .spinner::before,
.overlay.overlay-searching .spinner::after {
  content: '';
  position: absolute;
  top: 50%; left: 50%;
  width: 36px; height: 36px;
  margin: -18px 0 0 -18px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  opacity: 0;
  animation: search-ring 1.8s ease-out infinite;
}
.overlay.overlay-searching .spinner::after {
  border-width: 1.5px;
  border-color: rgba(108,99,255,.5);
  animation-delay: .9s;
}

@keyframes search-ring {
  0%   { transform: scale(1); opacity: .7; }
  100% { transform: scale(3); opacity: 0; }
}

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
.spinner.hidden { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

#overlay-text {
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
  padding: 0 20px;
}

/* ── Chat wrapper ────────────────────────────────────────────────────────── */
/*
  The grid-template-rows trick gives a smooth height transition without
  needing to know the panel's exact pixel height.
  grid-template-rows: 1fr  → panel takes its natural height
  grid-template-rows: 0fr  → panel collapses to zero (requires min-height:0 on child)
*/
.chat-wrapper {
  flex-shrink: 0;
  display: grid;
  grid-template-rows: 1fr;
  transition: grid-template-rows 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}

.chat-wrapper.chat-collapsed {
  grid-template-rows: 0fr;
}

/* ── Chat panel ──────────────────────────────────────────────────────────── */
/*
  min-height:0  — allows the grid 0fr collapse to actually reach zero height.
  overflow:hidden — clips content during the collapse animation.
  max-height:42vh — caps the panel so video always gets at least ~55% of screen.
*/
.chat-panel {
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: 42vh;
  background: rgba(13,15,24,0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid rgba(43,47,74,0.5);
}

/* ── Mobile action bar ───────────────────────────────────────────────────── */
/*
  Visible only on mobile (hidden via desktop media query).
  Replaces the desktop .controls-bar: buttons are grouped here and collapse
  together with the chat panel.
*/
.mobile-action-bar {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid rgba(43,47,74,0.4);
  flex-shrink: 0;
}

/* Action buttons inside the mobile bar */
.btn-action-m {
  flex: 1;
  justify-content: center;
  min-height: 44px;  /* Apple/Google minimum tap target */
  font-size: 13px;
  font-weight: 600;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
}

/* Icon-only button (collapse chevron) */
.btn-icon {
  flex: 0 0 44px;
  width: 44px;
  padding: 0;
  justify-content: center;
}

/* ── Chat messages ───────────────────────────────────────────────────────── */
.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.msg {
  max-width: 85%;
  padding: 6px 11px;
  border-radius: 14px;
  word-break: break-word;
  white-space: pre-wrap;
  font-size: 12.5px;
  line-height: 1.45;
}
.msg.mine   { align-self: flex-end;   background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.theirs { align-self: flex-start; background: var(--surface-2); border-bottom-left-radius: 4px; }
.msg.system {
  align-self: center;
  background: rgba(255,255,255,.055);
  color: var(--text-muted);
  font-size: 11px;
  padding: 3px 12px;
  border-radius: 20px;
  max-width: 88%;
  text-align: center;
}

.msg.error {
  background: rgba(224,85,85,.14);
  color: #ff9898;
  border: 1px solid rgba(224,85,85,.22);
}

.msg.warn {
  background: rgba(240,192,64,.11);
  color: #f0d070;
  border: 1px solid rgba(240,192,64,.22);
}

/* ── Chat input row ──────────────────────────────────────────────────────── */
.chat-input-row {
  display: flex;
  gap: 6px;
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  align-items: center;
}

/* ≥16px prevents iOS Safari auto-zoom when the input is focused */
#chat-input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color .2s;
}
#chat-input:focus        { border-color: var(--accent); }
#chat-input:disabled     { opacity: .4; cursor: not-allowed; }
#chat-input::placeholder { color: var(--text-muted); }

/* ── Desktop controls bar — hidden on mobile ─────────────────────────────── */
.controls-bar {
  display: none;  /* shown only in desktop media query */
}
.controls-inner {
  display: flex;
  gap: 10px;
  justify-content: center;
  align-items: center;
  max-width: 480px;
  margin: 0 auto;
}

/* ── Chat FAB (mobile only — expand collapsed chat) ─────────────────────── */
/*
  position:fixed keeps it completely out of the document flow.
  This is intentional: the FAB must never interfere with any in-flow element
  (input row, action bar, video grid).
  Hidden by default; JS adds .fab-visible when chat-wrapper gets .chat-collapsed.
*/
.btn-chat-fab {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  right: 16px;
  z-index: 50;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  display: none;             /* hidden until .fab-visible */
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 18px rgba(0,0,0,.55);
  transition: transform 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.btn-chat-fab.fab-visible { display: flex; }
.btn-chat-fab:active { transform: scale(0.88); }

/* Unread dot — shown when chat is collapsed and a new message arrives */
.btn-chat-fab::after {
  content: '';
  position: absolute;
  top: 5px; right: 5px;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--danger);
  border: 2px solid var(--bg);
  display: none;
}
.btn-chat-fab.has-unread::after { display: block; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  cursor: pointer;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  transition: background .15s, opacity .15s, transform .1s;
  outline: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.btn:active:not(:disabled) { transform: scale(.96); }
.btn:focus-visible { box-shadow: 0 0 0 3px rgba(108,99,255,.4); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* Desktop control buttons */
.btn-control {
  flex: 1;
  justify-content: center;
  padding: 12px 16px;
  min-height: 46px;
  font-size: 14px;
}

.btn-accent  { background: var(--accent);  color: #fff; }
.btn-accent:hover:not(:disabled)  { background: var(--accent-h); }

.btn-outline {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--text-muted);
}
.btn-outline:hover:not(:disabled) { border-color: var(--text-muted); color: var(--text); }

.btn-danger  { background: var(--danger);  color: #fff; }
.btn-danger:hover:not(:disabled)  { background: var(--danger-h); }

.btn-send-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  background: var(--accent);
  color: #fff;
  padding: 0;
  border-radius: var(--radius-sm);
  justify-content: center;
}
.btn-send-btn:hover:not(:disabled) { background: var(--accent-h); }
.btn-send-btn:disabled { background: var(--border); color: var(--text-muted); cursor: not-allowed; opacity: .7; }

/* ── Toast ───────────────────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 200;
  pointer-events: none;
}

.toast {
  background: rgba(22,25,39,.96);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 18px;
  border-radius: 24px;
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: toast-in .25s ease forwards;
}
.toast.out { animation: toast-out .25s ease forwards; }

@keyframes toast-in  { from { opacity:0; transform: translateY(8px); } to { opacity:1; transform:none; } }
@keyframes toast-out { from { opacity:1; } to { opacity:0; transform: translateY(-4px); } }

/* ── TOS Modal ───────────────────────────────────────────────────────────── */
.tos-overlay {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  background: rgba(10,11,18,.97);
  backdrop-filter: blur(6px);
  padding: 20px;
}

.tos-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  max-width: 500px; width: 100%;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 18px;
}
.tos-box h2 { font-size: 17px; font-weight: 700; }

.tos-body {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.7;
  max-height: 260px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.tos-body ul { padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }
.tos-body strong { color: var(--text); }
.tos-body a { color: var(--accent); text-decoration: none; }
.tos-body a:hover { text-decoration: underline; }

.tos-actions { display: flex; gap: 10px; justify-content: flex-end; }
.tos-actions .btn { padding: 9px 20px; }

/* ── Desktop layout (≥ 768px) ────────────────────────────────────────────── */
@media (min-width: 768px) {

  /* Side-by-side: video | chat sidebar */
  .main-area {
    flex-direction: row;
  }

  .video-grid {
    flex-direction: row;
    padding: 4px;
    gap: 4px;
    flex: 1;
    min-width: 0;
  }

  /*
    Reset chat-wrapper from grid (mobile collapse) to plain flex container
    acting as a fixed-width sidebar column.
    transform:none !important prevents any lingering .chat-collapsed state
    from affecting the desktop layout.
  */
  .chat-wrapper {
    display: flex;
    flex-direction: column;
    flex: 0 0 280px;
    min-height: 0;
    grid-template-rows: initial;
    transition: none;
    transform: none !important;
  }

  .chat-panel {
    flex: 1;
    min-height: 0;
    max-height: none;
    overflow: hidden;
    background: var(--surface);
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    border-top: none;
    border-left: 1px solid var(--border);
  }

  /* Mobile-only elements */
  .mobile-action-bar  { display: none; }
  .btn-chat-fab       { display: none !important; }
  .btn-flip-cam       { display: none !important; }
  .btn-report-overlay { display: none !important; }

  /* Input: smaller font is fine on desktop (no iOS auto-zoom risk) */
  #chat-input { font-size: 13px; padding: 7px 11px; }

  /* Desktop controls bar */
  .controls-bar {
    display: block;
    flex-shrink: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 10px 16px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }

  .controls-inner {
    max-width: 640px;
  }

  .btn-control {
    max-width: 200px;
  }

  /* Toast above the desktop controls bar */
  .toast-container {
    bottom: calc(46px + 20px + 16px); /* btn-height + padding + gap */
  }
}

/* ── Large screens ───────────────────────────────────────────────────────── */
@media (min-width: 1100px) {
  .chat-wrapper { flex: 0 0 300px; }
}

/* ── SPA footer (internal nav links) ────────────────────────────────────── */
.spa-footer {
  display: none;
  text-align: center;
  padding: 10px 20px;
  background: var(--bg);
  border-top: 1px solid rgba(255,255,255,.06);
  gap: 18px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}
.spa-footer a { color: var(--muted); text-decoration: none; transition: color .15s; }
.spa-footer a:hover { color: var(--accent); }
.spa-footer-copy { color: var(--muted); opacity: .5; }

@media (min-width: 768px) {
  .spa-footer { display: flex; }
}
