/* ─────────────────────────────────────────────────────────────────────────
   Hydroponic Control — bảng màu sáng, dùng chung cho trang đăng nhập và
   trang điều khiển. Mọi màu đều khai báo ở :root; không hard-code màu trong
   component, để đổi tông cả hệ thống chỉ sửa một chỗ.
   ───────────────────────────────────────────────────────────────────────── */
:root {
  --bg: #f4f6fa;
  --bg-soft: #eef2f7;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;

  --text: #0f172a;
  --text-2: #475569;
  --text-3: #94a3b8;

  --brand: #0f9d58;
  --brand-dark: #0b7d45;
  --brand-soft: #e6f6ee;

  --blue: #2563eb;   --blue-soft: #e8effd;
  --amber: #d97706;  --amber-soft: #fdf1e0;
  --violet: #7c3aed; --violet-soft: #f1eafe;
  --cyan: #0891b2;   --cyan-soft: #e0f4f8;
  --red: #dc2626;    --red-soft: #fdeaea;
  --slate: #64748b;  --slate-soft: #eef1f5;

  --radius: 14px;
  --radius-sm: 9px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, .06);
  --shadow: 0 1px 3px rgba(15, 23, 42, .07), 0 8px 24px -12px rgba(15, 23, 42, .18);
  --shadow-lg: 0 12px 40px -12px rgba(15, 23, 42, .25);

  --sidebar-w: 244px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

/* .btn đặt display:inline-flex, mà độ ưu tiên của class cao hơn [hidden] trong
   bảng mặc định của trình duyệt — thiếu dòng này thì el.hidden = true không
   giấu được nút nào. */
[hidden] { display: none !important; }

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--brand-dark); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Khung trang ───────────────────────────────────────────────────────── */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  z-index: 40;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 20px 18px;
  border-bottom: 1px solid var(--border);
}
.brand-mark {
  width: 36px; height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--brand) 0%, #34c77b 100%);
  display: grid; place-items: center;
  color: #fff; font-size: 18px;
  box-shadow: 0 4px 12px -4px rgba(15, 157, 88, .6);
}
.brand-text b { display: block; font-size: 14px; font-weight: 650; letter-spacing: -.01em; }
.brand-text span { display: block; font-size: 11px; color: var(--text-3); }

.nav { padding: 12px 10px; flex: 1; overflow-y: auto; }
.nav-label {
  font-size: 10.5px; font-weight: 600; letter-spacing: .08em;
  text-transform: uppercase; color: var(--text-3);
  padding: 14px 10px 6px;
}
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; margin-bottom: 2px;
  border-radius: var(--radius-sm);
  color: var(--text-2); font-weight: 500;
  cursor: pointer; user-select: none;
  border: 1px solid transparent;
  transition: background .12s, color .12s;
}
.nav-item:hover { background: var(--bg-soft); color: var(--text); }
.nav-item.active {
  background: var(--brand-soft);
  color: var(--brand-dark);
  border-color: #cceadb;
  font-weight: 600;
}
.nav-item .ico { width: 18px; text-align: center; font-size: 15px; }
.nav-item .badge {
  margin-left: auto; font-size: 10px; font-weight: 700;
  background: var(--red); color: #fff;
  border-radius: 20px; padding: 1px 6px;
}

.sidebar-foot {
  padding: 12px; border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
}
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--slate-soft); color: var(--text-2);
  display: grid; place-items: center; font-weight: 650; font-size: 13px;
  flex: 0 0 32px;
}
.sidebar-foot .who { flex: 1; min-width: 0; }
.sidebar-foot .who b { display: block; font-size: 13px; font-weight: 600; }
.sidebar-foot .who span { display: block; font-size: 11px; color: var(--text-3); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  background: rgba(255, 255, 255, .88);
  backdrop-filter: saturate(180%) blur(8px);
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
}
.topbar h1 { font-size: 17px; font-weight: 650; letter-spacing: -.015em; }
.topbar .sub { font-size: 12px; color: var(--text-3); }
.topbar .spacer { flex: 1; }

.content { padding: 22px 24px 48px; max-width: 1440px; width: 100%; }

.view { display: none; }
.view.active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ── Nút gạt sidebar trên mobile ───────────────────────────────────────── */
.menu-btn { display: none; }
.scrim { display: none; }

/* ── Thẻ ───────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.card-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.card-head h3 { font-size: 14px; font-weight: 620; letter-spacing: -.01em; }
.card-head .hint { font-size: 12px; color: var(--text-3); }
.card-head .spacer { flex: 1; }
.card-body { padding: 16px; }

.section-title {
  display: flex; align-items: baseline; gap: 10px;
  margin: 26px 0 12px;
}
.section-title:first-child { margin-top: 0; }
.section-title h2 { font-size: 15px; font-weight: 650; letter-spacing: -.01em; }
.section-title p { font-size: 12px; color: var(--text-3); }

.grid { display: grid; gap: 14px; }
.g-metrics { grid-template-columns: repeat(auto-fill, minmax(215px, 1fr)); }
.g-charts  { grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); }
.g-relays  { grid-template-columns: repeat(auto-fill, minmax(232px, 1fr)); }
.g-2       { grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); }

/* ── Thẻ số đo ─────────────────────────────────────────────────────────── */
.metric {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 15px 16px 13px;
  box-shadow: var(--shadow-sm);
  position: relative; overflow: hidden;
  transition: box-shadow .15s, transform .15s;
}
.metric:hover { box-shadow: var(--shadow); transform: translateY(-1px); }
.metric::after {
  content: ''; position: absolute; inset: 0 0 auto 0; height: 3px;
  background: var(--accent, var(--brand));
  opacity: .9;
}
.metric.stale { opacity: .62; }
.metric-top { display: flex; align-items: center; gap: 9px; margin-bottom: 9px; }
.metric-ico {
  width: 30px; height: 30px; border-radius: 9px;
  display: grid; place-items: center; font-size: 15px;
  background: var(--accent-soft, var(--brand-soft));
}
.metric-name { font-size: 12.5px; font-weight: 560; color: var(--text-2); }
.metric-val { font-size: 30px; font-weight: 640; letter-spacing: -.03em; line-height: 1.1; }
.metric-val small { font-size: 13px; font-weight: 550; color: var(--text-3); margin-left: 3px; letter-spacing: 0; }
.metric-foot {
  margin-top: 10px; padding-top: 9px; border-top: 1px dashed var(--border);
  display: flex; gap: 14px; flex-wrap: wrap;
  font-size: 11.5px; color: var(--text-3);
}
.metric-foot b { color: var(--text-2); font-weight: 600; }

/* ── Trạng thái / nhãn ─────────────────────────────────────────────────── */
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px;
  font-size: 11.5px; font-weight: 580;
  background: var(--slate-soft); color: var(--text-2);
  border: 1px solid transparent;
  white-space: nowrap;
}
.pill.ok   { background: var(--brand-soft); color: var(--brand-dark); border-color: #cceadb; }
.pill.warn { background: var(--amber-soft); color: var(--amber); border-color: #f6e0bf; }
.pill.bad  { background: var(--red-soft);   color: var(--red);   border-color: #f6cccc; }
.pill.info { background: var(--blue-soft);  color: var(--blue);  border-color: #d3e0fb; }

.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); flex: 0 0 7px; }
.dot.on  { background: var(--brand); box-shadow: 0 0 0 3px rgba(15, 157, 88, .16); }
.dot.off { background: var(--red);   box-shadow: 0 0 0 3px rgba(220, 38, 38, .13); }
.dot.live { animation: pulse 2s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .45; } }

/* ── Nút ───────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 8px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text);
  font-family: var(--font); font-size: 13px; font-weight: 560;
  cursor: pointer; white-space: nowrap;
  transition: background .12s, border-color .12s, box-shadow .12s, transform .06s;
}
.btn:hover { background: var(--surface-2); border-color: var(--text-3); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary {
  background: var(--brand); border-color: var(--brand); color: #fff;
  box-shadow: 0 1px 2px rgba(15, 157, 88, .35);
}
.btn.primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn.danger:hover { filter: brightness(.94); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--text-2); }
.btn.ghost:hover { background: var(--bg-soft); }
.btn.sm { padding: 5px 10px; font-size: 12px; border-radius: 7px; }
.btn.icon { padding: 6px 8px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* ── Ô nhập ────────────────────────────────────────────────────────────── */
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field label { font-size: 11.5px; font-weight: 580; color: var(--text-2); }
.field .help { font-size: 11px; color: var(--text-3); }

input[type=text], input[type=password], input[type=number], input[type=time], select, textarea {
  width: 100%;
  padding: 8px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font); font-size: 13px;
  transition: border-color .12s, box-shadow .12s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(15, 157, 88, .14);
}
input::placeholder { color: var(--text-3); }
select { cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath fill='%2394a3b8' d='M4.5 6.5 8 10l3.5-3.5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 8px center; background-size: 16px;
  padding-right: 28px;
}

.form-row { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-items: end; }

.switch { position: relative; display: inline-block; width: 40px; height: 22px; flex: 0 0 40px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--border-strong); border-radius: 22px; transition: background .16s;
}
.switch span::before {
  content: ''; position: absolute; height: 16px; width: 16px; left: 3px; bottom: 3px;
  background: #fff; border-radius: 50%; transition: transform .16s;
  box-shadow: var(--shadow-sm);
}
.switch input:checked + span { background: var(--brand); }
.switch input:checked + span::before { transform: translateX(18px); }

/* ── Thẻ relay ─────────────────────────────────────────────────────────── */
.relay {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow-sm);
  display: flex; flex-direction: column; gap: 11px;
  transition: border-color .15s, box-shadow .15s;
}
.relay.on { border-color: #b9e3cd; box-shadow: 0 0 0 3px rgba(15, 157, 88, .08); }
.relay-top { display: flex; align-items: flex-start; gap: 9px; }
.relay-name { font-weight: 600; font-size: 13.5px; flex: 1; }
.relay-meta { font-size: 11.5px; color: var(--text-3); }
.relay-btn {
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text-2);
  border-radius: var(--radius-sm);
  padding: 10px; font-size: 13px; font-weight: 620;
  cursor: pointer; width: 100%;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  transition: all .14s;
}
.relay-btn:hover { border-color: var(--text-3); }
.relay-btn.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.relay-btn.on:hover { background: var(--brand-dark); }
.relay-btn:disabled { opacity: .55; cursor: not-allowed; }

.spin {
  width: 14px; height: 14px; flex: 0 0 14px;
  border: 2px solid currentColor; border-right-color: transparent;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  opacity: .85;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Nút đang chờ bo mạch xác nhận: vẫn đọc được trạng thái cũ, nhưng bấm tiếp
   thì không ăn — hai lệnh chồng nhau trên cùng một cái bơm là thứ không ai
   muốn gỡ. */
.relay-btn.busy { cursor: progress; }
.relay.busy { border-color: var(--border-strong); }
.relay-wait {
  height: 3px; border-radius: 3px; background: var(--bg-soft);
  overflow: hidden; margin-top: -4px;
}
.relay-wait i {
  display: block; height: 100%; width: 40%; border-radius: 3px;
  background: var(--brand);
  animation: slide-lr 1.1s ease-in-out infinite;
}
@keyframes slide-lr {
  0%   { transform: translateX(-100%); }
  100% { transform: translateX(250%); }
}

/* ── Bảng ──────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; font-size: 11px; font-weight: 620;
  text-transform: uppercase; letter-spacing: .05em; color: var(--text-3);
  padding: 9px 12px; border-bottom: 1px solid var(--border);
  background: var(--surface-2); white-space: nowrap;
}
td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
td.actions { text-align: right; white-space: nowrap; }
td.actions .btn + .btn { margin-left: 6px; }

/* Luật đang tắt: vẫn đọc được nhưng nhạt đi, để không bị nhầm với luật đang
   canh gác. Gạch ngang thì quá tay — nó vẫn là luật, chỉ đang nghỉ. */
tbody tr.off td { opacity: .5; }
tbody tr.off:hover td { opacity: .75; }

/* Hàng đang được sửa ở form phía trên. */
tbody tr.editing { background: var(--brand-soft); }
tbody tr.editing:hover { background: var(--brand-soft); }
tbody tr.editing td { border-color: #cceadb; }
code, .mono { font-family: var(--mono); font-size: 12px; }

.empty {
  padding: 34px 16px; text-align: center; color: var(--text-3);
}
.empty .big { font-size: 26px; display: block; margin-bottom: 6px; opacity: .55; }

/* ── Biểu đồ ───────────────────────────────────────────────────────────── */
.chart-box { height: 250px; position: relative; }

/* ── AI ────────────────────────────────────────────────────────────────── */
.chat {
  height: 420px; overflow-y: auto;
  padding: 16px; display: flex; flex-direction: column; gap: 12px;
  background: var(--surface-2);
}
.msg { display: flex; gap: 10px; max-width: 86%; }
.msg.me { align-self: flex-end; flex-direction: row-reverse; }
.msg-ava {
  width: 28px; height: 28px; flex: 0 0 28px; border-radius: 8px;
  display: grid; place-items: center; font-size: 14px;
  background: var(--brand-soft);
}
.msg.me .msg-ava { background: var(--blue-soft); }
.msg-body {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 10px 13px;
  font-size: 13px; white-space: pre-wrap; word-break: break-word;
  box-shadow: var(--shadow-sm);
}
.msg.me .msg-body { background: var(--blue); border-color: var(--blue); color: #fff; }
.msg-body h4 { font-size: 13px; margin: 8px 0 4px; }
.msg-body h4:first-child { margin-top: 0; }
.chat-input { display: flex; gap: 9px; padding: 12px; border-top: 1px solid var(--border); background: var(--surface); }
.typing { display: inline-flex; gap: 4px; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%; background: var(--text-3);
  animation: bounce 1.2s infinite;
}
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-4px); opacity: 1; } }

/* ── Thiết bị ──────────────────────────────────────────────────────────── */
.device {
  display: flex; gap: 14px; align-items: flex-start;
  padding: 15px 16px; border-bottom: 1px solid var(--border);
}
.device:last-child { border-bottom: none; }
.device-ico {
  width: 40px; height: 40px; flex: 0 0 40px; border-radius: 11px;
  display: grid; place-items: center; font-size: 19px;
  background: var(--bg-soft);
}
.device-main { flex: 1; min-width: 0; }
.device-main b { font-size: 13.5px; font-weight: 620; }
.device-kv { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 6px; font-size: 12px; color: var(--text-3); }
.device-kv span b { font-weight: 600; color: var(--text-2); font-size: 12px; }

/* ── Phát hiện của bộ chẩn đoán ────────────────────────────────────────── */
.finding {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--slate);
  border-radius: var(--radius);
  padding: 15px 17px;
  box-shadow: var(--shadow-sm);
}
.finding.critical { border-left-color: var(--red); }
.finding.warn     { border-left-color: var(--amber); }
.finding.info     { border-left-color: var(--blue); }
.finding h4 { font-size: 14px; font-weight: 630; display: flex; align-items: center; gap: 8px; }
.finding p { font-size: 13px; color: var(--text-2); margin-top: 6px; }
.finding ul { margin: 10px 0 0 18px; font-size: 12.5px; color: var(--text-2); }
.finding li { margin-bottom: 3px; }
.finding .when { font-size: 11.5px; color: var(--text-3); margin-top: 9px; }

/* Dải cảnh báo trên trang Tổng quan. Không phải toast: toast biến mất, còn
   một cái bơm chạy vô ích thì không tự biến mất. */
.alertbar {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 16px; margin-bottom: 18px;
  border-radius: var(--radius);
  border: 1px solid #f6cccc; background: var(--red-soft); color: var(--red);
  font-size: 13.5px;
}
.alertbar.warn { border-color: #f6e0bf; background: var(--amber-soft); color: var(--amber); }
.alertbar b { font-weight: 650; }
.alertbar .spacer { flex: 1; }

/* ── Thông báo nổi ─────────────────────────────────────────────────────── */
.toasts {
  position: fixed; right: 18px; bottom: 18px; z-index: 90;
  display: flex; flex-direction: column; gap: 9px; max-width: 340px;
}
.toast {
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--brand);
  border-radius: 10px; padding: 11px 14px;
  box-shadow: var(--shadow-lg); font-size: 13px;
  animation: slide .2s ease;
}
.toast.err { border-left-color: var(--red); }
.toast.warn { border-left-color: var(--amber); }
@keyframes slide { from { opacity: 0; transform: translateX(16px); } to { opacity: 1; transform: none; } }

/* ── Bộ chọn khoảng thời gian ──────────────────────────────────────────── */
.segmented {
  display: inline-flex; background: var(--bg-soft);
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 2px;
}
.segmented button {
  border: none; background: transparent; cursor: pointer;
  font-family: var(--font); font-size: 12px; font-weight: 560; color: var(--text-2);
  padding: 5px 11px; border-radius: 7px;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }

.skeleton {
  background: linear-gradient(90deg, var(--bg-soft) 25%, #e7ecf3 50%, var(--bg-soft) 75%);
  background-size: 200% 100%; animation: shimmer 1.3s infinite;
  border-radius: 6px; color: transparent;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* ── Màn hình hẹp ──────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%); transition: transform .2s ease;
    box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-flex; }
  .scrim.show { display: block; position: fixed; inset: 0; background: rgba(15,23,42,.4); z-index: 35; }
  .content { padding: 16px 14px 40px; }
  .topbar { padding: 10px 14px; }
  .metric-val { font-size: 26px; }
}
