:root {
  --color-bg: #f3f4f6;
  --color-surface: #ffffff;
  --color-text: #111827;
  --color-text-muted: #6b7280;
  --color-border: #e5e7eb;
  --color-primary: #1d4ed8;
  --color-primary-dark: #1e3a8a;
  --color-success: #16a34a;
  --color-danger: #dc2626;
  --color-warning: #d97706;
  --radius: 10px;
  --tab-bar-h: 64px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { margin: 0 0 .5em; }
a { color: var(--color-primary); }
button { font-family: inherit; }

/* ---------------------------------------------------------------- layout */
#app-root { display: flex; min-height: 100vh; }

#sidebar {
  display: none;
  width: 220px;
  flex-shrink: 0;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  padding: 16px 8px;
}
#sidebar .brand { font-weight: 700; font-size: 1.1rem; padding: 8px 12px 20px; }
#sidebar nav a {
  display: block;
  padding: 12px 12px;
  border-radius: var(--radius);
  color: var(--color-text);
  text-decoration: none;
  font-size: .95rem;
  margin-bottom: 2px;
}
#sidebar nav a.active { background: var(--color-primary); color: #fff; }
#sidebar nav a:hover:not(.active) { background: var(--color-bg); }
#sidebar .user-box { margin-top: auto; padding: 12px; font-size: .85rem; color: var(--color-text-muted); }

#main-col { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#page-content { flex: 1; padding: 16px; padding-bottom: calc(var(--tab-bar-h) + 24px); max-width: 1100px; width: 100%; margin: 0 auto; }

#topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--color-surface); border-bottom: 1px solid var(--color-border);
}
#topbar .title { font-weight: 700; }
#topbar button.btn-logout { font-size: .85rem; }

#tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; height: var(--tab-bar-h);
  display: flex; background: var(--color-surface); border-top: 1px solid var(--color-border);
  z-index: 50;
}
#tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-decoration: none; color: var(--color-text-muted); font-size: .72rem; gap: 2px;
}
#tabbar a .icon { font-size: 1.3rem; }
#tabbar a.active { color: var(--color-primary); font-weight: 600; }

@media (min-width: 900px) {
  #sidebar { display: flex; flex-direction: column; }
  #tabbar { display: none; }
  #topbar { display: none; }
  #page-content { padding: 24px; padding-bottom: 24px; }
}

/* ---------------------------------------------------------------- buttons / forms */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: var(--radius);
  border: 1px solid var(--color-border); background: var(--color-surface);
  color: var(--color-text); font-size: .95rem; cursor: pointer;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--color-primary); border-color: var(--color-primary); color: #fff; }
.btn-secondary { background: var(--color-surface); }
.btn-danger { background: var(--color-danger); border-color: var(--color-danger); color: #fff; }
.btn-success { background: var(--color-success); border-color: var(--color-success); color: #fff; }
.btn-block { width: 100%; }
.btn-sm { min-height: 36px; padding: 0 12px; font-size: .85rem; }
.btn-icon { min-width: 44px; padding: 0; font-size: 1.1rem; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: .85rem; color: var(--color-text-muted); margin-bottom: 4px; }
.input, select.input, textarea.input {
  width: 100%; min-height: 44px; padding: 8px 12px; border-radius: var(--radius);
  border: 1px solid var(--color-border); background: var(--color-surface); font-size: 1rem; color: var(--color-text);
}
textarea.input { min-height: 80px; }
.input:focus, select.input:focus, textarea.input:focus { outline: 2px solid var(--color-primary); outline-offset: 1px; }
.input-error { outline: 2px solid var(--color-danger); }
.form-row { display: flex; gap: 12px; flex-wrap: wrap; }
.form-row > .field { flex: 1; min-width: 160px; }
.hint { font-size: .8rem; color: var(--color-text-muted); margin-top: 4px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; }
.checkbox-row input { width: 20px; height: 20px; }

/* ---------------------------------------------------------------- search / cards */
.search-box { display: flex; gap: 8px; margin-bottom: 12px; }
.search-box .input { flex: 1; font-size: 1.1rem; }

.card {
  background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius);
  padding: 14px; margin-bottom: 10px;
}
.card-title { font-weight: 700; font-size: 1.05rem; margin-bottom: 2px; }
.card-sub { font-size: .85rem; color: var(--color-text-muted); margin-bottom: 8px; }
.card-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: .8rem; font-weight: 600; }
.badge-danger { background: #fee2e2; color: var(--color-danger); }
.badge-grey { background: #f3f4f6; color: var(--color-text-muted); }
.badge-ok { background: #dcfce7; color: var(--color-success); }

.pill-local {
  display: inline-flex; align-items: center; gap: 6px; background: #eef2ff; color: #3730a3;
  border-radius: 999px; padding: 4px 10px; font-size: .82rem; margin: 2px 4px 2px 0;
}
.pill-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; border: 1px solid rgba(0,0,0,.15); }
.pill-qtd { font-weight: 700; }

.pills-wrap { display: flex; flex-wrap: wrap; }

/* ---------------------------------------------------------------- toasts */
#toast-container {
  position: fixed; bottom: calc(var(--tab-bar-h) + 12px); left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; z-index: 300; width: min(92vw, 420px); align-items: center;
}
@media (min-width: 900px) { #toast-container { bottom: 20px; } }
.toast {
  background: #1f2937; color: #fff; padding: 10px 16px; border-radius: var(--radius);
  font-size: .9rem; opacity: 0; transform: translateY(10px); transition: opacity .2s, transform .2s;
  width: 100%; text-align: center; box-shadow: 0 4px 12px rgba(0,0,0,.2);
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast-error { background: var(--color-danger); }
.toast-success { background: var(--color-success); }
.toast-info { background: #1f2937; }

/* ---------------------------------------------------------------- modal */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center;
  z-index: 200; padding: 16px;
}
.modal-box {
  background: var(--color-surface); border-radius: var(--radius); padding: 20px; width: min(92vw, 480px);
  max-height: 86vh; overflow: auto;
}
.modal-message { color: var(--color-text); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
body.modal-open { overflow: hidden; }

/* ---------------------------------------------------------------- loading */
#loading-overlay {
  position: fixed; inset: 0; background: rgba(255,255,255,.6); display: flex; align-items: center; justify-content: center;
  z-index: 400;
}
.spinner { width: 40px; height: 40px; border: 4px solid var(--color-border); border-top-color: var(--color-primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------------------------------------------------------- offline banner */
#offline-banner {
  position: fixed; top: 0; left: 0; right: 0; background: var(--color-warning); color: #fff;
  text-align: center; font-size: .85rem; padding: 6px; transform: translateY(-100%); transition: transform .2s; z-index: 500;
}
#offline-banner.show { transform: translateY(0); }

/* ---------------------------------------------------------------- camera scanner */
.camera-scanner { padding: 0; }
.camera-scanner-header { display: flex; align-items: center; gap: 8px; padding: 10px 14px; }
.camera-scanner-header strong { flex: 1; }
#camera-scanner-view { width: 100%; }
.camera-scanner-msg { padding: 0 14px 14px; color: var(--color-text-muted); font-size: .9rem; }

/* ---------------------------------------------------------------- tables */
table.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.table th, table.table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--color-border); }
table.table th { color: var(--color-text-muted); font-weight: 600; font-size: .8rem; text-transform: uppercase; }
.table-wrap { overflow-x: auto; }

/* ---------------------------------------------------------------- misc */
.section-title { font-size: 1.1rem; font-weight: 700; margin: 20px 0 10px; }
.empty-state { text-align: center; padding: 40px 16px; color: var(--color-text-muted); }
.login-wrap { max-width: 360px; margin: 10vh auto; padding: 24px; }
.login-wrap .logo { text-align: center; font-size: 1.4rem; font-weight: 700; margin-bottom: 20px; }
.tag-select { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.tag-select button { border-radius: 999px; border: 1px solid var(--color-border); background: var(--color-surface); padding: 4px 12px; font-size: .82rem; cursor: pointer; }
.tag-select button.active { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }
.banner-local {
  background: #eef2ff; border: 1px solid #c7d2fe; border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
.line-ok { border-left: 4px solid var(--color-success); }
.line-error { border-left: 4px solid var(--color-danger); }
.line-warning { border-left: 4px solid var(--color-warning); }
.inline-warning { color: var(--color-warning); font-size: .82rem; margin-top: 4px; }

/* ---------------------------------------------------------------- print / labels */
#print-area { display: none; }
@media print {
  body > *:not(#print-area) { display: none !important; }
  #print-area { display: block !important; }
}
.print-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--label-w));
  margin-top: var(--offset-top);
  margin-left: var(--offset-left);
}
.print-thermal { display: block; }
.print-thermal .label-cell { width: var(--label-w); height: var(--label-h); page-break-after: always; }
.label-cell {
  width: var(--label-w); height: var(--label-h); overflow: hidden; padding: 2mm;
  display: flex; align-items: center; justify-content: center;
}
.label-cell-empty { visibility: hidden; }
.label-cell-test { border: 1px dashed #999; font-size: 8px; color: #999; align-items: flex-start; justify-content: flex-start; }
.label-item, .label-local { width: 100%; text-align: center; }
.label-barcode { width: 90%; height: auto; max-height: 40%; }
.label-codigo { font-weight: 700; font-size: 11px; }
.label-descricao {
  font-size: 9px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.label-rodape { font-size: 8px; color: #444; display: flex; justify-content: center; gap: 6px; }
.label-local-codigo { font-weight: 800; font-size: 16px; }
.label-local-sub { font-size: 8px; }
.label-stripe { display: block; width: 100%; height: 3mm; margin-bottom: 1mm; }
