:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --ink: #1f2530;
  --muted: #6b7280;
  --line: #e3e6ea;
  --brand: #1f5fd0;
  --brand-ink: #ffffff;
  --danger: #c0392b;
  --radius: 8px;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Top bar / nav */
.topbar {
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  background: var(--panel); border-bottom: 1px solid var(--line);
  padding: 0 1.25rem; min-height: 56px;
  position: sticky; top: 0; z-index: 30;
}
.brand { font-weight: 700; color: var(--ink); white-space: nowrap; }
.nav-toggle {
  display: none; margin-left: auto; cursor: pointer; color: var(--ink);
  background: none; border: 1px solid var(--line); border-radius: 6px;
  padding: .35rem .6rem; font-size: 1.15rem; line-height: 1;
}
.topnav { display: flex; align-items: center; gap: .25rem; flex: 1; }
.topnav > a { padding: .5rem .8rem; border-radius: 6px; color: var(--ink); white-space: nowrap; }
.topnav > a:hover { background: var(--bg); text-decoration: none; }
.topnav > a.active { background: #eef2ff; color: var(--brand); font-weight: 600; }
.topnav-user { margin-left: auto; display: flex; align-items: center; gap: .5rem; color: var(--muted); }
.avatar { width: 28px; height: 28px; border-radius: 50%; }
.topnav-user .logout { padding: .4rem .7rem; border: 1px solid var(--line); border-radius: 6px; }

/* Admin nav dropdown (native <details>) */
.nav-dd { position: relative; }
.nav-dd > summary {
  list-style: none; cursor: pointer; white-space: nowrap;
  padding: .5rem .8rem; border-radius: 6px; color: var(--ink);
}
.nav-dd > summary::-webkit-details-marker { display: none; }
.nav-dd > summary::after { content: '▾'; margin-left: .3rem; color: var(--muted); font-size: .85em; }
.nav-dd > summary:hover { background: var(--bg); }
.nav-dd > summary.active { background: #eef2ff; color: var(--brand); font-weight: 600; }
.nav-dd-menu { display: flex; flex-direction: column; }
.nav-dd-menu > a { padding: .5rem .8rem; border-radius: 6px; color: var(--ink); white-space: nowrap; }
.nav-dd-menu > a:hover { background: var(--bg); text-decoration: none; }
.nav-dd-menu > a.active { background: #eef2ff; color: var(--brand); font-weight: 600; }
@media (min-width: 769px) {
  .nav-dd[open] > .nav-dd-menu {
    position: absolute; top: 100%; right: 0; z-index: 40; margin-top: .25rem;
    background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
    padding: .3rem; min-width: 170px; box-shadow: 0 8px 24px rgba(0,0,0,.10);
  }
}
@media (max-width: 768px) { .nav-dd-menu { padding-left: .6rem; } }

/* Segmented toggle (list grouping) */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg-btn { padding: .5rem .8rem; font-size: 14px; color: var(--ink); background: var(--panel); white-space: nowrap; }
.seg-btn + .seg-btn { border-left: 1px solid var(--line); }
.seg-btn.is-active { background: var(--brand); color: var(--brand-ink); }
.seg-btn:not(.is-active):hover { background: var(--bg); text-decoration: none; }

/* Grouped list subheader row */
.table tr.group-row th { background: #eef1f5; color: var(--ink); font-weight: 700;
  text-transform: none; letter-spacing: 0; font-size: 13px; }
.table tr.group-row th a { color: var(--ink); }

.container { max-width: 1080px; margin: 1.5rem auto; padding: 0 1.25rem; }
.footer { text-align: center; color: var(--muted); padding: 2rem; font-size: 13px; }

/* Headings / page head */
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1.25rem; }
.page-head h1 { font-size: 1.5rem; margin: .25rem 0; }
.breadcrumb { display: flex; flex-wrap: wrap; align-items: center; gap: .35rem; font-size: 13px; margin-bottom: .65rem; }
.breadcrumb a { color: var(--brand); }
.breadcrumb a:hover { text-decoration: underline; }
.breadcrumb-sep { color: var(--muted); }
.breadcrumb-current { color: var(--ink); font-weight: 600; }
.actions { display: flex; gap: .5rem; align-items: center; }

/* Cards & grid */
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem; margin-top: 1.25rem; }
/* min-width:0 lets a grid column shrink below its content's intrinsic width, so
   wide content (galleries, threads, long URLs) wraps inside the card instead of
   stretching the whole track to full screen width. */
.col { display: flex; flex-direction: column; gap: 1.25rem; min-width: 0; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.25rem; }
/* Space stacked top-level cards (e.g. Restore page); column cards use flex gap. */
.container > .card + .card { margin-top: 1.25rem; }
.card h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; }
.card-head h2 { margin: 0; }
@media (max-width: 800px) { .grid { grid-template-columns: 1fr; } }

/* Tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: .55rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { padding: .6rem 0; border-bottom: 1px solid var(--line); }
.list > li:last-child { border-bottom: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.empty { color: var(--muted); padding: 2rem 0; }

/* Badges */
.badge { display: inline-block; font-size: 11px; padding: .15rem .5rem; border-radius: 999px; vertical-align: middle; }
.badge--intelligence { background: #f3e8ff; color: #6b21a8; }
.badge--prospective { background: #eef2ff; color: #3949ab; }
.badge--approved { background: #e6f5ea; color: #1e7e34; }
.badge--suspended { background: #fff4e5; color: #b9770e; }
.badge--archived { background: #eceff1; color: #607d8b; }
.badge--role { background: #e0f2f1; color: #00695c; margin: 0 .15rem .15rem 0; }
.badge--due { background: #fff4e5; color: #b9770e; }
.badge--overdue { background: #fdecea; color: #c0392b; }

/* Follow-up count badge in the nav */
.nav-badge { display: inline-block; min-width: 1.1rem; padding: 0 .35rem; border-radius: 999px;
  background: #c0392b; color: #fff; font-size: 11px; text-align: center; }
.interaction-head { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; }

/* Company roles: badge row + checkbox grid on the form */
.roles { margin: .35rem 0 0; display: flex; flex-wrap: wrap; gap: .25rem; }
.checkgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr)); gap: .35rem .75rem; }

/* Buttons */
.btn {
  display: inline-block; cursor: pointer; font-size: 14px; line-height: 1;
  padding: .55rem .9rem; border-radius: 6px; border: 1px solid var(--line);
  background: var(--panel); color: var(--ink); text-decoration: none;
}
.btn:hover { text-decoration: none; border-color: #c7ccd3; }
.btn--primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn--primary:hover { background: #1a52b4; }
.btn--ghost { background: transparent; }
.btn--danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn--small { padding: .35rem .6rem; font-size: 13px; }
.btn--google { background: #fff; border: 1px solid #dadce0; font-weight: 600; padding: .7rem 1.2rem; }
.inline { display: inline; }
.link-danger { background: none; border: 0; color: var(--danger); cursor: pointer; font-size: 12px; padding: 0; }

/* Forms */
.form, .form-danger { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.25rem; max-width: 680px; }
.form-danger { margin-top: 1rem; border-color: #f1c7c2; }
.form label { display: block; margin-bottom: .9rem; font-size: 13px; color: var(--muted); }
.form input[type=text], .form input[type=email], .form input[type=number], .form input[type=search],
.form input[type=date], .form input[type=datetime-local], .form input[type=tel],
.form select, .form textarea {
  width: 100%; margin-top: .25rem; padding: .55rem .6rem; font: inherit;
  color: var(--ink); border: 1px solid #cfd4da; border-radius: 6px; background: #fff;
}
.form textarea { resize: vertical; }
.form-row { display: flex; gap: 1rem; }
.form-row > label { flex: 1; }
.checkbox { display: flex !important; align-items: center; gap: .5rem; color: var(--ink) !important; }
.checkbox input { width: auto; margin: 0; }
.form-actions { display: flex; gap: .6rem; align-items: center; margin-top: .5rem; }
.fieldset { border: 1px solid var(--line); border-radius: 6px; padding: 1rem; margin: 1.25rem 0 1rem; }
.fieldset legend { padding: 0 .4rem; color: var(--muted); font-size: 13px; }

/* Repeatable phone rows */
.phone-row { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: .5rem; align-items: center; }
.phone-row select, .phone-row input { margin-top: 0; width: auto; }
.phone-row select { flex: 0 0 auto; }
.phone-row .phone-cc { width: 5rem; flex: 0 0 auto; }
.phone-row .phone-num { flex: 1 1 8rem; }
.phone-row .phone-remove { flex: 0 0 auto; background: none; border: 1px solid var(--line); color: var(--danger); border-radius: 6px; cursor: pointer; padding: .4rem .65rem; line-height: 1; font-size: 1rem; }

/* Repeatable web-link rows */
.link-row { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: .5rem; align-items: center; }
.link-row input { margin-top: 0; width: auto; }
.link-row .link-title { flex: 1 1 8rem; }
.link-row .link-url { flex: 2 1 12rem; }
.link-row .link-remove { flex: 0 0 auto; background: none; border: 1px solid var(--line); color: var(--danger); border-radius: 6px; cursor: pointer; padding: .4rem .65rem; line-height: 1; font-size: 1rem; }

/* Filters */
.filters { display: flex; gap: .6rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.filters input[type=search], .filters select { padding: .5rem .6rem; border: 1px solid #cfd4da; border-radius: 6px; font: inherit; }
.filters input[type=search] { min-width: 240px; }

/* Details list */
.details { display: grid; grid-template-columns: 120px 1fr; gap: .4rem 1rem; margin: 0; }
.details dt { color: var(--muted); font-size: 13px; }
.details dd { margin: 0; }

/* Notes */
/* Collapsed "+ Add" toggles (notes, files) */
.note-add, .upload-toggle { margin: .25rem 0 1rem; }
.note-add > summary, .upload-toggle > summary {
  display: inline-block; cursor: pointer; list-style: none; user-select: none;
  font-size: 13px; font-weight: 500; color: var(--brand);
  background: #eef2ff; border: 1px solid #dbe3f8; padding: .35rem .7rem; border-radius: 6px;
}
.note-add > summary::-webkit-details-marker, .upload-toggle > summary::-webkit-details-marker { display: none; }
.note-add > summary:hover, .upload-toggle > summary:hover { background: #e2e9fb; }
.note-add[open] > summary, .upload-toggle[open] > summary { margin-bottom: .6rem; }
.note-form textarea { width: 100%; padding: .55rem; border: 1px solid #cfd4da; border-radius: 6px; font: inherit; resize: vertical; }
.note-form button { margin-top: .5rem; }
.note-bubble { margin: .4rem 0 .2rem; font-size: 13px; color: var(--ink) !important; }
.notes { list-style: none; margin: 1rem 0 0; padding: 0; display: flex; flex-direction: column; gap: .6rem; }
.note { padding: .55rem .75rem; border: 1px solid var(--line); border-radius: 8px; background: #fbfbfc; }
.note-head { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem .5rem; font-size: 12px; color: var(--muted); margin-bottom: .35rem; }
.note-author { font-weight: 600; color: var(--ink); }
.note-del { margin-left: auto; }
.note-body { white-space: normal; font-size: 14px; }

/* "edit" toggle: a small link, not a raw disclosure triangle. */
.note-edit > summary { list-style: none; cursor: pointer; color: var(--brand); }
.note-edit > summary::-webkit-details-marker { display: none; }
.note-edit > summary::marker { content: ''; }
.note-edit > summary:hover { text-decoration: underline; }
/* When open, drop the editor onto its own full-width row below the meta line
   instead of cramming it between the author/date and the delete link. */
.note-edit[open] { flex: 1 1 100%; order: 10; margin-top: .5rem; padding-top: .6rem; border-top: 1px dashed var(--line); }
.note-edit[open] > summary { margin-bottom: .5rem; font-weight: 600; color: var(--muted); }

/* Files / gallery */
.upload-form { margin-bottom: 1rem; }
.camera-btn { margin: .5rem .5rem .5rem 0; }
.camera-status { display: inline-block; vertical-align: middle; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: .75rem; }
.gallery-item { margin: 0; font-size: 12px; }
.gallery-item img { width: 100%; height: 90px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); }
.gallery-item figcaption { margin-top: .25rem; color: var(--muted); word-break: break-word; }
.filelist { list-style: none; margin: 1rem 0 0; padding: 0; }
.filelist li { display: flex; gap: .5rem; align-items: center; padding: .35rem 0; border-bottom: 1px solid var(--line); }

/* History */
.history { list-style: none; margin: 0; padding: 0; }
.history li { padding: .5rem 0; border-bottom: 1px solid var(--line); font-size: 13px; overflow: hidden; }
.history li:last-child { border-bottom: 0; }
.history-when { color: var(--muted); display: block; font-size: 12px; }
.history-del { float: right; margin-left: .5rem; }
.history-del button { font-size: 12px; }

/* Collapsible (History panel) */
details.collapsible > summary { cursor: pointer; font-weight: 600; font-size: 1.05rem; list-style: none; user-select: none; }
details.collapsible > summary::-webkit-details-marker { display: none; }
details.collapsible > summary::before { content: '▸'; display: inline-block; margin-right: .4rem; color: var(--muted); transition: transform .15s ease; }
details.collapsible[open] > summary::before { transform: rotate(90deg); }
details.collapsible > .history { margin-top: .6rem; }

/* Flash */
.flash { padding: .7rem 1rem; border-radius: 6px; margin-bottom: 1rem; }
.flash--success { background: #e6f5ea; color: #1e7e34; }
.flash--error { background: #fdecea; color: #b71c1c; }

/* Pager */
.pager { display: flex; gap: 1rem; align-items: center; margin-top: 1.25rem; }

/* Note source chips (roll-up) */
.note-src { margin-bottom: .25rem; }
.chip { display: inline-block; font-size: 11px; padding: .1rem .5rem; border-radius: 999px; background: #eef2ff; color: #3949ab; text-decoration: none; }
.chip--contact { background: #e6f5ea; color: #1e7e34; }
.chip--product { background: #fff4e5; color: #b9770e; }
.chip--company { background: #eceff1; color: #546e7a; }
a.chip:hover { text-decoration: none; filter: brightness(0.96); }

/* Emails / threads */
.emails { display: flex; flex-direction: column; gap: 1rem; }
.email { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.25rem; }
.email--outbound { border-left: 3px solid #1e7e34; }
.email--inbound { border-left: 3px solid #3949ab; }
.email-head { display: flex; align-items: center; gap: .6rem; }
.email-head .email-date { margin-left: auto; color: var(--muted); font-size: 13px; }
.email-subject { margin: .4rem 0 .2rem; }
.email-addrs { margin-bottom: .5rem; }
.email-body { white-space: normal; }
.email-atts { margin-top: .6rem; display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }
.email-meta { margin-top: .5rem; }

/* Interaction threads */
.thread > summary { cursor: pointer; list-style: none; user-select: none; }
.thread > summary::-webkit-details-marker { display: none; }
.thread > summary::before { content: '▸'; display: inline-block; margin-right: .4rem; color: var(--muted); transition: transform .15s ease; }
.thread[open] > summary::before { transform: rotate(90deg); }
.thread > summary { overflow-wrap: anywhere; }
.thread-msgs { margin: .6rem 0 .5rem; display: flex; flex-direction: column; gap: .6rem; }
.msg { padding: .5rem .7rem; border: 1px solid var(--line); border-radius: 8px; background: #fbfbfc; overflow-wrap: anywhere; }
.msg--outbound { border-left: 3px solid #1e7e34; }
.msg--inbound { border-left: 3px solid #3949ab; }
.badge--dir { background: #e8eefc; color: #3949ab; }

/* Login */
.login-body { display: grid; place-items: center; min-height: 100vh; background: var(--bg); }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 2.5rem; text-align: center; max-width: 380px; }
.login-card h1 { font-size: 1.3rem; }

/* ------------------------------------------------------------------ */
/* Responsive — phones & narrow screens                               */
/* ------------------------------------------------------------------ */
@media (max-width: 768px) {
  /* Nav collapses behind a toggle */
  .nav-toggle { display: block; }
  .topnav {
    display: none; flex-direction: column; align-items: stretch;
    flex-basis: 100%; gap: .1rem; padding-bottom: .6rem;
  }
  .topnav.open { display: flex; }
  .topnav > a { padding: .65rem .6rem; }
  .topnav-user {
    margin-left: 0; margin-top: .35rem; padding-top: .6rem;
    border-top: 1px solid var(--line); flex-wrap: wrap;
  }
  .topnav-user .logout { margin-left: auto; }

  /* Page headers stack; action buttons wrap */
  .page-head { flex-direction: column; align-items: stretch; }
  .actions { flex-wrap: wrap; }

  /* Two-up form rows stack */
  .form-row { flex-direction: column; gap: 0; }

  /* Filters / search go full width */
  .filters { flex-direction: column; align-items: stretch; }
  .filters input[type=search], .filters select { min-width: 0; width: 100%; }

  /* Tighter spacing */
  .container { padding: 0 1rem; margin-top: 1rem; }
  .form, .form-danger, .card { padding: 1rem; }
  .details { grid-template-columns: 1fr; gap: .15rem .5rem; }
  .details dt { margin-top: .4rem; }

  /* --- Mobile usability pass --- */
  /* Stop iOS auto-zoom on focus: any field under 16px triggers the zoom-jump. */
  input, select, textarea { font-size: 16px; }

  /* Comfortable ~44px touch targets for buttons and pager links. */
  .btn, .filters button, .pager a {
    min-height: 44px; display: inline-flex; align-items: center; justify-content: center;
  }
  /* Grow the hit area of the small text actions (edit / delete links). */
  .link-danger, .history-del button, .list a.small, .note-edit > summary {
    display: inline-block; padding-top: .45rem; padding-bottom: .45rem;
  }

  /* Primary actions span the width so they're easy to thumb. The page-head
     Edit/Delete pair stays compact (secondary) so it doesn't dominate. */
  .page-head > .btn { width: 100%; }
  .form-actions { flex-direction: column; align-items: stretch; }
  .form-actions .btn { width: 100%; }
  .filters .btn { width: 100%; }
  .note-form button, .upload-form button, .upload-form .btn { width: 100%; }
}

/* List tables on small phones: hide low-priority columns, and fold the most
   useful hidden field under the primary name. .cell-sub is desktop-hidden
   (that info lives in its own column there) and appears only on small screens. */
.cell-sub { display: none; }
@media (max-width: 560px) {
  .hide-sm { display: none; }
  .cell-sub { display: block; margin-top: .15rem; font-size: 12px; color: var(--muted); }
  .cell-sub a { color: inherit; }
  .cell-sub .badge--role { margin-top: .15rem; }
}

/* Collapsible detail-page cards on mobile (activated by footer JS via .card-toggle).
   A tappable heading with a rotating chevron; the body is hidden when collapsed. */
@media (max-width: 768px) {
  .card-toggle { cursor: pointer; user-select: none; }
  .card-toggle > h2, h2.card-toggle { display: flex; align-items: center; gap: .4rem; }
  .card-toggle > h2::before, h2.card-toggle::before {
    content: '\25B8'; /* ▸ */ color: var(--muted); font-size: .85em;
    flex: 0 0 auto; transition: transform .15s ease;
  }
  .card:not(.is-collapsed) > .card-toggle > h2::before,
  .card:not(.is-collapsed) > h2.card-toggle::before { transform: rotate(90deg); }
  /* Tidy the header row when the body is hidden. */
  .card.is-collapsed { padding-bottom: 1rem; }
  .card.is-collapsed > .card-head,
  .card.is-collapsed > h2.card-toggle { margin-bottom: 0; }
}
