* { box-sizing: border-box }

/* HTML `hidden` attribute should always win, even against display:flex
 * rules elsewhere. (Browser default specificity is zero — any class
 * override would otherwise unhide the element by accident.) */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  background: #1a1d22;
  color: #e8eaed;
  overflow: hidden;     /* off-screen siblings (translateX) must not extend scroll area */
}

button {
  font: inherit;
  padding: .55rem 1.1rem;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  background: #3a3f45;
  color: #e8eaed;
}
button.primary   { background: #3a6ea5; color: white }
button.primary:hover   { background: #4685c7 }
button.secondary { background: #3a3f45 }
button.secondary:hover { background: #4a4f55 }
button.danger    { background: #b03030; color: white }
button.iconbtn   { background: transparent; padding: .3rem .55rem; font-size: 1.25rem }

input[type=text] {
  font: inherit; padding: .55rem .7rem;
  background: #1a1d22; color: #e8eaed;
  border: 1px solid #3a3f45; border-radius: 8px;
  width: 100%;
}
input[type=text]:focus { outline: none; border-color: #4685c7 }

/* ============================== screens ============================== */

.screen {
  position: absolute; inset: 0;
  display: none;
  flex-direction: column;
  background: #1a1d22;
  overflow: hidden;
}
.screen.active { display: flex; }

.topbar {
  display: flex; align-items: center; gap: .5rem;
  padding: .6rem 1rem; background: #24272d;
  /* Clear the status bar (clock / signal icons) at the top. */
  padding-top: calc(.6rem + env(safe-area-inset-top));
  border-bottom: 1px solid #2f343a;
}
.topbar .title {
  flex: 1; font-weight: 600; font-size: 1.05rem;
  display: flex; align-items: baseline; gap: .5rem; min-width: 0;
}
.topbar .title .muted { font-weight: normal; font-size: .8rem }
#my-nickname { white-space: nowrap; overflow: hidden; text-overflow: ellipsis }
.muted-line {
  padding: .25rem 1rem; background: #20232a; color: #8a8f95;
  font-size: .8rem;
}
.muted { color: #8a8f95; font-size: .85rem }

.fatal {
  background: #5a2020; border-bottom: 1px solid #a44;
  padding: .8rem 1rem; color: #ffe0e0; font-weight: 500;
}

/* ============================== contacts ============================== */

.contacts {
  list-style: none; padding: 0; margin: 0;
  flex: 1; overflow-y: auto;
}
.contact {
  display: flex; align-items: center; gap: .75rem;
  padding: .85rem 1rem;
  border-bottom: 1px solid #20232a;
  cursor: pointer; user-select: none;
}
.contact:hover { background: #20232a }
.contact .avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: #3a6ea5; color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: .9rem; flex: 0 0 auto;
}
.contact .avatar.off { background: #3a3f45; color: #8a8f95 }
.contact .name { flex: 1; font-weight: 500 }
.contact .id   { color: #6a6f75; font-size: .8rem; font-family: ui-monospace, monospace; }
.contact .dot  { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto }

.dot-on   { background: #3c3; box-shadow: 0 0 5px #3c3 }
.dot-off  { background: #555 }
.dot-warn { background: #c93; box-shadow: 0 0 5px #c93 }

.empty {
  padding: 2rem 1rem; color: #8a8f95; font-size: .9rem; text-align: center;
}

.bottombar {
  display: flex; gap: .5rem; padding: .75rem 1rem;
  /* Keep clear of the Android/iOS bottom navigation bar. */
  padding-bottom: calc(.75rem + env(safe-area-inset-bottom));
  background: #24272d; border-top: 1px solid #2f343a;
}
.bottombar button { flex: 1 }

/* ============================== call screen ============================== */

#screen-call .video-stage {
  position: relative; flex: 1;
  background: #000;
  min-height: 280px;
}
.peer-video {
  position: absolute; inset: 0;
  width: 100%; height: 100%; object-fit: contain;
  background: #000;
}
.my-video {
  position: absolute; right: 12px; bottom: 12px;
  width: 30%; max-width: 180px; aspect-ratio: 3/4;
  border-radius: 8px; border: 2px solid #24272d;
  object-fit: cover; background: #111;
  z-index: 2;
}
.stats-toggle {
  position: absolute; top: 12px; right: 12px; z-index: 3;
  width: 32px; height: 32px; padding: 0;
  border-radius: 50%; background: rgba(0,0,0,.6); color: #fff;
  font-size: 1.1rem; line-height: 30px;
  cursor: pointer; border: 0;
}
.stats-overlay {
  position: absolute; top: 12px; left: 12px; z-index: 3;
  background: rgba(0,0,0,.7); color: #ddd;
  font-family: ui-monospace, monospace; font-size: .78rem;
  padding: .5rem .7rem; border-radius: 6px;
  white-space: pre; pointer-events: none;
}
#screen-call .controls {
  display: flex; gap: .5rem; padding: .75rem 1rem; flex-wrap: wrap;
  background: #24272d; border-top: 1px solid #2f343a;
}
#screen-call .controls button { flex: 1; min-width: 90px }
.resolution-select {
  background: #1a1d22; color: #e8eaed;
  border: 1px solid #3a3f45; border-radius: 8px;
  padding: .55rem .8rem; font: inherit; cursor: pointer;
  min-width: 80px;
}

.chat-wrap {
  display: flex; flex-direction: column;
  max-height: 260px; min-height: 100px;
  background: #1a1d22;
  border-top: 1px solid #2f343a;
}

/* Without an active call the video stage is pointless — hide it and let the
 * chat grow to fill the whole screen as the conversation gets longer. During
 * a call the video reclaims the space and the chat shrinks to a bottom panel. */
#screen-call:not(.calling) .video-stage { display: none; }
#screen-call:not(.calling) .chat-wrap { flex: 1; max-height: none; }
#chat {
  flex: 1; overflow-y: auto;
  padding: .5rem .75rem;
  display: flex; flex-direction: column; gap: .25rem;
}
.msg {
  max-width: 75%; padding: .35rem .6rem;
  border-radius: 12px; font-size: .9rem; word-wrap: break-word;
  display: inline-flex; align-items: flex-end; gap: .35rem;
}
.msg.me   { background: #3a6ea5; align-self: flex-end }
.msg.them { background: #3a3f45; align-self: flex-start }
.msg-status {
  font-size: .7rem; color: rgba(255,255,255,.6);
  flex: 0 0 auto;
}
.msg { cursor: default; }
.msg.editing { outline: 2px solid #c93; outline-offset: 1px; }
.msg-edited { font-size: .7rem; color: rgba(255,255,255,.45); }

/* The text must stay user-selectable (WebView often disables selection
 * globally) so links can be selected/copied natively. */
.msg-text { -webkit-user-select: text; user-select: text; }

/* Per-message actions handle (⋮). */
.msg-actions {
  background: transparent; border: 0; padding: 0 .1rem; margin: 0;
  color: rgba(255,255,255,.45); font-size: 1.05rem; line-height: 1;
  cursor: pointer; flex: 0 0 auto; align-self: center;
  -webkit-user-select: none; user-select: none;
}
.msg-actions:hover { color: #fff }

.edit-banner {
  display: flex; align-items: center; justify-content: space-between;
  gap: .5rem; padding: .35rem .75rem;
  background: #2c2a1c; color: #e8d8a0;
  border-top: 1px solid #4a4630; font-size: .85rem;
}

.msg-menu {
  position: fixed; z-index: 40;
  background: #2f343a; border: 1px solid #444a52;
  border-radius: 10px; padding: .25rem;
  box-shadow: 0 8px 30px rgba(0,0,0,.55);
  display: flex; flex-direction: column; min-width: 150px;
}
.msg-menu-item {
  background: transparent; text-align: left;
  padding: .55rem .8rem; border-radius: 6px; font-size: .9rem;
}
.msg-menu-item:hover { background: #3a4048 }
.msg-menu-item.danger { color: #ff8a8a }

.chat-input {
  display: flex; gap: .5rem; padding: .5rem .75rem;
  /* Keep the text field above the Android/iOS bottom navigation bar. */
  padding-bottom: calc(.5rem + env(safe-area-inset-bottom));
  border-top: 1px solid #2f343a; align-items: center;
}
.chat-input input[type=text] { flex: 1 }
.chat-input button { flex: 0 0 auto }
#attach-btn { padding: .55rem .8rem }

.msg-file {
  display: flex; flex-direction: column; gap: .25rem;
  min-width: 180px; max-width: 260px;
}
.msg-file img.thumb {
  max-width: 100%; max-height: 200px; border-radius: 6px;
  cursor: pointer; display: block;
}
.msg-file audio, .msg-file video {
  width: 100%; max-height: 200px; border-radius: 6px;
}
.msg-file .filemeta {
  font-size: .75rem; opacity: .8;
  display: flex; align-items: center; gap: .35rem;
}
.msg-file .filemeta a {
  color: inherit; text-decoration: underline;
}

.msg-link { color: #9cc4ff; text-decoration: underline; word-break: break-all; }

/* ============================== link previews ============================== */

.preview-card {
  display: block; text-decoration: none; color: inherit;
  max-width: 75%; margin: -.05rem 0 .2rem;
  background: #20232a; border: 1px solid #2f343a;
  border-left: 3px solid #3a6ea5;
  border-radius: 10px; overflow: hidden;
}
.preview-card.me   { align-self: flex-end }
.preview-card.them { align-self: flex-start }
.preview-card:hover { background: #24272d }
.preview-img {
  display: block; width: 100%; max-height: 180px;
  object-fit: cover; background: #15171b;
}
.preview-body { padding: .45rem .6rem; display: flex; flex-direction: column; gap: .15rem }
.preview-site  { font-size: .7rem; color: #8a8f95; text-transform: uppercase; letter-spacing: .03em }
.preview-title { font-size: .9rem; font-weight: 600; line-height: 1.25;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden }
.preview-desc  { font-size: .8rem; color: #b8bdc3; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden }

/* ============================== dialogs ============================== */

.dialog {
  position: fixed; inset: 0; z-index: 10;
  background: rgba(0,0,0,.6);
  display: none; align-items: center; justify-content: center;
  padding: 1rem;
}
.dialog:not([hidden]) { display: flex; }
.dialog-box {
  background: #24272d; border-radius: 12px;
  padding: 1.25rem; max-width: 420px; width: 100%;
  display: flex; flex-direction: column; gap: .5rem;
  box-shadow: 0 10px 40px rgba(0,0,0,.5);
}
.dialog-title { font-size: 1.1rem; font-weight: 600; margin-bottom: .25rem }
.dialog-text  { color: #b8bdc3; font-size: .9rem }
.dialog-box label { font-size: .8rem; color: #8a8f95; margin-top: .25rem }
.dialog-error { background: #5a2020; color: #ffe0e0; padding: .5rem .75rem; border-radius: 6px; font-size: .85rem }
.dialog-buttons {
  display: flex; gap: .5rem; justify-content: flex-end; margin-top: .75rem;
}

/* ============================== toasts ============================== */

.lightbox {
  position: fixed; inset: 0; z-index: 30;
  background: rgba(0, 0, 0, .95);
  display: none; align-items: center; justify-content: center;
}
.lightbox:not([hidden]) { display: flex; }
.lightbox img {
  max-width: 100vw; max-height: 100vh;
  object-fit: contain; user-select: none;
  cursor: pointer;
}
.lightbox-close {
  position: absolute; top: max(env(safe-area-inset-top), 12px); right: 12px;
  width: 40px; height: 40px; padding: 0; z-index: 31;
  border-radius: 50%; background: rgba(0, 0, 0, .6);
  color: #fff; font-size: 1.4rem; line-height: 38px; cursor: pointer; border: 0;
}

.toasts {
  position: fixed; bottom: 1rem; right: 1rem; z-index: 20;
  display: flex; flex-direction: column; gap: .5rem;
}
.toast {
  background: #2f343a; color: #e8eaed;
  padding: .6rem 1rem; border-radius: 8px;
  border-left: 3px solid #3a6ea5;
  font-size: .9rem; min-width: 200px; max-width: 360px;
  animation: toast-in .2s ease-out;
  pointer-events: auto; cursor: pointer;
}
.toast.error   { border-left-color: #b03030 }
.toast.success { border-left-color: #3a9a3a }
@keyframes toast-in {
  from { transform: translateX(20px); opacity: 0 }
  to   { transform: translateX(0);    opacity: 1 }
}

@media (max-width: 600px) {
  .my-video { width: 35%; max-width: 140px }
  .contact .id { display: none }
}
