/* ===== CSS Variables ===== */
:root {
  --bg:        #0b0f1a;
  --bg2:       #111827;
  --bg3:       #1f2937;
  --card:      #1e293b;
  --card2:     #263041;
  --border:    #334155;
  --border2:   #475569;
  --text:      #f1f5f9;
  --text2:     #94a3b8;
  --text3:     #64748b;
  --primary:   #3b82f6;
  --primary-d: #2563eb;
  --cyan:      #06b6d4;
  --green:     #10b981;
  --amber:     #f59e0b;
  --red:       #ef4444;
  --purple:    #8b5cf6;
  --shadow:    0 4px 24px rgba(0,0,0,.45);
  --radius:    12px;
  --radius-sm: 8px;
}

/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 14px; }
body { background: var(--bg); color: var(--text); font-family: 'Inter', 'PingFang SC', 'Microsoft YaHei', sans-serif; min-height: 100vh; line-height: 1.6; }
a { color: var(--primary); text-decoration: none; }
button { cursor: pointer; border: none; outline: none; font-family: inherit; }
input, select, textarea { font-family: inherit; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg2); }
::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 3px; }

/* ===== Layout ===== */
.page-wrapper { display: flex; min-height: 100vh; }

/* ===== Sidebar ===== */
.sidebar {
  width: 240px; min-height: 100vh; background: var(--bg2);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
  position: fixed; left: 0; top: 0; bottom: 0; z-index: 100;
  transition: transform .3s;
}
.sidebar-logo { padding: 20px 24px; border-bottom: 1px solid var(--border); }
.sidebar-logo h1 { font-size: 1.1rem; font-weight: 700; color: var(--cyan); }
.sidebar-logo p { font-size: .75rem; color: var(--text3); margin-top: 2px; }
.sidebar-nav { flex: 1; padding: 12px 0; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 24px;
  color: var(--text2); font-size: .9rem; cursor: pointer;
  transition: all .2s; border-left: 3px solid transparent;
}
.nav-item:hover { color: var(--text); background: rgba(255,255,255,.04); }
.nav-item.active { color: var(--primary); background: rgba(59,130,246,.1); border-left-color: var(--primary); }
.nav-item i { width: 20px; text-align: center; font-size: 1rem; }
.sidebar-footer { padding: 16px 24px; border-top: 1px solid var(--border); }
.user-info { display: flex; align-items: center; gap: 10px; }
.user-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--primary); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: .9rem; color: #fff; }
.user-name { font-size: .85rem; font-weight: 600; }
.user-role { font-size: .75rem; color: var(--text3); }
.btn-logout { margin-top: 10px; width: 100%; padding: 8px; border-radius: var(--radius-sm); background: rgba(239,68,68,.15); color: var(--red); font-size: .85rem; transition: background .2s; }
.btn-logout:hover { background: rgba(239,68,68,.25); }

/* ===== Main Content ===== */
.main-content { margin-left: 240px; flex: 1; padding: 24px; min-height: 100vh; }
.page-header { margin-bottom: 24px; display: flex; align-items: center; justify-content: space-between; }
.page-header h2 { font-size: 1.4rem; font-weight: 700; }
.page-header p { color: var(--text2); font-size: .85rem; margin-top: 2px; }

/* ===== Cards ===== */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow);
}
.card-title { font-size: .95rem; font-weight: 600; margin-bottom: 16px; display: flex; align-items: center; gap: 8px; }

/* ===== Stat Cards ===== */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; }
.stat-card .label { font-size: .8rem; color: var(--text2); margin-bottom: 8px; }
.stat-card .value { font-size: 2rem; font-weight: 700; }
.stat-card .icon { float: right; font-size: 2rem; opacity: .3; }
.stat-card.blue .value { color: var(--primary); }
.stat-card.green .value { color: var(--green); }
.stat-card.amber .value { color: var(--amber); }
.stat-card.cyan .value { color: var(--cyan); }

/* ===== Device Grid ===== */
.device-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }

/* ===== Device Card ===== */
.device-card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; transition: border-color .2s, box-shadow .2s; position: relative;
}
.device-card:hover { border-color: var(--border2); box-shadow: 0 8px 32px rgba(0,0,0,.4); }
.device-card.selected { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(59,130,246,.3); }
.device-card-header { padding: 16px 20px 12px; display: flex; align-items: flex-start; justify-content: space-between; border-bottom: 1px solid var(--border); }
.device-name { font-weight: 600; font-size: .95rem; }
.device-id { font-size: .75rem; color: var(--text3); font-family: monospace; margin-top: 2px; }
.device-status { display: flex; align-items: center; gap: 6px; font-size: .75rem; font-weight: 500; padding: 3px 8px; border-radius: 20px; }
.device-status.online { background: rgba(16,185,129,.15); color: var(--green); }
.device-status.offline { background: rgba(100,116,139,.15); color: var(--text3); }
.status-dot { width: 7px; height: 7px; border-radius: 50%; }
.online .status-dot { background: var(--green); animation: pulse 2s infinite; }
.offline .status-dot { background: var(--text3); }
@keyframes pulse { 0%,100%{opacity:1}50%{opacity:.3} }

/* ===== Temperature Display ===== */
.temp-display { padding: 20px; display: flex; align-items: center; gap: 20px; }
.temp-gauge { position: relative; width: 100px; height: 100px; flex-shrink: 0; }
.temp-gauge svg { width: 100%; height: 100%; }
.gauge-bg { fill: none; stroke: var(--bg3); stroke-width: 8; }
.gauge-fill { fill: none; stroke-width: 8; stroke-linecap: round; transform-origin: 50% 50%; transform: rotate(-90deg); transition: stroke-dasharray .8s ease, stroke .5s ease; }
.gauge-text { text-anchor: middle; dominant-baseline: middle; }
.gauge-temp { font-size: 22px; font-weight: 700; fill: var(--text); }
.gauge-unit { font-size: 10px; fill: var(--text3); }
.temp-info { flex: 1; }
.temp-current { font-size: 2.2rem; font-weight: 700; line-height: 1; }
.temp-target { font-size: .82rem; color: var(--text2); margin-top: 4px; }
.temp-mode { display: inline-flex; align-items: center; gap: 4px; font-size: .78rem; padding: 2px 8px; border-radius: 12px; margin-top: 6px; }
.temp-mode.heat { background: rgba(245,158,11,.15); color: var(--amber); }
.temp-mode.cool { background: rgba(6,182,212,.15); color: var(--cyan); }
.relay-state { display: inline-flex; align-items: center; gap: 4px; font-size: .78rem; padding: 2px 8px; border-radius: 12px; margin-left: 6px; }
.relay-state.on { background: rgba(16,185,129,.15); color: var(--green); }
.relay-state.off { background: rgba(100,116,139,.15); color: var(--text3); }

.power-state-badge { display: inline-flex; align-items: center; gap: 4px; font-size: .78rem; font-weight: 600; padding: 2px 10px; border-radius: 12px; }
.power-state-badge.on { background: rgba(16,185,129,.18); color: #10b981; }
.power-state-badge.off { background: rgba(239,68,68,.15); color: #ef4444; }

/* ===== Device Controls ===== */
.device-controls { padding: 0 20px 16px; }
.ctrl-row { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.ctrl-label { font-size: .8rem; color: var(--text2); min-width: 72px; }
.ctrl-value { font-size: .85rem; font-weight: 600; min-width: 40px; text-align: right; }
input[type="range"] {
  flex: 1; -webkit-appearance: none; height: 4px; border-radius: 2px;
  background: var(--bg3); outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px;
  border-radius: 50%; background: var(--primary); cursor: pointer;
  box-shadow: 0 0 0 3px rgba(59,130,246,.25);
}
.device-card-actions { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; flex-wrap: wrap; }
.device-select-cb { position: absolute; top: 16px; left: 16px; width: 18px; height: 18px; cursor: pointer; accent-color: var(--primary); display: none; }
.batch-mode .device-select-cb { display: block; }
.batch-mode .device-card-header { padding-left: 40px; }

/* ===== Toggle Switch ===== */
.toggle-wrap { display: flex; align-items: center; gap: 8px; }
.toggle { position: relative; width: 42px; height: 24px; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider { position: absolute; inset: 0; background: var(--bg3); border-radius: 12px; cursor: pointer; transition: .3s; }
.toggle-slider::before { content: ''; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .3s; }
.toggle input:checked + .toggle-slider { background: var(--primary); }
.toggle input:checked + .toggle-slider::before { transform: translateX(18px); }
.toggle-label { font-size: .82rem; }

/* ===== Buttons ===== */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; border-radius: var(--radius-sm); font-size: .82rem; font-weight: 500; transition: all .2s; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-d); }
.btn-secondary { background: var(--bg3); color: var(--text2); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--card2); color: var(--text); }
.btn-success { background: rgba(16,185,129,.15); color: var(--green); border: 1px solid rgba(16,185,129,.3); }
.btn-success:hover { background: rgba(16,185,129,.25); }
.btn-danger { background: rgba(239,68,68,.15); color: var(--red); border: 1px solid rgba(239,68,68,.3); }
.btn-danger:hover { background: rgba(239,68,68,.25); }
.btn-amber { background: rgba(245,158,11,.15); color: var(--amber); border: 1px solid rgba(245,158,11,.3); }
.btn-amber:hover { background: rgba(245,158,11,.25); }
.btn-sm { padding: 5px 10px; font-size: .78rem; }
.btn-lg { padding: 12px 24px; font-size: .95rem; }
.btn-icon { width: 34px; height: 34px; padding: 0; justify-content: center; border-radius: 50%; }
.btn-block { display: flex; width: 100%; justify-content: center; }

/* ===== FAB ===== */
.fab {
  position: fixed; right: 28px; bottom: 28px; width: 54px; height: 54px;
  border-radius: 50%; background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center; font-size: 1.5rem;
  box-shadow: 0 4px 20px rgba(59,130,246,.5); z-index: 200; transition: all .2s;
}
.fab:hover { background: var(--primary-d); transform: scale(1.08); }

/* ===== Batch Bar ===== */
.batch-bar {
  position: fixed; bottom: 0; left: 240px; right: 0;
  background: var(--card2); border-top: 1px solid var(--border);
  padding: 14px 24px; display: flex; align-items: center; gap: 12px;
  z-index: 150; transform: translateY(100%); transition: transform .3s;
}
.batch-bar.show { transform: translateY(0); }
.batch-bar-info { flex: 1; font-size: .9rem; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 500; opacity: 0; pointer-events: none; transition: opacity .2s;
}
.modal-overlay.show { opacity: 1; pointer-events: all; }
.modal {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 28px; width: 100%; max-width: 460px; max-height: 90vh; overflow-y: auto;
  transform: translateY(20px); transition: transform .2s;
}
.modal-overlay.show .modal { transform: translateY(0); }
.modal-title { font-size: 1.05rem; font-weight: 700; margin-bottom: 20px; display: flex; align-items: center; justify-content: space-between; }
.modal-close { background: none; color: var(--text2); font-size: 1.2rem; padding: 0; }
.modal-close:hover { color: var(--text); }

/* ===== Form ===== */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: .82rem; color: var(--text2); margin-bottom: 6px; font-weight: 500; }
.form-control {
  width: 100%; padding: 10px 14px; background: var(--bg3); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text); font-size: .88rem;
  transition: border-color .2s, box-shadow .2s;
}
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(59,130,246,.2); }
.form-hint { font-size: .75rem; color: var(--text3); margin-top: 4px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ===== Table ===== */
.table-wrap { overflow-x: auto; border-radius: var(--radius-sm); }
table { width: 100%; border-collapse: collapse; font-size: .83rem; }
thead { background: var(--bg3); }
th { padding: 10px 14px; text-align: left; font-weight: 600; color: var(--text2); white-space: nowrap; }
td { padding: 10px 14px; border-bottom: 1px solid var(--border); color: var(--text); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,.02); }
.monospace { font-family: monospace; }

/* ===== Pagination ===== */
.pagination { display: flex; align-items: center; gap: 6px; justify-content: flex-end; margin-top: 16px; }
.page-btn { padding: 6px 12px; border-radius: var(--radius-sm); background: var(--bg3); color: var(--text2); font-size: .82rem; border: 1px solid var(--border); cursor: pointer; transition: all .2s; }
.page-btn:hover { background: var(--card2); color: var(--text); }
.page-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.page-btn:disabled { opacity: .4; cursor: not-allowed; }
.page-info { font-size: .8rem; color: var(--text3); }

/* ===== Badges ===== */
.badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 12px; font-size: .75rem; font-weight: 500; }
.badge-green { background: rgba(16,185,129,.15); color: var(--green); }
.badge-red { background: rgba(239,68,68,.15); color: var(--red); }
.badge-amber { background: rgba(245,158,11,.15); color: var(--amber); }
.badge-blue { background: rgba(59,130,246,.15); color: var(--primary); }
.badge-gray { background: rgba(100,116,139,.15); color: var(--text3); }

/* ===== Tabs ===== */
.tabs { display: flex; gap: 4px; background: var(--bg3); padding: 4px; border-radius: var(--radius-sm); margin-bottom: 20px; width: fit-content; }
.tab-btn { padding: 8px 20px; border-radius: 6px; font-size: .85rem; color: var(--text2); transition: all .2s; }
.tab-btn.active { background: var(--card); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.3); }
.tab-btn:hover:not(.active) { color: var(--text); }

/* ===== Chart ===== */
.chart-wrap { position: relative; height: 240px; }

/* ===== Search Bar ===== */
.search-bar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; align-items: flex-end; }
.search-bar .form-control { max-width: 220px; }
.search-bar select.form-control { max-width: 140px; }

/* ===== Empty State ===== */
.empty-state { text-align: center; padding: 48px 20px; color: var(--text3); }
.empty-state i { font-size: 3rem; margin-bottom: 12px; display: block; }
.empty-state p { font-size: .9rem; }

/* ===== Loading ===== */
.loading { display: flex; align-items: center; justify-content: center; padding: 48px; gap: 10px; color: var(--text2); }
.spinner { width: 24px; height: 24px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Toast ===== */
.toast-container { position: fixed; top: 20px; right: 20px; z-index: 9999; display: flex; flex-direction: column; gap: 8px; }
.toast {
  padding: 12px 18px; border-radius: var(--radius-sm); font-size: .85rem; font-weight: 500;
  box-shadow: var(--shadow); min-width: 240px; max-width: 360px;
  animation: slideIn .3s ease; display: flex; align-items: center; gap: 10px;
}
@keyframes slideIn { from { opacity:0; transform: translateX(40px); } to { opacity:1; transform: none; } }
.toast.success { background: var(--green); color: #fff; }
.toast.error { background: var(--red); color: #fff; }
.toast.info { background: var(--primary); color: #fff; }
.toast.warning { background: var(--amber); color: #fff; }

/* ===== QR Code ===== */
.qr-wrap { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.qr-wrap img { width: 200px; height: 200px; border-radius: var(--radius-sm); border: 4px solid #fff; }
.qr-url { font-size: .75rem; color: var(--text3); word-break: break-all; text-align: center; max-width: 280px; }

/* ===== Scanner ===== */
#scanner-wrap { width: 100%; max-width: 300px; margin: 0 auto; border-radius: var(--radius-sm); overflow: hidden; }

/* ===== Login Page ===== */
.auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); }
.auth-page::before { content: ''; position: fixed; inset: 0; background: radial-gradient(ellipse at 20% 50%, rgba(59,130,246,.12) 0%, transparent 60%), radial-gradient(ellipse at 80% 20%, rgba(6,182,212,.08) 0%, transparent 50%); pointer-events: none; }
.auth-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 36px; width: 100%; max-width: 400px; box-shadow: var(--shadow); position: relative; z-index: 1; }
.auth-logo { text-align: center; margin-bottom: 28px; }
.auth-logo h1 { font-size: 1.5rem; font-weight: 800; background: linear-gradient(135deg, var(--cyan), var(--primary)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.auth-logo p { color: var(--text3); font-size: .8rem; margin-top: 4px; }

/* ===== Inline Tab Auth ===== */
.auth-tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 24px; }
.auth-tab { flex: 1; padding: 10px; text-align: center; font-size: .9rem; color: var(--text2); cursor: pointer; border-bottom: 2px solid transparent; transition: all .2s; }
.auth-tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* ===== Temp Color Scale ===== */
.temp-cold { color: var(--cyan); }
.temp-cool { color: #60a5fa; }
.temp-warm { color: var(--amber); }
.temp-hot  { color: var(--red); }

/* ===== Mini Chart Sparkline ===== */
.sparkline { width: 100%; height: 48px; }

/* ===== Mobile Header ===== */
.mobile-header {
  display: none;
  position: fixed; top: 0; left: 0; right: 0; z-index: 200;
  height: 52px; background: var(--bg2); border-bottom: 1px solid var(--border);
  align-items: center; padding: 0 14px; gap: 10px;
}
.mobile-menu-btn {
  background: none; border: none; color: var(--text); font-size: 1.1rem;
  width: 36px; height: 36px; cursor: pointer; display: flex; align-items: center;
  justify-content: center; border-radius: var(--radius-sm); flex-shrink: 0;
}
.mobile-menu-btn:hover { background: var(--bg3); }
.mobile-title { flex: 1; font-weight: 700; color: var(--cyan); font-size: .92rem; white-space: nowrap; }
.mobile-header-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.mobile-username { font-size: .78rem; color: var(--text2); max-width: 70px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mobile-logout-btn {
  background: none; border: 1px solid var(--border); color: var(--text2);
  padding: 5px 10px; border-radius: var(--radius-sm); cursor: pointer;
  font-size: .78rem; display: flex; align-items: center; gap: 5px;
  white-space: nowrap;
}
.mobile-logout-btn:hover { color: var(--red); border-color: var(--red); }

/* ===== Mobile Bottom Nav ===== */
.mobile-bottom-nav {
  display: none;
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 200;
  height: 58px; background: var(--bg2); border-top: 1px solid var(--border);
  flex-direction: row;
}
.mobile-nav-item {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px; color: var(--text3);
  cursor: pointer; font-size: .68rem; padding: 6px 0; transition: color .15s;
  -webkit-tap-highlight-color: transparent;
}
.mobile-nav-item i { font-size: 1.1rem; }
.mobile-nav-item.active { color: var(--primary); }
.mobile-nav-item:active { opacity: .7; }

/* ===== Sidebar Overlay ===== */
.sidebar-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.5); z-index: 99;
}
.sidebar-overlay.visible { display: block; }

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .mobile-header { display: flex; }
  .mobile-bottom-nav { display: flex; }
  .sidebar { transform: translateX(-100%); transition: transform .25s ease; }
  .sidebar.open { transform: translateX(0); }
  .main-content { margin-left: 0; padding: 16px; padding-top: 68px; padding-bottom: 70px; }
  .batch-bar { left: 0; bottom: 58px; }
  .device-grid { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr; }
  .fab { bottom: 72px; }
}

/* ===== Divider ===== */
.divider { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

/* ===== Utility ===== */
.flex { display: flex; }
.flex-1 { flex: 1; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }
.mt-2 { margin-top: 8px; }
.mt-3 { margin-top: 12px; }
.mb-3 { margin-bottom: 12px; }
.text-sm { font-size: .82rem; }
.text-xs { font-size: .75rem; }
.text-muted { color: var(--text2); }
.text-green { color: var(--green); }
.text-red { color: var(--red); }
.text-amber { color: var(--amber); }
.text-center { text-align: center; }
.font-mono { font-family: monospace; }
.hidden { display: none !important; }
.w-full { width: 100%; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
