@font-face {
  font-family: "DejaVu Sans";
  src: url("/static/fonts/DejaVuSans.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "DejaVu Sans";
  src: url("/static/fonts/DejaVuSans-Bold.ttf") format("truetype");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

:root {
  --obsidian: #0f1522;
  --obsidian-elevated: #172033;
  --obsidian-hover: #1e293b;
  --gold: #eab308;
  --gold-bright: #facc15;
  --gold-pale: #fef08a;
  --gold-border: #e6be5a;
  --ink: #ffffff;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #cbd5e1;
  --text: #0f1522;
  --muted: #475569;
  --primary: #0f1522;
  --primary-hover: #1e293b;
  --danger: #b91c1c;
  --danger-bg: #fef2f2;
  --danger-border: #fecaca;
  --badge-bg: #e2e8f0;
  --radius: 10px;
}

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

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  max-width: 100%;
  overflow-x: hidden;
  background-color: var(--bg);
  color: var(--text);
  font-family: "DejaVu Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
}

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

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 12px;
}

.topbar {
  background: var(--obsidian);
  color: var(--ink);
  border-bottom: 2px solid var(--gold-border);
  position: static;
  z-index: 20;
}

.topbar-inner {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 10px;
  padding-bottom: 10px;
}

.topbar-row-primary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand {
  font-weight: 700;
  font-size: 1rem;
  color: var(--ink);
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px 4px 0;
  max-width: 100%;
}

.logo-frame {
  position: relative;
  overflow: hidden;
  width: 34px;
  height: 34px;
  background-color: #000000;
  border-radius: 6px;
  border: 1.5px solid var(--gold-border);
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.logo-frame--lg {
  width: 64px;
  height: 64px;
  border-radius: 8px;
  border-width: 2px;
}

.logo-frame img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-name {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--gold-bright);
  line-height: 1.1;
  text-transform: uppercase;
}

.brand-sub {
  font-size: 0.72rem;
  font-weight: 400;
  color: #e2e8f0;
  line-height: 1.15;
}

.user-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  max-width: 100%;
}

.role-badge {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--obsidian-elevated);
  border: 1px solid var(--gold-border);
  color: var(--gold-pale);
  font-size: 0.875rem;
  font-weight: 600;
  max-width: 100%;
  word-break: break-word;
}

.topbar .btn-outline {
  background: var(--obsidian-elevated);
  color: var(--ink);
  border-color: var(--gold-border);
}

.topbar .btn-outline:hover {
  background: var(--obsidian-hover);
  border-color: var(--gold-bright);
  color: var(--gold-pale);
}

.logout-form {
  margin: 0;
}

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

.nav-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid rgba(230, 190, 90, 0.45);
  background: var(--obsidian-elevated);
  color: var(--ink);
  font-weight: 600;
  font-size: 0.9375rem;
}

.nav-link:hover {
  background: var(--obsidian-hover);
  border-color: var(--gold-bright);
  color: var(--gold-pale);
}

.nav-link.is-active {
  background: var(--gold);
  border-color: var(--gold-bright);
  color: var(--obsidian);
  font-weight: 700;
}

.main-content {
  padding-top: 18px;
  padding-bottom: 36px;
}

.card {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  min-width: 0;
  max-width: 100%;
}

body.auth-page {
  background-color: var(--obsidian);
  color: var(--ink);
}

.auth-page .main-content {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 16px;
  padding-bottom: 16px;
}

.auth-wrapper {
  width: 100%;
  min-height: calc(100vh - 64px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px 0;
}

.auth-page .auth-wrapper {
  min-height: auto;
  padding: 8px 0;
}

.auth-card {
  width: 100%;
  max-width: 420px;
}

.auth-page .auth-card {
  background: #141c2e;
  color: var(--ink);
  border: 1.5px solid var(--gold-border);
  border-top: 3px solid var(--gold-bright);
}

.auth-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 10px;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid rgba(230, 190, 90, 0.35);
}

.auth-brand-text {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.auth-title {
  margin: 0;
  font-size: 1.5rem;
  letter-spacing: 0.16em;
  color: var(--gold-bright);
  text-transform: uppercase;
}

.auth-subtitle {
  margin: 0;
  color: #e2e8f0;
  font-size: 0.9375rem;
}

.auth-form-lead {
  margin: 0 0 14px;
  color: var(--ink);
  font-size: 1.0625rem;
  font-weight: 600;
  text-align: center;
}

.auth-page .form-label {
  color: var(--ink);
}

.auth-page .form-control {
  background: #0f1522;
  color: var(--ink);
  border: 1.5px solid #475569;
}

.auth-page .form-control::placeholder {
  color: #94a3b8;
}

.auth-page .form-control:focus {
  outline: 2px solid var(--gold-bright);
  outline-offset: 1px;
  border-color: var(--gold-bright);
}

.auth-page .btn-primary {
  background: var(--gold);
  color: var(--obsidian);
  border-color: var(--gold-bright);
  font-weight: 700;
}

.auth-page .btn-primary:hover {
  background: var(--gold-bright);
  border-color: var(--gold-pale);
}

.auth-page .alert-error {
  background: #450a0a;
  border-color: #f87171;
  color: #fee2e2;
}

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.page-header > div {
  min-width: 0;
  max-width: 100%;
}

.page-title {
  margin: 0 0 4px;
  font-size: 1.25rem;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.page-subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.9375rem;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.stack-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

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

.form-label {
  font-weight: 600;
  font-size: 0.9375rem;
}

.form-control {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  font-family: inherit;
  font-size: 16px;
}

.form-control:focus {
  outline: 2px solid var(--gold);
  outline-offset: 1px;
  border-color: var(--obsidian);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-family: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-sm {
  min-width: 44px;
  min-height: 44px;
  padding: 8px 14px;
}

.btn-block {
  width: 100%;
}

.btn-primary {
  background: var(--obsidian);
  color: var(--ink);
  border-color: var(--obsidian);
}

.btn-primary:hover {
  background: var(--obsidian-hover);
  border-color: var(--gold-border);
}

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

.btn-outline:hover {
  background: var(--surface-alt);
  border-color: var(--obsidian);
}

.alert {
  padding: 12px 14px;
  border-radius: 8px;
  margin-bottom: 14px;
  font-size: 0.9375rem;
}

.alert-error {
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  color: var(--danger);
}

.notice-box {
  padding: 14px;
  border-radius: 8px;
  background: #f8fafc;
  border: 1px dashed var(--border);
  color: var(--muted);
}

.order-panels-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  margin-top: 16px;
}

@media (min-width: 768px) {
  .topbar {
    position: sticky;
    top: 0;
  }

  .container {
    padding: 0 20px;
  }

  .topbar-inner {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
  }

  .topbar-row-primary {
    flex: 1 1 auto;
  }

  .topnav {
    order: 2;
  }
}

/* M2: Orders & Trips */
.alert-success {
  background: #ecfdf5;
  border: 1px solid #a7f3d0;
  color: #166534;
}

.field-error {
  color: var(--danger);
  font-size: 0.875rem;
}

.field-hint {
  color: var(--muted);
  font-size: 0.8125rem;
}

.orders-header-card,
.order-detail-card {
  margin-bottom: 16px;
}

.form-page-title {
  margin-top: 10px;
}

.section-title {
  margin: 0 0 12px;
  font-size: 1.0625rem;
}

.form-field {
  min-width: 0;
}

.form-control {
  min-width: 0;
  max-width: 100%;
}

.filters-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 10px;
}

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

.form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 14px;
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

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

.detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
}

.detail-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 12px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.detail-label {
  font-size: 0.8125rem;
  color: var(--muted);
}

.detail-value {
  font-size: 0.9375rem;
  font-weight: 500;
  max-width: 100%;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.unloading-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.unloading-field {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 100%;
}

.status-badge,
.sub-badge {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  max-width: 100%;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.8125rem;
  font-weight: 600;
  line-height: 1.25;
}

.status-in_transit {
  background: #eff6ff;
  color: #1d4ed8;
}

.status-unloaded {
  background: #fefce8;
  color: #854d0e;
}

.status-invoiced {
  background: #f3e8ff;
  color: #6b21a8;
}

.status-partially_paid {
  background: #fff7ed;
  color: #c2410c;
}

.status-paid {
  background: #ecfdf5;
  color: #166534;
}

.badge-settled {
  background: #dcfce7;
  color: #166534;
}

.badge-unsettled {
  background: #f1f5f9;
  color: #475569;
}

.badge-reissue {
  background: #fef2f2;
  color: #b91c1c;
}

.badge-voided {
  background: #fee2e2;
  color: #991b1b;
}

.m3-voided-row {
  opacity: 0.65;
}

.cell-stack {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
}

.cell-badges {
  align-items: flex-end;
}

.order-link {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  align-self: flex-end;
  min-width: 44px;
  min-height: 44px;
  padding: 0 4px;
  max-width: 100%;
  color: var(--obsidian);
  font-weight: 700;
  text-decoration: underline;
  text-decoration-color: var(--gold);
  text-underline-offset: 3px;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.cell-muted {
  color: var(--muted);
  font-size: 0.8125rem;
}

.cell-sub {
  color: var(--muted);
  font-size: 0.75rem;
}

.cell-strong {
  font-weight: 700;
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.table-responsive {
  width: 100%;
  max-width: 100%;
}

.orders-table {
  width: 100%;
  border-collapse: collapse;
}

.orders-table thead {
  display: none;
}

.orders-table tbody {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.orders-table tr.order-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  min-width: 0;
}

.orders-table td {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 4px 0;
  border-bottom: 1px solid #f1f5f9;
  text-align: right;
  min-width: 0;
  overflow-wrap: anywhere;
}

.orders-table td::before {
  content: attr(data-label);
  font-weight: 600;
  color: var(--muted);
  font-size: 0.8125rem;
  text-align: left;
  flex: 0 1 44%;
  max-width: 46%;
}

.orders-table td:last-child {
  border-bottom: 0;
  padding-top: 8px;
  justify-content: flex-start;
  text-align: left;
}

.orders-table td:last-child::before {
  display: none;
}

.orders-table.report-table td:last-child {
  justify-content: space-between;
  text-align: right;
  padding-top: 4px;
}

.orders-table.report-table td:last-child::before {
  display: block;
}

.orders-table tr:not(.order-row) td[colspan] {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0;
  border-bottom: 0;
}

.orders-table tr:not(.order-row) td[colspan]::before {
  display: none;
}

@media (min-width: 768px) {
  .filters-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    align-items: end;
  }

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

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

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

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

  .unloading-form {
    flex-direction: row;
    align-items: flex-end;
  }

  .unloading-field {
    max-width: 280px;
  }

  .table-responsive {
    overflow-x: auto;
  }

  .orders-table {
    display: table;
    width: 100%;
  }

  .orders-table thead {
    display: table-header-group;
    background: var(--surface-alt);
  }

  .orders-table th {
    text-align: left;
    font-size: 0.8125rem;
    color: var(--obsidian);
    font-weight: 700;
    padding: 10px 8px;
    border-bottom: 2px solid var(--gold-border);
  }

  .orders-table tbody {
    display: table-row-group;
  }

  .orders-table tr.order-row {
    display: table-row;
    border: 0;
    border-radius: 0;
    padding: 0;
    background: var(--surface);
  }

  .orders-table tbody tr.order-row:nth-child(even) {
    background: var(--surface-alt);
  }

  .orders-table tbody tr.order-row:hover {
    background: var(--bg);
  }

  .orders-table td {
    display: table-cell;
    text-align: left;
    vertical-align: top;
    padding: 10px 8px;
    border-bottom: 1px solid var(--border);
    font-size: 0.875rem;
  }

  .orders-table td::before {
    display: none;
  }

  .orders-table td:last-child {
    padding-top: 10px;
  }

  .orders-table.report-table td:last-child {
    justify-content: initial;
    text-align: left;
    padding-top: 10px;
  }

  .orders-table.report-table td:last-child::before {
    display: none;
  }

  .orders-table tr:not(.order-row) td[colspan] {
    display: table-cell;
    padding: 10px 8px;
    border-bottom: 1px solid var(--border);
  }

  .cell-badges {
    align-items: flex-start;
  }

  .order-link {
    align-self: flex-start;
    justify-content: flex-start;
  }
}

/* M5: Reports, Error Pages, Mobile & Cascade Polish */
.m5-section-card {
  margin-bottom: 16px;
}

.error-wrapper {
  min-height: calc(100vh - 140px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px 0;
}

.error-card {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.error-code-badge {
  align-self: flex-start;
}

.error-title {
  margin: 0;
}

.error-detail {
  margin: 0;
}

.error-actions {
  margin-top: 4px;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.kpi-card {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.kpi-label {
  display: block;
  font-size: 0.8125rem;
  color: var(--muted);
  margin-bottom: 4px;
  font-weight: 600;
}

.kpi-value {
  display: block;
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--text);
  overflow-wrap: anywhere;
}

.kpi-sub {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 2px;
}

.net-positive {
  color: #166534;
}

.net-negative {
  color: #b91c1c;
}

.report-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.report-tab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #f8fafc;
  color: var(--text);
  font-weight: 600;
  font-size: 0.9375rem;
}

.report-tab:hover {
  background: #e2e8f0;
  border-color: var(--obsidian);
}

.report-tab.is-active {
  background: var(--obsidian);
  border-color: var(--gold-border);
  color: var(--gold-pale);
}

.report-split-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
}

.report-filters-actions {
  align-items: center;
}

/* M3/M4 mobile & cascade polish overrides */
body .m3-table td::before {
  flex: 0 1 44%;
  max-width: 46%;
}

body .allocation-row .form-field:last-child {
  align-self: end;
}

@media (max-width: 479px) {
  body .balance-value,
  .kpi-value {
    font-size: 1rem;
  }
}

@media (min-width: 768px) {
  .kpi-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

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

  .filters-grid .report-filters-actions,
  body .filters-grid .m4-filters-actions {
    grid-column: 1 / -1;
    align-items: center;
  }
}

/* Admin Panel & Telegram Settings */
.alert-success {
  background: #dcfce7;
  color: #166534;
  border: 1px solid #86efac;
  border-radius: 8px;
  padding: 10px 14px;
  margin-bottom: 14px;
  font-weight: 500;
}

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

.section-title {
  margin: 0 0 6px;
  font-size: 1.125rem;
}

.admin-top-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}

.admin-form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
  margin-top: 12px;
}

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

.admin-form-actions .btn,
.admin-tg-link-bar .btn,
.admin-header-actions .btn {
  min-height: 44px;
  min-width: 44px;
}

.admin-help-text {
  margin-top: 12px;
  line-height: 1.45;
}

.admin-users-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.admin-user-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  background: #f8fafc;
}

.admin-user-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.admin-user-name {
  display: block;
  font-size: 1.05rem;
}

.admin-user-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.admin-user-columns {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}

.admin-panel-block {
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  min-width: 0;
}

.admin-block-title {
  margin: 0 0 8px;
  font-size: 0.95rem;
}

.admin-tg-link-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.admin-link-token-box {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 10px;
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}

@media (min-width: 900px) {
  .admin-top-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: start;
  }

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

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

  .admin-form-grid .admin-form-actions {
    grid-column: 1 / -1;
  }
}

.profile-card {
  max-width: 680px;
  margin: 0 auto;
  border-top: 3px solid var(--gold);
}

.alert-warning {
  background: #fefce8;
  border: 1px solid #fde047;
  color: #854d0e;
}
