:root {
  /* Light (default — matches munk.me production) */
  --bg: #f8f9fa;
  --bg2: #ffffff;
  --border: #ced4da;
  --text: #212529;
  --muted: #6c757d;
  --accent: #0d6efd;
  --accent-dim: #e8f0fe;
  --accent-border: rgba(13,110,253,0.25);
  --danger: #dc3545;
  --success: #198754;
  --radius: 6px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", "Liberation Sans", Arial, sans-serif;
  --mono: 'SF Mono', 'Fira Code', Consolas, monospace;
}

:root[data-theme="dark"] {
  --bg: #0d0d0d;
  --bg2: #141414;
  --border: #2a2a2a;
  --text: #e8e8e8;
  --muted: #888;
  --accent: #5b9cf6;
  --accent-dim: #1a2a4a;
  --accent-border: rgba(91,156,246,0.25);
  --danger: #f25f5c;
  --success: #4caf7d;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ─── Layout ─────────────────────────────────────────────────── */

.page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 2rem;
}

.card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: 100%;
  max-width: 420px;
}

.card h1 {
  font-size: 1.25rem;
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.card .subtitle {
  color: var(--muted);
  font-size: 0.875rem;
  margin-bottom: 1.5rem;
}

/* ─── Logo ───────────────────────────────────────────────────── */

.logo-wrap {
  text-align: center;
  margin-bottom: 1.75rem;
}

.logo-wrap img {
  height: 32px;
}

/* ─── Form elements ──────────────────────────────────────────── */

.field { margin-bottom: 1rem; }

.field label {
  display: block;
  font-size: 0.8125rem;
  color: var(--muted);
  margin-bottom: 0.375rem;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9375rem;
  padding: 0.6rem 0.75rem;
  outline: none;
  font-family: var(--font);
  transition: border-color 0.15s, box-shadow 0.15s;
}

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

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9375rem;
  font-weight: 500;
  padding: 0.5rem 0.75rem;
  width: 100%;
  font-family: var(--font);
  transition: opacity 0.15s, box-shadow 0.15s;
}

button:hover, .btn:hover { opacity: 0.88; }
button:disabled, .btn:disabled { opacity: 0.4; cursor: not-allowed; }

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

.btn-danger {
  background: var(--danger);
}

.icon-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: inherit;
  font-family: inherit;
  text-decoration: none;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.icon-btn:hover { background: var(--border); color: var(--text); text-decoration: none; }
.icon-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ─── Status / alerts ────────────────────────────────────────── */

.alert {
  border-radius: var(--radius);
  font-size: 0.875rem;
  padding: 0.625rem 0.75rem;
  margin-bottom: 1rem;
  display: none;
}

.alert.visible { display: block; }
.alert-error   { background: rgba(220,53,69,0.08); border: 1px solid rgba(220,53,69,0.3); color: var(--danger); }
.alert-success { background: rgba(25,135,84,0.08); border: 1px solid rgba(25,135,84,0.3); color: var(--success); }
.alert-info    { background: var(--accent-dim); border: 1px solid var(--accent-border); color: var(--accent); }

/* ─── Recovery phrase display ────────────────────────────────── */

.phrase-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
  margin: 1rem 0;
}

.phrase-word {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--mono);
  font-size: 0.8125rem;
  padding: 0.375rem 0.5rem;
  display: flex;
  gap: 0.375rem;
}

.phrase-word .idx { color: var(--muted); min-width: 1.5rem; }

/* ─── QR code display ────────────────────────────────────────── */

.qr-container {
  background: #fff;
  border-radius: var(--radius);
  display: inline-flex;
  padding: 1rem;
  margin: 1rem 0;
}

.qr-container canvas { display: block; }

/* ─── App shell ──────────────────────────────────────────────── */

#app {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100vh;
  overflow: hidden;
}

#sidebar {
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

#sidebar-header {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.app-wordmark {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text);
}

#sidebar-footer {
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 0.5rem;
}

#convo-list { flex: 1; overflow-y: auto; }

.convo-item {
  padding: 0.75rem 1rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.875rem;
}

.convo-item:hover, .convo-item.active { background: var(--accent-dim); }

.convo-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.convo-remove {
  opacity: 0;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 1rem;
  padding: 0;
  width: auto;
  line-height: 1;
  flex-shrink: 0;
  transition: opacity 0.1s, color 0.1s;
}
.convo-item:hover .convo-remove { opacity: 1; }
.convo-remove:hover { color: var(--danger); opacity: 1; }

.convo-item .unread {
  background: var(--accent);
  border-radius: 10px;
  color: #fff;
  font-size: 0.75rem;
  padding: 0.125rem 0.4rem;
}

.convo-section-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  padding: 0.75rem 1rem 0.25rem;
}

.message .sender-name {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 0.15rem;
}

#chat {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#chat-header {
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  padding: 1rem;
  font-size: 0.9375rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#chat-title { flex: 1; }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.message {
  max-width: 70%;
  border-radius: var(--radius);
  font-size: 0.875rem;
  padding: 0.5rem 0.75rem;
  line-height: 1.5;
}

.message.mine {
  align-self: flex-end;
  background: var(--accent-dim);
  border: 1px solid var(--accent-border);
}

.message.theirs {
  align-self: flex-start;
  background: var(--bg2);
  border: 1px solid var(--border);
}

.message .meta {
  color: var(--muted);
  font-size: 0.6875rem;
  margin-top: 0.25rem;
}

.message .delete-btn {
  opacity: 0;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--muted);
  font-size: 0.75rem;
  padding: 0;
  margin-left: 0.4rem;
  line-height: 1;
  vertical-align: middle;
  transition: opacity 0.1s, color 0.1s;
}
.message:hover .delete-btn { opacity: 1; }
.message .delete-btn:hover { color: var(--danger); }

#typing-indicator {
  color: var(--muted);
  font-size: 0.8125rem;
  font-style: italic;
  padding: 0.25rem 1rem;
  min-height: 1.5rem;
}

#compose {
  border-top: 1px solid var(--border);
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
}

#compose input {
  flex: 1;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9375rem;
  padding: 0.5rem 0.75rem;
  outline: none;
  font-family: var(--font);
  transition: border-color 0.15s, box-shadow 0.15s;
}

#compose input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-border);
}

#compose button {
  width: 40px;
  height: 40px;
  padding: 0;
  flex-shrink: 0;
}

/* ─── Theme toggle button icon (CSS mask, no innerHTML needed) ── */

#btn-theme::before {
  content: '';
  display: block;
  width: 16px;
  height: 16px;
  background: currentColor;
  mask-repeat: no-repeat;
  mask-position: center;
  mask-size: contain;
  -webkit-mask-repeat: no-repeat;
  -webkit-mask-position: center;
  -webkit-mask-size: contain;
}

/* Light mode (default) → show moon icon to switch to dark */
:root:not([data-theme="dark"]) #btn-theme::before {
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z'/%3E%3C/svg%3E");
  -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z'/%3E%3C/svg%3E");
}

/* Dark mode → show sun icon to switch to light */
:root[data-theme="dark"] #btn-theme::before {
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='5'/%3E%3Cline x1='12' y1='1' x2='12' y2='3'/%3E%3Cline x1='12' y1='21' x2='12' y2='23'/%3E%3Cline x1='4.22' y1='4.22' x2='5.64' y2='5.64'/%3E%3Cline x1='18.36' y1='18.36' x2='19.78' y2='19.78'/%3E%3Cline x1='1' y1='12' x2='3' y2='12'/%3E%3Cline x1='21' y1='12' x2='23' y2='12'/%3E%3Cline x1='4.22' y1='19.78' x2='5.64' y2='18.36'/%3E%3Cline x1='18.36' y1='5.64' x2='19.78' y2='4.22'/%3E%3C/svg%3E");
  -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='5'/%3E%3Cline x1='12' y1='1' x2='12' y2='3'/%3E%3Cline x1='12' y1='21' x2='12' y2='23'/%3E%3Cline x1='4.22' y1='4.22' x2='5.64' y2='5.64'/%3E%3Cline x1='18.36' y1='18.36' x2='19.78' y2='19.78'/%3E%3Cline x1='1' y1='12' x2='3' y2='12'/%3E%3Cline x1='21' y1='12' x2='23' y2='12'/%3E%3Cline x1='4.22' y1='19.78' x2='5.64' y2='18.36'/%3E%3Cline x1='18.36' y1='5.64' x2='19.78' y2='4.22'/%3E%3C/svg%3E");
}

/* ─── Misc ───────────────────────────────────────────────────── */

.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.25rem 0;
}

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

.mono { font-family: var(--mono); }

@media (max-width: 600px) {
  #app { grid-template-columns: 1fr; }
  #sidebar { display: none; }
  #sidebar.open { display: flex; position: fixed; inset: 0; z-index: 50; }
}
