/* Quarto-Portal UI — vanilla CSS, no framework. Responsive from ~1280px laptops
   up to full-width monitors. */

:root {
  --accent: #2563eb;
  --accent-soft: rgba(37, 99, 235, 0.12);
  --green: #059669;
  --danger: #dc2626;
  --amber: #b45309;
  --text: #0f172a;
  --muted: #64748b;
  --border: rgba(128, 128, 128, 0.25);
  --bg: #f6f7f9;
  --surface: #ffffff;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

/* The shell fills the window so a scrolling table can pin its scrollbars to the
   real screen edges instead of the end of a very long document. */
html, body { height: 100%; }
#app { height: 100%; display: flex; flex-direction: column; min-height: 0; }
#app > .topbar { flex: 0 0 auto; }
#app > main { flex: 1 1 auto; min-height: 0; overflow: auto; }

.muted { color: var(--muted); font-size: 12px; }

.health-badge {
  border: 1px solid transparent;
  background: transparent;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  cursor: default;
  white-space: nowrap;
}
.health-badge.ok { color: var(--green); }
.health-badge.bad { color: #fff; background: var(--danger); border-color: var(--danger); cursor: help; }

/* --- Top bar ---------------------------------------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
}

.brand { font-weight: 700; font-size: 15px; white-space: nowrap; }

.market-tabs, .view-tabs { display: flex; gap: 4px; }

.tab {
  padding: 5px 12px;
  border: 1px solid var(--border);
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
  white-space: nowrap;
}
.tab:hover { border-color: var(--accent); }
.tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.view-tabs .tab.active { background: transparent; color: var(--accent); border-color: var(--accent); font-weight: 600; }

.sync { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.sync-time { white-space: nowrap; }

.actions { display: flex; gap: 8px; }

/* --- Buttons ---------------------------------------------------------------- */

button { font-family: inherit; }

.btn-primary, .btn-outline, .btn-ghost, .btn-danger {
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  border: 1px solid transparent;
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { filter: brightness(1.05); }
.btn-outline { background: transparent; border-color: var(--accent); color: var(--accent); font-weight: 600; }
.btn-outline:hover { background: var(--accent-soft); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--muted); }
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn-danger { background: var(--danger); color: #fff; font-weight: 600; }
.btn-sm { padding: 3px 10px; font-size: 12px; }
button:disabled { opacity: 0.45; cursor: not-allowed; }

/* --- KPI strip -------------------------------------------------------------- */

.wb-toolbar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 10px 16px;
}

.kpi-strip { display: flex; gap: 8px; flex-wrap: wrap; }

.kpi {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  cursor: pointer;
}
.kpi.active { border-color: var(--accent); }
.kpi-label { font-weight: 600; }
.kpi.active .kpi-label { color: var(--accent); border-bottom: 2px solid var(--accent); }
.kpi-count {
  background: var(--bg);
  border-radius: 10px;
  padding: 0 8px;
  font-size: 12px;
  color: var(--muted);
}
.kpi.active .kpi-count { background: var(--accent); color: #fff; }

.wb-controls { display: flex; align-items: center; gap: 10px; margin-left: auto; }

.booked-toolbar, .si-toolbar, .finished-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 0;
}
.booked-actions, .si-actions { display: flex; gap: 8px; margin-left: auto; }
.booked-hint { color: var(--muted); font-size: 12px; }
.modal-note { color: var(--muted); font-size: 12px; margin-top: 8px; }

.search-input {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  min-width: 240px;
  font-size: 13px;
}

.view-toggle { display: inline-flex; border: 1px solid var(--accent); border-radius: 6px; overflow: hidden; }
.toggle-btn {
  padding: 5px 10px;
  border: none;
  background: transparent;
  cursor: pointer;
  color: var(--accent);
  font-size: 12px;
  white-space: nowrap;
}
.toggle-btn.active { background: var(--accent); color: #fff; font-weight: 600; }

/* --- Workbench body --------------------------------------------------------- */

#content { padding: 0 16px 24px; }
/* Views with a pinned table own their own scrollbars, so the bottom padding would
   leave the horizontal scrollbar floating above the window edge. */
#content:has(> .view-shell) { padding-bottom: 0; overflow: hidden; }

.wb-body { display: grid; gap: 12px; align-items: start; flex: 1 1 auto; min-height: 0; }
.wb-body[data-layout="A"] { grid-template-columns: 1.4fr 1fr; overflow: auto; }
/* Collapsed drawer in layout A: the list spans the full row and the drawer
   column is removed until a project is selected. */
.wb-body[data-layout="A"]:not(.has-drawer) { grid-template-columns: 1fr; }
.wb-body[data-layout="A"]:not(.has-drawer) .wb-drawer-wrap { display: none; }
/* Layout B fills the content area so its table can pin its own scrollbars. */
.wb-body[data-layout="B"] { display: flex; flex-direction: column; overflow: hidden; }
.wb-body[data-layout="B"] .wb-main { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }

.wb-main { min-width: 0; }

/* --- Project list (layout A) ----------------------------------------------- */

.project-list { display: flex; flex-direction: column; gap: 8px; }

.project-card {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  cursor: pointer;
}
.project-card:hover { border-color: var(--accent); }
.project-card.selected { border-color: var(--accent); background: var(--accent-soft); }

.project-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.project-sub { color: var(--muted); font-size: 12px; margin-top: 3px; }

.status-badge {
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  background: var(--bg);
  color: var(--muted);
}
.status-badge.closed { background: rgba(5, 150, 105, 0.15); color: var(--green); }
.status-badge.booked { background: var(--accent-soft); color: var(--accent); }
.status-badge.active { background: rgba(217, 119, 6, 0.14); color: #b45309; }
.status-badge.hold { background: rgba(220, 38, 38, 0.12); color: var(--danger); }
.status-badge.mixed { background: rgba(100, 116, 139, 0.16); color: #475569; }

.empty { padding: 24px; text-align: center; color: var(--muted); }

/* --- Tables ----------------------------------------------------------------- */

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }

.view-shell { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.view-shell > .booked-toolbar,
.view-shell > .si-toolbar,
.view-shell > .finished-toolbar { flex: 0 0 auto; }

.pinned-view { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; padding-bottom: 16px; }
.pinned-head {
  flex: 0 0 auto;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
}
.pinned-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
}
.pinned-table { border-collapse: separate; border-spacing: 0; }
.pinned-table th { background: #f1f3f6; }
.pinned-table tbody tr:last-child td { border-bottom: none; }
/* The one horizontal scrollbar, pinned to the bottom edge of the window. */
.pinned-hscroll {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 16px;
  overflow-x: auto;
  overflow-y: hidden;
  z-index: 40;
  background: var(--bg);
  border-top: 1px solid var(--border);
}
.pinned-hscroll-spacer { height: 1px; }
.row-actions { text-align: right; width: 40px; }
.row-delete { color: var(--muted); }
.row-delete:hover { color: #dc2626; }

.data-table { border-collapse: collapse; width: 100%; font-size: 12.5px; }
.data-table th, .data-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}
.data-table th { background: #f1f3f6; font-weight: 600; position: sticky; top: 0; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr.clickable { cursor: pointer; }
.data-table tbody tr.clickable:hover { background: var(--accent-soft); }

/* --- Detail drawer ---------------------------------------------------------- */

.wb-drawer-wrap { min-width: 0; }

.drawer {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* Layout B: the drawer becomes a slide-over on the right, shown only when a
   table row has been clicked. */
.wb-body[data-layout="B"] .drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 460px;
  max-width: 92vw;
  transform: translateX(100%);
  transition: transform 0.25s ease;
  box-shadow: -8px 0 24px rgba(15, 23, 42, 0.18);
  z-index: 60;
  overflow-y: auto;
  border-radius: 0;
}
.wb-body[data-layout="B"] .drawer.open { transform: translateX(0); }

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}
.drawer-actions { display: flex; gap: 6px; }

.drawer-body { padding: 12px; }

.drawer-empty { padding: 32px 12px; text-align: center; color: var(--muted); }

.drawer-title { font-weight: 700; }
.drawer-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.drawer-section-label { font-weight: 600; margin: 14px 0 6px; font-size: 13px; color: #334155; }
.drawer-section-label:first-child { margin-top: 0; }

.split-switcher { display: flex; gap: 6px; flex-wrap: wrap; }
.split-chip {
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: transparent;
  cursor: pointer;
  font-size: 12px;
}
.split-chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* --- Form fields ------------------------------------------------------------ */

.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; }
.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field-inline { flex-direction: row; align-items: center; gap: 6px; }
.field-label { font-size: 11px; color: var(--muted); }
.field-orig { font-size: 11px; color: var(--muted); opacity: 0.85; }
.field-hint { font-size: 11px; line-height: 1.45; opacity: 0.85; }
/* A PO line whose destination or quantity needs a human check. */
.data-table tr.line-check td { background: rgba(217, 119, 6, 0.10); }
.data-table tr.line-check td.line-flag { color: #b45309; font-weight: 700; text-align: center; width: 22px; }
.manual-copy { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.manual-copy textarea { width: 100%; box-sizing: border-box; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; line-height: 1.5; }
.mail-preview { margin-top: 8px; font-size: 12px; }
.mail-preview table { border-collapse: collapse; }
.mail-preview th, .mail-preview td { border: 1px solid var(--border, #d0d5dd); padding: 4px 8px; text-align: left; }
.field-input {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  width: 100%;
  background: #fff;
}
.field-input:focus { outline: none; border-color: var(--accent); }
.field-check { width: 15px; height: 15px; }

/* --- Banners ---------------------------------------------------------------- */

.conflict-banner, .warn-banner, .info-banner, .success-banner {
  padding: 8px 12px;
  border-radius: 6px;
  margin-bottom: 10px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.conflict-banner { background: rgba(220, 38, 38, 0.1); border-left: 3px solid var(--danger); color: var(--danger); }
.warn-banner { background: rgba(180, 83, 9, 0.1); border-left: 3px solid var(--amber); color: var(--amber); }
.info-banner { background: var(--accent-soft); border-left: 3px solid var(--accent); color: var(--accent); }
.success-banner { background: rgba(5, 150, 105, 0.12); border-left: 3px solid var(--green); color: var(--green); }

/* --- Split shipment sub-modal ---------------------------------------------- */

.split-modal {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.split-modal-title { font-weight: 700; margin-bottom: 4px; }
.split-modal-row { margin: 8px 0; }
.split-modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* --- Modals ----------------------------------------------------------------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 100;
  overflow-y: auto;
}
.modal {
  background: var(--surface);
  border-radius: 10px;
  width: 720px;
  max-width: 100%;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}
.po-import-modal { width: 880px; }
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-body { padding: 16px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }

/* Booking modal specifics */
.lines-editor { display: flex; flex-direction: column; gap: 6px; }
.line-row { display: flex; gap: 8px; align-items: center; }
.holiday-row { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; }
.booked-list { display: flex; flex-direction: column; gap: 4px; }
.booked-list-row { display: flex; justify-content: space-between; padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; }
.schedule-form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; }
.schedule-preview { margin-top: 12px; }
.holiday-editor { margin-top: 4px; }

/* Holiday settings modal */
.holiday-days { text-align: right; white-space: nowrap; }
.holiday-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }

/* PO import specifics */
.drop-zone {
  border: 2px dashed var(--accent);
  border-radius: 8px;
  padding: 28px;
  text-align: center;
  cursor: pointer;
  color: var(--muted);
  margin: 12px 0;
}
.drop-zone.drag { background: var(--accent-soft); }
.drop-icon { font-size: 28px; }
.mode-toggle { display: inline-flex; gap: 6px; margin-bottom: 10px; }
.match-choices { display: flex; flex-direction: column; gap: 8px; }
.match-row { display: grid; grid-template-columns: 1.4fr auto 1fr; gap: 8px; align-items: center; }

/* Generated-schedule preview under the "Generate Schedule from PO EXW" form. */
.schedule-preview { margin-top: 10px; }
.preview-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.preview-table { margin-top: 6px; }
.preview-table td:first-child { white-space: nowrap; }
.preview-extra td { color: var(--muted); }

/* --- Login overlay ---------------------------------------------------------- */

.login-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.login-box {
  background: var(--surface);
  border-radius: 10px;
  padding: 24px;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-box h2 { margin: 0; font-size: 18px; }
.login-error { color: var(--danger); font-size: 12px; min-height: 16px; }

/* --- Toasts ----------------------------------------------------------------- */

.toast-root {
  position: fixed;
  bottom: 16px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 300;
}
.toast {
  padding: 10px 14px;
  border-radius: 6px;
  background: #1e293b;
  color: #fff;
  font-size: 13px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  max-width: 360px;
}
.toast-success { background: var(--green); }
.toast-error { background: var(--danger); }
.toast-info { background: #1e293b; }

/* --- Responsive ------------------------------------------------------------- */

@media (max-width: 1400px) {
  .wb-body[data-layout="A"] { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 1080px) {
  .wb-body[data-layout="A"] { grid-template-columns: 1fr; }
  .field-grid { grid-template-columns: 1fr; }
}
