:root {
  --bg: #080c11;
  --panel: #121922;
  --panel-2: #0f1820;
  --text: #e5e7eb;
  --text-dim: #9ca3af;
  --accent: #f7c948;
  --border: rgba(255, 255, 255, 0.12);
  --shadow: 0 12px 28px rgba(0, 0, 0, 0.28), inset 0 0 0 1px rgba(255, 255, 255, 0.03);
  --radius: 14px;
  --danger: #f87171;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding: 32px 16px 60px;
}

.contacts-container {
  max-width: 1280px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.contacts-header {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 24px;
  box-shadow: var(--shadow);
}

.header-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.back-link,
.settings-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--accent);
  text-decoration: none;
  font-weight: 600;
}

.contacts-header h1 {
  margin: 18px 0 6px;
  font-size: 28px;
}

.contacts-header h1 .lucide {
  width: 20px;
  height: 20px;
  margin-right: 8px;
  vertical-align: -3px;
}

.contacts-header p {
  margin: 0;
  color: var(--text-dim);
}

.contacts-section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 24px 24px;
  box-shadow: var(--shadow);
  --contact-grid:
    minmax(160px, 1.1fr)
    minmax(120px, 0.9fr)
    minmax(120px, 0.9fr)
    minmax(0, 1.6fr)
    minmax(0, 0.9fr)
    minmax(0, 1fr)
    minmax(0, 1.1fr)
    minmax(160px, 0.9fr);
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.section-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  justify-content: flex-end;
}

.section-title h2 {
  margin: 0;
  font-size: 20px;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text-dim);
}

input[type="text"],
input[type="tel"],
input[type="search"],
textarea {
  width: 100%;
  background: #0e1821;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  outline: none;
}

textarea {
  min-height: 60px;
  resize: vertical;
}

input:focus,
textarea:focus {
  border-color: rgba(247, 201, 72, 0.5);
  box-shadow: 0 0 0 3px rgba(247, 201, 72, 0.15);
}

.form-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #17222c;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #080c11;
}

.btn-secondary {
  background: var(--panel-2);
}

.btn-danger {
  background: #612e2e;
  border-color: rgba(239, 68, 68, 0.45);
  color: #ffe3e3;
}

.status {
  font-size: 13px;
  color: var(--text-dim);
}

.status.ok {
  color: #22c55e;
}

.status.error {
  color: var(--danger);
}

.search-input {
  width: min(320px, 100%);
}

.section-actions .search-input {
  flex: 1;
}

.sort-toggle {
  display: inline-flex;
  align-items: center;
  padding: 4px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  gap: 4px;
}

.sort-option {
  border: none;
  background: transparent;
  color: var(--text-dim);
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.sort-option.is-active {
  background: var(--accent);
  color: #080c11;
}

.sort-option:focus-visible {
  outline: 2px solid rgba(247, 201, 72, 0.7);
  outline-offset: 2px;
}

.contacts-list {
  display: flex;
  flex-direction: column;
  gap: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--panel-2);
}

.contact-row.contact-header {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
}

.contact-row.contact-header .contact-cell {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-row {
  background: transparent;
  border-bottom: 1px solid var(--border);
  padding: 8px 12px;
  display: grid;
  grid-template-columns: var(--contact-grid);
  gap: 10px;
  align-items: center;
}

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

.contact-row.contact-empty {
  grid-template-columns: 1fr;
  justify-items: center;
  color: var(--text-dim);
  padding: 14px 12px;
}

.contact-cell {
  font-size: 13px;
  color: var(--text-dim);
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-name {
  color: var(--accent);
  font-weight: 700;
  font-size: 14px;
}

.contact-passages {
  cursor: pointer;
  color: var(--text);
  font-weight: 600;
}

.contact-passages:hover {
  color: var(--accent);
}

.contact-passages:focus-visible {
  outline: 2px solid rgba(247, 201, 72, 0.7);
  outline-offset: 2px;
  border-radius: 6px;
}

.contact-notes {
  white-space: normal;
  overflow: visible;
  text-overflow: unset;
  word-break: break-word;
  align-self: center;
}

.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(8, 12, 17, 0.85);
  z-index: 1000;
}

.modal-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: min(520px, calc(100% - 32px));
  max-height: 80vh;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.modal-close {
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 10px;
  width: 32px;
  height: 32px;
  cursor: pointer;
}

.modal-close .lucide {
  width: 16px;
  height: 16px;
}

.modal-subtitle {
  margin: 0;
  color: var(--text-dim);
  font-size: 13px;
}

.history-list {
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 12px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 50vh;
  overflow-y: auto;
}

.history-item {
  background: #0e1821;
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  color: var(--text);
}

.history-item.is-muted {
  background: transparent;
  border: none;
  color: var(--text-dim);
}

.contact-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 0;
}

.contact-actions .btn {
  padding: 6px 10px;
  font-size: 12px;
}

@media (max-width: 720px) {
  .contacts-header,
  .contacts-section {
    padding: 18px;
  }

  .field-grid {
    grid-template-columns: 1fr;
  }

  .section-title {
    flex-direction: column;
    align-items: flex-start;
  }

  .section-actions {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
  }

  .sort-toggle {
    align-self: flex-start;
  }

  .form-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .contact-row {
    grid-template-columns: 1fr;
    row-gap: 6px;
  }

  .contact-row.contact-header {
    display: none;
  }

  .contact-actions {
    justify-content: flex-start;
  }
}
