/* ===================== Reset & Base ===================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #dbeafe;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --border-soft: #f1f5f9;
  --text: #1e293b;
  --text-muted: #64748b;
  --text-light: #94a3b8;
  --radius: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.04);
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,.08);
}

html, body { -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

button { font-family: inherit; }
input, select, textarea { font-family: inherit; font-size: 16px; /* 防止iOS聚焦缩放 */ }

/* ===================== Pages ===================== */
.page { display: none; }
.page.active { display: block; min-height: 100vh; }

/* ===================== Auth ===================== */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
}
.auth-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 32px 28px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.auth-brand { text-align: center; margin-bottom: 24px; }
.auth-logo { font-size: 40px; margin-bottom: 8px; }
.auth-title { font-size: 22px; font-weight: 700; margin-bottom: 4px; }
.auth-subtitle { color: var(--text-muted); font-size: 13px; }

.tab-bar { display: flex; border-bottom: 2px solid var(--border); margin-bottom: 22px; }
.tab-btn {
  flex: 1; padding: 11px;
  background: none; border: none; cursor: pointer;
  font-size: 14px; font-weight: 500; color: var(--text-muted);
  transition: color .15s;
}
.tab-btn.active { color: var(--primary); border-bottom: 2px solid var(--primary); margin-bottom: -2px; }

.auth-form { display: none; }
.auth-form.active { display: block; }

/* ===================== Top Bar ===================== */
.top-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}
.top-bar-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand-logo { font-size: 22px; }
.brand-text { font-weight: 700; font-size: 16px; }
.top-actions { display: flex; align-items: center; gap: 10px; }
.top-user { font-size: 13px; color: var(--text-muted); }

/* ===================== Container ===================== */
.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px 20px 40px;
  display: grid;
  gap: 16px;
}

/* ===================== Card ===================== */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  padding: 20px;
}
.card-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .5px;
  margin-bottom: 14px;
}
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.section-title { font-size: 16px; font-weight: 700; }

/* ===================== User Card ===================== */
.user-head { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; }
.user-avatar {
  width: 52px; height: 52px;
  border-radius: 50%;
  background: linear-gradient(135deg, #2563eb, #7c3aed);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 22px;
  flex-shrink: 0;
}
.user-meta { flex: 1; min-width: 0; }
.user-name { font-size: 18px; font-weight: 700; word-break: break-all; }
.user-role-row { display: flex; align-items: center; gap: 8px; margin-top: 4px; flex-wrap: wrap; }
.expire-text { font-size: 12px; color: var(--text-muted); }
.expire-text.expired { color: var(--danger); font-weight: 500; }
.expire-text.warning { color: var(--warning); font-weight: 500; }

.user-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 14px 0;
  border-top: 1px solid var(--border-soft);
  border-bottom: 1px solid var(--border-soft);
  margin-bottom: 14px;
}
.stat-item { text-align: center; }
.stat-num { font-size: 22px; font-weight: 700; color: var(--primary); }
.stat-name { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.user-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.user-actions .btn { flex: 1; min-width: 130px; justify-content: center; }

/* ===================== Device List ===================== */
.device-list { display: grid; gap: 10px; }
.device-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  background: var(--surface);
}
.device-item-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}
.device-item-name {
  font-weight: 600;
  font-size: 14px;
  flex: 1;
  min-width: 0;
  word-break: break-all;
}
.device-status-tag {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 20px;
  font-weight: 600;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.device-status-tag.online {
  background: #dcfce7;
  color: #15803d;
}
.device-status-tag.online::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 0 2px rgba(22,163,74,.2);
}
.device-status-tag.offline {
  background: #f1f5f9;
  color: #64748b;
}
.device-status-tag.offline::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #94a3b8;
}
.device-id {
  font-size: 11px;
  color: var(--text-light);
  font-family: 'SF Mono', Monaco, monospace;
  margin-bottom: 10px;
  word-break: break-all;
}
.device-grid-info {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px 14px;
  margin-bottom: 10px;
}
.info-row { display: flex; flex-direction: column; }
.info-key {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .3px;
}
.info-val {
  font-size: 13px;
  font-weight: 500;
  font-family: 'SF Mono', Monaco, monospace;
}
.device-switches {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.switch-badge {
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
}
.switch-badge.on { background: #dcfce7; color: #15803d; }
.switch-badge.off { background: #f1f5f9; color: #64748b; }
.device-item-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  padding-top: 10px;
  border-top: 1px solid var(--border-soft);
}

/* ===================== Forms ===================== */
.form-group { margin-bottom: 14px; }
.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
  color: var(--text);
}
.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  color: var(--text);
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
  outline: none;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}
.code-input {
  font-family: 'SF Mono', Monaco, monospace !important;
  letter-spacing: 2px;
  text-transform: uppercase;
}

/* ===================== Buttons ===================== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: background .15s, opacity .15s;
  text-decoration: none;
  white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #b91c1c; }
.btn-success { background: var(--success); color: #fff; }
.btn-outline {
  background: transparent;
  color: var(--primary);
  border: 1px solid var(--primary);
}
.btn-outline:hover { background: var(--primary-soft); }
.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid transparent;
}
.btn-ghost:hover { background: var(--bg); }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-full { width: 100%; justify-content: center; }

/* ===================== Badges ===================== */
.role-badge {
  padding: 2px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
}
.role-badge.admin { background: #fef3c7; color: #92400e; }
.role-badge.paid { background: #dbeafe; color: #1e40af; }
.role-badge.free { background: #f1f5f9; color: #475569; }

.code-badge {
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
}
.code-badge.used { background: #f1f5f9; color: #64748b; }
.code-badge.unused { background: #dcfce7; color: #15803d; }

/* ===================== Admin Section ===================== */
.admin-tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 14px;
  overflow-x: auto;
}
.admin-tab {
  background: none;
  border: none;
  padding: 8px 14px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  white-space: nowrap;
}
.admin-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}
.admin-panel { display: none; }
.admin-panel.active { display: block; }
.panel-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

/* ===================== Tables ===================== */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  -webkit-overflow-scrolling: touch;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 540px;
}
.data-table th {
  padding: 10px 12px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .3px;
  color: var(--text-muted);
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
  font-size: 13px;
}
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: #f8fafc; }
.data-table .actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* ===================== Stats Grid ===================== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.stat-card {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
}
.stat-value { font-size: 24px; font-weight: 700; color: var(--primary); }
.stat-label { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* ===================== Modal ===================== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
@media (min-width: 600px) {
  .modal { align-items: center; }
}
.modal.hidden { display: none; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.5);
  animation: fadeIn .2s;
}
.modal-box {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 92vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
  animation: slideUp .25s;
}
@media (min-width: 600px) {
  .modal-box {
    border-radius: var(--radius-lg);
    animation: scaleIn .2s;
  }
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}
.modal-header h3 { font-size: 16px; font-weight: 600; }
.modal-close {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px 8px;
  line-height: 1;
}
.modal-body { padding: 20px; }
.modal-footer {
  display: flex;
  gap: 10px;
  margin-top: 20px;
  justify-content: flex-end;
}
.modal-footer .btn { flex: 1; max-width: 140px; justify-content: center; }

/* ===================== Toast ===================== */
.toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 13px;
  font-weight: 500;
  z-index: 2000;
  box-shadow: 0 8px 24px rgba(0,0,0,.2);
  max-width: 90%;
  text-align: center;
  animation: slideDown .25s;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.hidden { display: none; }

/* ===================== Misc ===================== */
.hidden { display: none !important; }
.text-muted { color: var(--text-muted); font-size: 13px; }
.loading { text-align: center; padding: 30px; color: var(--text-muted); }
.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-muted);
}
.empty-state .empty-icon { font-size: 40px; margin-bottom: 8px; }
.empty-state p { font-size: 13px; line-height: 1.7; }
.code-text {
  font-family: 'SF Mono', Monaco, monospace;
  font-size: 12px;
  letter-spacing: 1px;
}

.msg {
  padding: 9px 12px;
  border-radius: var(--radius);
  font-size: 13px;
  margin-bottom: 12px;
}
.msg-error {
  color: var(--danger);
  background: #fef2f2;
  border: 1px solid #fecaca;
}
.msg-success {
  color: var(--success);
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
}

/* ===================== Animations ===================== */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(40px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes scaleIn { from { transform: scale(.95); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes slideDown { from { transform: translate(-50%, -20px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

/* ===================== Mobile Adaptation ===================== */
@media (max-width: 600px) {
  .container { padding: 12px 12px 32px; gap: 12px; }
  .card { padding: 16px; }
  .top-bar-inner { padding: 10px 14px; }
  .brand-text { display: none; }
  .top-user { font-size: 12px; }

  .user-head { gap: 12px; }
  .user-avatar { width: 46px; height: 46px; font-size: 18px; }
  .user-name { font-size: 16px; }
  .stat-num { font-size: 20px; }

  .user-actions .btn { flex: 1 1 100%; min-width: 0; }

  .device-grid-info { grid-template-columns: 1fr 1fr; gap: 6px 10px; }

  .modal-footer .btn { max-width: none; }

  .data-table { font-size: 12px; }
  .data-table th,
  .data-table td { padding: 8px 10px; }
}

@media (max-width: 380px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .device-grid-info { grid-template-columns: 1fr; }
}
