:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --green: #16a34a;
  --red: #dc2626;
  --amber: #d97706;
  --bg: #f3f4f6;
  --card: #ffffff;
  --border: #e5e7eb;
  --text: #1f2937;
  --text-muted: #6b7280;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

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

.topbar {
  background: var(--primary);
  color: white;
  padding: 14px 24px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.topbar h1 { margin: 0; font-size: 20px; }

.undo-controls { position: relative; display: flex; gap: 4px; align-items: center; }
.undo-btn {
  background: rgba(255,255,255,.15);
  color: white;
  border: 1px solid rgba(255,255,255,.3);
  border-radius: 6px;
  width: 32px;
  height: 32px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.undo-btn:hover:not(:disabled) { background: rgba(255,255,255,.28); }
.undo-btn:disabled { opacity: .35; cursor: default; }

.history-dropdown {
  position: absolute;
  top: 40px;
  right: 0;
  width: 320px;
  max-height: 360px;
  overflow-y: auto;
  background: white;
  color: var(--text);
  border-radius: 8px;
  box-shadow: 0 10px 24px rgba(0,0,0,.18);
  z-index: 50;
}
.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.history-item:last-child { border-bottom: none; }
.history-item:hover { background: var(--bg); }
.history-item.redoable { opacity: .5; }
.history-state { font-size: 10px; color: var(--primary); }
.history-item.redoable .history-state { color: var(--text-muted); }
.history-label { flex: 1; }
.history-time { font-size: 11px; color: var(--text-muted); white-space: nowrap; }

.layout {
  display: flex;
  flex: 1;
  min-height: 0;
}

.sidebar {
  width: 230px;
  flex-shrink: 0;
  background: var(--card);
  border-right: 1px solid var(--border);
  padding: 10px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-btn {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  padding: 9px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  color: var(--text-muted);
}
.nav-btn:hover { background: var(--bg); }
.nav-btn.active { background: var(--primary); color: white; }
.nav-btn.sub { padding-left: 28px; font-size: 13.5px; }
.nav-btn.sub2 { padding-left: 44px; font-size: 13px; }

.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-group.nested { gap: 0; }
.nav-group-header.sub {
  padding-left: 28px;
  font-size: 13.5px;
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
  background: #33FFFF;
}
.nav-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  border: none;
  background: transparent;
  padding: 9px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .3px;
  color: var(--text-muted);
}
.nav-group-header:hover { background: var(--bg); }
.nav-group-header.sub:hover { background: #29e0e0; }
.nav-group-header .chevron { transition: transform .15s; font-size: 11px; }
.nav-group.collapsed .nav-group-header .chevron { transform: rotate(-90deg); }
.nav-group.collapsed .nav-group-items { display: none; }

/* Cac muc chinh tren sidebar (Tong quan, Quan ly NPL, Quan ly nhan vien, Cham cong, Bao cao) */
.nav-btn.sidebar-main,
.nav-btn.sidebar-main.active,
.nav-group-header.sidebar-main {
  background: #861619;
  color: #fff;
}
.nav-btn.sidebar-main:hover,
.nav-group-header.sidebar-main:hover {
  background: #6f1013;
}

main#content { flex: 1; overflow-y: auto; padding: 20px 24px 60px; }
main#content > * { max-width: 1200px; margin-left: auto; margin-right: auto; }

@media (max-width: 720px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; flex-direction: row; flex-wrap: wrap; border-right: none; border-bottom: 1px solid var(--border); }
  .nav-group { flex-direction: row; flex-wrap: wrap; }
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px 18px;
  margin-bottom: 18px;
}
.card h2 { margin-top: 0; font-size: 16px; }

.grid-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.stat-box {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
}
.stat-box .label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.stat-box .value { font-size: 22px; font-weight: 600; }
.stat-box.green .value { color: var(--green); }
.stat-box.red .value { color: var(--red); }
.stat-box.amber .value { color: var(--amber); }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; }
tr:hover td { background: #fafafa; }

.form-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
  align-items: flex-end;
}
.field { display: flex; flex-direction: column; gap: 4px; min-width: 140px; }
.field label { font-size: 12px; color: var(--text-muted); }
.field input, .field select, .field textarea {
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  min-width: 140px;
}
.field.wide input, .field.wide textarea { min-width: 280px; }

button.primary {
  background: var(--primary);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  height: 34px;
}
button.primary:hover { background: var(--primary-dark); }
button.danger {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--red);
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}
button.small {
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: white;
  cursor: pointer;
}
button.small.tabbtn.active { background: var(--primary); color: white; border-color: var(--primary); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge.in { background: #dcfce7; color: #166534; }
.badge.out { background: #fee2e2; color: #991b1b; }
.badge.nvl { background: #e0e7ff; color: #3730a3; }
.badge.tp { background: #fef3c7; color: #92400e; }

table.bom-matrix th, table.bom-matrix td { text-align: center; }
table.bom-matrix th > div:first-child { text-align: left; }
tr.bom-default-row td { background: #f8fafc; }
tr.bom-default-row td:first-child { text-align: left; }
table.bom-matrix td:first-child { text-align: left; }

.bom-pill {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  padding: 3px 8px;
  border-radius: 10px;
  cursor: pointer;
  line-height: 1.25;
  font-size: 12px;
}
.bom-pill .pill-top { font-size: 10px; opacity: .7; }
.bom-pill .pill-main { font-weight: 700; color: #1f2937; }

.bom-plain {
  display: inline-block;
  padding: 4px 10px;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  border-radius: 6px;
}
.bom-plain:hover { background: var(--bg); }

.bom-plus {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1.5px dashed var(--border);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
}
.bom-plus:hover { border-color: var(--primary); color: var(--primary); }
.bom-plus.disabled { cursor: default; opacity: .5; }
.bom-plus.disabled:hover { border-color: var(--border); color: var(--text-muted); }

.bom-input { padding: 4px 6px; border: 1px solid var(--primary); border-radius: 6px; font-size: 13px; }

.attr-header { margin: 14px 0 6px; font-size: 13px; }
.attr-row {
  display: grid;
  grid-template-columns: 200px 1fr 32px;
  gap: 12px;
  align-items: start;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.attr-row .attr-name {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
}
.attr-del {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 15px;
  color: var(--text-muted);
  height: 36px;
}
.attr-del:hover { color: var(--red); }

.tag-box {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  min-height: 36px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  cursor: text;
}
.tag-box:focus-within { border-color: var(--primary); }
.tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #dbeafe;
  color: #1e3a8a;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 13px;
  white-space: nowrap;
}
.tag-x {
  border: none;
  background: transparent;
  cursor: pointer;
  color: #2563eb;
  font-size: 13px;
  line-height: 1;
  padding: 0;
}
.tag-input-field {
  border: none;
  outline: none;
  font-size: 14px;
  flex: 1;
  min-width: 140px;
  padding: 4px 2px;
}

.search-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 20;
  background: white;
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0,0,0,.1);
  max-height: 320px;
  overflow-y: auto;
  margin-top: 4px;
}
.search-result-item, .multi-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.search-result-item:last-child, .multi-item:last-child { border-bottom: none; }
.search-result-item:hover, .multi-item:hover { background: var(--bg); }
.search-result-item > div:first-child, .multi-item > div:first-child { flex: 1; }
.sri-name { font-size: 14px; font-weight: 600; }
.sri-meta { font-size: 12px; color: var(--text-muted); }
.sri-price { font-size: 14px; font-weight: 600; color: var(--green); white-space: nowrap; }

.multi-panel {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 12px;
  background: #fafafa;
}
.multi-list { max-height: 320px; overflow-y: auto; background: white; border: 1px solid var(--border); border-radius: 8px; margin-top: 8px; }
.multi-item input[type="checkbox"] { width: 16px; height: 16px; flex: none; }

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: #111827;
  color: white;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 14px;
  opacity: 0;
  transform: translateY(10px);
  transition: all .25s;
  pointer-events: none;
  max-width: 360px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--red); }

.empty { color: var(--text-muted); font-size: 13px; padding: 10px 0; }

.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 20px;
}
.login-card { width: 100%; max-width: 360px; }
.login-card h2 { margin-top: 0; }
.login-card .field { margin-bottom: 12px; }

.current-user-name { font-size: 13px; color: #fff; font-weight: bold; margin-right: 4px; white-space: nowrap; }
.section-title { font-size: 13px; text-transform: uppercase; color: var(--text-muted); margin: 18px 0 8px; }
