:root {
  --bg: #f8fafc;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --blue: #1266dd;
  --blue-hov: #0e58c0;
  --green: #16a34a;
  --red: #dc2626;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.wrap { max-width: 960px; margin: 0 auto; padding: 2rem 1.25rem 4rem; }
header { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 1.5rem; flex-wrap: wrap; gap: 0.5rem; }
header h1 { font-size: 1.25rem; margin: 0; font-weight: 600; }
header .sub { color: var(--muted); font-size: 0.85rem; }
.status { font-size: 0.8rem; color: var(--muted); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: middle; background: #cbd5e1; }
.dot.up { background: var(--green); }
.dot.down { background: var(--red); }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
}
.card h2 { font-size: 0.95rem; font-weight: 600; margin: 0 0 0.75rem; color: var(--text); }

.pushrow { display: flex; gap: 0.5rem; }
/* Normalise input rendering — Safari adds a native double-bezel on
   <input type="text"> by default which competes with our own border. */
input, select, textarea {
  appearance: none !important;
  -webkit-appearance: none !important;
  -moz-appearance: none !important;
  background-color: #fff;
  background-clip: padding-box;
  box-shadow: none !important;
  outline: none;
}
/* Hide Safari's auto-fill pseudo-buttons. Safari injects these inside
   inputs whose name/type smells like contact / credentials / etc., showing
   a small rounded rectangle to the right of the input. They render as
   visible boxes on macOS even with appearance:none. */
input::-webkit-credentials-auto-fill-button,
input::-webkit-strong-password-auto-fill-button,
input::-webkit-strong-confirmation-password-auto-fill-button,
input::-webkit-contacts-auto-fill-button,
input::-webkit-caps-lock-indicator,
input::-webkit-list-button,
input::-webkit-calendar-picker-indicator,
input::-webkit-search-decoration,
input::-webkit-search-cancel-button,
input::-webkit-search-results-button {
  display: none !important;
  visibility: hidden !important;
  pointer-events: none !important;
  -webkit-appearance: none !important;
  width: 0 !important;
  height: 0 !important;
}
/* Form-grid cells are pure layout — never paint a border / shadow on them. */
.form-grid > div { border: 0; outline: none; box-shadow: none; background: transparent; }
/* macOS Safari highlights autofilled fields with a yellow inset bezel that
   looks like a second border. Override its inset background-color shadow. */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
  -webkit-box-shadow: 0 0 0 1000px #fff inset !important;
  -webkit-text-fill-color: var(--text) !important;
  transition: background-color 5000s ease-in-out 0s;
}
/* Only the .pushrow ordname input gets the chunky monospace style. Inside
   .form-grid (Phone, City, etc.) the .form-grid input rule below takes over. */
.pushrow input[type=text] {
  flex: 1;
  padding: 0.65rem 0.85rem;
  border: 1px solid #cbd5e1;
  border-radius: 7px;
  font-size: 0.95rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
/* Focus ring — soft glow OUTSIDE the border instead of an offset outline.
   Old approach (outline-offset:-1px) drew a 2px blue ring INSIDE the grey
   border, leaving a visible grey ring + blue ring "double" effect. */
input:focus,
select:focus,
textarea:focus {
  border-color: var(--blue) !important;
  box-shadow: 0 0 0 3px rgba(18, 102, 221, 0.18) !important;
  outline: none !important;
}
button {
  padding: 0.65rem 1.25rem;
  border: 0;
  border-radius: 7px;
  background: var(--blue);
  color: #fff;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
}
button:hover:not(:disabled) { background: var(--blue-hov); }
button:disabled { background: #94a3b8; cursor: not-allowed; }

.log {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
  background: #0f172a;
  color: #e2e8f0;
  padding: 0.85rem 1rem;
  border-radius: 8px;
  max-height: 260px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.5;
}
.log .ts { color: #475569; margin-right: 0.5rem; }
.log .info { color: #93c5fd; }
.log .ok   { color: #86efac; }
.log .err  { color: #fca5a5; }
.log .step { color: #c4b5fd; }
.log .muted { color: #64748b; font-style: italic; }

table { width: 100%; border-collapse: collapse; font-size: 0.9rem; margin-top: 0.25rem; }
th, td { text-align: left; padding: 0.55rem 0.6rem; border-bottom: 1px solid var(--border); }
th { background: #f1f5f9; font-weight: 600; font-size: 0.82rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.02em; }
tr:last-child td { border-bottom: 0; }
td.num { font-family: ui-monospace, monospace; text-align: right; white-space: nowrap; }
tr.cheapest { background: #f0fdf4; }
tr.cheapest td:first-child::before { content: "★ "; color: var(--green); }

.pill { display: inline-block; padding: 0.15rem 0.55rem; border-radius: 999px; font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.pill.ok  { background: #dcfce7; color: #166534; }
.pill.err { background: #fee2e2; color: #991b1b; }
.pill.info { background: #dbeafe; color: #1e40af; }

details { margin-top: 0.75rem; }
summary { cursor: pointer; color: var(--blue); font-size: 0.85rem; user-select: none; }
pre { background: #f8fafc; border: 1px solid var(--border); padding: 0.75rem; border-radius: 6px; font-size: 0.78rem; overflow-x: auto; margin: 0.5rem 0 0; max-height: 340px; }

.hist-row { display: grid; grid-template-columns: 80px 1fr auto; gap: 0.75rem; align-items: center; padding: 0.5rem 0; border-bottom: 1px solid var(--border); font-size: 0.88rem; }
.hist-row:last-child { border-bottom: 0; }
.hist-time { color: var(--muted); font-family: ui-monospace, monospace; font-size: 0.8rem; }
.hist-empty { color: var(--muted); font-style: italic; font-size: 0.85rem; }

a { color: var(--blue); }

.banner { background: #fef3c7; border: 1px solid #fcd34d; color: #78350f; padding: 0.6rem 0.9rem; border-radius: 8px; font-size: 0.85rem; margin-bottom: 1rem; }
.banner strong { color: #78350f; }

.quoteid-box { display: flex; gap: 0.75rem; align-items: center; background: #f0f9ff; border: 1px solid #bae6fd; padding: 0.75rem 1rem; border-radius: 8px; margin-bottom: 0.75rem; }
.quoteid-box .label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: #0369a1; font-weight: 600; }
.quoteid-box .id { font-family: ui-monospace, monospace; font-size: 1.4rem; font-weight: 600; color: #0c4a6e; user-select: all; }
.quoteid-box button { padding: 0.3rem 0.7rem; font-size: 0.78rem; background: #e0f2fe; color: #0369a1; border: 1px solid #bae6fd; }
.quoteid-box button:hover:not(:disabled) { background: #bae6fd; }

button.book-btn { padding: 0.3rem 0.75rem; font-size: 0.78rem; background: var(--green); }
button.book-btn:hover:not(:disabled) { background: #15803d; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem 1rem; }
.form-grid label { font-size: 0.78rem; color: var(--muted); display: block; margin-bottom: 0.2rem; font-weight: 500; }
.form-grid input, .form-grid select { width: 100%; padding: 0.5rem 0.7rem; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 0.9rem; box-shadow: none; background: #fff; font-family: inherit; }
.form-grid .full { grid-column: 1 / -1; }
.form-warning { background: #fef3c7; border: 1px solid #fcd34d; color: #78350f; padding: 0.6rem 0.85rem; border-radius: 7px; font-size: 0.82rem; margin: 0.75rem 0; }
.form-row { display: flex; gap: 0.5rem; margin-top: 0.75rem; }
.btn-secondary { background: #f1f5f9; color: #334155; }
.btn-secondary:hover:not(:disabled) { background: #e2e8f0; }

.order-ref { font-family: ui-monospace, monospace; font-size: 1.3rem; font-weight: 600; color: #064e3b; }
.dl-list { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.5rem; }
.dl-btn { display: inline-flex; align-items: center; gap: 0.45rem; padding: 0.55rem 1rem; background: #16a34a; border: 1px solid #15803d; border-radius: 7px; color: #ffffff; font-size: 0.88rem; font-weight: 500; text-decoration: none; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.08); }
.dl-btn:hover { background: #15803d; color: #ffffff; }
.dl-btn::before { content: "⬇"; font-size: 1rem; }
.print-btn { display: inline-flex; align-items: center; gap: 0.45rem; padding: 0.55rem 1rem; background: #7c3aed; border: 1px solid #6d28d9; border-radius: 7px; color: #fff; font-size: 0.88rem; font-weight: 500; cursor: pointer; box-shadow: 0 1px 2px rgba(0,0,0,0.08); }
.print-btn:hover:not(:disabled) { background: #6d28d9; }
.print-btn::before { content: "🖨"; font-size: 1rem; }
.print-btn:disabled { opacity: 0.6; cursor: not-allowed; }

.invoice-table { margin-top: 0.5rem; }
.invoice-table td { font-size: 0.85rem; padding: 0.4rem 0.5rem; }
.invoice-table td.num { text-align: right; font-family: ui-monospace, monospace; }
.invoice-table tr.total td { font-weight: 600; border-top: 1px solid var(--border); }

#parcels-table input { width: 100%; padding: 0.4rem 0.5rem; border: 1px solid #cbd5e1; border-radius: 5px; font-size: 0.85rem; }
#parcels-table th { background: #f1f5f9; font-size: 0.72rem; padding: 0.4rem 0.5rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); font-weight: 600; text-align: left; border-bottom: 1px solid var(--border); }
#parcels-table td { padding: 0.35rem 0.4rem; border-bottom: 1px solid var(--border); }
#parcels-table td:first-child { color: var(--muted); text-align: center; font-family: ui-monospace, monospace; font-size: 0.82rem; }
#parcels-table tr:last-child td { border-bottom: 0; }

/* Nav / top-right header actions */
.nav-actions { display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; }
.nav-btn { display: inline-flex; align-items: center; gap: 0.35rem; padding: 0.45rem 0.85rem; border-radius: 7px; background: #f1f5f9; color: #334155; font-size: 0.85rem; font-weight: 500; text-decoration: none; border: 1px solid #e2e8f0; cursor: pointer; }
.nav-btn:hover { background: #e2e8f0; color: #0f172a; }
.nav-btn.primary { background: var(--green); color: #fff; border-color: #15803d; }
.nav-btn.primary:hover { background: #15803d; color: #fff; }
.nav-sender { display: inline-flex; align-items: center; gap: 0.3rem; font-size: 0.85rem; padding: 0.35rem 0.65rem; border: 1px solid #e2e8f0; border-radius: 7px; background: #fff; }
.nav-sender select { border: 0; background: transparent; font-size: 0.85rem; font-weight: 500; color: #1266dd; cursor: pointer; padding: 0.1rem 0.2rem; }

/* Toast notifications */
.toast-host { position: fixed; right: 1rem; bottom: 1rem; display: flex; flex-direction: column; gap: 0.5rem; z-index: 9999; pointer-events: none; }
.toast { pointer-events: auto; min-width: 260px; max-width: 420px; padding: 0.7rem 0.9rem; border-radius: 8px; font-size: 0.85rem; box-shadow: 0 6px 16px rgba(15,23,42,0.15); border: 1px solid transparent; display: flex; gap: 0.75rem; align-items: center; background: #0f172a; color: #e2e8f0; animation: toast-in 0.2s ease; }
.toast.ok  { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
.toast.err { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
.toast.info { background: #dbeafe; color: #1e40af; border-color: #bfdbfe; }
.toast button { background: transparent; color: inherit; border: 0; font-size: 0.8rem; font-weight: 600; padding: 0.25rem 0.5rem; border-radius: 4px; cursor: pointer; text-decoration: underline; }
.toast button:hover { background: rgba(0,0,0,0.08); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Modal dialog (native <dialog>) */
dialog.modal { border: 0; border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 2rem)); max-height: calc(100vh - 2rem); background: #fff; color: var(--text); box-shadow: 0 24px 60px rgba(15, 23, 42, 0.25); overflow: hidden; }
dialog.modal::backdrop { background: rgba(15, 23, 42, 0.45); backdrop-filter: blur(3px); }
dialog.modal[open] { animation: modal-in 0.15s ease-out; }
@keyframes modal-in { from { transform: translateY(12px) scale(0.98); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { display: flex; gap: 0.9rem; align-items: flex-start; padding: 1.1rem 1.25rem 0.6rem; }
.modal-icon { flex: 0 0 36px; width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; font-weight: 700; }
.modal-icon.err { background: #fee2e2; color: #b91c1c; }
.modal-icon.warn { background: #fef3c7; color: #92400e; }
.modal-icon.ok { background: #dcfce7; color: #166534; }
.modal-head h3 { margin: 0; font-size: 1.05rem; font-weight: 600; }
.modal-head .sub { margin: 0.15rem 0 0; color: var(--muted); font-size: 0.85rem; line-height: 1.35; }
.modal-close { margin-left: auto; background: transparent; color: var(--muted); border: 0; padding: 0.3rem 0.4rem; font-size: 1.1rem; cursor: pointer; border-radius: 6px; }
.modal-close:hover { background: #f1f5f9; color: var(--text); }
.modal-body { padding: 0.5rem 1.25rem 1rem; max-height: 60vh; overflow: auto; }
.modal-body ul.carrier-errs { list-style: none; margin: 0.25rem 0 0; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.modal-body ul.carrier-errs li { padding: 0.7rem 0.85rem; background: #fef2f2; border: 1px solid #fecaca; border-radius: 8px; font-size: 0.88rem; line-height: 1.4; color: #7f1d1d; }
.modal-body ul.carrier-errs li .carrier { font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; font-size: 0.72rem; padding: 0.1rem 0.5rem; border-radius: 999px; margin-right: 0.4rem; background: #fff; color: var(--red); }
.modal-body ul.carrier-errs li .msg { display: block; margin-top: 0.3rem; white-space: pre-wrap; }
.modal-body ul.carrier-errs li ul.notes { list-style: disc; margin: 0.35rem 0 0 1rem; padding: 0; color: #991b1b; font-size: 0.85rem; }

/* Humanised error cards — the friendly alternative */
.issue-list { display: flex; flex-direction: column; gap: 0.6rem; margin: 0.25rem 0 0; padding: 0; list-style: none; }
.issue { display: flex; gap: 0.75rem; padding: 0.85rem 0.95rem; background: #fffbeb; border: 1px solid #fde68a; border-radius: 10px; align-items: flex-start; }
.issue-emoji { flex: 0 0 auto; font-size: 1.3rem; line-height: 1.2; }
.issue-body { flex: 1; min-width: 0; }
.issue-title { font-weight: 600; color: #78350f; font-size: 0.95rem; margin: 0 0 0.15rem; }
.issue-text { color: #57534e; font-size: 0.88rem; line-height: 1.45; margin: 0; }
.issue-fix { color: #78350f; font-size: 0.85rem; margin: 0.35rem 0 0; }
.issue-actions { margin-top: 0.45rem; display: flex; gap: 0.4rem; flex-wrap: wrap; }
.issue-actions button { padding: 0.35rem 0.75rem; font-size: 0.78rem; background: #f59e0b; color: #78350f; border-radius: 6px; font-weight: 600; border: 0; cursor: pointer; }
.issue-actions button:hover { background: #d97706; color: #fff; }

/* Row action kebab menu */
.row-menu-trigger {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px;
  padding: 0;
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
}
.row-menu-trigger:hover { background: #f1f5f9; color: var(--text); border-color: var(--border); }
.row-menu-trigger[aria-expanded="true"] { background: #eef2ff; color: var(--blue); border-color: #c7d2fe; }
#row-menu-popup {
  position: absolute;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.18);
  padding: 0.35rem;
  min-width: 200px;
  z-index: 200;
  display: none;
  animation: rm-in 0.12s ease-out;
}
#row-menu-popup[data-open] { display: block; }
#row-menu-popup button,
#row-menu-popup a {
  display: flex; align-items: center; gap: 0.6rem;
  width: 100%;
  padding: 0.55rem 0.75rem;
  background: transparent;
  border: 0;
  font-size: 0.88rem;
  text-align: left;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  text-decoration: none;
  font-family: inherit;
}
#row-menu-popup button:hover,
#row-menu-popup a:hover { background: #f1f5f9; }
#row-menu-popup button.danger { color: #b91c1c; }
#row-menu-popup button.danger:hover { background: #fee2e2; }
#row-menu-popup .rm-icon { flex: 0 0 20px; font-size: 0.95rem; text-align: center; }
#row-menu-popup .rm-divider { height: 1px; background: var(--border); margin: 0.3rem 0.25rem; }
@keyframes rm-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }

/* Transient highlight for the "Fix it →" target field */
.field-flash { animation: field-flash 1.6s ease-out; }
@keyframes field-flash {
  0%   { box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.65); background: #fffbeb; }
  50%  { box-shadow: 0 0 0 6px rgba(245, 158, 11, 0.35); background: #fef3c7; }
  100% { box-shadow: 0 0 0 0 rgba(245, 158, 11, 0); background: transparent; }
}
.modal-body details { margin-top: 0.85rem; }
.modal-body details summary { color: var(--blue); cursor: pointer; font-size: 0.82rem; }
.modal-body details pre { max-height: 240px; font-size: 0.75rem; }
.modal-foot { display: flex; justify-content: flex-end; gap: 0.5rem; padding: 0.75rem 1.25rem 1rem; border-top: 1px solid var(--border); background: #f8fafc; }
.modal-foot button { padding: 0.55rem 1.1rem; font-size: 0.88rem; }

@media (max-width: 720px) {
  dialog.modal { width: calc(100vw - 1rem); border-radius: 10px; }
  .modal-head { padding: 0.85rem 0.9rem 0.4rem; }
  .modal-body { padding: 0.4rem 0.9rem 0.8rem; }
  .modal-foot { padding: 0.65rem 0.9rem 0.85rem; }
}

/* Quote filter chips */
.filter-bar { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; margin-bottom: 0.6rem; font-size: 0.82rem; }
.filter-bar .chip { padding: 0.3rem 0.7rem; background: #f1f5f9; border: 1px solid #e2e8f0; border-radius: 999px; cursor: pointer; user-select: none; }
.filter-bar .chip.active { background: var(--blue); color: #fff; border-color: var(--blue); }
.filter-bar input, .filter-bar select { padding: 0.3rem 0.5rem; border: 1px solid #cbd5e1; border-radius: 6px; font-size: 0.82rem; }
.filter-bar label { display: inline-flex; align-items: center; gap: 0.3rem; color: var(--muted); }

/* Live log — collapsible via <details> */
details.log-card summary { display: flex; justify-content: space-between; align-items: center; cursor: pointer; list-style: none; font-size: 0.95rem; font-weight: 600; padding: 0; }
details.log-card summary::-webkit-details-marker { display: none; }
details.log-card summary::after { content: '▸'; transition: transform 0.15s; color: var(--muted); font-size: 0.8rem; }
details.log-card[open] summary::after { transform: rotate(90deg); }
details.log-card .log { margin-top: 0.65rem; }
.log-badge { font-size: 0.72rem; font-weight: 500; color: var(--muted); }
.log-badge.err { color: var(--red); font-weight: 600; }

/* Mobile */
@media (max-width: 720px) {
  .wrap { padding: 1rem 0.75rem 3rem; }
  header { flex-direction: column; align-items: stretch; gap: 0.75rem; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .full { grid-column: 1; }
  table { font-size: 0.8rem; }
  th, td { padding: 0.45rem 0.4rem; }
  .quoteid-box { flex-direction: column; align-items: flex-start; }
  .pushrow { flex-direction: column; }
  .pushrow button { width: 100%; }
  /* Parcel dim table: keep landscape but shrink; scrollable if needed */
  #parcels-table { display: block; overflow-x: auto; }
  .dl-list { flex-direction: column; }
  .dl-btn, .print-btn { justify-content: center; }
  .nav-actions { width: 100%; justify-content: flex-start; }
  .nav-btn { flex: 1 1 auto; justify-content: center; }
  .toast-host { left: 1rem; right: 1rem; }
  .toast { min-width: 0; max-width: 100%; }
  /* Bookings / packaging tables become vertically-stacked cards */
  .stack-cards table thead { display: none; }
  .stack-cards table, .stack-cards tbody, .stack-cards tr, .stack-cards td { display: block; width: 100%; }
  .stack-cards tr { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 0.5rem; padding: 0.5rem; }
  .stack-cards td { border: 0; padding: 0.25rem 0; display: flex; justify-content: space-between; gap: 0.5rem; }
  .stack-cards td::before { content: attr(data-label); font-weight: 600; color: var(--muted); font-size: 0.75rem; text-transform: uppercase; }
}
