* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #f5f5f5;
  --surface: #fff;
  --surface-subtle: #fafafa;
  --surface-raised: #f8f9fa;
  --surface-th: #f8f8f8;
  --surface-tab-inactive: #ebebeb;
  --border: #ddd;
  --border-subtle: #eee;
  --border-light: #e8e8e8;
  --border-strong: #333;
  --text: #222;
  --text-secondary: #666;
  --text-muted: #999;
  --text-dim: #555;
  --text-faint: #888;
  --text-strong: #111;
  --text-th: #555;
  --accent: #4a90d9;
  --negative: #c0392b;
  --positive: #27ae60;
  --formula-bg: #f0f4f8;
  --formula-border: #d0dbe8;
  --formula-text: #444;
  --warning-bg: #fde8e8;
  --warning-border: #f5c6c6;
  --warning-text: #c0392b;
  --note-border: #ddd;
}

html.dark {
  --bg: #111;
  --surface: #1c1c1e;
  --surface-subtle: #242426;
  --surface-raised: #242426;
  --surface-th: #242426;
  --surface-tab-inactive: #2c2c2e;
  --border: #3a3a3c;
  --border-subtle: #2c2c2e;
  --border-light: #2c2c2e;
  --border-strong: #aeaeb2;
  --text: #e5e5e7;
  --text-secondary: #98989d;
  --text-muted: #636366;
  --text-dim: #aeaeb2;
  --text-faint: #8e8e93;
  --text-strong: #f2f2f7;
  --text-th: #aeaeb2;
  --accent: #5b9bd5;
  --negative: #ff453a;
  --positive: #30d158;
  --formula-bg: #1c2333;
  --formula-border: #2a3a50;
  --formula-text: #b0bec8;
  --warning-bg: #3a1a1a;
  --warning-border: #5a2a2a;
  --warning-text: #ff6b6b;
  --note-border: #3a3a3c;
}

html {
  overflow-y: scroll;
  font-size: 18px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding: 20px;
}

/* Layout: content + sticky TOC */
.layout {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  gap: 24px;
  align-items: flex-start;
}

.container {
  flex: 1;
  min-width: 0;
  padding-right: 214px; /* space for fixed TOC (190px + 24px gap) */
}

/* Fixed TOC — centered vertically on right side */
.toc {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  right: max(16px, calc((100vw - 1100px) / 2 - 4px));
  width: 190px;
  background: var(--surface-subtle);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  padding: 12px 8px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.toc-title {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 8px;
}

.toc ul {
  list-style: none;
  padding: 0;
}

.toc li {
  margin-bottom: 2px;
}

.toc a {
  display: block;
  padding: 4px 6px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: 4px;
  border-left: 2px solid transparent;
  transition: all 0.15s;
}

.toc a:hover {
  color: var(--text-dim);
  background: var(--border-subtle);
}

.toc a.active {
  color: var(--text);
  font-weight: 600;
  border-left-color: var(--border-strong);
  background: var(--surface-raised);
}

h1 {
  font-size: 1.5rem;
  margin: 0 0 4px;
}

.subtitle {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-top: 12px;
  margin-bottom: 16px;
}

/* Sticky app header (title + tabs) */
.app-header {
  background: var(--bg);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.btn-theme {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 1rem;
  cursor: pointer;
  color: var(--text-secondary);
  line-height: 1;
  flex-shrink: 0;
  transition: border-color 0.15s, background 0.15s;
}

.btn-theme:hover {
  background: var(--surface-raised);
  border-color: var(--text-muted);
}

.btn-theme::before {
  content: '☀️';
}

html.dark .btn-theme::before {
  content: '🌙';
}

.btn-reset {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 0.75rem;
  color: var(--text-muted);
  cursor: pointer;
  width: 100%;
  margin-top: 8px;
  text-align: center;
  transition: color 0.15s, border-color 0.15s;
}

.btn-reset:hover {
  color: var(--negative);
  border-color: var(--negative);
}

/* Tabs */
.tabs {
  display: flex;
  gap: 0;
  margin-bottom: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg);
}

.tab {
  flex: 1;
  padding: 10px 20px;
  font-size: 0.95rem;
  font-weight: 400;
  border: none;
  background: var(--surface-tab-inactive);
  color: var(--text-muted);
  cursor: pointer;
  box-shadow: inset 0 -3px 0 transparent;
  transition: background 0.15s, color 0.15s;
}

.tab:not(:last-child) {
  border-right: 1px solid var(--border);
}

.tab:hover {
  background: var(--border-subtle);
  color: var(--text-secondary);
}

.tab.active {
  background: var(--surface);
  color: var(--text-strong);
  font-weight: 700;
  box-shadow: inset 0 -3px 0 var(--border-strong);
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}

/* Top revenue input */
.top-input {
  background: var(--surface);
  border: 2px solid var(--border-strong);
  border-radius: 8px;
  padding: 16px 20px;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.top-input label {
  font-weight: 600;
  white-space: nowrap;
}

.top-input input {
  font-size: 1.3rem;
  font-weight: 700;
  width: 140px;
  padding: 4px 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  text-align: right;
  background: transparent;
  color: var(--text);
}

.top-input input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--surface);
}

.unit {
  color: var(--text-secondary);
  font-size: 0.85rem;
}

/* Panels */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 12px;
  overflow: hidden;
}

.panel-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--border-subtle);
}

.panel-header:hover {
  background: var(--surface-subtle);
}

.panel-header h2 {
  font-size: 1rem;
  flex: 1;
}

.panel-summary {
  font-size: 0.85rem;
  color: var(--text-secondary);
  font-weight: 600;
}

.arrow {
  font-size: 0.7rem;
  transition: transform 0.2s;
  color: var(--text-muted);
}

.panel.collapsed .arrow {
  transform: rotate(-90deg);
}

.panel.collapsed .panel-body {
  display: none;
}

.panel-body {
  padding: 16px;
}

.panel-body h3 {
  font-size: 0.9rem;
  color: var(--text-dim);
  margin: 16px 0 8px 0;
}

.panel-body h3:first-child {
  margin-top: 0;
}

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
  font-size: 0.88rem;
}

th {
  background: var(--surface-th);
  text-align: left;
  padding: 8px 10px;
  font-weight: 600;
  border-bottom: 2px solid var(--border);
  font-size: 0.82rem;
  color: var(--text-th);
}

/* Numeric columns: right-align header + data */
th:not(:first-child),
td:not(:first-child) {
  text-align: right;
}

td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--border-subtle);
}

tfoot td {
  border-top: 2px solid var(--border);
  border-bottom: none;
  font-weight: 600;
}

/* Match +∞ cell width/alignment to sibling inputs */
.tier-inf {
  display: inline-block;
  width: 70px;
  padding: 3px 26px 3px 0px;
  font-size: 0.88rem;
  text-align: right;
  box-sizing: border-box;
}

/* Inline inputs */
table input[type="number"],
table input[type="text"] {
  width: 70px;
  padding: 3px 6px;
  border: 1px solid transparent;
  border-radius: 4px;
  font-size: 0.88rem;
  text-align: right;
  background: transparent;
  color: var(--text);
}

table input[readonly] {
  color: var(--text-muted);
  cursor: default;
  pointer-events: none;
}

table input[type="number"]:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--surface);
}

/* Align tfoot headcount with input spinner above */
.staff-table .total-headcount {
  padding-right: 24px;
}

/* Extra cost rows in staff table footer */
.extra-cost td {
  border-top: none;
  font-weight: 400;
  color: var(--text-dim);
}

/* Staff table fixed column widths */
.staff-table th:first-child,
.staff-table td:first-child { width: 40%; }
.staff-table th:nth-child(2),
.staff-table td:nth-child(2) { width: 15%; }
.staff-table th:nth-child(3),
.staff-table td:nth-child(3) { width: 25%; }
.staff-table th:nth-child(4),
.staff-table td:nth-child(4) { width: 20%; }

/* Alloc table fixed column widths */
.alloc-table th:first-child,
.alloc-table td:first-child { width: 25%; }
.alloc-table th:nth-child(2),
.alloc-table td:nth-child(2) { width: 15%; }
.alloc-table th:nth-child(3),
.alloc-table td:nth-child(3) { width: 20%; }
.alloc-table th:nth-child(4),
.alloc-table td:nth-child(4) { width: 15%; }
.alloc-table th:nth-child(5),
.alloc-table td:nth-child(5) { width: 25%; }

/* Income table fixed column widths */
.income-table th:first-child,
.income-table td:first-child { width: 25%; }
.income-table th:nth-child(2),
.income-table td:nth-child(2) { width: 15%; }
.income-table th:nth-child(3),
.income-table td:nth-child(3) { width: 20%; }
.income-table th:nth-child(4),
.income-table td:nth-child(4) { width: 20%; }
.income-table th:nth-child(5),
.income-table td:nth-child(5) { width: 20%; }

.simple-table td {
  padding: 8px 10px;
}

.simple-table td:first-child {
  width: 55%;
}

/* Formula */
.formula {
  background: var(--formula-bg);
  border: 1px solid var(--formula-border);
  border-radius: 6px;
  padding: 10px 14px;
  font-size: 0.85rem;
  color: var(--formula-text);
  margin: 8px 0;
  font-family: monospace;
}

/* Result row */
.result-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 6px;
  margin: 8px 0;
}

.result-value {
  font-weight: 700;
  font-size: 1rem;
  text-align: right;
  white-space: nowrap;
}

/* Result detail (breakdown) */
.result-detail {
  font-size: 0.8rem;
  font-weight: 400;
  color: var(--text-faint);
  margin-top: 2px;
}

/* Warning */
.warning {
  color: var(--warning-text);
  font-weight: 600;
  font-size: 0.85rem;
  padding: 6px 10px;
  background: var(--warning-bg);
  border: 1px solid var(--warning-border);
  border-radius: 4px;
}

/* Note */
.note {
  font-size: 0.82rem;
  color: var(--text-faint);
  margin-bottom: 10px;
}

/* Colors */
.negative { color: var(--negative); }
.positive { color: var(--positive); }

/* Contribution simulation */
.contrib-dept {
  margin-bottom: 16px;
}

.contrib-dept h4 {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 6px;
  font-weight: 600;
}

.contrib-table {
  margin-bottom: 8px;
}

.contrib-table input[type="number"] {
  width: 65px;
}

.contrib-table th:first-child,
.contrib-table td:first-child { width: 10%; }
.contrib-table th:nth-child(2),
.contrib-table td:nth-child(2) { width: 18%; }
.contrib-table th:nth-child(3),
.contrib-table td:nth-child(3) { width: 24%; }
.contrib-table th:nth-child(4),
.contrib-table td:nth-child(4) { width: 18%; }
.contrib-table th:nth-child(5),
.contrib-table td:nth-child(5) { width: 24%; }

.contrib-table .loss td {
  color: var(--negative);
}

.contrib-result {
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
  padding: 6px 10px;
  background: var(--surface-raised);
  border-radius: 4px;
}

/* Income table */
.income-table td:last-child {
  font-weight: 700;
}

/* Inline panel notes */
.panel-note {
  margin-top: 12px;
  padding: 10px 12px 10px 16px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  line-height: 1.6;
  background: var(--surface-subtle);
  border-left: 3px solid var(--note-border);
  border-radius: 0 4px 4px 0;
}

.panel-note ul {
  margin: 0;
  padding-left: 18px;
}

.panel-note li {
  margin-bottom: 3px;
}

.panel-note strong {
  color: var(--text-dim);
}

/* Responsive */
@media (max-width: 900px) {
  .toc { display: none; }
  .layout { max-width: 860px; }
  .container { padding-right: 0; }
}

@media (max-width: 600px) {
  body { padding: 8px; }
  .layout { gap: 0; }
  .container { min-width: 0; }

  /* Sticky header */
  .app-header {
      /*margin-bottom: 12px;
      padding: 10px 0 12px; */
  }

  h1 { font-size: 1.2rem; }

  /* Tab bar */
  .tab-btn { padding: 8px 12px; font-size: 0.9rem; }

  /* Revenue input: giảm width để "triệu" không xuống dòng */
  .top-input { flex-wrap: nowrap; gap: 8px; padding: 12px; }
  .top-input input { width: 90px; font-size: 1.1rem; }

  /* Panel header: tiêu đề dài không bị gãy */
  .panel-header { gap: 6px; padding: 10px 10px; }
  .panel-header h2 { font-size: 0.88rem; }
  .panel-summary { display: none; }

  /* Result row: label dài như "Tổng chi phí cố định..." */
  .result-row { align-items: flex-start; gap: 8px; }
  .result-row > span:first-child { font-size: 0.82rem; line-height: 1.4; }

  /* Panels */
  .panel-body { padding: 10px 8px; }
  .panel h2 { font-size: 1rem; }

  /* All tables scroll horizontally */
  .staff-table,
  .alloc-table,
  .tier-table,
  .income-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }

  /* Contrib table scroll */
  .contrib-dept {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .contrib-table {
    min-width: 500px;
    white-space: nowrap;
  }

  table input[type="number"] { width: 52px; }
  .contrib-table input[type="number"] { width: 52px; }

  /* Summary bar */
  .summary-bar { font-size: 0.8rem; flex-wrap: wrap; gap: 4px; }

  /* Result detail */
  .result-detail { font-size: 0.78rem; }
}
