:root {
  --bg: #f5f7f8;
  --panel: #ffffff;
  --ink: #1c2526;
  --muted: #647172;
  --line: #d9e0e1;
  --primary: #0f766e;
  --primary-dark: #0b5f59;
  --accent: #a44918;
  --warn: #b42318;
  --soft: #eef6f4;
  --shadow: 0 14px 35px rgba(28, 37, 38, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Inter, Segoe UI, Roboto, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

button, input, select, textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 250px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: #142223;
  color: #eef7f5;
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 26px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 6px;
}

.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  background: #e7b75f;
  color: #172223;
  border-radius: 8px;
  font-weight: 800;
  font-size: 22px;
}

.brand h1, .brand p {
  margin: 0;
}

.brand h1 {
  font-size: 20px;
  line-height: 1.15;
}

.brand p {
  color: #a8b7b6;
  font-size: 13px;
  margin-top: 2px;
}

.nav-tabs {
  display: grid;
  gap: 6px;
}

.nav-tab {
  min-height: 42px;
  border: 0;
  background: transparent;
  color: #c8d4d2;
  display: grid;
  grid-template-columns: 20px 1fr;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  border-radius: 8px;
  text-align: left;
}

.nav-tab:hover, .nav-tab.active {
  background: #223637;
  color: #ffffff;
}

.nav-tab svg, button svg, .file-btn svg {
  width: 18px;
  height: 18px;
  stroke-width: 2;
}

button > *,
.file-btn > * {
  pointer-events: none;
}

.workspace {
  min-width: 0;
  padding: 24px;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 18px;
  margin-bottom: 24px;
}

.topbar h2, .section-head h3, .panel h3 {
  margin: 0;
}

.topbar h2 {
  font-size: 28px;
  letter-spacing: 0;
}

.eyebrow {
  color: var(--muted);
  margin: 0 0 5px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0;
}

.topbar-actions, .dialog-actions, .line-head, .panel-head, .section-head, .filters {
  display: flex;
  align-items: center;
  gap: 10px;
}

.section-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.topbar-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.topbar-profile {
  min-width: 240px;
  max-width: 360px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.topbar-profile select {
  min-height: 38px;
}

.primary-btn, .secondary-btn, .ghost-btn, .icon-btn, .segmented button {
  border: 1px solid transparent;
  border-radius: 8px;
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
  white-space: nowrap;
}

.primary-btn {
  background: var(--primary);
  color: #fff;
}

.primary-btn:hover { background: var(--primary-dark); }

.secondary-btn {
  background: #fff;
  border-color: var(--line);
  color: var(--ink);
}

.ghost-btn {
  background: transparent;
  color: var(--primary);
}

.icon-btn {
  width: 40px;
  padding: 0;
  background: #fff;
  border-color: var(--line);
  color: var(--ink);
}

.file-btn {
  position: relative;
  overflow: hidden;
}

.file-btn input,
.upload-btn input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.upload-btn {
  position: relative;
  overflow: hidden;
}

.view { display: none; }
.view.active { display: block; }

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.metric, .panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.metric {
  min-height: 112px;
  padding: 18px;
  display: grid;
  align-content: space-between;
}

.metric span {
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  font-size: 26px;
  letter-spacing: 0;
}

.split-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(320px, 0.8fr);
  gap: 18px;
}

.panel {
  overflow: hidden;
}

.panel-head, .section-head {
  justify-content: space-between;
}

.panel-head {
  padding: 16px 16px 0;
}

.section-head {
  margin-bottom: 14px;
}

.section-head p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 860px;
}

th, td {
  padding: 13px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
  font-size: 14px;
}

th {
  color: var(--muted);
  background: #fbfcfc;
  font-weight: 650;
}

tr:last-child td { border-bottom: 0; }

.num { text-align: right; font-variant-numeric: tabular-nums; }

.row-actions {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
}

.mini-btn {
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  display: inline-grid;
  place-items: center;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  background: var(--soft);
  color: var(--primary-dark);
  font-size: 12px;
  font-weight: 700;
}

.badge.warn {
  background: #fff0ed;
  color: var(--warn);
}

dialog {
  border: 0;
  border-radius: 8px;
  padding: 0;
  box-shadow: 0 28px 70px rgba(0,0,0,.25);
  width: min(720px, calc(100vw - 28px));
  max-height: calc(100dvh - 28px);
  overflow: hidden;
}

dialog::backdrop {
  background: rgba(14, 24, 25, 0.55);
}

.wide-dialog {
  width: min(1080px, calc(100vw - 28px));
}

.invoice-dialog {
  width: min(900px, calc(100vw - 28px));
}

dialog form, .invoice-dialog {
  background: #fff;
}

dialog form {
  max-height: calc(100dvh - 28px);
  overflow: auto;
}

#entryForm {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  overflow: hidden;
}

.dialog-scroll {
  min-height: 0;
  overflow: auto;
}

.dialog-head {
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dialog-head h3 {
  margin: 0;
}

.form-grid, .settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  padding: 18px 20px;
}

.settings-grid {
  padding: 18px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

input, select, textarea {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--ink);
  background: #fff;
}

textarea { resize: vertical; }

.span-2 { grid-column: span 2; }

.line-editor {
  padding: 0 20px 18px;
}

.line-head {
  justify-content: space-between;
  margin-bottom: 10px;
}

.line-head h4 {
  margin: 0;
}

.line-row {
  display: grid;
  grid-template-columns: minmax(180px, 1.2fr) 86px 110px 100px 110px 36px;
  gap: 8px;
  align-items: end;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 8px;
  background: #fbfcfc;
}

.totals-box {
  border-top: 1px solid var(--line);
  background: #fbfcfc;
  padding: 14px 20px;
  display: flex;
  justify-content: flex-end;
  gap: 28px;
}

.totals-box div {
  display: grid;
  gap: 3px;
  text-align: right;
}

.totals-box span {
  color: var(--muted);
  font-size: 12px;
}

.totals-box strong {
  font-size: 18px;
}

.dialog-actions {
  margin: 0;
  padding: 16px 20px 20px;
  justify-content: flex-end;
  background: #fff;
  border-top: 1px solid var(--line);
}

.segmented {
  display: inline-flex;
  background: #e9eeee;
  padding: 4px;
  border-radius: 8px;
  gap: 3px;
}

.segmented button {
  min-height: 32px;
  background: transparent;
  color: var(--muted);
  padding: 0 12px;
}

.segmented button.active {
  background: #fff;
  color: var(--ink);
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}

.filters {
  padding: 14px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.filters label {
  width: 180px;
}

.report-panel {
  padding: 18px;
}

.report-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.report-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.report-card span {
  color: var(--muted);
  font-size: 13px;
}

.report-card strong {
  display: block;
  margin-top: 8px;
  font-size: 22px;
}

#invoicePrintArea {
  padding: 24px;
}

.invoice-sheet {
  border: 1px solid var(--line);
  padding: 22px;
}

.invoice-top {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  border-bottom: 2px solid var(--ink);
  padding-bottom: 14px;
  margin-bottom: 16px;
}

.invoice-top h2, .invoice-top p {
  margin: 0;
}

.invoice-meta {
  text-align: right;
}

.invoice-party {
  margin-bottom: 16px;
}

.invoice-total {
  width: min(340px, 100%);
  margin-left: auto;
}

.invoice-total div {
  display: flex;
  justify-content: space-between;
  padding: 7px 0;
  border-bottom: 1px solid var(--line);
}

.invoice-total strong {
  font-size: 19px;
}

#toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  min-width: 220px;
  max-width: 360px;
  background: #182627;
  color: #fff;
  padding: 12px 14px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  transform: translateY(18px);
  opacity: 0;
  pointer-events: none;
  transition: .18s ease;
}

#toast.show {
  transform: translateY(0);
  opacity: 1;
}

.empty-row {
  color: var(--muted);
  text-align: center;
  padding: 22px;
}

@media (max-width: 980px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky;
    top: 0;
    z-index: 3;
    padding: 12px;
    padding-top: calc(12px + env(safe-area-inset-top));
    gap: 12px;
  }
  .brand { display: none; }
  .nav-tabs {
    display: flex;
    overflow-x: auto;
    scrollbar-width: thin;
    -webkit-overflow-scrolling: touch;
  }
  .nav-tab {
    grid-template-columns: 18px auto;
    min-width: max-content;
  }
  .metric-grid, .split-layout, .report-grid {
    grid-template-columns: 1fr 1fr;
  }
  .line-row {
    grid-template-columns: 1fr 80px 100px;
  }
  .line-row label:nth-child(4),
  .line-row label:nth-child(5),
  .line-row button {
    grid-column: span 1;
  }
}

@media (max-width: 820px) {
  .workspace {
    padding: 14px;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }
  .topbar, .section-head {
    align-items: stretch;
    flex-direction: column;
  }
  .section-actions {
    justify-content: stretch;
  }
  .topbar-actions {
    justify-content: flex-start;
  }
  .topbar-profile {
    min-width: 100%;
    max-width: 100%;
  }
  .topbar-actions .primary-btn,
  .section-head .primary-btn,
  .section-actions .secondary-btn,
  .dialog-actions .primary-btn,
  .dialog-actions .secondary-btn {
    width: 100%;
  }
  .metric-grid, .split-layout, .report-grid, .form-grid, .settings-grid {
    grid-template-columns: 1fr;
  }
  .metric {
    min-height: 92px;
  }
  table {
    min-width: 680px;
  }
  .span-2 { grid-column: span 1; }
  .line-row { grid-template-columns: 1fr; }
  .totals-box {
    display: grid;
    grid-template-columns: 1fr;
    justify-content: stretch;
    gap: 12px;
  }
  .totals-box div { text-align: left; }
  .dialog-head,
  .form-grid,
  .line-editor,
  .totals-box,
  .dialog-actions,
  #invoicePrintArea {
    padding-left: 14px;
    padding-right: 14px;
  }
  .dialog-actions {
    display: grid;
    grid-template-columns: 1fr;
  }
  .filters label,
  .filters .secondary-btn {
    width: 100%;
  }
  .invoice-top {
    display: grid;
  }
  .invoice-meta {
    text-align: left;
  }
}

@media print {
  body * { visibility: hidden; }
  #invoicePrintArea, #invoicePrintArea *, #reportOutput, #reportOutput * {
    visibility: visible;
  }
  #invoicePrintArea, #reportOutput {
    position: absolute;
    inset: 0;
    padding: 0;
  }
  .dialog-actions { display: none; }
}
