:root {
  --navy: #0f2942;
  --blue: #1c4e80;
  --accent: #2d81c4;
  --green: #2f8f4e;
  --amber: #c98a1a;
  --red: #c0392b;
  --gray: #6b7280;
  --border: #e2e5ea;
  --bg: #f5f7fa;
  --row-divider: #b9c0ca;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: #1a1a1a;
}

.hidden { display: none !important; }

/* ---- Login ---- */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--navy), var(--blue));
}

.login-card {
  background: #fff;
  border-radius: 12px;
  padding: 40px;
  width: 380px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.25);
  text-align: center;
}

.login-card h1 { font-size: 20px; margin: 0 0 4px; color: var(--navy); }
.subtitle { color: var(--gray); margin: 0 0 24px; font-size: 14px; }

.role-picker { display: flex; gap: 10px; }
.role-btn {
  flex: 1;
  padding: 16px 8px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: #fafbfc;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  color: var(--navy);
}
.role-btn:hover { border-color: var(--accent); background: #eef6fc; }
.role-btn small { font-weight: 400; color: var(--gray); }

#login-form { text-align: left; margin-top: 20px; }
#login-form label { display: block; font-size: 13px; color: var(--gray); margin-bottom: 10px; }
#login-form input[type="text"], #login-form input[type="password"] {
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}
.login-actions { display: flex; gap: 8px; margin-top: 6px; }
.login-actions button { flex: 1; padding: 10px; border-radius: 8px; border: none; font-weight: 600; cursor: pointer; }
#login-back { background: #eee; color: #333; }
#login-submit { background: var(--accent); color: #fff; }
.error { color: var(--red); font-size: 13px; margin-bottom: 8px; }

/* ---- Dashboard ---- */
.topbar {
  background: var(--navy);
  color: #fff;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.topbar-title { display: flex; align-items: center; gap: 12px; }
.topbar h1 { font-size: 18px; margin: 0; }
.badge {
  background: var(--accent);
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.5px;
}
.role-icon { margin-right: 4px; }
.admin-crown {
  filter: drop-shadow(0 0 2px #ffd700);
}
.topbar-actions { display: flex; align-items: center; gap: 14px; }
.refresh-indicator { font-size: 12px; color: #b9cbe0; }
button.secondary {
  background: transparent;
  border: 1px solid #4a6b8a;
  color: #fff;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
}

.upload-banner {
  margin: 16px 24px 0;
  padding: 10px 16px;
  border-radius: 8px;
  background: #eaf3fb;
  border: 1px solid #cfe3f5;
  color: var(--navy);
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  padding: 0;
}

.panel {
  background: #fff;
  margin: 16px 24px 0;
  padding: 16px 20px;
  border-radius: 10px;
  border: 1px solid var(--border);
}
.panel h2 { font-size: 15px; margin: 0 0 6px; }
.muted { color: var(--gray); font-size: 13px; }
#upload-form { display: flex; gap: 10px; margin-top: 10px; align-items: center; }
#upload-form button {
  background: var(--green);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
}
#upload-status { margin-top: 8px; }

.legend { padding: 12px 24px 0; display: flex; gap: 18px; align-items: center; flex-wrap: wrap; }
.legend-item { font-size: 12px; color: var(--gray); display: flex; align-items: center; gap: 6px; }
.legend-right { margin-left: auto; }
.dot.at-risk { background: #e0631f; }

.stats-bar { padding: 10px 24px 0; display: flex; gap: 10px; flex-wrap: wrap; }
.stat {
  font-size: 12px;
  color: var(--navy);
  background: #f0f3f7;
  padding: 4px 10px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.stat strong { font-size: 13px; }
.stat-at-risk { background: #fce7d9; color: #a1470f; }

.toolbar { padding: 12px 24px 0; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.toolbar select, .toolbar input[type="text"] {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
}
#search-box { flex: 1; min-width: 200px; max-width: 360px; }
.toolbar-spacer { flex: 1; }
.secondary-btn {
  background: #fff;
  border: 1px solid var(--border);
  color: var(--navy);
  padding: 7px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
}
.secondary-btn:hover { border-color: var(--accent); color: var(--accent); }

.bulk-toolbar {
  margin: 10px 24px 0;
  padding: 10px 16px;
  border-radius: 8px;
  background: #eaf3fb;
  border: 1px solid #cfe3f5;
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 13px;
  color: var(--navy);
}
.bulk-actions { display: flex; gap: 8px; }
.bulk-action-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  font-size: 12px;
}
#bulk-clear-btn { margin-left: auto; }

.status-pill.at-risk-flag { background: #e0631f; margin-left: 4px; animation: new-badge-pulse 1.8s ease-in-out infinite; }

.site-logo { max-width: 220px; max-height: 64px; margin-bottom: 10px; }
.site-logo-small { max-height: 28px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot.scheduled { background: var(--accent); }
.dot.delivered { background: var(--green); }
.dot.created { background: var(--amber); }

.table-wrap { margin: 12px 24px 40px; background: #fff; border-radius: 10px; border: 1px solid var(--border); overflow: auto; }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
th {
  text-align: left;
  padding: 8px 7px;
  background: #f0f3f7;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  color: var(--navy);
  white-space: nowrap;
  position: sticky;
  top: 0;
}
td { padding: 6px 7px; border-bottom: 2px solid var(--row-divider); vertical-align: middle; white-space: nowrap; }
tr:hover { background: #fafcfe; }
tbody tr { position: relative; }
tbody tr:last-child td { border-bottom: none; }

/* A truck emoji drives across the row when a delivery flips to Delivered. */
.truck-drive {
  position: absolute;
  top: 50%;
  left: -28px;
  transform: translateY(-50%);
  font-size: 18px;
  pointer-events: none;
  z-index: 5;
  animation: truck-drive-across 1.6s ease-in-out forwards;
}
@keyframes truck-drive-across {
  0% { left: -28px; opacity: 0; }
  12% { opacity: 1; }
  88% { opacity: 1; }
  100% { left: 100%; opacity: 0; }
}

td.po-cell { white-space: normal; max-width: 230px; min-width: 160px; }
.po-chip-row { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 4px; }
.po-chip-inline {
  display: inline-block;
  background: #eaf3fb;
  border: 1px solid #cfe3f5;
  color: var(--navy);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
}
.po-chip-inline:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.po-chip-inline.po-chip-copied { background: var(--green); color: #fff; border-color: var(--green); }
.po-more-link {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 10px;
  text-decoration: underline;
  padding: 0;
  white-space: nowrap;
}

.status-pill {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 20px;
  font-size: 10px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
}
.status-pill.scheduled { background: var(--accent); }
.status-pill.delivered { background: var(--green); }
.status-pill.created { background: var(--amber); }
.status-pill.not-scheduled { background: var(--red); }
.status-pill.manual-flag { background: var(--amber); margin-left: 4px; }

.new-badge {
  display: inline-block;
  background: var(--red);
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.5px;
  padding: 2px 6px;
  border-radius: 4px;
  margin-right: 5px;
  vertical-align: middle;
  animation: new-badge-pulse 1.8s ease-in-out infinite;
}
@keyframes new-badge-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

.cell-input {
  width: 68px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
}
.cell-input:disabled { background: #f5f5f5; color: #888; border-color: #eee; }
.appointment-input, .delivery-number-input { width: 108px; }
.appointment-cell { display: inline-block; border-radius: 6px; padding: 2px; }

.field-caption {
  font-size: 9px;
  color: var(--gray);
  margin-top: 2px;
  max-width: 68px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: default;
}

.field-flash { animation: field-flash-anim 2.5s ease; }
@keyframes field-flash-anim {
  0% { background: #fff4cc; border-color: #e8c94a; }
  100% { background: #fff; border-color: var(--border); }
}
.cell-input.saved { border-color: var(--green); }

.notes-link {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 12px;
  text-decoration: underline;
  padding: 1px 3px;
  margin-left: -3px;
  border-radius: 4px;
  white-space: nowrap;
}


input[type="checkbox"].confirm-box { width: 18px; height: 18px; cursor: pointer; }
input[type="checkbox"].confirm-box:disabled { cursor: not-allowed; opacity: 0.4; }

/* ---- Modals ---- */
.modal {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(15,41,66,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}
.modal-card {
  background: #fff;
  width: 420px;
  max-height: 80vh;
  border-radius: 10px;
  padding: 20px;
  display: flex;
  flex-direction: column;
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-header h3 { margin: 0; font-size: 16px; }
.icon-btn { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--gray); }

.notes-thread { overflow-y: auto; flex: 1; margin-bottom: 12px; max-height: 320px; }
.note-item { padding: 8px 10px; background: #f5f7fa; border-radius: 8px; margin-bottom: 8px; font-size: 13px; position: relative; }
.note-author { font-weight: 600; color: var(--navy); font-size: 12px; }
.note-time { color: var(--gray); font-size: 11px; margin-left: 6px; }
.note-text { margin-top: 3px; }
.delete-note-btn { position: absolute; top: 4px; right: 6px; font-size: 15px; line-height: 1; padding: 0 4px; }
.no-notes { color: var(--gray); font-size: 13px; }

#notes-form { display: flex; flex-direction: column; gap: 8px; }
#notes-text { resize: vertical; min-height: 60px; padding: 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; }
#notes-form button {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
}

.po-search {
  width: 100%;
  padding: 7px 10px;
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
}
.po-list {
  font-size: 14px;
  overflow-y: auto;
  max-height: 340px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px 12px;
}
.po-list .po-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: #f5f7fa;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.po-list .po-item:hover { background: #eaf3fb; border-color: var(--accent); }
.po-list .po-item.po-copied { background: #e4f5e9; border-color: var(--green); }
.po-list .po-item .po-index {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--navy);
  color: #fff;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.po-list .po-item .po-value { flex: 1; }
.po-list .po-item .po-copy-hint {
  font-size: 10px;
  color: var(--gray);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-weight: 400;
}
.po-modal-actions { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.po-modal-actions button {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 7px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
}

.modal-card-wide { width: 560px; }
.history-list { overflow-y: auto; max-height: 400px; }
.history-item {
  padding: 10px 12px;
  background: #f5f7fa;
  border-radius: 8px;
  margin-bottom: 8px;
  font-size: 13px;
}
.history-item .history-top { display: flex; justify-content: space-between; font-weight: 600; color: var(--navy); }
.history-item .history-meta { color: var(--gray); font-size: 12px; margin-top: 2px; }
.history-item .history-errors { color: var(--red); font-size: 12px; margin-top: 4px; }
.delete-upload-btn { font-size: 15px; margin-left: 6px; vertical-align: middle; }
.delete-upload-btn:hover { color: var(--red); }
.delete-delivery-btn { font-size: 18px; }
.delete-delivery-btn:hover { color: var(--red); }

.add-person-form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }
.add-person-form input[type="text"],
.add-person-form input[type="email"] {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  width: 100%;
}
.add-person-form select {
  grid-column: 1 / -1;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
}
.add-person-form button {
  grid-column: 1 / -1;
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 9px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
}
.new-person-result {
  background: #eef6fc;
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
  margin-bottom: 14px;
  color: var(--navy);
}
#people-list code,
.new-person-result code {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 12px;
  letter-spacing: 0.5px;
}

/* ---- Dark mode ---- */
/* The topbar is already a dark navy in both themes, so it needs no changes.
   Everything else gets a dark override here rather than fully re-theming
   with CSS variables, to keep this low-risk to the existing light styles. */
body.dark { background: #0b1622; color: #e6edf5; }
body.dark #login-screen { background: linear-gradient(135deg, #060d16, #0f2942); }
body.dark .login-card { background: #131f2e; color: #e6edf5; box-shadow: 0 20px 50px rgba(0,0,0,0.6); }
body.dark .login-card h1 { color: #e6edf5; }
body.dark .subtitle { color: #93a4b8; }
body.dark .role-btn { background: #16233d; border-color: #2a3d54; color: #e6edf5; }
body.dark .role-btn:hover { background: #1c2c40; border-color: var(--accent); }
body.dark .role-btn small { color: #93a4b8; }
body.dark #login-form label { color: #93a4b8; }
body.dark #login-form input[type="text"],
body.dark #login-form input[type="password"] {
  background: #0f1b28; border-color: #2a3d54; color: #e6edf5;
}
body.dark #login-back { background: #2a3d54; color: #e6edf5; }

body.dark .panel { background: #131f2e; border-color: #223244; }
body.dark .panel h2 { color: #e6edf5; }
body.dark .muted { color: #93a4b8; }
body.dark .legend-item { color: #93a4b8; }

body.dark .upload-banner { background: #16283a; border-color: #223855; color: #cfe0f0; }

body.dark .table-wrap { background: #131f2e; border-color: #223244; }
body.dark th { background: #16233d; color: #e6edf5; border-color: #223244; }
body.dark td { border-color: #3a4d64; color: #dbe4ee; }
body.dark tr:hover { background: #182635; }

body.dark .cell-input { background: #0f1b28; border-color: #2a3d54; color: #e6edf5; }
body.dark .cell-input:disabled { background: #131f2e; color: #6b7c8f; border-color: #223244; }
body.dark .field-caption { color: #8a99ab; }

body.dark .po-chip-inline { background: #16283a; border-color: #223855; color: #cfe0f0; }
body.dark .notes-link, body.dark .po-more-link, body.dark .link-btn { color: #6cb2ec; }

body.dark .modal { background: rgba(0,0,0,0.6); }
body.dark .modal-card { background: #131f2e; color: #e6edf5; }
body.dark .modal-header h3 { color: #e6edf5; }
body.dark .icon-btn { color: #93a4b8; }
body.dark .note-item { background: #16233d; }
body.dark .note-author { color: #cfe0f0; }
body.dark .no-notes { color: #93a4b8; }
body.dark #notes-text { background: #0f1b28; border-color: #2a3d54; color: #e6edf5; }

body.dark .po-search { background: #0f1b28; border-color: #2a3d54; color: #e6edf5; }
body.dark .po-list .po-item { background: #16233d; border-color: #2a3d54; }
body.dark .po-list .po-item:hover { background: #1c2c40; border-color: var(--accent); }

body.dark .history-item { background: #16233d; }
body.dark .history-item .history-top { color: #cfe0f0; }
body.dark .history-item .history-meta { color: #8a99ab; }

body.dark .add-person-form input[type="text"],
body.dark .add-person-form select { background: #0f1b28; border-color: #2a3d54; color: #e6edf5; }
body.dark .new-person-result { background: #16283a; border-color: var(--accent); color: #cfe0f0; }
body.dark #people-list code,
body.dark .new-person-result code { background: #0f1b28; border-color: #2a3d54; color: #e6edf5; }

body.dark .stat { background: #16233d; color: #dbe4ee; }
body.dark .stat-at-risk { background: #3a2313; color: #f3c08f; }
body.dark .toolbar select,
body.dark .toolbar input[type="text"] { background: #0f1b28; border-color: #2a3d54; color: #e6edf5; }
body.dark .secondary-btn { background: #16233d; border-color: #2a3d54; color: #e6edf5; }
body.dark .bulk-toolbar { background: #16283a; border-color: #223855; color: #cfe0f0; }

/* ---- Print (shift handoff) ---- */
@media print {
  .no-print,
  #login-screen,
  #bulk-toolbar,
  .modal { display: none !important; }
  body, body.dark { background: #fff !important; color: #000 !important; }
  .topbar { background: #fff !important; color: #000 !important; border-bottom: 2px solid #000; }
  .table-wrap { border: none; overflow: visible; }
  table { font-size: 11px; }
  th { background: #fff !important; color: #000 !important; position: static; }
  td { color: #000 !important; }
  .field-caption { color: #444 !important; }
  .cell-input { border-color: #999 !important; background: #fff !important; color: #000 !important; }
}
