/* ---------------------------------------------------------------------------
   Screen styles. Print rules live in print.css.
   --------------------------------------------------------------------------- */

:root {
  --blue: #00aeef;
  --blue-dark: #0090c6;
  --ink: #1e2430;
  --muted: #6b7686;
  --line: #e3e8ef;
  --bg: #f4f6f9;
  --card: #fff;
  --danger: #d94b4b;
  --ok: #1f9d63;
  --radius: 8px;
  --nav-w: 210px;
}

* { box-sizing: border-box; }

/* Must come before anything that sets `display`. The browser hides [hidden]
   via `display: none` in its own stylesheet, which any author `display` rule
   silently beats — that is how the sign-in form ended up visible (and
   unwired) before the app had booted. */
[hidden] { display: none !important; }

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

body.busy { cursor: progress; }

h1 { font-size: 22px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 12px; }
a { color: var(--blue-dark); text-decoration: none; }
a:hover { text-decoration: underline; }
code {
  background: var(--bg);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12px;
}
.muted { color: var(--muted); }
.strong { font-weight: 600; }
.num { text-align: right; }
.pre { white-space: pre-wrap; }
.warn { color: var(--danger); }

.fatal { max-width: 520px; margin: 80px auto; font-family: system-ui, sans-serif; }

/* --- sign in --------------------------------------------------------------- */

#login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: var(--bg);
}
.login-card {
  width: 320px;
  background: var(--card);
  padding: 32px;
  border-radius: var(--radius);
  box-shadow: 0 2px 14px rgba(20, 30, 50, .09);
  display: grid;
  gap: 14px;
}
.login-brand {
  text-align: center;
  color: var(--blue);
  margin-bottom: 6px;
}
.login-card--booting {
  text-align: center;
  color: var(--muted);
  padding: 40px 32px;
}
.login-error {
  color: var(--danger);
  margin: 0;
  min-height: 18px;
  font-size: 13px;
  line-height: 1.5;
  text-align: left;
}
.login-error code { color: var(--ink); }
/* A startup failure explains itself at length; give it room to do so. */
.login-card:has(.login-error:not(:empty)) { width: 400px; }

/* --- shell ----------------------------------------------------------------- */

#shell { display: flex; min-height: 100vh; }

#nav {
  width: var(--nav-w);
  flex: 0 0 var(--nav-w);
  background: var(--ink);
  color: #c7d0dd;
  padding: 18px 0 24px;
  display: flex;
  flex-direction: column;
}
#nav .brand {
  color: #fff;
  font-weight: 700;
  font-size: 17px;
  padding: 0 20px 18px;
}
#nav a {
  color: #c7d0dd;
  padding: 9px 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  border-left: 3px solid transparent;
}
#nav a:hover { background: rgba(255, 255, 255, .06); color: #fff; }
#nav a.active {
  background: rgba(0, 174, 239, .14);
  border-left-color: var(--blue);
  color: #fff;
}
#nav .ico {
  font-size: 10px;
  width: 26px;
  opacity: .75;
  letter-spacing: .5px;
}
.nav-sep {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #77839a;
  padding: 18px 20px 6px;
}
.nav-logout {
  margin: auto 20px 0;
  background: none;
  border: 1px solid #3b4557;
  color: #c7d0dd;
  padding: 7px;
  border-radius: 6px;
  cursor: pointer;
}
.nav-logout:hover { background: #3b4557; color: #fff; }

#view { flex: 1; padding: 26px 30px 60px; min-width: 0; }

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 18px;
}
.page-head p { margin: 4px 0 0; font-size: 13px; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* --- cards & tables -------------------------------------------------------- */

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 18px;
}
.card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.card-head h2 { margin: 0; }

.table { width: 100%; border-collapse: collapse; }
.table th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .6px;
  color: var(--muted);
  padding: 0 10px 8px;
  border-bottom: 1px solid var(--line);
}
.table th.num { text-align: right; }
.table td { padding: 10px; border-bottom: 1px solid var(--line); }
.table tr:last-child td { border-bottom: 0; }
.table .empty { text-align: center; color: var(--muted); padding: 28px; }

.row-actions { text-align: right; white-space: nowrap; }
.row-actions button, .row-actions a {
  background: none;
  border: 0;
  color: var(--blue-dark);
  cursor: pointer;
  font-size: 13px;
  padding: 2px 6px;
  font-family: inherit;
}
.row-actions button:hover { text-decoration: underline; }
.row-actions [data-del] { color: var(--danger); }

/* --- forms ----------------------------------------------------------------- */

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px 18px;
}
.field { display: grid; gap: 5px; align-content: start; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 500; }
.field--full { grid-column: 1 / -1; }
.form-grid h2 { margin: 6px 0 0; }

input, select, textarea {
  font: inherit;
  color: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 0;
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(0, 174, 239, .15);
}
textarea { resize: vertical; }
.inline-select { width: auto; min-width: 190px; }

.btn {
  display: inline-block;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  font: inherit;
  text-decoration: none;
}
.btn:hover { background: var(--bg); text-decoration: none; }
.btn--primary {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
  font-weight: 600;
}
.btn--primary:hover { background: var(--blue-dark); border-color: var(--blue-dark); }
.btn--ghost { border-style: dashed; color: var(--muted); }
.btn--wide { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }

/* --- document header grid --------------------------------------------------- */

.doc-head {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px 18px;
  align-items: start;
}
/* The customer picker is the widest thing on the row, as on the original. */
.doc-head .field--wide { grid-column: span 2; grid-row: span 2; }
.req { color: var(--danger); }
.list-meta { color: var(--muted); font-size: 12px; margin-bottom: 8px; }
.sub { color: var(--muted); font-size: 12px; }
.hidden { display: none; }

.linkish {
  background: none;
  border: 0;
  color: var(--blue-dark);
  cursor: pointer;
  font: inherit;
  padding: 0;
}
.linkish:hover { text-decoration: underline; }

.doc-title {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 10px;
}

/* --- line items ------------------------------------------------------------ */

.table--lines td { padding: 5px 6px; vertical-align: middle; }
.table--lines th { padding-bottom: 6px; }
.col-qty { width: 90px; }
.col-price { width: 120px; }
.col-disc { width: 110px; }
.col-amt { width: 120px; }
.col-x { width: 34px; }
.table--lines textarea {
  resize: none;
  overflow: hidden;
  min-height: 36px;
  line-height: 1.45;
}
.table--lines input.num { text-align: right; }
.table--lines .x {
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
}
.table--lines .x:hover { color: var(--danger); }

.totals-row {
  display: grid;
  grid-template-columns: 1fr 1fr 340px;
  gap: 18px;
  align-items: start;
}
.notes-col {
  grid-column: span 2;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
}
.totals { background: #f7f9fb; }
.totals-line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
}
.totals-line span { color: var(--muted); display: flex; align-items: center; gap: 6px; }
.totals-line input[type="number"] { width: 110px; padding: 4px 7px; text-align: right; }
.totals-line b { font-variant-numeric: tabular-nums; }
.totals-line--grand {
  border-top: 1px solid var(--line);
  margin-top: 6px;
  font-size: 16px;
  font-weight: 700;
  padding-top: 11px;
}
.totals-line--grand span { color: var(--ink); }

/* Rounding on/off switch. */
.switch { position: relative; display: inline-block; width: 30px; height: 16px; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch i {
  position: absolute;
  inset: 0;
  background: #c6cedb;
  border-radius: 20px;
  transition: background .18s;
  cursor: pointer;
}
.switch i::before {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  left: 2px;
  top: 2px;
  background: #fff;
  border-radius: 50%;
  transition: transform .18s;
}
.switch input:checked + i { background: var(--blue); }
.switch input:checked + i::before { transform: translateX(14px); }

/* --- dashboard ------------------------------------------------------------- */

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}
.tile {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  color: inherit;
  text-decoration: none;
  display: block;
}
.tile:hover { border-color: var(--blue); text-decoration: none; }
.tile-count { font-size: 26px; font-weight: 700; }
.tile-label { font-weight: 600; margin-top: 2px; }
.tile-sub { color: var(--muted); font-size: 12px; }
.tile--money .tile-count { font-size: 20px; color: var(--blue-dark); }

/* --- status pills ---------------------------------------------------------- */

.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--line);
}
.pill--new,
.pill--to_deliver { background: #fff5e5; color: #b5761a; border-color: #f6e2c2; }
.pill--unpaid     { background: #e8f4ff; color: #1f6fb2; border-color: #cbe4f8; }
.pill--accepted,
.pill--delivered,
.pill--paid       { background: #e6f6ed; color: var(--ok); border-color: #c7ebd7; }
.pill--cancelled  { background: #fdeceb; color: var(--danger); border-color: #f8d3d1; }

/* --- print view (on screen) ------------------------------------------------ */

.print-bar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 16px;
}
.sheet {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 40px;
  max-width: 820px;
  margin: 0 auto;
}
.sheet-head {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line);
}
.sheet-logo { width: 74px; height: 74px; object-fit: contain; flex: 0 0 auto; }
.sheet-company { flex: 1; }
.sheet-company h1 { font-size: 17px; margin-bottom: 5px; }
.sheet-company div { font-size: 12px; color: var(--muted); }
.sheet-meta { text-align: right; min-width: 220px; }
.sheet-meta h2 {
  font-size: 20px;
  letter-spacing: 2px;
  color: var(--blue);
  margin-bottom: 10px;
}
.sheet-meta table { margin-left: auto; border-collapse: collapse; }
.sheet-meta td, .sheet-meta th { padding: 2px 0 2px 14px; font-size: 12px; }
.sheet-meta td { color: var(--muted); text-align: right; }
.sheet-meta th { text-align: right; }

.sheet-to { padding: 18px 0; }
.label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  margin-bottom: 4px;
}
.sheet-to div { font-size: 13px; }

.sheet-lines { width: 100%; border-collapse: collapse; margin-top: 8px; }
.sheet-lines th {
  background: var(--bg);
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .5px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
}
.sheet-lines th.num { text-align: right; }
.sheet-lines td { padding: 9px 10px; border-bottom: 1px solid var(--line); font-size: 13px; }
.sheet-lines th:first-child, .sheet-lines td:first-child { width: 34px; }
.sheet-lines tfoot td {
  border-top: 1px solid var(--ink);
  border-bottom: 0;
  padding-top: 9px;
}
.sheet-title {
  font-size: 14px;
  font-weight: 700;
  margin: 14px 0 4px;
  text-transform: uppercase;
  line-height: 1.4;
}
.sheet-term { font-size: 12px; margin: 14px 0 0; }

.sheet-totals { margin: 14px 0 0 auto; border-collapse: collapse; min-width: 260px; }
.sheet-totals td { padding: 5px 0 5px 20px; font-size: 13px; }
.sheet-totals .grand td {
  border-top: 2px solid var(--ink);
  font-size: 16px;
  font-weight: 700;
  padding-top: 9px;
}

.sheet-notes { margin-top: 20px; font-size: 12px; }
.sheet-foot { display: flex; gap: 60px; margin-top: 46px; }
.sign { font-size: 11px; color: var(--muted); flex: 1; }
.sign-label { font-weight: 600; color: var(--ink); margin-bottom: 4px; }
.sign-img { height: 56px; object-fit: contain; display: block; }
.sign-space { height: 56px; }
.sign-line { border-top: 1px solid var(--ink); margin-bottom: 6px; }

/* Hide the sidebar while a document is on screen for printing. */
body.printing #nav { display: none; }

/* --- toasts ---------------------------------------------------------------- */

#toasts {
  position: fixed;
  right: 18px;
  bottom: 18px;
  display: grid;
  gap: 8px;
  z-index: 50;
}
.toast {
  background: var(--ink);
  color: #fff;
  padding: 10px 16px;
  border-radius: 6px;
  box-shadow: 0 3px 12px rgba(0, 0, 0, .2);
  transition: opacity .35s, transform .35s;
}
.toast--err { background: var(--danger); }
.toast--out { opacity: 0; transform: translateY(6px); }

/* --- narrow screens -------------------------------------------------------- */

@media (max-width: 820px) {
  #shell { flex-direction: column; }
  #nav {
    width: 100%;
    flex: none;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    padding: 10px;
    gap: 2px;
  }
  #nav .brand { padding: 0 12px 0 8px; }
  #nav .nav-sep { display: none; }
  #nav a { border-left: 0; border-bottom: 3px solid transparent; padding: 7px 10px; }
  #nav a.active { border-left: 0; border-bottom-color: var(--blue); }
  #nav .ico { display: none; }
  .nav-logout { margin: 0 0 0 auto; }
  #view { padding: 16px 14px 40px; }
  .form-grid, .totals-row, .doc-head, .notes-col { grid-template-columns: 1fr; }
  .doc-head .field--wide { grid-column: auto; grid-row: auto; }
  .notes-col { grid-column: auto; }
  .sheet { padding: 22px; }
  .sheet-head { flex-direction: column; }
  .sheet-meta { text-align: left; }
  .sheet-meta table { margin-left: 0; }
}
