:root {
  --border: #d0d5dd;
  --text: #1a1a1a;
  --muted: #667085;
  --accent: #2f6fed;
  --error: #b42318;
  --verified: #067647;
  --mismatch: #b42318;
  --neutral: #667085;
  --bg-card: #fafafa;
}

* { box-sizing: border-box; }

body {
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  margin: 0;
}

h1 { margin-top: 1.5rem; }

.hint { color: var(--muted); font-size: 0.9rem; }

.app-shell {
  display: flex;
  align-items: stretch;
  min-height: 100vh;
}

.sidebar {
  flex: 0 0 220px;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 1.5rem 1rem;
  border-right: 1px solid var(--border);
  background: var(--bg-card);
  overflow-y: auto;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}
.sidebar .brand { font-weight: 700; }
.sidebar a {
  color: var(--text);
  text-decoration: none;
  padding: 0.5rem 0.6rem;
  border-radius: 6px;
  white-space: nowrap;
}
.sidebar a:hover { color: var(--accent); background: white; }
.sidebar a,
.sidebar .logout-form button {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.nav-icon { width: 18px; height: 18px; flex-shrink: 0; }

.sidebar-nav { display: flex; flex-direction: column; gap: 0.15rem; }

/* licensetag API reachability (src/backendStatus.js). While it's down,
   every section is hidden -- only Server + Log out remain. */
.server-status {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.6rem;
  margin-bottom: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
}
.status-badge {
  margin-left: auto;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  color: white;
}
.status-badge::before { content: "● "; }
.status-badge.on { background: var(--verified); }
.status-badge.off { background: var(--error); }
.backend-down-hint { display: none; margin: 0 0 0.5rem; padding: 0 0.6rem; }
.sidebar.backend-down .sidebar-nav { display: none; }
.sidebar.backend-down .backend-down-hint { display: block; }

/* Separates the everyone-links from the admin-only group below them. */
.sidebar-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.75rem 0 0.5rem;
}
.sidebar-section-label {
  padding: 0 0.6rem 0.25rem;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

/* Minimize/expand button -- one in the topnav (always reachable, even once
   the sidebar itself is hidden/off-screen) and one inside the sidebar
   header (mobile-only "X", see the media query below). */
.sidebar-toggle {
  flex-shrink: 0;
  width: 2rem;
  height: 2rem;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  color: var(--text);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
}
.sidebar-toggle:hover { border-color: var(--accent); color: var(--accent); }
.sidebar-close { display: none; }

.sidebar-backdrop { display: none; }

/* Desktop: "collapsed" just removes the sidebar from the flex row entirely
   -- .app-main (flex: 1) fills the freed space on its own, no extra rule
   needed for it. */
.app-shell.sidebar-collapsed .sidebar { display: none; }

.app-main {
  flex: 1;
  min-width: 0;
  padding: 0 1.5rem 3rem;
}
.app-main > main {
  max-width: 720px;
  margin: 0 auto;
}

.topnav {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  max-width: 720px;
  margin: 0 auto;
  padding: 1rem 0;
  border-bottom: 1px solid var(--border);
}
.topnav a { color: var(--text); text-decoration: none; }
.topnav a:hover { color: var(--accent); }
/* Sits at the end of the sidebar's link list -- margin-top: auto pushes it
   down to the bottom of the flex column regardless of how many links are
   above it, rather than just trailing right after "Hướng dẫn". */
.sidebar .logout-form { margin-top: auto; padding-top: 1rem; }

/* Mobile: the sidebar becomes an off-canvas overlay instead of a permanent
   column -- `position: fixed` also takes it out of .app-shell's flex flow,
   so .app-main naturally reclaims the full width with no extra rule. */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 60;
    width: 220px;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 16px rgba(0, 0, 0, 0.2);
  }
  /* Inverted from desktop: absence of .sidebar-collapsed is the OPEN state
     here (off-canvas by default, see the base .sidebar rule above), so
     "collapsed" can't just be `display: none` -- that would break the
     slide transition on the way back in. */
  .app-shell.sidebar-collapsed .sidebar { display: flex; transform: translateX(-100%); }
  .app-shell:not(.sidebar-collapsed) .sidebar { transform: translateX(0); }

  .sidebar-close { display: inline-block; }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 55;
    background: rgba(0, 0, 0, 0.4);
  }
  .app-shell.sidebar-collapsed .sidebar-backdrop { display: none; }
  .app-shell:not(.sidebar-collapsed) .sidebar-backdrop { display: block; }
}
.logout-form button {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 0.9rem; padding: 0;
}
.logout-form button:hover { color: var(--error); }
/* Matches the sidebar's own <a> links (full-width, padded, rounded hover)
   instead of the bare text-button look above, which was tailored for
   sitting inline in the topnav. */
.sidebar .logout-form button {
  width: 100%;
  text-align: left;
  padding: 0.5rem 0.6rem;
  border-radius: 6px;
}
.sidebar .logout-form button:hover { background: white; }

.card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem;
  margin: 1rem 0;
  background: var(--bg-card);
}

form.card label { display: block; margin-top: 0.75rem; font-weight: 600; font-size: 0.9rem; }
form.card label.checkbox { font-weight: 400; display: flex; align-items: center; gap: 0.5rem; }
form.card input[type="text"],
form.card input[type="email"],
form.card input[type="password"],
form.card input[type="file"] {
  width: 100%;
  padding: 0.5rem;
  margin-top: 0.25rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1rem;
}
form.card fieldset.optional {
  margin-top: 1rem;
  border: 1px dashed var(--border);
  border-radius: 6px;
  padding: 0.75rem 1rem 1rem;
}
form.card fieldset.optional legend { color: var(--muted); font-size: 0.85rem; padding: 0 0.4rem; }

button[type="submit"] {
  margin-top: 1.25rem;
  padding: 0.6rem 1.25rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
}
button[type="submit"]:hover { opacity: 0.9; }
button[type="submit"]:disabled { opacity: 0.45; cursor: not-allowed; }
/* Loading state set by public/forms.js while a form's request is in flight
   -- spinner before the label; less faded than a plain disabled button so
   it reads as "working", not "unavailable". */
button.is-loading { display: inline-flex; align-items: center; gap: 0.5rem; }
button.is-loading:disabled { opacity: 0.75; cursor: progress; }
button.is-loading::before {
  content: "";
  width: 0.9em;
  height: 0.9em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: button-spin 0.7s linear infinite;
}
@keyframes button-spin { to { transform: rotate(360deg); } }
/* Destructive action (e.g. Update order's "Rebuild order files", which
   trashes the order folder's files) -- red so it isn't mistaken for the
   page's ordinary submit button. */
button[type="submit"].danger { background: var(--error); }

.rebuild-form { margin-top: 1rem; border-top: 1px solid var(--border); padding-top: 0.5rem; }

.error {
  color: var(--error);
  background: #fef3f2;
  border: 1px solid #fecdca;
  border-radius: 6px;
  padding: 0.6rem 0.9rem;
}

dl.report { margin: 0; }
dl.report dt { font-weight: 600; margin-top: 0.9rem; }
dl.report dd { margin: 0.2rem 0 0; }
dl.report ul { margin: 0.2rem 0; padding-left: 1.2rem; }

table.entries { width: 100%; border-collapse: collapse; margin-top: 1rem; font-size: 0.9rem; }

.order-table {
  display: flex;
  justify-content: center;
}
.order-table table.entries { width: auto; max-width: 100%; margin: 0 auto; }
table.entries th, table.entries td {
  text-align: left; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border);
}
table.entries code { font-size: 0.85em; }
table.entries .col-drive {
  max-width: 12rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Below this width a normal <table> can't fit its columns -- every table
   using .entries (jobs list + job detail, gdrive, orders, read.ejs' match
   table) switches to one bordered card per row, with each <td> labeled by
   its own th's text via a `data-label` attribute (see the .ejs files) since
   thead itself is hidden. Cells that are pure actions (a "View items" link,
   a "Retry" button) have no data-label on purpose -- attr() on a missing
   attribute renders as empty, so their ::before is silently blank. */
@media (max-width: 700px) {
  .order-table { display: block; }
  table.entries { width: 100%; }
  table.entries thead { display: none; }
  table.entries, table.entries tbody, table.entries tr {
    display: block;
    width: 100%;
  }
  table.entries tr {
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 0.75rem;
    padding: 0.4rem 0.75rem;
    background: white;
  }
  table.entries td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    padding: 0.45rem 0;
    border-bottom: 1px dashed var(--border);
    text-align: right;
  }
  table.entries td:last-child { border-bottom: none; }
  table.entries td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--muted);
    text-align: left;
    flex-shrink: 0;
  }
  /* Action-only cells (no data-label) don't need the label slot at all --
     let the button/link take the full row width. */
  table.entries td:not([data-label]) {
    display: block;
    text-align: right;
  }
  table.entries td.col-drive {
    max-width: none;
    white-space: normal;
    overflow: visible;
    text-overflow: unset;
  }
}

.combo-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.6rem;
  margin: 0.5rem 0 0.25rem;
}
.combo-card {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.5rem;
  background: white;
  cursor: pointer;
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.95rem;
  user-select: none;
}
.combo-card:hover { border-color: var(--accent); }
/* :has() support (all evergreen browsers) highlights the whole card when
   its checkbox is checked -- the checkbox itself stays visible too, this
   is a bonus affordance, not the only way to tell what's selected. */
.combo-card:has(input:checked) {
  border-color: var(--accent);
  background: #eef3ff;
  color: var(--accent);
}
.combo-card input { margin: 0; }
/* Update order page: a part the order already has -- shown checked but
   locked, so it reads as "already bought" rather than "selected to add". */
.combo-card.owned {
  cursor: not-allowed;
  border-style: dashed;
  background: #f3f4f6;
  color: var(--muted);
}
.combo-card.owned:hover { border-color: var(--border); }

/* Update order page: link input + Search button on one line. */
.search-row { display: flex; gap: 0.5rem; align-items: center; }
form.card .search-row input[type="text"] { flex: 1; margin-top: 0; }
.search-row button[type="submit"] { margin-top: 0; white-space: nowrap; }
/* Order check page: "Only mismatches" checkbox inside the search row. */
form.card .search-row label.checkbox { margin-top: 0; white-space: nowrap; }
/* Orders page: column select + keyword input + Search on one line. */
.order-search .search-row select {
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1rem;
  background: var(--bg-card);
}
.order-search .hint { margin: 0.5rem 0 0; }
/* Mobile (same breakpoint as the card-style tables below): one line is too
   narrow -- the keyword input gets squeezed and Order check's checkbox
   label overflows. Wrap instead: input on its own full-width first row,
   checkbox (Order check) on the next, then select / button / Clear share
   the last row, with select + button splitting it as wide tap targets. */
@media (max-width: 700px) {
  .order-search .search-row { flex-wrap: wrap; }
  form.card.order-search .search-row input[type="text"] { flex: 1 1 100%; order: -1; }
  form.card.order-search .search-row label.checkbox { flex: 1 1 100%; white-space: normal; }
  .order-search .search-row select,
  .order-search .search-row button[type="submit"] { flex: 1 1 0; min-width: 0; }
  .order-search .search-row button[type="submit"] { justify-content: center; }
  .order-search .search-row a { padding: 0.5rem 0.25rem; }
}
form.card input[readonly] { background: #f3f4f6; color: var(--muted); }

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}
.badge-verified { background: #ecfdf3; color: var(--verified); }
.badge-mismatch { background: #fef3f2; color: var(--mismatch); }
.badge-neutral { background: #f2f4f7; color: var(--neutral); }

.pager {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin: 1rem 0;
}
.pager a { color: var(--accent); text-decoration: none; }
.pager a:hover { text-decoration: underline; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
}
/* An author `display` rule beats the browser's own `[hidden] { display:
   none }` default regardless of the hidden attribute being set -- without
   this, the modal above shows up immediately on page load (hidden or not)
   and toggling `.hidden` in JS (Cancel/Confirm) has no visible effect. */
.modal-overlay[hidden] { display: none; }
.modal-box {
  background: white;
  border-radius: 8px;
  padding: 1.5rem;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
.modal-box h2 { margin-top: 0; }

/* List books page: Sync + Create side by side. #create-open isn't a
   submit button (it opens the Create dialog), so it gets the submit look here. */
.book-actions { display: flex; gap: 0.75rem; align-items: flex-start; margin: 1rem 0; }
.book-actions button[type="submit"] { margin-top: 0; }
#create-open {
  padding: 0.6rem 1.25rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
}
#create-open:hover { opacity: 0.9; }
#create-open:disabled { opacity: 0.45; cursor: not-allowed; }
/* Create dialog fields: .modal-box is also a form.card, so text inputs are
   already styled -- give the Number select and the Key input the same look. */
#create-modal select {
  width: 100%;
  padding: 0.5rem;
  margin-top: 0.25rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 1rem;
  background: white;
}
#create-modal #create-key { text-transform: uppercase; }
#create-modal .modal-actions button { margin-top: 0; }
#create-cancel { background: white; border: 1px solid var(--border); color: var(--text); }

/* List books page: per-row "Edit" (title) button + its dialog. */
.title-edit {
  margin-left: 0.4rem;
  padding: 0.1rem 0.5rem;
  font-size: 0.8rem;
  background: white;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--accent);
  cursor: pointer;
}
.title-edit:hover { border-color: var(--accent); }
#title-modal .modal-actions button { margin-top: 0; }
#title-cancel { background: white; border: 1px solid var(--border); color: var(--text); }

/* List books page: admin-only "Waiting for review" section. */
.pending-review { border-left: 4px solid var(--accent); }
.pending-review h2 { margin-top: 0; font-size: 1.1rem; }
.pending-review td .hint { display: block; font-size: 0.8rem; margin-top: 0.2rem; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  margin-top: 1.25rem;
}
.modal-actions button { padding: 0.5rem 1.1rem; border-radius: 6px; font-size: 0.95rem; cursor: pointer; }
.modal-actions #confirm-cancel { background: white; border: 1px solid var(--border); color: var(--text); }
.modal-actions #confirm-cancel:hover { border-color: var(--muted); }
.modal-actions #confirm-ok { background: var(--accent); border: none; color: white; }
.modal-actions #confirm-ok:hover { opacity: 0.9; }

/* Item-status filter buttons on job_upload_file_stamp.ejs' job detail view.
   Plain links (GET ?status=) styled as a pill row; .active = current filter. */
.status-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 1rem 0;
}
.status-filter a {
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  color: var(--text);
  text-decoration: none;
  font-size: 0.9rem;
}
.status-filter a:hover { border-color: var(--muted); }
.status-filter a.active { background: var(--accent); border-color: var(--accent); color: white; }
.status-filter a .count { opacity: 0.75; margin-left: 0.25rem; }
