:root {
  color-scheme: dark;
  --bg: #050608;
  --panel: rgba(16, 18, 24, 0.92);
  --panel2: rgba(8, 10, 14, 0.96);
  --line: rgba(255,255,255,0.11);
  --text: #e9edf3;
  --muted: rgba(233,237,243,0.58);
  --good: #62ff9e;
  --warn: #ffca62;
  --bad: #ff6272;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body {
  position: fixed;
  inset: 0;
  min-height: 100dvh;
  overflow: hidden;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at 50% 20%, rgba(255,255,255,0.06), transparent 34rem),
    var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.app {
  width: min(92vw, 520px);
}

.panel,
.chat {
  position: relative;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: linear-gradient(180deg, var(--panel), var(--panel2));
  box-shadow: 0 24px 90px rgba(0,0,0,0.48);
  backdrop-filter: blur(16px);
}

.panel {
  padding: 18px;
  display: grid;
  gap: 10px;
  align-items: center;
}

.joinBox {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: center;
}

.phrase,
.send input {
  width: 100%;
  min-width: 0;
  height: 48px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(0,0,0,0.35);
  color: var(--text);
  outline: none;
  padding: 0 15px;
  font: inherit;
}

.phrase:focus,
.send input:focus {
  border-color: rgba(255,255,255,0.28);
}

.buttons {
  display: flex;
  gap: 8px;
}

button {
  height: 48px;
  min-width: 76px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(255,255,255,0.075);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button:hover { background: rgba(255,255,255,0.12); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: default; transform: none; }

.passgen {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 48px;
  gap: 8px;
  align-items: center;
}

.password-box {
  width: 100%;
  min-width: 0;
  height: 38px;
  padding: 0 12px;
  border: 1px solid rgba(255,255,255,0.085);
  border-radius: 14px;
  background: rgba(0,0,0,0.22);
  color: rgba(233,237,243,0.82);
  font-size: 13px;
  font-weight: 650;
  letter-spacing: 0.03em;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  text-align: center;
  transition: color 0.25s ease, border-color 0.25s ease, background 0.25s ease;
}

.password-box.copied {
  border-color: rgba(98,255,158,0.25);
  background: rgba(98,255,158,0.055);
}

.generate-btn {
  min-width: 48px;
  width: 48px;
  height: 38px;
  border-radius: 14px;
  font-size: 18px;
  line-height: 1;
  padding-bottom: 2px;
}

.password-popup {
  grid-column: 1 / -1;
  min-height: 12px;
  margin-top: -3px;
  text-align: center;
  font-size: 10px;
  line-height: 12px;
  color: var(--good);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease;
}

.password-popup.show {
  opacity: 1;
}

.dot {
  position: absolute;
  top: 5px;
  left: 16%;
  right: 16%;
  width: auto;
  height: 4px;
  border-radius: 999px;
  background: transparent;
  opacity: 0;
  box-shadow: 0 0 0 transparent;
  transition: opacity 0.18s ease, background 0.18s ease, box-shadow 0.18s ease;
}

.dot.waiting { opacity: 1; background: var(--good); box-shadow: 0 0 16px rgba(98,255,158,0.45); }
.dot.good { opacity: 1; background: var(--good); box-shadow: 0 0 16px rgba(98,255,158,0.55); }
.dot.bad { opacity: 1; background: var(--bad); box-shadow: 0 0 16px rgba(255,98,114,0.55); }

.hidden { display: none; }

.chat {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  min-height: 100dvh;
  border: 0;
  border-radius: 0;
  padding:
    calc(14px + env(safe-area-inset-top))
    calc(14px + env(safe-area-inset-right))
    calc(14px + env(safe-area-inset-bottom))
    calc(14px + env(safe-area-inset-left));
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 12px;
  overflow: hidden;
}
.chat:not(.hidden) { display: grid; }

.disconnect {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top));
  left: calc(12px + env(safe-area-inset-left));
  z-index: 3;
  width: 38px;
  min-width: 38px;
  height: 38px;
  border-radius: 13px;
  padding: 0;
  font-size: 24px;
  line-height: 30px;
  color: rgba(233,237,243,0.74);
  background: rgba(255,255,255,0.065);
}

.disconnect:hover {
  color: var(--bad);
  border-color: rgba(255,98,114,0.28);
}

.chat-status {
  position: absolute;
  left: 12px;
  top: 50%;
  z-index: 2;
  width: 14px;
  height: 14px;
  border-radius: 999px;
  border: 3px solid transparent;
  background: transparent;
  opacity: 0;
  box-shadow: 0 0 0 transparent;
  transform: translateY(-50%);
  transition: opacity 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease;
  pointer-events: none;
}

.chat-status.waiting {
  opacity: 1;
  border-color: var(--warn);
  box-shadow: 0 0 12px rgba(255,202,98,0.38);
}

.chat-status.good {
  opacity: 1;
  border-color: var(--good);
  box-shadow: 0 0 12px rgba(98,255,158,0.45);
}

.chat-status.bad {
  opacity: 1;
  border-color: var(--bad);
  box-shadow: 0 0 12px rgba(255,98,114,0.45);
}

.messages {
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding:
    calc(58px + env(safe-area-inset-top))
    14px
    8px
    calc(58px + env(safe-area-inset-left));
  display: block;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: rgba(233,237,243,0.42) rgba(255,255,255,0.06);
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

.messages::-webkit-scrollbar { width: 10px; }
.messages::-webkit-scrollbar-track { background: rgba(255,255,255,0.06); border-radius: 999px; }
.messages::-webkit-scrollbar-thumb { background: rgba(233,237,243,0.36); border-radius: 999px; border: 2px solid rgba(5,6,8,0.85); }
.messages::-webkit-scrollbar-thumb:hover { background: rgba(233,237,243,0.58); }

.messages.overflowing {
  scroll-behavior: auto;
}

.msg {
  display: block;
  width: fit-content;
  max-width: min(82%, 620px);
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(255,255,255,0.075);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.35;
  clear: both;
}

.msg + .msg {
  margin-top: 8px;
}

.msg.me {
  margin-left: auto;
  background: rgba(255,255,255,0.12);
}

.msg.peer {
  margin-right: auto;
}

.send {
  position: relative;
  min-height: 48px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

.send input { font-size: 16px; padding-left: 36px; }
.send button { min-width: 86px; }

@media (max-width: 520px) {
  .joinBox { grid-template-columns: 1fr; }
  .buttons { display: grid; grid-template-columns: 1fr 1fr; }
  button { width: 100%; }
  .app { width: min(94vw, 520px); }
  .password-box { font-size: 12px; }
  .messages {
    padding-left: calc(54px + env(safe-area-inset-left));
    padding-right: 8px;
  }
  .messages::-webkit-scrollbar { width: 7px; }
  .send { grid-template-columns: 1fr 76px; }
  .send button { min-width: 76px; }
}



.privacy-curtain {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background:
    radial-gradient(circle at 50% 50%, rgba(255,255,255,0.035), transparent 20rem),
    #050608;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.08s ease;
}

.privacy-curtain.show {
  opacity: 1;
  pointer-events: auto;
}

@media print {
  body * {
    visibility: hidden !important;
  }
  body::before {
    content: "";
    position: fixed;
    inset: 0;
    visibility: visible !important;
    background: #050608 !important;
  }
}


/* Advanced media branch */
.image-input { display: none; }
.send {
  grid-template-columns: 42px 42px minmax(0, 1fr) auto !important;
  gap: 8px !important;
}
.send input { padding-left: 36px !important; }
.send button { min-width: 76px; }
.mini-btn {
  width: 42px;
  min-width: 42px !important;
  height: 48px;
  padding: 0;
  border-radius: 15px;
  font-size: 24px;
  line-height: 1;
}
.cam-btn { font-size: 22px; padding-bottom: 2px; }
.chat-status { left: 98px !important; }

.msg.image-msg {
  position: relative;
  padding: 6px;
  max-width: min(78%, 520px);
}
.msg.image-msg img {
  display: block;
  max-width: 100%;
  max-height: min(52vh, 460px);
  border-radius: 12px;
  object-fit: contain;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.msg.image-msg.burning::after {
  content: "5s";
  position: absolute;
  right: 10px;
  top: 10px;
  min-width: 28px;
  height: 22px;
  padding: 2px 7px;
  border: 1px solid rgba(255,202,98,0.28);
  border-radius: 999px;
  background: rgba(0,0,0,0.56);
  color: var(--warn);
  font-size: 11px;
  line-height: 16px;
  text-align: center;
}

.image-sheet:not(.hidden) {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(5,6,8,0.72);
  backdrop-filter: blur(14px);
}
.image-card {
  width: min(92vw, 520px);
  border: 1px solid var(--line);
  border-radius: 24px;
  background: linear-gradient(180deg, var(--panel), var(--panel2));
  padding: 12px;
  box-shadow: 0 24px 90px rgba(0,0,0,0.48);
}
.image-card img {
  display: block;
  width: 100%;
  max-height: 62vh;
  border-radius: 16px;
  object-fit: contain;
  background: rgba(0,0,0,0.28);
}
.image-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}
.image-actions button { min-width: 0; }

.call-panel:not(.hidden) {
  position: fixed;
  inset: 0;
  z-index: 30;
  width: 100vw;
  height: 100dvh;
  background: #020304;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  padding:
    calc(10px + env(safe-area-inset-top))
    calc(10px + env(safe-area-inset-right))
    calc(10px + env(safe-area-inset-bottom))
    calc(10px + env(safe-area-inset-left));
  gap: 10px;
}
.video-stack {
  min-height: 0;
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 10px;
}
.video {
  width: 100%;
  height: 100%;
  min-height: 0;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 22px;
  background:
    radial-gradient(circle at 50% 50%, rgba(255,255,255,0.06), transparent 18rem),
    #080a0f;
}
.call-close {
  position: absolute;
  top: calc(14px + env(safe-area-inset-top));
  left: calc(14px + env(safe-area-inset-left));
  z-index: 2;
  width: 42px;
  min-width: 42px !important;
  height: 42px;
  border-radius: 14px;
  padding: 0;
  font-size: 26px;
  line-height: 34px;
  color: rgba(233,237,243,0.8);
  background: rgba(0,0,0,0.36);
}
.call-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.call-controls button { min-width: 0; }

@media (max-width: 560px) {
  .send {
    grid-template-columns: 40px 40px minmax(0, 1fr) 68px !important;
    gap: 6px !important;
  }
  .send button { min-width: 0; }
  .mini-btn {
    width: 40px;
    min-width: 40px !important;
  }
  .chat-status { left: 92px !important; }
  .send input { padding-left: 34px !important; }
  .msg.image-msg { max-width: 88%; }
}




/* Advanced v3 fixes */
.input-wrap {
  position: relative;
  min-width: 0;
}
.input-wrap input {
  width: 100%;
  padding-left: 36px !important;
}
.input-wrap .chat-status {
  position: absolute !important;
  left: 13px !important;
  top: 50% !important;
  right: auto !important;
  z-index: 2;
  width: 14px;
  height: 14px;
  border-radius: 999px;
  border: 3px solid transparent;
  background: transparent;
  opacity: 0;
  box-shadow: 0 0 0 transparent;
  transform: translateY(-50%);
  pointer-events: none;
}
.input-wrap .chat-status.waiting { opacity: 1; border-color: var(--warn); box-shadow: 0 0 12px rgba(255,202,98,0.38); }
.input-wrap .chat-status.good { opacity: 1; border-color: var(--good); box-shadow: 0 0 12px rgba(98,255,158,0.45); }
.input-wrap .chat-status.bad { opacity: 1; border-color: var(--bad); box-shadow: 0 0 12px rgba(255,98,114,0.45); }
.send {
  grid-template-columns: 42px 42px minmax(0, 1fr) auto !important;
  align-items: stretch;
}
.send > input#messageInput { padding-left: 15px !important; }
.image-sheet.busy .image-card::after {
  content: "Processing…";
  display: block;
  margin-top: 8px;
  text-align: center;
  font-size: 12px;
  color: var(--warn);
}
@media (max-width: 560px) {
  .send { grid-template-columns: 40px 40px minmax(0, 1fr) 68px !important; }
  .input-wrap input { padding-left: 34px !important; }
  .input-wrap .chat-status { left: 12px !important; }
}



/* Advanced v4 */
button.host-active {
  color: var(--good);
  border-color: rgba(98,255,158,0.36);
  box-shadow: 0 0 14px rgba(98,255,158,0.16);
}

.call-invite {
  position: fixed;
  inset: 0;
  z-index: 25;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(5,6,8,0.74);
  backdrop-filter: blur(14px);
}

.call-invite-card {
  width: min(92vw, 360px);
  border: 1px solid var(--line);
  border-radius: 24px;
  background: linear-gradient(180deg, var(--panel), var(--panel2));
  padding: 16px;
  box-shadow: 0 24px 90px rgba(0,0,0,0.48);
}

.call-invite-text {
  text-align: center;
  font-weight: 800;
  color: var(--text);
  margin-bottom: 12px;
}

.call-invite-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.call-invite-actions button {
  min-width: 0;
}



/* Advanced v5 media/video/call fixes */
.image-card video {
  display: block;
  width: 100%;
  max-height: 62vh;
  border-radius: 16px;
  object-fit: contain;
  background: rgba(0,0,0,0.28);
}
.msg.image-msg video {
  display: block;
  max-width: 100%;
  max-height: min(52vh, 460px);
  border-radius: 12px;
  object-fit: contain;
  background: rgba(0,0,0,0.28);
}
.msg.system {
  clear: both;
  margin-left: auto;
  margin-right: auto;
  max-width: min(88%, 560px);
  color: var(--warn);
  background: rgba(255,202,98,0.055);
  border-color: rgba(255,202,98,0.18);
  font-size: 13px;
  text-align: center;
}
.call-panel {
  overflow: hidden;
  grid-template-rows: minmax(0, 1fr) 52px;
}
.video-stack {
  height: 100%;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  gap: 12px;
}
.video {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  max-height: 100%;
  overflow: hidden;
  contain: layout paint;
}
.call-controls {
  min-height: 48px;
}
@supports (-webkit-touch-callout: none) {
  .call-panel {
    height: 100svh;
    min-height: 100svh;
  }
}



/* Advanced v9 media stability */
.image-card video:not(.hidden) {
  display: block;
  width: 100%;
  max-height: 62vh;
  border-radius: 16px;
  object-fit: contain;
  background: rgba(0,0,0,0.28);
}

.msg.video-msg {
  min-width: min(78vw, 260px);
}

.msg.video-msg video {
  width: min(78vw, 520px);
  min-height: 140px;
  background: rgba(0,0,0,0.34);
}

button.host-active {
  color: var(--good);
  border-color: rgba(98,255,158,0.36);
  box-shadow: 0 0 14px rgba(98,255,158,0.16);
}



/* Advanced v10 transfer/video stability */
.msg.video-msg {
  min-width: min(78vw, 260px);
}

.msg.video-msg video {
  width: min(78vw, 520px);
  min-height: 140px;
  background: rgba(0,0,0,0.34);
}



/* Advanced v11 Android media picker */
.media-pick-sheet {
  position: fixed;
  inset: 0;
  z-index: 19;
  display: grid;
  place-items: end center;
  padding:
    18px
    calc(18px + env(safe-area-inset-right))
    calc(18px + env(safe-area-inset-bottom))
    calc(18px + env(safe-area-inset-left));
  background: rgba(5,6,8,0.54);
  backdrop-filter: blur(10px);
}

.media-pick-card {
  width: min(92vw, 420px);
  border: 1px solid var(--line);
  border-radius: 22px;
  background: linear-gradient(180deg, var(--panel), var(--panel2));
  padding: 10px;
  box-shadow: 0 24px 90px rgba(0,0,0,0.48);
}

.media-pick-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.media-pick-actions button {
  min-width: 0;
}

@media (max-width: 420px) {
  .media-pick-actions {
    grid-template-columns: 1fr 1fr;
  }
}

/* Must stay last: media overlays should never override this at first load. */
.hidden { display: none !important; }
