:root {
  --bg: #f8f9fa;
  --surface: #fff;
  --border: #dee2e6;
  --text: #212529;
  --text-light: #495057;
  --muted: #6c757d;
  --primary: #0d6efd;
  --primary-dark: #0b5ed7;
  --primary-light: #e7f1ff;
  --danger: #dc3545;
  --success: #198754;
  --warning: #ffc107;
  --radius: 8px;
  --radius-lg: 16px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.1);
  --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.12);
  --transition: 0.2s ease;
}

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

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

.container { max-width: 960px; margin: 0 auto; padding: 0 1.5rem; }

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

/* ========== Navbar ========== */

.navbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 0;
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(8px);
  background: rgba(255, 255, 255, 0.95);
}
.navbar-inner { display: flex; align-items: center; gap: 1rem; }
.brand { font-weight: 800; font-size: 1.15rem; color: var(--text); text-decoration: none; letter-spacing: -0.02em; }
.brand:hover { text-decoration: none; color: var(--primary); }
.nav-links { display: flex; gap: 0.25rem; margin-left: 1rem; }
.nav-link { padding: 0.4rem 0.75rem; border-radius: var(--radius); color: var(--text-light); font-size: 0.9rem; font-weight: 500; transition: background var(--transition), color var(--transition); text-decoration: none; }
.nav-link:hover { background: var(--bg); color: var(--text); text-decoration: none; }
.nav-user { margin-left: auto; color: var(--muted); font-size: 0.9rem; font-weight: 500; }
.nav-actions { margin-left: auto; display: flex; gap: 0.5rem; }

/* ========== Flash Messages ========== */

.flash { padding: 0.75rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; font-size: 0.9rem; }
.flash-notice { background: #d1e7dd; color: #0f5132; }
.flash-alert { background: #f8d7da; color: #842029; }

/* ========== Buttons ========== */

.btn {
  display: inline-block;
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  line-height: 1.5;
}
.btn:hover { background: var(--bg); text-decoration: none; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { background: #bb2d3b; }
.btn-sm { padding: 0.3rem 0.6rem; font-size: 0.8rem; }
.btn-block { width: 100%; text-align: center; padding: 0.75rem 1rem; font-size: 1rem; }

/* ========== Cards ========== */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}
.card h2 { font-size: 1.1rem; margin-bottom: 0.5rem; }
.card p { color: var(--text-light); font-size: 0.95rem; }

/* ========== Forms ========== */

.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-weight: 600; margin-bottom: 0.3rem; font-size: 0.9rem; }
.form-group input, .form-group textarea, .form-group select {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-family: inherit;
  transition: border-color var(--transition), box-shadow var(--transition);
}
.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}
.form-group input::placeholder { color: var(--muted); }
.form-group textarea { min-height: 200px; font-family: monospace; }

.actions { display: flex; gap: 0.5rem; margin-top: 1rem; flex-wrap: wrap; }

/* ========== Auth Forms (Login/Register) ========== */

.auth-form {
  max-width: 420px;
  margin: 3rem auto;
  padding: 0 1.5rem;
}
.auth-form h1 { font-size: 1.75rem; font-weight: 700; margin-bottom: 0.25rem; }
.auth-subtitle { color: var(--muted); margin-bottom: 1.5rem; }
.auth-footer { text-align: center; color: var(--muted); font-size: 0.9rem; margin-top: 1.5rem; }

/* ========== Dashboard / App Pages ========== */

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}
.page-header h1 { font-size: 1.5rem; }

.contact-list { list-style: none; }
.contact-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.contact-item:last-child { border-bottom: none; }
.contact-name { font-weight: 600; }
.contact-meta { color: var(--muted); font-size: 0.85rem; }

.detail-section { margin-bottom: 1.75rem; padding-bottom: 1.75rem; border-bottom: 1px solid var(--border); }
.detail-section:last-of-type { border-bottom: none; padding-bottom: 0; }
.detail-section h3 { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 0.5rem; }

.dav-url {
  background: var(--bg);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius);
  font-family: monospace;
  font-size: 0.85rem;
  word-break: break-all;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.dav-url span { flex: 1; }

.copy-btn, .action-link {
  display: inline-flex;
  align-items: center;
  background: none;
  border: none;
  padding: 0.15rem;
  cursor: pointer;
  color: var(--muted);
  vertical-align: middle;
  text-decoration: none;
  flex-shrink: 0;
}

.copy-btn:hover, .action-link:hover { color: var(--text); }
.copy-btn.copied { color: #198754; }
.copy-btn svg, .action-link svg { width: 15px; height: 15px; }

.detail-value {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.25rem;
}

.detail-value span { word-break: break-word; }

pre.raw-vcard {
  background: var(--bg);
  padding: 1rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

.text-muted { color: var(--muted); font-size: 0.9rem; }

/* ========== Import Form ========== */
.import-form { display: flex; flex-direction: column; gap: 0.75rem; }
.import-form input[type="file"] {
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
}

/* ========== Subscription Status ========== */

.subscription-status { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.75rem; }
.status-badge {
  padding: 0.2rem 0.75rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.status-active { background: #d1e7dd; color: #0f5132; }
.status-warning { background: #fff3cd; color: #664d03; }
.status-danger { background: #f8d7da; color: #842029; }

/* ========== Sharing ========== */

.sharing-section { margin-top: 1.5rem; }
.sharing-section h3 { margin-bottom: 1rem; }
.sharing-section h4 { margin: 1.25rem 0 0.5rem; font-size: 0.9rem; color: var(--muted); }
.share-list { margin-bottom: 1rem; }
.share-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}
.share-row:last-child { border-bottom: none; }
.share-username { font-weight: 600; }
.share-form {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}
.public-link-box {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.share-hint {
  margin-top: 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
  font-family: monospace;
}

/* ========== Footer ========== */

.site-footer {
  border-top: 1px solid var(--border);
  padding: 2rem 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: 4rem;
}

/* ========== Search Input ========== */

.search-input {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
}
.search-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.15);
}

/* ========== All Contacts Layout ========== */

.contacts-layout {
  display: flex;
  height: calc(100vh - 52px);
  overflow: hidden;
}

.contacts-sidebar {
  width: 420px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--surface);
}

.contacts-sidebar-header {
  padding: 1rem 1.25rem 0.75rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.all-contacts-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.all-contacts-header h1 {
  font-size: 1.35rem;
  font-weight: 700;
}

.contact-count {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.9rem;
}

.contacts-toolbar {
  display: flex;
  gap: 0.5rem;
}

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

.search-wrapper .search-icon {
  position: absolute;
  left: 0.6rem;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--muted);
  pointer-events: none;
}

.search-wrapper .search-input {
  width: 100%;
  padding-left: 2rem;
}

.contacts-toolbar select {
  min-width: 140px;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
  background: var(--surface);
  color: var(--text);
}

.contacts-toolbar select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.contacts-scroll {
  flex: 1;
  overflow-y: auto;
}

.letter-heading {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--primary);
  text-transform: uppercase;
  padding: 0.4rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
}

.contacts-sidebar .contact-item {
  padding: 0.6rem 1.25rem;
  cursor: pointer;
  transition: background var(--transition);
}

.contacts-sidebar .contact-item:hover {
  background: var(--bg);
}

.contacts-sidebar .contact-item.active {
  background: var(--primary-light);
  border-left: 3px solid var(--primary);
  padding-left: calc(1.25rem - 3px);
}

.contact-addressbook-badge {
  display: inline-block;
  font-size: 0.7rem;
  color: var(--muted);
  background: var(--bg);
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  margin-left: 0.4rem;
  font-weight: 400;
  vertical-align: middle;
}

.contacts-sidebar .contact-item.active .contact-addressbook-badge {
  background: rgba(255, 255, 255, 0.7);
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 0.75rem 0.5rem;
  border-top: 1px solid var(--border);
}
.pagination-info {
  font-size: 0.85rem;
  color: var(--muted);
}
.pagination .btn.disabled {
  opacity: 0.4;
  pointer-events: none;
}

.alphabet-bar {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.15rem;
  padding: 0.5rem 0.5rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.alphabet-bar a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--primary);
  text-decoration: none;
  transition: background var(--transition);
}

.alphabet-bar a:hover {
  background: var(--primary-light);
  text-decoration: none;
}

.alphabet-bar a.disabled {
  color: var(--border);
  pointer-events: none;
}

.contacts-detail {
  flex: 1;
  overflow-y: auto;
  background: var(--bg);
  padding: 2rem;
}

.detail-header {
  margin-bottom: 2rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
}

.detail-header h2 {
  font-size: 1.5rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.25rem;
}

.detail-header .contact-meta {
  margin-bottom: 0.75rem;
}

.detail-header .actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.detail-header .actions .btn svg {
  width: 14px;
  height: 14px;
  vertical-align: -2px;
  margin-right: 0.15rem;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 200px;
  color: var(--muted);
  text-align: center;
  gap: 0.75rem;
}

.empty-state p {
  font-size: 0.95rem;
}

/* New contact dropdown */
.new-contact-dropdown {
  position: relative;
  display: inline-block;
}

.new-contact-dropdown[open] .dropdown-menu {
  display: block;
}

.dropdown-menu {
  display: none;
  position: absolute;
  right: 0;
  top: 100%;
  margin-top: 0.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  min-width: 180px;
  z-index: 50;
  list-style: none;
  padding: 0.25rem 0;
}

.dropdown-menu li a {
  display: block;
  padding: 0.5rem 1rem;
  color: var(--text);
  text-decoration: none;
  font-size: 0.9rem;
}

.dropdown-menu li a:hover {
  background: var(--bg);
  text-decoration: none;
}

/* ========== Mobile Nav Toggle ========== */

.nav-toggle { display: none; }

/* ========== Responsive ========== */

@media (max-width: 768px) {
  .nav-toggle {
    display: flex;
    flex-direction: column;
    gap: 4px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    margin-left: auto;
    order: 2;
  }
  .nav-toggle span {
    display: block;
    width: 20px;
    height: 2px;
    background: var(--text);
    border-radius: 1px;
    transition: transform var(--transition);
  }
  .navbar-inner { flex-wrap: wrap; }
  .nav-links {
    display: none;
    flex-direction: column;
    width: 100%;
    order: 10;
    gap: 0;
    margin-left: 0;
    padding: 0.5rem 0;
  }
  .nav-open .nav-links { display: flex; }
  .nav-link {
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border);
  }
  .nav-user { order: 3; margin-left: 0; }
  .nav-actions {
    order: 3;
    margin-left: 0;
    gap: 0.5rem;
  }
  .btn-sm { padding: 0.45rem 0.75rem; }
  .copy-btn { padding: 0.35rem; }
  .container { padding: 0 1rem; }
  .contacts-layout { flex-direction: column; height: auto; min-height: calc(100vh - 52px); }
  .contacts-sidebar { width: 100%; border-right: none; }
  .contacts-scroll { overflow-y: visible; }
  .contacts-detail { display: none; }
  .form-group input, .form-group textarea, .form-group select,
  .search-input { font-size: 16px; }
}

@media (max-width: 480px) {
  .contacts-toolbar { flex-direction: column; }
  .contacts-toolbar select { min-width: auto; }
}
