:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #dfe2e8;
  --text: #1c1f26;
  --muted: #6b7280;
  --accent: #3366ff;
  --danger: #e0364a;
  --header-bg: #0b0c0f;
  --header-border: #23252b;
  --header-text: #f5f6f8;
  --header-muted: #8b8f99;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  background: var(--header-bg);
  padding: 1rem 2rem;
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "title nav logout";
  align-items: center;
  gap: 0.75rem 1.5rem;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.04), 0 8px 24px rgba(0, 0, 0, 0.25);
}

header h1 {
  grid-area: title;
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--header-text);
  white-space: nowrap;
}

nav {
  grid-area: nav;
  display: flex;
  gap: 0.4rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

nav::-webkit-scrollbar { display: none; }

.header-actions {
  grid-area: logout;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--header-muted);
  border: 1px solid rgba(255, 255, 255, 0.15);
  font-size: 0.85rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease;
}

.btn-icon:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--header-text);
  opacity: 1;
}

.tab-btn {
  background: transparent;
  color: var(--header-muted);
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.45rem 1rem;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease;
}

.tab-btn:hover {
  background: rgba(255, 255, 255, 0.06);
  color: var(--header-text);
}

.tab-btn.active {
  background: var(--accent);
  color: white;
}

.tab-btn.active:hover {
  background: var(--accent);
}

.btn-logout {
  background: transparent;
  color: var(--danger);
  border: 1px solid rgba(224, 54, 74, 0.5);
  border-radius: 999px;
  padding: 0.45rem 1.1rem;
  font-size: 0.85rem;
  font-weight: 500;
  transition: background 0.15s ease, color 0.15s ease;
}

.btn-logout:hover {
  background: var(--danger);
  color: white;
  opacity: 1;
}

main { padding: 2rem; max-width: 1100px; margin: 0 auto; }

.tab { display: none; }
.tab.active { display: block; }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.card-alert .card-value { color: var(--danger); }

.card-label { color: var(--muted); font-size: 0.85rem; }
.card-value { font-size: 1.8rem; font-weight: 600; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.2rem;
  margin-bottom: 1.5rem;
}

.panel h2 { margin-top: 0; font-size: 1.1rem; }

.panel p { line-height: 1.5; color: var(--text); }
.panel > p:last-child { margin-bottom: 0; }

.changelog {
  margin: 0;
  padding-left: 1.2rem;
  line-height: 1.6;
}

.changelog li { margin-bottom: 0.5rem; }
.changelog li:last-child { margin-bottom: 0; }

.faq {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.9rem;
  margin-bottom: 0.6rem;
}

.faq:last-child { margin-bottom: 0; }

.faq summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 0.95rem;
  list-style: none;
}

.faq summary::-webkit-details-marker { display: none; }

.faq summary::before {
  content: "+";
  display: inline-block;
  width: 1.1rem;
  color: var(--accent);
  font-weight: 700;
}

.faq[open] summary::before { content: "−"; }

.faq-body {
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border);
  font-size: 0.9rem;
  line-height: 1.55;
  color: var(--text);
}

.faq-body p { margin: 0 0 0.6rem; }
.faq-body p:last-child { margin-bottom: 0; }
.faq-body ul,
.faq-body ol {
  margin: 0 0 0.6rem;
  padding-left: 1.3rem;
}
.faq-body li { margin-bottom: 0.3rem; }

.panel-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.8rem;
  flex-wrap: wrap;
}

.panel-header h2 { margin: 0; }

.panel-header button:last-child { margin-left: auto; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.6rem;
  align-items: center;
}

#form-produto {
  display: flex;
  flex-wrap: nowrap;
  align-items: flex-end;
  gap: 0.6rem;
}

#form-produto .field {
  flex: 1 1 0;
  min-width: 0;
}

#form-produto button[type="submit"] {
  flex-shrink: 0;
}

input, select, button {
  background: #ffffff;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.6rem;
  font-size: 0.9rem;
}

.input-error {
  border-color: var(--danger);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.field label {
  font-size: 0.78rem;
  color: var(--muted);
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type="number"] {
  -moz-appearance: textfield;
}

button {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  cursor: pointer;
}

button:hover { opacity: 0.9; }

.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

th, td {
  text-align: left;
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--border);
}

th { color: var(--muted); font-weight: 500; }

.row-clickable { cursor: pointer; }
.row-clickable:hover { background: var(--bg); }

.bulk-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  margin-bottom: 0.8rem;
  font-size: 0.85rem;
}

.bulk-actions[hidden] {
  display: none;
}

.bulk-actions #bulk-count {
  color: var(--muted);
  margin-right: auto;
}

td .edit-nome,
td .edit-minimo {
  width: 100%;
  padding: 0.3rem 0.4rem;
  font-size: 0.85rem;
}

.btn-excluir {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
}

.btn-excluir:hover {
  background: var(--danger);
  color: white;
  opacity: 1;
}

.toast-container {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  max-width: min(340px, calc(100vw - 2rem));
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-left: 4px solid #1f9d5f;
  border-radius: 8px;
  padding: 0.75rem 0.9rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  font-size: 0.88rem;
  color: var(--text);
  animation: toast-in 0.2s ease-out;
}

.toast.toast-error { border-left-color: var(--danger); }

.toast-fechar {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1;
  padding: 0;
  margin-left: auto;
  cursor: pointer;
  flex-shrink: 0;
}

.toast-fechar:hover { color: var(--text); opacity: 1; }

.toast.toast-saindo {
  animation: toast-out 0.2s ease-in forwards;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(-8px); }
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.5rem;
  width: 100%;
  max-width: 340px;
}

#novo-produto-modal .modal-box {
  max-width: 420px;
}

.modal-box h2 {
  margin: 0 0 1rem;
  font-size: 1.05rem;
}

.modal-box p {
  margin: 0 0 1.2rem;
  font-size: 0.95rem;
}


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

.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: white;
}

.btn-cancel {
  background: transparent;
  color: var(--danger);
  border: 1px solid rgba(224, 54, 74, 0.5);
}

.btn-cancel:hover {
  background: var(--danger);
  color: white;
  opacity: 1;
}

.btn-confirm {
  background: #1f9d5f;
  border-color: #1f9d5f;
  color: white;
}

.msg { min-height: 1.2rem; font-size: 0.85rem; }
.msg.error { color: var(--danger); }
.msg.ok { color: #4fd18c; }

.status-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.status-ativo { background: #4fd18c; }
.status-inativo { background: var(--danger); }

.tipo-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.7rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.tipo-badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.tipo-entrada {
  background: rgba(79, 209, 140, 0.15);
  color: #1f9d5f;
}

.tipo-saida {
  background: rgba(224, 54, 74, 0.12);
  color: var(--danger);
}

.tipo-balanco {
  background: rgba(245, 197, 66, 0.18);
  color: #b8860b;
}

.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.login-screen[hidden] {
  display: none;
}

.login-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 2rem;
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.login-box h1 {
  margin: 0 0 0.5rem;
  font-size: 1.2rem;
  text-align: center;
}

.header-user {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  margin-top: 0.8rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.autocomplete { position: relative; }

.autocomplete-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 10;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-top: 0.2rem;
  max-height: 220px;
  overflow-y: auto;
}

.autocomplete-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.5rem 0.6rem;
  cursor: pointer;
  font-size: 0.85rem;
}

.autocomplete-nome {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.autocomplete-estoque {
  flex-shrink: 0;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  background: var(--bg);
  border-radius: 999px;
  padding: 0.15rem 0.5rem;
}

.autocomplete-item:hover,
.autocomplete-item.active {
  background: var(--bg);
}

.autocomplete-empty {
  padding: 0.5rem 0.6rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.autocomplete-novo {
  padding: 0.5rem 0.6rem;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--accent);
  font-weight: 600;
}

/* ---------- Mobile-first ---------- */

@media (max-width: 700px) {
  main { padding: 1rem; }

  header {
    padding: 0.85rem 1rem;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "title logout"
      "nav nav";
    gap: 0.6rem 0.75rem;
  }

  header h1 { font-size: 0.95rem; }

  .panel { padding: 1rem; }

  .cards { gap: 0.6rem; }
  .card { padding: 0.9rem; }
  .card-value { font-size: 1.5rem; }

  .panel-header { gap: 0.6rem; }
  .panel-header input,
  .panel-header select {
    flex: 1 1 100%;
  }
  .panel-header button:last-child { margin-left: 0; }
  .panel-header label { flex: 1 1 100%; }

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

  #form-produto {
    flex-wrap: wrap;
  }

  #form-produto .field {
    flex: 1 1 100%;
  }

  input, select, button {
    font-size: 16px; /* evita zoom automático no iOS ao focar */
    padding: 0.65rem 0.75rem;
  }

  button { min-height: 44px; }

  .bulk-actions { flex-wrap: wrap; }
  .bulk-actions button { flex: 1 1 auto; }

  .modal-overlay { padding: 1rem; }
  .login-screen { padding: 1rem; }
  .login-box { padding: 1.5rem; }

  .modal-actions { flex-wrap: wrap; }
  .modal-actions button { flex: 1 1 auto; }

  /* Tabelas viram listas de cards em telas estreitas */
  table thead { display: none; }

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

  table tr {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.6rem 0.8rem;
    margin-bottom: 0.6rem;
    background: var(--panel);
  }

  table tr:last-child { margin-bottom: 0; }

  table td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.4rem 0;
    border-bottom: 1px dashed var(--border);
    text-align: right;
  }

  table td:last-child { border-bottom: none; }

  table td[data-label]::before {
    content: attr(data-label);
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--muted);
    text-align: left;
    margin-right: auto;
  }

  table td.cell-plain,
  table td.cell-actions {
    justify-content: flex-end;
    border-bottom: none;
  }

  table td.cell-actions {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  table td .edit-nome,
  table td .edit-minimo {
    max-width: 60%;
    margin-left: auto;
  }

  .row-clickable:hover { background: var(--panel); }

  .autocomplete-item,
  .autocomplete-novo,
  .autocomplete-empty {
    padding: 0.75rem 0.6rem;
    font-size: 0.9rem;
  }

  .tab-btn { padding: 0.55rem 0.9rem; }
}

.autocomplete-novo:hover {
  background: var(--bg);
}
