:root {
  --bg: #f4f2ed;
  --panel: #ffffff;
  --ink: #242721;
  --muted: #696f66;
  --line: #ddd5c7;
  --brand: #1f493b;
  --brand-strong: #15362c;
  --brand-soft: #e8f0eb;
  --accent: #e47f25;
  --accent-strong: #b95f13;
  --accent-soft: #fff2e4;
  --surface: #fbfaf6;
  --surface-strong: #f1eee6;
  --danger: #b42318;
  --success: #287052;
  --shadow: 0 14px 34px rgba(49, 44, 35, 0.09);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  background:
    linear-gradient(180deg, rgba(31, 73, 59, 0.13), rgba(31, 73, 59, 0) 235px),
    linear-gradient(90deg, rgba(228, 127, 37, 0.05), rgba(228, 127, 37, 0) 34%),
    var(--bg);
  color: var(--ink);
}

body.tv-mode {
  background: #151715;
  color: #f7f4ef;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 0;
  background: var(--brand);
  color: white;
  min-height: 44px;
  padding: 11px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 700;
  box-shadow: 0 7px 18px rgba(31, 73, 59, 0.14);
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
}

button:hover {
  background: var(--brand-strong);
  box-shadow: 0 9px 22px rgba(31, 73, 59, 0.2);
  transform: translateY(-1px);
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.shell {
  width: min(1240px, 100%);
  margin: 0 auto;
  padding: 20px 22px 32px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  padding: 17px 18px;
  margin-bottom: 16px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand-logo {
  width: 164px;
  height: 66px;
  object-fit: contain;
  background: white;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
}

.brand-kicker {
  display: inline-flex;
  width: fit-content;
  margin-bottom: 3px;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--brand-soft);
  color: var(--brand);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
}

h1,
h2 {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.08;
  color: var(--ink);
}

h2 {
  font-size: 21px;
  margin-bottom: 18px;
}

h3 {
  margin: 0 0 12px;
  font-size: 16px;
}

p {
  margin: 6px 0 0;
  color: var(--muted);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.hidden {
  display: none !important;
}

label {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.form-hint {
  margin: 4px 0 14px;
  color: var(--muted);
  font-size: 14px;
}

input,
select {
  width: 100%;
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 11px 12px;
  color: var(--ink);
  background: white;
  font-weight: 500;
}

input:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(228, 127, 37, 0.18);
  outline: none;
}

select.native-hidden {
  display: none;
}

.custom-select {
  position: relative;
  width: 100%;
}

.custom-select-button {
  width: 100%;
  min-height: 46px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 11px 12px;
  background: white;
  color: var(--ink);
  text-align: left;
  font-weight: 600;
  box-shadow: none;
}

.custom-select-button:hover,
.custom-select.open .custom-select-button {
  background: white;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(228, 127, 37, 0.18);
  color: var(--ink);
  transform: none;
}

.custom-select-button::after {
  content: "";
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  margin-top: -4px;
  flex: 0 0 auto;
}

.custom-select-options {
  position: absolute;
  z-index: 30;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  max-height: 240px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  box-shadow: 0 18px 38px rgba(49, 44, 35, 0.18);
}

.custom-select-option {
  display: block;
  width: 100%;
  min-height: 38px;
  border: 0;
  border-radius: 0;
  padding: 9px 12px;
  background: white;
  color: var(--ink);
  text-align: left;
  font-weight: 400;
}

.custom-select-option:hover,
.custom-select-option.active {
  background: var(--brand-soft);
  color: var(--ink);
}

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.tabs {
  display: flex;
  gap: 7px;
  margin-bottom: 16px;
  overflow-x: auto;
  padding: 0 2px 7px;
  scrollbar-gutter: stable;
}

.tab {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 104px;
  max-width: 132px;
  height: 46px;
  padding: 0 14px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  color: var(--ink);
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: 0 5px 16px rgba(49, 44, 35, 0.06);
}

.tab.active {
  background: var(--brand);
  color: white;
  border-color: var(--brand);
  box-shadow: inset 0 -3px 0 var(--accent), 0 8px 22px rgba(31, 73, 59, 0.2);
}

.tab:hover {
  border-color: var(--accent);
}

.icon-button {
  min-width: 72px;
}

.message {
  min-height: 24px;
  font-weight: 700;
}

.message:not(:empty) {
  width: fit-content;
  max-width: 100%;
  margin-top: 10px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--brand-soft);
  color: var(--brand);
}

.message.error:not(:empty) {
  background: #fff0ee;
  color: var(--danger);
}

.message.success:not(:empty) {
  background: var(--brand-soft);
  color: var(--brand);
}

.summary {
  display: flex;
  gap: 8px;
  align-items: center;
  margin: 16px 0;
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-strong);
}

.summary strong {
  color: var(--brand);
}

.section-heading {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: start;
  margin-bottom: 16px;
}

.section-heading p {
  margin-top: 4px;
}

.toolbar {
  display: flex;
  align-items: end;
  gap: 12px;
  margin-bottom: 16px;
}

.toolbar label {
  width: min(240px, 100%);
  margin-bottom: 0;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.metric {
  padding: 15px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, #ffffff, var(--surface));
  min-height: 96px;
  box-shadow: 0 8px 18px rgba(49, 44, 35, 0.05);
}

.metric strong {
  display: block;
  font-size: 32px;
  line-height: 1.1;
  color: var(--brand);
}

.metric span {
  display: block;
  margin-top: 6px;
  color: var(--muted);
}

.operation-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin: 0 0 18px;
}

.operation-strip.compact {
  grid-template-columns: minmax(0, 1fr);
}

.operation-card {
  display: grid;
  gap: 5px;
  min-height: 102px;
  padding: 15px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, #ffffff, var(--surface));
  box-shadow: 0 8px 18px rgba(49, 44, 35, 0.05);
}

.operation-card.primary {
  border-color: rgba(31, 73, 59, 0.25);
  background: linear-gradient(180deg, #fbfffb, var(--brand-soft));
}

.operation-card span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.operation-card strong {
  color: var(--brand);
  font-size: 26px;
  line-height: 1.05;
  overflow-wrap: anywhere;
}

.operation-card small {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.packing-current-card {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) auto auto;
  align-items: center;
  gap: 12px;
  margin: 10px 0 14px;
  padding: 14px;
  border: 1px solid rgba(31, 73, 59, 0.24);
  border-radius: 8px;
  background: linear-gradient(180deg, #f8fbf7, var(--brand-soft));
}

.packing-current-card > span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.packing-current-card > strong {
  font-size: 22px;
  color: var(--brand);
}

.packing-counter {
  min-width: 150px;
  text-align: right;
}

.packing-counter b {
  display: block;
  color: var(--brand);
  font-size: 32px;
  line-height: 1;
}

.packing-counter small {
  display: block;
  margin-top: 4px;
  color: var(--muted);
}

.report-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 18px;
}

.fixed-alert-card {
  margin: 16px 0;
}

.age-bucket-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}

.age-bucket {
  display: grid;
  gap: 7px;
  min-height: 92px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  text-align: left;
  box-shadow: 0 8px 18px rgba(49, 44, 35, 0.05);
}

.age-bucket strong,
.age-bucket span {
  color: inherit;
}

.age-bucket span {
  font-size: 13px;
}

.age-bucket i {
  display: block;
  height: 7px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--line);
}

.age-bucket b {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--brand);
}

.age-bucket.active {
  border-color: var(--brand);
  box-shadow: 0 0 0 2px rgba(31, 66, 54, 0.12);
}

.age-bucket.alert {
  border-color: rgba(231, 133, 44, 0.65);
  background: var(--accent-soft);
}

.age-bucket.alert b {
  background: var(--accent);
}

.report-section {
  display: grid;
  align-content: start;
  gap: 4px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, #ffffff, var(--surface));
}

.report-section.wide {
  grid-row: span 2;
}

.details-block {
  margin-top: 4px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.details-block summary {
  cursor: pointer;
  font-weight: 700;
  color: var(--ink);
}

.details-block button {
  margin: 14px 0 10px;
}

.empty {
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: 6px;
  color: var(--muted);
  background: var(--surface);
}

.row.alert-row {
  border-color: rgba(231, 133, 44, 0.65);
  background: var(--accent-soft);
}

.pill {
  display: inline-flex;
  width: fit-content;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--brand);
  color: white;
  font-size: 12px;
  font-weight: 700;
}

.pill.alert {
  background: var(--accent);
}

.label-grid {
  display: grid;
  grid-template-columns: repeat(5, 15mm);
  gap: 8px;
  margin: 14px 0 18px;
  align-items: start;
}

.pallet-label-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100mm, 1fr));
  gap: 14px;
  margin: 14px 0 18px;
  align-items: start;
}

.field-label-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(60mm, 1fr));
  gap: 10px;
  margin: 14px 0 18px;
  align-items: start;
}

.field-ticket {
  width: 60mm;
  height: 30mm;
  position: relative;
  overflow: hidden;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  text-align: center;
}

.label-ticket {
  width: 15mm;
  height: 50mm;
  position: relative;
  overflow: hidden;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  text-align: center;
}

.barcode-svg {
  position: absolute;
  left: 5.7mm;
  top: 25mm;
  width: 37mm;
  height: 8.4mm;
  transform: translate(-50%, -50%) rotate(90deg);
  transform-origin: center;
}

.label-name {
  position: absolute;
  left: 12.2mm;
  top: 25mm;
  width: 34mm;
  color: var(--ink);
  font-size: 6.5px;
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transform: translate(-50%, -50%) rotate(90deg);
  transform-origin: center;
}

.label-date {
  display: none;
}

.barcode-text {
  display: none;
}

.field-ticket .barcode-svg {
  position: absolute;
  left: 30mm;
  top: 12mm;
  width: 50mm;
  height: 13mm;
  transform: translate(-50%, -50%);
}

.field-label-name {
  position: absolute;
  left: 4mm;
  right: 4mm;
  bottom: 3.5mm;
  color: var(--ink);
  font-size: 14px;
  font-weight: 800;
  line-height: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pallet-ticket {
  width: 100mm;
  height: 120mm;
  display: grid;
  grid-template-rows: auto 1fr auto auto auto;
  gap: 4mm;
  padding: 7mm;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  text-align: center;
}

.pallet-ticket-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8mm;
}

.pallet-ticket-head img {
  max-width: 34mm;
  max-height: 18mm;
  object-fit: contain;
}

.pallet-ticket-head strong {
  font-size: 28px;
  letter-spacing: 0;
}

.pallet-qr {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 58mm;
}

.pallet-qr .barcode-svg {
  position: static;
  width: 82mm;
  height: 46mm;
  transform: none;
}

.pallet-code {
  font-size: 26px;
  line-height: 1.1;
}

.pallet-date,
.pallet-hint {
  color: var(--muted);
  font-size: 16px;
  font-weight: 700;
}

.list {
  display: grid;
  gap: 8px;
}

.register-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.subpanel {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 15px;
  background: linear-gradient(180deg, #ffffff, var(--surface));
  box-shadow: 0 8px 18px rgba(49, 44, 35, 0.04);
}

.subpanel h3 {
  border-left: 4px solid var(--accent);
  padding-left: 9px;
  color: var(--ink);
}

.segmented {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.segment {
  min-height: 38px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  background: white;
  color: var(--ink);
  font-weight: 700;
  box-shadow: none;
}

.segment:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--ink);
  transform: none;
}

.segment.active {
  background: var(--accent);
  border-color: var(--accent-strong);
  color: white;
}

.row {
  display: grid;
  gap: 4px;
  padding: 12px 13px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  box-shadow: 0 5px 14px rgba(49, 44, 35, 0.04);
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}

.small-button {
  min-height: 34px;
  padding: 8px 10px;
  font-size: 13px;
}

.row strong {
  font-size: 15px;
}

.row span {
  color: var(--muted);
  font-size: 14px;
}

#fieldView #collaboratorCodeInput,
#receptionView #receptionCollaboratorCodeInput,
#packingView #packerCodeInput,
#packingView #packingPalletCodeInput,
#palletView #palletLabelCodeInput,
#traceView #tracePalletInput {
  min-height: 54px;
  border-width: 2px;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0;
  background: #fffdf9;
}

#fieldView #collaboratorCodeInput:focus,
#receptionView #receptionCollaboratorCodeInput:focus,
#packingView #packerCodeInput:focus,
#packingView #packingPalletCodeInput:focus,
#palletView #palletLabelCodeInput:focus,
#traceView #tracePalletInput:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(228, 127, 37, 0.18);
}

@media (max-width: 720px) {
  .shell {
    padding: 12px;
  }

  .topbar {
    align-items: flex-start;
    padding: 12px;
  }

  .brand {
    align-items: flex-start;
    gap: 10px;
  }

  .brand-logo {
    width: 112px;
    height: 50px;
  }

  h1 {
    font-size: 24px;
  }

  .panel {
    padding: 16px;
  }

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

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

  .metric-grid,
  .operation-strip,
  .age-bucket-grid,
  .report-grid,
  .packing-current-card,
  .field-label-grid,
  .label-grid {
    grid-template-columns: 1fr;
  }

  .packing-counter {
    text-align: left;
  }

  .toolbar {
    display: grid;
  }
}

@media print {
  body * {
    visibility: hidden;
  }

  #labelPrintArea,
  #labelPrintArea * {
    visibility: visible;
  }

  #labelPrintArea {
    position: absolute;
    inset: 0;
    display: grid;
    grid-template-columns: repeat(5, 15mm);
    grid-auto-rows: 50mm;
    gap: 0;
    padding: 0;
  }

  #labelPrintArea.pallet-label-grid {
    grid-template-columns: 100mm;
    grid-auto-rows: 120mm;
  }

  #labelPrintArea.field-label-grid {
    grid-template-columns: 60mm;
    grid-auto-rows: 30mm;
  }

  .label-ticket,
  .field-ticket,
  .pallet-ticket {
    break-inside: avoid;
    border: 0;
    border-radius: 0;
    padding: 0;
  }

  .field-ticket {
    width: 60mm;
    height: 30mm;
  }

  .pallet-ticket {
    width: 100mm;
    height: 120mm;
    padding: 7mm;
  }
}

.tv {
  min-height: 100vh;
  padding: 22px;
  background:
    linear-gradient(180deg, rgba(231, 133, 44, 0.14), rgba(21, 23, 21, 0) 260px),
    #151715;
  color: #f7f4ef;
}

.tv-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 20px;
}

.tv-brand {
  display: flex;
  align-items: center;
  gap: 18px;
}

.tv-logo {
  width: 154px;
  height: 66px;
  object-fit: contain;
  background: white;
  border-radius: 8px;
  padding: 8px 10px;
}

.tv h1 {
  font-size: 34px;
  color: #f7f4ef;
}

.tv h2 {
  color: #f7f4ef;
  font-size: 20px;
  margin-bottom: 12px;
}

.tv p,
.tv span {
  color: #c9c3b9;
}

.tv-clock {
  display: grid;
  justify-items: end;
  gap: 8px;
  text-align: right;
}

.tv-controls {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.tv-controls input,
.tv-controls select,
.tv-controls .custom-select-toggle {
  min-height: 38px;
  border-color: rgba(222, 216, 206, 0.28);
  background: rgba(255, 255, 255, 0.1);
  color: #f7f4ef;
}

.tv-controls input {
  width: 150px;
}

.tv-controls .custom-select {
  width: 96px;
}

.tv-clock strong {
  display: block;
  font-size: 42px;
  line-height: 1;
  color: #f7f4ef;
}

.tv-clock span {
  display: block;
  margin-top: 6px;
  font-size: 18px;
}

.tv-metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.tv-card {
  min-height: 126px;
  padding: 18px;
  border: 1px solid rgba(222, 216, 206, 0.18);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.07);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.18);
}

.tv-card strong {
  display: block;
  font-size: 44px;
  line-height: 1.05;
  color: #ffffff;
}

.tv-card span {
  display: block;
  margin-top: 8px;
  font-size: 17px;
}

.tv-card.alert {
  border-color: rgba(231, 133, 44, 0.7);
  background: rgba(231, 133, 44, 0.14);
}

.tv-card.good {
  border-color: rgba(75, 145, 104, 0.55);
  background: rgba(47, 75, 64, 0.58);
}

.tv-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.tv-panel {
  min-height: 260px;
  padding: 16px;
  border: 1px solid rgba(222, 216, 206, 0.16);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
}

.tv-list {
  display: grid;
  gap: 10px;
}

.compact-tv-list {
  margin-top: 10px;
}

.compact-tv-list .tv-row {
  padding: 9px 10px;
}

.compact-tv-list .tv-row strong {
  font-size: 15px;
}

.compact-tv-list .tv-row span {
  font-size: 13px;
}

.tv-row {
  display: grid;
  gap: 6px;
  padding: 12px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
}

.tv-row.alert {
  border: 1px solid rgba(231, 133, 44, 0.7);
  background: rgba(231, 133, 44, 0.16);
}

.tv-row strong {
  color: #ffffff;
  font-size: 18px;
}

.tv-row span {
  font-size: 15px;
}

.tv-bar {
  height: 8px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.14);
  overflow: hidden;
}

.tv-bar i {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
}

@media (max-width: 1000px) {
  .tv-metrics,
  .tv-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
