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

body {
  font-family: system-ui, sans-serif;
  background: #f4f6f9;
  color: #222;
  min-height: 100vh;
}

header {
  background: #1a1a2e;
  color: #fff;
  padding: 16px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
}
header h1 { font-size: 1.2rem; font-weight: 600; }

main { max-width: 960px; margin: 32px auto; padding: 0 16px; }

.card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0,0,0,.1);
  padding: 20px 24px;
  margin-bottom: 24px;
}
.card h2 { font-size: 1rem; margin-bottom: 16px; color: #444; }

.form-grid {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 1fr;
  gap: 10px;
  align-items: end;
}
.form-grid-venda {
  grid-template-columns: 3fr 1fr 1fr 1fr;
}
@media (max-width: 640px) { .form-grid { grid-template-columns: 1fr 1fr; } }

label { font-size: .8rem; color: #666; display: block; margin-bottom: 4px; }

input, select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: .9rem;
}
input:focus, select:focus { outline: 2px solid #4f46e5; border-color: transparent; }

.btn {
  padding: 9px 18px;
  border: none;
  border-radius: 6px;
  font-size: .9rem;
  cursor: pointer;
  font-weight: 500;
}
.btn-primary  { background: #4f46e5; color: #fff; }
.btn-primary:hover  { background: #4338ca; }
.btn-danger   { background: #ef4444; color: #fff; padding: 5px 10px; font-size: .8rem; }
.btn-danger:hover   { background: #dc2626; }
.btn-edit     { background: #f59e0b; color: #fff; padding: 5px 10px; font-size: .8rem; }
.btn-edit:hover     { background: #d97706; }
.btn-cancel   { background: #e5e7eb; color: #333; }
.btn-cancel:hover   { background: #d1d5db; }
.btn-success  { background: #16a34a; color: #fff; width: 100%; }
.btn-success:hover  { background: #15803d; }

.toolbar {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
  align-items: center;
}
.toolbar input { max-width: 240px; }
.toolbar .total { margin-left: auto; font-size: .85rem; color: #666; }

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

table {
  width: 100%;
  border-collapse: collapse;
  font-size: .9rem;
}
thead th {
  text-align: left;
  padding: 10px 12px;
  background: #f8fafc;
  border-bottom: 2px solid #e2e8f0;
  font-size: .78rem;
  text-transform: uppercase;
  color: #64748b;
  letter-spacing: .04em;
}
tbody tr { border-bottom: 1px solid #f1f5f9; }
tbody tr:hover { background: #f8fafc; }
td { padding: 10px 12px; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: .75rem;
  font-weight: 600;
}
.badge-ok    { background: #dcfce7; color: #15803d; }
.badge-low   { background: #fef9c3; color: #a16207; }
.badge-empty { background: #fee2e2; color: #b91c1c; }

.actions { display: flex; gap: 6px; }

.tfoot-total td {
  padding: 10px 12px;
  border-top: 2px solid #e2e8f0;
  background: #f8fafc;
}

/* ── Produto Cards ─────────────────────────────────── */
.produtos-grid {
  display: none;
  flex-direction: column;
  gap: 8px;
}

.produto-card {
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.produto-card-topo {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px 10px;
  background: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
}

.produto-nome {
  font-size: .88rem;
  color: #222;
  line-height: 1.3;
  word-break: break-word;
  flex: 1;
}

.produto-card-body {
  padding: 6px 14px;
  flex: 1;
}

.produto-campo {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid #f1f5f9;
  gap: 8px;
}
.produto-campo:last-child { border-bottom: none; }

.campo-label {
  font-size: .73rem;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
}

.campo-valor { font-size: .87rem; color: #222; text-align: right; }

.produto-card-footer {
  display: flex;
  gap: 6px;
  padding: 8px 14px 10px;
  border-top: 1px solid #f1f5f9;
}

/* ── Venda linha (mobile) ──────────────────────────── */
.venda-linha {
  border-bottom: 1px solid #f1f5f9;
  cursor: pointer;
}
.venda-linha:last-child { border-bottom: none; }
.venda-linha:hover .venda-linha-row { background: #f8fafc; }

.venda-linha-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
}

.venda-ln-nome {
  flex: 1;
  min-width: 0;
  font-size: .88rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.venda-ln-qtd {
  font-size: .75rem;
  color: #94a3b8;
  white-space: nowrap;
}

.venda-ln-total {
  font-size: .88rem;
  color: #15803d;
  white-space: nowrap;
}

.venda-card-detalhe {
  max-height: 0;
  overflow: hidden;
  transition: max-height .25s ease, padding .25s ease;
  padding: 0 14px;
  background: #f8fafc;
}
.venda-card-detalhe.open {
  max-height: 500px;
  padding: 10px 14px;
  border-top: 1px solid #e2e8f0;
}

.venda-det-data {
  font-size: .8rem;
  color: #64748b;
  margin-bottom: 8px;
  display: flex;
  gap: 6px;
  align-items: center;
}

.venda-det-acoes {
  display: flex;
  gap: 6px;
  padding-top: 10px;
  margin-top: 6px;
  border-top: 1px solid #e2e8f0;
}

.produtos-total-row {
  padding: 10px 0 0;
  text-align: right;
  font-size: .85rem;
  color: #64748b;
  border-top: 2px solid #e2e8f0;
  margin-top: 10px;
}

#msg {
  padding: 10px 14px;
  border-radius: 6px;
  margin-bottom: 14px;
  display: none;
  font-size: .9rem;
}
.msg-ok  { background: #dcfce7; color: #15803d; }
.msg-err { background: #fee2e2; color: #b91c1c; }

.empty-state {
  text-align: center;
  padding: 40px;
  color: #94a3b8;
  font-size: .9rem;
}

/* Itens da venda */
.itens-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: .85rem;
  font-weight: 600;
  color: #444;
}

.item-linha {
  display: grid;
  grid-template-columns: 1fr 100px 90px 32px;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
.item-linha select, .item-linha input { margin: 0; }
.item-sub { font-size: .82rem; color: #64748b; text-align: right; }

/* Autocomplete */
.ac-wrap { position: relative; }
.ac-wrap input[type="text"] { width: 100%; }

.ac-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,.12);
  z-index: 200;
  max-height: 220px;
  overflow-y: auto;
}

.ac-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  cursor: pointer;
  font-size: .88rem;
  gap: 8px;
}
.ac-item:hover { background: #f0f4ff; }
.ac-item + .ac-item { border-top: 1px solid #f1f5f9; }
.ac-nome { color: #222; }
.ac-estoque { color: #94a3b8; font-size: .78rem; white-space: nowrap; }

.btn-remove-item {
  background: none;
  border: none;
  color: #ef4444;
  font-size: 1.2rem;
  cursor: pointer;
  line-height: 1;
  padding: 0;
}
.btn-remove-item:hover { color: #b91c1c; }

.venda-resumo {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid #e2e8f0;
}

.pago-badge-label { cursor: pointer; user-select: none; }
.pago-badge-label input { display: none; }

.pago-badge { display: inline-block; padding: 5px 14px; border-radius: 99px; font-size: .82rem; font-weight: 600; transition: background .15s, color .15s; }

.pago-off { display: inline; }
.pago-on  { display: none; }

.pago-badge { background: #fee2e2; color: #b91c1c; }
.pago-badge-label input:checked + .pago-badge { background: #dcfce7; color: #15803d; }
.pago-badge-label input:checked + .pago-badge .pago-off { display: none; }
.pago-badge-label input:checked + .pago-badge .pago-on  { display: inline; }

.venda-total-display { font-size: 1rem; color: #222; }
.venda-total-display strong { font-size: 1.15rem; color: #15803d; }

/* Tabela vendas — slidedown */
.venda-row { cursor: pointer; }
.venda-row:hover { background: #f0fdf4 !important; }

.chevron {
  display: inline-block;
  font-size: .75rem;
  color: #94a3b8;
  transition: transform .2s;
}

.detalhe-row td { padding: 0; }
.detalhe-content {
  max-height: 0;
  overflow: hidden;
  transition: max-height .28s ease, padding .28s ease;
  padding: 0 16px;
  background: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
}
.detalhe-row.open .detalhe-content {
  max-height: 600px;
  padding: 12px 16px;
}

.detalhe-item {
  display: grid;
  grid-template-columns: 1fr 60px 100px 100px;
  gap: 8px;
  align-items: center;
  padding: 6px 0;
  font-size: .85rem;
  border-bottom: 1px solid #e2e8f0;
}
.detalhe-item:last-child { border-bottom: none; }
.di-nome { color: #222; }
.di-cat, .di-tam { color: #64748b; font-size: .85rem; text-align: center; }
.di-sub { color: #222; font-weight: 500; text-align: right; }

.detalhe-descricao {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed #e2e8f0;
  font-size: .83rem;
  color: #64748b;
  font-style: italic;
}

/* Modal */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }

.modal {
  background: #fff;
  border-radius: 10px;
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  margin: 16px;
  box-shadow: 0 8px 32px rgba(0,0,0,.18);
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px 14px;
  border-bottom: 1px solid #e2e8f0;
}
.modal-header h2 { font-size: 1rem; color: #222; }

.modal-close {
  background: none;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  color: #94a3b8;
  line-height: 1;
  padding: 0 4px;
}
.modal-close:hover { color: #222; }

.modal-body { padding: 20px 24px; }

.modal-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.modal-grid .col-span-2 { grid-column: span 2; }

.modal-lg { max-width: 700px; }
.modal-sm { max-width: 380px; }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 24px 18px;
  border-top: 1px solid #e2e8f0;
}

/* ── Mobile / Tablet ───────────────────────────────── */
@media (max-width: 768px) {
  main { margin: 16px auto; padding: 0 12px; }
  .card { padding: 16px; }

  /* Troca tabela por cards */
  .table-wrap { display: none; }
  /* Produtos: scroll horizontal */
  .produtos-grid {
    display: flex;
    flex-direction: row;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 6px;
  }
  .produto-card { min-width: 220px; flex-shrink: 0; }

  /* Vendas: lista vertical */
  #vendas-grid {
    flex-direction: column;
    overflow-x: unset;
    padding-bottom: 0;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    overflow: hidden;
    gap: 0;
  }

  /* Toolbar: botão ocupa a linha toda */
  .toolbar { gap: 6px; }
  .toolbar input { max-width: none; flex: 1 1 100%; order: 3; }
  .toolbar strong { order: 1; }
  .toolbar .total { order: 2; }
  .toolbar .btn   { order: 4; width: 100%; margin-left: 0; }

  /* Modal */
  .modal { margin: 10px; }
  .modal-body { padding: 16px; }
  .modal-header { padding: 14px 16px 12px; }
  .modal-footer { padding: 12px 16px 16px; flex-direction: column-reverse; }
  .modal-footer .btn { width: 100%; }
  .modal-grid { grid-template-columns: 1fr; }
  .modal-grid .col-span-2 { grid-column: span 1; }

  /* Item venda: produto em cima, preco/sub/remove embaixo */
  .item-linha {
    grid-template-columns: 1fr auto auto;
    grid-template-areas: "produto produto produto" "preco sub remove";
    padding-bottom: 10px;
    border-bottom: 1px solid #f1f5f9;
    margin-bottom: 10px;
  }
  .item-linha .ac-wrap         { grid-area: produto; }
  .item-linha .item-preco      { grid-area: preco; }
  .item-linha .item-sub        { grid-area: sub; align-self: center; }
  .item-linha .btn-remove-item { grid-area: remove; justify-self: end; align-self: center; font-size: 1.4rem; }

  /* Resumo venda */
  .venda-resumo { flex-direction: column; align-items: flex-start; gap: 12px; }
  .venda-total-display { align-self: flex-end; }
}
