:root {
  color-scheme: light;
  --bg: #f3f6f8;
  --surface: #ffffff;
  --surface-soft: #f8fafb;
  --line: #d8e0e7;
  --line-strong: #bdc9d4;
  --text: #17212f;
  --muted: #637083;
  --muted-strong: #405064;
  --accent: #0f766e;
  --accent-dark: #0b5f59;
  --accent-soft: #e5f4f2;
  --danger: #b4232a;
  --shadow: 0 16px 40px rgba(22, 34, 51, 0.08);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background:
    linear-gradient(180deg, #e9eff3 0, #f3f6f8 220px),
    var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  font-size: 14px;
}

.app-shell {
  width: min(1360px, calc(100% - 40px));
  margin: 22px auto 32px;
}

.page-header {
  align-items: end;
  display: flex;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

.header-actions {
  align-items: center;
  display: flex;
  gap: 10px;
}

.title-group {
  display: grid;
  gap: 4px;
}

.app-kicker {
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  color: #111927;
  font-size: 30px;
  font-weight: 750;
  line-height: 1.15;
}

h2 {
  font-size: 15px;
  font-weight: 750;
  line-height: 1.25;
}

p {
  color: var(--muted);
  line-height: 1.45;
}

button,
select,
input {
  font: inherit;
}

.primary-action,
.secondary-action,
.icon-action {
  align-items: center;
  border-radius: 7px;
  cursor: pointer;
  display: inline-flex;
  font-weight: 750;
  height: 42px;
  justify-content: center;
  padding: 0 18px;
  transition: background 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}

.primary-action {
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #fff;
}

.primary-action:hover {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
  box-shadow: 0 8px 18px rgba(15, 118, 110, 0.2);
}

.secondary-action {
  background: #fff;
  border: 1px solid var(--line-strong);
  color: var(--muted-strong);
}

.secondary-action:hover {
  background: var(--surface-soft);
  border-color: #9aa8b7;
}

.config-access-action {
  gap: 8px;
}

.gear-symbol {
  font-size: 18px;
  line-height: 1;
}

.icon-action {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--muted-strong);
  font-size: 24px;
  height: 36px;
  padding: 0;
  width: 36px;
}

.row-action-text {
  display: none;
}

.icon-action:hover {
  background: var(--surface-soft);
}

.filters-panel,
.data-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.filters-panel {
  padding: 16px;
}

.section-heading {
  align-items: center;
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.filters {
  display: grid;
  gap: 12px;
  grid-template-columns: minmax(150px, 0.9fr) minmax(130px, 0.75fr) minmax(240px, 1.45fr) minmax(140px, 0.7fr) minmax(140px, 0.7fr);
}

label {
  display: grid;
  gap: 6px;
  min-width: 0;
}

label span {
  color: var(--muted-strong);
  font-size: 12px;
  font-weight: 750;
}

select,
input {
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  color: var(--text);
  height: 40px;
  min-width: 0;
  outline: none;
  padding: 0 11px;
  transition: border-color 120ms ease, box-shadow 120ms ease, background 120ms ease;
  width: 100%;
}

select {
  cursor: pointer;
}

select:focus,
input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

select:disabled {
  background: #edf1f4;
  color: #7b8796;
  cursor: not-allowed;
}

.client-search {
  position: relative;
}

.client-results {
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  box-shadow: 0 18px 38px rgba(22, 34, 51, 0.18);
  left: 0;
  max-height: 300px;
  overflow: auto;
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 10;
}

.client-result {
  background: #fff;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  color: var(--text);
  cursor: pointer;
  display: grid;
  gap: 3px;
  min-height: 0;
  padding: 10px 12px;
  text-align: left;
  width: 100%;
}

.client-result:hover {
  background: var(--accent-soft);
}

.client-result strong,
.client-result span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.client-result strong {
  font-size: 13px;
  font-weight: 750;
}

.client-result span {
  color: var(--muted);
  font-size: 12px;
}

.summary-bar {
  align-items: stretch;
  display: grid;
  gap: 12px;
  grid-template-columns: 1fr auto;
  margin: 14px 0;
}

.status-pill,
.total-box {
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 22px rgba(22, 34, 51, 0.05);
  display: flex;
  min-height: 54px;
}

.status-pill {
  color: var(--muted-strong);
  padding: 0 16px;
}

.status-pill::before {
  background: var(--accent);
  border-radius: 99px;
  content: "";
  height: 8px;
  margin-right: 10px;
  width: 8px;
}

.summary-bar.error .status-pill {
  color: var(--danger);
}

.summary-bar.error .status-pill::before {
  background: var(--danger);
}

.total-box {
  display: grid;
  gap: 2px;
  min-width: 230px;
  padding: 8px 16px;
  text-align: right;
}

.total-box span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.total-box strong {
  color: #101828;
  font-size: 21px;
  font-weight: 800;
}

.data-panel {
  overflow: hidden;
}

.table-heading {
  border-bottom: 1px solid var(--line);
  margin: 0;
  padding: 15px 16px;
}

.table-wrap {
  overflow: auto;
}

table {
  border-collapse: separate;
  border-spacing: 0;
  min-width: 1280px;
  table-layout: fixed;
  width: 100%;
}

.pedido-col {
  width: 8%;
}

.fecha-col {
  width: 8%;
}

.cliente-col {
  width: 22%;
}

.sucursal-col {
  width: 10%;
}

.vendedor-col {
  width: 15%;
}

.importe-col {
  width: 10%;
}

.factura-col {
  width: 8%;
}

.fecha-factura-col {
  width: 9%;
}

.acciones-col {
  width: 10%;
}

th,
td {
  border-bottom: 1px solid var(--line);
  overflow: hidden;
  padding: 12px 16px;
  text-align: left;
  text-overflow: ellipsis;
  vertical-align: middle;
  white-space: nowrap;
}

th {
  background: #f7f9fb;
  color: #536173;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  position: sticky;
  text-transform: uppercase;
  top: 0;
  z-index: 1;
}

td {
  background: #fff;
  color: #1d2939;
}

tbody tr:nth-child(even) td {
  background: #fbfcfd;
}

tbody tr:hover td {
  background: #eef8f6;
}

.numeric {
  font-variant-numeric: tabular-nums;
  text-align: right;
}

tbody td:first-child {
  color: var(--accent-dark);
  font-weight: 800;
}

.empty {
  color: var(--muted);
  padding: 34px 16px;
  text-align: center;
}

.factura-cancelada {
  color: var(--danger);
  font-weight: 700;
  text-decoration: line-through;
}

.acciones-th,
.acciones-td {
  text-align: center;
}

.acciones-td {
  padding: 6px 8px;
}

.row-action {
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  color: var(--accent-dark);
  cursor: pointer;
  font-size: 12px;
  font-weight: 800;
  height: 30px;
  letter-spacing: 0;
  margin: 0 2px;
  padding: 0;
  transition: background 120ms ease, border-color 120ms ease, color 120ms ease;
  width: 30px;
}

.row-action:hover:not(:disabled) {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.row-action:disabled {
  color: #aab4c0;
  cursor: not-allowed;
  opacity: 0.6;
}

.modal-backdrop {
  align-items: center;
  background: rgba(17, 25, 39, 0.42);
  display: flex;
  inset: 0;
  justify-content: center;
  padding: 20px;
  position: fixed;
  z-index: 30;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 26px 70px rgba(17, 25, 39, 0.26);
  max-width: 620px;
  width: min(100%, 620px);
}

.detail-modal {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 40px);
  max-width: 1100px;
  width: min(100%, 1100px);
}

.detail-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
  overflow: auto;
  padding: 18px;
}

.detail-meta {
  display: grid;
  gap: 10px 18px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin: 0;
}

.meta-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.detail-meta dt {
  color: var(--muted);
  font-size: 11px;
  font-weight: 750;
  letter-spacing: 0;
  margin: 0;
  text-transform: uppercase;
}

.detail-meta dd {
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-status {
  color: var(--muted-strong);
  padding: 6px 0;
}

.detail-status.error {
  color: var(--danger);
  font-weight: 750;
}

.detail-table-wrap {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: auto;
}

.detail-table {
  border-collapse: separate;
  border-spacing: 0;
  min-width: 780px;
  table-layout: fixed;
  width: 100%;
}

.detail-table th,
.detail-table td {
  border-bottom: 1px solid var(--line);
  overflow: hidden;
  padding: 10px 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-table th {
  background: #f7f9fb;
  color: #536173;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.detail-table tbody tr:nth-child(even) td {
  background: #fbfcfd;
}

.detail-table tfoot td {
  background: #f3f6f8;
  border-bottom: 0;
  font-weight: 800;
}

.detail-total-label {
  text-align: right;
}

.det-articulo-col { width: 12%; }
.det-nombre-col { width: 38%; }
.det-cantidad-col { width: 10%; }
.det-unidad-col { width: 10%; }
.det-precio-col { width: 13%; }
.det-importe-col { width: 17%; }

.modal-header {
  align-items: start;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 18px;
}

.config-form {
  display: grid;
  gap: 14px;
  grid-template-columns: 1fr 140px;
  padding: 18px;
}

.config-form label:nth-of-type(3),
.config-form label:nth-of-type(4),
.full-field,
.config-status,
.modal-actions {
  grid-column: 1 / -1;
}

.config-status {
  color: var(--muted-strong);
  margin: 0;
  min-height: 20px;
}

.config-status.success {
  color: var(--accent-dark);
  font-weight: 750;
}

.config-status.error {
  color: var(--danger);
  font-weight: 750;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

@media (max-width: 1050px) {
  .filters {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .client-filter {
    grid-column: 1 / -1;
  }
}

@media (max-width: 760px) {
  .detail-meta {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 680px) {
  body {
    background:
      linear-gradient(180deg, #e9eff3 0, #f3f6f8 160px),
      var(--bg);
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  }

  .app-shell {
    width: min(100% - 20px, 1360px);
    margin: 12px auto 24px;
  }

  /* Inputs: 16px to block iOS auto-zoom */
  select,
  input {
    font-size: 16px;
    height: 44px;
  }

  /* Header */
  .page-header {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 14px;
  }

  h1 {
    font-size: 22px;
  }

  .title-group p {
    font-size: 13px;
  }

  .header-actions {
    align-items: stretch;
    flex-direction: row;
    gap: 8px;
  }

  .primary-action,
  .secondary-action {
    flex: 1;
    height: 44px;
  }

  .config-access-action {
    flex: 0 0 auto;
    padding: 0 14px;
  }

  .config-access-label {
    display: none;
  }

  .gear-symbol {
    font-size: 20px;
  }

  /* Filters: dates side-by-side, rest stacked */
  .filters {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .filters label:not(.client-filter):nth-child(-n+3) {
    grid-column: 1 / -1;
  }

  .client-filter {
    grid-column: 1 / -1;
  }

  /* Summary bar */
  .summary-bar {
    grid-template-columns: 1fr;
    gap: 10px;
    margin: 12px 0;
  }

  .total-box {
    min-width: 0;
    padding: 10px 16px;
    text-align: left;
  }

  .total-box strong {
    font-size: 24px;
  }

  .status-pill {
    min-height: 44px;
  }

  /* Main table → card layout */
  .table-heading {
    padding: 14px 14px 12px;
  }

  .table-heading h2 {
    font-size: 14px;
  }

  .data-panel {
    background: transparent;
    border: 0;
    box-shadow: none;
  }

  .table-wrap {
    overflow: visible;
  }

  table {
    display: block;
    min-width: 0;
    table-layout: auto;
  }

  thead {
    display: none;
  }

  tbody {
    display: block;
  }

  tbody tr {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 4px 14px rgba(22, 34, 51, 0.05);
    display: grid;
    gap: 4px 12px;
    grid-template-columns: 1fr auto;
    margin: 0 0 12px;
    padding: 14px 14px 12px;
  }

  tbody tr:nth-child(even) td,
  tbody tr:hover td {
    background: transparent;
  }

  tbody td {
    background: transparent;
    border: 0;
    color: var(--text);
    padding: 0;
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }

  tbody td::before {
    color: var(--muted);
    content: attr(data-label);
    display: block;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 0.04em;
    line-height: 1.4;
    margin-bottom: 1px;
    text-transform: uppercase;
  }

  tbody td.empty {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 12px;
    color: var(--muted);
    grid-column: 1 / -1;
    padding: 28px 16px;
    text-align: center;
  }

  tbody td.empty::before {
    display: none;
  }

  /* Card cell positioning */
  .cell-pedido {
    grid-column: 1;
    grid-row: 1;
    font-size: 17px;
  }

  tbody td.cell-pedido {
    color: var(--accent-dark);
    font-weight: 800;
  }

  .cell-fecha {
    grid-column: 2;
    grid-row: 1;
    font-size: 12px;
    text-align: right;
  }

  .cell-fecha::before {
    display: none;
  }

  .cell-cliente {
    grid-column: 1 / -1;
    grid-row: 2;
    margin-top: 2px;
  }

  tbody td.cell-cliente {
    font-size: 15px;
    font-weight: 700;
  }

  .cell-sucursal {
    grid-column: 1;
    grid-row: 3;
  }

  .cell-vendedor {
    grid-column: 2;
    grid-row: 3;
    text-align: right;
  }

  .cell-importe {
    grid-column: 1 / -1;
    grid-row: 4;
    margin-top: 6px;
    padding-top: 8px;
    border-top: 1px solid var(--line);
    text-align: left;
  }

  tbody td.cell-importe {
    font-size: 20px;
    font-weight: 800;
    color: #101828;
  }

  .cell-factura {
    grid-column: 1;
    grid-row: 5;
  }

  .cell-fecha-factura {
    grid-column: 2;
    grid-row: 5;
    text-align: right;
  }

  .acciones-td {
    display: grid;
    grid-column: 1 / -1;
    grid-row: 6;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-top: 10px;
    padding: 0;
  }

  .acciones-td::before {
    display: none;
  }

  .row-action {
    font-size: 13px;
    font-weight: 750;
    height: 44px;
    margin: 0;
    width: 100%;
  }

  .row-action-icon {
    display: none;
  }

  .row-action-text {
    display: inline;
  }

  /* Modals: fullscreen on phone */
  .modal-backdrop {
    padding: 0;
  }

  .modal {
    border-radius: 0;
    height: 100dvh;
    max-height: 100dvh;
    max-width: 100%;
    width: 100%;
  }

  .detail-modal {
    height: 100dvh;
    max-height: 100dvh;
    max-width: 100%;
    width: 100%;
  }

  .modal-header {
    padding: 14px 14px 12px;
    padding-top: max(14px, env(safe-area-inset-top));
  }

  .modal-header h2 {
    font-size: 18px;
  }

  .icon-action {
    height: 40px;
    width: 40px;
  }

  .detail-body {
    gap: 14px;
    padding: 14px;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }

  .detail-meta {
    grid-template-columns: 1fr 1fr;
    gap: 10px 14px;
  }

  /* Detail table → cards */
  .detail-table-wrap {
    border: 0;
    overflow: visible;
  }

  .detail-table {
    display: block;
    min-width: 0;
    table-layout: auto;
  }

  .detail-table thead {
    display: none;
  }

  .detail-table tbody {
    display: block;
  }

  .detail-table tbody tr {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    display: grid;
    gap: 4px 10px;
    grid-template-columns: 1fr auto;
    margin: 0 0 10px;
    padding: 12px;
  }

  .detail-table tbody tr:nth-child(even) td {
    background: transparent;
  }

  .detail-table tbody td {
    background: transparent;
    border: 0;
    padding: 0;
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }

  .detail-table tbody td::before {
    color: var(--muted);
    content: attr(data-label);
    display: block;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }

  .detail-table tbody td.empty {
    grid-column: 1 / -1;
    padding: 20px 12px;
  }

  .detail-table tbody td.empty::before {
    display: none;
  }

  .det-cell-articulo {
    grid-column: 1;
    grid-row: 1;
    font-weight: 800;
    color: var(--accent-dark);
  }

  .det-cell-cantidad {
    grid-column: 2;
    grid-row: 1;
    text-align: right;
  }

  .det-cell-nombre {
    grid-column: 1 / -1;
    grid-row: 2;
    font-weight: 600;
  }

  .det-cell-unidad {
    grid-column: 1;
    grid-row: 3;
    font-size: 12px;
  }

  .det-cell-precio {
    grid-column: 2;
    grid-row: 3;
    font-size: 12px;
    text-align: right;
  }

  .det-cell-importe {
    grid-column: 1 / -1;
    grid-row: 4;
    margin-top: 4px;
    padding-top: 8px;
    border-top: 1px solid var(--line);
    font-weight: 800;
    text-align: right;
  }

  .detail-table tfoot {
    display: block;
  }

  .detail-table tfoot tr {
    display: grid;
    grid-template-columns: 1fr auto;
    background: var(--accent-soft);
    border-radius: 10px;
    padding: 12px 14px;
  }

  .detail-table tfoot td {
    background: transparent;
    border: 0;
    padding: 0;
  }

  .detail-total-label {
    text-align: left;
    grid-column: 1;
    font-size: 13px;
  }

  .detail-table tfoot #detailTotal {
    grid-column: 2;
    font-size: 17px;
  }

  /* Config form */
  .config-form {
    grid-template-columns: 1fr 110px;
    gap: 12px;
    padding: 14px;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }

  .modal-actions {
    flex-direction: column-reverse;
  }

  .modal-actions .primary-action,
  .modal-actions .secondary-action {
    width: 100%;
  }
}

@media (max-width: 380px) {
  .filters {
    grid-template-columns: 1fr;
  }

  .filters label {
    grid-column: 1 / -1 !important;
  }

  tbody tr {
    grid-template-columns: 1fr;
  }

  .cell-pedido,
  .cell-fecha,
  .cell-sucursal,
  .cell-vendedor,
  .cell-factura,
  .cell-fecha-factura {
    grid-column: 1;
    grid-row: auto;
    text-align: left;
  }

  .acciones-td {
    grid-template-columns: 1fr;
  }
}

/* No hover effects on touch */
@media (hover: none) {
  .primary-action:hover {
    background: var(--accent);
    border-color: var(--accent);
    box-shadow: none;
  }

  .secondary-action:hover {
    background: #fff;
    border-color: var(--line-strong);
  }

  tbody tr:hover td {
    background: inherit;
  }

  .row-action:hover:not(:disabled) {
    background: #fff;
    border-color: var(--line-strong);
  }
}
