:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --border: #2a2f3a;
  --text: #e8eaf0;
  --muted: #8b93a3;
  --accent: #4f8cff;
  --accent-text: #ffffff;
  --error: #ff6b6b;
  font-size: 16px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

body.full-bleed {
  overflow: hidden;
}

/* ---------- Topbar (non-messenger pages) ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.brand {
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  letter-spacing: 0.01em;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.who { color: var(--muted); font-size: 0.9rem; }

.btn-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.9rem;
}
.btn-link:hover { text-decoration: underline; }

.container {
  max-width: 640px;
  margin: 0 auto;
  padding: 2rem 1rem;
}

.app-main {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  top: var(--app-offset-top, 0px);
  height: 100vh;
  height: 100dvh;
  height: var(--app-height, 100dvh);
  padding: 0;
  display: flex;
  flex-direction: column;
}
.app-main .flashes { padding: 0.6rem 1rem 0; }

/* ---------- Shared building blocks ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
}

.auth-card {
  max-width: 360px;
  margin: 3rem auto;
}

h1 { font-size: 1.25rem; margin: 0 0 1.25rem; }

form { display: flex; flex-direction: column; gap: 0.5rem; }

label { font-size: 0.85rem; color: var(--muted); margin-top: 0.5rem; }

input, textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

button {
  margin-top: 0.75rem;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
button:hover { filter: brightness(1.08); }

.switch { margin-top: 1rem; font-size: 0.9rem; color: var(--muted); }
.switch a { color: var(--accent); }

.radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.25rem;
  margin: 0.4rem 0 0.5rem;
}
.radio-option {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
}
.radio-option input { accent-color: var(--accent); }

.flashes { margin-bottom: 1rem; }
.flash {
  padding: 0.6rem 0.8rem;
  border-radius: 8px;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}
.flash-error { background: rgba(255, 107, 107, 0.15); color: var(--error); }
.flash-success { background: rgba(79, 140, 255, 0.15); color: var(--accent); }
.flash-message { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }

.container .flashes {
  max-width: 360px;
  margin-left: auto;
  margin-right: auto;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.3rem;
  height: 1.3rem;
  padding: 0 0.35rem;
  border-radius: 999px;
  background: var(--error);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1;
}
.badge-total { font-size: 0.68rem; }
.badge[hidden] { display: none; }

.avatar {
  width: 2.15rem;
  height: 2.15rem;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.muted { color: var(--muted); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
  margin: 0;
  text-decoration: none;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn-accent { background: var(--accent); color: var(--accent-text); }
.icon-btn-accent:hover { background: var(--accent); filter: brightness(1.1); color: var(--accent-text); }

/* ---------- App shell (Telegram-style two pane) ---------- */

.app-shell {
  display: grid;
  grid-template-columns: 320px 1fr;
  flex: 1;
  min-height: 0;
  background: var(--bg);
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.95rem 1.1rem;
  border-bottom: 1px solid var(--border);
}
.sidebar-title {
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}
.sidebar-actions {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  flex-shrink: 0;
}
.chat-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}
.sidebar-empty { padding: 1rem 0.7rem; font-size: 0.88rem; }

.chat-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.6rem;
  border-radius: 10px;
  text-decoration: none;
  color: var(--text);
}
.chat-row:hover { background: var(--surface-2); }
.chat-row-active { background: var(--surface-2); }

.chat-row-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
.chat-row-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
}
.chat-row-name {
  font-weight: 600;
  font-size: 0.92rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-row-time { font-size: 0.7rem; color: var(--muted); flex-shrink: 0; }
.chat-row-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.chat-row-preview {
  font-size: 0.82rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.main-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.95rem;
}

.settings-panel {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  justify-content: center;
  padding: 2.5rem 1.5rem;
}
.settings-form { width: 100%; max-width: 420px; }
.settings-save { margin-top: 1.3rem; }

.field-hint {
  margin: 0.3rem 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
}

.color-picker-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.3rem;
}
.color-picker-row input[type="color"] {
  width: 3rem;
  height: 2.4rem;
  padding: 0.2rem;
  cursor: pointer;
}
.color-picker-value {
  font-size: 0.85rem;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.icon-btn-active { background: var(--surface-2); color: var(--text); }

.main-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.7rem;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  min-height: 3.5rem;
}
.main-panel-header-start {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  min-width: 0;
  flex: 1;
}
.main-panel-header-end {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  flex-shrink: 0;
}
.main-panel-header h1 { margin: 0; font-size: 1.05rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back-btn { display: inline-flex; }

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.msg {
  position: relative;
  max-width: 75%;
  padding: 0.55rem 0.8rem;
  border-radius: 14px;
  font-size: 0.95rem;
  line-height: 1.35;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.msg-theirs {
  align-self: flex-start;
  background: var(--surface-2);
  border-bottom-left-radius: 4px;
}
.msg-mine {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 4px;
  /* Own bubbles have a custom long-press menu — suppress the native
     text-selection/callout popup so it doesn't race our JS timer. */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}
.msg-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.25rem;
  margin-top: 0.2rem;
}
.msg-time {
  font-size: 0.7rem;
  opacity: 0.65;
}
.msg-status {
  display: inline-flex;
  opacity: 0.6;
}
.msg-status-read { opacity: 1; }

.msg-image-wrap {
  position: relative;
  display: block;
  min-width: 220px;
  min-height: 180px;
  max-width: 100%;
  max-height: 320px;
  background: var(--surface-2);
  border-radius: 10px;
  overflow: hidden;
}
.msg-image {
  display: block;
  max-width: 100%;
  max-height: 320px;
  border-radius: 10px;
  object-fit: cover;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
  cursor: zoom-in;
}
.msg-countdown {
  position: absolute;
  bottom: 0.4rem;
  right: 0.4rem;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  font-size: 0.68rem;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  font-variant-numeric: tabular-nums;
}

.msg-image-wrap.msg-image-blurred { cursor: pointer; }
.msg-image-wrap.msg-image-blurred .msg-image {
  filter: blur(24px) brightness(0.7);
  cursor: pointer;
}
.msg-image-wrap.msg-image-blurred::after {
  content: attr(data-reveal-label);
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 0 1rem;
  color: #fff;
  font-weight: 600;
  font-size: 0.85rem;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.7);
  pointer-events: none;
}

.msg-body { margin-top: 0.3rem; }
.msg-image-wrap + .msg-time,
.msg-image-wrap ~ .msg-time { margin-top: 0.3rem; }

.msg-removed-label {
  display: block;
  font-size: 0.85rem;
  font-style: italic;
  opacity: 0.75;
}

/* ---------- Image lightbox ---------- */

.image-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  align-items: center;
  justify-content: center;
  padding: 2rem;
  z-index: 1000;
  cursor: zoom-out;
}
.image-modal[hidden] { display: none; }
.image-modal:not([hidden]) { display: flex; }

.image-modal img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  cursor: default;
}

.image-modal-close {
  position: fixed;
  top: 1rem;
  right: 1.25rem;
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  cursor: pointer;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.image-modal-close:hover { background: rgba(255, 255, 255, 0.22); }

/* ---------- Context menu ---------- */

.context-menu {
  position: fixed;
  z-index: 1100;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  padding: 0.3rem;
  min-width: 170px;
}
.context-menu[hidden] { display: none; }

.context-menu-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  background: none;
  border: none;
  color: var(--error);
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  margin: 0;
  text-align: left;
}
.context-menu-item:hover { background: rgba(255, 107, 107, 0.12); }

/* ---------- Confirm modal ---------- */

.confirm-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  align-items: center;
  justify-content: center;
  z-index: 1200;
  padding: 1rem;
}
.confirm-modal[hidden] { display: none; }
.confirm-modal:not([hidden]) { display: flex; }

.confirm-modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.4rem;
  max-width: 360px;
  width: 100%;
}
.confirm-modal-box h2 { margin: 0 0 0.5rem; font-size: 1.05rem; }
.confirm-modal-box p { margin: 0 0 1.1rem; color: var(--muted); font-size: 0.88rem; line-height: 1.4; }
.confirm-modal-actions { display: flex; justify-content: flex-end; gap: 0.6rem; }

.btn-secondary {
  margin: 0;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 0.5rem 0.95rem;
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
}
.btn-secondary:hover { background: var(--border); filter: none; }

.btn-danger {
  margin: 0;
  background: var(--error);
  color: #fff;
  border: none;
  padding: 0.5rem 0.95rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary {
  margin: 0;
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  padding: 0.5rem 0.95rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary:hover { filter: brightness(1.08); }

#image-options-modal .confirm-modal-box { max-width: 420px; }
.option-group { margin-bottom: 1.1rem; }
.option-group-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.4rem;
}
.option-group .radio-group { margin: 0; }

/* ---------- Composer ---------- */

.send-form {
  flex-direction: row;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.8rem 1.1rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
  margin: 0;
}
.send-form textarea {
  flex: 1;
  resize: none;
  height: 2.6rem;
  max-height: 8rem;
}
.send-form .icon-btn { margin-top: 0; }

.ephemeral-note {
  margin: 0;
  padding: 0 1.25rem 0.75rem;
  font-size: 0.72rem;
  color: var(--muted);
  text-align: center;
  background: var(--surface);
}

/* ---------- Mobile: list-or-chat, Telegram style ---------- */

@media (max-width: 768px) {
  .app-shell { grid-template-columns: 1fr; }
  .app-shell-chat-open .sidebar { display: none; }
  .app-shell:not(.app-shell-chat-open) .main-panel { display: none; }
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #f0f1f4;
    --border: #e2e4e9;
    --text: #1a1d24;
    --muted: #6b7280;
  }
}
