:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #273449;
  --line: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #f59e0b;
  --accent-dim: #78350f;
  --green: #34d399;
  --red: #f87171;
  --blue: #60a5fa;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: var(--red); }

/* ---------------------------------------------------------- login */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px 24px;
  text-align: center;
}
.login-card img { border-radius: 14px; }
.login-card h1 { font-size: 20px; margin: 14px 0 4px; }
.login-card input {
  width: 100%;
  margin: 18px 0 12px;
  padding: 14px;
  font-size: 20px;
  text-align: center;
  letter-spacing: 6px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
}
.login-card input:focus { outline: 2px solid var(--blue); border-color: transparent; }

/* ---------------------------------------------------------- chrome */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  background: rgba(15, 23, 42, .92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 17px; margin: 0; letter-spacing: .2px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); flex: none; }
.dot.stale { background: var(--accent); }
.dot.bad { background: var(--red); }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.icon-btn {
  background: none; border: 0; color: var(--muted);
  padding: 8px; border-radius: 10px; cursor: pointer;
  display: grid; place-items: center;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.spinning svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.tabs {
  position: sticky; top: 0; z-index: 15;
  display: flex; gap: 4px;
  padding: 10px 12px;
  overflow-x: auto;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none;
  display: flex; align-items: center; gap: 7px;
  padding: 9px 14px;
  font-size: 14px; font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid transparent;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}
.tab.active { color: var(--text); background: var(--surface-2); border-color: var(--line); }
.badge {
  min-width: 20px; padding: 1px 6px;
  font-size: 12px; font-weight: 700;
  background: var(--line); color: var(--text);
  border-radius: 999px;
}
.badge.amber { background: var(--accent-dim); color: var(--accent); }
.badge.zero { opacity: .45; }

main {
  padding: 14px 12px calc(28px + var(--safe-b));
  max-width: 780px;
  margin: 0 auto;
}
.loading { text-align: center; color: var(--muted); padding: 40px 0; }

/* ---------------------------------------------------------- cards */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.card.urgent { border-color: var(--accent); }
.card-head { display: flex; align-items: flex-start; gap: 10px; }
.card-head h3 {
  margin: 0; font-size: 15px; font-weight: 650; line-height: 1.35;
  flex: 1; word-break: break-word;
}
.meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.pill {
  font-size: 11px; font-weight: 650;
  padding: 3px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
  border: 1px solid var(--line);
}
.pill.unread { background: var(--accent-dim); color: var(--accent); border-color: transparent; }
.pill.overdue { background: #7f1d1d; color: #fecaca; border-color: transparent; }
.pill.label { background: #1e3a8a; color: #bfdbfe; border-color: transparent; }
.pill.ok { background: #064e3b; color: #a7f3d0; border-color: transparent; }
.pill.saved { background: #4c1d95; color: #ddd6fe; border-color: transparent; }

.sortbar {
  display: flex; align-items: center; gap: 8px;
  margin: 0 2px 12px;
}
.sortbar label {
  font-size: 11px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .06em;
}
.sortbar select {
  flex: 1;
  padding: 9px 11px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  font: inherit; font-size: 13px;
}
.sortbar select:focus { outline: 2px solid var(--blue); border-color: transparent; }
@media (min-width: 720px) { .sortbar select { flex: 0 0 260px; } }

.summary { margin: 10px 0 0; font-size: 14px; color: #cbd5e1; }

.attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.attach {
  font-size: 12px;
  padding: 5px 9px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: #cbd5e1;
  word-break: break-all;
}
.questions { margin: 10px 0 0; padding-left: 18px; font-size: 14px; color: #e2e8f0; }
.questions li { margin: 3px 0; }

.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.btn {
  /* inline-flex so <a class="btn"> lines up with real <button>s rather than
     rendering as inline text with a different box */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 auto;
  min-width: 112px;
  padding: 11px 14px;
  font: inherit;
  font-size: 14px; font-weight: 650;
  line-height: 1.2;
  white-space: nowrap;      /* keep "☆ Save" on one line */
  text-decoration: none;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 11px;
  cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--blue); border-color: var(--blue); color: #06203f; }
.btn.good { background: var(--green); border-color: var(--green); color: #043027; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); }
/* auto basis, never 0, or the label wraps character by character */
.btn.tiny { flex: 0 0 auto; min-width: 0; padding: 9px 14px; font-size: 13px; }

.draft-body {
  margin-top: 12px;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 11px;
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 90px;
  color: #e2e8f0;
}
.draft-body:focus { outline: 2px solid var(--blue); }

.group-head {
  display: flex; align-items: center; gap: 8px;
  margin: 24px 2px 12px;
  font-size: 12px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted);
}
.group-head:first-child { margin-top: 4px; }
.group-head .count {
  padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--text);
  font-size: 11px; letter-spacing: 0;
}
.group-note { margin: -4px 2px 12px; font-size: 13px; color: var(--muted); }

.empty { text-align: center; padding: 48px 20px; color: var(--muted); }
.empty .big { font-size: 40px; margin-bottom: 10px; }

.toast {
  position: fixed;
  left: 50%; bottom: calc(20px + var(--safe-b));
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--line);
  padding: 11px 18px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 60;
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
}

/* ---------------------------------------------------------- modal */
.modal {
  width: min(680px, 94vw);
  max-height: 84dvh;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  /* The dialog must not scroll itself — only .modal-body does. Without this
     both boxes get a scrollbar and you see two side by side. */
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.modal::backdrop { background: rgba(0,0,0,.6); }
.modal-head {
  flex: none;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
/* flex child owns the scrolling; no max-height needed and no second scrollbar */
.modal-body { flex: 1 1 auto; padding: 16px; overflow-y: auto; min-height: 0; }
.msg { padding: 12px 0; border-bottom: 1px solid var(--line); }
.msg:last-child { border-bottom: 0; }
.msg-head { display: flex; justify-content: space-between; gap: 10px; font-size: 13px; }
.msg-who { font-weight: 650; }
.msg.me .msg-who { color: var(--blue); }
.msg-body { margin-top: 6px; font-size: 14px; white-space: pre-wrap; color: #cbd5e1; }

/* ---------------------------------------------------------- people */
.person {
  display: flex; align-items: stretch; gap: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 10px;
  overflow: hidden;
}
.person-open {
  flex: 1; min-width: 0;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 6px 12px 14px;
  background: none; border: 0;
  cursor: pointer; text-align: left;
  color: inherit; font: inherit;
}
.person-open:active { transform: scale(.995); }
.person-mail {
  flex: none;
  width: 58px;
  display: grid; place-items: center;
  background: var(--surface-2);
  border: 0; border-left: 1px solid var(--line);
  font-size: 19px; cursor: pointer;
}
.person-mail:hover { background: var(--line); }

.avatar {
  flex: none; width: 42px; height: 42px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--surface-2);
  font-weight: 700; font-size: 15px;
  color: var(--blue);
}
.person-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.person-name { font-weight: 650; font-size: 15px; color: var(--text); }
/* readable secondary text: --muted is too dim against the card for body copy */
.person-title {
  font-size: 13.5px; color: #dbe4ef; font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.person-sub {
  font-size: 12.5px; color: #a9b6c7;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.subhead {
  margin: 20px 0 10px;
  font-size: 11px; font-weight: 700; letter-spacing: .08em;
  text-transform: uppercase; color: var(--muted);
  border-top: 1px solid var(--line); padding-top: 16px;
}
.phone-row { display: flex; gap: 8px; }
.phone-row input { flex: 1 1 auto; min-width: 0; }
.phone-row select {
  flex: 0 0 132px;
  padding: 11px 8px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  font: inherit; font-size: 13px;
}
.phone-row select:focus, .field select:focus { outline: 2px solid var(--blue); border-color: transparent; }

.field { margin-bottom: 14px; }
.field label {
  display: block; font-size: 12px; font-weight: 650;
  color: var(--muted); margin-bottom: 5px;
  text-transform: uppercase; letter-spacing: .05em;
}
.field input, .field textarea {
  width: 100%; padding: 11px 12px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px;
  font: inherit; font-size: 14px;
}
.field textarea { min-height: 74px; resize: vertical; }
.field input:focus, .field textarea:focus { outline: 2px solid var(--blue); border-color: transparent; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 520px) { .row2 { grid-template-columns: 1fr; gap: 0; } }

.contact-links { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.contact-links a {
  flex: 1; min-width: 120px; text-align: center;
  padding: 11px; border-radius: 11px;
  background: var(--surface-2); border: 1px solid var(--line);
  color: var(--text); text-decoration: none;
  font-size: 14px; font-weight: 650;
}

@media (min-width: 720px) {
  main { padding-top: 20px; }
  .card { padding: 18px; }
  .btn { flex: 0 0 auto; }
}

/* On narrow phones let the primary action take the full row so the smaller
   buttons below it get real spacing instead of being crushed together. */
@media (max-width: 460px) {
  .actions { gap: 8px; }
  .btn { min-width: 96px; }
  .btn.good, .btn.primary { flex: 1 0 100%; }
}
