:root {
  --sidebar-width: 248px;
  --sidebar-collapsed-width: 64px;
  --topbar-height: 56px;

  --color-bg: #f4f6fa;
  --color-surface: #ffffff;
  --color-border: #e3e7ee;
  --color-text: #1f2937;
  --color-muted: #6b7280;
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-primary-soft: #e8efff;
  --color-danger: #dc2626;
  --color-success: #16a34a;

  --sidebar-bg: #111827;
  --sidebar-text: #cbd5e1;
  --sidebar-text-active: #ffffff;
  --sidebar-hover: #1f2937;
  --sidebar-active: #2563eb;

  --radius: 8px;
  --transition: 0.2s ease;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--color-text);
  background: var(--color-bg);
}

h1, h2, h3 { margin: 0; }

/* ---------- Login ---------- */

.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: linear-gradient(135deg, #1e3a8a, #111827);
}

.login-card {
  width: 100%;
  max-width: 360px;
  padding: 32px 28px;
  background: var(--color-surface);
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.login-brand { display: flex; align-items: center; gap: 10px; }
.login-brand h1 { font-size: 24px; }
.login-subtitle { margin: 0 0 8px; color: var(--color-muted); }
.provider-button { display: flex; justify-content: center; min-height: 44px; }

.login-error {
  margin: 4px 0 0;
  color: var(--color-danger);
  text-align: center;
}

.microsoft-signin-button {
  width: 280px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border: 1px solid #8c8c8c;
  border-radius: 4px;
  background: #fff;
  color: #5e5e5e;
  font: 500 14px "Segoe UI", system-ui, sans-serif;
  cursor: pointer;
}
.microsoft-signin-button:hover { background: #f5f5f5; }
.microsoft-signin-button:disabled { opacity: 0.6; cursor: wait; }

.brand-logo {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--color-primary);
  color: #fff;
  font-weight: 700;
  font-size: 16px;
}

/* ---------- Layout ---------- */

.app { display: flex; min-height: 100vh; }

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  z-index: 30;
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  transition: width var(--transition), transform var(--transition);
}

.sidebar-header {
  height: var(--topbar-height);
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  overflow: hidden;
  white-space: nowrap;
}
.brand-name { color: #fff; font-size: 16px; font-weight: 600; }

.main {
  flex: 1;
  min-width: 0;
  margin-left: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  transition: margin-left var(--transition);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.breadcrumb { flex: 1; min-width: 0; display: flex; gap: 6px; color: var(--color-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.breadcrumb strong { color: var(--color-text); font-weight: 600; }
.breadcrumb-sep { color: #c0c6d0; }

.topbar-user { display: flex; align-items: center; gap: 12px; }
.user-info { display: flex; align-items: center; gap: 8px; }
.user-name { font-weight: 500; max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.content { flex: 1; padding: 24px; }

/* ---------- Menu ---------- */

.menu { flex: 1; padding: 12px 8px; overflow-y: auto; overflow-x: hidden; }

.menu-group + .menu-group { margin-top: 4px; }

.menu-group-header {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
}
.menu-group-header:hover { background: var(--sidebar-hover); color: var(--sidebar-text-active); }
.menu-group.active > .menu-group-header { color: var(--sidebar-text-active); }

.menu-icon { flex: none; display: inline-flex; }
.menu-label { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.menu-chevron { flex: none; transition: transform var(--transition); }
.menu-group.open .menu-chevron { transform: rotate(90deg); }

.submenu { display: none; padding: 4px 0 4px 32px; }
.menu-group.open .submenu { display: block; }
.submenu-title { display: none; }

.menu-item {
  display: block;
  padding: 8px 12px;
  margin: 2px 0;
  border-radius: var(--radius);
  color: var(--sidebar-text);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.menu-item:hover { background: var(--sidebar-hover); color: var(--sidebar-text-active); }
.menu-item.active { background: var(--sidebar-active); color: var(--sidebar-text-active); }

/* Sidebar thu gọn (desktop): chỉ còn icon cấp 1, hover hiện flyout menu cấp 2 */
.sidebar-collapsed .sidebar { width: var(--sidebar-collapsed-width); }
.sidebar-collapsed .main { margin-left: var(--sidebar-collapsed-width); }
.sidebar-collapsed .brand-name,
.sidebar-collapsed .menu-label,
.sidebar-collapsed .menu-chevron { display: none; }
.sidebar-collapsed .sidebar-header { padding: 0 16px; }
.sidebar-collapsed .menu { overflow: visible; }
.sidebar-collapsed .menu-group { position: relative; }
.sidebar-collapsed .menu-group-header { justify-content: center; padding: 0; }
.sidebar-collapsed .menu-group.active > .menu-group-header { background: var(--sidebar-hover); }
.sidebar-collapsed .menu-group .submenu,
.sidebar-collapsed .menu-group.open .submenu { display: none; }
.sidebar-collapsed .menu-group:hover .submenu {
  display: block;
  position: absolute;
  top: 0;
  left: 100%;
  min-width: 200px;
  margin-left: 8px;
  padding: 6px;
  background: var(--sidebar-bg);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}
/* Cầu nối vô hình giữa icon và flyout để rê chuột qua khoảng trống không bị mất hover */
.sidebar-collapsed .menu-group:hover .submenu::before {
  content: "";
  position: absolute;
  inset: 0 100% 0 -8px;
}
.sidebar-collapsed .submenu-title {
  display: block;
  padding: 6px 12px;
  color: #fff;
  font-weight: 600;
}

.sidebar-backdrop { display: none; }

/* Mobile: sidebar dạng off-canvas */
@media (max-width: 768px) {
  .sidebar,
  .sidebar-collapsed .sidebar { width: var(--sidebar-width); transform: translateX(-100%); }
  .main,
  .sidebar-collapsed .main { margin-left: 0; }

  .sidebar-collapsed .brand-name,
  .sidebar-collapsed .menu-label,
  .sidebar-collapsed .menu-chevron { display: initial; }
  .sidebar-collapsed .menu-group-header { justify-content: flex-start; padding: 0 12px; }
  .sidebar-collapsed .menu-group.open .submenu { display: block; position: static; box-shadow: none; padding: 4px 0 4px 32px; margin: 0; }
  .sidebar-collapsed .submenu-title { display: none; }

  .sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-open .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 25;
    background: rgba(0, 0, 0, 0.4);
  }

  .content { padding: 16px; }
  .user-name { display: none; }
}

/* ---------- Components ---------- */

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.page-header h2 { font-size: 20px; }

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 16px;
}

.empty-state { padding: 48px 16px; text-align: center; color: var(--color-muted); }

.toolbar { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.toolbar .input { max-width: 320px; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
  color: var(--color-text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.button:hover { background: #f3f4f6; }
.button-primary { background: var(--color-primary); border-color: var(--color-primary); color: #fff; }
.button-primary:hover { background: var(--color-primary-hover); }
.button-danger { color: var(--color-danger); }
.button-danger:hover { background: #fef2f2; }
.button-ghost { border-color: transparent; background: transparent; }
.button-small { height: 28px; padding: 0 10px; font-size: 13px; }

.icon-button {
  width: 36px;
  height: 36px;
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--color-text);
  cursor: pointer;
}
.icon-button:hover { background: #f3f4f6; }

.input {
  width: 100%;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: #fff;
  color: inherit;
  font: inherit;
}
.input:focus { outline: 2px solid var(--color-primary-soft); border-color: var(--color-primary); }
.input:disabled { background: #f3f4f6; color: var(--color-muted); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-weight: 500; }

.table-wrapper { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th,
.table td { padding: 10px 12px; border-bottom: 1px solid var(--color-border); text-align: left; white-space: nowrap; }
.table th { color: var(--color-muted); font-weight: 600; font-size: 13px; background: #f9fafb; }
.table tbody tr:hover { background: #f9fafb; }
.col-actions { text-align: right !important; width: 1%; }
.col-actions .button + .button { margin-left: 4px; }
.empty-row { text-align: center !important; color: var(--color-muted); padding: 32px !important; }

.role-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: #f3f4f6;
  color: #374151;
}
.role-badge[data-role="super_admin"] { background: #fee2e2; color: #b91c1c; }
.role-badge[data-role="manager"] { background: #e0e7ff; color: #3730a3; }
.role-badge[data-role="user"] { background: #dcfce7; color: #166534; }

.text-muted { color: var(--color-muted); }
.text-error { color: var(--color-danger); margin: 0; }

.dialog {
  width: min(440px, calc(100vw - 32px));
  padding: 0;
  border: 0;
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.dialog-form { display: flex; flex-direction: column; gap: 14px; padding: 20px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.toast-container {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  padding: 10px 14px;
  border-radius: var(--radius);
  color: #fff;
  background: var(--color-success);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}
.toast-error { background: var(--color-danger); }

.input-auto { width: auto; min-width: 160px; }
.required { color: var(--color-danger); font-style: normal; }
.field-hint { color: var(--color-muted); font-size: 12px; }
.table-summary { margin: 12px 0 0; font-size: 13px; }
.dialog { max-height: calc(100vh - 32px); overflow-y: auto; }

.status-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.status-badge[data-status="active"] { background: #dcfce7; color: #166534; }
.status-badge[data-status="inactive"] { background: #f3f4f6; color: #6b7280; }

/* ---------- Drawer (panel trượt ra từ bên phải) ---------- */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.3);
}

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 41;
  width: min(420px, 100vw);
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  box-shadow: -8px 0 32px rgba(0, 0, 0, 0.15);
  transform: translateX(100%);
  visibility: hidden;
  transition: transform var(--transition), visibility var(--transition);
}
.drawer.open { transform: translateX(0); visibility: visible; }

.drawer-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--color-border);
}
.drawer-header h3 { font-size: 17px; }
.drawer-header p { margin: 4px 0 0; font-size: 13px; }
.drawer-body { flex: 1; overflow-y: auto; padding: 16px 20px; }
.drawer-section-title { margin: 0 0 12px; font-size: 14px; }
.drawer-list { margin: 0; padding-left: 18px; }
.drawer-list li + li { margin-top: 6px; }

.status-badge[data-status="suspended"] { background: #fef3c7; color: #92400e; }
.textarea { height: auto; min-height: 72px; padding: 8px 10px; resize: vertical; }
.input-uppercase { text-transform: uppercase; }
.table td.cell-wrap { white-space: normal; min-width: 200px; max-width: 360px; }

.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar-end { margin-left: auto; }
.drawer-wide { width: min(1000px, 100vw); }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; }
.cell-mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 13px; }

.device-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.device-item { padding: 12px; border: 1px solid var(--color-border); border-radius: var(--radius); }
.device-item-title { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.device-item-desc { margin: 6px 0; color: var(--color-text); }
.device-item-meta { display: flex; gap: 16px; flex-wrap: wrap; color: var(--color-muted); }

/* ---------- Thu chi ---------- */

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 16px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-left-width: 4px;
  border-radius: var(--radius);
}
.stat-card > span { color: var(--color-muted); font-size: 13px; }
.stat-card > strong { font-size: 18px; font-variant-numeric: tabular-nums; }
.stat-income { border-left-color: var(--color-success); }
.stat-expense { border-left-color: var(--color-danger); }
.stat-warning { border-left-color: #f59e0b; background: #fffbeb; }

.col-number { text-align: right !important; font-variant-numeric: tabular-nums; }
.money { font-weight: 600; }
.money[data-type="income"] { color: var(--color-success); }
.money[data-type="expense"] { color: var(--color-danger); }

.type-badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.type-badge[data-type="income"] { background: #dcfce7; color: #166534; }
.type-badge[data-type="expense"] { background: #fee2e2; color: #b91c1c; }

.status-badge[data-status="tracking"] { background: #dbeafe; color: #1e40af; }
.status-badge[data-status="completed"] { background: #f3f4f6; color: #6b7280; }

.segmented { display: inline-flex; gap: 8px; }
.segmented label { position: relative; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: inline-block;
  min-width: 96px;
  padding: 7px 16px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  text-align: center;
  font-weight: 500;
}
.segmented input:focus-visible + span { outline: 2px solid var(--color-primary); }
.segmented input[value="income"]:checked + span { background: #dcfce7; border-color: var(--color-success); color: #166534; }
.segmented input[value="expense"]:checked + span { background: #fee2e2; border-color: var(--color-danger); color: #b91c1c; }

/* ---------- Cho mượn ---------- */

.stat-borrow { border-left-color: #6366f1; }
.stat-lend { border-left-color: #0ea5e9; }
.type-badge[data-type="borrow"] { background: #e0e7ff; color: #3730a3; }
.type-badge[data-type="lend"] { background: #e0f2fe; color: #075985; }
.segmented input[value="borrow"]:checked + span { background: #e0e7ff; border-color: #6366f1; color: #3730a3; }
.segmented input[value="lend"]:checked + span { background: #e0f2fe; border-color: #0ea5e9; color: #075985; }

/* ---------- Tồn kho thiết bị ---------- */

.type-badge[data-type="in"] { background: #dcfce7; color: #166534; }
.type-badge[data-type="out"] { background: #fee2e2; color: #b91c1c; }
.segmented input[value="in"]:checked + span { background: #dcfce7; border-color: var(--color-success); color: #166534; }
.segmented input[value="out"]:checked + span { background: #fee2e2; border-color: var(--color-danger); color: #b91c1c; }

.stock-value { font-weight: 700; }
.stock-empty { color: var(--color-danger); }

.button-primary-soft { background: var(--color-primary-soft); border-color: transparent; color: var(--color-primary); }
.button-primary-soft:hover { background: #dbe6ff; }

.dialog-wide { width: min(760px, calc(100vw - 32px)); }
.dialog-subtitle { margin: 4px 0 0; font-size: 13px; }
.dialog-title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; }

/* ---------- Nhật ký thiết bị ---------- */

.dialog-xwide { width: min(1080px, calc(100vw - 32px)); }

/* ---------- Nhật ký công việc ---------- */

.stat-grid-4 { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.stat-card-button { font: inherit; color: inherit; text-align: left; cursor: pointer; transition: box-shadow var(--transition), transform var(--transition); }
.stat-card-button:hover { box-shadow: 0 4px 14px rgba(0, 0, 0, 0.08); transform: translateY(-1px); }
.stat-card-button.active { box-shadow: 0 0 0 2px var(--color-primary); }
.stat-card-button > strong { font-size: 26px; }
.stat-card-button > small { color: var(--color-muted); font-size: 12px; }
.stat-todo { border-left-color: #94a3b8; }
.stat-doing { border-left-color: var(--color-primary); }
.stat-done { border-left-color: var(--color-success); }
.stat-overdue { border-left-color: var(--color-danger); }
.stat-overdue.active { box-shadow: 0 0 0 2px var(--color-danger); }

.stage-badge { display: inline-block; margin-left: 4px; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.stage-badge[data-stage="todo"] { background: #f1f5f9; color: #475569; }
.stage-badge[data-stage="doing"] { background: var(--color-primary-soft); color: var(--color-primary); }
.stage-badge[data-stage="overdue"] { background: #fee2e2; color: #b91c1c; }

.tag { display: inline-block; padding: 2px 8px; border-radius: 6px; background: #f3f4f6; font-size: 12px; }
.table td.cell-content { min-width: 200px; max-width: 340px; }
.table td.cell-stack > div + div { margin-top: 4px; }
.cell-stack .stage-badge { margin-left: 0; }
