/* Thanh công cụ giữ tìm kiếm và mọi nút trên cùng một hàng khi đủ chỗ. */
#view-customers .customer-toolbar {
  display: grid;
  grid-template-columns: minmax(0, 440px) minmax(0, 1fr);
  align-items: center;
  gap: 16px;
}
#view-customers .customer-toolbar .search-wrap {
  width: 100%;
  max-width: none;
  min-width: 0;
}
#view-customers .customer-toolbar-actions {
  display: flex;
  flex-wrap: nowrap;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
  margin-left: 0;
}
#view-customers .customer-toolbar-actions #customer-sort { flex: 0 0 200px; }
#view-customers .customer-toolbar-actions #sync-label { white-space: nowrap; }
#view-customers .customer-toolbar-actions #sync-full,
#view-customers .customer-toolbar-actions #sync-now { flex: none; }

/* Bảng khách hàng cần nhiều chỗ hơn phần còn lại của ứng dụng. */
@media (max-width: 1550px) {
  #view-customers .customer-toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
  }
  #view-customers .customer-toolbar .search-wrap {
    width: 100%;
    max-width: none;
    min-width: 0;
  }
  #view-customers .customer-toolbar .search-wrap input { width: 100%; }
  #view-customers .customer-toolbar-actions {
    width: 100%;
    margin-left: 0;
    justify-content: flex-start;
  }
  #view-customers .customer-toolbar-actions #customer-sort {
    display: block;
    flex: 0 0 200px;
    max-width: 280px;
  }
  #view-customers .customer-table-scroll { overflow-x: visible; }
  #view-customers .customer-head { display: none; }
  #view-customers .customer-totals:not([hidden]) {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    min-width: 0;
    gap: 10px;
    padding: 12px 16px;
  }
  #view-customers .customer-totals > span:first-child { grid-column: 1 / -1; font-weight: 700; }
  #view-customers .customer-totals > span:not(:first-child) { display: none; }
  #view-customers .customer-totals > strong {
    min-width: 0;
    padding: 0;
    text-align: left;
    overflow-wrap: anywhere;
  }
  #view-customers .customer-totals > strong:nth-child(4)::before { content: 'Nợ hiện tại'; }
  #view-customers .customer-totals > strong:nth-child(5)::before { content: 'Tổng bán'; }
  #view-customers .customer-totals > strong:nth-child(6)::before { content: 'Tổng bán trừ trả hàng'; }
  #view-customers .customer-totals > strong::before {
    display: block;
    margin-bottom: 3px;
    color: #64748b;
    font-size: 11px;
    font-weight: 400;
  }
  #view-customers #customer-list {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    padding: 12px;
  }
  #view-customers #customer-list > .empty-state { grid-column: 1 / -1; }
  #view-customers .customer-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(120px, auto);
    gap: 7px 12px;
    min-width: 0;
    min-height: 0;
    padding: 15px;
    border: 1px solid #dce3eb;
    border-radius: 10px;
    align-items: start;
  }
  #view-customers .customer-row > .customer-code { grid-column: 1; grid-row: 1; }
  #view-customers .customer-row > .customer-name { grid-column: 1; grid-row: 2; padding: 0; }
  #view-customers .customer-row .customer-name strong { font-size: 15px; }
  #view-customers .customer-row .customer-summary-meta { font-size: 12px; }
  #view-customers .customer-row > .customer-phone { grid-column: 1; grid-row: 3; padding: 0; }
  #view-customers .customer-row > .customer-debt { grid-column: 2; grid-row: 3; }
  #view-customers .customer-row > .customer-invoiced { grid-column: 1; grid-row: 4; }
  #view-customers .customer-row > .customer-revenue { grid-column: 2; grid-row: 4; }
  #view-customers .customer-row > .customer-debt,
  #view-customers .customer-row > .customer-invoiced,
  #view-customers .customer-row > .customer-revenue {
    min-width: 0;
    padding: 0;
    text-align: left;
    white-space: normal;
    overflow-wrap: anywhere;
  }
  #view-customers .customer-row > .customer-debt::before { content: 'Nợ hiện tại'; }
  #view-customers .customer-row > .customer-invoiced::before { content: 'Tổng bán'; }
  #view-customers .customer-row > .customer-revenue::before { content: 'Tổng bán trừ trả hàng'; }
  #view-customers .customer-row > .customer-debt::before,
  #view-customers .customer-row > .customer-invoiced::before,
  #view-customers .customer-row > .customer-revenue::before {
    display: block;
    margin-bottom: 3px;
    color: #64748b;
    font-size: 11px;
    font-weight: 400;
  }
  #view-customers .customer-row > .customer-actions {
    grid-column: 2;
    grid-row: 1 / span 2;
    width: 132px;
    max-width: 100%;
    justify-self: end;
    align-self: start;
  }
  #view-customers .customer-row > .customer-actions .secondary-button {
    grid-column: auto;
    grid-row: auto;
    width: 100%;
    justify-self: stretch;
    line-height: 1.3;
    white-space: normal;
  }
  #view-customers .customer-row > .customer-messenger-status {
    grid-column: 1 / -1;
    grid-row: 5;
    padding: 10px 0 0;
    margin: 4px 0 0;
    border-top: 1px solid #e6ebf1;
  }
  #view-customers .customer-row > .customer-messenger-editor {
    grid-column: 1 / -1;
    grid-row: 6;
    grid-template-columns: 1fr;
    padding: 15px;
    margin-top: 5px;
  }
}

/* Cửa sổ nhỏ: điều hướng chuyển lên trên thay vì chiếm một cột dọc. */
@media (max-width: 1050px) {
  .app-shell { display: block; }
  .sidebar {
    position: relative;
    display: block;
    width: 100%;
    height: auto;
    padding: 10px 16px 0;
    border-right: 0;
    border-bottom: 1px solid #dce3eb;
  }
  .sidebar-top { display: block; }
  .sidebar .brand { padding: 0 90px 8px 0; }
  .sidebar .nav-label, .sidebar-status { display: none; }
  .sidebar .nav {
    display: flex;
    gap: 4px;
    width: 100%;
    overflow-x: auto;
    padding: 0 0 7px;
    scrollbar-width: thin;
  }
  .sidebar .nav-item {
    flex: none;
    width: auto;
    min-height: 38px;
    padding: 0 11px;
    white-space: nowrap;
  }
  .sidebar-bottom { position: absolute; top: 9px; right: 16px; padding: 0; }
  .sidebar .logout-button { width: auto; padding: 9px 0; white-space: nowrap; }
  .topbar-label, .topbar-separator { display: none; }
  .topbar-inner { padding: 0 20px; }
  main { padding: 24px 20px 50px; }
}

@media (max-width: 760px) {
  #view-customers #customer-list { grid-template-columns: minmax(0, 1fr); }
  #view-customers .customer-totals:not([hidden]) { grid-template-columns: minmax(0, 1fr); }
  #view-customers .customer-totals > span:first-child { margin-bottom: 3px; }
  #view-customers .customer-totals > strong {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    padding: 5px 0;
    text-align: right;
  }
  #view-customers .customer-totals > strong::before { flex: 0 0 42%; margin: 0; text-align: left; }
  #view-customers .customer-row {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
    padding: 14px;
    border-radius: 5px;
  }
  #view-customers .customer-row > .customer-code,
  #view-customers .customer-row > .customer-name,
  #view-customers .customer-row > .customer-phone,
  #view-customers .customer-row > .customer-debt,
  #view-customers .customer-row > .customer-invoiced,
  #view-customers .customer-row > .customer-revenue,
  #view-customers .customer-row > .customer-messenger-status,
  #view-customers .customer-row > .customer-actions,
  #view-customers .customer-row > .customer-messenger-editor { grid-column: 1; min-width: 0; }
  #view-customers .customer-row > .customer-code { grid-row: 1; }
  #view-customers .customer-row > .customer-name { grid-row: 2; margin-top: 4px; padding: 0; }
  #view-customers .customer-row > .customer-phone { grid-row: 3; margin-top: 5px; padding: 0; }
  #view-customers .customer-row > .customer-phone-empty { display: none; }
  #view-customers .customer-row > .customer-debt,
  #view-customers .customer-row > .customer-invoiced,
  #view-customers .customer-row > .customer-revenue {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 0 0;
    text-align: right;
  }
  #view-customers .customer-row > .customer-debt { grid-row: 4; margin-top: 10px; padding-top: 10px; border-top: 1px solid #e6ebf1; }
  #view-customers .customer-row > .customer-invoiced { grid-row: 5; }
  #view-customers .customer-row > .customer-revenue { grid-row: 6; }
  #view-customers .customer-row > .customer-debt::before,
  #view-customers .customer-row > .customer-invoiced::before,
  #view-customers .customer-row > .customer-revenue::before {
    flex: 0 0 42%;
    margin: 0;
    text-align: left;
  }
  #view-customers .customer-row > .customer-messenger-status {
    grid-row: 7;
    margin-top: 10px;
    padding: 10px 0 0;
    border-top: 1px solid #e6ebf1;
  }
  #view-customers .customer-row > .customer-actions {
    grid-row: 8;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    width: 100%;
    max-width: none;
    margin-top: 12px;
    justify-self: stretch;
  }
  #view-customers .customer-row > .customer-actions .secondary-button {
    grid-column: auto;
    grid-row: auto;
    width: 100%;
    min-height: 38px;
    justify-self: stretch;
    white-space: normal;
  }
  #view-customers .customer-row > .customer-messenger-editor { grid-row: 9; }
  #view-customers .customer-toolbar-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }
  #view-customers .customer-toolbar-actions #customer-sort,
  #view-customers .customer-toolbar-actions #sync-label { grid-column: 1 / -1; width: 100%; max-width: none; }
  #view-customers .customer-toolbar-actions #sync-full,
  #view-customers .customer-toolbar-actions #sync-now { width: 100%; min-width: 0; white-space: normal; }
}

@media (max-width: 520px) {
  .sidebar { padding-inline: 12px; }
  .sidebar .brand strong { font-size: 15px; }
  .sidebar .brand small { font-size: 10px; }
  .sidebar .nav-item { font-size: 12px; }
  .topbar-inner { padding-inline: 14px; }
  main { padding: 18px 12px 40px; }
}
