*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --border: #e2e8f0;
  --sidebar-bg: #1e293b;
  --sidebar-text: #94a3b8;
  --sidebar-active: #ffffff;
  --sidebar-active-bg: #334155;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --text: #1e293b;
  --text-muted: #64748b;
  --error: #ef4444;
  --success: #22c55e;
  --input-bg: #f8fafc;
  --radius: 8px;
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* ── Auth page ─────────────────────────────────────────── */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 4px 24px rgba(0,0,0,.06);
}

.auth-card h1 {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

.auth-card .subtitle {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 1.75rem;
}

/* ── Layout ────────────────────────────────────────────── */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* ── Sidebar ───────────────────────────────────────────── */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  padding: 1.5rem 0;
}

.sidebar-logo {
  color: #fff;
  font-weight: 700;
  font-size: 1.1rem;
  padding: 0 1.25rem 1.5rem;
  border-bottom: 1px solid #334155;
  margin-bottom: 1rem;
}

.sidebar-section {
  padding: 0 0.75rem;
  margin-bottom: 0.25rem;
}

.sidebar-label {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: #475569;
  padding: 0.5rem 0.5rem 0.25rem;
}

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  color: var(--sidebar-text);
  text-decoration: none;
  font-size: 0.875rem;
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  transition: background .15s, color .15s;
}

.sidebar-item:hover,
.sidebar-item.active {
  background: var(--sidebar-active-bg);
  color: var(--sidebar-active);
}

.sidebar-bottom {
  margin-top: auto;
  padding: 0 0.75rem;
  border-top: 1px solid #334155;
  padding-top: 1rem;
}

.sidebar-bottom form { width: 100%; }

/* ── Main content ──────────────────────────────────────── */
.main-content {
  flex: 1;
  padding: 2rem;
  overflow-y: auto;
}

.page-title {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 1.5rem;
}

/* ── Card / Section ────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1.25rem;
}

.card-title {
  font-size: 0.9rem;
  font-weight: 600;
  margin-bottom: 1rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .05em;
  font-size: 0.75rem;
}

/* ── Form ──────────────────────────────────────────────── */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.form-group.full-width {
  grid-column: 1 / -1;
}

label {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text);
}

label .hint {
  font-weight: 400;
  color: var(--text-muted);
  font-size: 0.75rem;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
select {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  color: var(--text);
  width: 100%;
  transition: border-color .15s, box-shadow .15s;
}

input[type="text"]:focus,
input[type="email"]:focus,
input[type="password"]:focus,
input[type="number"]:focus,
select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,.15);
}

input[readonly] {
  background: #f1f5f9;
  color: var(--text-muted);
  cursor: default;
}

/* ── Buttons ───────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition: background .15s, opacity .15s;
  text-decoration: none;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary {
  background: #e2e8f0;
  color: var(--text);
}

.btn-secondary:hover { background: #cbd5e1; }

.btn-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-outline:hover { background: var(--bg); }

.btn-sm {
  padding: 0.375rem 0.75rem;
  font-size: 0.8125rem;
}

.btn[disabled], .btn.htmx-request {
  opacity: .6;
  cursor: not-allowed;
}

/* ── Flash messages ────────────────────────────────────── */
.flash {
  padding: 0.75rem 1rem;
  border-radius: 6px;
  font-size: 0.875rem;
  margin-bottom: 1rem;
}

.flash-success {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #166534;
}

.flash-error {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #991b1b;
}

/* ── Actions row ───────────────────────────────────────── */
.actions-row {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-top: 0.5rem;
}

/* ── HTMX indicator ────────────────────────────────────── */
.htmx-indicator {
  display: none;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
  display: inline-block;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Readonly section ──────────────────────────────────── */
.readonly-section {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.25rem;
}

.readonly-section .card-title { margin-bottom: 1rem; }

/* ── Source rows ───────────────────────────────────────── */
.source-row {
  padding: 1rem 0;
  border-bottom: 1px solid var(--border);
}

.source-row:last-child { border-bottom: none; }

.source-row-grid {
  display: grid;
  grid-template-columns: 2fr 2fr 2fr 1.5fr auto;
  gap: 0.75rem;
  align-items: flex-end;
}

.source-row-delete {
  display: flex;
  align-items: flex-end;
  padding-bottom: 1px;
}

.sources-empty {
  color: var(--text-muted);
  font-size: 0.875rem;
  padding: 0.5rem 0;
}

/* ── Integrations hub ──────────────────────────────────── */
.integrations-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1.25rem;
}

.integration-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding: 2rem 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color .15s, box-shadow .15s;
  text-decoration: none;
  color: var(--text);
  font-family: inherit;
  font-size: inherit;
}

.integration-card:hover:not([disabled]) {
  border-color: var(--primary);
  box-shadow: 0 2px 12px rgba(59,130,246,.12);
}

.integration-card[disabled] {
  opacity: .5;
  cursor: not-allowed;
}

.integration-card-icon {
  font-size: 2rem;
}

.integration-card-title {
  font-weight: 600;
  font-size: 0.9375rem;
}

.integration-card-soon {
  font-size: 0.75rem;
  color: var(--text-muted);
  background: var(--bg);
  padding: 0.125rem 0.5rem;
  border-radius: 4px;
}

/* ── Company list ─────────────────────────────────────── */
.company-list {
  width: 100%;
  border-collapse: collapse;
}
.company-list th {
  padding: 0.75rem 1rem;
  font-weight: 600;
  font-size: 0.8125rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  border-bottom: 2px solid var(--border);
  text-align: left;
}
.company-list td {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.company-list tr:last-child td {
  border-bottom: none;
}
.company-list tr:hover {
  background: var(--bg);
}
.company-link {
  color: var(--text);
  text-decoration: none;
  font-weight: 500;
}
.company-link:hover {
  color: var(--primary);
}

/* ── Autocomplete ─────────────────────────────────────── */
.ac-wrap {
  position: relative;
}

.ac-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 50;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 6px 6px;
  max-height: 260px;
  overflow-y: auto;
  box-shadow: 0 4px 16px rgba(0,0,0,.1);
  display: none;
}

.ac-dropdown.open {
  display: block;
}

.ac-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  font-size: 0.875rem;
  transition: background .1s;
}

.ac-item:hover {
  background: var(--bg);
}

.ac-item-type {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: 0.1rem 0.375rem;
  border-radius: 3px;
  flex-shrink: 0;
}

.ac-item-type.company {
  background: #dbeafe;
  color: #1d4ed8;
}

.ac-item-type.contact {
  background: #fef3c7;
  color: #92400e;
}

.ac-hint {
  padding: 0.5rem 0.75rem;
  color: var(--text-muted);
  font-size: 0.8125rem;
}

.ac-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
}

.ac-chip .ac-item-type {
  font-size: 0.65rem;
}

.ac-chip-remove {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.1rem;
  color: var(--text-muted);
  padding: 0;
  line-height: 1;
  transition: color .15s;
}

.ac-chip-remove:hover {
  color: var(--error);
}

/* ── Connection status ─────────────────────────────────── */
.connection-status {
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 500;
}

.status-ok {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #166534;
}

.status-fail {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #991b1b;
}

/* ── Responsive ────────────────────────────────────────── */
@media (max-width: 768px) {
  .form-grid { grid-template-columns: 1fr; }
  .source-row-grid { grid-template-columns: 1fr; }
  .integrations-grid { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}
