:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #dfe3e8;
  --text: #1f2937;
  --muted: #6b7280;
  --brand: #b5471b;
  --brand-dark: #8f3814;
  --accent: #2f6f4f;
  --danger: #b3261e;
  --radius: 8px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }

/* Login */
.login-screen {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
}
.login-card {
  background: var(--panel);
  padding: 2.5rem;
  border-radius: var(--radius);
  width: 320px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.login-card h1 { margin: 0 0 1.5rem; font-size: 1.3rem; color: var(--brand-dark); }
.login-card label { display: block; margin-bottom: 1rem; font-size: 0.85rem; color: var(--muted); }
.login-card input {
  display: block; width: 100%; margin-top: 0.3rem; padding: 0.55rem 0.7rem;
  border: 1px solid var(--border); border-radius: 6px; font-size: 0.95rem;
}
.login-card button {
  width: 100%; padding: 0.6rem; background: var(--brand); color: white;
  border: none; border-radius: 6px; font-size: 0.95rem; cursor: pointer;
}
.login-card button:hover { background: var(--brand-dark); }
.error-text { color: var(--danger); font-size: 0.85rem; margin-top: 0.8rem; }
.login-divider { display: flex; align-items: center; text-align: center; color: var(--muted); font-size: 0.78rem; margin: 1.1rem 0; }
.login-divider::before, .login-divider::after { content: ""; flex: 1; border-bottom: 1px solid var(--border); }
.login-divider span { padding: 0 0.6rem; }
.ms-login-btn {
  display: flex; align-items: center; justify-content: center; gap: 0.6rem; width: 100%;
  padding: 0.55rem; border: 1px solid var(--border); border-radius: 6px; background: white;
  color: var(--text); text-decoration: none; font-size: 0.9rem; box-sizing: border-box;
}
.ms-login-btn:hover { background: #f7f7f7; }

/* App shell */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px; background: #1c2431; color: #cfd6e0; display: flex; flex-direction: column;
  padding: 1.2rem 0; flex-shrink: 0;
}
.sidebar .brand { font-weight: 700; font-size: 1.05rem; padding: 0 1.2rem 1.2rem; color: white; }
.sidebar .brand span { display: block; font-weight: 400; font-size: 0.75rem; color: var(--brand); letter-spacing: 0.1em; }
.sidebar a {
  display: block; padding: 0.6rem 1.2rem; color: #cfd6e0; text-decoration: none; font-size: 0.9rem;
}
.sidebar a:hover, .sidebar a.active { background: #2a3444; color: white; }
.sidebar-footer { margin-top: auto; padding: 1rem 1.2rem 0; border-top: 1px solid #2a3444; }
#current-user { font-size: 0.8rem; color: #9aa5b1; margin-bottom: 0.5rem; }
.link-btn { background: none; border: none; color: var(--brand); cursor: pointer; font-size: 0.85rem; padding: 0; }

.main-content { flex: 1; padding: 1.8rem 2.2rem; overflow-y: auto; max-width: 100%; }

/* Generic layout helpers */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.2rem; flex-wrap: wrap; gap: 0.6rem; }
.page-header h2 { margin: 0; font-size: 1.4rem; }
.toolbar { display: flex; gap: 0.6rem; flex-wrap: wrap; align-items: center; }

button, .btn {
  cursor: pointer; border: none; border-radius: 6px; padding: 0.5rem 0.9rem; font-size: 0.85rem;
  background: var(--brand); color: white;
}
button:hover, .btn:hover { background: var(--brand-dark); }
button.secondary { background: #e5e7eb; color: var(--text); }
button.secondary:hover { background: #d1d5db; }
button.danger { background: var(--danger); }
button.danger:hover { background: #8f1d17; }
button:disabled { opacity: 0.5; cursor: not-allowed; }

input, select, textarea {
  padding: 0.45rem 0.6rem; border: 1px solid var(--border); border-radius: 6px; font-size: 0.85rem;
  font-family: inherit; background: white;
}
textarea { width: 100%; min-height: 70px; }

table { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: var(--radius); overflow: hidden; }
table th, table td { text-align: left; padding: 0.55rem 0.7rem; border-bottom: 1px solid var(--border); font-size: 0.85rem; }
table th { background: #eef1f4; font-weight: 600; color: var(--muted); text-transform: uppercase; font-size: 0.72rem; letter-spacing: 0.03em; }
table tbody tr:hover { background: #fafbfc; cursor: pointer; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.2rem; margin-bottom: 1rem; }
.card h3 { margin: 0 0 0.9rem; font-size: 1rem; color: var(--brand-dark); }

.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 0.8rem 1rem; }
.form-grid label { display: flex; flex-direction: column; font-size: 0.78rem; color: var(--muted); gap: 0.3rem; }
.form-grid label.span-2 { grid-column: span 2; }
.checkbox-row { display: flex; align-items: center; gap: 0.4rem; font-size: 0.85rem; }
.checkbox-row input { width: auto; }

.badge { display: inline-block; padding: 0.15rem 0.55rem; border-radius: 999px; font-size: 0.72rem; font-weight: 600; }
.badge.existing { background: #dcf5e6; color: var(--accent); }
.badge.prospect { background: #fef3cd; color: #92660c; }
.badge.open { background: #e0edff; color: #1d4ed8; }
.badge.closed_won { background: #dcf5e6; color: var(--accent); }
.badge.closed_lost { background: #fbe0df; color: var(--danger); }

.tag-list { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.tag { background: #eef1f4; border-radius: 6px; padding: 0.2rem 0.5rem; font-size: 0.78rem; display: flex; align-items: center; gap: 0.3rem; }
.tag button { padding: 0; background: none; color: var(--danger); font-size: 0.9rem; line-height: 1; }

.tabs { display: flex; gap: 0.3rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.tabs button { background: none; color: var(--muted); border-radius: 6px 6px 0 0; padding: 0.5rem 1rem; }
.tabs button.active { color: var(--brand-dark); background: var(--panel); border: 1px solid var(--border); border-bottom-color: var(--panel); position: relative; top: 1px; }

.empty-state { color: var(--muted); font-size: 0.85rem; padding: 1rem 0; }
.back-link { color: var(--muted); text-decoration: none; font-size: 0.85rem; }
.back-link:hover { color: var(--brand); }

.archived-notice {
  background: #fff4e5; border: 1px solid #f0c088; color: #7a4b12;
  padding: 0.7rem 0.9rem; border-radius: 6px; margin-bottom: 0.9rem; font-size: 0.88rem;
}

.modal-overlay {
  position: fixed; inset: 0; background: rgba(15,20,28,0.45); display: flex; align-items: flex-start;
  justify-content: center; padding: 3rem 1rem; overflow-y: auto; z-index: 50;
}
.modal { background: var(--panel); border-radius: var(--radius); padding: 1.5rem; width: 100%; max-width: 640px; }
.modal h3 { margin-top: 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.2rem; }

.attachment-row { display: flex; align-items: center; justify-content: space-between; padding: 0.4rem 0; border-bottom: 1px solid var(--border); font-size: 0.85rem; }
.attachment-row:last-child { border-bottom: none; }

.toast {
  position: fixed; bottom: 1.5rem; right: 1.5rem; background: #1c2431; color: white; padding: 0.7rem 1.1rem;
  border-radius: 6px; font-size: 0.85rem; z-index: 100; box-shadow: 0 6px 18px rgba(0,0,0,0.25);
}
.toast.error { background: var(--danger); }

.update-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 200; background: var(--brand); color: white;
  padding: 0.6rem 1rem; display: flex; align-items: center; justify-content: center; gap: 1rem;
  font-size: 0.85rem; box-shadow: 0 2px 10px rgba(0,0,0,0.2);
}
.update-banner button { background: white; color: var(--brand-dark); padding: 0.3rem 0.8rem; }
.update-banner button:hover { background: #f0f0f0; }

.item-row { border: 1px solid var(--border); border-radius: 6px; padding: 0.8rem; margin-bottom: 0.6rem; }
.item-row-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; }

/* Interaction entry: single column at every width, capped so inputs don't
   stretch across a wide monitor. */
.narrow-form { max-width: 640px; }
.stacked-form { display: flex; flex-direction: column; gap: 0.9rem; }
.stacked-form label { display: block; }
.stacked-form input, .stacked-form select, .stacked-form textarea { width: 100%; }

.typeahead { position: relative; }
.typeahead-results {
  position: absolute; z-index: 20; left: 0; right: 0; background: white;
  border: 1px solid #d5dae1; border-radius: 6px; max-height: 260px; overflow-y: auto;
  box-shadow: 0 6px 18px rgba(0,0,0,0.12);
}
.typeahead-results button {
  /* `button, .btn` sets color:white for the app's primary buttons - these sit on a
     white dropdown, so the color has to be reset or the names are invisible. */
  display: block; width: 100%; text-align: left; background: none; border: none;
  color: var(--text); padding: 0.6rem 0.8rem; font-size: 0.9rem; cursor: pointer;
}
.typeahead-results button:hover { background: #f1f4f8; }
.typeahead-empty { padding: 0.6rem 0.8rem; color: #6b7280; font-size: 0.85rem; }
.typeahead-selected {
  margin-top: 0.4rem; font-weight: 600; font-size: 0.9rem;
}

/* ---------- Mobile (<=720px) ---------- */
.nav-toggle { display: none; }

@media (max-width: 720px) {
  .app-shell { flex-direction: column; }
  .sidebar {
    width: 100%; flex-direction: row; flex-wrap: wrap; align-items: center;
    padding: 0.6rem 0.9rem; position: sticky; top: 0; z-index: 30;
  }
  .sidebar .brand { padding: 0; flex: 1; }
  .nav-toggle {
    display: block; background: #2a3444; color: white; border: none; border-radius: 6px;
    padding: 0.5rem 0.9rem; font-size: 0.9rem; min-height: 44px; cursor: pointer;
  }
  .sidebar-links { display: none; width: 100%; }
  .sidebar-links.open { display: block; }
  .sidebar a { padding: 0.85rem 0.4rem; min-height: 44px; box-sizing: border-box; }
  .sidebar-footer { margin-top: 0.6rem; padding: 0.8rem 0.4rem 0; }

  .main-content { padding: 0.9rem; }
  .form-grid { grid-template-columns: 1fr; }
  .narrow-form { max-width: none; }

  /* 16px minimum stops iOS Safari zooming the page on focus. */
  input, select, textarea { font-size: 16px; }
  button { min-height: 44px; }

  table { display: block; overflow-x: auto; overflow-y: visible; max-width: 100%; }
}
