:root {
  --primary: #0F766E;
  --primary-light: #14B8A6;
  --primary-dark: #0D5C56;
  --secondary: #475569;
  --success: #059669;
  --warning: #D97706;
  --error: #DC2626;
  --info: #0284C7;
  --bg: #F1F5F9;
  --paper: #FFFFFF;
  --text: #0F172A;
  --text-muted: #64748B;
  --border: rgba(15, 23, 42, 0.08);
  --muted: #F8FAFC;
  --sidebar-w: 268px;
  --radius: 12px;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.04);
  font-family: "Plus Jakarta Sans", "IBM Plex Sans", system-ui, sans-serif;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

a {
  color: inherit;
  text-decoration: none;
}

img {
  max-width: 100%;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 0.65rem 1.1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn-primary {
  color: #fff;
  background: linear-gradient(180deg, var(--primary-light), var(--primary));
}

.btn-primary:hover {
  background: linear-gradient(180deg, var(--primary), var(--primary-dark));
}

.btn-outline {
  background: #fff;
  border-color: var(--border);
  color: var(--text);
}

.btn-outline:hover {
  border-color: var(--primary-light);
  color: var(--primary-dark);
}

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

.btn-sm {
  padding: 0.4rem 0.75rem;
  font-size: 0.85rem;
}

.btn-ghost {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 0.45rem;
  border-radius: 8px;
  cursor: pointer;
}

.btn-ghost:hover {
  background: rgba(15, 118, 110, 0.08);
  color: var(--primary);
}

.card {
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.card-body {
  padding: 1.25rem 1.35rem;
}

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

.page-header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.page-header p {
  margin: 0.25rem 0 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.grid {
  display: grid;
  gap: 1rem;
}

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

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

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

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

@media (max-width: 1100px) {
  .grid-6 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .grid-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 700px) {
  .grid-6,
  .grid-4,
  .grid-3,
  .grid-2 {
    grid-template-columns: 1fr;
  }
}

.stat-card .label {
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 500;
}

.stat-card .value {
  font-size: 1.35rem;
  font-weight: 700;
  margin: 0.55rem 0 0.45rem;
}

.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: max-content;
  max-width: none;
  border-radius: 999px;
  padding: 0.2rem 0.7rem;
  font-size: 0.75rem;
  font-weight: 650;
  line-height: 1.35;
  border: 1px solid var(--border);
  background: var(--muted);
  white-space: nowrap;
  box-sizing: border-box;
  font-variant-numeric: tabular-nums;
}

.chip-amount {
  border-radius: 8px;
  padding: 0.28rem 0.75rem;
  font-size: 0.82rem;
  letter-spacing: 0.01em;
}

.chip-success {
  color: var(--success);
  background: #ecfdf5;
  border-color: #a7f3d0;
}

.chip-warning {
  color: var(--warning);
  background: #fffbeb;
  border-color: #fde68a;
}

.chip-primary {
  color: var(--primary-dark);
  background: rgba(15, 118, 110, 0.08);
  border-color: rgba(15, 118, 110, 0.2);
}

.chip-error {
  color: var(--error);
  background: #fef2f2;
  border-color: #fecaca;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
}

.form-grid .full {
  grid-column: 1 / -1;
}

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

.field label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.35rem;
  color: var(--secondary);
}

.field input,
.field select,
.field textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.7rem 0.85rem;
  font: inherit;
  background: #fff;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: 2px solid rgba(15, 118, 110, 0.25);
  border-color: var(--primary);
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
}

table.data {
  width: 100%;
  border-collapse: collapse;
  font-family: "IBM Plex Sans", "Plus Jakarta Sans", sans-serif;
  font-size: 0.9rem;
  min-width: 640px;
}

table.data th,
table.data td {
  padding: 0.75rem 0.9rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
  vertical-align: middle;
}

table.data td.wrap,
table.data th.wrap {
  white-space: normal;
  word-break: break-word;
  min-width: 120px;
}

table.data th {
  background: var(--muted);
  color: var(--text-muted);
  font-weight: 650;
  font-size: 0.8rem;
}

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

table.data tr:hover td {
  background: rgba(15, 118, 110, 0.03);
}

.alert {
  border-radius: 10px;
  padding: 0.85rem 1rem;
  margin-bottom: 1rem;
  font-size: 0.92rem;
}

.alert-success {
  background: #ecfdf5;
  color: #065f46;
  border: 1px solid #a7f3d0;
}

.alert-error {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

.empty {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--text-muted);
}

/* Auth */
.auth-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1.15fr 1fr;
}

.auth-hero {
  background: linear-gradient(145deg, #0B3D3A 0%, #0F766E 42%, #134E4A 100%);
  color: #fff;
  padding: 3rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
  overflow: hidden;
}

.auth-hero::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at 20% 20%, rgba(94, 234, 212, 0.22) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 80%, rgba(2, 132, 199, 0.18) 0%, transparent 45%);
  pointer-events: none;
}

.auth-hero > * {
  position: relative;
}

.auth-panel {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background: var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem;
}

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 800;
}

.company-cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1.25rem;
  max-width: 760px;
  margin: 0 auto;
}

.company-card {
  text-align: left;
  padding: 1.5rem;
  transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
  cursor: pointer;
  border: 1px solid var(--border);
  background: #fff;
  width: 100%;
  font: inherit;
  border-radius: 16px;
}

.company-card:hover {
  transform: translateY(-2px);
  border-color: var(--primary);
  box-shadow: 0 12px 28px rgba(15, 118, 110, 0.12);
}

/* App shell */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  background: #fff;
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 1rem 0.85rem;
  flex-shrink: 0;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.35rem 0.65rem 1.25rem;
}

.sidebar-brand strong {
  display: block;
  line-height: 1.15;
}

.sidebar-brand small {
  color: var(--text-muted);
}

.nav-group {
  margin-bottom: 1rem;
}

.nav-group-label {
  font-size: 0.68rem;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0.35rem 0.85rem;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.65rem 0.85rem;
  border-radius: 10px;
  color: var(--text-muted);
  font-size: 0.875rem;
  font-weight: 550;
  margin: 0.15rem 0;
}

.nav-link:hover {
  background: rgba(15, 118, 110, 0.06);
}

.nav-link.active {
  background: rgba(15, 118, 110, 0.1);
  color: var(--primary-dark);
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 0.5rem;
}

.main {
  flex: 1;
  min-width: 0;
}

.toolbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 68px;
  padding: 0.75rem 1.5rem;
  background: rgba(241, 245, 249, 0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.toolbar h2 {
  margin: 0;
  font-size: 1.15rem;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.content {
  padding: 1.5rem;
  max-width: 1600px;
  margin: 0 auto;
}

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  background: var(--primary);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  font-weight: 700;
}

.mobile-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
}

@media (max-width: 960px) {
  .auth-shell {
    grid-template-columns: 1fr;
  }
  .auth-hero {
    display: none;
  }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 40;
    transform: translateX(-105%);
    transition: transform 0.2s ease;
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .mobile-toggle {
    display: inline-flex;
  }
  .company-cards {
    grid-template-columns: 1fr;
  }
  .content {
    padding: 1rem;
  }
}

.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  z-index: 30;
}

.overlay.show {
  display: block;
}

.actions {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  margin-top: 1.25rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.dashboard-split {
  grid-template-columns: 1.5fr 1fr;
  gap: 1rem;
}

@media (max-width: 900px) {
  .dashboard-split {
    grid-template-columns: 1fr;
  }
}

.muted {
  color: var(--text-muted);
}

.filter-bar {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.85rem;
  align-items: end;
  padding: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--muted);
}

.filter-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: center;
}

@media (max-width: 900px) {
  .filter-bar {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 640px) {
  .filter-bar {
    grid-template-columns: 1fr;
  }
}

/* Dispatch bill layout */
.dispatch-sheet {
  max-width: 1100px;
  margin: 0 auto;
}

.invoice-header {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1rem;
}

.invoice-header .full {
  grid-column: 1 / -1;
}

.readonly-field {
  background: var(--muted) !important;
  color: var(--secondary);
}

.invoice-lines select,
.invoice-lines input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.65rem;
  font: inherit;
  background: #fff;
}

.invoice-lines td {
  vertical-align: top;
}

.invoice-lines tfoot td {
  background: var(--muted);
}

@media (max-width: 960px) {
  .invoice-header {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 640px) {
  .invoice-header {
    grid-template-columns: 1fr;
  }
}

/* Shared dialogs (party / unit / etc.) — always on top of app shell */
body.sm-dialog-open {
  overflow: hidden !important;
}
.sm-dialog[hidden] {
  display: none !important;
}
.sm-dialog {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  margin: 0;
}
.sm-dialog.is-open {
  display: flex !important;
}
.sm-dialog-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  cursor: pointer;
}
.sm-dialog-card {
  position: relative;
  z-index: 2;
  width: min(480px, 100%);
  max-height: min(90vh, 640px);
  overflow: auto;
  background: var(--paper);
  border-radius: 14px;
  box-shadow: 0 24px 60px rgba(15, 23, 42, 0.28);
  border: 1px solid var(--border);
  pointer-events: auto;
}
.sm-dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1.1rem 0.75rem;
  border-bottom: 1px solid var(--border);
}
.sm-dialog-head h2 {
  margin: 0;
  font-size: 1.1rem;
}
.sm-dialog-head .muted {
  margin: 0.25rem 0 0;
  font-size: 0.85rem;
}
.sm-dialog-form {
  padding: 1rem 1.1rem 1.1rem;
  display: grid;
  gap: 0.85rem;
}
.sm-dialog-error {
  margin: 0;
  color: var(--error);
  font-size: 0.9rem;
}
.sm-dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.25rem;
}

/* Dispatch bill preview modal */
.sm-modal[hidden] { display: none !important; }
.sm-modal {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.sm-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
}
.sm-modal-panel {
  position: relative;
  z-index: 1;
  width: min(920px, 100%);
  max-height: min(92vh, 960px);
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.25);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sm-modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.1rem;
  border-bottom: 1px solid var(--border);
}
.sm-modal-header h2 {
  margin: 0;
  font-size: 1.05rem;
}
.sm-modal-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.sm-modal-body {
  overflow: auto;
  padding: 0.75rem 1rem 1.25rem;
  background: #e2e8f0;
}
.dispatch-preview-sheet {
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
  overflow: auto;
}
.dispatch-preview-sheet body,
.dispatch-preview-sheet {
  font-family: DejaVu Sans, "Plus Jakarta Sans", system-ui, sans-serif;
}

/* —— Responsive polish (phones / tablets) —— */
body.nav-open {
  overflow: hidden;
}

@media (max-width: 960px) {
  .toolbar {
    padding: 0.65rem 0.85rem;
    min-height: 60px;
    gap: 0.5rem;
  }
  .toolbar h2 {
    font-size: 1rem;
  }
  .toolbar-admin {
    display: none;
  }
  .toolbar-switch {
    padding: 0.35rem 0.65rem;
  }
  .sidebar {
    width: min(var(--sidebar-w), 86vw);
    box-shadow: 8px 0 32px rgba(15, 23, 42, 0.18);
  }
  .card-body {
    padding: 1rem;
  }
  .page-header {
    margin-bottom: 1rem;
  }
  .page-header h1 {
    font-size: 1.05rem;
  }
  .form-actions {
    flex-direction: column-reverse;
    align-items: stretch;
  }
  .form-actions .btn {
    width: 100%;
  }
  .sm-modal-panel {
    width: 100%;
    max-height: 94vh;
    border-radius: 12px;
  }
  .sm-modal-header {
    flex-direction: column;
    align-items: stretch;
  }
  .sm-modal-actions {
    justify-content: stretch;
  }
  .sm-modal-actions .btn {
    flex: 1;
  }
  .sm-dialog {
    padding: 0.65rem;
    align-items: flex-end;
  }
  .sm-dialog-card {
    width: 100%;
    max-height: min(88vh, 640px);
    border-radius: 16px 16px 12px 12px;
  }
  .sm-dialog-footer {
    flex-direction: column-reverse;
  }
  .sm-dialog-footer .btn {
    width: 100%;
  }
}

@media (max-width: 700px) {
  .field input,
  .field select,
  .field textarea,
  .invoice-lines select,
  .invoice-lines input {
    font-size: 16px; /* prevent iOS zoom on focus */
    min-height: 44px;
  }
  .btn {
    min-height: 42px;
  }
  .btn-sm {
    min-height: 36px;
  }
  table.data {
    font-size: 0.82rem;
    min-width: 560px;
  }
  table.data th,
  table.data td {
    padding: 0.6rem 0.7rem;
  }
  .chip {
    font-size: 0.7rem;
    padding: 0.18rem 0.55rem;
  }
  .stat-card .value {
    font-size: 1.2rem;
  }
  .auth-panel {
    padding: 1.25rem 1rem;
    min-height: 100vh;
    align-items: flex-start;
    padding-top: 2.5rem;
  }
  .auth-card {
    padding: 1.35rem;
    border-radius: 14px;
  }
  .product-select-wrap > div {
    flex-wrap: wrap;
  }
  .product-select-wrap [data-open-product-quick] {
    width: 100%;
  }
  .invoice-lines {
    min-width: 720px;
  }
}

@media (max-width: 480px) {
  .content {
    padding: 0.75rem;
  }
  .toolbar-actions .avatar {
    display: none;
  }
  .page-header p {
    font-size: 0.82rem;
  }
}

/* Safe area (notched phones) */
@supports (padding: max(0px)) {
  @media (max-width: 960px) {
    .content {
      padding-left: max(1rem, env(safe-area-inset-left));
      padding-right: max(1rem, env(safe-area-inset-right));
      padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }
    .sidebar {
      padding-top: max(1rem, env(safe-area-inset-top));
      padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }
    .toolbar {
      padding-top: max(0.65rem, env(safe-area-inset-top));
    }
  }
}

/* Screen print of ERP pages */
@media print {
  .sidebar,
  .toolbar,
  .overlay,
  .mobile-toggle,
  .sm-dialog,
  .form-actions,
  .btn,
  .nav-link {
    display: none !important;
  }
  .app-shell,
  .main,
  .content {
    display: block !important;
    padding: 0 !important;
    margin: 0 !important;
    max-width: none !important;
  }
  .card {
    box-shadow: none;
    border: 1px solid #ccc;
    break-inside: avoid;
  }
  table.data {
    min-width: 0;
  }
}
