:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e3e6ea;
  --text: #1d2530;
  --muted: #6b7685;
  --primary: #2f6fed;
  --primary-dark: #2356c4;
  --danger: #d6453d;
  --danger-dark: #b6362f;
  --ok: #1f9d6b;
  --shadow: 0 1px 3px rgba(20, 30, 50, .08), 0 6px 20px rgba(20, 30, 50, .06);
  --radius: 9px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  display: flex;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: 224px;
  flex-shrink: 0;
  background: #14202e;
  color: #c7d0db;
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 20px;
}

.brand-mark {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  display: grid;
  place-items: center;
  font-size: 18px;
}

.brand-name {
  font-size: 17px;
  font-weight: 650;
  color: #fff;
  letter-spacing: .2px;
}

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

.nav a {
  padding: 9px 12px;
  border-radius: 7px;
  color: #c7d0db;
  text-decoration: none;
  font-weight: 500;
  transition: background .12s, color .12s;
}

.nav a:hover { background: rgba(255, 255, 255, .07); color: #fff; }
.nav a.active { background: var(--primary); color: #fff; }

.sidebar-foot { margin-top: auto; padding-top: 16px; }

/* ---------- Content ---------- */
.content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 32px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.topbar h1 { margin: 0; font-size: 21px; font-weight: 650; }
.topbar-right { display: flex; align-items: center; gap: 16px; }
.page-actions { display: flex; gap: 8px; }

/* ---------- Region switcher ---------- */
.region-switcher {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-right: 16px;
  border-right: 1px solid var(--border);
}

.region-switcher[hidden] { display: none; }

.region-label {
  font-size: 11.5px;
  letter-spacing: .4px;
  text-transform: uppercase;
  font-weight: 600;
  color: var(--muted);
}

.region-select {
  width: auto;
  padding: 7px 28px 7px 10px;
  font-size: 13px;
  font-weight: 550;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path fill='%236b7685' d='M0 0l5 6 5-6z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

.view {
  padding: 26px 32px;
  overflow-y: auto;
}

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 8px 14px;
  font-size: 13.5px;
  font-weight: 550;
  cursor: pointer;
  font-family: inherit;
  transition: background .12s, border-color .12s, opacity .12s;
}

.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover:not(:disabled) { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-dark); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-link {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font: inherit;
  padding: 2px 4px;
}
.btn-link:hover { text-decoration: underline; }
.btn-link.danger { color: var(--danger); }

/* ---------- Cards & panels ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card + .card { margin-top: 20px; }

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.card-head h2 { margin: 0; font-size: 15px; font-weight: 620; }
.card-body { padding: 6px 0; }

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

.tab {
  padding: 9px 16px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 550;
  cursor: pointer;
  margin-bottom: -1px;
}

.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; }

th {
  text-align: left;
  font-size: 11.5px;
  letter-spacing: .4px;
  text-transform: uppercase;
  color: var(--muted);
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
}

td {
  padding: 11px 18px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #fafbfc; }

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

.num { text-align: right; font-variant-numeric: tabular-nums; }
.col-actions { text-align: right; white-space: nowrap; }
.col-actions .btn-link + .btn-link { margin-left: 8px; }

/* ---------- Chart of accounts tree ---------- */
.tree { list-style: none; margin: 0; padding: 0; }
.tree .tree { margin-left: 22px; }

.tree-group > .tree-row {
  font-weight: 600;
  padding: 7px 10px;
  border-radius: 6px;
}

.tree-group > .tree-row:hover { background: var(--bg); }

.tree-account .tree-row {
  padding: 6px 10px;
  display: flex;
  justify-content: space-between;
  color: var(--text);
}

.tree-account .tree-row:hover { background: #fafbfc; }
.tree-code {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  margin-right: 10px;
}

.tag {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 1px 7px;
  border-radius: 20px;
  margin-left: 8px;
}
.tag-muted { background: #eef0f3; color: var(--muted); }
.tag-expense { background: #fdeceb; color: var(--danger); }
.tag-income { background: #e7f5ee; color: var(--ok); }

/* ---------- Forms ---------- */
.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 5px;
  color: var(--text);
}

.field-inline { display: flex; align-items: center; gap: 8px; }
.field-inline label { margin: 0; }

input[type="text"], input[type="number"], input[type="date"], select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font: inherit;
  background: var(--surface);
  color: var(--text);
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(47, 111, 237, .12);
}

textarea { resize: vertical; min-height: 56px; }
.form-row { display: flex; gap: 14px; }
.form-row > .field { flex: 1; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 22, 33, .5);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 50;
}

.modal {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(10, 20, 40, .35);
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.modal.wide { max-width: 780px; }

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
}

.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 20px 22px; overflow-y: auto; }
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 22px;
  border-top: 1px solid var(--border);
}

.modal-close {
  background: none;
  border: none;
  font-size: 22px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
}
.modal-close:hover { color: var(--text); }

/* ---------- Journal lines editor ---------- */
.lines-table th, .lines-table td { padding: 6px 8px; }
.lines-table td:first-child, .lines-table th:first-child { padding-left: 0; }
.lines-table input, .lines-table select { padding: 6px 8px; }

.balance-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  margin-top: 12px;
  border-radius: 7px;
  font-weight: 600;
}
.balance-ok { background: #e7f5ee; color: var(--ok); }
.balance-off { background: #fdeecb; color: #9a6b00; }

/* ---------- Misc ---------- */
.empty {
  text-align: center;
  color: var(--muted);
  padding: 44px 20px;
}

.loading { color: var(--muted); padding: 30px; }

.banner-error {
  background: #fdeceb;
  border: 1px solid #f3c6c2;
  color: var(--danger-dark);
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 18px;
}

.toast-root {
  position: fixed;
  bottom: 22px;
  right: 22px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 80;
}

.toast {
  padding: 11px 16px;
  border-radius: 8px;
  color: #fff;
  box-shadow: 0 8px 24px rgba(10, 20, 40, .25);
  font-weight: 550;
  animation: slide-in .2s ease;
}
.toast-ok { background: var(--ok); }
.toast-err { background: var(--danger); }

@keyframes slide-in {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

.muted { color: var(--muted); }
.section-sub { color: var(--muted); margin: -6px 0 16px; }
