/* ============================================================
   BUILDYBOT — APP VIEWER LAYOUT (existing)
   ============================================================ */

/* --- App-specific custom properties (override these) --- */
:root {
  --app-header-height: 48px;
  --app-header-bg: var(--nav-bg);
  --app-header-text: var(--nav-text);
  --app-header-border: var(--border-color);
  --app-header-font-size: 0.88rem;
  --app-header-padding-x: 1rem;
  --app-back-btn-bg: transparent;
  --app-back-btn-color: var(--nav-text);
  --app-back-btn-hover-bg: rgba(255, 255, 255, 0.12);
  --app-back-btn-radius: var(--radius-sm);
  --app-body-bg: var(--bg);
  --app-body-padding: 0;

  /* Added sensible fallbacks if no global theme is present */
  --bg: #0b0f14;
  --panel: #0f1722;
  --panel-2: #101b2a;
  --text: #e8eef7;
  --muted: rgba(232, 238, 247, 0.72);
  --border-color: rgba(232, 238, 247, 0.12);
  --nav-bg: #0f1722;
  --nav-text: #e8eef7;
  --primary: #4f8cff;
  --danger: #ff5d5d;
  --radius-sm: 10px;
  --radius-md: 14px;
  --shadow: 0 10px 30px rgba(0,0,0,.35);
  --transition-speed: 0.18s;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  --sans: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
}

/* --- Header bar --- */
.app-viewer-header {
  position: sticky;
  top: 0;
  z-index: 1030;
  display: flex;
  align-items: center;
  height: var(--app-header-height);
  background: var(--app-header-bg);
  color: var(--app-header-text);
  border-bottom: 1px solid var(--app-header-border);
  padding: 0 var(--app-header-padding-x);
  gap: 0.75rem;
  font-size: var(--app-header-font-size);
  transition: background-color var(--transition-speed, 0.2s) ease,
              border-color var(--transition-speed, 0.2s) ease;
}

/* --- Back button --- */
.app-viewer-back {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.65rem;
  background: var(--app-back-btn-bg);
  color: var(--app-back-btn-color);
  border: none;
  border-radius: var(--app-back-btn-radius);
  font-size: inherit;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.15s ease;
}

.app-viewer-back:hover,
.app-viewer-back:focus-visible {
  background: var(--app-back-btn-hover-bg);
  color: var(--app-back-btn-color);
  text-decoration: none;
}

.app-viewer-back i {
  font-size: 1.1em;
}

/* --- Separator between back button and app name --- */
.app-viewer-sep {
  width: 1px;
  height: 1.2rem;
  background: currentColor;
  opacity: 0.25;
  flex-shrink: 0;
}

/* --- App name --- */
.app-viewer-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* --- Right-side slot for optional actions --- */
.app-viewer-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

/* --- Main content area beneath the header --- */
.app-viewer-body {
  flex: 1;
  background: var(--app-body-bg);
  padding: var(--app-body-padding);
  min-height: calc(100vh - var(--app-header-height));
}

/* --- Responsive --- */
@media (max-width: 575.98px) {
  .app-viewer-header {
    gap: 0.5rem;
    --app-header-padding-x: 0.65rem;
  }

  .app-viewer-back span {
    display: none;
  }
}

/* ============================================================
   MINI CRM — APP STYLES
   ============================================================ */

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
}

.app { min-height: 100vh; display: flex; flex-direction: column; }

.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0);
  white-space: nowrap; border: 0;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: .5rem;
}

.search-wrap {
  display: flex;
  align-items: center;
  gap: .35rem;
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border-color);
  border-radius: 999px;
  padding: .25rem .35rem .25rem .6rem;
  min-width: 320px;
}
.search-wrap input {
  width: 100%;
  min-width: 220px;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: .92rem;
}
.search-wrap input::placeholder { color: rgba(232,238,247,.55); }

select, input, textarea, button { font: inherit; }
select {
  background: rgba(255,255,255,0.06);
  color: var(--text);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: .45rem .55rem;
  outline: none;
}
select:focus-visible { border-color: rgba(79,140,255,.6); }

.btn {
  border: 1px solid var(--border-color);
  background: rgba(255,255,255,0.06);
  color: var(--text);
  padding: .45rem .7rem;
  border-radius: 10px;
  cursor: pointer;
  transition: background var(--transition-speed) ease, border-color var(--transition-speed) ease;
}
.btn:hover { background: rgba(255,255,255,0.09); }
.btn:focus-visible { outline: 2px solid rgba(79,140,255,.45); outline-offset: 2px; }

.btn.primary { background: rgba(79,140,255,.18); border-color: rgba(79,140,255,.35); }
.btn.primary:hover { background: rgba(79,140,255,.26); }
.btn.danger { background: rgba(255,93,93,.12); border-color: rgba(255,93,93,.35); }
.btn.small { padding: .35rem .55rem; font-size: .88rem; border-radius: 10px; }

.icon-btn {
  border: 1px solid transparent;
  background: transparent;
  color: rgba(232,238,247,.8);
  cursor: pointer;
  padding: .35rem .45rem;
  border-radius: 10px;
}
.icon-btn:hover { background: rgba(255,255,255,0.08); color: var(--text); }
.icon-btn.copy { border-color: var(--border-color); background: rgba(255,255,255,0.04); }
.icon-btn.copy:hover { background: rgba(255,255,255,0.09); }

.muted { color: var(--muted); }
.small { font-size: .88rem; }
.kbd { font-family: var(--mono); font-size: .9em; border: 1px solid var(--border-color); padding: .05rem .35rem; border-radius: 8px; background: rgba(255,255,255,0.05); }
.sep-dot { opacity: .5; padding: 0 .35rem; }

.layout {
  display: grid;
  grid-template-columns: 380px 1fr;
  min-height: calc(100vh - var(--app-header-height));
}

.list-pane {
  border-right: 1px solid var(--border-color);
  background: linear-gradient(180deg, rgba(255,255,255,0.02), transparent 120px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.list-header {
  padding: .75rem .85rem .45rem;
  border-bottom: 1px solid var(--border-color);
}
.list-meta { display: flex; align-items: baseline; justify-content: space-between; gap: .75rem; }
.kbd-hint { font-size: .82rem; }

.list {
  overflow: auto;
  padding: .25rem 0;
}
.row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: .35rem .7rem;
  padding: .6rem .85rem;
  border-bottom: 1px solid rgba(232,238,247,0.06);
  cursor: pointer;
}
.row:hover { background: rgba(255,255,255,0.04); }
.row[aria-selected="true"] { background: rgba(79,140,255,0.16); border-bottom-color: rgba(79,140,255,0.22); }
.row-top { min-width: 0; }
.row-title {
  display: flex; align-items: baseline; gap: .4rem; min-width: 0;
}
.row-company { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-name { color: rgba(232,238,247,.82); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; margin-top: .2rem; }
.primary-contact { font-size: .86rem; color: var(--muted); }
.chip {
  font-size: .78rem;
  padding: .12rem .45rem;
  border-radius: 999px;
  border: 1px solid rgba(232,238,247,0.12);
  background: rgba(255,255,255,0.04);
  color: rgba(232,238,247,0.86);
}
.chip button {
  all: unset;
  cursor: pointer;
  margin-left: .35rem;
  opacity: .65;
}
.chip button:hover { opacity: 1; }

.followup-pill {
  justify-self: end;
  align-self: start;
  font-size: .78rem;
  padding: .12rem .45rem;
  border-radius: 999px;
  border: 1px solid rgba(232,238,247,0.12);
  background: rgba(255,255,255,0.04);
  color: rgba(232,238,247,0.86);
  white-space: nowrap;
}
.followup-pill.overdue { border-color: rgba(255,93,93,.4); background: rgba(255,93,93,.10); }
.followup-pill.today { border-color: rgba(255,180,79,.45); background: rgba(255,180,79,.12); }
.followup-pill.upcoming { border-color: rgba(79,140,255,.40); background: rgba(79,140,255,.10); }

.empty {
  padding: 1.2rem .95rem;
}
.empty-title { font-weight: 650; margin-bottom: .25rem; }

.detail-pane {
  overflow: auto;
  padding: 1rem;
}
.detail-empty {
  height: 100%;
  display: grid;
  place-items: center;
  border: 1px dashed rgba(232,238,247,0.18);
  border-radius: var(--radius-md);
  padding: 1.25rem;
  background: rgba(255,255,255,0.02);
}
.detail-empty-title { font-weight: 700; font-size: 1.05rem; margin-bottom: .2rem; }

.detail-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: .75rem;
}
.title-block { min-width: 0; width: 100%; }
.title-row { display: flex; align-items: baseline; min-width: 0; }
.title-input {
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  font-weight: 700;
  font-size: 1.15rem;
  padding: .25rem .35rem;
  border-radius: 10px;
  min-width: 0;
}
.title-input.company { font-weight: 650; opacity: .9; }
.title-input:focus-visible { outline: 2px solid rgba(79,140,255,.45); outline-offset: 2px; border-color: rgba(79,140,255,.25); background: rgba(79,140,255,.06); }

.tags-row { display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin-top: .35rem; flex-wrap: wrap; }
.chips { display: flex; flex-wrap: wrap; gap: .35rem; }
.tag-editor input {
  width: min(320px, 100%);
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border-color);
  color: var(--text);
  padding: .42rem .55rem;
  border-radius: 10px;
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .8rem;
}
.card {
  border: 1px solid var(--border-color);
  background: rgba(255,255,255,0.03);
  border-radius: var(--radius-md);
  padding: .85rem;
  box-shadow: none;
}
.card-title { font-weight: 700; margin-bottom: .65rem; }
.card.span-2 { grid-column: span 2; }

.field { margin-bottom: .65rem; }
.field label { display: block; font-size: .86rem; color: var(--muted); margin-bottom: .3rem; }
.field-row { display: grid; grid-template-columns: 1fr auto; gap: .5rem; align-items: center; }
.field input, textarea {
  width: 100%;
  border: 1px solid var(--border-color);
  background: rgba(255,255,255,0.04);
  color: var(--text);
  padding: .5rem .6rem;
  border-radius: 10px;
  outline: none;
}
.field input:focus-visible, textarea:focus-visible { outline: 2px solid rgba(79,140,255,.35); outline-offset: 2px; border-color: rgba(79,140,255,.25); }
textarea { resize: vertical; line-height: 1.35; }

.divider { height: 1px; background: rgba(232,238,247,0.10); margin: .75rem 0; }

.inline-actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .5rem; }
.dupes {
  margin-top: .6rem;
  border: 1px solid rgba(232,238,247,0.12);
  border-radius: 12px;
  overflow: hidden;
}
.dupe-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .55rem .65rem;
  border-top: 1px solid rgba(232,238,247,0.08);
  background: rgba(255,255,255,0.02);
}
.dupe-row:first-child { border-top: none; }
.dupe-row .meta { min-width: 0; }
.dupe-row .meta .t { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dupe-row .meta .s { font-size: .85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.related-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.related-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .4rem; }
.related-title { font-weight: 650; }
.related-list {
  border: 1px solid rgba(232,238,247,0.12);
  border-radius: 12px;
  min-height: 56px;
  overflow: hidden;
}
.related-item {
  padding: .55rem .65rem;
  border-top: 1px solid rgba(232,238,247,0.08);
  display: flex;
  justify-content: space-between;
  gap: .75rem;
}
.related-item:first-child { border-top: none; }
.related-item .left { min-width: 0; }
.related-item .right { color: var(--muted); white-space: nowrap; }
.related-item .top { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.related-item .sub { color: var(--muted); font-size: .86rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.related-empty { padding: .65rem; color: var(--muted); font-size: .9rem; }

.modal::backdrop { background: rgba(0,0,0,.55); }
.modal { border: none; padding: 0; background: transparent; }
.modal-card {
  width: min(760px, calc(100vw - 1.2rem));
  border-radius: var(--radius-md);
  border: 1px solid rgba(232,238,247,0.14);
  background: rgba(15,23,34,0.95);
  box-shadow: var(--shadow);
  color: var(--text);
  overflow: hidden;
}
.modal-head, .modal-foot {
  padding: .8rem .9rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
}
.modal-head { border-bottom: 1px solid rgba(232,238,247,0.10); }
.modal-title { font-weight: 750; }
.modal-body { padding: .9rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.grid2 .span-2 { grid-column: span 2; }

.warn {
  margin-top: .75rem;
  border: 1px solid rgba(255,180,79,.35);
  background: rgba(255,180,79,.10);
  padding: .6rem .7rem;
  border-radius: 12px;
  color: rgba(255,230,200,.95);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  background: rgba(15,23,34,0.92);
  border: 1px solid rgba(232,238,247,0.14);
  color: var(--text);
  padding: .55rem .75rem;
  border-radius: 12px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

.followups {
  position: fixed;
  left: 10px;
  top: calc(var(--app-header-height) + 10px);
  width: 320px;
  max-height: calc(100vh - var(--app-header-height) - 20px);
  overflow: auto;
  background: rgba(15,23,34,0.92);
  border: 1px solid rgba(232,238,247,0.14);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
  z-index: 2000;
  display: none;
}
.followups.open { display: block; }
.followups-header {
  position: sticky;
  top: 0;
  background: rgba(15,23,34,0.96);
  border-bottom: 1px solid rgba(232,238,247,0.10);
  padding: .7rem .75rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
}
.followups-title { font-weight: 750; }
.followups-groups { padding: .55rem .55rem .7rem; }
.group { margin-bottom: .7rem; }
.group-title { font-weight: 700; font-size: .9rem; margin: .35rem .2rem; color: rgba(232,238,247,.88); }
.followup-item {
  border: 1px solid rgba(232,238,247,0.12);
  background: rgba(255,255,255,0.03);
  border-radius: 12px;
  padding: .55rem .6rem;
  margin: .35rem 0;
  cursor: pointer;
}
.followup-item:hover { background: rgba(255,255,255,0.06); }
.followup-item .t { font-weight: 650; }
.followup-item .s { color: var(--muted); font-size: .86rem; margin-top: .1rem; display: flex; justify-content: space-between; gap: .5rem; }
.followup-item.overdue { border-color: rgba(255,93,93,.35); }
.followup-item.today { border-color: rgba(255,180,79,.35); }
.followup-item.upcoming { border-color: rgba(79,140,255,.30); }

.merge-list {
  margin-top: .75rem;
  border: 1px solid rgba(232,238,247,0.12);
  border-radius: 12px;
  overflow: hidden;
}
.merge-opt {
  display: flex;
  gap: .6rem;
  padding: .6rem .7rem;
  border-top: 1px solid rgba(232,238,247,0.08);
  align-items: flex-start;
  background: rgba(255,255,255,0.02);
}
.merge-opt:first-child { border-top: none; }
.merge-opt input { margin-top: .2rem; }
.merge-opt .meta { min-width: 0; }
.merge-opt .meta .t { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.merge-opt .meta .s { color: var(--muted); font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

@media (max-width: 980px) {
  .layout { grid-template-columns: 360px 1fr; }
  .search-wrap { min-width: 260px; }
}

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
  .list-pane { border-right: none; border-bottom: 1px solid var(--border-color); height: 45vh; }
  .detail-pane { height: auto; }
  .detail-grid { grid-template-columns: 1fr; }
  .card.span-2 { grid-column: auto; }
  .related-grid { grid-template-columns: 1fr; }
  .followups { width: calc(100vw - 20px); left: 10px; }
}