:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #dbeafe;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --purple: #8b5cf6;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --text-secondary: #64748b;
  --border: #e2e8f0;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.08);
  --radius: 12px;
  --radius-sm: 8px;
  --header-h: 56px;
  --bottom-nav-h: 64px;
  --sidebar-w: 240px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}
a { color: var(--primary); text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: none; background: none; }
input, select { font-family: inherit; font-size: 14px; }

.view { display: none; }
.view.active { display: block; }
.page { display: none; animation: fadeIn .2s ease; }
.page.active { display: block; }
.hidden { display: none !important; }

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

/* ========== 登录页 ========== */
.login-wrap {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.login-header {
  text-align: center;
  color: #fff;
  margin-bottom: 32px;
}
.logo { font-size: 48px; margin-bottom: 8px; }
.login-header h1 { font-size: 28px; font-weight: 700; }
.subtitle { opacity: .85; margin-top: 4px; font-size: 14px; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 20px;
  width: 100%;
  max-width: 360px;
}
#login-form.card { padding: 28px 24px; }

.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 6px;
  font-weight: 500;
}
.field input, .field select {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: #f8fafc;
  transition: all .2s;
  outline: none;
}
.field input:focus, .field select:focus {
  border-color: var(--primary);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 14px;
  transition: all .15s;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-block { width: 100%; padding: 14px; font-size: 15px; }
.btn-ghost {
  color: var(--text-secondary);
  padding: 10px;
  font-size: 13px;
}
.btn-ghost:hover { color: var(--danger); }
.btn-back {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 8px 0;
  color: var(--primary);
  font-weight: 600;
  margin-bottom: 12px;
  font-size: 14px;
}

.error-msg {
  color: var(--danger);
  font-size: 13px;
  min-height: 18px;
  margin-bottom: 8px;
}
.login-tip {
  text-align: center;
  color: var(--text-secondary);
  font-size: 12px;
  margin-top: 16px;
}

/* ========== 主应用布局 ========== */
#view-app {
  min-height: 100vh;
  background: var(--bg);
  padding-top: var(--header-h);
}
.app-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-h);
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  z-index: 100;
  gap: 8px;
}
.app-bar h1 {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  font-size: 20px;
  position: relative;
  transition: background .15s;
}
.icon-btn:hover { background: #f1f5f9; }

.badge {
  position: absolute;
  top: 4px;
  right: 4px;
  min-width: 16px;
  height: 16px;
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  border-radius: 8px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* Sidebar */
.sidebar {
  position: fixed;
  top: var(--header-h);
  left: 0;
  bottom: 0;
  width: var(--sidebar-w);
  background: #fff;
  border-right: 1px solid var(--border);
  display: none;
  flex-direction: column;
  z-index: 90;
  transition: transform .25s;
}
.sidebar.open { display: flex; }
.sidebar-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  color: var(--text-secondary);
}
.sidebar-footer {
  margin-top: auto;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  transition: all .15s;
  position: relative;
  cursor: pointer;
}
.nav-item:hover { background: #f8fafc; color: var(--text); }
.nav-item.active {
  background: var(--primary-light);
  color: var(--primary);
}
.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--primary);
  border-radius: 0 2px 2px 0;
}
.nav-icon { font-size: 18px; width: 24px; text-align: center; }

/* Content */
.content {
  padding: 16px;
  padding-bottom: calc(var(--bottom-nav-h) + 16px);
  max-width: 720px;
  margin: 0 auto;
}

/* Bottom nav */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--bottom-nav-h);
  background: #fff;
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 80;
  box-shadow: 0 -1px 3px rgba(0,0,0,0.04);
}
.bottom-nav .nav-item {
  flex: 1;
  flex-direction: column;
  gap: 2px;
  padding: 8px 4px;
  font-size: 11px;
  border-radius: 0;
  color: var(--text-secondary);
}
.bottom-nav .nav-item.active {
  background: transparent;
  color: var(--primary);
}
.bottom-nav .nav-item.active::before { display: none; }
.bottom-nav .nav-icon { font-size: 22px; }

/* ========== Stats Grid ========== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
  box-shadow: var(--shadow);
  border-top: 3px solid transparent;
}
.stat-value {
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
  margin-bottom: 6px;
}
.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
}
.stat-blue { border-top-color: var(--primary); }
.stat-blue .stat-value { color: var(--primary); }
.stat-red { border-top-color: var(--danger); }
.stat-red .stat-value { color: var(--danger); }
.stat-green { border-top-color: var(--success); }
.stat-green .stat-value { color: var(--success); }
.stat-purple { border-top-color: var(--purple); }
.stat-purple .stat-value { color: var(--purple); }
.stat-gray { border-top-color: var(--text-secondary); }
.stat-gray .stat-value { color: var(--text-secondary); }

/* ========== Sections ========== */
.section { margin-bottom: 24px; }
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.section-header h2 {
  font-size: 16px;
  font-weight: 600;
}
.link { font-size: 13px; font-weight: 500; }

/* ========== Elder List ========== */
.elder-list { display: flex; flex-direction: column; gap: 12px; }
.elder-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 14px;
  transition: transform .15s, box-shadow .15s;
  cursor: pointer;
}
.elder-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}
.elder-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  flex-shrink: 0;
}
.elder-info { flex: 1; min-width: 0; }
.elder-info h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 4px;
}
.elder-meta {
  font-size: 12px;
  color: var(--text-secondary);
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.health-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
}
.health-normal { background: #dcfce7; color: #166534; }
.health-good { background: #dbeafe; color: #1e40af; }
.health-poor { background: #fef3c7; color: #92400e; }
.health-critical { background: #fee2e2; color: #991b1b; }

/* ========== Alert List ========== */
.alert-list { display: flex; flex-direction: column; gap: 10px; }
.alert-item {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  border-left: 4px solid;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  cursor: pointer;
  transition: transform .1s;
}
.alert-item:hover { transform: translateX(2px); }
.alert-item.info { border-left-color: var(--primary); }
.alert-item.warning { border-left-color: var(--warning); }
.alert-item.critical { border-left-color: var(--danger); }
.alert-item.acknowledged { opacity: .6; }

.alert-icon { font-size: 20px; flex-shrink: 0; }
.alert-content { flex: 1; min-width: 0; }
.alert-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.alert-meta {
  font-size: 11px;
  color: var(--text-secondary);
  display: flex;
  gap: 8px;
}
.alert-level {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 600;
}
.alert-level.info { background: var(--primary-light); color: var(--primary); }
.alert-level.warning { background: #fef3c7; color: #92400e; }
.alert-level.critical { background: #fee2e2; color: #991b1b; }

/* ========== Filter Bar ========== */
.filter-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}
.filter-bar select {
  flex: 1;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  outline: none;
}

/* ========== Device List ========== */
.device-list { display: flex; flex-direction: column; gap: 10px; }
.device-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 12px;
}
.device-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  background: var(--primary-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}
.device-info { flex: 1; min-width: 0; }
.device-info h3 {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 4px;
}
.device-meta { font-size: 12px; color: var(--text-secondary); }
.device-status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-online { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-offline { background: var(--text-secondary); }
.status-warning { background: var(--warning); }

/* ========== Profile ========== */
.profile-card {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}
.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--primary-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  flex-shrink: 0;
}
.profile-info h2 { font-size: 18px; font-weight: 600; margin-bottom: 4px; }
.profile-info p { font-size: 13px; color: var(--text-secondary); }

.binding-list { display: flex; flex-direction: column; gap: 10px; }
.binding-item {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px 16px;
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.binding-name { font-weight: 600; font-size: 14px; }
.binding-meta { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }

/* ========== Elder Detail ========== */
.elder-detail .card { margin-bottom: 16px; }
.detail-row {
  display: flex;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.detail-row:last-child { border-bottom: none; }
.detail-label {
  width: 100px;
  color: var(--text-secondary);
  flex-shrink: 0;
}
.detail-value { flex: 1; color: var(--text); }

/* ========== Empty State ========== */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-secondary);
}
.empty-icon { font-size: 48px; margin-bottom: 12px; }

/* ========== Modal ========== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 200;
  animation: fadeIn .2s;
}
.modal-content {
  background: #fff;
  border-radius: 16px 16px 0 0;
  width: 100%;
  max-width: 500px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  animation: slideUp .25s ease;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 16px; font-weight: 600; }
.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
  flex: 1;
}
.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}
.info-row {
  display: flex;
  padding: 8px 0;
  border-bottom: 1px solid #f1f5f9;
  font-size: 13px;
}
.info-row:last-child { border-bottom: none; }
.info-row .label { width: 90px; color: var(--text-secondary); flex-shrink: 0; }

/* ========== Toast ========== */
.toast {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: #0f172a;
  color: #fff;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 300;
  box-shadow: var(--shadow-lg);
  animation: fadeIn .2s;
}

/* ========== Loading ========== */
.loading {
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 400;
}
.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ========== 响应式 ========== */
@media (min-width: 768px) {
  .stats-grid { grid-template-columns: repeat(4, 1fr); }
  .bottom-nav { display: none; }
  .sidebar { display: flex; }
  #view-app { padding-left: var(--sidebar-w); padding-top: var(--header-h); }
  .content { padding-bottom: 24px; }
  .bottom-nav .badge { display: none; }
  .app-bar { padding: 0 20px; }
  #menu-btn { display: none; }
  .modal { align-items: center; }
  .modal-content { border-radius: 16px; max-height: 90vh; }
}

@media (max-width: 767px) {
  .sidebar {
    transform: translateX(-100%);
    display: flex;
  }
  .sidebar.open { transform: translateX(0); box-shadow: 4px 0 20px rgba(0,0,0,0.1); }
}

/* ========== 管理员样式 ========== */
.admin-nav-section {
  border-top: 1px solid var(--border);
  padding-top: 8px;
}

.admin-header {
  font-size: 12px;
  color: var(--primary);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.admin-stats {
  grid-template-columns: repeat(3, 1fr);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
  display: inline-flex;
  align-items: center;
}

.btn-edit {
  border: 1px solid var(--primary);
  color: var(--primary);
  background: #fff;
  margin-right: 6px;
}

.btn-delete {
  border: 1px solid var(--danger);
  color: var(--danger);
  background: #fff;
}

.admin-family-list,
.admin-elder-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.admin-family-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.admin-family-info h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 6px;
}

.admin-family-meta {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 3px;
}

.admin-family-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}

.admin-status {
  font-size: 12px;
  padding: 2px 10px;
  border-radius: 10px;
  font-weight: 600;
}

.admin-status.active { background: #dcfce7; color: #166534; }
.admin-status.inactive { background: #fee2e2; color: #991b1b; }

.admin-elder-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 14px;
}

.admin-elder-info { flex: 1; min-width: 0; }
.admin-elder-info h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 6px;
}

.admin-elder-meta {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 3px;
}

.gender-tag {
  font-size: 11px;
  padding: 1px 8px;
  border-radius: 10px;
  margin-left: 8px;
  font-weight: 600;
}

.gender-tag.男 { background: var(--primary-light); color: var(--primary); }
.gender-tag.女 { background: #fce7f3; color: #c2185b; }

.admin-elder-actions { display: flex; gap: 6px; }

.log-list { display: flex; flex-direction: column; gap: 10px; }

.log-item {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}

.log-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.log-action {
  font-size: 12px;
  padding: 2px 10px;
  border-radius: 10px;
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 600;
}

.log-action.create { background: #dcfce7; color: #166534; }
.log-action.delete { background: #fee2e2; color: #991b1b; }
.log-action.update { background: #fef3c7; color: #92400e; }
.log-action.update_status { background: #e0e7ff; color: #3730a3; }
.log-action.resolve { background: #fce7f3; color: #9d174d; }

.log-time { font-size: 11px; color: var(--text-secondary); }

.log-body { font-size: 13px; }
.log-operator { font-weight: 600; margin-bottom: 4px; }
.log-detail { color: var(--text); margin-bottom: 4px; }
.log-target { color: var(--text-secondary); font-size: 12px; margin-bottom: 4px; }
.log-footer { color: var(--text-secondary); font-size: 11px; }

.elder-form .field { margin-bottom: 14px; }
.elder-form textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: #f8fafc;
  resize: vertical;
}
.elder-form .modal-footer {
  display: flex;
  gap: 12px;
  padding-top: 12px;
}
.elder-form .modal-footer .btn { flex: 1; }

@media (min-width: 768px) {
  .admin-stats { grid-template-columns: repeat(4, 1fr); }
}
