:root {
  --bg: #14171a;
  --surface: #1c2024;
  --surface-2: #23282d;
  --border: #2e343a;
  --text: #e8e9eb;
  --text-muted: #8a8f98;
  --accent: #f5a623;
  --online: #3ecf8e;
  --offline: #e5484d;
  --mono: "JetBrains Mono", "SFMono-Regular", Consolas, monospace;
  --sans: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
}

a { color: inherit; text-decoration: none; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}

.brand {
  font-family: var(--mono);
  font-weight: 700;
  letter-spacing: 0.5px;
  font-size: 14px;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent); }

nav { display: flex; gap: 4px; }
nav a {
  padding: 7px 14px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-muted);
  font-family: var(--mono);
}
nav a:hover, nav a.active { background: var(--surface-2); color: var(--text); }

.container { max-width: 1080px; margin: 0 auto; padding: 28px 20px 60px; }

.page-header { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 20px; }
.page-header h1 { font-size: 20px; margin: 0; }
.page-header p { color: var(--text-muted); font-size: 13px; margin: 4px 0 0; }

/* ---------- Card / module ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px;
}

.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }

.device-card { display: flex; flex-direction: column; gap: 10px; cursor: pointer; transition: border-color .15s; }
.device-card:hover { border-color: var(--accent); }

.device-card-head { display: flex; align-items: center; justify-content: space-between; }
.device-name { font-weight: 600; font-size: 15px; }
.device-id { font-family: var(--mono); font-size: 11px; color: var(--text-muted); }

.status-led { width: 9px; height: 9px; border-radius: 50%; display: inline-block; margin-right: 6px; }
.status-led.online { background: var(--online); box-shadow: 0 0 6px var(--online); }
.status-led.offline { background: var(--offline); }

.status-label { font-family: var(--mono); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; display: flex; align-items: center; }

.meta-row { font-size: 12px; color: var(--text-muted); display: flex; justify-content: space-between; }

/* ---------- Pin module (signature element) ---------- */
.pin-module {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.pin-id {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  white-space: nowrap;
}
.pin-label { font-size: 13px; font-weight: 500; }
.pin-sub { font-size: 11px; color: var(--text-muted); font-family: var(--mono); }

.toggle {
  width: 42px; height: 22px; border-radius: 11px;
  background: var(--border); position: relative; cursor: pointer; flex-shrink: 0;
  transition: background .15s;
}
.toggle.on { background: var(--online); }
.toggle .knob {
  position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; transition: left .15s;
}
.toggle.on .knob { left: 22px; }

.sensor-value { font-family: var(--mono); font-size: 18px; color: var(--accent); }

/* ---------- Buttons / forms ---------- */
.btn {
  font-family: var(--sans); font-size: 13px; font-weight: 600;
  padding: 8px 14px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #1a1200; border-color: var(--accent); }
.btn.danger:hover { border-color: var(--offline); }

input, select {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 8px 10px; font-family: var(--sans); font-size: 13px;
}
label { font-size: 12px; color: var(--text-muted); display: block; margin-bottom: 4px; }
.field { margin-bottom: 12px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); }
th { color: var(--text-muted); font-weight: 500; font-family: var(--mono); font-size: 11px; text-transform: uppercase; }

.empty-state { text-align: center; padding: 50px 20px; color: var(--text-muted); }
.empty-state .glyph { font-family: var(--mono); font-size: 28px; color: var(--border); margin-bottom: 10px; }

.badge { font-family: var(--mono); font-size: 10px; padding: 2px 6px; border-radius: 4px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text-muted); }

/* Chặn input/select có width cố định (px) tràn ra ngoài màn hình hẹp */
input, select, textarea { max-width: 100%; }

.form-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }

/* ---------- Sơ đồ đấu dây (hiện theo loại cảm biến/chân được chọn) ---------- */
.wiring-box {
  margin-top: 10px;
  padding: 12px 14px;
  background: var(--bg);
  border: 1px dashed var(--border);
  border-radius: 8px;
}
.wiring-box .wiring-title { font-size: 12px; font-weight: 600; color: var(--accent); margin-bottom: 6px; }
.wiring-box pre {
  margin: 0 0 6px; white-space: pre-wrap; word-break: break-word;
  font-family: var(--mono); font-size: 12px; color: var(--text); line-height: 1.6;
}
.wiring-box .wiring-note { font-size: 11px; color: var(--text-muted); margin: 0; }

/* ---------- Bảng cuộn ngang trên màn hình hẹp thay vì bị bóp méo ---------- */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-scroll table { min-width: 540px; }

/* ---------- Responsive: điện thoại / màn hình hẹp ---------- */
@media (max-width: 720px) {
  .container { padding: 18px 14px 50px; }
  .topbar { padding: 10px 14px; flex-wrap: wrap; row-gap: 8px; }
  .topbar nav {
    width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch;
    padding-bottom: 2px; justify-content: flex-start;
  }
  .topbar nav a { flex-shrink: 0; }

  .page-header { flex-direction: column; align-items: stretch; gap: 12px; }
  .page-header > div:last-child { display: flex; flex-wrap: wrap; gap: 8px; }
  .page-header > div:last-child > .btn,
  .page-header > div:last-child > a.btn { flex: 1 1 auto; text-align: center; }
  .page-header > .btn, .page-header > a.btn { width: 100%; text-align: center; }

  .card { padding: 14px; }
  .grid { grid-template-columns: 1fr; }

  /* Form: mỗi field xuống hàng riêng, input/select rộng hết cỡ, dễ chạm trên điện thoại */
  .field { width: 100% !important; }
  .field input, .field select { width: 100% !important; }
  .form-row > .btn { width: 100%; }

  .pin-module { flex-wrap: wrap; row-gap: 10px; }
  .pin-module > div:last-child { text-align: left !important; width: 100%; }
  .pin-module > div:last-child > div[style*="margin-top:8px"] { justify-content: flex-start !important; }

  table { font-size: 12px; }
  th, td { padding: 8px 6px; }
}

/* ---------- Trang đăng nhập / khởi tạo tài khoản / tài khoản ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 26px;
}
.auth-brand {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; letter-spacing: 0.04em; color: var(--text-muted);
  margin-bottom: 18px; text-transform: uppercase;
}
.auth-card h1 { font-size: 19px; margin: 0 0 6px; }
.auth-card p.sub { font-size: 13px; color: var(--text-muted); margin: 0 0 20px; }
.auth-card .field { margin-bottom: 14px; }
.auth-card .field label { display: block; font-size: 12px; color: var(--text-muted); margin-bottom: 5px; }
.auth-card input {
  width: 100%; padding: 10px 12px; border-radius: 8px;
  background: var(--bg); border: 1px solid var(--border); color: var(--text); font-size: 14px;
}
.auth-card input:focus { outline: none; border-color: var(--accent); }
.auth-card .btn.primary { width: 100%; padding: 11px; font-size: 14px; margin-top: 4px; }
.auth-error {
  background: rgba(229, 72, 77, 0.12); border: 1px solid var(--offline); color: #ff9a9d;
  font-size: 13px; padding: 9px 12px; border-radius: 8px; margin-bottom: 16px;
}
.auth-success {
  background: rgba(62, 207, 142, 0.12); border: 1px solid var(--online); color: #8ef0c2;
  font-size: 13px; padding: 9px 12px; border-radius: 8px; margin-bottom: 16px;
}
.auth-foot { margin-top: 16px; font-size: 12px; color: var(--text-muted); text-align: center; }
.auth-foot a { color: var(--accent); }
