* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --primary: #3eb8a8;
  --primary-dark: #2d9a8c;
  --primary-light: #e8f7f5;
  --danger: #e74c3c;
  --warning: #f39c12;
  --text: #333;
  --text-muted: #999;
  --bg: #f0f2f5;
  --white: #fff;
  --radius: 12px;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 430px;
  margin: 0 auto;
  min-height: 100vh;
  background: var(--bg);
  position: relative;
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.08);
}

/* Header */
.page-header {
  background: var(--primary);
  color: var(--white);
  text-align: center;
  padding: 14px 16px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.page-header h1 {
  font-size: 18px;
  font-weight: 500;
}

.page-header.with-back {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.back-btn {
  position: absolute;
  left: 12px;
  background: none;
  border: none;
  color: var(--white);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
}

/* Pages */
.page, .subpage {
  display: none;
  padding-bottom: 70px;
  min-height: calc(100vh - 56px);
}

.page.active {
  display: block;
}

.subpage.active {
  display: block;
}

.page-body {
  padding: 12px;
}

.hidden {
  display: none !important;
}

#sub-views:not(.hidden) {
  display: block;
}

#sub-views:not(.hidden) ~ .tab-bar {
  display: none;
}

/* Stats Card */
.stats-card {
  background: var(--white);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.stats-card.compact {
  display: flex;
  gap: 24px;
}

.stats-card.with-actions {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.stats-left {
  flex: 1;
}

.stats-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat-item {
  margin-bottom: 4px;
  font-size: 14px;
}

.stat-label {
  color: var(--text);
}

.stat-value {
  color: var(--danger);
  font-weight: 600;
}

/* Scan Row */
.scan-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  gap: 8px;
}

.mode-toggle {
  display: flex;
  gap: 12px;
  align-items: center;
}

.toggle-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  cursor: pointer;
  color: var(--text-muted);
}

.toggle-item input {
  display: none;
}

.toggle-item.active {
  color: var(--primary);
  font-weight: 500;
}

.check-icon {
  display: inline-flex;
  width: 18px;
  height: 18px;
  background: var(--primary);
  color: white;
  border-radius: 50%;
  font-size: 11px;
  align-items: center;
  justify-content: center;
}

.scan-btn-inline {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
}

.scan-icon {
  display: inline-flex;
  width: 28px;
  height: 28px;
  border: 2px solid var(--primary);
  border-radius: 50%;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}

/* Inputs */
.input-group {
  display: flex;
  align-items: center;
  background: var(--white);
  border-radius: 24px;
  padding: 4px 4px 4px 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.input-group.plain {
  padding: 4px 16px;
}

.input-field {
  flex: 1;
  border: none;
  outline: none;
  padding: 10px 0;
  font-size: 14px;
  background: transparent;
}

.select-field {
  appearance: none;
  cursor: pointer;
  padding: 10px 8px;
}

.input-scan-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid var(--primary);
  background: var(--white);
  color: var(--primary);
  cursor: pointer;
  font-size: 14px;
  flex-shrink: 0;
}

/* Battery Preview */
.battery-preview {
  background: var(--white);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow);
}

.battery-bar-wrap {
  width: 80px;
  height: 12px;
  background: #eee;
  border-radius: 6px;
  overflow: hidden;
}

.battery-bar {
  height: 100%;
  background: var(--primary);
  border-radius: 6px;
  transition: width 0.3s, background 0.3s;
}

.battery-bar.low {
  background: var(--danger);
}

.battery-text {
  font-size: 14px;
  font-weight: 500;
}

.battery-status {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
}

.battery-status.normal {
  background: var(--primary-light);
  color: var(--primary);
}

.battery-status.low {
  background: #fdecea;
  color: var(--danger);
}

/* Buttons */
.action-row {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-top: 20px;
}

.btn-primary {
  background: var(--primary);
  color: var(--white);
  border: none;
  border-radius: 24px;
  padding: 12px 48px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-primary.btn-outline {
  background: var(--white);
  color: var(--primary);
  border: 1px solid var(--primary);
}

.btn-search {
  background: var(--primary);
  color: var(--white);
  border: none;
  border-radius: 20px;
  padding: 10px 20px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}

.btn-mini {
  background: var(--white);
  border: 1px solid #ddd;
  border-radius: 16px;
  padding: 6px 16px;
  font-size: 13px;
  cursor: pointer;
}

.btn-primary-mini {
  background: var(--primary);
  color: var(--white);
  border-color: var(--primary);
}

.btn-detect {
  width: 100%;
  background: var(--primary);
  color: var(--white);
  border: none;
  border-radius: 24px;
  padding: 12px;
  font-size: 15px;
  cursor: pointer;
  margin-bottom: 12px;
}

.btn-detect:active {
  opacity: 0.85;
}

.btn-detect.loading {
  opacity: 0.7;
  pointer-events: none;
}

/* Search */
.search-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  align-items: center;
}

.search-row .input-field {
  background: var(--white);
  border-radius: 24px;
  padding: 10px 16px;
  box-shadow: var(--shadow);
}

.search-row.dual {
  flex-wrap: wrap;
}

.search-row.dual .input-field {
  flex: 1;
  min-width: calc(50% - 4px);
}

/* Table */
.table-header {
  display: grid;
  grid-template-columns: 1.2fr 1.5fr 0.8fr 0.8fr 1fr;
  gap: 4px;
  padding: 10px 12px;
  background: var(--white);
  border-radius: var(--radius) var(--radius) 0 0;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
}

.table-header.cols-3 {
  grid-template-columns: 1.5fr 1fr 0.8fr;
}

.table-header.cols-4 {
  grid-template-columns: 1.2fr 1.2fr 0.8fr 0.8fr;
}

.table-header.cols-4-wide {
  grid-template-columns: 0.8fr 1.2fr 1.5fr 1.5fr;
  font-size: 11px;
}

.list-container {
  background: var(--bg);
}

.list-row {
  display: grid;
  grid-template-columns: 1.2fr 1.5fr 0.8fr 0.8fr 1fr;
  gap: 4px;
  padding: 14px 12px;
  background: var(--white);
  margin-bottom: 8px;
  border-radius: var(--radius);
  font-size: 13px;
  align-items: center;
  text-align: center;
  box-shadow: var(--shadow);
}

.list-row.cols-3 {
  grid-template-columns: 1.5fr 1fr 0.8fr;
}

.list-row.cols-4 {
  grid-template-columns: 1.2fr 1.2fr 0.8fr 0.8fr;
}

.list-row.cols-4-wide {
  grid-template-columns: 0.8fr 1.2fr 1.5fr 1.5fr;
  font-size: 12px;
}

.list-row span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.text-muted {
  color: var(--text-muted);
}

.status-online {
  color: var(--primary);
  font-weight: 500;
}

.status-normal {
  color: var(--primary);
}

.status-low {
  color: var(--danger);
  font-weight: 500;
}

.status-fault {
  color: var(--warning);
}

.status-offline {
  color: var(--text-muted);
}

/* Alert Icon */
.alert-btn {
  width: 36px;
  height: 36px;
  background: var(--danger);
  border: none;
  border-radius: 50%;
  color: white;
  font-size: 16px;
  cursor: pointer;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s;
}

.alert-btn:hover {
  transform: scale(1.08);
}

.alert-btn.pulse {
  animation: pulse 1s ease infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

.action-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.detail-link {
  color: var(--danger);
  font-size: 12px;
  cursor: pointer;
  text-decoration: none;
}

.detail-link:hover {
  text-decoration: underline;
}

/* User Card */
.user-card {
  background: var(--white);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.avatar {
  width: 56px;
  height: 56px;
  background: var(--primary-light);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
}

.user-info {
  font-size: 15px;
}

/* Menu Grid */
.menu-grid {
  background: var(--white);
  border-radius: var(--radius);
  padding: 20px 12px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px 8px;
  box-shadow: var(--shadow);
}

.menu-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: opacity 0.2s;
}

.menu-item:hover {
  opacity: 0.75;
}

.menu-icon {
  width: 48px;
  height: 48px;
  background: var(--primary-light);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  color: var(--primary);
}

/* Tab Bar */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 430px;
  background: var(--white);
  display: flex;
  border-top: 1px solid #eee;
  z-index: 200;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 11px;
  transition: color 0.2s;
}

.tab-item.active {
  color: var(--primary);
}

.tab-icon {
  font-size: 22px;
}

/* Toast */
.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.75);
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 999;
  pointer-events: none;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-mask {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
}

.modal-content {
  position: relative;
  background: var(--white);
  border-radius: var(--radius);
  padding: 24px;
  width: 85%;
  max-width: 360px;
  z-index: 1;
}

.modal-content h3 {
  margin-bottom: 16px;
  text-align: center;
}

.modal-content .detail-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
  font-size: 14px;
}

.modal-content .detail-row:last-of-type {
  border-bottom: none;
}

.modal-close {
  width: 100%;
  margin-top: 16px;
}

/* Empty State */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
  font-size: 14px;
}

/* Desktop preview hint */
@media (min-width: 500px) {
  body {
    padding: 20px 0;
    background: #ddd;
  }
}
