/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f3f4f6;
  --bg-elev: #ffffff;
  --bg-elev-2: #f8f9fb;
  --bg-head: #eef1f5;
  --border: #d6dae1;
  --border-light: #c3c9d3;
  --grid: #e3e6eb;
  --text: #1f2733;
  --text-dim: #5b6472;
  --text-faint: #9aa2b1;
  --primary: #217346;         /* Excel green */
  --primary-hover: #1a5c38;
  --accent: #2563eb;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --success: #16a34a;
  --radius: 6px;
  --radius-sm: 5px;
  --shadow: 0 8px 28px rgba(15, 23, 42, .16);
  --sidebar-w: 260px;
}

html, body { height: 100%; }

body {
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 13.5px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

input, textarea, button, select { font-family: inherit; font-size: inherit; color: inherit; }

::-webkit-scrollbar { width: 12px; height: 12px; }
::-webkit-scrollbar-track { background: #eceef1; }
::-webkit-scrollbar-thumb { background: #c0c6cf; border-radius: 8px; border: 3px solid #eceef1; }
::-webkit-scrollbar-thumb:hover { background: #a7aeb9; }

/* ===== Layout ===== */
.app { display: flex; height: 100vh; overflow: hidden; }

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 40;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 10px;
  border-bottom: 1px solid var(--border);
}

.logo { display: flex; align-items: center; gap: 6px; }
.logo-mark {
  width: 18px; height: 18px;
  display: grid; place-items: center;
  background: var(--primary);
  border-radius: 4px;
  font-weight: 800; font-size: 11px; color: #fff;
}
.logo-text { font-weight: 700; font-size: 12.5px; letter-spacing: -.2px; }

/* Close/collapse button sits in the sidebar's top-right corner (all sizes). */
.sidebar-close {
  display: grid;
  position: absolute; top: 5px; right: 6px; z-index: 5;
}
/* Keep the nav row clear of the close button. */
.sidebar .nav-switch { padding-right: 36px; }

.sidebar-search { padding: 6px 10px 4px; }
.sidebar-search input {
  width: 100%;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.sidebar-search input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(33,115,70,.12); }

.tag-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.tag-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 1px solid transparent;
  transition: background .12s, border-color .12s;
}
.tag-item:hover { background: var(--bg-elev-2); }
.tag-item.active { background: #e8f3ec; border-color: #bfe0cc; }

.tag-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.tag-name { flex: 1; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-view { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow: hidden; }
.history-view[hidden] { display: none; }
.history-toolbar { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin: 0 20px 10px; flex: none; }
.mail-check-box { display: flex; align-items: stretch; gap: 8px; flex: 1; max-width: 720px; }
.mail-check-box textarea { flex: 1; min-width: 260px; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 8px; resize: vertical; }
.history-message { white-space: normal; line-height: 1.45; min-width: 280px; }
.mail-result-wrap { max-height: 260px; overflow: auto; border: 1px solid var(--grid); border-radius: 5px; background: #fff; }
.mail-result-table { width: 100%; border-collapse: collapse; font-size: 12px; table-layout: auto; }
.mail-result-table th { position: sticky; top: 0; background: var(--bg-head); z-index: 1; text-align: left; padding: 6px 8px; border: 1px solid var(--grid); }
.mail-result-table td { padding: 5px 8px; border: 1px solid var(--grid); vertical-align: top; }
.mail-result-table th:nth-child(1),
.mail-result-table td:nth-child(1) { width: 92px; min-width: 92px; white-space: nowrap; }
.mail-result-table th:nth-child(2),
.mail-result-table td:nth-child(2) { min-width: 220px; }
.mail-result-table .mail-used td { background: #fff1f2; }
.mail-result-table .mail-unused td { background: #f0fdf4; }
.mail-result-table .mail-used td:nth-child(1) { color: var(--danger); font-weight: 900; }
.mail-result-table .mail-unused td:nth-child(1) { color: var(--success); font-weight: 900; }
.mail-result-table .mail-used .mail-result-address { color: var(--danger); font-weight: 800; text-decoration: line-through; text-decoration-thickness: 2px; }
.mail-result-table .mail-unused .mail-result-address { color: var(--success); font-weight: 800; }
.history-ok { color: var(--success); font-weight: 700; }
.history-bad { color: var(--danger); font-weight: 700; }
.history-warn { color: #b45309; font-weight: 700; }
.history-row.history-issue { background: #fff7ed; }
#historyTable th, #historyTable td { border: 1px solid var(--grid); }
.cell-paid { color: var(--success); font-weight: 700; }
.cell-check { cursor: text; }
.check-value { color: #7c3aed; font-weight: 800; }
.check-date { color: var(--text-dim); font-weight: 500; }
.check-edit-input { text-align: right; }
.tag-count {
  font-size: 12px; color: var(--text-dim);
  background: var(--bg-head); padding: 1px 8px; border-radius: 20px;
  min-width: 24px; text-align: center;
}
.tag-actions { display: flex; gap: 2px; opacity: 0; }
.tag-item:hover .tag-actions { opacity: 1; }
.mini-btn {
  background: none; border: none; cursor: pointer;
  color: var(--text-dim); padding: 2px 5px; border-radius: 5px; font-size: 13px;
}
.mini-btn:hover { background: var(--border); color: var(--text); }

.sidebar-footer { padding: 12px; border-top: 1px solid var(--border); }

.tag-list-empty { padding: 24px 14px; text-align: center; color: var(--text-faint); font-size: 13px; }

/* ===== Main ===== */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  gap: 12px;
}
.topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
/* Logo now lives in the topbar (moved out of the sidebar to save height). */
.topbar-logo {
  flex: none;
  padding-right: 12px;
  border-right: 1px solid var(--border);
}
.topbar-title { min-width: 0; }
.topbar-title h1 {
  font-size: 17px; font-weight: 700; letter-spacing: -.3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: flex; align-items: center; gap: 8px;
}
.title-dot { width: 12px; height: 12px; border-radius: 3px; display: inline-block; flex: none; }
.topbar-sub { font-size: 12.5px; color: var(--text-dim); }
.topbar-actions { display: flex; gap: 8px; flex-shrink: 0; align-items: center; }
/* The menu button toggles the sidebar on every screen size:
   wide screens collapse it away, narrow screens slide it in. */
.menu-btn { display: grid; }

/* Desktop: collapse the sidebar out of the layout for more table room. */
body.sidebar-collapsed .sidebar { display: none; }

/* ===== Stats ===== */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
  padding: 14px 20px 2px;
}
.stats:empty { display: none; }
.stat-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.stat-label { font-size: 12px; color: var(--text-dim); margin-bottom: 4px; }
.stat-value { font-size: 22px; font-weight: 700; letter-spacing: -.5px; }

/* ===== Toolbar ===== */
.toolbar {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 20px 10px;
}
.toolbar[hidden] { display: none; }
.search-box { flex: 1; }
.search-box input {
  width: 100%;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 13px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.search-box input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(33,115,70,.12); }
.toolbar-buttons { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

/* ===== Sync badge ===== */
.sync-badge {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 5px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 500;
  background: var(--bg-elev-2); border: 1px solid var(--border);
  color: var(--text-dim); white-space: nowrap;
}
.sync-badge .sync-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: currentColor; flex: none;
}
.sync-badge.sync-saving { color: #b45309; border-color: rgba(180, 83, 9, .35); background: #fef6e7; }
.sync-badge.sync-saving .sync-dot { animation: syncPulse 1s ease-in-out infinite; }
.sync-badge.sync-saved { color: #15803d; border-color: rgba(21, 128, 61, .35); background: #eafaf0; }
.sync-badge.sync-offline { color: #b91c1c; border-color: rgba(185, 28, 28, .35); background: #fdeaea; }
@keyframes syncPulse { 0%,100% { opacity: 1; } 50% { opacity: .3; } }

/* ===== Table (Excel-like) ===== */
.table-wrap {
  margin: 0 20px 20px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  overflow: hidden;
  flex: 1;
  background: var(--bg-elev);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.table-wrap[hidden] { display: none; }

.table-scroll {
  flex: 1;
  overflow: auto;
  min-height: 0;
}

.data-table {
  border-collapse: collapse;
  font-size: 13px;
  table-layout: fixed;
  width: max-content;
  min-width: 100%;
}
.data-table td, .data-table th {
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Header */
.data-table thead th {
  position: sticky; top: 0; z-index: 2;
  background: var(--bg-head);
  text-align: left;
  font-weight: 600;
  font-size: 12.5px;
  color: var(--text);
  padding: 9px 12px;
  border: 1px solid var(--grid);
  border-top: none;
  white-space: nowrap;
  user-select: none;
}
.data-table th[data-sort] { cursor: pointer; }
.data-table th[data-sort]:hover { background: #e2e7ee; }
.data-table th.sort-asc::after { content: " ▲"; font-size: 9px; color: var(--primary); }
.data-table th.sort-desc::after { content: " ▼"; font-size: 9px; color: var(--primary); }

.col-idx { text-align: center !important; }
.col-actions { text-align: center !important; }

/* Column resize grip (right edge of header cell) */
.col-resizer {
  position: absolute;
  top: 0; right: -3px;
  width: 7px; height: 100%;
  cursor: col-resize;
  z-index: 3;
  user-select: none;
}
.col-resizer:hover { background: var(--primary); opacity: .4; }

/* Row resize grip (inside # cell, bottom edge) */
.col-idx { position: relative; }
.row-resizer {
  position: absolute;
  left: 0; bottom: -3px;
  height: 7px; width: 100%;
  cursor: row-resize;
  z-index: 1;
}
.row-resizer:hover { background: var(--primary); opacity: .4; }

body.resizing-col, body.resizing-col * { cursor: col-resize !important; user-select: none !important; }
body.resizing-row, body.resizing-row * { cursor: row-resize !important; user-select: none !important; }
body.resizing-group-detail, body.resizing-group-detail * { cursor: ns-resize !important; user-select: none !important; }

/* Body cells — full gridlines like a spreadsheet */
.data-table tbody td {
  padding: 7px 12px;
  border: 1px solid var(--grid);
  vertical-align: middle;
}
.data-table tbody tr:nth-child(even) td { background: #fafbfc; }
.data-table tbody tr:hover td { background: #eef6f1; }

.col-idx { color: var(--text-faint); background: var(--bg-elev-2) !important; font-variant-numeric: tabular-nums; }

.cell-note { max-width: 260px; white-space: pre-wrap; word-break: break-word; color: var(--text); }
.cell-bookname { font-size: 60%; }
.cell-bookname { font-size: 60%; }
.badge {
  font-family: ui-monospace, 'Cascadia Code', monospace;
  font-size: 12px;
  color: var(--accent);
  background: #eef3fe;
  border: 1px solid #d7e3fb;
  border-radius: 4px;
  padding: 1px 6px;
  white-space: nowrap;
}
.badge-missing-api {
  color: #b91c1c !important;
  background: #fee2e2 !important;
  border-color: #ef4444 !important;
  font-weight: 800;
  box-shadow: 0 0 0 2px rgba(239, 68, 68, .16);
}
.data-table a { color: var(--accent); text-decoration: none; }
.data-table a:hover { text-decoration: underline; }
.link-btn {
  border: none;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
  padding: 0;
  font: inherit;
  text-decoration: underline;
}
.group-breakdown-cell { background: #f8fafc !important; padding: 10px !important; }
.group-breakdown-title { font-weight: 800; margin-bottom: 8px; color: var(--text); }
.mini-breakdown-table { width: 100%; border-collapse: collapse; background: #fff; }
.mini-breakdown-table th,
.mini-breakdown-table td { border: 1px solid var(--grid); padding: 5px 8px; }
.mini-breakdown-table th { background: var(--bg-head); font-weight: 800; }
.muted { color: var(--text-faint); }

/* Numeric columns (Paid / Net) */
.data-table th.col-num, .data-table td.col-num {
  text-align: right;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  font-family: ui-monospace, 'Cascadia Code', monospace;
}
/* Net column: bold red numbers. */
.data-table td.cell-net { font-weight: 700; color: #dc2626; }

/* Totals row */
.data-table tbody tr.total-row td {
  border-top: 2px solid var(--border-light);
  background: #eef1f5 !important;
  font-weight: 700;
  position: sticky; bottom: 0;
}
.total-label { text-align: right; color: var(--text-dim); }

/* Extra Book IDs from the API that are missing from the table */
.data-table tbody tr.extra-sep td {
  background: #fbeaea !important;
  color: #b91c1c;
  font-weight: 700;
  font-size: 12px;
  border-top: 2px solid #f0b4b4;
  padding: 8px 12px;
}
.data-table tbody tr.extra-row td {
  background: #fdf2f2 !important;
  color: #b91c1c;
  font-weight: 700;
}
.data-table tbody tr.extra-row:hover td { background: #fbe6e6 !important; }
.data-table tbody tr.extra-total td {
  background: #fbeaea !important;
  border-top: 2px solid #f0b4b4;
}
.data-table tbody tr.extra-total:hover td { background: #fbeaea !important; }
.data-table tbody tr.extra-row .badge {
  color: #b91c1c;
  background: #fbe0e0;
  border-color: #f0b4b4;
}

.col-actions { white-space: nowrap; }

/* ===== Net month / date-range picker ===== */
.net-range {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px 3px;
  flex-shrink: 0;
}
.net-range-compact { max-width: 390px; }
.paid-month-inline {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 1px;
  height: 1px;
}
.paid-head-label { vertical-align: middle; }
.paid-month-btn,
.paid-month-clear {
  margin-left: 4px;
  border: 1px solid #86efac;
  background: #f0fdf4;
  color: var(--success);
  border-radius: 4px;
  width: 18px;
  height: 18px;
  line-height: 15px;
  cursor: pointer;
  font-size: 11px;
  font-weight: 800;
}
.paid-month-clear { color: var(--danger); border-color: #fecaca; background: #fef2f2; }
.paid-month-btn:hover,
.paid-month-clear:hover { filter: brightness(.96); }
.net-range input[type="month"],
.net-range input[type="date"] {
  border: none; background: transparent; outline: none;
  font-size: 12px; color: var(--text); padding: 3px 1px;
  font-variant-numeric: tabular-nums;
}
.net-range input[type="month"] { width: 104px; min-width: 104px; }
.net-range input[type="date"] { width: 132px; min-width: 132px; padding-right: 4px; }
.net-range-sep, .net-range-dash {
  font-size: 11px; color: var(--text-faint); user-select: none;
}
.btn-clear-month {
  border: none; background: transparent; cursor: pointer;
  color: var(--text-dim); font-size: 17px; line-height: 1;
  width: 22px; height: 22px; border-radius: 5px;
}
.btn-clear-month:hover { background: var(--bg-elev-2); color: var(--danger); }

/* ===== Net account dropdown ===== */
.net-account {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px 4px 2px 6px;
  flex-shrink: 0;
}
.net-account select {
  border: none; background: transparent; outline: none; cursor: pointer;
  font-size: 13px; color: var(--text); padding: 4px 2px;
  max-width: 180px;
}
.btn-refresh-acc {
  border: none; background: transparent; cursor: pointer;
  color: var(--text-dim); font-size: 15px; line-height: 1;
  width: 22px; height: 22px; border-radius: 5px;
}
.btn-refresh-acc:hover { background: var(--bg-elev-2); color: var(--primary); }

/* ===== Net group-by dropdown ===== */
.net-group {
  display: inline-flex; align-items: center;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px 4px 2px 6px;
  flex-shrink: 0;
}
.net-group select {
  border: none; background: transparent; outline: none; cursor: pointer;
  font-size: 13px; color: var(--text); padding: 4px 2px;
}

/* ===== Group detail table (Get Net theo user/owner) ===== */
.group-detail {
  position: fixed;
  left: calc(var(--sidebar-w) + 20px);
  right: 20px;
  bottom: 16px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  background: var(--bg-elev);
  overflow: hidden;
  height: 34vh;
  max-height: calc(100vh - 120px);
  min-height: 120px;
  display: flex;
  flex-direction: column;
  z-index: 30;
  box-shadow: 0 12px 34px rgba(15, 23, 42, .22);
}
.group-detail[hidden] { display: none; }
.group-detail-resizer {
  height: 8px;
  flex: 0 0 8px;
  cursor: ns-resize;
  background: linear-gradient(to bottom, #e5e7eb, #f8fafc);
  position: relative;
}
.group-detail-resizer::after {
  content: "";
  position: absolute;
  left: 50%; top: 3px;
  width: 48px; height: 2px;
  transform: translateX(-50%);
  border-radius: 999px;
  background: #94a3b8;
}
.group-detail-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-head);
}
.group-detail-head h3 { font-size: 13px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.group-detail .table-scroll { overflow: auto; min-height: 0; }
.group-detail .data-table thead th { padding: 6px 8px; font-size: 12px; }
.group-detail .data-table tbody td { padding: 5px 8px; }
body.sidebar-collapsed .group-detail { left: 20px; }
@media (max-width: 820px) {
  .group-detail { left: 10px; right: 10px; bottom: 10px; height: 42vh; max-height: calc(100vh - 90px); }
}

/* Two fields side by side in a form */
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; margin-bottom: 15px; }

.table-empty { padding: 40px; text-align: center; color: var(--text-faint); }

/* ===== Empty states ===== */
.empty-state {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 40px; gap: 6px;
}
.empty-state[hidden] { display: none; }
.empty-icon {
  width: 60px; height: 60px; display: grid; place-items: center;
  font-size: 28px; font-weight: 800;
  background: #e8f3ec; border: 1px solid #bfe0cc;
  border-radius: 16px; color: var(--primary); margin-bottom: 10px;
}
.empty-state h2 { font-size: 18px; font-weight: 700; }
.empty-state p { color: var(--text-dim); max-width: 340px; margin-bottom: 12px; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 600;
  white-space: nowrap;
  transition: background .12s, border-color .12s, opacity .12s;
}
.btn:active { transform: translateY(1px); }
.btn-icon { font-size: 15px; line-height: 1; }
.btn-block { width: 100%; justify-content: center; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: var(--bg-elev); border-color: var(--border); color: var(--text); }
.btn-ghost:hover { border-color: var(--border-light); background: var(--bg-elev-2); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }

.icon-btn {
  background: none; border: none; cursor: pointer; color: var(--text-dim);
  width: 32px; height: 32px; border-radius: 7px; font-size: 19px;
  display: grid; place-items: center; transition: background .12s, color .12s;
}
.icon-btn:hover { background: var(--bg-elev-2); color: var(--text); }

/* ===== Modal ===== */
.modal { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15,23,42,.4); backdrop-filter: blur(2px); }
.modal-card {
  position: relative;
  background: var(--bg-elev);
  border: 1px solid var(--border-light);
  border-radius: 12px;
  width: 100%; max-width: 520px;
  box-shadow: var(--shadow);
  animation: pop .16s ease;
}
.modal-sm { max-width: 400px; }
@keyframes pop { from { opacity: 0; transform: scale(.96) translateY(6px); } to { opacity: 1; transform: none; } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { font-size: 16px; font-weight: 700; }
.modal-body { padding: 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

.field { margin-bottom: 15px; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: 13px; font-weight: 500; color: var(--text-dim); margin-bottom: 6px; }
.field input, .field textarea, .field select {
  width: 100%;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
  resize: vertical;
}
.field select { cursor: pointer; }
.field input:focus, .field textarea:focus, .field select:focus {
  border-color: var(--primary); box-shadow: 0 0 0 3px rgba(33,115,70,.12);
}
.field code {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 12px;
}
.paste-hint {
  margin-top: 10px;
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.5;
}
.paste-hint b { color: var(--text); }

.color-picker { display: flex; gap: 8px; flex-wrap: wrap; }
.color-swatch {
  width: 30px; height: 30px; border-radius: 8px; cursor: pointer;
  border: 2px solid transparent; transition: transform .1s; position: relative;
}
.color-swatch:hover { transform: scale(1.1); }
.color-swatch.active { border-color: var(--text); box-shadow: 0 0 0 2px #fff inset; }

/* ===== Toast ===== */
.toast-wrap {
  position: fixed; bottom: 22px; right: 22px; z-index: 200;
  display: flex; flex-direction: column; gap: 10px;
}
.toast {
  background: var(--bg-elev);
  border: 1px solid var(--border-light);
  border-left: 3px solid var(--text-dim);
  border-radius: var(--radius-sm);
  padding: 11px 15px;
  box-shadow: var(--shadow);
  min-width: 220px; max-width: 340px;
  opacity: 0; transform: translateX(20px);
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: none; }
.toast-success { border-left-color: var(--success); }
.toast-error { border-left-color: var(--danger); }

/* ===== Sidebar nav switch ===== */
.nav-switch {
  display: flex; flex-direction: row; gap: 4px;
  padding: 5px 8px;
  border-bottom: 1px solid var(--border);
}
.nav-item {
  flex: 1;
  display: flex; align-items: center; justify-content: center; gap: 5px;
  padding: 6px 6px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  font-weight: 600;
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
  white-space: nowrap;
  transition: background .12s, color .12s;
}
.nav-item[hidden] { display: none; }
.nav-item:hover { background: var(--bg-elev-2); color: var(--text); }
.nav-item.active { background: var(--bg-elev-2); color: var(--text); border-color: var(--border-light); }
.nav-ico {
  width: 18px; height: 18px; flex: none;
  display: grid; place-items: center;
  border-radius: 4px; font-weight: 800; font-size: 11px;
  background: var(--bg); color: var(--primary);
}
.sidebar-section { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.sidebar-section[hidden] { display: none; }
.sidebar-note {
  padding: 14px 16px; font-size: 12.5px; color: var(--text-dim);
  line-height: 1.55; flex: 1;
}

/* ===== Book Stats view ===== */
.bookstats-view { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow: hidden; }
.bookstats-view[hidden] { display: none; }
.bs-filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  padding: 18px 22px 6px;
  align-items: end;
}
.bs-filter { display: flex; flex-direction: column; gap: 5px; }
.bs-filter label { font-size: 12px; font-weight: 500; color: var(--text-dim); }
.bs-filter input, .bs-filter select {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  outline: none;
  transition: border-color .15s;
}
.bs-filter input:focus, .bs-filter select:focus { border-color: var(--primary); }
.bs-filter-actions { display: flex; gap: 8px; align-items: flex-end; }
.bs-filter-actions .btn { flex: 1; justify-content: center; }

.bs-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 6px 22px 12px;
}
.bs-status { font-size: 13px; color: var(--text-dim); }
.bs-status-loading { color: var(--primary); }
.bs-status-error { color: var(--danger); font-weight: 600; }

.bs-pager {
  display: flex; align-items: center; justify-content: center; gap: 16px;
  padding: 14px; border-top: 1px solid var(--border); flex: none;
}
.bs-pager[hidden] { display: none; }
.bs-page-info { font-size: 13px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.btn:disabled { opacity: .4; cursor: not-allowed; }

#bsTableWrap { margin-top: 0; }

/* ===== Sidebar overlay (mobile) ===== */
.sidebar-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.4); z-index: 35;
  display: none;
}

/* ===== Responsive ===== */
@media (max-width: 820px) {
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .sidebar-overlay.show { display: block; }
  .menu-btn { display: grid; }
  .stats { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 520px) {
  .stats { grid-template-columns: 1fr; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar-buttons { justify-content: flex-end; }
  .cell-note { max-width: 140px; }
}

/* ===== Login overlay ===== */
.login-overlay {
  position: fixed; inset: 0; z-index: 500;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #e8eef0, #f3f4f6);
  padding: 20px;
}
.login-overlay[hidden] { display: none; }
.login-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  width: 100%; max-width: 360px;
  padding: 28px 26px;
}
.login-logo {
  display: flex; align-items: center; gap: 8px;
  font-weight: 800; font-size: 18px; margin-bottom: 4px;
}
.login-logo .logo-mark {
  width: 28px; height: 28px; display: grid; place-items: center;
  background: var(--primary); color: #fff; border-radius: 7px; font-size: 16px;
}
.login-card h2 { font-size: 16px; font-weight: 700; margin-bottom: 18px; color: var(--text-dim); }
.login-card .field { margin-bottom: 14px; }
.login-error {
  color: var(--danger); font-size: 13px; margin-bottom: 12px;
  background: #fdeaea; border: 1px solid #f3b4b4; border-radius: var(--radius-sm);
  padding: 8px 10px;
}
.login-error[hidden] { display: none; }

/* ===== User chip / logout ===== */
.user-chip {
  font-size: 12.5px; font-weight: 600; color: var(--text-dim);
  background: var(--bg-elev-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 5px 11px; white-space: nowrap;
}

/* ===== Users view ===== */
.users-view[hidden] { display: none; }
.users-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 16px 20px 12px;
}
.users-note { font-size: 13px; color: var(--text-dim); }
.users-view .table-wrap { margin: 0 20px 20px; }
.sidebar-note {
  padding: 14px 16px; font-size: 12.5px; color: var(--text-dim);
  line-height: 1.5;
}

/* ===== Role gating (employee = read-only) ===== */
body.role-employee #addRowBtn,
body.role-employee #pasteBtn,
body.role-employee #importBtn,
body.role-employee #exportBtn,
body.role-employee #deleteSelectedBtn,
body.role-employee .tag-actions {
  display: none !important;
}
/* Row-select checkboxes are an admin-only feature. */
body.role-employee #selectAllRows,
body.role-employee .row-select { display: none !important; }
.col-actions { white-space: nowrap; }
.row-select { cursor: pointer; vertical-align: middle; margin-right: 2px; }
#selectAllRows { cursor: pointer; vertical-align: middle; }
/* Reset cột/hàng button removed for everyone (not needed). */
#resetLayoutBtn { display: none !important; }
body.role-employee .tag-item { cursor: default; }

/* ===== Virtual "All" view (admin) : merged, read-only across every tag ===== */
/* Editing there can't map back to a real tag, so hide edit/delete/add/paste,
   Get Net (per-tag API filter), selection checkboxes, and the actions column. */
body.viewing-all #addRowBtn,
body.viewing-all #pasteBtn,
body.viewing-all #getNetBtn,
body.viewing-all #exportTagCsvBtn,
body.viewing-all #deleteSelectedBtn,
body.viewing-all .net-group,
body.viewing-all .net-range,
body.viewing-all .net-account,
body.viewing-all #dataTable .col-actions,
body.viewing-all #dataTable th[data-col="actions"] .row-select,
body.viewing-all #selectAllRows,
body.viewing-all .row-select,
body.viewing-all #dataTable [data-edit],
body.viewing-all #dataTable [data-del] {
  display: none !important;
}
/* The "All" list item and the title badge. */
.tag-item-all .tag-name { font-weight: 700; }
.all-badge {
  font-size: 11px; font-weight: 600; color: #475569;
  background: #e2e8f0; border-radius: 10px; padding: 1px 8px;
  vertical-align: middle;
}
/* Per-row origin tag chip shown in the Note column of the All view. */
.tag-origin {
  display: inline-block; color: #fff; font-size: 11px; font-weight: 600;
  border-radius: 4px; padding: 1px 6px; margin-right: 2px; white-space: nowrap;
}
/* In the All view the Bank cell is not click-to-edit. */
body.viewing-all #dataTable .cell-bank { cursor: default; }
/* Collapse the now-empty actions column track so there's no gap on the right. */
body.viewing-all #dataTable col[data-col="actions"] { width: 0 !important; }

/* Employees don't get the Mail or Actions columns. Every row still renders all
   11 cells so the fixed-layout colgroup stays consistent and the total-row
   colspans keep lining up. We collapse the Mail and Actions columns by zeroing
   their <col> tracks and clipping the matching header/body cells. We must NOT
   use display:none on the cells: with table-layout:fixed that shifts the
   remaining cells onto the wrong column tracks and misaligns the total row.
   applyColWidths() writes an inline width on each <col>, so width needs
   !important to win. */
body.role-employee #dataTable col[data-col="mail"],
body.role-employee #dataTable col[data-col="actions"] {
  width: 0 !important;
}
body.role-employee #dataTable th[data-col="mail"],
body.role-employee #dataTable td.cell-mail,
body.role-employee #dataTable th[data-col="actions"],
body.role-employee #dataTable td.col-actions {
  width: 0 !important;
  max-width: 0 !important;
  min-width: 0 !important;
  padding: 0 !important;
  border-left: 0 !important;
  border-right: 0 !important;
  overflow: hidden !important;
  white-space: nowrap !important;
  font-size: 0 !important;
  line-height: 0 !important;
}
/* Hide anything inside those cells (links, buttons, resizer, header label). */
body.role-employee #dataTable th[data-col="mail"] > *,
body.role-employee #dataTable td.cell-mail > *,
body.role-employee #dataTable th[data-col="actions"] > *,
body.role-employee #dataTable td.col-actions > * {
  display: none !important;
}

/* Bank cell colors: admin-filled = purple, user-filled = black. */
.data-table td.cell-bank.bank-admin { color: #7c3aed; font-weight: 700; }
.data-table td.cell-bank.bank-user { color: var(--text); }

/* Inline Bank editing (employees can edit only the Bank cell) */
body.role-employee #dataTable .cell-bank,
body.role-employee #dataTable .cell-note { cursor: text; }

body.role-employee #dataTable .cell-bank.bank-locked { cursor: not-allowed; }
.bank-edit-input {
  width: 100%;
  min-width: 90px;
  border: 1px solid var(--primary);
  border-radius: 4px;
  padding: 3px 5px;
  outline: none;
  background: #fff;
}
.note-edit-input {
  min-height: 44px;
  resize: vertical;
  white-space: pre-wrap;
}
