*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg:         #0e0e10;
  --card:       #17171a;
  --input-bg:   #1c1c1f;
  --border:     rgba(255, 255, 255, 0.06);
  --border-mid: rgba(255, 255, 255, 0.08);
  --text:       #f1f1f3;
  --hint:       rgba(255, 255, 255, 0.55);
  --hint-dim:   rgba(255, 255, 255, 0.4);
  --accent:     #6c47ff;
  --accent-2:   #9b7aff;
  --shadow-fab: 0 8px 24px rgba(108, 71, 255, 0.4);
  --shadow-btn: 0 4px 14px rgba(108, 71, 255, 0.35);
  --shadow-sheet: 0 -8px 40px rgba(0, 0, 0, 0.6);
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  letter-spacing: -0.1px;
}

/* ---- Screens ---- */

.screen {
  display: none;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  position: absolute;
  inset: 0;
}

.screen.active { display: flex; }

/* ---- List header ---- */

.list-header {
  padding: 18px 20px 14px;
  flex-shrink: 0;
}

.list-header h1 {
  font-size: 28px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.6px;
  margin-bottom: 14px;
}

.search-bar {
  background: var(--input-bg);
  border-radius: 14px;
  padding: 11px 16px;
  display: flex;
  align-items: center;
  gap: 9px;
  border: 1px solid var(--border-mid);
}

.search-icon {
  font-size: 15px;
  opacity: 0.35;
  flex-shrink: 0;
}

.search-bar input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  font-size: 15px;
  font-family: inherit;
  color: var(--text);
  min-width: 0;
}

.search-bar input::placeholder {
  color: var(--text);
  opacity: 0.4;
}

/* ---- Detail header ---- */

header {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  gap: 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  min-height: 56px;
  flex-shrink: 0;
}

.btn-icon {
  background: rgba(255, 255, 255, 0.07);
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--text);
  padding: 0;
  border-radius: 12px;
  line-height: 1;
  min-width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s;
  flex-shrink: 0;
  letter-spacing: 1px;
}

.btn-icon:active { background: rgba(255, 255, 255, 0.13); }

.btn-delete { color: var(--hint); }

.contact-header {
  flex: 1;
  min-width: 0;
}

.detail-name {
  font-size: 17px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.3px;
  color: var(--text);
}

.detail-sub {
  font-size: 12px;
  color: var(--hint);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- Hero card ---- */

.hero-card {
  margin: 14px 16px 0;
  background: var(--card);
  border-radius: 20px;
  padding: 16px 18px;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.hero-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 14px;
}

.hero-avatar {
  width: 60px;
  height: 60px;
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
  overflow: hidden;
}

.hero-name {
  font-size: 19px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.4px;
}

.hero-handle {
  font-size: 14px;
  color: var(--accent-2);
  margin-top: 3px;
  font-weight: 500;
}

.stats-row {
  display: flex;
  gap: 8px;
}

.stat-pill {
  flex: 1;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 12px;
  padding: 9px 10px;
  text-align: center;
  border: 1px solid var(--border);
}

.stat-pill.strength-high {
  background: rgba(34, 197, 94, 0.06);
  border-color: rgba(34, 197, 94, 0.2);
}
.stat-pill.strength-med {
  background: rgba(234, 179, 8, 0.06);
  border-color: rgba(234, 179, 8, 0.2);
}
.stat-pill.strength-low {
  background: rgba(239, 68, 68, 0.06);
  border-color: rgba(239, 68, 68, 0.2);
}

.stat-value {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.3px;
}
.stat-value.strength-high { color: #4ade80; }
.stat-value.strength-med  { color: #fbbf24; }
.stat-value.strength-low  { color: #f87171; }

.stat-label {
  font-size: 10px;
  color: var(--text);
  opacity: 0.45;
  margin-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}

/* ---- Contact list ---- */

.contacts-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 8px;
  padding: 40px 20px;
  text-align: center;
}

.empty-state p {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  opacity: 0.4;
}

.empty-state .hint {
  font-size: 14px;
  font-weight: 400;
  color: var(--hint);
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  gap: 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
  user-select: none;
}

.contact-item:active { background: rgba(255, 255, 255, 0.03); }

.contact-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 19px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
  overflow: hidden;
}

.contact-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

/* Avatar gradient palette — assigned deterministically by name */
.av-0 { background: linear-gradient(135deg, #6c47ff, #9b7aff); }
.av-1 { background: linear-gradient(135deg, #0ea5e9, #38bdf8); }
.av-2 { background: linear-gradient(135deg, #f97316, #fb923c); }
.av-3 { background: linear-gradient(135deg, #22c55e, #4ade80); }
.av-4 { background: linear-gradient(135deg, #ec4899, #f472b6); }
.av-5 { background: linear-gradient(135deg, #eab308, #fbbf24); }

.contact-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.contact-name {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.2px;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
}

.badge {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 6px;
  letter-spacing: 0.02em;
  flex-shrink: 0;
  white-space: nowrap;
}
.badge-high { background: rgba(34, 197, 94, 0.15);  color: #4ade80; }
.badge-med  { background: rgba(234, 179, 8, 0.15);   color: #fbbf24; }
.badge-low  { background: rgba(239, 68, 68, 0.15);   color: #f87171; }

.contact-bio {
  font-size: 12px;
  color: var(--hint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.35;
  opacity: 0.7;
}

.contact-preview {
  font-size: 13px;
  color: var(--hint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.35;
}

.contact-reason {
  font-size: 12px;
  color: #9b7aff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.35;
}

.contact-time {
  font-size: 11px;
  color: var(--hint-dim);
  flex-shrink: 0;
  align-self: flex-start;
  margin-top: 4px;
  white-space: nowrap;
}

/* ---- FAB ---- */

.fab {
  position: fixed;
  bottom: 24px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border: none;
  font-size: 30px;
  cursor: pointer;
  box-shadow: var(--shadow-fab);
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  padding-bottom: 2px;
  transition: all 0.15s ease;
  z-index: 50;
  font-weight: 300;
}

.fab:active {
  opacity: 0.85;
  transform: scale(0.94);
}

/* ---- Hero bio ---- */

.hero-bio {
  width: 100%;
  background: none;
  border: none;
  border-top: 1px solid var(--border);
  outline: none;
  font-size: 14px;
  font-family: inherit;
  color: var(--hint);
  resize: none;
  overflow: hidden;
  line-height: 1.5;
  padding: 10px 0 2px;
  margin-top: 10px;
  max-height: 80px;
  transition: color 0.15s;
}

.hero-bio:empty,
.hero-bio[data-empty="true"] {
  border-top-color: transparent;
  margin-top: 0;
  padding-top: 0;
}

.hero-bio:focus { color: var(--text); }
.hero-bio::placeholder { color: var(--text); opacity: 0.2; }

/* ---- Avatar wrap + camera button ---- */

.hero-avatar-wrap {
  position: relative;
  flex-shrink: 0;
}

.avatar-camera-btn {
  position: absolute;
  bottom: -4px;
  right: -4px;
  width: 22px;
  height: 22px;
  background: var(--card);
  border: 1.5px solid var(--border-mid);
  border-radius: 8px;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  line-height: 1;
  pointer-events: none; /* let the input capture taps */
}

#avatar-file-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
}

.hero-avatar img {
  width: 100%;
  height: 100%;
  border-radius: inherit;
  object-fit: cover;
}

.hero-text { flex: 1; min-width: 0; }

/* ---- Edit / danger button ---- */

.btn-danger {
  width: 100%;
  background: rgba(239, 68, 68, 0.12);
  color: #f87171;
  border: 1px solid rgba(239, 68, 68, 0.2);
  border-radius: 18px;
  padding: 14px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 10px;
  transition: all 0.12s;
}

.btn-danger:active { opacity: 0.8; }

/* ---- Notes list ---- */

.notes-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 16px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 8px;
}

.note-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 11px 15px;
  max-width: 100%;
}

.note-item.note-latest {
  background: rgba(108, 71, 255, 0.1);
  border-color: rgba(108, 71, 255, 0.25);
}

.note-text {
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
}

.note-time {
  font-size: 11px;
  color: var(--hint-dim);
  margin-top: 5px;
  text-align: right;
}

.note-latest .note-time {
  color: rgba(108, 71, 255, 0.8);
}

.empty-notes {
  text-align: center;
  color: var(--hint);
  font-size: 15px;
  font-weight: 500;
  margin-top: 60px;
  opacity: 0.6;
}

/* ---- Input bar ---- */

.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 14px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.input-bar textarea {
  flex: 1;
  background: var(--input-bg);
  border: 1px solid var(--border-mid);
  border-radius: 20px;
  padding: 10px 16px;
  font-size: 15px;
  font-family: inherit;
  color: var(--text);
  resize: none;
  max-height: 120px;
  line-height: 1.45;
  outline: none;
  transition: border-color 0.15s;
}

.input-bar textarea::placeholder { color: var(--hint); }

.input-bar textarea:focus {
  border-color: rgba(108, 71, 255, 0.5);
}

.btn-send {
  width: 40px;
  height: 40px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border: none;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  box-shadow: var(--shadow-btn);
  transition: all 0.12s;
  font-weight: 700;
}

.btn-send:active {
  opacity: 0.8;
  transform: scale(0.93);
}

/* ---- Modal ---- */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 100;
  backdrop-filter: blur(4px);
}

.modal-overlay.open { display: block; }

.modal {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 101;
  transform: translateY(100%);
  transition: transform 0.28s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.modal.open { transform: translateY(0); }

.modal-sheet {
  background: var(--card);
  border-radius: 28px 28px 0 0;
  padding: 14px max(20px, env(safe-area-inset-right)) max(32px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  max-height: calc(var(--tg-viewport-height, 90vh) - 20px);
  overflow-y: auto;
  gap: 0;
  box-shadow: var(--shadow-sheet);
  border-top: 1px solid var(--border-mid);
}

.modal-handle {
  width: 40px;
  height: 4px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
  margin: 0 auto 20px;
}

.modal-sheet h2 {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: -0.4px;
  color: var(--text);
  margin-bottom: 18px;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
}

.field-wrap {
  background: var(--bg);
  border: 1px solid var(--border-mid);
  border-radius: 16px;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field-wrap:focus-within {
  border-color: rgba(108, 71, 255, 0.6);
  box-shadow: 0 0 0 3px rgba(108, 71, 255, 0.12);
}

.field-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text);
  opacity: 0.45;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.field-wrap input,
.field-wrap textarea {
  background: none;
  border: none;
  outline: none;
  font-size: 16px;
  font-family: inherit;
  color: var(--text);
  width: 100%;
  resize: none;
  line-height: 1.45;
  padding: 0;
}

.field-wrap input::placeholder,
.field-wrap textarea::placeholder { color: var(--hint); opacity: 0.6; }

.field-wrap textarea { min-height: 60px; }

.modal-sheet #btn-save-edit {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border: none;
  border-radius: 18px;
  padding: 16px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  width: 100%;
  letter-spacing: -0.1px;
  box-shadow: var(--shadow-fab);
  transition: all 0.12s;
}

.modal-sheet #btn-save-edit:active {
  opacity: 0.85;
  transform: scale(0.98);
}

.modal-sheet #btn-save-contact {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border: none;
  border-radius: 18px;
  padding: 16px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  width: 100%;
  letter-spacing: -0.1px;
  box-shadow: var(--shadow-fab);
  transition: all 0.12s;
}

.modal-sheet #btn-save-contact:active {
  opacity: 0.85;
  transform: scale(0.98);
}

/* ---- Toast error ---- */

#toast-error {
  position: fixed;
  top: 20px;
  left: 16px;
  right: 16px;
  background: #d93025;
  color: #fff;
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 600;
  z-index: 999;
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}

#toast-error.visible {
  opacity: 1;
  transform: translateY(0);
}
