:root {
  color-scheme: light;
  --bg: #f5efe6;
  --panel: #fffaf1;
  --panel-2: #f2e6d8;
  --text: #1f1b16;
  --muted: #6d655d;
  --accent: #134e4a;
  --accent-2: #b45309;
  --border: rgba(31, 27, 22, 0.14);
  --shadow: 0 18px 40px rgba(41, 31, 19, 0.12);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Georgia, 'Times New Roman', serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(180, 83, 9, 0.12), transparent 28%),
    radial-gradient(circle at top right, rgba(19, 78, 74, 0.12), transparent 25%),
    linear-gradient(180deg, #fbf6ee, var(--bg));
  min-height: 100vh;
}

a { color: var(--accent); }

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
  padding: 1rem 1.25rem;
  position: sticky;
  top: 0;
  backdrop-filter: blur(12px);
  background: rgba(251, 246, 238, 0.88);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.brand { font-size: 1.25rem; font-weight: 700; letter-spacing: 0.04em; }
.subtitle { color: var(--muted); font-size: 0.9rem; }
.navlinks { display: flex; gap: 0.8rem; flex-wrap: wrap; align-items: center; }
.navlinks a, .linklike {
  background: transparent;
  border: 0;
  text-decoration: none;
  color: var(--text);
  font: inherit;
  cursor: pointer;
  padding: 0.25rem 0;
}
.language-selector { display: flex; align-items: center; gap: 0.4rem; margin-left: 0.5rem; }
.language-selector select { padding: 0.35rem 0.5rem; border-radius: 10px; font-size: 0.9rem; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }

.page-shell {
  width: min(1600px, calc(100% - 2rem));
  margin: 1.5rem auto 3rem;
}

.page-shell.narrow { width: min(760px, calc(100% - 2rem)); }
.card {
  background: linear-gradient(180deg, rgba(255,255,255,0.72), rgba(255,250,241,0.96));
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 1.25rem;
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
}

.hero-card { padding: 2rem; }
.muted { color: var(--muted); }
.form-grid { display: grid; gap: 0.85rem; }
.form-grid.two-col { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.inline-form { display: flex; gap: 0.75rem; flex-wrap: wrap; align-items: end; }
label { display: grid; gap: 0.35rem; }
label span { font-size: 0.88rem; color: var(--muted); }
/* Plugin code field: a div (not a label) so clicks on the CodeMirror editor
   aren't swallowed by label-control activation. Mirrors the label look. */
.form-field { display: grid; gap: 0.35rem; }
.form-field > span { font-size: 0.88rem; color: var(--muted); }
input, select, button, textarea {
  border: 1px solid rgba(31, 27, 22, 0.18);
  border-radius: 14px;
  padding: 0.78rem 0.92rem;
  font: inherit;
  background: rgba(255,255,255,0.92);
}
button {
  background: var(--accent);
  color: white;
  cursor: pointer;
  border: 0;
  transition: transform 0.15s ease, opacity 0.15s ease;
}
button:hover { transform: translateY(-1px); }
button:disabled { opacity: 0.6; cursor: not-allowed; }
button.cancel-edit {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}
button.cancel-edit:hover { background: rgba(31, 27, 22, 0.06); color: var(--text); transform: none; }
/* Secondary action button (e.g. the waybill "Clean" button next to
   "Crea spedizione") — muted/outline so it reads as a non-primary action. */
button.secondary {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}
button.secondary:hover { background: rgba(31, 27, 22, 0.06); color: var(--text); transform: none; }
.actions-row { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.full-row { grid-column: 1 / -1; }
.hidden { display: none !important; }

/* Loaded-invoice chips on the waybill page (multi-invoice flow). */
.invoice-chips { list-style: none; margin: 0.5rem 0 0; padding: 0; display: flex; gap: 0.5rem; flex-wrap: wrap; }
.invoice-chips li {
  display: inline-flex; align-items: center; gap: 0.5rem;
  padding: 0.3rem 0.6rem;
  border: 1px solid rgba(31, 27, 22, 0.18);
  border-radius: 0.4rem;
  background: rgba(31, 27, 22, 0.03);
  font-size: 0.9rem;
}
.invoice-chips li .invoice-chip-meta { color: rgba(31, 27, 22, 0.6); font-size: 0.8rem; }
.invoice-chips li button.invoice-chip-remove {
  border: none; background: none; cursor: pointer; padding: 0 0.2rem;
  color: rgba(31, 27, 22, 0.55); line-height: 1; font-size: 1.05rem;
}
.invoice-chips li button.invoice-chip-remove:hover { color: #b91c1c; transform: none; }
.invoice-chips li button.invoice-chip-remove[disabled] { cursor: not-allowed; opacity: 0.4; }
.history-empty-row td.history-empty-cell {
  text-align: center;
  padding: 2rem 1rem;
  font-style: italic;
}

/* ---- Toast notifications -----------------------------------------------
   The container is fixed at the top-right and floats above the page,
   the loading overlay, and any modal so a new toast is never hidden by
   content the user is looking at. Toasts are created on the fly by
   `showToast` in public/js/app.js and dismissed automatically after a
   few seconds, with a manual close button for sticky ones. */
.toast-container {
  position: fixed;
  top: 1rem;
  right: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  z-index: 1000;
  pointer-events: none;
  max-width: min(380px, calc(100vw - 2rem));
}
.toast {
  pointer-events: auto;
  background: linear-gradient(180deg, rgba(255,255,255,0.96), rgba(255,250,241,0.96));
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-radius: 14px;
  padding: 0.85rem 1rem;
  box-shadow: var(--shadow);
  color: var(--text);
  font-size: 0.95rem;
  line-height: 1.35;
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  animation: toast-in 0.18s ease-out;
}
.toast.leaving { animation: toast-out 0.18s ease-in forwards; }
.toast .toast-body { flex: 1; }
.toast .toast-title { font-weight: 600; }
.toast .toast-details {
  display: block;
  margin-top: 0.2rem;
  color: var(--muted);
  font-size: 0.88rem;
}
.toast .toast-close {
  background: transparent;
  color: var(--muted);
  border: 0;
  padding: 0 0.25rem;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 6px;
}
.toast .toast-close:hover { color: var(--text); transform: none; }

.toast.success { border-left-color: #16a34a; }
.toast.success .toast-title { color: #15803d; }
.toast.error   { border-left-color: #b91c1c; }
.toast.error   .toast-title { color: #b91c1c; }
.toast.info    { border-left-color: var(--accent); }
.toast.info    .toast-title { color: var(--accent); }

@keyframes toast-in {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: translateX(0); }
}
@keyframes toast-out {
  from { opacity: 1; transform: translateX(0); }
  to   { opacity: 0; transform: translateX(20px); }
}
@media (prefers-reduced-motion: reduce) {
  .toast,
  .toast.leaving { animation: none; }
}

/* Checkbox-style label: horizontal layout, no big grid gap. */
label.checkbox {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  align-self: end;
  min-height: calc(0.78rem * 2 + 1.84rem); /* match the height of text inputs */
}
label.checkbox input[type=checkbox] {
  width: 1.1rem;
  height: 1.1rem;
  margin: 0;
  padding: 0;
}
label.checkbox span { color: var(--text); font-size: 0.95rem; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
.data-table th, .data-table td {
  border-bottom: 1px solid var(--border);
  padding: 0.65rem 0.5rem;
  vertical-align: top;
}
.data-table th {
  text-align: left;
  position: sticky;
  top: 0;
  background: rgba(242, 230, 216, 0.95);
}
.data-table td.muted { color: var(--muted); }
.data-table .row-actions {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.data-table .row-actions button {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
  padding: 0.35rem 0.7rem;
  font-size: 0.85rem;
  border-radius: 10px;
}
.data-table .row-actions button:hover {
  background: rgba(19, 78, 74, 0.08);
  transform: none;
}
.data-table .row-actions button[data-action=delete] {
  color: #b91c1c;
  border-color: rgba(185, 28, 28, 0.3);
}
.data-table .row-actions button[data-action=delete]:hover {
  background: rgba(185, 28, 28, 0.08);
}

.table-wrap { overflow: auto; max-height: 70vh; border-radius: 18px; }

/* History page — card header with right-aligned action buttons. */
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}
.card-header h1 { margin: 0; }
.card-header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.card-header-actions label {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--muted);
  font-size: 0.9rem;
}

/* Waybill — per-carrier service health badges served by
   monitor.11112024.xyz. The badges are SVG shields embedded as
   <img>; this block just aligns the row + gives the carrier label
   a small monospace pill so the eye can pair SDA/BRT/GLS with the
   matching shield. */
.monitor-badges {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem 0.85rem;
  margin-top: 0.9rem;
  padding-top: 0.75rem;
  border-top: 1px dashed var(--border);
}
.monitor-badges-title {
  color: var(--muted);
  font-size: 0.85rem;
  margin-right: 0.25rem;
}
.monitor-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.monitor-badge-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--muted);
  background: rgba(31, 27, 22, 0.05);
  padding: 0.1rem 0.4rem;
  border-radius: 6px;
}
.monitor-badge img {
  display: block;
  height: 20px;
  width: auto;
}
.monitor-badge.is-broken {
  display: none;
}

/* History tabs — GLS uses a tab strip above the table to switch
   between the shipments inbox and the closing-reports history.
   BRT/SDA render the strip too (hidden), so the markup is uniform. */
.history-tabs {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.85rem;
  padding: 0.3rem;
  background: rgba(242, 230, 216, 0.65);
  border: 1px solid var(--border);
  border-radius: 14px;
}
.history-tab {
  background: transparent;
  color: var(--muted);
  border: 0;
  padding: 0.45rem 0.9rem;
  font-size: 0.9rem;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, transform 0.15s ease;
}
.history-tab:hover { transform: none; color: var(--text); background: rgba(255, 255, 255, 0.5); }
.history-tab.is-active {
  background: var(--accent);
  color: white;
  box-shadow: 0 4px 10px rgba(19, 78, 74, 0.18);
}
.history-tab.is-active:hover { background: var(--accent); color: white; }
.history-section { transition: opacity 0.15s ease; }
.history-section-intro { margin-top: -0.25rem; margin-bottom: 0.75rem; font-size: 0.9rem; }
.history-section[data-pdf-name] .data-table td { word-break: break-all; }
.closing-report-download { white-space: nowrap; }

/* History pagination — BRT/SDA only. GLS hides the block in JS
   because listGlsHistory has no paginated endpoint. */
.history-pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 0.85rem;
  padding: 0.4rem 0.25rem 0.1rem;
}
.history-pagination-info {
  color: var(--muted);
  font-size: 0.9rem;
}
.history-pagination-actions {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}
.history-pagination-actions .history-page-btn {
  min-width: 2.2rem;
  padding: 0.3rem 0.55rem;
  font-size: 0.85rem;
  line-height: 1;
}
.history-pagination-actions .history-page-position {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0 0.4rem;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.history-pagination-actions .history-page-current {
  font-weight: 600;
}
.history-pagination-actions .history-page-sep {
  color: var(--muted);
}
.data-table .select-cell { width: 2.2rem; text-align: center; }
.data-table .select-cell input { transform: scale(1.1); cursor: pointer; }
.pdf-frame, .tracking-frame { width: 100%; min-height: 70vh; border: 1px solid var(--border); border-radius: 18px; background: #fff; }
.tracking-frame { min-height: 80vh; }
.pre-box { white-space: pre-wrap; background: rgba(255,255,255,0.9); padding: 1rem; border-radius: 16px; border: 1px solid var(--border); }

@media (max-width: 800px) {
  .topbar { align-items: start; flex-direction: column; }
  .form-grid.two-col { grid-template-columns: 1fr; }
  .data-table { font-size: 0.85rem; }
  .data-table .row-actions button { padding: 0.3rem 0.55rem; font-size: 0.8rem; }
}

/* --- Spinner / loading overlay --- */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(31, 27, 22, 0.35);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.85rem;
  z-index: 1000;
  backdrop-filter: blur(2px);
}
.loading-overlay .loading-text {
  color: #fff;
  font-weight: 600;
  letter-spacing: 0.02em;
  background: rgba(31, 27, 22, 0.55);
  padding: 0.4rem 0.9rem;
  border-radius: 12px;
  margin: 0;
}
.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
button[disabled], button.is-loading {
  opacity: 0.65;
  cursor: progress;
}

/* --- Modal --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(31, 27, 22, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1100;
  padding: 1rem;
}
.modal-card {
  background: linear-gradient(180deg, #fffaf1, #f6eede);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 1.5rem;
  box-shadow: var(--shadow);
  max-width: 440px;
  width: 100%;
}
.modal-card h2 { margin: 0 0 0.6rem; font-size: 1.15rem; }
.modal-card p  { margin: 0 0 1.2rem; color: var(--muted); }
.modal-card .actions-row { justify-content: flex-end; }

/* Notice dialog body — preserve newlines from the n8n /GetNotice
   message so multi-line notices (e.g. address blocks) display as the
   operator wrote them. `pre-line` keeps all other whitespace handling
   (no extra padding, no need for a <pre>). The body is also capped
   to a max height with overflow-y:auto so very long notices scroll
   inside the modal card instead of stretching the page. */
.modal-card #noticeModalBody {
  white-space: pre-line;
  max-height: 50vh;
  overflow-y: auto;
}

/* CodeMirror editor for the plugin code field. The real <textarea name=code>
   is hidden and carries the value for FormData; this div hosts CodeMirror. */
.plugin-code-editor {
  border: 1px solid var(--border, #ccc);
  border-radius: 4px;
  height: 320px;
  overflow-y: auto;
  font-size: 13px;
}
.plugin-code-editor .cm-editor { height: 100%; }
.plugin-code-editor .cm-scroller { overflow-y: auto; }

/* Danger (destructive) button variant */
button.danger {
  background: #b3261e;
  color: #fff;
  border: 0;
}
button.danger:hover { background: #c3322a; transform: translateY(-1px); }
