/* =====================================================================
   TRƯỜNG HỌC THÔNG MINH - GIAO DIỆN
   Bảng xanh (chalkboard) + phấn vàng, nền giấy vở. Có chế độ tối.
   ===================================================================== */
:root {
  --board: #1f4d3a;          /* xanh bảng đen */
  --board-2: #275f48;
  --chalk: #f7f3e8;
  --accent: #f2b705;         /* vàng phấn */
  --accent-ink: #3d2e00;

  --bg: #f4f1ea;             /* nền giấy */
  --surface: #ffffff;
  --surface-2: #faf8f3;
  --line: #e6e0d3;
  --ink: #1d2521;
  --ink-2: #5b655f;
  --ink-3: #8b938e;

  --ok: #1f8a4c;
  --ok-bg: #e5f4ea;
  --warn: #b26a00;
  --warn-bg: #fff2d6;
  --danger: #c62828;
  --danger-bg: #fde7e7;
  --info: #1d5fa8;
  --info-bg: #e5eefa;

  --bin-giay: #c58a1c;
  --bin-kim_loai: #6b6f76;
  --bin-nhua: #1e6fd9;

  --radius: 14px;
  --shadow: 0 1px 2px rgba(29, 37, 33, .06), 0 4px 14px rgba(29, 37, 33, .05);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --board: #163a2c;
    --board-2: #1c4a38;
    --bg: #121614;
    --surface: #1b211e;
    --surface-2: #202824;
    --line: #2c3631;
    --ink: #e8ece9;
    --ink-2: #a9b3ad;
    --ink-3: #78837d;
    --ok: #4cc27f;      --ok-bg: #173323;
    --warn: #f0b03c;    --warn-bg: #3a2c10;
    --danger: #ff6b6b;  --danger-bg: #3d1a1a;
    --info: #6aa8f0;    --info-bg: #15263d;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  }
}
:root[data-theme="dark"] {
  --board: #163a2c; --board-2: #1c4a38;
  --bg: #121614; --surface: #1b211e; --surface-2: #202824; --line: #2c3631;
  --ink: #e8ece9; --ink-2: #a9b3ad; --ink-3: #78837d;
  --ok: #4cc27f; --ok-bg: #173323; --warn: #f0b03c; --warn-bg: #3a2c10;
  --danger: #ff6b6b; --danger-bg: #3d1a1a; --info: #6aa8f0; --info-bg: #15263d;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
  min-height: 100vh;
}
button { font: inherit; color: inherit; cursor: pointer; }
small { font-size: .8rem; }

/* ============ BẢNG ĐEN (HEADER) ============ */
.board {
  background:
    radial-gradient(circle at 15% 20%, rgba(255,255,255,.05), transparent 40%),
    radial-gradient(circle at 85% 80%, rgba(255,255,255,.04), transparent 45%),
    linear-gradient(135deg, var(--board), var(--board-2));
  color: var(--chalk);
  border-bottom: 6px solid #8a5a2b;   /* viền gỗ */
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.15);
}
.board-inner {
  padding: 20px 24px;
  display: flex;
  flex-wrap: wrap;
  gap: 16px 24px;
  align-items: center;
  justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-logo {
  width: 52px; height: 52px;
  display: grid; place-items: center;
  font-size: 28px;
  background: rgba(255,255,255,.1);
  border: 2px dashed rgba(247,243,232,.35);
  border-radius: 14px;
}
.brand h1 { font-size: clamp(1.3rem, 2.6vw, 1.75rem); font-weight: 800; letter-spacing: .2px; }
.brand p { font-size: .9rem; opacity: .8; }

.board-right { display: flex; align-items: center; gap: 20px; flex-wrap: wrap; }
.clock { text-align: right; }
.clock-time { font-size: 1.6rem; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; line-height: 1.1; }
.clock-date { font-size: .82rem; opacity: .8; }

.devices { display: flex; flex-direction: column; gap: 6px; }
.device-pill {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: .8rem; font-weight: 600;
  padding: 5px 12px;
  border-radius: 999px;
  background: rgba(0,0,0,.2);
  border: 1px solid rgba(255,255,255,.12);
}
.device-pill i { width: 8px; height: 8px; border-radius: 50%; background: #9aa39e; }
.device-pill.online i { background: #5ee38f; box-shadow: 0 0 0 3px rgba(94,227,143,.25); }

/* ============ BỐ CỤC ============ */
.page { padding: 24px; display: flex; flex-direction: column; gap: 28px; }
.section { display: flex; flex-direction: column; gap: 14px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.section-head h2 { font-size: 1.15rem; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.section-icon {
  display: inline-grid; place-items: center;
  width: 32px; height: 32px;
  background: var(--accent); color: var(--accent-ink);
  border-radius: 9px; font-size: 1rem;
}
.section-note { font-size: .8rem; color: var(--ink-3); }

.layout-env { display: grid; grid-template-columns: minmax(0, 1fr) minmax(320px, 24%); gap: 16px; align-items: start; }
.layout-school { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr) minmax(0, .8fr); gap: 16px; }
.layout-cams { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.layout-faces { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); gap: 16px; align-items: start; }
.layout-faces[hidden] { display: none; }
.layout-bottom { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 16px; }
/* Cho phép ô grid co nhỏ hơn nội dung -> không tràn ngang trên điện thoại */
.layout-env > *, .layout-school > *, .layout-bottom > *, .layout-cams > *, .layout-faces > *, .tiles > *, .bins > * { min-width: 0; }

/* ============ THẺ ============ */
.card, .tile {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card { padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card-head h3 { font-size: 1rem; font-weight: 700; }

.tag {
  font-size: .75rem; font-weight: 700;
  padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2);
  border: 1px solid var(--line);
  white-space: nowrap;
}
.tag-muted { font-weight: 500; }
.tag.ok { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.tag.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }

/* ============ Ô SỐ LIỆU ============ */
.tiles { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.tile { padding: 14px 16px; display: flex; flex-direction: column; gap: 6px; position: relative; overflow: hidden; }
.tile::before {   /* đường kẻ lề vở */
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px;
  background: var(--line);
}
.tile.is-ok::before { background: var(--ok); }
.tile.is-warn::before { background: var(--accent); }
.tile.is-danger::before { background: var(--danger); }
.tile.is-danger { background: var(--danger-bg); border-color: transparent; }
.tile-wide { grid-column: span 2; }
.tile-label { font-size: .82rem; font-weight: 600; color: var(--ink-2); }
.tile-value { font-size: 1.9rem; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.tile-value small { font-size: .9rem; font-weight: 600; color: var(--ink-3); margin-left: 3px; }
.tile-text { font-size: 1.35rem; }
.tile-note { font-size: .8rem; color: var(--ink-3); }
.tile.is-danger .tile-value, .tile.is-danger .tile-note { color: var(--danger); }

.meter { height: 10px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); overflow: hidden; }
.meter-fill { height: 100%; width: 0; border-radius: 999px; background: var(--ok); transition: width .6s ease, background .3s; }
.meter-fill.dry { background: var(--accent); }
.meter-fill.wet { background: var(--info); }

/* ============ ĐIỀU KHIỂN ============ */
.segmented { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border: 1px solid var(--line); border-radius: 11px; padding: 4px; gap: 4px; }
.segmented button { border: 0; background: transparent; padding: 9px 8px; border-radius: 8px; font-weight: 600; font-size: .88rem; color: var(--ink-2); }
.segmented button.active { background: var(--board); color: var(--chalk); box-shadow: var(--shadow); }
.control-hint { font-size: .8rem; color: var(--ink-3); margin-top: -4px; }

.switch-row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  text-align: left; padding: 12px; border-radius: 12px;
  background: var(--surface-2); border: 1px solid var(--line);
  transition: border-color .2s, background .2s;
}
.switch-row:hover { border-color: var(--ink-3); }
.switch-row:disabled { opacity: .6; cursor: wait; }
.switch-icon { font-size: 1.4rem; width: 38px; height: 38px; display: grid; place-items: center; background: var(--surface); border-radius: 10px; border: 1px solid var(--line); }
.switch-text { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.switch-text b { font-size: .92rem; }
.switch-text small { color: var(--ink-3); }
.switch { width: 44px; height: 26px; border-radius: 999px; background: var(--line); position: relative; flex-shrink: 0; transition: background .2s; }
.switch span { position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s; }
.switch-row.on { border-color: var(--ok); background: var(--ok-bg); }
.switch-row.on .switch { background: var(--ok); }
.switch-row.on .switch span { transform: translateX(18px); }

/* ============ CỔNG ============ */
/* Cổng trượt: 2 cánh trượt vào 2 trụ cổng khi mở */
.gate-visual {
  height: 92px; position: relative; overflow: hidden;
  display: flex; justify-content: center;
  border-bottom: 4px solid #8a5a2b;
  margin: 0 8%;
}
.gate-visual::before, .gate-visual::after {   /* 2 trụ cổng */
  content: ""; position: absolute; bottom: 0; width: 14px; height: 100%;
  background: #8a5a2b; border-radius: 4px 4px 0 0; z-index: 2;
}
.gate-visual::before { left: 0; }
.gate-visual::after { right: 0; }
.gate-door {
  width: calc(50% - 14px); height: 78%; align-self: flex-end;
  background: repeating-linear-gradient(90deg, var(--board) 0 5px, transparent 5px 15px);
  border: 3px solid var(--board); border-bottom: 0;
  transition: transform .8s ease;
}
.gate.open .gate-door.left { transform: translateX(-100%); }
.gate.open .gate-door.right { transform: translateX(100%); }

.gate-last { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 2px; }
.gate-last small { color: var(--ink-3); }
.gate-last b { font-size: 1.2rem; }
.gate-last span { font-size: .8rem; color: var(--ink-3); }

.stat-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-row > div { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px; text-align: center; display: flex; flex-direction: column; }
.stat-row b { font-size: 1.4rem; font-variant-numeric: tabular-nums; }
.stat-row small { color: var(--ink-3); }

/* ============ THÙNG RÁC ============ */
.bins { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; align-items: center; flex: 1; padding-top: 18px; }
.bin { --c: var(--bin-kim_loai); display: flex; flex-direction: column; align-items: center; }
.bin-giay { --c: var(--bin-giay); }
.bin-nhua { --c: var(--bin-nhua); }
.bin-lid {
  width: 86%; height: 12px; border-radius: 8px 8px 3px 3px;
  background: var(--c); transform-origin: left bottom;
  transition: transform .5s ease;
}
.bin.open .bin-lid { transform: rotate(-22deg); }
.bin-body {
  width: 76%; margin-top: 3px; padding: 16px 6px 12px;
  background: color-mix(in srgb, var(--c) 14%, var(--surface));
  border: 2px solid var(--c); border-top: 0;
  border-radius: 0 0 14px 14px;
  clip-path: polygon(0 0, 100% 0, 92% 100%, 8% 100%);
  display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center;
}
.bin-emoji { font-size: 1.6rem; }
.bin-body b { font-size: .9rem; color: var(--c); }
.bin-body small { color: var(--ink-2); font-weight: 600; }
.bin.open .bin-body { box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 35%, transparent); }

/* ============ PIN ============ */
.battery-card { align-items: center; text-align: center; }
.battery-card .card-head { align-self: stretch; }
.battery {
  width: 64px; height: 110px; border: 4px solid var(--ink-2); border-radius: 12px;
  position: relative; padding: 4px; display: flex; align-items: flex-end;
}
.battery::before { content: ""; position: absolute; top: -10px; left: 50%; width: 24px; height: 6px; transform: translateX(-50%); background: var(--ink-2); border-radius: 3px 3px 0 0; }
.battery-fill { width: 100%; height: 0; border-radius: 6px; background: var(--ok); transition: height .6s ease, background .3s; }
.battery-fill.mid { background: var(--accent); }
.battery-fill.low { background: var(--danger); }
.battery-value { font-size: 1.7rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.battery-value small { font-size: .9rem; color: var(--ink-3); margin-left: 2px; }

/* ============ BIỂU ĐỒ & NHẬT KÝ ============ */
.chart-wrap { position: relative; height: 280px; }
.log { list-style: none; display: flex; flex-direction: column; max-height: 300px; overflow-y: auto; }
.log li { display: flex; gap: 12px; align-items: flex-start; padding: 10px 2px; border-bottom: 1px dashed var(--line); }
.log li:last-child { border-bottom: 0; }
.log-icon { width: 34px; height: 34px; flex-shrink: 0; display: grid; place-items: center; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); }
.log-main { flex: 1; min-width: 0; }
.log-main b { font-size: .9rem; display: block; }
.log-main small { color: var(--ink-3); }
.log-time { font-size: .78rem; color: var(--ink-3); white-space: nowrap; font-variant-numeric: tabular-nums; }
.log-empty { color: var(--ink-3); justify-content: center; font-size: .9rem; }

/* ============ CAMERA AI ============ */
.cam-frame {
  position: relative; aspect-ratio: 4 / 3; border-radius: 10px; overflow: hidden;
  background: #1d2521 repeating-linear-gradient(45deg, rgba(255,255,255,.03) 0 10px, transparent 10px 20px);
}
.cam-frame img { width: 100%; height: 100%; object-fit: contain; display: block; }
.cam-status { font-size: .85rem; color: var(--ink-2); min-height: 1.3em; }

/* ============ NÚT & Ô NHẬP ============ */
.btn {
  border: 1px solid var(--line); background: var(--surface-2); color: var(--ink);
  padding: 9px 14px; border-radius: 10px; font-weight: 600; font-size: .88rem;
}
.btn:hover { border-color: var(--ink-3); }
.btn:disabled { opacity: .55; cursor: wait; }
.btn-primary { background: var(--board); color: var(--chalk); border-color: transparent; }
.btn-ghost { background: transparent; padding: 6px 10px; font-size: .8rem; }
.btn-danger { color: var(--danger); background: transparent; border-color: transparent; padding: 6px 10px; }
.btn-danger:hover { background: var(--danger-bg); }
input[type="text"], input[type="password"], input:not([type]), input[type="file"] {
  width: 100%; font: inherit; color: var(--ink);
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
}
input:focus-visible, .btn:focus-visible, .switch-row:focus-visible, .segmented button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
input[type="file"] { padding: 8px; font-size: .85rem; }
.form-msg { color: var(--ink-3); min-height: 1.2em; }
.form-msg.ok { color: var(--ok); }
.form-msg.err { color: var(--danger); }

/* ============ QUẢN LÝ KHUÔN MẶT ============ */
.unlock { display: flex; flex-direction: column; gap: 10px; max-width: 520px; }
.unlock-row { display: flex; gap: 10px; }
.unlock-row .btn { white-space: nowrap; }
.face-form { display: flex; flex-direction: column; gap: 10px; }
.face-form > label { font-weight: 600; font-size: .88rem; }
.face-ways { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.face-way {
  display: flex; flex-direction: column; gap: 8px; padding: 14px;
  border: 1px dashed var(--line); border-radius: 12px; background: var(--surface-2);
}
.face-way small { color: var(--ink-3); flex: 1; }
.people { list-style: none; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.person {
  display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center;
  padding: 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2);
}
.person img {
  width: 76px; height: 76px; border-radius: 50%; object-fit: cover;
  border: 3px solid var(--surface); box-shadow: 0 0 0 2px var(--ok); background: var(--line);
}
.person b { font-size: .92rem; overflow-wrap: anywhere; }
.person small { color: var(--ink-3); }
.person.warn img { box-shadow: 0 0 0 2px var(--accent); }
.people-empty { grid-column: 1 / -1; color: var(--ink-3); text-align: center; padding: 18px; }

/* ============ CẢNH BÁO ============ */
.alert {
  display: flex; gap: 14px; align-items: center;
  padding: 14px 18px; border-radius: var(--radius);
  background: var(--danger); color: #fff;
  animation: pulse 1.2s ease-in-out infinite;
}
.alert[hidden] { display: none; }
.alert-icon { font-size: 1.8rem; }
.alert strong { font-size: 1.05rem; }
.alert p { font-size: .9rem; opacity: .95; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(198,40,40,.25); } }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: var(--ink); color: var(--bg);
  padding: 10px 18px; border-radius: 10px; font-size: .88rem; box-shadow: var(--shadow);
  max-width: calc(100% - 32px);
}
.toast[hidden] { display: none; }
.footer { text-align: center; color: var(--ink-3); font-size: .8rem; padding: 8px 16px 28px; }

/* ============ ĐÁP ỨNG MÀN HÌNH ============ */
@media (max-width: 1100px) {
  .layout-env { grid-template-columns: 1fr; }
  .layout-school { grid-template-columns: 1fr 1fr; }
  .battery-card { grid-column: span 2; flex-direction: row; justify-content: space-around; flex-wrap: wrap; }
  .battery-card .card-head { flex-basis: 100%; }
}
@media (max-width: 760px) {
  .page, .board-inner { padding: 16px; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .layout-school, .layout-bottom, .layout-cams, .layout-faces, .face-ways { grid-template-columns: 1fr; }
  .unlock-row { flex-direction: column; }
  .battery-card { grid-column: auto; }
  .board-right { width: 100%; justify-content: space-between; }
  .brand p { font-size: .82rem; }
  .clock { text-align: left; }
  .devices { flex-direction: row; flex-wrap: wrap; }
  .bins-card .card-head, .battery-card .card-head { flex-wrap: wrap; }
  .segmented button { font-size: .82rem; }
}
@media (max-width: 380px) {
  .tile-value { font-size: 1.6rem; }
  .bins { gap: 6px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
