:root {
  --crm-header-height: 56px;
  --crm-toolbar-height: 52px;
  --crm-sticky-table-z: 95;
}
/* Comments column */
.col-comments {
  min-width: 180px;
  max-width: 260px;
  color: var(--text-dim);
  word-break: break-word;
}
/* ── CRM Dark Theme ── */
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}

:root {
  --bg:        #0f1114;
  --surface:   #1a1d23;
  --surface2:  #22262e;
  --border:    #2e3340;
  --scroll-track: #161a21;
  --scroll-thumb: #3a4150;
  --scroll-thumb-hover: #4a5364;
  --text:      #e0e0e0;
  --text-dim:  #8c919a;
  --accent:    #6c8cff;
  --accent2:   #4fc3f7;
  --green:     #66bb6a;
  --orange:    #ffa726;
  --red:       #ef5350;
  --radius:    6px;
}

html { font-size: 14px; }

body {
  font-family: "Source Sans Pro", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}

/* Scrollbar theme */
html,
body,
.table-container {
  scrollbar-color: var(--scroll-thumb) var(--scroll-track);
  scrollbar-width: thin;
}

::-webkit-scrollbar {
  width: 12px;
  height: 12px;
}

::-webkit-scrollbar-track {
  background: var(--scroll-track);
}

::-webkit-scrollbar-thumb {
  background: var(--scroll-thumb);
  border-radius: 10px;
  border: 2px solid var(--scroll-track);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--scroll-thumb-hover);
}

#crmApp {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Header ── */
.crm-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 1.5rem;
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 10;
  box-shadow: 0 2px 8px -2px rgba(0,0,0,0.08);
  min-height: var(--crm-header-height);
}
.crm-header .logo {
  display: inline-flex;
  align-items: center;
  border: none;
  padding: 0;
  border-radius: 0;
  line-height: 0;
}
.crm-header .logo img {
  height: 45px;
  width: auto;
  display: block;
}
.crm-header .logo:hover { text-decoration: none; opacity: 0.92; }
.crm-header h1 {
  flex: 1 1 220px;
  min-width: 140px;
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text);
}
.header-stats {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.45rem;
  font-size: 0.85rem;
  color: var(--text-dim);
  max-width: 100%;
}
.header-stats .stat {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.24rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(255,255,255,0.02);
  white-space: nowrap;
}
.header-stats .dot {
  width: 8px; height: 8px; border-radius: 50%; display: inline-block;
}
.dot-green  { background: var(--green); }
.dot-orange { background: var(--orange); }
.dot-red    { background: var(--red); }

/* ── Toolbar ── */
.toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  position: relative;
  z-index: 9;
  box-shadow: 0 2px 8px -2px rgba(0,0,0,0.06);
  min-height: var(--crm-toolbar-height);
}
.toolbar-left { display: flex; gap: 0.5rem; flex: 1 1 420px; min-width: 260px; flex-wrap: wrap; }
.toolbar-right { display: flex; gap: 0.5rem; flex-wrap: wrap; justify-content: flex-end; }

#searchInput {
  flex: 1;
  min-width: 260px;
  padding: 0.5rem 0.8rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.2s;
}
#searchInput:focus { border-color: var(--accent); }
#searchInput::placeholder { color: var(--text-dim); }

#categoryFilter { min-width: 190px; }
#qualityFilter { min-width: 140px; }

select {
  padding: 0.55rem 0.9rem;
  min-height: 38px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  outline: none;
}
select:focus { border-color: var(--accent); }

.row-select {
  width: 100%;
  min-height: 34px;
  padding: 0.4rem 0.6rem;
  border-radius: 5px;
  font-size: 0.86rem;
  line-height: 1.2;
}

.row-owner-select {
  min-width: 118px;
}

.row-quality-select {
  min-width: 108px;
}

.row-status-select {
  min-width: 130px;
}

.quality-select {
  font-weight: 700;
}

.quality-select.quality-high {
  border-color: rgba(102,187,106,0.6);
  color: var(--green);
}

.quality-select.quality-medium {
  border-color: rgba(255,167,38,0.6);
  color: var(--orange);
}

.quality-select.quality-low {
  border-color: rgba(239,83,80,0.6);
  color: var(--red);
}

/* ── Buttons ── */
.btn {
  padding: 0.58rem 1.15rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: #5a7ae6; }
.btn-outline {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}
.btn-outline:hover { background: rgba(108,140,255,0.1); }
.btn-sm { padding: 0.4rem 0.68rem; font-size: 0.84rem; }
.btn-ghost {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.btn-ghost:hover { color: var(--text); border-color: var(--text-dim); }

/* ── Table ── */
.table-container {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: scroll;
  overflow-y: auto;
  max-width: 100%;
  max-height: none;
  height: 100%;
  padding: 0;
  scrollbar-gutter: stable both-edges;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  min-width: 1500px;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.85rem;
}

thead th {
  background: var(--surface2);
  padding: 0.65rem 0.75rem;
  text-align: left;
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  user-select: none;
  position: sticky;
  top: 0;
  z-index: 4;
}
thead th.sortable { cursor: pointer; }
thead th.sortable:hover { color: var(--accent); }

tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.12s ease;
}
tbody tr:nth-child(odd) { background: rgba(255,255,255,0.01); }
tbody tr:hover { background: rgba(108,140,255,0.08); }
tbody td {
  padding: 0.72rem 0.78rem;
  vertical-align: middle;
  line-height: 1.45;
}

.col-id   { width: 45px; text-align: center; color: var(--text-dim); }
.col-created { width: 110px; color: var(--text-dim); white-space: nowrap; }
.col-owner { width: 150px; }
.col-cat  { width: 140px; }
.col-company { min-width: 160px; }
.col-contact { min-width: 180px; }
.col-desc { min-width: 140px; color: var(--text-dim); }
.col-phone { min-width: 130px; }
.col-email { min-width: 160px; }
.col-linkedin { min-width: 190px; }
.col-website { min-width: 190px; }
.col-quality { width: 140px; text-align: center; }
.col-status { width: 140px; }
.col-actions { width: 155px; text-align: center; white-space: nowrap; }

/* ── Cell Styling ── */
.company-name { font-weight: 600; color: #fff; }
.contact-name { color: var(--accent2); }
.category-tag {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  background: rgba(108,140,255,0.12);
  color: var(--accent);
  border-radius: 3px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.email-link { word-break: break-all; }
.linkedin-link {
  display: inline-block;
  margin: 0.04rem 0;
  color: var(--accent2);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.linkedin-link:hover {
  color: #82d6ff;
}
.linkedin-sep {
  color: var(--text-dim);
}
.email-personal { color: var(--green); }
.email-generic  { color: var(--orange); }
.email-support  { color: var(--red); }
.phone-text { font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Quality badges */
.quality-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.15rem 0.55rem;
  border-radius: 10px;
  font-size: 0.75rem;
  font-weight: 700;
}
.q-high   { background: rgba(102,187,106,0.15); color: var(--green); }
.q-medium { background: rgba(255,167,38,0.15); color: var(--orange); }
.q-low    { background: rgba(239,83,80,0.15); color: var(--red); }

/* Status badges */
.status-badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 10px;
  font-size: 0.72rem;
  font-weight: 600;
}
.s-connected { background: rgba(102,187,106,0.15); color: var(--green); }
.s-requested { background: rgba(255,167,38,0.15); color: var(--orange); }
.s-yes       { background: rgba(79,195,247,0.15); color: var(--accent2); }
.s-followup  { background: rgba(206,147,255,0.15); color: #ce93ff; }
.s-none      { color: var(--text-dim); font-size: 0.72rem; }

/* ── Modal ── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}
.modal-overlay.active { display: flex; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 90%;
  max-width: 640px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { font-size: 1rem; font-weight: 600; }
.modal-close {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.2rem;
  cursor: pointer;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 1.25rem; }
.modal-footer {
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
  text-align: right;
}

/* Modal analysis items */
.analysis-item {
  padding: 0.6rem 0;
  border-bottom: 1px solid var(--border);
}
.analysis-item:last-child { border: none; }
.analysis-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin-bottom: 0.2rem;
}
.analysis-value { font-size: 0.95rem; }
.analysis-ok   { color: var(--green); }
.analysis-warn { color: var(--orange); }
.analysis-miss { color: var(--red); }

.issue-list { list-style: none; padding: 0; margin: 0.5rem 0 0 0; }
.issue-list li {
  padding: 0.35rem 0.5rem;
  margin-bottom: 0.25rem;
  border-radius: 4px;
  font-size: 0.85rem;
}
.issue-list li.ok   { background: rgba(102,187,106,0.1); color: var(--green); }
.issue-list li.warn { background: rgba(255,167,38,0.1); color: var(--orange); }
.issue-list li.bad  { background: rgba(239,83,80,0.1); color: var(--red); }

/* ── Login Screen ── */
.login-screen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}
.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2.5rem 2rem;
  text-align: center;
  width: 90%;
  max-width: 360px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
.login-box h1 {
  font-size: 1.4rem;
  margin-bottom: 0.3rem;
  color: var(--text);
}
.login-box p {
  color: var(--text-dim);
  margin-bottom: 1.2rem;
  font-size: 0.9rem;
}
.login-box input {
  width: 100%;
  padding: 0.65rem 0.9rem;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 0.8rem;
  outline: none;
  text-align: center;
  letter-spacing: 0.15em;
}
.login-box input:focus { border-color: var(--accent); }
.login-box .btn { width: 100%; padding: 0.6rem; font-size: 0.95rem; }
.login-error {
  color: var(--red);
  font-size: 0.85rem;
  margin-top: 0.5rem;
  min-height: 1.2em;
}

/* ── Editable cells ── */
td[contenteditable="true"] {
  cursor: text;
  outline: none;
  border-radius: 3px;
  transition: background 0.15s, box-shadow 0.15s;
}
td[contenteditable="true"]:hover {
  background: rgba(108,140,255,0.06);
}
td[contenteditable="true"]:focus {
  background: rgba(108,140,255,0.12);
  box-shadow: inset 0 0 0 1px var(--accent);
}
td.cell-dirty {
  position: relative;
}
td.cell-dirty::after {
  content: '';
  position: absolute;
  top: 4px;
  right: 4px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--orange);
}

/* Save indicator */
.save-toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--green);
  color: #000;
  padding: 0.6rem 1.2rem;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: 0.85rem;
  z-index: 2000;
  opacity: 0;
  transform: translateY(10px);
  transition: all 0.3s;
}
.save-toast.show {
  opacity: 1;
  transform: translateY(0);
}

.ai-log {
  max-height: 180px;
  overflow-y: auto;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem;
}

.ai-log-line {
  font-size: 0.82rem;
  color: var(--text-dim);
  padding: 0.22rem 0;
  border-bottom: 1px dashed rgba(255,255,255,0.08);
}

.ai-log-line:last-child {
  border-bottom: none;
}

.ai-compare-wrap {
  overflow-x: auto;
}

.ai-compare-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.ai-compare-table th,
.ai-compare-table td {
  border: 1px solid var(--border);
  padding: 0.45rem;
  vertical-align: top;
}

.ai-compare-table th {
  background: rgba(108,140,255,0.08);
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.7rem;
}

.ai-keep-toggle {
  transform: scale(1.15);
}

/* Delete row button */
.btn-delete {
  background: transparent;
  border: 1px solid rgba(239,83,80,0.3);
  color: var(--red);
  padding: 0.35rem 0.62rem;
  font-size: 0.84rem;
  border-radius: 3px;
  cursor: pointer;
  margin-left: 0.3rem;
}
.btn-delete:hover { background: rgba(239,83,80,0.15); }

.btn-ai {
  background: rgba(79,195,247,0.15);
  border: 1px solid rgba(79,195,247,0.45);
  color: var(--accent2);
  padding: 0.35rem 0.62rem;
  font-size: 0.84rem;
  border-radius: 3px;
  cursor: pointer;
  margin-left: 0.3rem;
}

.btn-ai:hover {
  background: rgba(79,195,247,0.22);
}

.btn-ai.is-loading {
  opacity: 0.6;
  pointer-events: none;
}

.col-actions .btn-sm,
.col-actions .btn-ai,
.col-actions .btn-delete {
  min-width: 34px;
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ── Responsive ── */
@media (max-width: 1400px) {
  .crm-header,
  .toolbar {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  .header-stats {
    margin-left: 0;
  }
}

@media (max-width: 1080px) {
  html { font-size: 13px; }

  .toolbar-left,
  .toolbar-right {
    width: 100%;
  }

  .toolbar-right {
    justify-content: flex-start;
  }

  #searchInput {
    min-width: 0;
  }

  table {
    min-width: 1280px;
  }
}

@media (max-width: 768px) {
  .crm-header {
    gap: 0.65rem;
    padding-top: 0.6rem;
    padding-bottom: 0.6rem;
  }

  .crm-header h1 {
    order: 3;
    flex-basis: 100%;
    font-size: 0.95rem;
  }

  .header-stats {
    width: 100%;
    justify-content: flex-start;
  }

  .toolbar {
    gap: 0.65rem;
  }

  .toolbar-right .btn {
    flex: 1 1 48%;
    text-align: center;
  }

  table {
    min-width: 1100px;
  }

  .col-desc,
  .col-cat,
  .col-status,
  .col-comments {
    display: none;
  }
}

@media (max-width: 520px) {
  .login-box {
    padding: 2rem 1.25rem;
  }

  .header-stats .stat {
    font-size: 0.78rem;
    padding: 0.2rem 0.45rem;
  }

  .toolbar-right .btn {
    flex-basis: 100%;
  }

  table {
    min-width: 980px;
  }
}
