:root {
  --navy: #0b1f3a;
  --navy-2: #12294b;
  --accent: #c8a45c;      /* warm gold — APMC-ish */
  --bg: #f4f6f9;
  --panel: #ffffff;
  --line: #e3e8ef;
  --text: #1a2433;
  --muted: #6b7890;
  --bubble-in: #eef1f6;
  --bubble-out: #0b1f3a;
}

* { 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);
  overflow-x: hidden;            /* backstop: the page must never scroll sideways */
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }

/* ---------- Login ---------- */
.login-screen {
  height: 100%; min-height: 100dvh;
  display: flex; align-items: center; justify-content: center; padding: 16px;
  background: linear-gradient(160deg, var(--navy), var(--navy-2));
}
.login-card {
  width: 100%; max-width: 340px; background: var(--panel); border-radius: 16px; padding: 32px 28px;
  display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 24px 60px rgba(0,0,0,.35);
}
.brand { font-size: 20px; font-weight: 700; color: var(--navy); }
.brand-sub { font-size: 12px; color: var(--muted); margin-bottom: 10px; letter-spacing: .02em; }
.login-card input {
  padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; font-size: 16px;
}
.login-card input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.login-card button {
  margin-top: 6px; padding: 12px; border: 0; border-radius: 10px; cursor: pointer;
  background: var(--navy); color: #fff; font-size: 15px; font-weight: 600;
}
.login-card button:hover { background: var(--navy-2); }
.error { color: #c0392b; font-size: 13px; min-height: 16px; }

/* ---------- App layout ---------- */
.app { height: 100%; min-height: 100dvh; display: grid; grid-template-columns: 320px 1fr; overflow: hidden; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px; border-bottom: 1px solid var(--line);
}
.sidebar-title { font-weight: 700; color: var(--navy); }
.sidebar-sub { font-size: 12px; color: var(--muted); }
.head-actions { display: flex; align-items: center; gap: 8px; }
.ghost-btn { background: none; border: 1px solid var(--line); color: var(--muted); border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 12px; }
.ghost-btn:hover { color: var(--text); border-color: var(--muted); }
#settings-btn { font-size: 15px; line-height: 1; padding: 6px 9px; }

/* View tabs */
.view-tabs { display: flex; gap: 4px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.tab { flex: 1; background: none; border: 0; padding: 8px; border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600; color: var(--muted); }
.tab:hover { background: #f3f6fb; }
.tab.active { background: var(--navy); color: #fff; }

/* Archive button in thread header */
.archive-btn { margin-left: auto; flex: 0 0 auto; border: 1px solid var(--line); background: #fff; color: var(--muted); border-radius: 8px; padding: 7px 12px; font-size: 12px; font-weight: 600; cursor: pointer; }
.archive-btn:hover { color: var(--text); border-color: var(--muted); }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(11,31,58,.45); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-card { width: 340px; max-width: calc(100vw - 32px); background: #fff; border-radius: 16px; padding: 24px; box-shadow: 0 24px 60px rgba(0,0,0,.3); }
.modal-title { font-size: 18px; font-weight: 700; color: var(--navy); margin-bottom: 14px; }
.field { display: block; font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.field input { display: block; width: 100%; margin-top: 5px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px; font-size: 16px; }
.field input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.modal-actions button:not(.ghost-btn) { border: 0; border-radius: 9px; padding: 9px 18px; background: var(--navy); color: #fff; font-weight: 600; cursor: pointer; }
.modal-actions button:not(.ghost-btn):hover { background: var(--navy-2); }

.conv-list { overflow-y: auto; flex: 1; min-height: 0; }
.empty { padding: 24px 16px; color: var(--muted); font-size: 14px; }
.conv {
  padding: 14px 16px; border-bottom: 1px solid var(--line); cursor: pointer;
  display: flex; flex-direction: column; gap: 3px; min-width: 0;
}
.conv:hover { background: #f7f9fc; }
.conv.active { background: #eef2f8; border-left: 3px solid var(--accent); padding-left: 13px; }
.conv-name { font-weight: 600; font-size: 14px; }
.conv-preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-unread { width: 8px; height: 8px; background: var(--accent); border-radius: 50%; align-self: flex-start; }

/* ---------- Thread ---------- */
.thread { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.thread-head { padding: 14px 16px; border-bottom: 1px solid var(--line); background: var(--panel); display: flex; align-items: center; gap: 12px; min-width: 0; }
.thread-title { font-weight: 700; color: var(--navy); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back-btn { display: none; flex: 0 0 auto; width: 40px; height: 40px; border: 1px solid var(--line); background: #fff; color: var(--navy); border-radius: 9px; font-size: 24px; line-height: 1; cursor: pointer; align-items: center; justify-content: center; padding: 0; }
.back-btn:hover { background: #f3f6fb; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; min-height: 0; min-width: 0; }
.msg { max-width: 80%; padding: 9px 13px; border-radius: 14px; font-size: 14px; line-height: 1.4; overflow-wrap: anywhere; }
.msg.inbound { background: var(--bubble-in); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.outbound { background: var(--bubble-out); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.msg .meta { display: block; font-size: 11px; opacity: .6; margin-top: 4px; }

.composer { display: flex; gap: 10px; padding: 14px 16px; padding-bottom: calc(14px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--panel); }
.composer textarea {
  flex: 1; min-width: 0; resize: none; padding: 11px 14px; border: 1px solid var(--line); border-radius: 12px;
  font-size: 16px; font-family: inherit; max-height: 120px;
}
.composer textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }
.composer button {
  border: 0; border-radius: 12px; padding: 0 20px; background: var(--navy); color: #fff;
  font-weight: 600; cursor: pointer;
}
.composer button:disabled { background: #b7c0cf; cursor: not-allowed; }

/* Single-pane master-detail on phones, tablets (incl. iPad portrait 768px),
   and narrow windows. Above this, the desktop 2-column layout applies. */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .app.thread-open .sidebar { display: none; }
  .app:not(.thread-open) .thread { display: none; }
  .app.thread-open .back-btn { display: inline-flex; }

  /* Anchor the modal near the top so the on-screen keyboard can't bury it,
     and let it scroll if it's taller than the visible area. */
  .modal { align-items: flex-start; }
  .modal-card { margin-top: 5vh; max-height: 90vh; overflow-y: auto; }
}
