:root {
  --bg: #f6f8fb;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #8b5cf6;
  --primary-dark: #6d28d9;
  --border: #e5e7eb;
  --danger: #dc2626;
  --success: #059669;
  --accent: #f43f5e;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color: var(--text);
  background: linear-gradient(180deg, var(--bg), color-mix(in srgb, var(--bg), #dbeafe 15%));
  min-height: 100dvh;
  padding-bottom: 34px;
}

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1rem;
}

.login-card {
  width: min(420px, 100%);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
  box-shadow: 0 12px 30px rgba(2, 6, 23, 0.12);
}

.login-card h2 {
  margin: 0 0 0.8rem;
}

.login-form {
  display: grid;
  gap: 0.7rem;
}

.login-user-label {
  color: color-mix(in srgb, var(--primary), var(--text) 35%);
  font-weight: 600;
  letter-spacing: 0.01em;
}

.remember {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
}

.login-error {
  margin: 0;
  color: var(--danger);
  font-size: 0.85rem;
}

.user-badge {
  display: inline-block;
  padding: 0.35rem 0.65rem;
  border: 1px solid color-mix(in srgb, var(--primary), var(--border) 45%);
  border-radius: 999px;
  font-size: 0.84rem;
  font-weight: 600;
  color: color-mix(in srgb, var(--primary), var(--text) 40%);
  background: color-mix(in srgb, var(--primary), var(--card) 92%);
  box-shadow: 0 2px 10px color-mix(in srgb, var(--primary), transparent 82%);
}

[data-theme="dark"] {
  --bg: #0b1020;
  --card: #11182b;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --primary: #a78bfa;
  --primary-dark: #8b5cf6;
  --border: #243044;
  --danger: #f87171;
  --success: #34d399;
  --accent: #fb7185;
}

.container {
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 1rem;
  display: grid;
  gap: 1rem;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  justify-content: flex-end;
}

.header h1 {
  margin: 0 0 0.25rem;
}

#mainTitleUser {
  color: #0ea5e9;
  font-size: 0.72em;
  font-weight: 600;
}

.header p {
  margin: 0;
  color: var(--muted);
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.05);
}

.card h2 {
  margin-top: 0;
}

label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.95rem;
}

input,
select,
textarea,
button {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.7rem;
  font: inherit;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--primary), white 45%);
  outline-offset: 1px;
}

input[readonly] {
  background: color-mix(in srgb, var(--card), #f9fafb 65%);
}

button {
  cursor: pointer;
  border: 0;
  color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--accent));
}

button:hover {
  background: var(--primary-dark);
}

button.secondary {
  background: #475569;
}

button.secondary:hover {
  background: #334155;
}

.hidden {
  display: none;
}

.app-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  text-align: center;
  font-size: 0.66rem;
  color: #cbd5e1;
  padding: 0.26rem 0.55rem 0.34rem;
  border-top: 1px solid #334155;
  background: linear-gradient(180deg, #1e293b, #0f172a);
  letter-spacing: 0.02em;
  z-index: 40;
}

.super-admin-only.hidden {
  display: none !important;
}

hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 0.25rem 0 0.5rem;
}

.dashboard {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.7rem;
  margin-bottom: 0.95rem;
}

.kpi {
  border-radius: 12px;
  padding: 0.75rem 0.8rem;
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--card), #f8fafc 35%);
}

.kpi p {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.kpi h3 {
  margin: 0.2rem 0 0;
  font-size: 1.25rem;
}

.kpi-1 {
  border-left: 5px solid #f59e0b;
}

.kpi-2 {
  border-left: 5px solid #3b82f6;
}

.kpi-3 {
  border-left: 5px solid #22c55e;
}

.kpi-4 {
  border-left: 5px solid #ef4444;
}

.table-controls {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.filters {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.search input {
  min-width: 280px;
}

.table-wrap {
  overflow-x: auto;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
}

table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

th,
td {
  border-bottom: 1px solid var(--border);
  text-align: left;
  padding: 0.6rem;
  font-size: 0.9rem;
  vertical-align: top;
  word-break: break-word;
}

th {
  background: color-mix(in srgb, var(--card), #f8fafc 50%);
  position: sticky;
  top: 0;
  z-index: 1;
}

.text-danger {
  color: var(--danger);
  font-weight: 600;
}

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

.status-badge {
  display: inline-block;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.2rem 0.55rem;
  font-size: 0.78rem;
  color: var(--muted);
}

.status-badge.entregada {
  color: var(--success);
  border-color: color-mix(in srgb, var(--success), var(--border) 60%);
}

.status-badge.cancelada {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger), var(--border) 60%);
}

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

.normal-only .actions-col {
  width: 132px;
  min-width: 132px;
  max-width: 132px;
}

.normal-only .actions-col .row-actions {
  flex-wrap: nowrap;
  justify-content: center;
}

.row-actions button {
  padding: 0.35rem 0.55rem;
  font-size: 0.8rem;
}

.row-actions .danger {
  background: #b91c1c;
}

.row-actions .danger:hover {
  background: #991b1b;
}

.row-actions .success {
  background: #16a34a;
}

.row-actions .success:hover {
  background: #15803d;
}

.row-actions .action-gap {
  margin-left: 0.45rem;
}

.status-select {
  min-width: 120px;
  max-width: 150px;
  margin-top: 0.35rem;
  padding: 0.25rem 0.4rem;
  font-size: 0.78rem;
}

.status-actions {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin-top: 0.35rem;
  align-items: center;
}

.status-btn {
  padding: 0.2rem 0.35rem;
  font-size: 0.78rem;
  line-height: 1;
  border-radius: 6px;
  background: #334155;
  border: 1px solid #1e293b;
}

.status-btn:hover {
  background: #1e293b;
}

.view-message-btn {
  padding: 0.18rem 0.42rem;
  font-size: 0.72rem;
}

.cell-title {
  font-weight: 600;
}

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

.amount-line {
  display: block;
  font-size: 0.84rem;
}

.amount-line strong {
  font-size: 0.88rem;
}

.deliveries-list {
  display: grid;
  gap: 0.75rem;
}

.deliveries-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.6rem;
}

.delivery-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.85rem;
  background: color-mix(in srgb, var(--card), #f8fafc 40%);
}

.delivery-card h4 {
  margin: 0 0 0.35rem;
}

.delivery-line {
  margin: 0.2rem 0;
  font-size: 0.92rem;
}

.delivery-map a {
  text-decoration: none;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.62);
  z-index: 100;
  padding: 1rem;
  display: grid;
  align-items: center;
  justify-items: center;
}

.modal.hidden {
  display: none;
}

body.admin-mode .normal-only,
body.admin-mode #openNewSaleBtn,
body.admin-mode #mapBtn,
body.admin-mode #adminModeBtn,
body.admin-mode #riderModeBtn {
  display: none;
}

body.admin-mode .admin-only {
  display: block;
}

body.super-admin #openNewSaleBtn,
body.super-admin #riderModeBtn {
  display: none;
}

body.rider-mode .dashboard {
  display: none;
}

body.rider-mode #openNewSaleBtn,
body.rider-mode #mapBtn,
body.rider-mode #adminModeBtn,
body.rider-mode #salesSectionTitle {
  display: none;
}

body.rider-mode #currentUser,
body.rider-mode #logoutBtn {
  display: none !important;
}

body.rider-mode #riderModeBtn {
  padding: 0.38rem 0.55rem;
  font-size: 0.82rem;
  line-height: 1.1;
}

body.rider-mode table th:nth-child(6),
body.rider-mode table th:nth-child(7),
body.rider-mode table td:nth-child(6),
body.rider-mode table td:nth-child(7) {
  display: none;
}

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

.admin-option-cards {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.9rem;
}

.admin-option-card {
  text-align: left;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  min-height: 140px;
  background: color-mix(in srgb, var(--card), #f8fafc 40%);
  color: var(--text);
  transition: transform 140ms ease, box-shadow 180ms ease;
}

.admin-option-card:hover {
  transform: translateY(-2px);
  background: color-mix(in srgb, var(--card), #e2e8f0 55%);
  box-shadow: 0 10px 22px rgba(2, 6, 23, 0.12);
}

.admin-option-card h3 {
  margin: 0 0 0.35rem;
}

.admin-option-card p {
  margin: 0;
  color: var(--muted);
}

.admin-module {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.85rem;
  margin-bottom: 1rem;
  background: color-mix(in srgb, var(--card), #f8fafc 35%);
}

.admin-module h3 {
  margin-top: 0;
}

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

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

.admin-table table {
  table-layout: auto;
}

.modal-content {
  width: min(980px, 96vw);
  height: min(92vh, 860px);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto 1fr;
}

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

.modal-head h3 {
  margin: 0;
}

#saleFrame {
  width: 100%;
  height: 100%;
  border: 0;
}

#adminModuleFrame {
  width: 100%;
  height: 100%;
  border: 0;
}

@media (max-width: 900px) {
  .table-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .search input {
    min-width: 0;
  }

  .header {
    flex-direction: column;
    align-items: stretch;
  }

  .header-actions {
    justify-content: flex-start;
  }

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

  .admin-grid,
  .admin-form {
    grid-template-columns: 1fr;
  }

  .admin-option-cards {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .container {
    padding: 0.75rem;
  }

  .header-actions {
    width: 100%;
    gap: 0.5rem;
  }

  #currentUser {
    width: 100%;
    text-align: center;
    order: 1;
  }

  #openNewSaleBtn {
    order: 2;
    flex: 1 1 calc(100% - 4rem);
  }

  #mapBtn,
  #adminModeBtn,
  #riderModeBtn {
    flex: 1 1 calc(50% - 0.25rem);
  }

  #mapBtn {
    order: 3;
  }

  #adminModeBtn {
    order: 4;
  }

  #riderModeBtn {
    order: 5;
  }

  #headerSpacer {
    display: none !important;
  }

  #themeToggle,
  #logoutBtn {
    flex: 0 0 2.5rem;
    width: 2.5rem;
    min-width: 2.5rem;
    padding-left: 0;
    padding-right: 0;
  }

  #themeToggle {
    order: 6;
    margin-left: auto;
  }

  #logoutBtn {
    order: 7;
  }

  .dashboard {
    grid-template-columns: 1fr;
  }

  .table-wrap {
    overflow: visible;
    border: 0;
    background: transparent;
  }

  table {
    width: 100%;
    table-layout: auto;
  }

  thead {
    display: none;
  }

  tbody,
  tr,
  td {
    display: block;
    width: 100%;
  }

  tbody tr {
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--card);
    margin-bottom: 0.6rem;
    overflow: hidden;
  }

  tbody td {
    border-bottom: 1px solid var(--border);
    padding: 0.55rem 0.65rem;
  }

  tbody td:last-child {
    border-bottom: 0;
  }

  tbody td[colspan] {
    border: 1px solid var(--border);
    border-radius: 10px;
    text-align: center;
    background: var(--card);
    margin: 0.3rem 0;
  }

  tbody td:not([colspan])::before {
    display: block;
    margin-bottom: 0.2rem;
    font-size: 0.76rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    color: var(--muted);
    text-transform: uppercase;
  }

  tbody td:nth-child(1)::before { content: "Estatus"; }
  tbody td:nth-child(2)::before { content: "Venta y horario"; }
  tbody td:nth-child(3)::before { content: "Cliente"; }
  tbody td:nth-child(4)::before { content: "Receptor"; }
  tbody td:nth-child(5)::before { content: "Direccion y mapa"; }
  tbody td:nth-child(6)::before { content: "Cobro"; }
  tbody td:nth-child(7)::before { content: "Acciones"; }

  .status-actions {
    margin-top: 0.45rem;
  }

  .row-actions {
    justify-content: flex-start;
  }
}
