/* Stockly -- cards, forms, tables, key figures and the trend chart. */

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px;
  margin-bottom: 14px;
}

.card > h2 {
  margin: 0 0 12px;
  font-size: 16px;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.card-head h2 {
  margin: 0;
  font-size: 16px;
}

.g {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--mut);
}

input,
select,
textarea {
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  font: 15px inherit;
  width: 100%;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--ac);
  border-color: transparent;
}

/* Textareas grow downwards only, so a dragged corner cannot reflow the page. */
textarea {
  resize: vertical;
  min-height: 44px;
}

input[type='checkbox'] {
  width: auto;
  accent-color: var(--ac);
}

.check {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--ink);
}

.btn {
  background: var(--ac);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 10px 18px;
  font: 600 14px inherit;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}

.btn.alt {
  background: none;
  color: var(--ink);
  border: 1px solid var(--line);
}

.btn.danger {
  background: var(--bad);
  color: #fff;
}

.btn.sm {
  padding: 7px 12px;
  font-size: 13px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  margin-top: 12px;
}

.pv {
  margin: 10px 0;
  color: var(--ac);
  font-weight: 600;
  min-height: 22px;
}

/* The confirmation behind a destructive submit. A native <dialog>, so the
   browser traps focus, closes on Esc and dims the page for us. */
.dlg {
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  max-width: min(400px, 90vw);
  box-shadow: 0 18px 40px rgb(0 0 0 / 18%);
}

.dlg::backdrop {
  background: rgb(0 0 0 / 45%);
}

.dlg[open] {
  animation: dlg-in 140ms ease-out;
}

.dlg > h2 {
  margin: 0 0 8px;
  font-size: 17px;
}

.dlg p {
  margin: 0;
}

@keyframes dlg-in {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.98);
  }
}

/* Respect a stated preference for less motion. */
@media (prefers-reduced-motion: reduce) {
  .dlg[open] {
    animation: none;
  }
}

.pv.bad {
  color: var(--bad);
}

.sc {
  overflow-x: auto;
}

.invoice-preview {
  display: grid;
  grid-template-columns: minmax(140px, 1.4fr) repeat(5, minmax(96px, 1fr)) auto;
  gap: 10px;
  align-items: end;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 12px;
}

.invoice-preview > div {
  min-width: 0;
}

.invoice-preview span,
.invoice-preview small {
  display: block;
  color: var(--mut);
  font-size: 12px;
  font-weight: 600;
}

.invoice-preview strong {
  display: block;
  color: var(--ink);
  font-size: 14px;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.invoice-preview .btn {
  align-self: center;
  white-space: nowrap;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

th {
  text-align: left;
  color: var(--mut);
  font-size: 12px;
  font-weight: 600;
  padding: 8px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

td {
  padding: 9px 8px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

td.r,
th.r {
  text-align: right;
}

tfoot td {
  font-weight: 700;
  border: 0;
}

tr:last-child td {
  border-bottom: 0;
}

@media (max-width: 760px) {
  .invoice-preview {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .invoice-preview .btn {
    width: 100%;
  }
}

.kp {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.kp > div {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
}

.kp small {
  color: var(--mut);
  font-weight: 600;
}

.kp strong {
  display: block;
  font-size: 22px;
  letter-spacing: -0.02em;
}

.seg {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 14px;
}

.sg {
  display: flex;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 3px;
}

.sg button,
.sg a {
  border: 0;
  background: none;
  color: var(--mut);
  padding: 6px 12px;
  border-radius: 7px;
  font: 600 13px inherit;
  cursor: pointer;
  text-decoration: none;
}

.sg .on {
  background: var(--ink);
  color: var(--bg);
}

.seg input {
  width: auto;
}

.seg .mut {
  color: var(--mut);
  font-size: 13px;
}

/* The per-sale subtotal row. Top-ruled so a reader can tell where one sale ends
   and the next begins, and bold because it is the figure being looked for. */
tr.sub td {
  border-top: 1px solid var(--line);
  font-weight: 600;
}

/* The in-progress line in the cart: the figures are live, but nothing here has
   been added to the sale yet, so it reads as provisional. */
tr.draft td {
  color: var(--mut);
  font-style: italic;
}

/* Form rows: each row is a grid container */
.form-rows {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.form-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  align-items: end;
  padding: 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
}

/* Row 2 buy page: 4 columns (Unit, Qty, Cost, Sell Price) */
.form-row.row-2-buy {
  grid-template-columns: repeat(4, 1fr);
}

/* Row 2 sales: 4 columns (Unit, Qty, Cost, Sell Price) */
.form-row.row-2-sale {
  grid-template-columns: repeat(4, 1fr);
}

/* Inner unit rows: 5 columns (Unit+Handle, Per, Cost, Sell Price, Remove) */
.form-row.inner-unit-row {
  grid-template-columns: repeat(5, 1fr);
}

/* Drag-and-drop for inner units */
.inner-unit-row {
  transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.inner-unit-row.dragging {
  opacity: 0.5;
  transform: scale(1.02);
  box-shadow: 0 8px 24px rgb(0 0 0 / 15%);
  z-index: 10;
}

.inner-unit-row.drag-over {
  box-shadow: inset 0 0 0 2px var(--ac);
}

.drag-handle {
  cursor: grab;
  color: var(--mut);
  font-size: 16px;
  line-height: 1;
  padding: 4px;
  user-select: none;
}

.drag-handle:active {
  cursor: grabbing;
}

/* Form field inside the row */
.form-field {
  width: 100%;
}

/* Mobile: stack fields vertically */
@media (max-width: 768px) {
  .form-row {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 8px;
  }

  .form-row:has([x-text*="Selling price"]) {
    grid-template-columns: 1fr;
  }
}

/* Transaction totals cell that spans multiple lines */
.totals-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
  line-height: 1.3;
}

.totals-cell span {
  font-size: 12px;
}

.totals-cell .cost {
  color: var(--mut);
}

.totals-cell .total {
  font-weight: 600;
}

.totals-cell .profit {
  font-weight: 600;
}

.totals-cell .profit.pos {
  color: var(--ok);
}

.totals-cell .profit.neg {
  color: var(--bad);
}

/* Ensure rowspan cells align content to top */
td[rowspan] {
  vertical-align: top;
}

/* Paid status badge and button */
.paid-status {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-start;
}

.paid-status .btn {
  padding: 4px 10px;
  font-size: 12px;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
}

.badge.ok {
  background: var(--ok);
  color: #fff;
}

.badge.warn {
  background: var(--warn);
  color: #000;
}
