:root {
  --dark: #0d3225;
  --forest: #1b4d3a;
  --mid: #2f6b4f;
  --sage: #6fa98a;
  --sage-light: #e7f2ec;
  --cream: #f8f9f6;
  --text: #1e2b24;
  --text-muted: #5b6b62;
  --white: #ffffff;
  --danger: #b3413b;
  --danger-bg: #fdecea;
  --amber: #a3670a;
  --amber-bg: #fdf3e0;
  --shadow: 0 12px 30px -16px rgba(13, 50, 37, 0.25);
  --radius: 14px;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: 'Sarabun', 'Segoe UI', sans-serif;
  color: var(--text);
  background: var(--cream);
  line-height: 1.6;
}
a { color: inherit; text-decoration: none; }
h1, h2, h3, h4 { margin: 0 0 8px; font-weight: 700; color: var(--dark); }

.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 228px;
  flex: none;
  background: linear-gradient(180deg, var(--dark) 0%, #0a2a1e 100%);
  color: var(--white);
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.sidebar .brand { display: flex; align-items: center; gap: 10px; margin-bottom: 24px; padding: 0 4px; }
.sidebar .brand img { height: 34px; width: auto; background: var(--white); border-radius: 8px; padding: 4px; }
.sidebar .brand span { font-weight: 700; font-size: 16px; letter-spacing: .01em; }
.sidebar nav a {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14.5px;
  font-weight: 500;
  color: rgba(255,255,255,0.78);
  margin-bottom: 2px;
  transition: background .15s ease, color .15s ease;
}
.sidebar nav a svg { width: 18px; height: 18px; flex: none; opacity: .75; }
.sidebar nav a:hover { background: rgba(255,255,255,0.08); color: var(--white); }
.sidebar nav a:hover svg { opacity: 1; }
.sidebar nav a.active { background: var(--forest); color: var(--white); box-shadow: inset 0 1px 0 rgba(255,255,255,0.08); }
.sidebar nav a.active svg { opacity: 1; }
.sidebar .logout { margin-top: auto; font-size: 14px; color: rgba(255,255,255,0.6); padding: 8px 12px; }
.sidebar .logout:hover { color: var(--white); }

.main { flex: 1; padding: 32px 40px; max-width: 1160px; }
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; flex-wrap: wrap; gap: 12px; }
.page-head p.subtitle { margin: 4px 0 0; color: var(--text-muted); font-size: 14.5px; }

.card { background: var(--white); border-radius: var(--radius); padding: 24px; box-shadow: var(--shadow); margin-bottom: 24px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 22px; border-radius: 999px; font-weight: 600; font-size: 14.5px;
  cursor: pointer; border: 2px solid transparent; transition: all .2s ease;
  font-family: inherit;
}
.btn-primary { background: var(--forest); color: var(--white); }
.btn-primary:hover { background: var(--dark); }
.btn-outline { border-color: var(--forest); color: var(--forest); background: transparent; }
.btn-outline:hover { background: var(--forest); color: var(--white); }
.btn-danger { border-color: var(--danger); color: var(--danger); background: transparent; }
.btn-danger:hover { background: var(--danger); color: var(--white); }
.btn-sm { padding: 7px 16px; font-size: 13px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px 14px; font-size: 14.5px; border-bottom: 1px solid var(--sage-light); }
th { color: var(--text-muted); font-weight: 600; font-size: 13px; text-transform: uppercase; letter-spacing: .03em; }
tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--sage-light); }
.text-right { text-align: right; }
.empty-state { text-align: center; padding: 48px 20px; color: var(--text-muted); }
.table-sub { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.cell-danger { color: var(--danger); font-weight: 600; }

.tabs { display: flex; gap: 8px; margin-bottom: 20px; flex-wrap: wrap; }
.tabs a { padding: 8px 18px; border-radius: 999px; font-size: 13.5px; font-weight: 600; color: var(--text-muted); background: var(--sage-light); }
.tabs a:hover { color: var(--dark); }
.tabs a.active { background: var(--forest); color: var(--white); }

.badge { display: inline-block; padding: 4px 12px; border-radius: 999px; font-size: 12.5px; font-weight: 600; }
.badge-unpaid { background: var(--danger-bg); color: var(--danger); }
.badge-paid { background: var(--sage-light); color: var(--forest); }
.badge-void { background: #eee; color: #777; }
.badge-partial { background: #fff3e0; color: #b26a00; }

/* Phrases that must never be split across lines (dates, period labels) —
   if the line is full, the whole phrase wraps to the next line together. */
.nowrap { white-space: nowrap; }

/* Searchable combobox (client / product pickers) */
.combo { position: relative; }
.combo-list {
  position: absolute; z-index: 40; top: 100%; left: 0; right: 0; margin-top: 4px;
  background: var(--white); border: 1px solid #dfe7e2; border-radius: 10px;
  max-height: 280px; overflow-y: auto; box-shadow: 0 8px 24px rgba(0,0,0,0.12);
  display: none;
}
.combo-list.open { display: block; }
.combo-item { padding: 9px 14px; cursor: pointer; font-size: 14px; }
.combo-item:hover, .combo-item.active { background: var(--sage-light); }
.combo-group { padding: 8px 14px 2px; font-size: 12px; font-weight: 700; color: var(--text-muted); }
.combo-empty { padding: 9px 14px; font-size: 13.5px; color: var(--text-muted); }
.line-items-table .combo input[type="text"] { width: 100%; }
.line-items-table input.month-input { width: 100%; min-width: 120px; }

/* PromptPay QR box on the invoice document */
.doc-qr { display: flex; align-items: center; gap: 14px; margin-top: 10px; }
.doc-qr img { width: 96px; height: 96px; image-rendering: pixelated; }
.doc-qr .doc-qr-note { font-size: 12.5px; color: var(--text-muted); }

.field { margin-bottom: 18px; }
.field label { display: block; font-size: 14px; font-weight: 600; color: var(--dark); margin-bottom: 6px; }
.label-hint { font-weight: 400; color: var(--text-muted); font-size: 12.5px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 11px 14px; border-radius: 10px; border: 1px solid #dfe7e2;
  font-family: inherit; font-size: 14.5px; background: var(--cream);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--mid); background: var(--white); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.form-note { font-size: 13px; color: var(--text-muted); margin-top: -10px; margin-bottom: 18px; }

.alert { padding: 12px 16px; border-radius: 10px; font-size: 14.5px; margin-bottom: 18px; }
.alert-error { background: var(--danger-bg); color: var(--danger); }
.alert-ok { background: var(--sage-light); color: var(--forest); }

.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: radial-gradient(ellipse at top right, var(--sage-light) 0%, var(--cream) 55%); }
.login-card { background: var(--white); border-radius: 20px; box-shadow: var(--shadow); padding: 40px; width: 100%; max-width: 380px; text-align: center; }
.login-card img { height: 64px; margin: 0 auto 18px; }

.card-section-title {
  font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  color: var(--mid); margin: 0 0 16px; display: flex; align-items: center; gap: 9px;
}
.card-section-title::before { content: ''; width: 4px; height: 14px; background: var(--mid); border-radius: 2px; }

/* overflow must stay visible so the product-picker dropdown isn't clipped */
.line-items-card { padding: 0; overflow: visible; }
.line-items-card .card-section-title { padding: 22px 24px 0; }
.line-items-card .add-row-wrap { padding: 14px 24px 22px; }

.line-items-table input { width: 100%; padding: 9px 10px; font-size: 14px; font-family: inherit; border-radius: 8px; border: 1px solid transparent; background: transparent; }
.line-items-table input:focus { border-color: var(--mid); background: var(--white); }
.line-items-table input.qty-input, .line-items-table input.price-input { text-align: right; }
.line-items-table .combo input[type="text"] { border: 1px solid #d7ded9; background: var(--cream); }
.line-items-table .combo input[type="text"]:focus { border-color: var(--mid); background: var(--white); }
/* dropdown opens wider than its cell so full names are readable */
.line-items-table .combo-list { left: 0; right: auto; width: 360px; max-width: 70vw; }
.line-items-table select.product-select {
  width: 100%; min-width: 170px; max-width: 220px; padding: 9px 10px; font-size: 13.5px; font-family: inherit;
  color: var(--text); border-radius: 8px; border: 1px solid #d7ded9; background: var(--cream); cursor: pointer;
}
.line-items-table select.product-select:focus { outline: none; border-color: var(--mid); background: var(--white); }
.line-items-table td { vertical-align: middle; }
.line-items-table tbody tr:nth-child(even) td { background: rgba(111,169,138,0.06); }
.line-items-table tbody tr:hover td { background: var(--sage-light); }
.line-items-table .amount-cell { font-weight: 600; color: var(--dark); }
.remove-row {
  width: 30px; height: 30px; border-radius: 50%; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--danger); background: var(--danger-bg); border: 1px solid #f6d9d6;
  font-size: 16px; line-height: 1; cursor: pointer; transition: all .15s ease;
}
.remove-row:hover { background: var(--danger); color: var(--white); border-color: var(--danger); }

.toggle-row { display: flex; align-items: center; gap: 12px; cursor: pointer; margin-bottom: 14px; }
.toggle-switch { position: relative; width: 42px; height: 24px; flex: none; }
.toggle-switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0; background: #d8e2dc; border-radius: 999px; transition: background .2s ease;
}
.toggle-slider::before {
  content: ''; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px;
  background: var(--white); border-radius: 50%; transition: transform .2s ease; box-shadow: 0 1px 3px rgba(0,0,0,.25);
}
.toggle-switch input:checked + .toggle-slider { background: var(--forest); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(18px); }
.toggle-label { font-weight: 600; color: var(--dark); font-size: 14.5px; }

.totals-box { max-width: 360px; margin: 0 0 0 auto; background: var(--sage-light); border-radius: var(--radius); padding: 18px 22px; }
.totals-box .row { display: flex; justify-content: space-between; padding: 7px 0; font-size: 15px; color: var(--text); gap: 14px; }
.totals-box .row.total { border-top: 2px solid var(--forest); font-weight: 700; font-size: 18px; color: var(--dark); margin-top: 4px; padding-top: 12px; }

/* Company switcher (sidebar) */
.company-block { margin: -6px 0 20px; }
.company-block-label {
  font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .07em;
  color: rgba(255,255,255,0.5); margin-bottom: 7px; padding: 0 4px;
}
.company-avatar {
  flex: none; width: 32px; height: 32px; border-radius: 9px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 15px; font-weight: 700; color: var(--white); line-height: 1;
  background: linear-gradient(135deg, var(--sage) 0%, var(--mid) 100%);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.25), 0 2px 6px rgba(0,0,0,0.25);
}
.company-switch, .company-single {
  position: relative; display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 12px;
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.14);
  transition: background .15s ease, border-color .15s ease;
}
.company-switch:hover, .company-switch:focus-within {
  background: rgba(255,255,255,0.12);
  border-color: var(--sage);
}
.company-switch::after {
  content: ''; position: absolute; right: 14px; top: 50%; width: 7px; height: 7px;
  border-right: 2px solid var(--sage); border-bottom: 2px solid var(--sage);
  transform: translateY(-70%) rotate(45deg); pointer-events: none;
  transition: border-color .15s ease;
}
.company-switch:hover::after { border-color: var(--white); }
.company-switch select {
  flex: 1; width: 100%; min-width: 0; padding: 6px 26px 6px 0;
  font-size: 13.5px; font-weight: 600; line-height: 1.35;
  background: transparent; color: var(--white);
  border: none; font-family: inherit; cursor: pointer;
  appearance: none; -webkit-appearance: none; -moz-appearance: none;
  white-space: normal;
}
.company-switch select:focus { outline: none; }
.company-switch select option { color: #222; background: var(--white); font-weight: 500; }
.company-single {
  font-size: 13.5px; font-weight: 600; color: var(--white); line-height: 1.35;
}

/* Stat cards */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { margin-bottom: 0; padding: 20px; border-top: 4px solid var(--sage); }
.stat-card .stat-label { font-size: 13px; font-weight: 600; color: var(--text-muted); margin-bottom: 6px; }
.stat-card .stat-value { font-size: 24px; font-weight: 700; color: var(--dark); }
.stat-card .stat-sub { font-size: 12.5px; color: var(--text-muted); margin-top: 4px; }
.stat-card-danger { border-top-color: var(--danger); }
.stat-card-danger .stat-value { color: var(--danger); }

/* Dashboard */
.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; align-items: start; }
.dash-grid .card { margin-bottom: 0; }
@media (max-width: 1000px) { .dash-grid { grid-template-columns: 1fr; } }

.chart-legend { display: flex; gap: 18px; font-size: 13px; color: var(--text-muted); }
.chart-legend span { display: inline-flex; align-items: center; gap: 7px; }
.chart-dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.chart-dot-inv { background: var(--sage); }
.chart-dot-rec { background: var(--forest); }
.chart {
  display: flex; align-items: flex-end; gap: 8px;
  height: 220px; margin-top: 20px; padding-top: 10px;
  border-bottom: 2px solid var(--sage-light);
}
.chart-col { flex: 1; display: flex; flex-direction: column; height: 100%; min-width: 0; cursor: default; }
.chart-bars { flex: 1; display: flex; align-items: flex-end; justify-content: center; gap: 3px; }
.chart-bar { width: 38%; max-width: 22px; border-radius: 4px 4px 0 0; transition: opacity .15s ease; }
.chart-col:hover .chart-bar { opacity: .8; }
.chart-bar-inv { background: var(--sage); }
.chart-bar-rec { background: var(--forest); }
.chart-label { text-align: center; font-size: 11px; color: var(--text-muted); line-height: 1.3; margin-top: 6px; }

/* Document language toggle */
.doc-lang-bar {
  width: 158mm; max-width: 100%; margin: 0 auto 14px;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  font-size: 13.5px; color: var(--text-muted); font-weight: 600;
}
.doc-lang-bar a {
  padding: 6px 16px; border-radius: 999px; background: var(--sage-light);
  color: var(--text-muted); font-weight: 600; font-size: 13px;
}
.doc-lang-bar a:hover { color: var(--dark); }
.doc-lang-bar a.active { background: var(--forest); color: var(--white); }
.doc-lang-hint { font-weight: 400; font-size: 12px; color: var(--amber); background: var(--amber-bg); padding: 4px 12px; border-radius: 8px; }

/* Printable document — A5 landscape-width kept as before (148 x 210mm portrait) */
.doc-page {
  background: var(--white);
  width: 158mm;
  max-width: 100%;
  margin: 0 auto;
  padding: 10mm 9mm 8mm;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border-top: 6px solid var(--forest);
  position: relative;
  font-size: 13px;
}
.doc-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 16px; margin-bottom: 12px; padding-bottom: 10px;
  border-bottom: 2px solid var(--sage-light);
}
.doc-issuer { display: flex; gap: 10px; align-items: flex-start; min-width: 0; }
.doc-issuer img { height: 42px; flex: none; }
.doc-issuer-name { margin: 0 0 2px; font-weight: 700; font-size: 14px; color: var(--dark); line-height: 1.35; }
.doc-title { text-align: right; flex: none; }
.doc-title h1 { font-size: 21px; color: var(--forest); letter-spacing: .04em; margin-bottom: 6px; }
.doc-title h1.doc-title-long { font-size: 16px; max-width: 230px; margin-left: auto; line-height: 1.35; }
.doc-title .doc-stamp { margin-bottom: 6px; }
.doc-meta-table { width: auto; margin-left: auto; border-collapse: collapse; }
.doc-meta-table td { padding: 1px 0 1px 12px; border: none; font-size: 11.5px; text-align: right; }
.doc-meta-table td:first-child { color: var(--text-muted); }
.doc-meta-table td:last-child { font-weight: 600; color: var(--text); white-space: nowrap; }
.doc-meta { margin: 0; font-size: 11px; color: var(--text-muted); line-height: 1.5; white-space: pre-line; }

.doc-billto { background: var(--cream); border-radius: 10px; padding: 10px 14px; margin-bottom: 12px; }
.doc-billto h4 { font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--mid); margin-bottom: 3px; }
.doc-billto p { margin: 0; line-height: 1.45; }
.doc-billto p.doc-meta { margin-top: 3px; }

.doc-table { font-size: 12px; margin-bottom: 12px; }
.doc-table th, .doc-table td { padding: 6px 8px; }
.doc-table th { background: var(--sage-light); color: var(--dark); font-size: 10.5px; }
.doc-table th:first-child { border-radius: 6px 0 0 6px; }
.doc-table th:last-child { border-radius: 0 6px 6px 0; }
.doc-table tbody tr:nth-child(even) td { background: rgba(111,169,138,0.05); }
.doc-table tbody tr:hover td { background: rgba(111,169,138,0.05); }

.doc-summary { display: flex; gap: 14px; align-items: stretch; margin-bottom: 4px; }
.doc-words {
  flex: 1; background: var(--cream); border-radius: 10px; padding: 10px 14px;
  display: flex; flex-direction: column; justify-content: center;
}
.doc-words h4 { font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--mid); margin-bottom: 3px; }
.doc-words p { margin: 0; font-weight: 600; font-size: 12px; color: var(--text); }
.doc-page .totals-box { max-width: 46%; min-width: 45%; margin: 0; font-size: 12px; padding: 10px 14px; }
.doc-page .totals-box .row { padding: 4px 0; font-size: 12px; }
.doc-page .totals-box .row.total { font-size: 14px; padding-top: 7px; }

.doc-notes { margin-top: 10px; font-size: 11px; color: var(--text-muted); line-height: 1.45; }
.doc-notes-success { background: var(--sage-light); color: var(--forest); border-radius: 10px; padding: 10px 14px; border-left: 4px solid var(--forest); }
.doc-bank { margin-top: 10px; padding: 10px 14px; background: var(--sage-light); border-radius: 10px; font-size: 11px; border-left: 4px solid var(--forest); }
.doc-bank h4 { font-size: 10.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--mid); margin-bottom: 3px; }

.doc-signs { display: flex; justify-content: flex-end; margin-top: 26px; }
.doc-sign { width: 48%; text-align: center; }
.doc-sign-note { margin: 4px 0 0; font-size: 12px; font-weight: 700; color: var(--dark); }
.doc-sign-line { border-bottom: 1px dotted #9ab0a3; height: 34px; margin: 0 12px 6px; position: relative; }
.doc-sign-img { position: absolute; bottom: -3px; left: 50%; transform: translateX(-50%); max-height: 52px; max-width: 75%; }
.doc-sign p { margin: 0; font-size: 11.5px; font-weight: 600; color: var(--text); }
.doc-sign p.doc-meta { font-weight: 400; margin-top: 2px; }

.doc-stamp {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 14px 4px 10px; border: 2.5px solid currentColor; border-radius: 8px;
  font-weight: 800; font-size: 11px; letter-spacing: .06em;
}
.doc-stamp-paid { color: var(--forest); }
.doc-stamp-paid::before {
  content: ''; width: 6px; height: 9px; flex: none;
  border-right: 2px solid currentColor; border-bottom: 2px solid currentColor;
  transform: rotate(40deg) translate(-1px, -1px);
}
.doc-stamp-void {
  position: absolute; top: 32%; left: 50%; color: var(--danger); font-size: 20px;
  transform: translate(-50%, -50%) rotate(-16deg); border-width: 4px; padding: 7px 24px;
  opacity: .8; background: transparent; z-index: 5;
}

@media (max-width: 860px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; flex-direction: row; align-items: center; overflow-x: auto; gap: 4px; }
  .sidebar .brand { margin-bottom: 0; }
  .sidebar nav { display: flex; }
  .sidebar nav a span { display: none; }
  .sidebar nav a { padding: 10px; }
  .sidebar .company-block { margin: 0; }
  .sidebar .logout { margin-top: 0; margin-left: auto; white-space: nowrap; }
  .main { padding: 20px; }
  .form-row { grid-template-columns: 1fr; }
  .doc-summary { flex-direction: column; }
  .doc-page .totals-box { max-width: 100%; min-width: 0; }
}

@page { size: A5; margin: 7mm; }
@media print {
  .no-print { display: none !important; }
  body { background: var(--white); }
  .app-shell { display: block; }
  .main { padding: 0; max-width: none; }
  .doc-page { width: auto; max-width: none; padding: 0; box-shadow: none; border-radius: 0; margin: 0; border-top: none; }
  /* Tighten vertical rhythm so a full document stays on one A5 page */
  .doc-head { margin-bottom: 8px; padding-bottom: 7px; }
  .doc-billto { padding: 7px 10px; margin-bottom: 8px; }
  .doc-table { margin-bottom: 8px; }
  .doc-table th, .doc-table td { padding: 4px 7px; }
  .doc-words { padding: 7px 10px; }
  .doc-page .totals-box { padding: 7px 10px; }
  .doc-page .totals-box .row { padding: 2.5px 0; }
  .doc-page .totals-box .row.total { padding-top: 5px; }
  .doc-notes { margin-top: 7px; }
  .doc-notes-success { padding: 7px 10px; }
  .doc-bank { margin-top: 7px; padding: 7px 10px; }
  /* Pin the signature box to the bottom-right edge of the page */
  .doc-signs { position: fixed; bottom: 0; right: 0; width: 48%; margin: 0; }
  .doc-sign { width: 100%; }
  .doc-sign-note { margin-bottom: 5px; }
  .doc-sign-line { height: 24px; }
  /* Reserve space so content never overlaps the pinned signature */
  .doc-page { padding-bottom: 22mm; }
}
