:root {
  --color-primary: #0B1D32;
  --color-primary-light: #132D4A;
  --color-accent: #1A6A96;
  --color-accent-hover: #145678;
  --color-accent-soft: rgba(26, 106, 150, 0.16);
  --text-primary: #122033;
  --text-secondary: #5C6B7A;
  --text-muted: #66727F;
  --line: rgba(18, 32, 51, 0.10);
  --line-strong: rgba(18, 32, 51, 0.16);
  --color-gray-50: #F6F7F9;
  --color-gray-100: #EEF1F4;
  --color-gray-200: #E3E7EC;
  --color-gray-300: #CDD3DA;
  --color-gray-400: #66727F;
  --color-gray-500: #5C6B7A;
  --color-gray-600: #475569;
  --color-gray-800: #122033;
  --color-danger: #DC2626;
  --font-heading: "Source Serif 4", "Iowan Old Style", Palatino, Georgia, serif;
  --font-body: "Inter", system-ui, sans-serif;
  --radius: 16px;
  --shadow: 0 8px 24px rgba(11, 29, 50, 0.12);
  --ring: 0 0 0 4px var(--color-accent-soft);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
html, body { min-height: 100%; }
body {
  font-family: var(--font-body);
  color: var(--text-primary);
  background: var(--color-gray-50);
  line-height: 1.5;
  font-size: 16px;
}
img { max-width: 100%; display: block; }
button, input, select, textarea { font-family: inherit; font-size: 16px; color: var(--text-primary); }
input::placeholder, textarea::placeholder { color: var(--text-muted); opacity: 1; }
button { touch-action: manipulation; }

.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-weight: 500;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}
.field input, .field select, .field textarea,
.add-row input, .prod-search input, .desc-mini input, table.items input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  font-size: 16px;
  min-height: 44px;
  background: #fff;
  color: var(--text-primary);
}
.field textarea { min-height: 80px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus,
.add-row input:focus, .desc-mini input:focus, table.items input:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: var(--ring);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 8px 16px;
  min-height: 36px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}
.btn:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
.btn-primary {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #fff;
  min-height: 44px;
  padding: 12px 24px;
  font-weight: 600;
}
.btn-primary:hover { background: var(--color-accent-hover); border-color: var(--color-accent-hover); }
.btn-accent, .btn-ghost {
  background: #fff;
  color: var(--text-secondary);
  border-color: var(--line-strong);
}
.btn-accent:hover, .btn-ghost:hover { background: var(--color-gray-50); color: var(--text-primary); }
.btn-danger { background: #fee2e2; color: var(--color-danger); border-color: transparent; }

.app-header {
  background: var(--color-primary);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 20;
}
.app-header-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 16px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.app-header img { height: 32px; filter: brightness(0) invert(1); }
.user-meta { display: flex; align-items: center; gap: 12px; font-size: 13px; font-weight: 500; }
.header-link {
  color: #fff;
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 10px;
  padding: 8px 12px;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  background: transparent;
}
.header-link:hover { background: rgba(255, 255, 255, 0.12); }
.header-link:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.aviso-teste {
  background: #f59e0b;
  color: #0b1d32;
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 16px;
}
body.modo-teste { background: #fff7e6; }
body.modo-teste .app-header { box-shadow: inset 0 -3px 0 #f59e0b; }

.layout {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px 24px 32px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 24px;
  align-items: start;
}
.panel {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: none;
}
.panel h2 {
  font-family: var(--font-heading);
  font-weight: 560;
  font-size: 24px;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  margin: 32px 0 16px;
}
.panel h2:first-child { margin-top: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.hint { font-size: 13px; color: var(--text-muted); margin: 8px 0 0; line-height: 1.45; }
.mini {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.mini-field { display: block; min-width: 0; }
.mini-field input { width: 100%; }

.linha-switch {
  display: flex;
  gap: 4px;
  padding: 4px;
  width: fit-content;
  max-width: 100%;
  background: var(--color-gray-100);
  border-radius: 14px;
}
.chip {
  position: relative;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 16px;
  min-height: 36px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
  color: var(--text-secondary);
}
.chip input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.chip:has(input:focus-visible) { outline: 2px solid var(--color-accent); outline-offset: 2px; }
.chip.on {
  background: #fff;
  color: var(--color-accent);
  box-shadow: 0 1px 2px rgba(18, 32, 51, 0.1), 0 0 0 1px rgba(18, 32, 51, 0.06);
}
.add-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 120px auto;
  gap: 8px;
  margin-bottom: 16px;
  align-items: end;
}
.add-row.sem-valor { grid-template-columns: minmax(0, 1fr) 90px auto; }
.add-btn { min-height: 40px; white-space: nowrap; }
.prod-search-wrap { min-width: 0; }
.prod-search { position: relative; display: flex; min-width: 0; border-radius: 10px; }
.prod-search input {
  flex: 1;
  min-width: 0;
  border-radius: 10px 0 0 10px;
  border-right: 0;
}
.field .prod-search input { border-radius: 10px 0 0 10px; }
.prod-search input:focus { outline: none; border-color: var(--color-accent); box-shadow: none; z-index: 1; }
.prod-search:focus-within { box-shadow: var(--ring); }
.prod-search:focus-within input,
.prod-search:focus-within .btn-lupa { border-color: var(--color-accent); }
.btn-lupa {
  width: 44px;
  min-height: 44px;
  border: 1px solid var(--line-strong);
  border-radius: 0 10px 10px 0;
  background: #fff;
  color: var(--text-secondary);
  cursor: pointer;
  flex-shrink: 0;
  font-size: 24px;
  line-height: 1;
}
.btn-lupa:hover { background: var(--color-gray-50); color: var(--text-secondary); }
.btn-lupa:focus-visible { outline: 2px solid var(--color-accent); outline-offset: 2px; }
.prod-sugestoes {
  position: absolute;
  left: 0;
  right: 44px;
  top: calc(100% + 4px);
  max-height: 280px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  z-index: 15;
}
.prod-sugestao {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 8px 12px;
  min-height: 44px;
  border: 0;
  background: #fff;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-primary);
}
.prod-sugestao .prod-sugestao-txt { min-width: 0; }
.prod-sugestao img, .item-foto {
  width: 40px;
  height: 40px;
  object-fit: contain;
  flex-shrink: 0;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.prod-cell { display: flex; align-items: center; gap: 12px; }
.item-foto { width: 44px; height: 44px; }
.prod-sugestao small { display: block; color: var(--text-muted); font-size: 12px; }
.prod-sugestao small .doc { color: var(--text-primary); font-weight: 600; }
.prod-sugestao:hover, .prod-sugestao:focus-visible { background: var(--color-accent-soft); }
.prod-sugestao-vazia { padding: 12px 16px; color: var(--text-muted); font-size: 13px; }
.hidden { display: none !important; }

.table-wrap { overflow-x: auto; margin-bottom: 16px; }
table.items { width: 100%; border-collapse: collapse; font-size: 14px; }
table.items th {
  text-align: left;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  padding: 8px;
  border-bottom: 1px solid var(--line);
}
table.items td {
  padding: 8px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
  color: var(--text-primary);
}
table.items input { padding: 8px 12px; min-height: 40px; }
table.items .btn { min-height: 36px; padding: 8px 12px; font-size: 13px; }
.empty-row { text-align: center; color: var(--text-muted); padding: 24px 8px !important; font-size: 13px; }

.sticky-bar {
  position: static;
  margin-top: 16px;
  padding: 16px 0 0;
  background: transparent;
  border-top: 1px solid var(--line);
}
.actions-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.actions-row .btn-primary { min-width: 160px; }

#hist-hint { font-size: 11px; line-height: 1.3; }
.history-list { display: grid; gap: 16px; }
.hist-bloco { display: grid; gap: 8px; justify-items: start; }
.hist-bloco-titulo {
  margin: 0;
  font-family: var(--font-body);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
}
.history-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  font-size: 13px;
  background: #fff;
}
.hist-topo { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; }
.hist-entrega { color: var(--text-secondary); font-size: 12px; white-space: nowrap; }
.history-item .hist-nome {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  width: 100%;
  font-weight: 700;
  color: var(--text-primary);
}
.hist-det { color: var(--text-secondary); font-size: 12px; }
.hist-num { white-space: nowrap; }
.history-item .btn, .hist-mais {
  min-height: 36px;
  height: 36px;
  margin-top: 4px;
  padding: 0 12px;
  font-size: 13px;
}
.totals {
  width: max-content;
  margin: 0 0 16px auto;
  display: grid;
  justify-items: stretch;
  gap: 8px;
}
.desc-mini {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}
.desc-mini input { width: 108px; min-height: 36px; padding: 8px; }
.totals-row { display: flex; justify-content: space-between; color: var(--text-secondary); font-size: 13px; }
.totals-row.grand {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  width: 100%;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}
.col-total { white-space: nowrap; font-weight: 600; color: var(--text-primary); }

@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; padding: 16px 16px 96px; gap: 16px; }
  .panel { padding: 16px; }
  .grid-2 { grid-template-columns: 1fr; }
  .add-row { grid-template-columns: 1fr 1fr; }
  .add-row.sem-valor { grid-template-columns: 1fr 1fr; }
  .prod-search-wrap, .add-btn { grid-column: 1 / -1; }
  .linha-switch { width: 100%; }
  .chip { flex: 1 1 0; justify-content: center; text-align: center; }
  .chip, .btn, .btn-ghost, .btn-accent, .header-link, .add-btn,
  table.items input, .desc-mini input { min-height: 44px; }
  .app-header-inner { padding: 12px 16px; }
  .app-header img { height: 32px; }
  .prod-sugestoes { right: 0; max-height: min(280px, 45dvh); }
  .table-wrap { overflow: visible; }
  table.items thead { display: none; }
  table.items, table.items tbody { display: block; width: 100%; }
  table.items tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 12px;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 12px;
    margin-bottom: 12px;
  }
  table.items td {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 0;
    border: 0;
  }
  table.items td::before {
    content: attr(data-label);
    font-size: 12px;
    font-weight: 500;
    color: var(--text-muted);
  }
  table.items td:nth-child(2),
  table.items td.col-act { grid-column: 1 / -1; }
  table.items td.col-act::before { content: none; }
  table.items td.col-act .btn { width: 100%; min-height: 44px; }
  table.items .empty-row { grid-column: 1 / -1; }
  table.items .empty-row::before { content: none; }
  .sticky-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    box-shadow: 0 -8px 24px rgba(11, 29, 50, 0.1);
    z-index: 25;
  }
  .actions-row { width: 100%; }
  .actions-row .btn { flex: 1; }
  .history-item .btn, .hist-mais { min-height: 36px; height: 36px; width: auto; flex: none; }
}
