:root {
  --bg:         #f2f2f7;
  --bg2:        #ffffff;
  --bg3:        #e5e5ea;
  --text:       #000000;
  --text2:      #3c3c43;
  --text3:      #8e8e93;
  --accent:     #0057D9;
  --accent2:    #e8f0fd;
  --green:      #34c759;
  --red:        #ff3b30;
  --separator:  #c6c6c8;
  --card-bg:    #ffffff;
  --header-bg:  rgba(242,242,247,0.92);
  --sheet-bg:   #ffffff;
  --radius:     12px;
  --shadow:     0 1px 3px rgba(0,0,0,0.1), 0 4px 12px rgba(0,0,0,0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:         #000000;
    --bg2:        #1c1c1e;
    --bg3:        #2c2c2e;
    --text:       #ffffff;
    --text2:      #ebebf5cc;
    --text3:      #ebebf599;
    --accent:     #4C9FFF;
    --accent2:    #1a2a4a;
    --green:      #30d158;
    --red:        #ff453a;
    --separator:  #38383a;
    --card-bg:    #1c1c1e;
    --header-bg:  rgba(0,0,0,0.85);
    --sheet-bg:   #1c1c1e;
  }
}

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

html, body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  overscroll-behavior: none;
}

#app { height: 100%; width: 100%; max-width: 100%; display: flex; flex-direction: column; overflow: hidden; }

/* ── Login ── */
#login-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #0057D9 0%, #0033a0 100%);
  padding: 2rem;
}
.login-content { text-align: center; color: #fff; max-width: 340px; }
.login-logo {
  width: 96px; height: 96px;
  background: rgba(255,255,255,0.2);
  border-radius: 24px;
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 1.5rem;
  backdrop-filter: blur(10px);
}
.login-logo svg { width: 52px; height: 52px; }
.login-content h1 { font-size: 2rem; font-weight: 700; line-height: 1.1; margin-bottom: 0.75rem; }
.login-content p { font-size: 1rem; opacity: 0.85; margin-bottom: 2.5rem; line-height: 1.5; }
.btn-signin {
  display: inline-flex; align-items: center; gap: 0.6rem;
  background: #fff; color: #0057D9;
  border: none; border-radius: 14px;
  font-size: 1rem; font-weight: 600;
  padding: 0.9rem 1.75rem;
  cursor: pointer; width: 100%;
  justify-content: center;
  transition: opacity 0.15s, transform 0.1s;
}
.btn-signin:active { opacity: 0.85; transform: scale(0.98); }
.btn-signin svg { width: 20px; height: 20px; }

/* ── Main screen ── */
#main-screen { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.header {
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: env(safe-area-inset-top, 0) 0 0;
  border-bottom: 1px solid var(--separator);
  z-index: 10;
  flex-shrink: 0;
}
.header-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.25rem 0.5rem;
}
.header h1 { font-size: 1.75rem; font-weight: 700; }
.header-actions { display: flex; align-items: center; gap: 0.5rem; }
.icon-btn {
  width: 36px; height: 36px;
  border: none; background: var(--bg3);
  border-radius: 50%; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--accent); transition: opacity 0.15s;
}
.icon-btn:active { opacity: 0.6; }
.icon-btn svg { width: 18px; height: 18px; }

.search-wrap { padding: 0.5rem 1rem 0.75rem; }
.search-bar {
  position: relative;
  background: var(--bg3);
  border-radius: 10px;
  display: flex; align-items: center;
  padding: 0 0.75rem;
}
.search-bar svg { width: 16px; height: 16px; color: var(--text3); flex-shrink: 0; }
.search-bar input {
  flex: 1; border: none; background: transparent;
  padding: 0.6rem 0.5rem;
  font-size: 1rem; color: var(--text);
  outline: none;
}
.search-bar input::placeholder { color: var(--text3); }
.search-clear {
  background: none; border: none; padding: 0; cursor: pointer;
  color: var(--text3); display: flex; align-items: center;
}
.search-clear svg { width: 16px; height: 16px; }

.filter-row {
  display: flex; gap: 0.5rem;
  padding: 0 1rem 0.75rem;
  overflow-x: auto; scrollbar-width: none;
}
.filter-row::-webkit-scrollbar { display: none; }
.filter-chip {
  white-space: nowrap; border: none;
  background: var(--bg3); color: var(--text2);
  border-radius: 20px; padding: 0.3rem 0.85rem;
  font-size: 0.8rem; font-weight: 500;
  cursor: pointer; transition: all 0.15s;
  flex-shrink: 0;
}
.filter-chip.active { background: var(--accent); color: #fff; }

/* ── User list ── */
.list-area {
  flex: 1; display: flex; overflow: hidden;
  max-width: 100%;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
#user-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior: contain;
  max-width: 100%;
}

.section-header {
  position: sticky; top: 0;
  background: var(--header-bg);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  padding: 0.25rem 1.25rem;
  font-size: 0.8rem; font-weight: 600;
  color: var(--text3);
  border-bottom: 1px solid var(--separator);
  z-index: 5;
}

.user-row {
  display: flex; align-items: center; gap: 0.9rem;
  padding: 0.7rem 1.25rem;
  border-bottom: 1px solid var(--separator);
  background: var(--card-bg);
  cursor: pointer; transition: background 0.1s;
  -webkit-user-select: none;
}
.user-row:active { background: var(--bg3); }

.avatar {
  width: 46px; height: 46px; border-radius: 50%;
  background: var(--accent2); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem; font-weight: 600;
  flex-shrink: 0; overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

.user-info { flex: 1; min-width: 0; }
.user-name { font-size: 1rem; font-weight: 500; color: var(--text); }
.user-meta { font-size: 0.8rem; color: var(--text3); margin-top: 0.1rem; }
.user-row-chevron { color: var(--text3); flex-shrink: 0; }
.user-row-chevron svg { width: 14px; height: 14px; }

/* ── Alphabet index ── */
#alpha-index {
  width: 22px; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 4px 0; gap: 0; overflow: hidden;
  flex-shrink: 0;
}
.alpha-letter {
  font-size: 10px; font-weight: 600; color: var(--accent);
  padding: 1px 0; cursor: pointer; line-height: 1;
  -webkit-user-select: none;
}
.alpha-letter:active { background: var(--accent); color: #fff; border-radius: 3px; padding: 1px 2px; }

/* ── Empty / Loading ── */
.state-view {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 4rem 2rem; text-align: center;
  color: var(--text3); gap: 1rem; flex: 1;
}
.state-view svg { width: 60px; height: 60px; opacity: 0.4; }
.state-view p { font-size: 0.95rem; line-height: 1.5; }
.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--bg3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── User Detail Sheet ── */
#detail-sheet {
  position: fixed; inset: 0;
  z-index: 100;
  display: none;
}
#detail-sheet.open { display: block; }
.sheet-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(4px);
}
.sheet-panel {
  position: absolute; bottom: 0; left: 0; right: 0;
  background: var(--sheet-bg);
  border-radius: 20px 20px 0 0;
  max-height: 92vh;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
  transform: translateY(100%);
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}
.sheet-panel.visible { transform: translateY(0); }

.sheet-handle {
  width: 36px; height: 4px;
  background: var(--separator);
  border-radius: 2px;
  margin: 0.75rem auto 1rem;
}
.sheet-close {
  position: absolute; top: 1.25rem; right: 1.25rem;
  width: 30px; height: 30px;
  background: var(--bg3); border: none; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: var(--text3);
}
.sheet-close svg { width: 14px; height: 14px; }

.detail-header {
  display: flex; flex-direction: column; align-items: center;
  padding: 0 1.5rem 1.5rem;
  text-align: center;
}
.detail-avatar {
  width: 88px; height: 88px; border-radius: 50%;
  background: var(--accent2); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 2rem; font-weight: 600;
  margin-bottom: 1rem; overflow: hidden;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
}
.detail-avatar img { width: 100%; height: 100%; object-fit: cover; }
.detail-name { font-size: 1.4rem; font-weight: 700; margin-bottom: 0.2rem; }
.detail-title { font-size: 0.9rem; color: var(--accent); font-weight: 500; }
.detail-dept { font-size: 0.85rem; color: var(--text3); margin-top: 0.15rem; }

.action-row {
  display: flex; gap: 1rem; padding: 0 1.5rem 1.5rem;
  justify-content: center;
}
.action-btn {
  display: flex; flex-direction: column; align-items: center; gap: 0.35rem;
  flex: 1; max-width: 80px;
  border: none; background: var(--bg3);
  border-radius: var(--radius); padding: 0.75rem 0.5rem;
  cursor: pointer; font-size: 0.72rem; font-weight: 500;
  color: var(--accent); transition: opacity 0.15s;
}
.action-btn:active { opacity: 0.6; }
.action-btn svg { width: 22px; height: 22px; }

.info-section { padding: 0 1.25rem 1rem; }
.info-card {
  background: var(--bg3);
  border-radius: var(--radius);
  overflow: hidden; margin-bottom: 1rem;
}
.info-row {
  display: flex; align-items: flex-start; gap: 0.75rem;
  padding: 0.8rem 1rem;
  border-bottom: 1px solid var(--separator);
  position: relative;
}
.info-row:last-child { border-bottom: none; }
.info-icon { color: var(--accent); flex-shrink: 0; margin-top: 0.1rem; }
.info-icon svg { width: 18px; height: 18px; }
.info-label { font-size: 0.72rem; color: var(--text3); margin-bottom: 0.15rem; }
.info-value { font-size: 0.95rem; color: var(--text); line-height: 1.4; }
.info-value a { color: var(--accent); text-decoration: none; }
.phone-link {
  background: none; border: none; padding: 0; margin: 0;
  font: inherit; color: var(--accent);
  cursor: pointer; text-align: left;
}
.info-copy {
  position: absolute; right: 0.75rem; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer;
  color: var(--text3); padding: 0.5rem;
}
.info-copy svg { width: 16px; height: 16px; }
.copy-toast {
  position: fixed; bottom: 100px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,0.8); color: #fff;
  padding: 0.5rem 1.25rem; border-radius: 20px;
  font-size: 0.85rem; z-index: 200;
  opacity: 0; transition: opacity 0.2s;
  pointer-events: none; white-space: nowrap;
}
.copy-toast.show { opacity: 1; }

.hidden { display: none !important; }

/* ── Number picker (call/text) ── */
.picker-overlay { position: fixed; inset: 0; z-index: 150; }
.picker-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.4); }
.picker-sheet {
  position: absolute; bottom: 0; left: 0; right: 0;
  padding: 0.5rem 0.75rem max(0.75rem, env(safe-area-inset-bottom));
  transform: translateY(110%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.picker-sheet.visible { transform: translateY(0); }
.picker-title {
  text-align: center; font-size: 0.8rem; color: var(--text3);
  padding: 0.75rem; background: var(--sheet-bg);
  border-radius: 14px 14px 0 0;
}
.picker-option {
  display: flex; flex-direction: column; align-items: center; gap: 0.15rem;
  width: 100%; border: none; background: var(--sheet-bg);
  border-top: 1px solid var(--separator);
  padding: 0.85rem; cursor: pointer;
}
.picker-opt-label { font-size: 0.8rem; color: var(--text3); }
.picker-opt-number { font-size: 1.1rem; color: var(--accent); font-weight: 500; }
.picker-option:last-of-type { border-radius: 0 0 14px 14px; }
.picker-cancel {
  width: 100%; border: none; background: var(--sheet-bg);
  border-radius: 14px; margin-top: 0.5rem;
  padding: 0.95rem; font-size: 1.05rem; font-weight: 600;
  color: var(--accent); cursor: pointer;
}

@media (min-width: 600px) {
  .sheet-panel { max-width: 480px; left: 50%; right: auto; transform: translateX(-50%) translateY(100%); border-radius: 20px; bottom: 2rem; }
  .sheet-panel.visible { transform: translateX(-50%) translateY(0); }
  .picker-sheet { max-width: 420px; left: 50%; right: auto; transform: translateX(-50%) translateY(110%); }
  .picker-sheet.visible { transform: translateX(-50%) translateY(0); }
}
