/* ============================================================
   Màu sắc: mọi màu đều là biến, giao diện tối chỉ đổi biến.
   Chế độ: tự động theo hệ điều hành, hoặc ép sáng / tối bằng
   <html data-theme="light|dark"> (nút ◐ trên thanh tiêu đề).
   ============================================================ */
:root {
  color-scheme: light;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;

  --bg: #ffffff;              /* nền trang, ô bảng */
  --surface: #ffffff;         /* ô nhập, thẻ, danh sách nổi */
  --surface-2: #f8f8f8;       /* thanh công cụ */
  --header-bg: #f3f3f3;       /* tiêu đề cột, số dòng */
  --text: #222222;
  --muted: #666666;
  --border: #d4d4d4;
  --border-strong: #bbbbbb;
  --grid-line: #e6e6e6;
  --input-border: #c8c8c8;

  --accent: #217346;          /* xanh Excel */
  --accent-strong: #185c37;
  --accent-soft: #e6f2ea;     /* hover, mục đang chọn */
  --on-accent: #ffffff;
  --bar-bg: #217346;          /* thanh tiêu đề + thanh trạng thái */

  --range-bg: #d7eadd;        /* vùng ô đang chọn */
  --range-header: #d3e3d8;
  --calc-bg: #f2f7f3;         /* dòng tổng */
  --color-cell-bg: #fafafa;   /* cột màu */
  --total-bg: #f6faf7;        /* cột Tổng */
  --pill-bg: #eef1f4;         /* ô chọn mẫu */
  --zero: #c9c9c9;
  --neg: #cc0000;
  --error: #cc0000;
  --overlay: rgba(255, 255, 255, .7);
  --shadow: rgba(0, 0, 0, .12);
  --toast-bg: #1f2d24;
  --login-bg: linear-gradient(160deg, #e9f3ec 0%, #f7f7f7 60%);
}

/* Bảng màu tối — dùng chung cho 2 trường hợp bên dưới */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #1b1d1f;
    --surface: #25282b;
    --surface-2: #212427;
    --header-bg: #2a2d31;
    --text: #e4e6e8;
    --muted: #9aa0a6;
    --border: #3a3e43;
    --border-strong: #4a4f55;
    --grid-line: #303438;
    --input-border: #4a4f55;
    --accent: #4caf7a;
    --accent-strong: #7fd1a3;
    --accent-soft: #24382c;
    --on-accent: #ffffff;
    --bar-bg: #1d5537;
    --range-bg: #2a4a36;
    --range-header: #2f4d3a;
    --calc-bg: #1f2a23;
    --color-cell-bg: #232629;
    --total-bg: #1f2b24;
    --pill-bg: #30343a;
    --zero: #50555b;
    --neg: #ff7b7b;
    --error: #ff8a8a;
    --overlay: rgba(20, 22, 24, .7);
    --shadow: rgba(0, 0, 0, .45);
    --toast-bg: #2f3b33;
    --login-bg: linear-gradient(160deg, #16261d 0%, #1b1d1f 60%);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1b1d1f;
  --surface: #25282b;
  --surface-2: #212427;
  --header-bg: #2a2d31;
  --text: #e4e6e8;
  --muted: #9aa0a6;
  --border: #3a3e43;
  --border-strong: #4a4f55;
  --grid-line: #303438;
  --input-border: #4a4f55;
  --accent: #4caf7a;
  --accent-strong: #7fd1a3;
  --accent-soft: #24382c;
  --on-accent: #ffffff;
  --bar-bg: #1d5537;
  --range-bg: #2a4a36;
  --range-header: #2f4d3a;
  --calc-bg: #1f2a23;
  --color-cell-bg: #232629;
  --total-bg: #1f2b24;
  --pill-bg: #30343a;
  --zero: #50555b;
  --neg: #ff7b7b;
  --error: #ff8a8a;
  --overlay: rgba(20, 22, 24, .7);
  --shadow: rgba(0, 0, 0, .45);
  --toast-bg: #2f3b33;
  --login-bg: linear-gradient(160deg, #16261d 0%, #1b1d1f 60%);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex;
  flex-direction: column;
  font: 13px var(--font);
  color: var(--text);
  background: var(--bg);
}

/* ---- Title bar ---- */
.titlebar {
  display: flex; align-items: center; gap: 10px;
  height: 36px; padding: 0 12px;
  background: var(--bar-bg); color: #fff;
}
.logo { font-size: 18px; }
.doc-title { font-weight: 600; }
.retailer { opacity: .85; font-size: 12px; }
.bar-btn {
  font: inherit; font-size: 12px; height: 24px; padding: 0 10px; cursor: pointer;
  color: #fff; background: transparent; border: 1px solid rgba(255,255,255,.6); border-radius: 3px;
}
.bar-btn:hover { background: rgba(255,255,255,.15); }
.user-label { color: #fff; opacity: .9; font-size: 12px; }
.user-label::before { content: "👤 "; }
.user-label:empty { display: none; }

/* ---- Toolbar ---- */
.toolbar {
  display: flex; align-items: center; flex-wrap: wrap; gap: 6px;
  padding: 6px 10px;
  background: var(--surface-2); border-bottom: 1px solid var(--border);
}
.toolbar button, .toolbar select, .toolbar input {
  font: inherit; height: 28px; padding: 0 10px;
  border: 1px solid var(--input-border); border-radius: 3px; background: var(--surface); color: var(--text);
  cursor: pointer;
}
.toolbar button:hover { background: var(--accent-soft); border-color: var(--accent); }
.toolbar button:disabled { opacity: .6; cursor: default; }
.toolbar input[type=search] { width: 200px; cursor: text; }
.toolbar input::placeholder { color: var(--muted); }
.tb-label { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.tb-label input[type=checkbox] { height: auto; accent-color: var(--accent); }
.sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* ---- Formula bar ---- */
.formula-bar {
  display: flex; align-items: center;
  height: 30px; border-bottom: 1px solid var(--border); background: var(--bg);
}
.name-box {
  width: 90px; height: 100%; border: 0; border-right: 1px solid var(--border);
  text-align: center; font: inherit; background: var(--bg); color: var(--text);
}
.fx { padding: 0 10px; color: var(--muted); font-style: italic; border-right: 1px solid var(--border); line-height: 30px; }
.formula-box { flex: 1; height: 100%; border: 0; padding: 0 8px; font: inherit; background: var(--bg); color: var(--text); }

/* ---- Grid ---- */
main { flex: 1; min-height: 0; position: relative; }
#grid { height: 100%; }
.overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: var(--overlay); z-index: 20;
}
.overlay-box {
  max-width: 520px; padding: 16px 20px; border-radius: 6px;
  background: var(--surface); border: 1px solid var(--border); box-shadow: 0 4px 16px var(--shadow);
  white-space: pre-line; text-align: center;
}
.overlay-box.error { border-color: var(--error); color: var(--error); }

/* Tabulator → giao diện kiểu Excel (ghi đè màu mặc định của thư viện) */
.tabulator { border: 0; font-size: 13px; background: var(--bg); }
.tabulator .tabulator-tableholder .tabulator-table { background: var(--bg); color: var(--text); }
.tabulator .tabulator-tableholder .tabulator-placeholder .tabulator-placeholder-contents { color: var(--muted); }
.tabulator .tabulator-header {
  background: var(--header-bg); color: var(--text); border-bottom: 1px solid var(--border-strong); font-weight: 500;
}
.tabulator .tabulator-header .tabulator-col { background: var(--header-bg); border-right: 1px solid var(--border); }
.tabulator .tabulator-header .tabulator-col .tabulator-col-content { padding: 3px 6px; }
.tabulator .tabulator-header .tabulator-col .tabulator-header-filter input {
  height: 22px; border: 1px solid var(--input-border); border-radius: 2px; padding: 0 4px; font-size: 12px;
  background: var(--surface); color: var(--text);
}
.tabulator .tabulator-header .tabulator-col .tabulator-header-filter input::placeholder { color: var(--muted); }
@media (hover: hover) and (pointer: fine) {
  .tabulator .tabulator-header .tabulator-col.tabulator-sortable.tabulator-col-sorter-element:hover { background: var(--accent-soft); }
}
.tabulator .tabulator-header .tabulator-frozen.tabulator-frozen-left { border-right: 2px solid var(--border-strong); }
.tabulator-row { min-height: 22px; background: var(--bg); border-bottom: 1px solid var(--grid-line); }
.tabulator-row.tabulator-row-even { background: var(--bg); }
.tabulator-row .tabulator-cell { border-right: 1px solid var(--grid-line); padding: 3px 6px; }
.tabulator-row .tabulator-cell.tabulator-frozen.tabulator-frozen-left { border-right: 2px solid var(--border-strong); }
.tabulator-row .tabulator-cell.tabulator-row-header,
.tabulator .tabulator-header .tabulator-col.tabulator-row-header {
  background: var(--header-bg); color: var(--muted); text-align: center; border-right: 1px solid var(--border-strong);
}
.tabulator .tabulator-cell.tabulator-range-selected:not(.tabulator-range-only-cell-selected):not(.tabulator-row-header) {
  background: var(--range-bg);
}
.tabulator .tabulator-cell.tabulator-range-highlight.tabulator-row-header,
.tabulator .tabulator-header .tabulator-col.tabulator-range-highlight { background: var(--range-header); color: var(--accent-strong); }
.tabulator .tabulator-tableholder .tabulator-range-overlay .tabulator-range,
.tabulator .tabulator-tableholder .tabulator-range-overlay .tabulator-range-cell-active { border-color: var(--accent); }
.tabulator .tabulator-tableholder .tabulator-range-overlay .tabulator-range.tabulator-range-active::after { background-color: var(--accent); }
.tabulator .tabulator-footer { background: var(--header-bg); color: var(--text); border-top: 1px solid var(--border-strong); }
.tabulator .tabulator-footer .tabulator-calcs-holder,
.tabulator .tabulator-footer .tabulator-calcs-holder .tabulator-row {
  background: var(--calc-bg) !important; border-top: 0; border-bottom: 0; font-weight: 600;
}
.tabulator .tabulator-col-resize-guide { background: var(--accent); }
.num { font-variant-numeric: tabular-nums; }
.neg { color: var(--neg); }
.zero { color: var(--zero); }
.out { color: var(--neg); font-weight: 600; }  /* hết hàng / âm */

/* ---- Bảng màu × size ---- */
.tabulator .tabulator-header .tabulator-col.col-size .tabulator-col-title { text-align: center; font-weight: 700; }
.tabulator-row .tabulator-cell.color-cell { font-weight: 700; text-align: center; background: var(--color-cell-bg); }
.tabulator-row .tabulator-cell.total-cell { font-weight: 600; background: var(--total-bg); }
.group-picker { position: relative; }
.group-select {
  width: 100%; height: 26px; padding: 0 22px 0 10px; border-radius: 13px;
  border: 1px solid var(--input-border); background: var(--pill-bg); color: var(--text);
  font: inherit; font-weight: 600;
}
.group-select:focus { outline: none; border-color: var(--accent); background: var(--surface); font-weight: 400; }
.group-caret { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); color: var(--muted); cursor: pointer; font-size: 11px; }
.group-list {
  position: fixed; z-index: 1000; width: 420px; max-width: calc(100vw - 16px); max-height: 60vh; overflow-y: auto;
  background: var(--surface); color: var(--text); border: 1px solid var(--input-border); border-radius: 6px;
  box-shadow: 0 6px 20px var(--shadow); padding: 4px 0; font-size: 13px;
}
.group-item { padding: 5px 12px; cursor: pointer; display: flex; flex-direction: column; gap: 1px; }
.group-item b { font-weight: 600; }
.group-item small { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.group-item.current b { color: var(--accent-strong); }
.group-item.active { background: var(--accent-soft); }
.group-empty { padding: 8px 12px; color: var(--muted); }

/* ---- Footer: status ---- */
footer { border-top: 1px solid var(--border); }
.statusbar {
  display: flex; gap: 16px; align-items: center;
  height: 24px; padding: 0 12px; font-size: 12px;
  background: var(--bar-bg); color: #fff;
}
#statusSel b { font-weight: 600; margin-left: 4px; }
#statusSel span { margin-left: 16px; }

.toast {
  position: fixed; right: 16px; bottom: 36px; z-index: 2000; max-width: min(420px, calc(100vw - 32px));
  padding: 10px 14px; border-radius: 6px; background: var(--toast-bg); color: #fff; font-size: 13px;
  box-shadow: 0 6px 20px var(--shadow);
}

/* ---- Trang đăng nhập ---- */
body.login-page {
  align-items: center; justify-content: center; padding: 16px;
  background: var(--login-bg);
}
.login-card {
  position: relative;
  width: min(360px, 100%); padding: 28px 28px 24px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 10px 30px var(--shadow);
}
.login-brand { display: flex; align-items: center; gap: 8px; color: var(--accent); font-weight: 700; }
.login-card h1 { margin: 12px 0 18px; font-size: 20px; }
.login-card label { display: block; margin-bottom: 12px; font-weight: 500; }
.login-card input {
  display: block; width: 100%; margin-top: 5px; height: 36px; padding: 0 10px;
  font: inherit; border: 1px solid var(--input-border); border-radius: 4px;
  background: var(--bg); color: var(--text);
}
.login-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.login-card .error { color: var(--error); min-height: 1.2em; margin: 4px 0 10px; }
.login-card button[type=submit] {
  width: 100%; height: 38px; font: inherit; font-weight: 600; cursor: pointer;
  border: 0; border-radius: 4px; background: var(--accent); color: var(--on-accent);
}
.login-card button[type=submit]:disabled { opacity: .7; cursor: default; }
.login-theme {
  position: absolute; top: 12px; right: 12px; width: 30px; height: 30px; padding: 0;
  font-size: 15px; cursor: pointer; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
}
.login-theme:hover { color: var(--text); border-color: var(--accent); }
