:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --text: #1c2333;
  --text-muted: #6b7280;
  --primary: #1e88e5;
  --primary-dark: #1565c0;
  --danger: #e53935;
  --border: #dde3ec;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-md: 0 10px 25px rgba(16, 24, 40, 0.16);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--primary);
  color: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}

.header-inner {
  max-width: 680px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.header-user {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.user-email {
  font-size: 0.85rem;
  opacity: 0.9;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: none;
}

.btn-light {
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
}
.btn-light:hover { background: rgba(255, 255, 255, 0.26); }

.logo {
  font-size: 1.15rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}

/* ---------- Auth view ---------- */
.auth-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border-bg);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  padding: 28px 24px;
  margin: auto;
}

.auth-logo {
  font-size: 1.4rem;
  font-weight: 700;
  margin: 0 0 4px;
  color: var(--primary);
}

.auth-sub {
  margin: 0 0 18px;
  color: var(--text-muted);
  font-size: 0.92rem;
}

.auth-tabs {
  display: flex;
  background: #eff2f7;
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 18px;
  gap: 4px;
}

.auth-tab {
  flex: 1;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 8px 0;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.auth-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.auth-error {
  margin: 4px 0 12px;
  padding: 10px 12px;
  border-radius: 8px;
  background: #fdecea;
  color: #b71c1c;
  font-size: 0.88rem;
}

.btn-block {
  width: 100%;
  margin-top: 6px;
}

.auth-switch {
  display: block;
  margin: 16px auto 0;
  background: none;
  border: none;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
}
.auth-switch strong { color: var(--primary); }

/* ---------- Layout ---------- */
.container {
  max-width: 680px;
  margin: 0 auto;
  padding: 16px;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.05s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn:active { transform: scale(0.97); }

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

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover { background: #d32f2f; }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover { background: rgba(0, 0, 0, 0.05); color: var(--text); }

.btn-icon {
  border-radius: 12px;
  padding: 9px 14px;
}
.btn .btn-no-padding { padding: 0; }

.icon-btn {
  padding: 6px;
  border-radius: 8px;
  display: inline-flex;
}

/* ---------- Toolbar / search ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.search-wrap {
  position: relative;
  flex: 1;
}

.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

#searchInput {
  width: 100%;
  padding: 11px 14px 11px 38px;
  font-family: inherit;
  font-size: 0.95rem;
  border: 1px solid var(--border-bg);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

#searchInput:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(30, 136, 229, 0.15);
}

.count {
  font-size: 0.85rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ---------- Status ---------- */
.status {
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 0.9rem;
  margin-bottom: 12px;
  display: none;
}
.status.show { display: block; }
.status.success { background: #e8f5e9; color: #1b5e20; }
.status.error { background: #fdecea; color: #b71c1c; }

/* ---------- Contact list ---------- */
.contact-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.contact-card {
  background: var(--surface);
  border: 1px solid var(--border-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  transition: border-color 0.15s ease;
}

.contact-card:hover { border-color: #c3d4ee; }

.avatar {
  width: 42px;
  height: 42px;
  min-width: 42px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1rem;
  text-transform: uppercase;
}

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

.contact-name {
  font-weight: 600;
  font-size: 1rem;
  margin: 0 0 2px;
  word-break: break-word;
}

.contact-meta {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 0.88rem;
  color: var(--text-muted);
}

.contact-meta li {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  margin-top: 2px;
  word-break: break-word;
}

.contact-date {
  display: block;
  margin-top: 6px;
  font-size: 0.78rem;
  color: #9aa3b0;
}

.contact-actions {
  display: flex;
  gap: 4px;
}

.no-padding { padding: 0; }

/* ---------- Empty state ---------- */
.empty-state {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-muted);
}
.empty-state .empty-icon { font-size: 2.6rem; }
.empty-state p { font-weight: 600; color: var(--text); margin: 10px 0 4px; }
.empty-state span { font-size: 0.9rem; }

/* ---------- Modal ---------- */
/* Modals are CLOSED by default (display:none). They become visible only via
   the .open class added by JS. No reliance on the hidden attribute alone,
   so a mismatch can never leave a modal blocking the page on load. */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal.open {
  display: flex;
}

/* Defensive: keep hidden attribute respected regardless of other rules */
.modal[hidden] {
  display: none !important;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
}

.modal-card {
  position: relative;
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  padding: 20px;
  max-height: 90vh;
  overflow-y: auto;
  animation: slideUp 0.2s ease;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.modal-head h2 { font-size: 1.15rem; margin: 0; }

/* ---------- Form ---------- */
.field { display: block; margin-bottom: 14px; }
.field-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}
.field-label em { color: var(--danger); font-style: normal; }

.field input,
.field textarea {
  width: 100%;
  font-family: inherit;
  font-size: 0.95rem;
  padding: 11px 12px;
  border: 1px solid var(--border-bg);
  border-radius: 10px;
  background: #fbfcfe;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  resize: vertical;
}

.field input:focus,
.field textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(30, 136, 229, 0.15);
  background: #fff;
}

/* Show/hide password toggle */
.password-wrap {
  position: relative;
  display: block;
}
.password-wrap input {
  padding-right: 46px;
}
.password-toggle {
  position: absolute;
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.password-toggle:hover {
  background: rgba(0, 0, 0, 0.05);
  color: var(--text);
}
.password-toggle:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}
.password-toggle svg { pointer-events: none; }
.password-toggle .off-line { display: none; }
.password-toggle.is-visible .off-line { display: block; }

.field.invalid input,
.field.invalid textarea {
  border-color: var(--danger);
}

.field-error {
  display: block;
  margin-top: 4px;
  font-size: 0.8rem;
  color: var(--danger);
  min-height: 1em;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

.confirm-text { margin: 0 0 4px; color: var(--text-muted); }

/* ---------- Desktop tweaks ---------- */
@media (min-width: 640px) {
  .container { padding: 24px 16px; }
  .header-inner { padding: 14px 16px; }
  .logo { font-size: 1.25rem; }
  .user-email { display: inline; }
}

/* ---------- Animations ---------- */
.contact-card {
  animation: fadeIn 0.2s ease;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}