:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1c2330;
  --muted: #667085;
  --primary: #2456d6;
  --primary-text: #ffffff;
  --danger: #c0352b;
  --ok-bg: #e2f4e8;
  --ok-text: #17663a;
  --warn-bg: #fdf0d5;
  --warn-text: #8a5a00;
  --info-bg: #e3ecfd;
  --info-text: #1d47ad;
  --neutral-bg: #e9ecf1;
  --neutral-text: #475467;
  --error-bg: #fbe4e2;
  --error-text: #9a2820;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1a1f28;
    --surface-2: #222835;
    --border: #2e3644;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --primary: #5b8cff;
    --primary-text: #0b1020;
    --danger: #ff7b72;
    --ok-bg: #173524;
    --ok-text: #7ee2a8;
    --warn-bg: #3a2c0c;
    --warn-text: #f5c66b;
    --info-bg: #17284f;
    --info-text: #9dbcff;
    --neutral-bg: #2a303c;
    --neutral-text: #b6bfcc;
    --error-bg: #3d1a18;
    --error-text: #ffaaa3;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1 { font-size: 1.45rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 0.75rem; }
p { margin: 0 0 0.75rem; }
a { color: var(--primary); }
.muted { color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Layout */
.layout { display: flex; min-height: 100%; }

.sidebar {
  width: 240px;
  flex: 0 0 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.brand { font-weight: 700; font-size: 1.2rem; letter-spacing: 0.04em; }
.role-line { font-size: 0.85rem; color: var(--muted); margin: 0; }

.nav { display: flex; flex-direction: column; gap: 2px; margin-top: 0.5rem; }
.nav-title {
  margin: 0.9rem 0 0.2rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.nav a {
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
  color: var(--text);
  text-decoration: none;
}
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--info-bg); color: var(--info-text); font-weight: 600; }

.sidebar-foot {
  margin-top: auto;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.main { flex: 1; min-width: 0; padding: 1.5rem; outline: none; }

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.page-head .sub { margin: 0.2rem 0 0; color: var(--muted); }
.actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }

.toolbar { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: end; margin-bottom: 1rem; }
.toolbar .field { min-width: 180px; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
  margin-bottom: 1rem;
}
.card.flush { padding: 0; overflow: hidden; }

.rate-card { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.75rem; }
tfoot tr.estimate td { font-weight: 400; color: var(--muted); border-top: 0; }
tfoot tr.plain td { font-weight: 400; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; margin: 1.25rem 0 0.6rem; }
.section-head h2 { margin: 0; }

.logo-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 0.75rem; margin-bottom: 1rem; }
.logo-card { display: flex; gap: 0.9rem; align-items: center; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.9rem; }
/* Logos are made for white paper, so the preview is white in both themes. */
.logo-preview { flex: 0 0 120px; height: 72px; display: grid; place-items: center; background: #fff; border: 1px dashed var(--border); border-radius: 6px; overflow: hidden; }
.logo-preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
.logo-preview .muted { color: #667085; font-size: 0.8rem; }
.logo-info { display: grid; gap: 0.3rem; min-width: 0; }
tfoot tr + tr td { border-top: 0; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 0.75rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.9rem 1rem; }
.stat .label { color: var(--muted); font-size: 0.85rem; }
.stat .value { font-size: 1.6rem; font-weight: 650; font-variant-numeric: tabular-nums; }

.meta { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 0.75rem 1.25rem; margin: 0; }
.meta dt { color: var(--muted); font-size: 0.8rem; }
.meta dd { margin: 0; font-weight: 550; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.6rem 0.8rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
/* Keep names readable instead of wrapping one word per line; the table scrolls. */
td:first-child { min-width: 13rem; }
/* A column of checkboxes to pick rows stays narrow. */
td:first-child:has(> input[type="checkbox"]), th:first-child:has(> input[type="checkbox"]) { min-width: 0; width: 2.75rem; }
.row-actions .btn { white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: var(--surface-2); }
tfoot td { font-weight: 650; border-top: 2px solid var(--border); border-bottom: 0; }
.empty { padding: 2rem 1rem; text-align: center; color: var(--muted); }
.row-actions { display: flex; gap: 0.35rem; justify-content: flex-end; }

/* Badges */
.badge { display: inline-block; padding: 0.12rem 0.55rem; border-radius: 999px; font-size: 0.78rem; font-weight: 600; white-space: nowrap; }
.badge-neutral { background: var(--neutral-bg); color: var(--neutral-text); }
.badge-info { background: var(--info-bg); color: var(--info-text); }
.badge-warn { background: var(--warn-bg); color: var(--warn-text); }
.badge-ok { background: var(--ok-bg); color: var(--ok-text); }
.badge-error { background: var(--error-bg); color: var(--error-text); }

/* Buttons */
.btn {
  font: inherit;
  padding: 0.5rem 0.9rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-small { padding: 0.25rem 0.55rem; font-size: 0.85rem; }
.btn-block { width: 100%; }

/* Forms */
.form { display: grid; gap: 0.8rem; }
.field { display: grid; gap: 0.25rem; }
.field > span { font-size: 0.82rem; color: var(--muted); }
.field.check { display: flex; align-items: center; gap: 0.5rem; }
.field.check > span { font-size: inherit; color: var(--text); }
input[type="text"], input[type="password"], input[type="number"], input[type="email"], input[type="date"], input[type="search"], select, textarea {
  font: inherit;
  width: 100%;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
}
textarea { min-height: 4.5rem; resize: vertical; }

/* A search box: wider than the other filters, with a magnifying glass. */
.field.search { flex: 1 1 280px; max-width: 460px; }
.field.search input {
  padding-left: 2.1rem;
  border-radius: 999px;
  background-color: var(--surface);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2398a2b3' stroke-width='2.2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 0.7rem center;
  background-size: 1rem;
  box-shadow: var(--shadow);
  -webkit-appearance: none;
  appearance: none;
}
.field.search input::placeholder { color: var(--muted); }
.field.search input:focus { border-color: var(--primary); }
/* A number typed inside a table row. */
td input.cell-input { width: 5.5rem; padding: 0.25rem 0.4rem; text-align: right; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.hint { font-size: 0.8rem; color: var(--muted); }

.alert { padding: 0.6rem 0.8rem; border-radius: 6px; margin: 0; }
.alert-error { background: var(--error-bg); color: var(--error-text); }
.alert-info { background: var(--info-bg); color: var(--info-text); }
.alert-ok { background: var(--ok-bg); color: var(--ok-text); }

/* Dialog */
dialog {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  padding: 1.25rem;
  width: min(480px, calc(100vw - 2rem));
}
dialog::backdrop { background: rgba(10, 14, 22, 0.55); }
dialog h2 { margin-bottom: 1rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.5rem; }

/* Toasts */
.toasts { position: fixed; right: 1rem; bottom: 1rem; display: grid; gap: 0.5rem; z-index: 50; max-width: min(360px, calc(100vw - 2rem)); }
.toast { padding: 0.65rem 0.9rem; border-radius: 8px; background: var(--text); color: var(--bg); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25); }
.toast.error { background: var(--danger); color: #fff; }

/* Login */
.login-page { display: grid; place-items: center; padding: 1rem; }
.login-card { width: min(360px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 1.75rem; box-shadow: var(--shadow); }
.login-card h1 { margin-bottom: 0.25rem; letter-spacing: 0.04em; }
.login-card .alert { margin-bottom: 0.75rem; }

/* Phone: a bar on top, and the menu slides in from the left. */
.topbar, .scrim { display: none; }

@media (max-width: 760px) {
  .layout { display: block; }

  .topbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    position: sticky;
    top: 0;
    z-index: 30;
    padding: 0.5rem 0.75rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
  }
  .menu-button {
    display: grid;
    place-items: center;
    width: 2.6rem;
    height: 2.6rem;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
  }
  .topbar-title { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 40;
    width: min(300px, 85vw);
    overflow-y: auto;
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.3);
    transform: translateX(-100%);
    /* Hidden, a closed menu cannot take the keyboard's focus. */
    visibility: hidden;
    transition: transform 0.2s ease, visibility 0s 0.2s;
  }
  body.menu-open .sidebar { transform: none; visibility: visible; transition: transform 0.2s ease; }
  body.menu-open .scrim { display: block; position: fixed; inset: 0; z-index: 35; background: rgba(10, 14, 22, 0.55); }
  body.menu-open { overflow: hidden; }
  .nav a { padding: 0.6rem 0.6rem; }

  .main { padding: 1rem 0.75rem; }
  h1 { font-size: 1.25rem; }

  /* Under 16px, iPhones zoom the page in when a field is touched. */
  input, select, textarea { font-size: 16px; }
  .toolbar .field, .field.search { flex: 1 1 100%; min-width: 0; max-width: none; }

  th, td { padding: 0.5rem 0.6rem; }
  td:first-child { min-width: 10rem; }
  .card { padding: 0.85rem; }
  .stat .value { font-size: 1.35rem; }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none !important; }
}

/* Under the company selector, for system administrators. */
#new-company { width: 100%; margin: 0.4rem 0 0.2rem; }

/* A choice found by typing: the product of an order line. */
.pick { position: relative; display: grid; gap: 0.3rem; }
.pick input[type="search"] { -webkit-appearance: none; appearance: none; }
.pick-list {
  max-height: 15rem;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.pick-item {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.45rem 0.6rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.pick-item:last-child { border-bottom: 0; }
.pick-item:hover, .pick-item.active { background: var(--info-bg); color: var(--info-text); }
.pick-detail { flex: none; color: var(--muted); font-size: 0.85rem; white-space: nowrap; font-variant-numeric: tabular-nums; }
.pick-item:hover .pick-detail, .pick-item.active .pick-detail { color: inherit; }
.pick-detail.danger, .pick-chosen.danger { color: var(--danger); }
.pick-empty { padding: 0.5rem 0.6rem; color: var(--muted); font-size: 0.9rem; }
.pick-chosen { font-size: 0.85rem; color: var(--muted); }
.field > label { font-size: 0.82rem; color: var(--muted); }
/* Room for a product's code, name and stock on one line. */
dialog:has(.pick) { width: min(40rem, calc(100vw - 2rem)); }

/* Told once to a page left open across an update. */
.new-version {
  position: fixed;
  left: 50%;
  bottom: 1rem;
  transform: translateX(-50%);
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 1rem;
  max-width: calc(100vw - 2rem);
  border: 1px solid var(--border);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}

/* The customer search of accounts receivable, with its "see all" button. */
.customer-filter { display: flex; align-items: flex-start; gap: 0.5rem; }
.customer-filter .pick { flex: 1; }
.customer-filter .pick-list { position: absolute; top: 100%; left: 0; right: 0; z-index: 20; margin-top: 2px; }

/* Product photos: a thumbnail in the list, and the gallery of one product. */
.product-cell { display: flex; align-items: center; gap: 0.6rem; }
.thumb {
  flex: none;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  display: grid;
  place-items: center;
  border: 1px dashed var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--muted);
  overflow: hidden;
  cursor: pointer;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.photos-dialog { width: min(46rem, calc(100vw - 2rem)); }
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 0.75rem; margin-bottom: 0.75rem; }
.photo-card { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface-2); }
.photo-card img { display: block; width: 100%; aspect-ratio: 1; object-fit: cover; background: #fff; }
.photo-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.35rem; padding: 0.45rem; }

/* The bell: in the side menu on a computer, in the top bar on a phone. */
.brand-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.bell {
  position: relative;
  display: grid;
  place-items: center;
  width: 2.4rem;
  height: 2.4rem;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.bell:hover { background: var(--surface-2); }
.bell[hidden], .bell-count[hidden] { display: none; }
.bell-count {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 1.15rem;
  padding: 0 0.25rem;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1.15rem;
  text-align: center;
}
.topbar .bell { margin-left: auto; flex: none; }
.notices-dialog { width: min(32rem, calc(100vw - 2rem)); }
.push-section { display: grid; justify-items: start; gap: 0.5rem; padding-bottom: 0.9rem; margin-bottom: 0.9rem; border-bottom: 1px solid var(--border); }
.push-section p { margin: 0; font-size: 0.9rem; }
.notice-list { list-style: none; margin: 0 0 0.75rem; padding: 0; max-height: 50vh; overflow-y: auto; }
.notice {
  display: grid;
  gap: 0.15rem;
  width: 100%;
  padding: 0.6rem 0.7rem;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.notice:hover { background: var(--surface-2); }
.notice.is-new { background: var(--info-bg); }
.notice-time { font-size: 0.8rem; }
