:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #1f2933;
  --muted: #697586;
  --line: #d9dee7;
  --accent: #0f766e;
  --accent-dark: #115e59;
  --soft: #edf7f6;
}

* {
  box-sizing: border-box;
}

html {
  height: 100%;
}

body {
  margin: 0;
  height: 100%;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* SSO banner styling */
.sso-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 1000;
}
.sso-avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; background: #ddd; }
.sso-name { font-weight: 600; font-size: 14px; }

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

button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  min-height: 36px;
  padding: 0 12px;
}

button:hover {
  border-color: var(--accent);
  color: var(--accent-dark);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--accent-dark);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.2;
}

h2 {
  font-size: 16px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.actions button:nth-child(2),
.actions button:nth-child(3) {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(360px, 520px) minmax(420px, 1fr);
  gap: 18px;
  padding: 18px;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

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

.controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
}

.status-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  min-height: 20px;
  color: var(--muted);
  font-size: 13px;
}

.grid {
  display: grid;
  gap: 12px;
}

.grid.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

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

textarea {
  resize: vertical;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.field-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.btn-icon {
  min-height: 24px;
  min-width: 24px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
  border-radius: 50%;
  flex-shrink: 0;
}

.add-customer-form {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.add-customer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

#newCustomerStatus {
  font-size: 12px;
  color: var(--muted);
}

.customer-search-wrap {
  position: relative;
}

.customer-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, .12);
  z-index: 300;
  max-height: 300px;
  overflow-y: auto;
}

.customer-dropdown-row {
  padding: 9px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--line);
}

.customer-dropdown-row:last-child { border-bottom: none; }

.customer-dropdown-row:hover,
.customer-dropdown-row.active {
  background: var(--soft);
}

.cdr-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
}

.cdr-meta {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cdr-empty {
  padding: 12px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.customer-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  padding: 12px;
  min-height: 72px;
  color: var(--muted);
}

.customer-card strong {
  display: block;
  color: var(--ink);
  font-size: 15px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.add-badges { display: flex; gap: 8px; flex-wrap: wrap; }

.add-badge {
  font-size: 12px;
  font-weight: 700;
  padding: 3px 12px;
  min-height: 26px;
  border-radius: 13px;
  color: var(--accent);
  border-color: var(--accent);
  background: var(--soft);
}
.add-badge:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

.btn-remove-section {
  padding: 0;
  min-height: 22px;
  min-width: 22px;
  border-radius: 50%;
  font-size: 14px;
  color: var(--muted);
}
.btn-remove-section:hover { color: #dc2626; border-color: #dc2626; }

#serviceOrderSection, #invoicePartsSection {
  display: flex;
  flex-direction: column;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: var(--soft);
}
#serviceOrderSection[hidden], #invoicePartsSection[hidden] { display: none; }

.inv-part-line {
  display: grid;
  grid-template-columns: 1fr 64px 92px 36px;
  gap: 6px;
  align-items: center;
  margin-bottom: 6px;
}
.ipl-remove { padding: 0; min-width: 36px; font-size: 16px; }

.add-part-row { font-size: 12px; min-height: 28px; padding: 0 10px; align-self: flex-start; }

/* Invoice preview service order / parts boxes */
.so-preview-box {
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  padding: 16px 18px;
}
.so-preview-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
}
.so-preview-label { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: #667085; }
.so-preview-wo { font-weight: 700; font-size: 15px; }
.so-meta { font-size: 13px; color: #4b5563; margin-bottom: 3px; }
.so-preview-table { width: 100%; border-collapse: collapse; margin: 8px 0; font-size: 13px; }
.so-preview-table th {
  color: #667085; font-size: 11px; text-transform: uppercase;
  border-bottom: 1px solid #e5e7eb; padding: 5px 0; text-align: left;
}
.so-preview-table th:not(:first-child),
.so-preview-table td:not(:first-child) { text-align: right; }
.so-preview-table td { padding: 5px 0; border-bottom: 1px solid #f3f4f6; }
.so-labour-line { font-size: 13px; color: #374151; padding: 6px 0; border-top: 1px solid #e5e7eb; margin-top: 4px; }
.so-section-total { text-align: right; font-weight: 700; font-size: 13px; padding-top: 6px; }

.line-head,
.line-item {
  display: grid;
  grid-template-columns: minmax(140px, 1fr) 72px 92px 72px 36px;
  gap: 8px;
  align-items: center;
}

.line-head {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

#lineItems {
  display: grid;
  gap: 8px;
}

.remove-line {
  padding: 0;
  min-width: 36px;
}

.preview-wrap {
  padding: 22px;
  overflow: auto;
}

.invoice {
  max-width: 860px;
  min-height: 980px;
  margin: 0 auto;
  background: #fff;
  color: #111827;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 44px;
}

.invoice-header,
.invoice-meta,
.invoice-total-row,
.invoice-party-grid {
  display: flex;
  justify-content: space-between;
  gap: 28px;
}

.invoice-title {
  font-size: 34px;
  font-weight: 800;
}

.invoice-subtitle,
.invoice-muted {
  color: #667085;
}

.invoice-box {
  margin-top: 28px;
}

.invoice-party-grid > div {
  flex: 1;
}

.invoice table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 28px;
}

.invoice th,
.invoice td {
  border-bottom: 1px solid #e5e7eb;
  padding: 11px 0;
  text-align: left;
  vertical-align: top;
}

.invoice th:not(:first-child),
.invoice td:not(:first-child) {
  text-align: right;
}

.totals {
  margin: 24px 0 0 auto;
  width: min(340px, 100%);
}

.invoice-total-row {
  padding: 7px 0;
  border-bottom: 1px solid #e5e7eb;
}

.invoice-total-row.grand {
  font-size: 20px;
  font-weight: 800;
  border-bottom: 0;
}

.notes {
  margin-top: 34px;
  white-space: pre-wrap;
}

.invoice-number-block {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  text-align: right;
}

.invoice-qr-box {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  padding: 10px 12px;
  background: #fff;
}

.invoice-qr {
  display: block;
  width: 88px;
  height: 88px;
}

.invoice-qr-label {
  font-size: 11px;
  font-weight: 700;
  color: #111827;
  letter-spacing: 0.04em;
  text-align: center;
}

@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 620px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .grid.two,
  .line-head,
  .line-item {
    grid-template-columns: 1fr;
  }

  .line-head {
    display: none;
  }
}

@media print {
  body {
    background: #fff;
  }

  .topbar,
  .controls,
  .app-sidebar,
  .sso-banner,
  .app-topnav {
    display: none;
  }

  .app-layout,
  .app-main-area,
  .workspace,
  .preview-wrap {
    display: block;
    padding: 0;
  }

  .panel,
  .invoice {
    border: 0;
    border-radius: 0;
  }

  .invoice {
    max-width: none;
    min-height: auto;
    padding: 24px;
  }
}

/* ===== App layout ===== */

.app-layout {
  display: flex;
  min-height: 100vh;
}

.app-sidebar {
  width: 220px;
  flex-shrink: 0;
  background: #fff;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  align-self: flex-start;
}

.app-main-area {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.sidebar-logo {
  padding: 18px 16px 14px;
  border-bottom: 1px solid var(--line);
}

.sidebar-logo-eyebrow {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--accent-dark);
  letter-spacing: 0.06em;
  margin-bottom: 2px;
}

.sidebar-logo-name {
  font-size: 15px;
  font-weight: 700;
  color: var(--ink);
}

.sidebar-nav {
  flex: 1;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar-section-label {
  padding: 12px 8px 4px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: 0.08em;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  text-decoration: none;
  color: var(--ink);
  font-size: 13px;
  font-weight: 500;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  min-height: auto;
}

.sidebar-link:hover {
  background: var(--soft);
  color: var(--accent-dark);
  border-color: transparent;
}

.sidebar-link.active {
  background: var(--soft);
  color: var(--accent-dark);
  font-weight: 700;
}

.sidebar-bottom {
  padding: 8px;
  border-top: 1px solid var(--line);
  margin-top: auto;
}

/* ===== Invoice list page ===== */

.inv-list-page {
  padding: 24px;
}

.inv-list-page-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.inv-list-page-header h2 {
  font-size: 20px;
  font-weight: 700;
  margin: 0;
}

.inv-list-status {
  color: var(--muted);
  font-size: 13px;
}

.inv-status-tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 16px;
}

.inv-tab {
  padding: 8px 14px;
  border-radius: 6px 6px 0 0;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  min-height: auto;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.inv-tab:hover {
  color: var(--ink);
  background: var(--soft);
  border-color: transparent;
}

.inv-tab.active {
  color: var(--accent-dark);
  font-weight: 700;
  border-bottom-color: var(--accent);
  background: none;
}

.inv-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 860px;
}

.inv-list-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 16px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  text-decoration: none;
  color: var(--ink);
  transition: border-color 0.1s;
}

.inv-list-row:hover {
  border-color: var(--accent);
}

.inv-list-row-main {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.inv-list-number {
  font-weight: 700;
  font-size: 13px;
  white-space: nowrap;
}

.inv-list-customer {
  color: var(--muted);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inv-list-row-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.inv-list-amount {
  font-weight: 700;
  font-size: 13px;
}

.inv-list-date {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.inv-status-badge {
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
}

.inv-status-draft { background: #6b7280; }
.inv-status-sent  { background: #0f766e; }
.inv-status-paid  { background: #059669; }
.inv-status-void  { background: #9ca3af; color: #374151; }

.inv-list-empty {
  padding: 48px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

/* ===== View CRM button in customer card ===== */

.btn-view-crm {
  margin-top: 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent-dark);
  border-color: var(--accent);
  background: var(--soft);
  min-height: 28px;
  padding: 0 10px;
}

.btn-view-crm:hover {
  background: var(--accent);
  color: #fff;
}

/* ===== Invoice contact panel (right overlay) ===== */

#invContactOverlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .35);
  z-index: 900;
}

#invContactOverlay.open { display: block; }

#invContactPanel {
  position: fixed;
  top: 0;
  right: -420px;
  width: 420px;
  max-width: 100vw;
  height: 100vh;
  background: #fff;
  border-left: 1px solid var(--line);
  z-index: 901;
  display: flex;
  flex-direction: column;
  transition: right .25s ease;
  overflow: hidden;
}

#invContactPanel.open { right: 0; }

#invContactPanelHeader {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: #fff;
  flex-shrink: 0;
}

#invContactPanelClose {
  border: none;
  background: none;
  font-size: 16px;
  cursor: pointer;
  color: var(--muted);
  min-height: auto;
  padding: 4px 8px;
  border-radius: 4px;
}

#invContactPanelClose:hover {
  color: var(--ink);
  background: var(--soft);
  border-color: transparent;
}

#invContactPanelTitle {
  font-weight: 700;
  font-size: 15px;
  flex: 1;
}

.inv-cdp-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.inv-cdp-section {
  margin-bottom: 20px;
}

.inv-cdp-section-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: 0.06em;
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
}

.inv-cdp-name {
  font-size: 17px;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: 4px;
}

.inv-cdp-sub {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}

.inv-cdp-meta-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}

.inv-cdp-meta-link {
  color: var(--accent);
  text-decoration: none;
}

.inv-cdp-inv-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 12px;
  background: var(--soft);
  border: 1px solid var(--line);
  border-radius: 6px;
  text-decoration: none;
  color: var(--ink);
  margin-bottom: 6px;
  transition: border-color .1s;
}

.inv-cdp-inv-row:hover { border-color: var(--accent); }

.inv-cdp-inv-num {
  font-size: 13px;
  font-weight: 700;
}

.inv-cdp-inv-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.inv-cdp-inv-amount {
  font-weight: 600;
  color: var(--ink);
  font-size: 12px;
}

.inv-cdp-inv-date {
  color: var(--muted);
  font-size: 12px;
}

.inv-cdp-empty {
  color: var(--muted);
  font-size: 13px;
  padding: 8px 0;
}

.inv-cdp-loading {
  color: var(--muted);
  font-size: 13px;
  padding: 16px;
}

/* ── App-wide top nav bar (shared across all pages) ──────────────────────── */
.app-topnav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.app-topnav-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-topnav-logo {
  width: 28px;
  height: 28px;
  object-fit: contain;
  border-radius: 6px;
  flex-shrink: 0;
}

.app-topnav-tenant {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent-dark);
}

.app-topnav-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ── Topbar module icons + user badge ─────────────────────────────────────── */
.topbar-module-icons {
  display: flex;
  align-items: center;
  gap: 2px;
}

.topbar-module-icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  transition: background 0.15s, color 0.15s;
}

.topbar-module-icon-btn:hover {
  background: var(--bg);
  color: var(--accent-dark);
}

.topbar-module-icon-btn svg {
  stroke: currentColor;
}

.topbar-user-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  cursor: default;
  flex-shrink: 0;
}

.inv-list-header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}
