:root {
  --bg: #0f1115;
  --panel: #161922;
  --panel-2: #1d212c;
  --line: #272c38;
  --text: #e6e8ee;
  --muted: #8b91a0;
  --accent: #6ea8fe;
  --user: #2b4d7a;
  --assistant: #20242f;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; height: 100vh; height: 100dvh; }

/* Back button — chat header, mobile only (see media query below). */
.back-btn {
  display: none;
  width: 34px; height: 34px; margin-right: 4px; flex: 0 0 auto;
  align-items: center; justify-content: center;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel-2); color: var(--text);
  font-size: 22px; line-height: 1; cursor: pointer;
}
.back-btn:hover { border-color: var(--accent); color: var(--accent); }

/* Sidebar */
#sidebar {
  width: 280px;
  flex: 0 0 280px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 18px 12px;
}
.sidebar-head h1 { font-size: 18px; margin: 0; letter-spacing: .2px; }
#new-contact {
  width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--text); font-size: 18px; cursor: pointer;
}
#new-contact:hover { border-color: var(--accent); color: var(--accent); }

#contact-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }

/* Usage readout (sidebar footer) */
#usage {
  border-top: 1px solid var(--line);
  padding: 12px 16px;
  font-size: 12px;
  background: var(--panel);
}
.usage-row { display: flex; align-items: baseline; justify-content: space-between; }
.usage-label { color: var(--muted); }
.usage-cost { font-variant-numeric: tabular-nums; font-weight: 600; }
.usage-row.total .usage-cost { color: var(--accent); font-size: 15px; }
.usage-sub {
  display: flex; justify-content: space-between;
  color: var(--muted); font-size: 11px; margin-top: 2px; font-variant-numeric: tabular-nums;
}
.usage-row.convo { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line); }
.usage-row.convo .usage-cost { font-size: 13px; }
.contact {
  padding: 11px 12px; border-radius: 10px; cursor: pointer;
  display: flex; flex-direction: column; gap: 2px;
}
.contact:hover { background: var(--panel-2); }
.contact.active { background: var(--panel-2); outline: 1px solid var(--line); }
.contact .name { font-weight: 600; }
.contact .badge {
  font-size: 11px; color: var(--muted);
}
.contact .badge.onboarding { color: var(--accent); }

/* Main */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* Contact hub (conversation list + new-conversation box) */
#contact-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#contact-view[hidden] { display: none; }
.contact-view-head {
  display: flex; align-items: center; gap: 8px;
  padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.contact-view-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
#new-convo {
  display: flex; gap: 10px; padding: 16px 20px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
#new-input {
  flex: 1; resize: none; max-height: 160px; padding: 11px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); font: inherit;
}
#new-input:focus { outline: none; border-color: var(--accent); }
#new-convo button {
  border: none; background: var(--accent); color: #0b1220; font-weight: 600;
  padding: 0 20px; border-radius: 12px; cursor: pointer;
}
#conversation-list {
  list-style: none; margin: 0; padding: 14px 16px; overflow-y: auto; flex: 1;
  display: flex; flex-direction: column; gap: 8px;
}
.convo {
  padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel-2); cursor: pointer; display: flex; flex-direction: column; gap: 4px;
}
.convo:hover { border-color: var(--accent); }
.convo-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convo-meta { font-size: 12px; color: var(--muted); }
.convo-empty { color: var(--muted); padding: 8px 4px; }

/* The chat-header back arrow returns to the hub; visible on every breakpoint. */
#back-btn { display: inline-flex; }

#chat-head {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--panel);
}
#chat-head[hidden] { display: none; }
.chat-head-info { display: flex; flex-direction: column; }
#chat-name { font-weight: 600; font-size: 16px; }
#chat-sub { font-size: 12px; color: var(--muted); }

#messages { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; padding: 24px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 68%; padding: 11px 14px; border-radius: var(--radius); line-height: 1.45; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--user); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--assistant); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.typing { color: var(--muted); font-style: italic; }
.msg.error { align-self: stretch; max-width: 100%; text-align: center; background: #fdecec; color: #9b1c1c; border: 1px solid #f5c2c2; font-size: 0.9em; }

/* Rendered Markdown (assistant turns). Block elements handle layout, so the
   pre-wrap whitespace from .msg is reset here. */
.msg.md { white-space: normal; }
.msg.md > :first-child { margin-top: 0; }
.msg.md > :last-child { margin-bottom: 0; }
.msg.md p { margin: 0 0 0.6em; }
.msg.md h1, .msg.md h2, .msg.md h3, .msg.md h4, .msg.md h5, .msg.md h6 { margin: 0.6em 0 0.3em; line-height: 1.25; }
.msg.md h1 { font-size: 1.3em; } .msg.md h2 { font-size: 1.2em; } .msg.md h3 { font-size: 1.1em; }
.msg.md h4, .msg.md h5, .msg.md h6 { font-size: 1em; }
.msg.md ul, .msg.md ol { margin: 0 0 0.6em; padding-left: 1.4em; }
.msg.md li { margin: 0.15em 0; }
.msg.md a { color: inherit; text-decoration: underline; }
.msg.md code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.88em; background: rgba(0, 0, 0, 0.06); padding: 0.1em 0.35em; border-radius: 4px; }
.msg.md pre { margin: 0 0 0.6em; background: rgba(0, 0, 0, 0.06); padding: 10px 12px; border-radius: 8px; overflow-x: auto; }
.msg.md pre code { background: none; padding: 0; font-size: 0.85em; }
.msg.md blockquote { margin: 0 0 0.6em; padding-left: 0.8em; border-left: 3px solid var(--line); color: var(--muted); }
.msg.md hr { border: none; border-top: 1px solid var(--line); margin: 0.6em 0; }
.msg.md strong { font-weight: 650; }
.dots span { animation: blink 1.2s infinite both; }
.dots span:nth-child(2) { animation-delay: .2s; }
.dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }

#composer { display: flex; gap: 10px; padding: 16px 20px; border-top: 1px solid var(--line); background: var(--panel); }
#input {
  flex: 1; resize: none; max-height: 160px; padding: 11px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); font: inherit;
}
#input:focus { outline: none; border-color: var(--accent); }
#send { border: none; background: var(--accent); color: #0b1220; font-weight: 600; padding: 0 20px; border-radius: 12px; cursor: pointer; }
#send:disabled { opacity: .5; cursor: default; }

#empty-state { margin: auto; color: var(--muted); padding: 24px; text-align: center; }

/* ---- Mobile: single-pane navigation ----
   Below 720px the two panes stack into one viewport. The sidebar is the base
   "contacts" screen; #main is absolutely positioned on top and slid off-screen
   until a conversation is open (#app.show-chat), giving a list → chat → back
   flow driven by the .show-chat class (see app.js). */
@media (max-width: 720px) {
  #app { position: relative; overflow: hidden; }

  #sidebar {
    width: 100%; flex: 1 1 auto; border-right: none;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .sidebar-head { padding-top: max(18px, env(safe-area-inset-top)); }

  #main {
    position: absolute; inset: 0; z-index: 2;
    background: var(--bg);
    transform: translateX(100%);
    transition: transform .25s ease;
  }
  #app.show-chat #main { transform: translateX(0); }

  .back-btn { display: inline-flex; }

  #chat-head { padding: 12px 14px; padding-top: max(12px, env(safe-area-inset-top)); }
  .contact-view-head { padding: 12px 14px; padding-top: max(12px, env(safe-area-inset-top)); }
  #new-convo { padding: 12px 14px; }
  #new-input { font-size: 16px; }
  #conversation-list { padding: 12px 14px; }
  #messages { padding: 16px 14px; }
  .msg { max-width: 85%; }
  .msg.error { max-width: 100%; }

  #composer { padding: 12px 14px; padding-bottom: max(12px, env(safe-area-inset-bottom)); gap: 8px; }
  /* 16px keeps iOS Safari from auto-zooming the viewport on focus. */
  #input { font-size: 16px; }
  #send { padding: 0 18px; }
}

/* ---- Login gate ---- */
#login {
  display: flex; align-items: center; justify-content: center;
  height: 100vh; height: 100dvh; padding: 24px;
}
#login-form {
  width: 100%; max-width: 320px;
  display: flex; flex-direction: column; gap: 12px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 28px 24px;
}
#login-form h1 { margin: 0 0 8px; font-size: 20px; text-align: center; }
#login-form input {
  width: 100%; padding: 12px 14px; font-size: 16px;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; outline: none;
}
#login-form input:focus { border-color: var(--accent); }
#login-form button {
  padding: 12px 14px; font-size: 15px; cursor: pointer;
  background: var(--accent); color: #0f1115; border: none;
  border-radius: 10px; font-weight: 600;
}
#login-form button:hover { filter: brightness(1.06); }
#login-error { margin: 0; color: #ff8b8b; font-size: 13px; text-align: center; }

/* Sign-out (sidebar footer) */
#logout-btn {
  margin-top: 10px; width: 100%; padding: 8px 10px; font-size: 13px; cursor: pointer;
  background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--line); border-radius: 8px;
}
#logout-btn:hover { border-color: var(--accent); color: var(--accent); }

/* Export / import controls (sidebar footer) */
.data-actions { display: flex; gap: 8px; margin-top: 10px; }
.data-actions button {
  flex: 1; padding: 8px 10px; font-size: 13px; cursor: pointer;
  background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--line); border-radius: 8px;
}
.data-actions button:hover { border-color: var(--accent); color: var(--accent); }

/* Password modal (export/import) */
#pw-modal {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.55);
}
#pw-modal[hidden] { display: none; }
.pw-box {
  width: min(380px, 90vw); padding: 20px; border-radius: 14px;
  background: var(--panel); border: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 10px;
}
.pw-box h3 { margin: 0; font-size: 16px; }
.pw-hint { margin: 0; font-size: 12px; color: var(--muted); line-height: 1.4; }
.pw-error { margin: 0; font-size: 12px; color: #ff8080; }
#pw-input {
  padding: 10px 12px; border-radius: 10px; font: inherit;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
}
#pw-input:focus { outline: none; border-color: var(--accent); }
.pw-actions { display: flex; gap: 8px; justify-content: flex-end; }
.pw-actions button {
  padding: 8px 16px; font-size: 13px; cursor: pointer; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
}
#pw-ok { background: var(--accent); color: #0b1220; font-weight: 600; border-color: var(--accent); }
.pw-actions button:hover { border-color: var(--accent); }
