:root {
  --bg: #FAFAF7;
  --surface: #FFFFFF;
  --ink: #23262B;
  --ink-soft: #6B6E73;
  --border: #E4E1D8;
  --accent: #B9812E;
  --accent-soft: #F3E6CD;
  --danger: #A6402C;
  --danger-soft: #F5E1DB;
  --success: #3F7554;
  --radius: 6px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
}

.app {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  background: #1F2229;
  color: #EDEBE4;
  display: flex;
  flex-direction: column;
  padding: 24px 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 24px;
  border-bottom: 1px solid #34383F;
  margin-bottom: 20px;
}

.brand-mark {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  font-weight: 600;
  background: var(--accent);
  color: #1F2229;
  padding: 6px 8px;
  border-radius: 4px;
  font-size: 13px;
}

.brand-name {
  font-size: 13px;
  line-height: 1.3;
  font-weight: 500;
  color: #C8C5BC;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-item {
  text-align: left;
  background: none;
  border: none;
  color: #C8C5BC;
  padding: 10px 12px;
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
}

.nav-item:hover { background: #2A2E36; color: #fff; }

.nav-item.active {
  background: var(--accent-soft);
  color: #1F2229;
  font-weight: 600;
}

.nav-separator {
  height: 1px;
  background: #3A3E47;
  margin: 8px 4px;
}

.sidebar-stats {
  margin-top: auto;
  padding-top: 20px;
  border-top: 1px solid #34383F;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sidebar-footer {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid #34383F;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
}

.app-version {
  margin-top: 8px;
  font-size: 11px;
  color: #6B6E73;
  text-align: center;
}

#current-username {
  color: #EDEBE4;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#btn-dang-xuat {
  background: none;
  border: 1px solid #3A3E47;
  border-radius: var(--radius);
  color: #C8C5BC;
  padding: 6px 10px;
  font-family: inherit;
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
}

#btn-dang-xuat:hover { background: #2A2E36; color: #fff; }

.stat { display: flex; flex-direction: column; }
.stat-value {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  font-size: 20px;
  font-weight: 600;
  color: #EDEBE4;
}
.stat-warn .stat-value { color: #E0A94E; }
.stat-label { font-size: 11.5px; color: #93968E; }
.stat-warn { cursor: pointer; }

/* Main */
.main {
  padding: 32px 40px;
  max-width: 1040px;
}

.view { display: none; }
.view.active { display: block; }

.view-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 24px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.view-header h1 {
  font-size: 22px;
  margin: 0 0 4px 0;
  font-weight: 600;
}

.subtitle {
  margin: 0;
  color: var(--ink-soft);
  font-size: 13.5px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.filter-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--ink-soft);
  cursor: pointer;
  white-space: nowrap;
}

.filter-toggle input { cursor: pointer; }

.filter-toggle select {
  font-family: inherit;
  font-size: 13px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
  cursor: pointer;
}

#search-input {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 13.5px;
  min-width: 220px;
  background: var(--surface);
}

.btn {
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
}

.btn-primary { background: var(--ink); color: #fff; }
.btn-primary:hover { background: #000; }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #8C3423; }

.btn-thu-boc {
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
  background: var(--success);
  color: #fff;
}
.btn-thu-boc:hover:not(:disabled) { background: #2F5A41; }
.btn-thu-boc:disabled { opacity: 0.6; cursor: default; }

.row-actions button.btn-boc-thuoc {
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
  background: var(--success);
  color: #fff;
}
.row-actions button.btn-boc-thuoc:hover:not(:disabled) { background: #2F5A41; text-decoration: none; }
.row-actions button.btn-boc-thuoc:disabled { opacity: 0.6; cursor: default; }

.row-actions button.btn-sua-don {
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
  background: var(--success);
  color: #fff;
}
.row-actions button.btn-sua-don:hover:not(:disabled) { background: #2F5A41; text-decoration: none; }
.row-actions button.btn-sua-don:disabled { opacity: 0.6; cursor: default; }

.thieu-hang-table {
  max-width: 640px;
}

#thu-boc-nhieu-ket-qua:not(:empty) {
  margin-bottom: 28px;
  padding-bottom: 20px;
  border-bottom: 2px solid var(--border);
}

.btn-ghost { background: transparent; color: var(--ink-soft); }
.btn-ghost:hover { color: var(--ink); }

.btn-outline-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--danger-soft);
}
.btn-outline-danger:hover { background: var(--danger-soft); }

/* Ledger table */
.ledger {
  width: 100%;
  border-collapse: collapse;
}

.ledger thead th {
  text-align: left;
  font-size: 11.5px;
  color: var(--ink-soft);
  font-weight: 600;
  padding: 0 10px 10px 10px;
  border-bottom: 1px solid var(--border);
}

.ledger th.num, .ledger td.num {
  text-align: right;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
}

.ledger tbody tr {
  border-bottom: 1px solid var(--border);
}

.ledger tbody tr:hover { background: #F3F1EA; }

.ledger td {
  padding: 12px 10px;
  font-size: 13.5px;
  vertical-align: middle;
}

.ledger td.name-cell {
  font-weight: 500;
}

.table-tong-ket {
  margin: 0 0 10px 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-soft);
}

.table-tong-ket div + div {
  margin-top: 2px;
}

.tong-ket-nhan { color: var(--ink-soft); }
.tong-ket-tri { color: var(--accent); font-weight: 600; }

.row-low-stock td.name-cell {
  border-left: 3px solid var(--accent);
  padding-left: 7px;
}

.tag-low {
  display: inline-block;
  margin-left: 8px;
  font-size: 11px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 2px 6px;
  border-radius: 3px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.tag-expired {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  color: var(--danger);
  background: var(--danger-soft);
  padding: 2px 6px;
  border-radius: 3px;
}

.tag-expiring {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 2px 6px;
  border-radius: 3px;
}

.row-actions button {
  background: none;
  border: none;
  color: var(--ink-soft);
  font-size: 12.5px;
  cursor: pointer;
  font-weight: 600;
}
.row-actions button:hover { color: var(--ink); text-decoration: underline; }

.empty-state {
  color: var(--ink-soft);
  font-size: 13.5px;
  padding: 24px 10px;
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 16px;
  font-size: 13px;
  color: var(--ink-soft);
}

.pagination button {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  font-family: inherit;
  font-size: 13px;
  color: var(--ink);
  cursor: pointer;
}

.pagination button:hover:not(:disabled) { background: var(--bg); }
.pagination button:disabled { opacity: 0.4; cursor: default; }

/* Transaction badge */
.badge {
  font-size: 11.5px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 3px;
}
.badge-nhap { background: #E1EDE3; color: var(--success); }
.badge-xuat { background: var(--danger-soft); color: var(--danger); }

/* Forms */
.transaction-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.transaction-form label,
.modal label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-soft);
}

.transaction-form input,
.transaction-form select,
.modal input,
.modal select {
  font-family: inherit;
  font-size: 14px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
}

.select-with-add {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.select-with-add select {
  flex: 1;
  min-width: 140px;
}

.select-with-add .btn-them-ncc,
.select-with-add .btn-sua-ncc,
.select-with-add .btn-quick-add {
  white-space: nowrap;
  font-size: 12.5px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.form-feedback {
  margin-top: 14px;
  font-size: 13.5px;
  max-width: 480px;
}
.form-feedback.success { color: var(--success); }
.form-feedback.error { color: var(--danger); }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 20, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  background: var(--surface);
  border-radius: 8px;
  padding: 28px;
  width: 440px;
  max-width: 92vw;
  max-height: 88vh;
  overflow-y: auto;
}

.modal h2 { margin: 0 0 18px 0; font-size: 18px; }

.modal form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

.don-toolbar {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  max-width: 720px;
  margin-bottom: 16px;
}

.don-tim-cu {
  flex: 1;
}

.don-toolbar input {
  width: 100%;
  font-family: inherit;
  font-size: 14px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--ink);
  background: var(--surface);
}

.don-toolbar .btn {
  white-space: nowrap;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 14px;
}

.autocomplete-wrap {
  position: relative;
}

.suggestions-box {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: 240px;
  overflow-y: auto;
  z-index: 20;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}

.suggestion-item {
  padding: 8px 10px;
  cursor: pointer;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}

.suggestion-item:last-child { border-bottom: none; }
.suggestion-item:hover { background: var(--bg); }

.suggestion-item .sugg-meta {
  color: var(--ink-soft);
  font-size: 12px;
  margin-top: 2px;
}

/* Boc don thuoc */
.don-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 720px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.don-header-fields {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
}

.don-header-fields label,
.don-ghichu-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-soft);
}

.don-header-fields input,
.don-ghichu-field textarea {
  font-family: inherit;
  font-size: 14px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.don-ghichu-field textarea {
  resize: vertical;
  min-height: 90px;
}

.don-items-table select,
.don-items-table input {
  font-family: inherit;
  font-size: 13.5px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
}

.don-items-table select { min-width: 220px; }
.don-items-table input[type="number"] { width: 100px; text-align: right; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace; }

.don-items-table td.thanh-tien {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  text-align: right;
  white-space: nowrap;
}

.btn-xoa-dong {
  background: none;
  border: none;
  color: var(--ink-soft);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
}
.btn-xoa-dong:hover { color: var(--danger); }

.don-total-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
}

.don-total-value {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  font-size: 18px;
}

.btn-boc { align-self: flex-start; }

/* Don thuoc history detail */
.btn-chi-tiet {
  background: none;
  border: none;
  color: var(--ink-soft);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}
.btn-chi-tiet:hover { color: var(--ink); text-decoration: underline; }

.don-detail-row td {
  background: #F3F1EA;
  padding: 14px 10px 14px 20px;
}

.don-detail-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
}

.don-detail-list .detail-line {
  display: flex;
  justify-content: space-between;
  max-width: 480px;
}

.don-detail-list .detail-line span:last-child {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
}

.don-detail-table {
  align-self: flex-start;
  width: auto;
  max-width: 480px;
  font-size: 13px;
}

.don-detail-table th, .don-detail-table td {
  padding: 6px 10px;
}

.don-detail-table td.num, .don-detail-table th.num {
  padding-left: 24px;
}

.don-detail-table td:first-child, .don-detail-table th:first-child {
  padding-left: 0;
  width: 40px;
}

.don-detail-table tfoot td {
  font-weight: 600;
  border-top: 1px solid var(--border);
}

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; align-items: center; overflow-x: auto; }
  .sidebar-stats { display: none; }
  .brand { border: none; margin: 0; padding: 0 12px 0 0; }
  .main { padding: 20px; }
}
