:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --brand: #849b7e;
  --brand-dark: #6b8268;
  --danger: #dc2626;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

body.auth-locked .app-menubar {
  display: none;
}

.app-menubar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.5rem;
  flex-wrap: wrap;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1.5rem;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.menubar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  text-decoration: none;
  color: inherit;
  min-width: 0;
}

.menubar-brand:hover .menubar-title {
  color: var(--brand-dark);
}

.menubar-logo {
  width: 44px;
  height: 44px;
  object-fit: contain;
  flex-shrink: 0;
}

.menubar-brand-text {
  min-width: 0;
}

.menubar-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.menubar-tagline {
  margin: 0.15rem 0 0;
  color: var(--text-muted);
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.menubar-nav {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.menubar-link {
  display: inline-block;
  padding: 0.5rem 0.875rem;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 500;
  font-size: 0.9rem;
  border-radius: var(--radius);
  white-space: nowrap;
}

.menubar-link:hover {
  color: var(--brand-dark);
  background: rgba(132, 155, 126, 0.1);
}

.menubar-link.active {
  color: #fff;
  background: var(--brand);
}

.menubar-logout {
  margin-left: 0.5rem;
}

.login-content {
  max-width: none;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 2rem 1.5rem;
}

.login-page {
  width: 100%;
  max-width: 400px;
}

.login-card {
  text-align: center;
  padding: 2rem;
}

.login-logo {
  width: 64px;
  height: 64px;
  object-fit: contain;
  margin: 0 auto 1rem;
  display: block;
}

.login-title {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
}

.login-subtitle {
  margin: 0.35rem 0 1.5rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.login-form {
  display: grid;
  gap: 1rem;
  text-align: left;
}

.login-submit {
  width: 100%;
  padding: 0.625rem 1rem;
  margin-top: 0.25rem;
}

.login-error {
  margin: 0;
  padding: 0.5rem 0.75rem;
  background: #fef2f2;
  color: var(--danger);
  border-radius: var(--radius);
  font-size: 0.875rem;
}

.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem;
  min-height: calc(100vh - 72px);
}

.dashboard-stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.dashboard-stat {
  margin-bottom: 0;
  padding: 1rem 1.25rem;
}

.dashboard-stat-label {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.dashboard-stat-value {
  margin: 0.35rem 0 0;
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--brand-dark);
}

.dashboard-stat-hint {
  margin: 0.25rem 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.dashboard-panels {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1rem;
}

.dashboard-panel {
  margin-bottom: 0;
}

.dashboard-panel-title {
  margin: 0 0 1rem;
  font-size: 1rem;
  font-weight: 600;
}

@media (max-width: 768px) {
  .app-menubar {
    flex-direction: column;
    align-items: stretch;
  }

  .menubar-nav {
    overflow-x: auto;
    flex-wrap: nowrap;
    padding-bottom: 0.25rem;
  }
}

/* Legacy sidebar classes kept for compatibility — menubar is primary nav */
.app-header,
.app-layout,
.sidebar,
.nav-link {
  display: none;
}

.main-content h2 {
  margin: 0;
  font-size: 1.25rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}

.entity-form,
.invoice-form {
  display: grid;
  gap: 1rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.form-group label {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-muted);
}

.form-control {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9375rem;
  font-family: inherit;
}

.form-control:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

textarea.form-control {
  min-height: 80px;
  resize: vertical;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}

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

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

.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}

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

.btn-whatsapp {
  background: #25d366;
  color: #fff;
}

.btn-whatsapp:hover {
  background: #1da851;
}

.btn-sm {
  padding: 0.25rem 0.625rem;
  font-size: 0.8125rem;
}

.btn-icon {
  padding: 0.5rem;
  min-width: 2.25rem;
  min-height: 2.25rem;
  line-height: 0;
}

.btn-sm.btn-icon {
  padding: 0.35rem;
  min-width: 1.875rem;
  min-height: 1.875rem;
}

.btn-icon.is-loading {
  opacity: 0.6;
  cursor: wait;
}

.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon svg {
  display: block;
}

.action-cell {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  white-space: nowrap;
}

.modal-close.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-muted);
  min-width: 2rem;
  min-height: 2rem;
  padding: 0.25rem;
}

.modal-close.btn-icon:hover {
  color: var(--text);
  background: var(--bg);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

.data-table th,
.data-table td {
  padding: 0.625rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.data-table th {
  font-weight: 600;
  color: var(--text-muted);
  background: var(--bg);
}

.data-table tr:last-child td {
  border-bottom: none;
}

.page-hint {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin: 0.25rem 0 0;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.page-header h2 {
  margin: 0;
}

.page-titles {
  flex: 1;
}

.modal-open {
  overflow: hidden;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 2000;
}

.modal-dialog {
  background: var(--surface);
  border-radius: var(--radius);
  width: 100%;
  max-width: min(520px, calc(100vw - 2rem));
  max-height: 90vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}

.modal-dialog.modal-lg {
  width: 80%;
  max-width: 80vw;
  max-height: 90vh;
}

.modal-dialog.modal-document {
  width: 860px;
  max-width: min(860px, calc(100vw - 2rem));
}

.modal-body-document {
  padding: 0;
  overflow-x: hidden;
  overflow-y: auto;
  background: #f0f0f0;
}

.doc-modal-toolbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  bottom: 0;
}

.doc-status-control {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-right: auto;
}

.doc-status-label {
  font-size: 0.875rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.doc-status-select {
  min-width: 8rem;
  padding: 0.35rem 0.5rem;
  font-size: 0.875rem;
}

/* Sales document — Hikari template */
.sales-document-hikari {
  background: #fff;
  color: #222;
  font-size: 0.8125rem;
  line-height: 1.5;
  padding: 1.25rem 1.5rem 1.5rem;
}

.doc-elec-notice {
  margin: 0 0 0.75rem;
  font-size: 0.7rem;
  color: #666;
  font-style: italic;
}

.doc-header-row {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
}

.doc-logo-main {
  height: 72px;
  width: auto;
  max-width: 96px;
  object-fit: contain;
  flex-shrink: 0;
}

.doc-company-block {
  flex: 1;
}

.doc-company-title {
  margin: 0 0 0.25rem;
  font-size: 1.1rem;
  font-weight: 700;
  color: #222;
}

.doc-company-block p {
  margin: 0;
  color: #444;
  font-size: 0.8125rem;
}

.doc-main-title {
  margin: 0 0 1rem;
  font-size: 1rem;
  font-weight: 700;
  color: #333;
  border-bottom: 2px solid #849b7e;
  padding-bottom: 0.35rem;
}

.doc-info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
  margin-bottom: 1rem;
}

.doc-party-heading {
  margin: 0 0 0.35rem;
  font-size: 0.85rem;
  font-weight: 700;
  color: #849b7e;
}

.doc-party-name {
  font-weight: 700;
  margin: 0 0 0.25rem;
}

.doc-party-box p {
  margin: 0 0 0.15rem;
  font-size: 0.8125rem;
}

.doc-meta-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8125rem;
}

.doc-meta-table td {
  border: 1px solid #ccc;
  padding: 0.3rem 0.5rem;
}

.doc-meta-label-cell {
  background: #f5f5f5;
  font-weight: 600;
  width: 42%;
  white-space: nowrap;
}

.doc-meta-value-cell {
  width: 58%;
}

.doc-gst-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.7rem;
  margin-bottom: 1rem;
}

.doc-gst-table th,
.doc-gst-table td {
  border: 1px solid #bbb;
  padding: 0.3rem 0.35rem;
}

.doc-gst-table th {
  background: #e8ede7;
  font-weight: 700;
  text-align: center;
}

.doc-td-center { text-align: center; }
.doc-td-right { text-align: right; }
.doc-td-discount {
  text-align: right;
  color: #b45309;
  white-space: nowrap;
}
.doc-empty-row { text-align: center; color: #888; }

.doc-item-cell {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.doc-item-name {
  font-weight: 600;
}

.doc-item-period {
  font-size: 0.75rem;
  color: #666;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.doc-line-notes-block {
  margin: 1rem 0;
  padding: 0.75rem 1rem;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  background: #fafafa;
}

.doc-line-notes-block h4 {
  margin: 0 0 0.5rem;
  font-size: 0.875rem;
  color: var(--brand);
}

.doc-line-notes-block p {
  margin: 0 0 0.35rem;
  font-size: 0.8125rem;
  color: #444;
  line-height: 1.45;
}

.doc-summary-section {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 1rem;
  margin-bottom: 0.75rem;
  align-items: start;
}

.doc-bank-box h4 {
  margin: 0 0 0.35rem;
  font-size: 0.85rem;
  color: #849b7e;
}

.doc-bank-box p {
  margin: 0 0 0.15rem;
  font-size: 0.75rem;
}

.doc-summary-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8125rem;
}

.doc-summary-table td {
  border: 1px solid #ccc;
  padding: 0.35rem 0.5rem;
}

.doc-summary-total-row td {
  background: #849b7e;
  color: #fff;
  font-weight: 700;
}

.doc-summary-yearly-row td {
  background: #6b8268;
  color: #fff;
  font-weight: 700;
}

.doc-td-yearly {
  font-weight: 600;
}

.doc-amount-words {
  margin: 0 0 1rem;
  font-size: 0.8125rem;
  font-weight: 600;
  font-style: italic;
}

.doc-terms-block {
  margin-bottom: 1.25rem;
}

.doc-terms-block h4 {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
  color: #849b7e;
}

.doc-terms-block ol {
  margin: 0;
  padding-left: 1.25rem;
  font-size: 0.75rem;
  color: #444;
}

.doc-terms-block li {
  margin-bottom: 0.35rem;
}

.doc-signatures {
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
  gap: 2rem;
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid #ddd;
}

.doc-signature-block {
  text-align: center;
  flex: 1;
  max-width: 180px;
}

.doc-signature-img {
  max-width: 140px;
  max-height: 60px;
  object-fit: contain;
  display: block;
  margin: 0 auto 0.25rem;
}

.doc-signature-name {
  margin: 0;
  font-weight: 700;
  font-size: 0.8125rem;
  border-top: 1px solid #333;
  padding-top: 0.25rem;
}

.doc-signature-title {
  margin: 0.15rem 0 0;
  font-size: 0.75rem;
  color: #555;
}

.doc-footer {
  text-align: center;
  font-size: 0.7rem;
  color: #666;
  padding: 1rem 0 0;
  margin: 0;
}

.doc-table-wrap {
  overflow-x: auto;
}

.doc-gst-table thead {
  display: table-header-group;
}

.doc-print-footer {
  page-break-inside: avoid;
}

.print-root {
  display: none;
}

@media (max-width: 768px) {
  .doc-info-grid,
  .doc-summary-section {
    grid-template-columns: 1fr;
  }

  .doc-signatures {
    flex-direction: column;
    align-items: center;
  }
}

@media print {
  @page {
    size: A4 portrait;
    margin: 8mm;
  }

  html,
  body {
    margin: 0;
    padding: 0;
    background: #fff !important;
    overflow: visible !important;
  }

  body.printing-document > *:not(#print-root) {
    display: none !important;
  }

  body.printing-document #print-root {
    display: block !important;
    width: var(--print-doc-width, 860px);
    max-width: none;
    margin: 0 auto;
    background: #fff;
    zoom: var(--print-zoom, 1);
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  body.printing-document #print-root,
  body.printing-document #print-root * {
    visibility: visible !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  body.printing-document #print-root .sales-document-hikari {
    width: 100%;
    box-sizing: border-box;
    box-shadow: none;
    padding: 1.25rem 1.5rem 1.5rem;
    font-size: 0.8125rem;
    line-height: 1.5;
  }

  body.printing-document #print-root .doc-table-wrap {
    overflow: visible;
  }

  body.printing-document #print-root .doc-gst-table {
    font-size: 0.7rem;
    width: 100%;
  }

  body.printing-document #print-root .doc-info-grid {
    grid-template-columns: 1fr 1fr;
  }

  body.printing-document #print-root .doc-summary-section {
    grid-template-columns: 1fr 280px;
  }

  body.printing-document #print-root .doc-print-footer,
  body.printing-document #print-root .doc-line-notes-block {
    page-break-inside: avoid;
  }

  body.printing-document #print-root .doc-gst-table tr {
    page-break-inside: avoid;
  }

  @supports not (zoom: 1) {
    body.printing-document #print-root {
      transform: scale(var(--print-zoom, 1));
      transform-origin: top center;
    }
  }
}

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

.modal-header h3 {
  margin: 0;
  font-size: 1.125rem;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0 0.25rem;
}

.modal-close:hover {
  color: var(--text);
}

.modal-body {
  padding: 1.25rem;
  overflow-x: hidden;
  overflow-y: auto;
  min-width: 0;
}

.modal-body .entity-form,
.modal-body .invoice-form {
  min-width: 0;
}

.modal-body .form-control,
.modal-body select.form-control {
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

.modal-body .form-row {
  grid-template-columns: 1fr;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1rem;
}

.empty-state,
.loading {
  color: var(--text-muted);
  font-size: 0.9375rem;
}

.invoice-lines {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.invoice-lines-heading {
  display: block;
  font-size: 0.875rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.invoice-lines-section {
  margin-bottom: 0.5rem;
}

.invoice-line {
  display: grid;
  grid-template-columns: 1.1fr 1.8fr 0.55fr 0.9fr 0.7fr 0.85fr 1fr auto;
  gap: 0.5rem;
  align-items: end;
}

.invoice-line-field {
  min-width: 0;
}

.invoice-line-label {
  display: none;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.invoice-line-header {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding-bottom: 0.25rem;
}

.modal-body .invoice-line-header {
  display: none;
}

.modal-body .invoice-lines {
  gap: 0.75rem;
}

.modal-body .invoice-line {
  grid-template-columns: 1fr;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  align-items: stretch;
}

.modal-body .invoice-line-label {
  display: block;
}

.modal-body .invoice-line .btn-danger {
  justify-self: end;
  width: auto;
  margin-top: 0.25rem;
}

.modal-body .invoice-line-actions {
  display: flex;
  justify-content: flex-end;
}

@media (min-width: 1100px) {
  .modal-lg .invoice-line {
    grid-template-columns: 1.1fr 1.8fr 0.55fr 0.9fr 0.7fr 0.85fr 1fr auto;
    padding: 0;
    border: none;
    border-radius: 0;
    background: transparent;
    align-items: end;
  }

  .modal-lg .invoice-line-header {
    display: grid;
  }

  .modal-lg .invoice-line-label {
    display: none;
  }

  .modal-lg .invoice-line-actions {
    justify-content: center;
    align-items: end;
  }

  .modal-lg .invoice-line .btn-danger {
    margin-top: 0;
  }
}

.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius);
  color: #fff;
  font-size: 0.875rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  animation: slideIn 0.2s ease;
}

.toast-success { background: #16a34a; }
.toast-error { background: var(--danger); }
.toast-info { background: var(--primary); }

@keyframes slideIn {
  from { transform: translateY(1rem); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@media (max-width: 768px) {
  .app-layout {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    flex-direction: row;
    overflow-x: auto;
    padding: 0.5rem;
    border-bottom: 1px solid var(--border);
  }

  .nav-link {
    border-left: none;
    border-bottom: 3px solid transparent;
    white-space: nowrap;
  }

  .nav-link.active {
    border-left: none;
    border-bottom-color: var(--primary);
  }

  .form-row,
  .invoice-line {
    grid-template-columns: 1fr;
  }
}
