:root {
  --bg: #f3f5fa;
  --card: #ffffff;
  --text: #1c2333;
  --muted: #6b7489;
  --primary: #3b6cf6;
  --primary-dark: #2a52c9;
  --border: #e1e6f0;
  --plus: #16a34a;
  --minus: #dc2626;
  --tot: #16a34a;
  --kha: #2563eb;
  --dat: #d97706;
  --chua-dat: #dc2626;
  --radius: 12px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

body {
  font-family: "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

h2 { margin-bottom: 16px; }
h3 { margin-bottom: 10px; font-size: 1.05rem; }

/* ---------- Đăng nhập ---------- */
#login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(135deg, #3b6cf6, #7c3aed);
}
#login-form {
  background: var(--card);
  width: 100%;
  max-width: 380px;
  padding: 28px;
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#login-form p { color: var(--muted); margin-bottom: 8px; }
#login-error { color: var(--minus) !important; font-size: .9rem; }

/* ---------- Form chung ---------- */
label { font-size: .9rem; font-weight: 600; }
input, select, textarea, button { font: inherit; }
input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--primary);
  border-color: transparent;
}
input[type="checkbox"] { width: auto; margin-right: 6px; }

button {
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
}
button:hover { background: var(--primary-dark); }
button.danger { background: var(--minus); }
button.small { padding: 5px 10px; font-size: .85rem; }

/* ---------- Thanh trên ---------- */
#topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 20px;
  padding: 10px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; }
#main-nav { display: flex; flex-wrap: wrap; gap: 6px; flex: 1; }
#main-nav button { background: transparent; color: var(--text); }
#main-nav button:hover { background: var(--bg); }
#main-nav button.active { background: var(--primary); color: #fff; }
.user-box { display: flex; align-items: center; gap: 8px; font-size: .9rem; }
#current-user-role { color: var(--muted); }
.user-box button { background: var(--muted); }

/* ---------- Nội dung ---------- */
main { max-width: 1100px; margin: 0 auto; padding: 20px; }
article, #record-form {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.stat-card { margin: 0; text-align: center; }
.stat-card h3 { color: var(--muted); font-size: .85rem; font-weight: 600; }
.stat-card p { font-size: 1.8rem; font-weight: 700; }

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
}
ol, ul { padding-left: 20px; }
li { margin-bottom: 4px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
.toolbar > * { flex: 1; min-width: 140px; }

/* ---------- Bảng ---------- */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--card);
  border-radius: var(--radius);
  display: block;
  overflow-x: auto;
}
th, td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
th { background: #eef2fb; font-size: .85rem; }
td:nth-child(3), td:nth-child(5) { white-space: normal; }

.plus { color: var(--plus); font-weight: 700; }
.minus { color: var(--minus); font-weight: 700; }

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  color: #fff;
  font-size: .8rem;
  font-weight: 600;
}
.badge.tot { background: var(--tot); }
.badge.kha { background: var(--kha); }
.badge.dat { background: var(--dat); }
.badge.chua-dat { background: var(--chua-dat); }

/* ---------- Form ghi nhận / quản trị ---------- */
#record-form { max-width: 520px; display: flex; flex-direction: column; gap: 8px; }
.preview {
  padding: 10px;
  border-radius: 8px;
  background: var(--bg);
  margin: 6px 0;
}

#account-form, #student-form, #behavior-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}
#account-form > *, #student-form > *, #behavior-form > * { flex: 1; min-width: 160px; }
fieldset {
  flex-basis: 100% !important;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 6px;
}
legend { padding: 0 6px; font-weight: 600; }
fieldset label { font-weight: 400; }

.profile-card p { margin-bottom: 4px; }

@media (max-width: 600px) {
  main { padding: 12px; }
  #topbar { padding: 10px 12px; }
}

/* ================= NAVBAR GỌN ================= */
#topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px 20px;
}
#main-nav { justify-content: center; }
#main-nav button { white-space: nowrap; }

@media (max-width: 820px) {
  #topbar { grid-template-columns: 1fr auto; }
  .brand { grid-column: 1; grid-row: 1; }
  .user-box { grid-column: 2; grid-row: 1; }
  #main-nav {
    grid-column: 1 / -1;
    grid-row: 2;
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    scrollbar-width: none;
  }
  #main-nav::-webkit-scrollbar { display: none; }
  #main-nav button { flex-shrink: 0; }
}
@media (max-width: 480px) {
  #current-user-role { display: none; }
  .brand { font-size: 1rem; }
}

/* ================= ANIMATION ================= */
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: none; }
}
@keyframes fadeOut {
  to { opacity: 0; transform: scale(1.04); }
}
@keyframes dropIn {
  from { opacity: 0; transform: translateY(-100%); }
  to   { opacity: 1; transform: none; }
}

#login-form { animation: fadeUp .6s ease both; }
#login-page.leaving { animation: fadeOut .4s ease forwards; }

#app:not([hidden]) #topbar { animation: dropIn .5s ease both; }
#app:not([hidden]) main { animation: fadeUp .6s .15s ease both; }
.page:not([hidden]) { animation: fadeUp .35s ease both; }

.stat-card { animation: fadeUp .5s ease both; }
.stat-card:nth-child(1) { animation-delay: .10s; }
.stat-card:nth-child(2) { animation-delay: .16s; }
.stat-card:nth-child(3) { animation-delay: .22s; }
.stat-card:nth-child(4) { animation-delay: .28s; }
.stat-card:nth-child(5) { animation-delay: .34s; }
.stat-card:nth-child(6) { animation-delay: .40s; }

button { transition: background .2s, transform .1s; }
button:active { transform: scale(.97); }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* ================= DARK MODE ================= */
:root { --th: #eef2fb; }
[data-theme="dark"] {
  --bg: #0f1420;
  --card: #1a2133;
  --text: #e8ecf5;
  --muted: #9aa4bb;
  --border: #2a3550;
  --primary: #5b84ff;
  --primary-dark: #4a6fe0;
  --th: #222c45;
  color-scheme: dark;
}
[data-theme="dark"] #login-page { background: linear-gradient(135deg, #1b2a5e, #3b1d6e); }
[data-theme="dark"] .user-box button:not(.theme-btn) { background: #3a4561; }

th { background: var(--th); }
input, select, textarea { color: var(--text); }
[data-theme="dark"] input,
[data-theme="dark"] select,
[data-theme="dark"] textarea { background: #131a2b; }
body { transition: background .3s, color .3s; }

/* Nút đổi giao diện */
.theme-btn {
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 1.1rem;
  line-height: 1;
}
.theme-btn:hover { background: var(--border); }
.login-theme { position: fixed; top: 14px; right: 14px; z-index: 20; }

/* ================= BỎ KHỐI XANH KHI BẤM NÚT ================= */
* { -webkit-tap-highlight-color: transparent; }
button {
  -webkit-user-select: none;
  user-select: none;
  outline: none;
}
button:focus, button:focus-visible { outline: none; }

/* ================= CÔNG TẮC ĐĂNG NHẬP ================= */
.switch-row {
  display: flex;
  align-items: center;
  gap: 12px;
  position: relative;
  font-weight: 400;
  font-size: .92rem;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  margin-top: 4px;
}
.switch-row input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.switch {
  flex: 0 0 44px;
  width: 44px;
  height: 25px;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  transition: background .25s;
}
.switch::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 19px;
  height: 19px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .25s;
}
.switch-row input:checked + .switch { background: var(--primary); }
.switch-row input:checked + .switch::after { transform: translateX(19px); }
.switch-text small {
  display: block;
  color: var(--muted);
  font-size: .78rem;
  line-height: 1.3;
}
