:root {
  --bg: #0e1621;
  --surface: #17212b;
  --elev: #1e2c3a;
  --border: #2b3a4b;
  --text: #e7edf4;
  --muted: #8b9bab;
  --accent: #5288c1;
  --accent-soft: rgba(82, 136, 193, 0.18);
  --ok: #6bcf7f;
  --warn: #e6b35c;
  --danger: #e07070;
  font-family: system-ui, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

a {
  color: var(--accent);
}

#app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  max-width: 720px;
  margin: 0 auto;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: max(0.65rem, env(safe-area-inset-top)) 1rem 0.65rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}

.topbar-title {
  flex: 1;
  min-width: 0;
}

.topbar h1 {
  font-size: 1.05rem;
  font-weight: 600;
  margin: 0;
  min-width: 0;
  letter-spacing: 0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-subtitle {
  font-size: 0.74rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-top: 0.1rem;
}

.server-indicator {
  font-size: 0.74rem;
  padding: 0.25rem 0.5rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--elev);
  white-space: nowrap;
}

.server-indicator.online {
  color: var(--ok);
}

.server-indicator.offline {
  color: var(--danger);
}

.presence-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  background: #7f8c9a;
  flex-shrink: 0;
}

.presence-dot.online {
  background: var(--ok);
  box-shadow: 0 0 0 1px rgba(107, 207, 127, 0.5);
}

.presence-dot.offline {
  background: #7f8c9a;
}

.icon-btn {
  background: var(--elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.45rem 0.65rem;
  cursor: pointer;
}

.icon-btn:active {
  transform: scale(0.98);
}

main {
  flex: 1;
  padding: 1rem calc(1rem + env(safe-area-inset-right)) 1rem calc(1rem + env(safe-area-inset-left));
  overflow-y: auto;
  min-height: 0;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.1rem;
  margin-bottom: 1rem;
}

label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
  margin-top: 0.75rem;
}

label:first-of-type {
  margin-top: 0;
}

label input[type="checkbox"] {
  margin-right: 0.4rem;
  width: auto;
  cursor: pointer;
}

input[type="text"],
input[type="password"],
textarea,
select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.75rem;
  outline: none;
  color: var(--text);
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke='%238b9bab' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 16px;
  padding-right: 2.5rem;
  cursor: pointer;
}

select option {
  background: var(--surface);
  color: var(--text);
}

textarea {
  min-height: 120px;
  resize: vertical;
}

h2 {
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0 0 0.75rem 0;
  color: var(--text);
}

ol {
  margin: 0.5rem 0;
  padding-left: 1.5rem;
  font-size: 0.9rem;
  line-height: 1.6;
}

ol li {
  margin: 0.35rem 0;
}

ol li strong {
  font-weight: 600;
  color: var(--accent);
}

.row {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-top: 1.2rem;
}

.row:first-of-type {
  margin-top: 0;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 0.65rem 1rem;
  cursor: pointer;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
}

.btn.secondary {
  background: var(--elev);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.phrase-box {
  font-family: ui-monospace, "Cascadia Code", monospace;
  font-size: 0.95rem;
  line-height: 1.5;
  background: var(--bg);
  border-radius: 10px;
  padding: 0.85rem;
  border: 1px dashed var(--warn);
  color: var(--warn);
  word-break: break-word;
}

.warn-banner {
  background: rgba(230, 179, 92, 0.12);
  border: 1px solid rgba(230, 179, 92, 0.35);
  color: var(--warn);
  padding: 0.75rem;
  border-radius: 10px;
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.list {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.list-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  background: var(--elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  text-align: left;
}

.list-item:active {
  filter: brightness(1.05);
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-weight: 700;
  flex-shrink: 0;
}

.list-meta {
  flex: 1;
  min-width: 0;
}

.list-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.list-title {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.list-badge {
  min-width: 1.35rem;
  height: 1.35rem;
  padding: 0 0.35rem;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.72rem;
  display: inline-grid;
  place-items: center;
  font-weight: 700;
}

.list-item.has-unread {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px rgba(82, 136, 193, 0.18);
}

.list-sub {
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-bar {
  margin-bottom: 1rem;
}

.chat-feed {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding-bottom: 5rem;
}

.bubble {
  max-width: 75%;
  padding: 0.6rem 0.8rem;
  border-radius: 14px;
  font-size: 0.95rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  user-select: none;
  -webkit-user-select: none;
}

.bubble.me {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.bubble.queued {
  opacity: 0.88;
  border: 1px dashed rgba(255, 255, 255, 0.35);
}

.bubble.they {
  align-self: flex-start;
  background: var(--elev);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.bubble-text {
  white-space: pre-wrap;
  word-break: break-word;
}

.bubble-media {
  margin-top: 0.35rem;
}

.bubble-img {
  max-width: min(100%, 320px);
  max-height: 280px;
  border-radius: 10px;
  display: block;
  vertical-align: middle;
}

.bubble-file {
  color: inherit;
  font-weight: 600;
  text-decoration: underline;
}

.bubble-meta {
  font-size: 0.7rem;
  opacity: 0.75;
  margin-top: 0.25rem;
  text-align: right;
}

.bubble-date {
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
  margin: 1rem 0 0.5rem 0;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.bubble.selected {
  background: var(--accent-soft);
  border: 2px solid var(--accent);
}

.bubble-context-menu {
  position: fixed;
  transform: translateY(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  z-index: 1000;
  min-width: 140px;
  padding: 4px 0;
}

.context-item {
  display: block;
  width: 100%;
  padding: 0.65rem 1rem;
  background: none;
  border: none;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-size: 0.95rem;
  transition: background 0.15s;
}

.context-item:hover:not(:disabled) {
  background: var(--elev);
}

.context-item:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.context-item.danger {
  color: var(--danger);
}

.selection-toolbar {
  display: none;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

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

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.compose {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 0.65rem calc(1rem + env(safe-area-inset-right)) calc(0.65rem + env(safe-area-inset-bottom)) calc(1rem + env(safe-area-inset-left));
  background: linear-gradient(180deg, transparent, var(--bg) 30%);
  max-width: 720px;
  margin: 0 auto;
}

.compose-inner {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.45rem;
}

.compose-inner textarea {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  border: none;
  background: transparent;
  padding: 0.45rem;
}

.compose .btn {
  flex-shrink: 0;
  align-self: flex-end;
}

.error {
  color: var(--danger);
  font-size: 0.9rem;
  margin-top: 0.75rem;
}

.muted {
  color: var(--muted);
  font-size: 0.88rem;
  margin-top: 0.75rem;
}

.empty-state {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--muted);
}

.bubble-delete {
  margin-left: 0.5rem;
  padding: 0.2rem 0.45rem;
  background: var(--danger);
  border: none;
  border-radius: 4px;
  color: white;
  cursor: pointer;
  font-size: 0.85rem;
  opacity: 0.7;
  transition: opacity 0.2s;
}

.bubble:hover .bubble-delete {
  opacity: 1;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}

.modal-content {
  max-width: 90vw;
  max-height: 90vh;
  position: relative;
}

.modal-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1;
}

.modal-close:hover {
  background: rgba(255, 255, 255, 0.3);
}

.modal-image {
  max-width: 90vw;
  max-height: 80vh;
  width: auto;
  height: auto;
}

.modal-buttons {
  position: absolute;
  bottom: 1rem;
  left: 1rem;
  right: 1rem;
  display: flex;
  gap: 0.5rem;
}

.modal-download {
  padding: 0.5rem 1rem;
  background: var(--accent);
  border: none;
  border-radius: 4px;
  color: white;
  cursor: pointer;
  font-size: 0.9rem;
}

.modal-download:hover {
  background: #4778b1;
}

@media (min-width: 721px) {
  .compose {
    left: calc(50% - 360px);
    right: calc(50% - 360px);
  }
}

/* Menu styles */
.settings-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  z-index: 100;
  min-width: 220px;
  display: none;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  margin-top: 0.35rem;
}

.settings-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.7rem 1rem;
  border: none;
  background: none;
  color: inherit;
  cursor: pointer;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
  transition: background 0.2s;
}

.settings-menu-item:hover {
  background: var(--bg);
}

.settings-menu-item:last-child {
  border-bottom: none;
}

.settings-menu-item.danger {
  color: var(--danger);
}

.settings-menu-item.danger:hover {
  background: rgba(224, 112, 112, 0.1);
}

.settings-menu-section {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.settings-menu-section:last-child {
  border-bottom: none;
}

.settings-menu-section label {
  margin-bottom: 0.5rem;
  display: block;
}

.settings-menu-section select {
  width: 100%;
  margin-top: 0.35rem;
}

.toast {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  color: var(--text);
  padding: 1rem 1.5rem;
  border-radius: 8px;
  border-left: 4px solid var(--accent);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  z-index: 2000;
  max-width: calc(100% - 2rem);
  animation: slideUp 0.3s ease-out;
}

.toast.error {
  border-left-color: var(--danger);
}

.toast.success {
  border-left-color: var(--ok);
}

@keyframes slideUp {
  from {
    transform: translateX(-50%) translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
  }
}

