:root {
  --bg: #0b141a;
  --bg-panel: #111b21;
  --bg-elevated: #202c33;
  --bg-input: #2a3942;
  --bg-hover: #182229;
  --bubble-mine: #005c4b;
  --bubble-theirs: #202c33;
  --text: #e9edef;
  --text-muted: #8696a0;
  --text-dim: #aebac1;
  --accent: #00a884;
  --accent-2: #25d366;
  --danger: #f15c6d;
  --border: rgba(134, 150, 160, 0.15);
  --shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
  --radius: 8px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button { font: inherit; color: inherit; }
button, a, input, textarea, select, [role="button"], [role="slider"] {
  touch-action: manipulation;
}

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* ---------- Login ---------- */
#login-screen {
  align-items: center;
  justify-content: center;
  padding-top: env(safe-area-inset-top);
  background:
    radial-gradient(1200px 600px at 50% -200px, rgba(0,168,132,0.18), transparent 60%),
    var(--bg);
}
.login-card {
  width: min(360px, 92vw);
  background: var(--bg-panel);
  border-radius: 14px;
  padding: 28px 26px 24px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  gap: 14px;
  border: 1px solid var(--border);
}
.login-logo {
  width: 56px; height: 56px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--accent), #008f72);
  color: #fff;
  font-weight: 700;
  letter-spacing: 1px;
  align-self: center;
}
.login-card h1 {
  margin: 0;
  text-align: center;
  font-size: 22px;
  font-weight: 600;
}
.login-sub {
  margin: -6px 0 6px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}
.login-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.login-card label span {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.login-card input {
  background: var(--bg-input);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 11px 12px;
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button[type="submit"] {
  margin-top: 8px;
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 11px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.login-card button[type="submit"]:hover { background: #00bd96; }
.login-card button[type="submit"]:disabled { opacity: 0.6; cursor: not-allowed; }
.login-error {
  background: rgba(241, 92, 109, 0.12);
  color: var(--danger);
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 14px;
  text-align: center;
}

/* ---------- Chat ---------- */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.peer-avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #5e9bff, #6362ef);
  display: grid; place-items: center;
  font-weight: 600;
  color: #fff;
  text-transform: uppercase;
}
.peer-info { flex: 1; min-width: 0; }
.peer-name { font-weight: 500; }
.peer-status { font-size: 12px; color: var(--text-muted); }
.peer-status.online { color: var(--accent-2); }
.header-actions { display: flex; gap: 4px; }

.icon-btn {
  width: 40px; height: 40px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  border-radius: 50%;
  cursor: pointer;
  display: grid; place-items: center;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
.icon-btn:hover { background: rgba(255,255,255,0.05); color: var(--text); }
.icon-btn:active { transform: scale(0.94); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 6vw 8px;
  background:
    radial-gradient(circle at 10% -10%, rgba(0,168,132,0.06), transparent 40%),
    radial-gradient(circle at 90% 110%, rgba(0,168,132,0.06), transparent 40%),
    var(--bg);
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}
.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.08); border-radius: 8px; }

.empty-state {
  margin: auto;
  color: var(--text-muted);
  text-align: center;
}

.day-divider {
  align-self: center;
  background: var(--bg-elevated);
  color: var(--text-muted);
  padding: 4px 12px;
  border-radius: 10px;
  font-size: 12px;
  margin: 10px 0 6px;
  box-shadow: var(--shadow);
}

.bubble {
  max-width: min(540px, 75%);
  padding: 10px 10px 8px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  position: relative;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.bubble.mine {
  align-self: flex-end;
  background: var(--bubble-mine);
  border-bottom-right-radius: 2px;
}
.bubble.theirs {
  align-self: flex-start;
  background: var(--bubble-theirs);
  border-bottom-left-radius: 2px;
}
.bubble.tail-mine { margin-bottom: 8px; }
.bubble.tail-theirs { margin-bottom: 8px; }
.bubble + .bubble.mine { margin-top: 2px; }
.bubble + .bubble.theirs { margin-top: 2px; }

.bubble .text { white-space: pre-wrap; }
.msg-link {
  color: #53bdeb;
  text-decoration: underline;
  word-break: break-all;
}
.bubble.mine .msg-link { color: #a4d3ff; }

.link-preview {
  display: block;
  margin: 6px 0 2px;
  border-radius: 6px;
  overflow: hidden;
  background: rgba(0, 0, 0, 0.22);
  text-decoration: none;
  color: var(--text);
  max-width: 320px;
  border-left: 3px solid var(--accent);
  transition: background 0.15s;
}
.link-preview:hover { background: rgba(0, 0, 0, 0.32); }
.link-preview-img {
  display: block;
  width: 100%;
  max-height: 180px;
  object-fit: cover;
  background: rgba(0, 0, 0, 0.25);
}
.link-preview-body {
  padding: 7px 10px 9px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.link-preview-site {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.link-preview-title {
  font-weight: 600;
  font-size: 14px;
  line-height: 1.3;
  color: var(--text);
}
.link-preview-desc {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.bubble .meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
  margin-left: 8px;
  user-select: none;
}
.bubble.mine .meta { color: rgba(233, 237, 239, 0.7); }

/* ---------- Image bubble ---------- */
.bubble.image { padding: 4px; }
.bubble.image .img-wrap {
  border-radius: 6px;
  overflow: hidden;
  position: relative;
  background: rgba(0,0,0,0.2);
  min-width: 160px;
  min-height: 90px;
}
.bubble.image img {
  display: block;
  max-width: 100%;
  max-height: 360px;
  width: auto; height: auto;
  cursor: zoom-in;
}
.bubble.image .meta {
  position: absolute;
  bottom: 8px; right: 10px;
  background: rgba(0,0,0,0.45);
  padding: 2px 6px;
  border-radius: 8px;
  color: #fff;
  margin: 0;
}
.bubble.image.with-caption { padding: 4px 4px 6px; }
.bubble.image.with-caption .text { padding: 6px 6px 0; }
.bubble.image.with-caption .meta {
  position: static;
  background: transparent;
  padding: 0;
  color: var(--text-muted);
  margin-top: 2px;
  margin-right: 4px;
}

/* ---------- Voice note bubble ---------- */
.vn {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  min-width: 240px;
  padding: 2px 0;
}
.vn-play {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.08);
  color: var(--text);
  display: grid; place-items: center;
  cursor: pointer;
  flex-shrink: 0;
}
.bubble.mine .vn-play { background: rgba(255,255,255,0.12); }
.vn-play:hover { background: rgba(255,255,255,0.18); }
.vn-play svg { width: 18px; height: 18px; }

.vn-track {
  position: relative;
  height: 32px;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  display: flex;
  align-items: center;
}
.vn-wave {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  gap: 2px;
  pointer-events: none;
}
.vn-bar {
  flex: 1;
  background: var(--text-muted);
  border-radius: 1px;
  min-height: 3px;
  transition: background 0.05s linear;
}
.bubble.mine .vn-bar { background: rgba(255,255,255,0.45); }
.vn-bar.played { background: var(--text); }
.bubble.mine .vn-bar.played { background: #fff; }

.vn-handle {
  position: absolute;
  width: 14px; height: 14px;
  background: var(--text);
  border-radius: 50%;
  top: 50%;
  left: 0;
  transform: translate(-50%, -50%);
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
  opacity: 0;
  transition: opacity 0.15s;
}
.bubble.mine .vn-handle { background: #fff; }
.vn-track:hover .vn-handle,
.vn.playing .vn-handle,
.vn.scrubbing .vn-handle { opacity: 1; }

.vn-meta {
  display: flex;
  align-items: center;
  gap: 6px;
}
.vn-time {
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  min-width: 34px;
  text-align: right;
}
.bubble.mine .vn-time { color: rgba(255,255,255,0.7); }
.vn-speed {
  background: rgba(255,255,255,0.08);
  border: none;
  color: var(--text-dim);
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  font-variant-numeric: tabular-nums;
}
.bubble.mine .vn-speed { background: rgba(255,255,255,0.12); color: rgba(255,255,255,0.85); }
.vn-speed.active { background: var(--accent); color: #fff; }
.vn-speed:hover { color: var(--text); }

.vn-bubble-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: flex-end;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}
.bubble.mine .vn-bubble-meta { color: rgba(233, 237, 239, 0.7); }
.vn-played-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  border: 1.5px solid rgba(255,255,255,0.55);
  background: transparent;
  display: inline-block;
  transition: background 0.2s, border-color 0.2s;
}
.vn-played-dot.played {
  background: #53bdeb;
  border-color: #53bdeb;
}

/* ---------- Composer ---------- */
.composer {
  background: var(--bg-elevated);
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
}
.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}
.composer-input-wrap {
  flex: 1;
  background: var(--bg-input);
  border-radius: 22px;
  padding: 7px 14px;
  min-height: 40px;
  display: flex;
  align-items: center;
}
#text-input {
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  width: 100%;
  resize: none;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.4;
  max-height: 140px;
  overflow-y: auto;
}
#text-input { line-height: 1; caret-color: var(--accent); }
#text-input.has-text { line-height: 1.4; }
#text-input::placeholder { color: var(--text-muted); }

.composer-btn {
  width: 44px; height: 44px;
  background: transparent;
  flex-shrink: 0;
}
.send-btn {
  background: var(--accent);
  color: #fff;
}
.send-btn:hover { background: #00bd96; color: #fff; }
.send-btn svg { transform: translateX(1px); }

/* ---------- Recorder bar ---------- */
.recorder-bar {
  display: flex;
  align-items: center;
  gap: 8px;
}
.recorder-meter {
  flex: 1;
  background: var(--bg-input);
  border-radius: 22px;
  padding: 6px 14px;
  height: 44px;
  display: flex;
  align-items: center;
  gap: 10px;
  overflow: hidden;
}
.recorder-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--danger);
  flex-shrink: 0;
  animation: pulse 1.4s ease-in-out infinite;
}
.recorder-dot.paused { animation: none; background: var(--text-muted); }
@keyframes pulse {
  0%,100% { opacity: 1; transform: scale(1); }
  50%     { opacity: 0.4; transform: scale(0.8); }
}
.recorder-time {
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  color: var(--text);
  min-width: 42px;
}
.recorder-wave {
  flex: 1;
  height: 36px;
  display: block;
}
.recorder-cancel { color: var(--danger); }
.recorder-cancel:hover { background: rgba(241, 92, 109, 0.12); color: var(--danger); }
.recorder-pause, .recorder-resume { color: var(--text); }

/* ---------- Typing ---------- */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 4px 18px 8px;
  background: var(--bg);
}
.typing-indicator span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typing-bounce 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ---------- Drag-and-drop overlay ---------- */
.drop-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 168, 132, 0.18);
  backdrop-filter: blur(2px);
  display: grid;
  place-items: center;
  z-index: 90;
  pointer-events: none;
  animation: drop-fade 0.15s ease-out;
}
@keyframes drop-fade { from { opacity: 0; } to { opacity: 1; } }
.drop-overlay-card {
  background: var(--bg-elevated);
  border: 2px dashed var(--accent);
  border-radius: 14px;
  padding: 28px 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-weight: 500;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.drop-overlay-card svg { color: var(--accent); }

/* ---------- Update banner ---------- */
.update-banner {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 200;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 500;
}
.update-banner button {
  background: rgba(255,255,255,0.2);
  border: 1px solid rgba(255,255,255,0.5);
  color: #fff;
  border-radius: 6px;
  padding: 4px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.update-banner button:active { background: rgba(255,255,255,0.35); }

/* ---------- Image viewer ---------- */
.image-viewer {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  display: grid;
  place-items: center;
  z-index: 100;
  overflow: hidden;
}
.image-viewer img {
  max-width: 95vw;
  max-height: 92vh;
  object-fit: contain;
  box-shadow: 0 20px 60px rgba(0,0,0,0.7);
  border-radius: 4px;
  touch-action: none;
  will-change: transform;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}
.image-viewer img.zoomed { cursor: grabbing; }
.image-viewer-close {
  position: absolute;
  top: 16px; right: 20px;
  width: 44px; height: 44px;
  background: transparent;
  border: none;
  color: #fff;
  font-size: 32px;
  cursor: pointer;
  border-radius: 50%;
  line-height: 1;
}
.image-viewer-close:hover { background: rgba(255,255,255,0.1); }

/* ---------- Toasts ---------- */
.toast {
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elevated);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
  z-index: 50;
  border: 1px solid var(--border);
  font-size: 14px;
  animation: toast-in 0.2s ease-out;
}
@keyframes toast-in {
  from { transform: translate(-50%, 12px); opacity: 0; }
  to   { transform: translate(-50%, 0);    opacity: 1; }
}

/* ---------- Pending / sending state ---------- */
.bubble.pending { opacity: 0.72; }
.bubble.failed  { opacity: 1; }

.pending-status {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 5px;
  margin-top: 3px;
}

@keyframes spin { to { transform: rotate(360deg); } }
.send-spinner {
  display: inline-block;
  width: 11px; height: 11px;
  border: 2px solid rgba(255,255,255,0.25);
  border-top-color: rgba(255,255,255,0.85);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
  flex-shrink: 0;
}
.send-fail-icon { color: var(--danger); font-size: 13px; }
.send-retry-btn {
  background: var(--danger);
  color: #fff;
  border: none;
  padding: 2px 9px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.send-retry-btn:hover { background: #d94d5d; }

@media (max-width: 640px) {
  .messages { padding-left: 12px; padding-right: 12px; }
  .bubble { max-width: 86%; }
}
