:root {
  color-scheme: light dark;
  --bg: #f7f7f8;
  --surface: #ffffff;
  --border: #e3e3e6;
  --text: #1c1c1f;
  --text-muted: #6b6b72;
  --accent: #2f5fef;
  --accent-contrast: #ffffff;
  --danger: #d1392f;
  --warning: #b8860b;
  --radius: 0.6rem;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 12px rgba(0, 0, 0, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --surface: #1b1b1e;
    --border: #2d2d31;
    --text: #edeef0;
    --text-muted: #9a9aa2;
    --accent: #5b82f7;
    --accent-contrast: #0e1116;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.35);
  }
}

* {
  box-sizing: border-box;
}

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

.topnav {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topnav-inner {
  padding: 0 1.5rem;
  height: 3.25rem;
  display: flex;
  align-items: center;
  gap: 1.75rem;
}

.topnav a {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  height: 100%;
  display: flex;
  align-items: center;
  border-bottom: 2px solid transparent;
}

.topnav a:hover {
  color: var(--text);
}

.topnav a.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.page {
  padding: 2.5rem 1.5rem;
}

header {
  margin-bottom: 2rem;
}

h1 {
  font-size: 1.4rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 0;
}

h2 {
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--text-muted);
  text-transform: uppercase;
  margin: 0 0 1rem;
}

section {
  margin-bottom: 1.5rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.5rem;
  overflow-x: auto;
}

form {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: end;
}

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

.field.wide {
  flex: 1 1 100%;
}

label,
.field > span {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
}

input,
select,
textarea,
button {
  font: inherit;
  font-size: 0.9rem;
}

input,
select,
textarea {
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  background: var(--surface);
  color: var(--text);
}

input:focus,
select:focus,
textarea:focus,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

textarea {
  resize: vertical;
}

button {
  cursor: pointer;
  padding: 0.45rem 0.9rem;
  border-radius: 0.4rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  align-self: flex-start;
  transition: background-color 0.1s ease, border-color 0.1s ease;
}

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

form > button[type="submit"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  font-weight: 500;
}

button.delete {
  border-color: transparent;
  color: var(--danger);
  background: none;
}

button.delete:hover {
  border-color: var(--danger);
}

table {
  width: 100%;
  border-collapse: collapse;
}

th {
  text-align: left;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0 0.6rem 0.6rem;
  border-bottom: 1px solid var(--border);
}

td {
  text-align: left;
  padding: 0.6rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
  font-size: 0.9rem;
}

tbody tr:last-child td {
  border-bottom: none;
}

td button {
  padding: 0.3rem 0.65rem;
  font-size: 0.8rem;
}

.error {
  color: var(--danger);
  font-size: 0.85rem;
}

.warning {
  color: var(--warning);
  font-size: 0.85rem;
}

.warning-list {
  margin: 0.5rem 0 0;
  padding-left: 1.25rem;
}

.hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  font-style: italic;
}

/* --- Dispatch quantities table --- */

.allergen-checkboxes {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  font-size: 0.85rem;
  border: 1px solid transparent;
  border-radius: 0.4rem;
  padding: 0.35rem;
  margin: -0.35rem;
}

.allergen-checkboxes label {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  color: var(--text);
  font-weight: 400;
}

.layout {
  display: flex;
  gap: 1.5rem;
  align-items: flex-start;
}

.layout > main {
  flex: 1 1 auto;
  min-width: 0;
}

.panel {
  flex: 0 0 33%;
  position: sticky;
  top: 1.5rem;
}

.panel-empty {
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* Marks a sidebar edit field as differing from its last-saved value. */
input.dirty,
select.dirty,
textarea.dirty,
.allergen-checkboxes.dirty,
.ingredient-line.dirty {
  border-color: var(--warning);
  background: color-mix(in srgb, var(--warning) 10%, var(--surface));
}

.panel-fields {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.panel-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

tbody tr {
  cursor: pointer;
}

tbody tr:hover {
  background: color-mix(in srgb, var(--accent) 4%, transparent);
}

tbody tr.selected {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.ingredient-line {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  border: 1px solid transparent;
  border-radius: 0.4rem;
  padding: 0.15rem 0.3rem;
  margin: -0.15rem -0.3rem;
}

.ingredient-line input[type="number"] {
  width: 5rem;
}

.ingredient-line select {
  width: 5rem;
}

.dispatch-quantity {
  width: 4.5rem;
}

/* --- Label sheet rendering/printing --- */
/* Physical units (in) throughout, to match the real label stock exactly -
   both on screen (preview) and when printed. */

.sheet {
  width: 8.5in;
  height: 11in;
  background: white;
  margin: 0 auto 1.5rem;
  box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: repeat(2, 4in);
  grid-template-rows: repeat(5, 2in);
  column-gap: 0.1875in;
  row-gap: 0;
  padding: 0.5in 0.15625in;
}

.label {
  width: 4in;
  height: 2in;
  box-sizing: border-box;
  border-radius: 0.15625in;
  border: 0.05in solid var(--label-accent, #3b260c);
  background: var(--label-bg, #eae0c0);
  padding: 0.08in;
  display: flex;
  gap: 0.1in;
  color: #000;
  overflow: hidden;
  /* Browsers drop background colors on print by default - without this, the
     label prints with a white background even though the preview (and this
     same background property) look right on screen. */
  print-color-adjust: exact;
  -webkit-print-color-adjust: exact;
}

.label-left {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.05in;
  max-width: 1.6in;
}

.label-logo {
  flex: 1 1 0;
  min-height: 0;
  width: 100%;
  object-fit: contain;
}

.label-business-row {
  display: flex;
  align-items: center;
  gap: 0.08in;
}

.label-qr {
  flex: 0 0 auto;
  width: 0.35in;
  height: 0.35in;
}

.label-business {
  font-size: 5pt;
  text-align: left;
  line-height: 1.3;
}

.label-website {
  font-size: 7pt;
  line-height: 1;
  font-weight: bold;
  color: var(--label-accent, #3b260c);
  text-align: left;
  margin-top: auto;
}

.label-content {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.label-heading {
  font-size: 10pt;
  font-weight: bold;
  margin: 0 0 0.01in;
  white-space: pre-line;
}

.label-ingredients {
  font-size: 5pt;
  line-height: 1.35;
}

.label-ingredients .label-ingredients-heading {
  font-weight: bold;
}

.label-bottom {
  margin-top: auto;
  display: flex;
  flex-direction: column;
}

.label-produced {
  display: flex;
  align-items: center;
  gap: 0.05in;
  font-size: 5pt;
  font-weight: bold;
}

.label-produced-box {
  flex: 1 1 auto;
  background: #fff;
  border: 0.01in solid #0003;
  height: 0.18in;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 6pt;
  font-weight: normal;
}

.label-disclaimer {
  font-size: 6pt;
  margin-top: 0.03in;
}

.label-net-weight {
  font-size: 13.5pt;
  line-height: 1;
  text-align: right;
  margin-top: 0.03in;
}

.label-blank {
  visibility: hidden;
}

@media print {
  body * {
    visibility: hidden;
  }
  .print-area,
  .print-area * {
    visibility: visible;
  }
  .print-area {
    position: absolute;
    top: 0;
    left: 0;
  }
  .sheet {
    box-shadow: none;
    margin: 0;
    page-break-after: always;
  }
  @page {
    size: 8.5in 11in;
    margin: 0;
  }
}
