/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #00593b;
  --primary-light: #007a52;
  --accent: #00593b;
  --danger: #e53935;
  --warning: #ff9800;
  --bg: #f5f5f5;
  --card: #ffffff;
  --text: #212121;
  --text-light: #757575;
  --border: #e0e0e0;
  --radius: 12px;
}

html { font-size: 16px; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

/* === Header === */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--primary);
  color: white;
  padding: 0.75rem 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
}

.app-header h1 {
  font-size: 1.1rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.app-header h1 img.header-logo {
  height: 28px;
  filter: brightness(0) invert(1);
  margin-right: 0.3rem;
}

.badge-online {
  background: #ececc8;
  color: #00593b;
  font-size: 0.65rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-weight: 700;
  text-transform: uppercase;
  vertical-align: middle;
  margin-left: 0.2rem;
  transition: background 0.3s, color 0.3s;
}

.badge-offline {
  background: var(--danger);
  color: #fff;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.status-indicator {
  font-size: 0.75rem;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  font-weight: 600;
}

.status-indicator.online { background: var(--accent); color: #000; }
.status-indicator.offline { background: var(--danger); color: #fff; }

#sync-badge {
  background: var(--warning);
  color: #000;
  font-size: 0.7rem;
  padding: 0.15rem 0.4rem;
  border-radius: 999px;
  font-weight: 700;
  display: none;
}

#user-name { font-size: 0.85rem; }

.sync-db-indicator {
  font-size: 0.65rem;
  padding: 0.15rem 0.4rem;
  border-radius: 999px;
  font-weight: 600;
  cursor: pointer;
  background: rgba(255,255,255,0.2);
  color: #fff;
  white-space: nowrap;
}

.btn-logout {
  background: rgba(255,255,255,0.2);
  border: none;
  color: white;
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}

/* === Auth === */
.auth-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  min-height: 100vh;
  padding: 1rem;
}

.auth-card {
  background: var(--card);
  padding: 2rem;
  border-radius: var(--radius);
  box-shadow: 0 4px 24px rgba(0,0,0,0.1);
  width: 100%;
  max-width: 400px;
}

.auth-card h1 {
  color: var(--primary);
  margin-bottom: 0.25rem;
  font-size: 1.5rem;
}

.auth-subtitle {
  color: var(--text-light);
  margin-bottom: 1.5rem;
}

.auth-link {
  text-align: center;
  margin-top: 1rem;
  color: var(--text-light);
}

.auth-link a { color: var(--primary); text-decoration: none; font-weight: 600; }

/* === Subheader / Nav bar === */
.page-nav {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 1rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 48px; /* below header */
  z-index: 90;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

.page-nav .btn-back {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.page-nav .btn-back:active {
  background: var(--bg);
}

.page-nav .page-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

/* === iPad / Touch fixes === */
button, .btn, a, select, input[type="submit"] {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

@supports (-webkit-touch-callout: none) {
  /* iOS button press fix */
  .btn, button {
    cursor: pointer;
    -webkit-appearance: none;
  }
  input, select, textarea {
    font-size: 16px; /* prevents zoom on focus on iOS */
  }
}

/* === Tempo atendimento === */
.tempo-badge {
  font-size: 0.7rem;
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  font-weight: 600;
  white-space: nowrap;
}
.tempo-rapido { background: #e8f5e9; color: #2e7d32; }
.tempo-medio { background: #fff3e0; color: #e65100; }
.tempo-lento { background: #ffebee; color: #c62828; }

/* === Forms === */
.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.3rem;
  font-size: 0.9rem;
  color: var(--text);
}

input, select, textarea {
  width: 100%;
  padding: 0.7rem 0.8rem;
  border: 2px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  transition: border-color 0.2s;
  background: #fff;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary-light);
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.7rem 1.2rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
  font-family: inherit;
}

.btn:active { opacity: 0.8; }
.btn-primary { background: var(--primary); color: white; }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-full { width: 100%; }
.btn-large { padding: 1rem; font-size: 1.1rem; }
.btn-small { padding: 0.4rem 0.7rem; font-size: 0.85rem; }

.error-msg { color: var(--danger); padding: 0.5rem 0; font-size: 0.9rem; }
.success-msg { color: var(--accent); padding: 0.5rem 0; font-size: 0.9rem; font-weight: 600; }

/* === Dashboard === */
.dashboard { padding: 1rem; }

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--card);
  padding: 1.2rem;
  border-radius: var(--radius);
  text-align: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  border-left: 4px solid var(--primary);
}

.stat-card.stat-pendente { border-left-color: var(--warning); }
.stat-card.stat-entregue { border-left-color: var(--accent); }
.stat-card.stat-offline { border-left-color: var(--danger); }

.stat-card { transition: transform 0.15s, box-shadow 0.15s; }
.stat-card[style*="cursor"]:active { transform: scale(0.96); box-shadow: 0 2px 4px rgba(0,0,0,0.15); }

.stat-number { font-size: 2rem; font-weight: 700; color: var(--primary); }
.stat-label { font-size: 0.85rem; color: var(--text-light); margin-top: 0.2rem; }

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* === Form Page === */
.form-page, .detail-page {
  padding: 1rem;
  max-width: 600px;
  margin: 0 auto;
}

.list-page {
  padding: 1rem;
  max-width: 1200px;
  margin: 0 auto;
}

.form-page h2, .list-page h2, .detail-page h2 {
  margin-bottom: 1rem;
  color: var(--primary);
}

.form-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1rem;
}

.form-actions .btn { flex: 1; }

/* === Caixas === */
.caixa-add-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}

.caixa-add-row input:first-of-type { flex: 1; min-width: 120px; }
.caixa-add-row input:nth-of-type(2) { flex: 2; min-width: 120px; }

.caixas-list {
  list-style: none;
}

.caixas-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.75rem;
  background: var(--bg);
  border-radius: 6px;
  margin-bottom: 0.3rem;
  font-size: 0.9rem;
}

.btn-remove-caixa {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0 0.3rem;
}

/* === Signature === */
.signature-container {
  position: relative;
}

.signature-container canvas {
  width: 100%;
  height: 200px;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: #fff;
  touch-action: none;
}

.btn-clear-sig {
  position: absolute;
  top: 0.3rem;
  right: 0.3rem;
  background: rgba(0,0,0,0.05);
}

/* === List === */
.filters {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

@media (min-width: 600px) {
  .filters {
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 900px) {
  .filters {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

.filters input, .filters select {
  width: 100%;
  min-width: 0;
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.filter-group label {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-light);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.protocolos-list { display: flex; flex-direction: column; gap: 0.75rem; }

.protocolo-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  cursor: pointer;
  transition: box-shadow 0.2s;
}

.protocolo-card:active { box-shadow: 0 1px 4px rgba(0,0,0,0.1); }

.proto-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.4rem;
}

.proto-numero { font-weight: 700; color: var(--primary); }

.proto-status {
  font-size: 0.75rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-weight: 600;
  text-transform: uppercase;
}

.status-pendente { background: #fff3e0; color: #e65100; }
.status-entregue { background: #e8f5e9; color: #2e7d32; }
.status-confirmado { background: #e3f2fd; color: #1565c0; }

.proto-info {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 0.3rem;
  font-size: 0.9rem;
}

.tipo-entrega { color: var(--primary-light); }
.tipo-transferencia { color: #7b1fa2; }

.proto-footer {
  display: flex;
  gap: 0.75rem;
  font-size: 0.8rem;
  color: var(--text-light);
}

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

/* === Detail === */
.detail-grid {
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 1rem;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--bg);
}

.detail-row.full { flex-direction: column; gap: 0.3rem; }
.detail-label { color: var(--text-light); font-size: 0.85rem; }
.detail-value { font-weight: 600; }

.detail-page h3 { margin: 1rem 0 0.5rem; color: var(--text); font-size: 1rem; }

.caixas-detail-list {
  list-style: none;
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
}

.caixas-detail-list li {
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--bg);
}

.sig-img {
  width: 100%;
  max-width: 400px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}

/* === Barcode Scanner === */
#barcode-scanner-container {
  margin: 0.5rem 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#btn-scan-barcode {
  white-space: nowrap;
  min-width: 40px;
  font-size: 1.1rem;
  padding: 0.4rem 0.6rem;
}

/* === Foto Documento === */
.foto-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.foto-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  width: 100%;
  max-width: 400px;
  padding: 1.5rem 1rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: #fafafa;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.foto-placeholder:active {
  border-color: var(--primary);
  background: #f0f0f0;
}

.foto-placeholder span {
  font-size: 0.85rem;
  color: var(--text-light);
  text-align: center;
}

.foto-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: center;
}

/* === Auth Logo === */
.auth-logo {
  display: block;
  max-width: 180px;
  margin: 0 auto 1rem;
}

/* === Admin Panel === */
.admin-page {
  padding: 1rem;
  max-width: 700px;
  margin: 0 auto;
}

.admin-page h2 {
  margin-bottom: 1rem;
  color: var(--primary);
}

.user-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 0.75rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.user-info {
  flex: 1;
  min-width: 200px;
}

.user-info .user-nome {
  font-weight: 700;
  color: var(--text);
}

.user-info .user-email {
  font-size: 0.85rem;
  color: var(--text-light);
}

.user-info .user-role {
  font-size: 0.75rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-weight: 600;
  text-transform: uppercase;
}

.role-admin { background: #e8f5e9; color: #2e7d32; }
.role-operador { background: #e3f2fd; color: #1565c0; }

.user-status-ativo { color: var(--accent); font-weight: 600; font-size: 0.8rem; }
.user-status-inativo { color: var(--danger); font-weight: 600; font-size: 0.8rem; }

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

.user-actions .btn {
  font-size: 0.8rem;
  padding: 0.4rem 0.7rem;
}

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

.admin-section {
  margin-bottom: 1.5rem;
}

.admin-section h3 {
  margin-bottom: 0.75rem;
  color: var(--text);
  font-size: 1rem;
  border-bottom: 2px solid var(--primary);
  padding-bottom: 0.3rem;
}

.pending-badge {
  background: var(--danger);
  color: white;
  font-size: 0.7rem;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  font-weight: 700;
  margin-left: 0.3rem;
}

/* === Relatórios === */
.rel-table {
  width: 100%;
  min-width: 700px;
  border-collapse: collapse;
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: 0.82rem;
}

.rel-table th {
  background: var(--primary);
  color: white;
  padding: 0.5rem 0.6rem;
  text-align: left;
  font-weight: 600;
  white-space: nowrap;
  font-size: 0.75rem;
}

.rel-table td {
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid var(--bg);
  white-space: nowrap;
}

.rel-table td:nth-child(2) {
  white-space: normal;
  max-width: 200px;
}

.rel-table tbody tr:hover {
  background: #f5f5f5;
}

.rel-table tbody tr:active {
  background: #eeeeee;
}

/* === Utilities === */
.loading, .empty {
  text-align: center;
  padding: 2rem;
  color: var(--text-light);
}

/* === Tablet optimizations === */
@media (min-width: 600px) {
  .form-page, .detail-page {
    max-width: 700px;
    padding: 1.5rem;
  }

  .list-page {
    padding: 1.5rem;
  }

  .caixa-add-row input:first-of-type { flex: 1; }
  .caixa-add-row input:nth-of-type(2) { flex: 2; }
}

@media (min-width: 900px) {
  .stats-grid { grid-template-columns: repeat(4, 1fr); }
  .action-buttons { flex-direction: row; }
  .action-buttons .btn { flex: 1; }
}
