:root {
  --ink: #14171c;
  --panel: #1b1f26;
  --paper: #ece7dc;
  --muted: #7c8494;
  --seal: #b8863d;
  --seal-dim: rgba(184, 134, 61, 0.16);
  --line: #262b33;
  --danger: #c15b4a;
  --font-display: 'Lora', Georgia, serif;
  --font-body: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, Menlo, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

/* Several elements below (.auth-screen, .app-shell, .conversation-empty,
   .conversation-active) set an unconditional `display` value. Any author
   rule with a display value outranks the browser's built-in `[hidden] {
   display: none }` default, so JS toggling the `hidden` attribute alone
   would do nothing. This one rule guarantees `hidden` always wins. */
[hidden] {
  display: none !important;
}
body {
  margin: 0;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; }
ul { list-style: none; margin: 0; padding: 0; }

/* ---------- Storage warning banner ---------- */
.storage-warning {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: var(--danger);
  color: var(--ink);
  padding: 0.65rem 1.25rem;
  font-size: 0.85rem;
  line-height: 1.4;
}
.storage-warning p { margin: 0; }
.storage-warning-dismiss {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--ink);
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.2rem;
}
.storage-warning-dismiss:hover { opacity: 0.7; }

/* ---------- Auth screen ---------- */
.auth-screen {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1.1fr 1fr;
}
@media (max-width: 860px) {
  .auth-screen { grid-template-columns: 1fr; }
  .auth-hero { min-height: 34vh; padding: 3rem 1.5rem; }
  .auth-form-wrap { padding: 3rem 1.5rem; }
}

.auth-hero {
  padding: 4rem 3rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 1.25rem;
  border-right: 1px solid var(--line);
}
.wordmark {
  font-family: var(--font-display);
  font-style: italic;
  font-size: 2.25rem;
  margin: 0;
}
.wordmark.small { font-size: 1.2rem; }
.hero-line {
  font-family: var(--font-display);
  font-size: 1.45rem;
  line-height: 1.4;
  max-width: 30ch;
  margin: 0;
}
.hero-detail {
  color: var(--muted);
  max-width: 44ch;
  line-height: 1.6;
  margin: 0;
}

.auth-form-wrap {
  background: var(--panel);
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 4rem 3rem;
}
.auth-tabs {
  display: flex;
  gap: 1.5rem;
  margin-bottom: 2rem;
  border-bottom: 1px solid var(--line);
}
.auth-tab {
  background: none;
  border: none;
  color: var(--muted);
  padding: 0 0 0.75rem 0;
  font-size: 0.95rem;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.auth-tab.active { color: var(--paper); border-bottom-color: var(--seal); }

.auth-form { display: flex; flex-direction: column; gap: 1.25rem; max-width: 340px; }
.field { display: flex; flex-direction: column; gap: 0.4rem; font-size: 0.85rem; color: var(--muted); }
.field input {
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--line);
  color: var(--paper);
  font-size: 1rem;
  padding: 0.5rem 0.1rem;
  font-family: var(--font-body);
}
.field input:focus { outline: none; border-bottom-color: var(--seal); }

.btn-primary {
  background: var(--seal);
  color: var(--ink);
  border: none;
  padding: 0.7rem 1.4rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  align-self: flex-start;
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-primary:disabled { opacity: 0.6; cursor: default; }

.btn-primary:focus-visible,
.link-btn:focus-visible,
.auth-tab:focus-visible,
.contact-item:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--seal);
  outline-offset: 2px;
}

.form-error { color: var(--danger); font-size: 0.85rem; margin: 0; }
.form-error.small { padding: 0 1.25rem; }

/* ---------- App shell ---------- */
.app-shell {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 300px 1fr;
  grid-template-rows: 1fr;
}
@media (max-width: 720px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar,
  .conversation {
    grid-column: 1;
    grid-row: 1;
  }
  .app-shell .conversation { display: none; }
  .app-shell.show-conversation .sidebar { display: none; }
  .app-shell.show-conversation .conversation { display: flex; }
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 0;
  min-height: 0;
}
.identity-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 0 1.25rem;
}
.whoami {
  padding: 0.5rem 1.25rem 1rem 1.25rem;
  color: var(--muted);
  font-size: 0.85rem;
  border-bottom: 1px solid var(--line);
}
.whoami strong { color: var(--paper); font-weight: 600; }

.link-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 0;
}
.link-btn:hover { color: var(--paper); }
.backup-actions {
  display: flex;
  gap: 1.25rem;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--line);
}
.link-btn.danger {
  color: var(--danger);
  opacity: 0.75;
  margin: 0;
  padding: 0.9rem 1.25rem;
  text-align: left;
  border-top: 1px solid var(--line);
}
.link-btn.danger:hover { opacity: 1; }
.link-btn.tiny { font-size: 0.75rem; margin-top: 0.2rem; }

.new-convo-mode-tabs {
  display: flex;
  gap: 0.4rem;
  padding: 1rem 1.25rem 0 1.25rem;
}
.mode-tab {
  background: none;
  border: 1px solid var(--line);
  color: var(--muted);
  padding: 0.3rem 0.75rem;
  font-size: 0.75rem;
  border-radius: 999px;
  cursor: pointer;
}
.mode-tab.active { color: var(--ink); background: var(--seal); border-color: var(--seal); }

.new-convo { padding: 1rem 1.25rem 0.5rem 1.25rem; display: flex; flex-direction: column; gap: 0.5rem; }
.group-name-input {
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--line);
  color: var(--paper);
  font-size: 0.9rem;
  padding: 0.5rem 0.1rem;
  font-family: var(--font-body);
}
.group-name-input:focus { outline: none; border-bottom-color: var(--seal); }
.chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.chip {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--seal-dim);
  border: 1px solid rgba(184, 134, 61, 0.35);
  color: var(--paper);
  border-radius: 999px;
  padding: 0.2rem 0.4rem 0.2rem 0.65rem;
  font-size: 0.8rem;
}
.chip-remove {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1;
  padding: 0 0.15rem;
}
.chip-remove:hover { color: var(--danger); }
.new-convo-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.search-field {
  position: relative;
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  transition: border-color 0.12s ease, background 0.12s ease;
}
.search-field:focus-within {
  border-color: var(--seal);
  background: rgba(255, 255, 255, 0.09);
}
.search-icon {
  position: absolute;
  left: 0.65rem;
  color: var(--muted);
  pointer-events: none;
}
.search-field input {
  width: 100%;
  background: transparent;
  border: none;
  color: var(--paper);
  padding: 0.6rem 0.75rem 0.6rem 2.1rem;
  font-size: 0.9rem;
  font-family: var(--font-body);
}
.search-field input::placeholder { color: var(--muted); }
.search-field input:focus { outline: none; }

.contact-list { flex: 1; margin-top: 0.25rem; overflow-y: auto; min-height: 0; }
.contact-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.7rem 1.25rem;
  cursor: pointer;
  border-left: 2px solid transparent;
  transition: background 0.12s ease;
}
.contact-item:hover { background: rgba(255, 255, 255, 0.03); }
.contact-item.active { border-left-color: var(--seal); background: var(--seal-dim); }
.contact-avatar {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--seal-dim);
  color: var(--seal);
  border: 1px solid rgba(184, 134, 61, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-size: 0.95rem;
  text-transform: uppercase;
}
.contact-item.active .contact-avatar { background: var(--seal); color: var(--ink); }
.contact-row { display: flex; align-items: center; gap: 0.75rem; overflow: hidden; }
.contact-name-cell { display: flex; flex-direction: column; gap: 0.15rem; overflow: hidden; }
.contact-name-cell .name { font-size: 0.92rem; }
.contact-name-cell .preview {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}
.unread-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--seal); flex-shrink: 0; }

/* ---------- Conversation ---------- */
.conversation { display: flex; flex-direction: column; min-height: 0; }
.conversation-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  color: var(--muted);
  padding: 2rem;
  text-align: center;
}
.conversation-empty .wordmark { color: var(--muted); opacity: 0.6; }
.conversation-active { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.conversation-header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 0.85rem;
}
.back-btn { display: none; flex-shrink: 0; font-size: 1.3rem; line-height: 1; }
@media (max-width: 720px) {
  .back-btn { display: inline-flex; align-items: center; }
}
.contact-name { margin: 0; font-family: var(--font-display); font-size: 1.15rem; }
.group-meta {
  margin: 0.15rem 0 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  max-width: 46ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.header-actions { display: flex; gap: 1rem; margin-top: 0.2rem; }

.fingerprint-panel {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--line);
  background: rgba(184, 134, 61, 0.06);
  font-size: 0.85rem;
  color: var(--muted);
}
.fingerprint-value {
  font-family: var(--font-mono);
  color: var(--paper);
  font-size: 0.85rem;
  margin: 0.6rem 0 0 0;
  white-space: pre-wrap;
}

.message-log {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  min-height: 0;
}
.message {
  max-width: 60%;
  padding: 0.6rem 0.85rem;
  font-size: 0.92rem;
  line-height: 1.45;
  border: 1px solid var(--line);
  white-space: pre-wrap;
  word-break: break-word;
}
@media (max-width: 720px) {
  .message { max-width: 82%; }
}
.message.mine { align-self: flex-end; background: var(--seal-dim); border-color: rgba(184, 134, 61, 0.35); }
.message.theirs { align-self: flex-start; background: var(--panel); }
.message .meta { display: block; font-size: 0.7rem; color: var(--muted); margin-top: 0.35rem; }
.message .sender {
  display: block;
  font-size: 0.72rem;
  color: var(--seal);
  font-weight: 600;
  margin-bottom: 0.15rem;
}

.fingerprint-list { display: flex; flex-direction: column; gap: 0.75rem; margin-top: 0.75rem; }
.fingerprint-row .fingerprint-value { margin: 0.3rem 0 0 0; }

.composer { display: flex; align-items: flex-end; gap: 0.6rem; padding: 1rem 1.5rem; border-top: 1px solid var(--line); }
.composer textarea {
  flex: 1;
  resize: none;
  background: var(--ink);
  border: 1px solid var(--line);
  color: var(--paper);
  padding: 0.6rem 0.75rem;
  font-family: var(--font-body);
  font-size: 0.92rem;
  max-height: 140px;
}
.composer textarea:focus { outline: none; border-color: var(--seal); }

.icon-btn {
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--line);
  color: var(--muted);
  width: 38px;
  height: 38px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.icon-btn:hover { color: var(--paper); border-color: var(--seal); }
.icon-btn:disabled { opacity: 0.5; cursor: default; }

.attachment-preview {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin: 0 1.5rem;
  padding: 0.5rem 0.75rem;
  background: var(--seal-dim);
  border: 1px solid rgba(184, 134, 61, 0.35);
  border-radius: 8px;
  font-size: 0.82rem;
}
.attachment-preview-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* attachments rendered inside a message bubble */
.attachment { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: 0.4rem; }
.attachment-media {
  display: block;
  max-width: min(320px, 60vw);
  max-height: 320px;
  border-radius: 4px;
  background: var(--ink);
}
.attachment-status { margin: 0; font-size: 0.8rem; color: var(--muted); }
.attachment-download {
  align-self: flex-start;
  font-size: 0.72rem;
  color: var(--seal);
  text-decoration: none;
}
.attachment-download:hover { text-decoration: underline; }
@media (max-width: 720px) {
  .attachment-media { max-width: 78vw; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
