:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --text: #1f2933;
  --muted: #637083;
  --border: #dde3ea;
  --primary: #0f6e5b;
  --primary-dark: #0a5143;
  --primary-soft: #e3f3ef;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #9a6700;
  --warn-soft: #fff6db;
  --info-soft: #e8f0fe;
  --success-soft: #e3f6e8;
  --sidebar: #12302a;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1rem 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--danger); }
.pre { white-space: pre-line; }

/* Layout */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 236px; flex-shrink: 0; background: var(--sidebar); color: #cfe3dd;
  position: sticky; top: 0; height: 100vh; overflow-y: auto; padding: 1rem .75rem;
}
.brand { display: flex; align-items: center; gap: .5rem; color: #fff; font-weight: 700; font-size: 1.05rem; padding: .25rem .5rem 1rem; }
.brand-mark { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: #fff; font-size: .9rem; }
.nav-section { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: #7fa59b; margin: 1rem .5rem .35rem; }
.nav a { display: block; color: #d7e9e4; padding: .45rem .6rem; border-radius: 7px; margin-bottom: 1px; }
.nav a:hover { background: rgba(255,255,255,.07); text-decoration: none; }
.nav a.active { background: var(--primary); color: #fff; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .65rem 1.5rem; background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar .user { display: flex; align-items: center; gap: .75rem; font-size: .9rem; }
.menu-toggle { display: none; background: none; border: 1px solid var(--border); border-radius: 8px; padding: .35rem .6rem; font-size: 1.1rem; cursor: pointer; }
.content { padding: 1.5rem; max-width: 1400px; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: .75rem; margin-bottom: 1.25rem; }
.page-head .sub { color: var(--muted); margin-top: .2rem; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }

/* Panels & grids */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem 1.25rem; margin-bottom: 1.25rem; }
.card > h2:first-child { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.grid { display: grid; gap: 1.25rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: clamp(1.05rem, 1.6vw, 1.35rem); font-weight: 700; margin-top: .2rem; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat.alert .value { color: var(--danger); }
dl.details { display: grid; grid-template-columns: max-content 1fr; gap: .4rem 1.25rem; margin: 0; }
dl.details dt { color: var(--muted); }
dl.details dd { margin: 0; }

/* Tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -1.25rem; padding: 0 1.25rem; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .55rem .6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; white-space: nowrap; }
tbody tr:hover { background: #fafbfc; }
tfoot td, tr.total td { font-weight: 700; border-top: 2px solid var(--border); }
.empty { color: var(--muted); padding: 1rem 0; text-align: center; }

/* Badges */
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .75rem; font-weight: 600; background: #eef1f4; color: #384250; white-space: nowrap; }
.badge.green, .badge.paid, .badge.active, .badge.completed { background: var(--success-soft); color: #146c2e; }
.badge.red, .badge.overdue, .badge.urgent, .badge.terminated { background: var(--danger-soft); color: var(--danger); }
.badge.amber, .badge.partial, .badge.high, .badge.in_progress, .badge.upcoming { background: var(--warn-soft); color: var(--warn); }
.badge.blue, .badge.unpaid, .badge.open, .badge.trust { background: var(--info-soft); color: #1a4fb4; }
.badge.void, .badge.expired, .badge.cancelled { background: #eef1f4; color: var(--muted); }

/* Alerts */
.alert { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; border: 1px solid transparent; }
.alert ul { margin: .25rem 0 0; padding-left: 1.25rem; }
.alert.success { background: var(--success-soft); border-color: #b7e4c4; }
.alert.error { background: var(--danger-soft); border-color: #f5c2bd; }
.alert.warn { background: var(--warn-soft); border-color: #f2dc9b; }
.alert.info { background: var(--info-soft); border-color: #c5d6fb; }

/* Forms */
form.stacked { max-width: 760px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .9rem 1.25rem; }
.field { display: flex; flex-direction: column; gap: .3rem; }
.field.full { grid-column: 1 / -1; }
.field label { font-weight: 600; font-size: .88rem; }
.field .help { color: var(--muted); font-size: .8rem; }
.field.check { flex-direction: row; align-items: center; gap: .5rem; }
.form-section { border-top: 1px solid var(--border); padding-top: .9rem; margin-top: .4rem; }
.form-section:first-child { border-top: 0; padding-top: 0; margin-top: 0; }
.form-section h2 { margin: 0; font-size: 1.05rem; }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=month], input[type=number], select, textarea {
  width: 100%; padding: .55rem .65rem; border: 1px solid #c8d1db; border-radius: 8px; font: inherit; background: #fff; color: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
textarea { min-height: 80px; resize: vertical; }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--primary); }
.inline-form { display: flex; flex-wrap: wrap; gap: .5rem; align-items: flex-end; }
.inline-form .field { min-width: 140px; flex: 1; }
.inline-form .field.grow { flex: 3; }
.filters { display: flex; flex-wrap: wrap; gap: .6rem; align-items: flex-end; margin-bottom: 1rem; }
.filters .field { min-width: 150px; }
.form-actions { display: flex; gap: .6rem; margin-top: 1.25rem; flex-wrap: wrap; }
.lines-table input, .lines-table select { min-width: 110px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem; padding: .5rem .95rem; border-radius: 8px;
  border: 1px solid var(--primary); background: var(--primary); color: #fff; font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--primary-dark); text-decoration: none; }
.btn.secondary { background: #fff; color: var(--text); border-color: #c8d1db; }
.btn.secondary:hover { background: #f3f5f7; }
.btn.danger { background: var(--danger); border-color: var(--danger); }
.btn.small { padding: .3rem .6rem; font-size: .83rem; }
.btn.link { background: none; border: none; color: var(--primary); padding: 0; font-weight: 500; }
form.inline { display: inline; }
details.disclosure { margin-top: .5rem; }
details.disclosure summary { cursor: pointer; color: var(--primary); font-weight: 600; }
details.disclosure[open] summary { margin-bottom: .75rem; }

/* Auth pages */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth-card { width: 100%; max-width: 400px; }
.auth-card .brand { color: var(--text); justify-content: center; padding-bottom: 1.25rem; font-size: 1.3rem; }

/* Invoice / print documents */
.doc { background: #fff; max-width: 860px; margin: 1.5rem auto; padding: 2.5rem; border: 1px solid var(--border); border-radius: var(--radius); }
.doc-head { display: flex; justify-content: space-between; gap: 2rem; flex-wrap: wrap; margin-bottom: 2rem; }
.doc-title { font-size: 1.6rem; font-weight: 800; letter-spacing: .02em; }
.doc .parties { display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin-bottom: 1.5rem; }
.doc-toolbar { max-width: 860px; margin: 1rem auto 0; display: flex; gap: .5rem; justify-content: flex-end; padding: 0 1rem; }
.stamp { display: inline-block; border: 2px solid currentColor; padding: .15rem .6rem; border-radius: 6px; font-weight: 800; text-transform: uppercase; transform: rotate(-4deg); }
.stamp.paid { color: #146c2e; }
.stamp.void { color: var(--danger); }

/* Responsive */
@media (max-width: 1100px) {
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
  .sidebar { position: fixed; left: -260px; z-index: 50; transition: left .2s ease; box-shadow: 4px 0 20px rgba(0,0,0,.2); }
  body.nav-open .sidebar { left: 0; }
  body.nav-open::after { content: ""; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; }
  .menu-toggle { display: inline-block; }
  .content { padding: 1rem; }
  .topbar { padding: .6rem 1rem; }
  .grid-2, .grid-3, .form-grid { grid-template-columns: 1fr; }
  .doc { padding: 1.25rem; margin: .75rem; }
  .doc .parties { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
}
@media (max-width: 520px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat .value { font-size: 1.1rem; }
  .page-head h1 { font-size: 1.25rem; }
  .card { padding: 1rem; }
  .table-wrap { margin: 0 -1rem; padding: 0 1rem; }
  .topbar .user .name { display: none; }
}
@media print {
  body { background: #fff; }
  .sidebar, .topbar, .doc-toolbar, .no-print { display: none !important; }
  .doc { border: none; margin: 0; padding: 0; max-width: none; }
  .content { padding: 0; }
}

.auth-card .field + .field, form.stack .field + .field, .card > form > .field + .field { margin-top: .8rem; }
.btn.block { width: 100%; }
.mt { margin-top: 1rem; }
.right { text-align: right; }

[hidden] { display: none !important; }
