:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1d2330;
  --muted: #6b7385;
  --line: #dde1e8;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --danger: #c62828;
  --ok: #2e7d32;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171d;
    --card: #1e232c;
    --text: #e8ebf0;
    --muted: #97a0b3;
    --line: #333b48;
    --accent: #4d8dff;
    --danger: #ef6a6a;
    --ok: #66bb6a;
  }
}

:root[data-theme="dark"] {
  --bg: #14171d;
  --card: #1e232c;
  --text: #e8ebf0;
  --muted: #97a0b3;
  --line: #333b48;
  --accent: #4d8dff;
  --danger: #ef6a6a;
  --ok: #66bb6a;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-weight: 700; font-size: 17px; }
nav { display: flex; flex-wrap: wrap; gap: 6px; }
nav button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

main { padding: 16px; max-width: 1280px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.card h3 { margin: 4px 0 12px; font-size: 16px; }

input, select, textarea, button { font: inherit; }
input, select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
}
button {
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.big { padding: 12px 18px; font-weight: 600; }
button.ghost { background: transparent; }
button.small { padding: 4px 8px; font-size: 13px; }
button.danger { color: var(--danger); }

label { display: block; font-size: 13px; color: var(--muted); }
label input, label select { margin-top: 4px; color: var(--text); }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }
label.check input { width: auto; margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); font-size: 14px; }
.num { text-align: right; white-space: nowrap; }

.row { display: flex; gap: 10px; align-items: flex-end; margin-bottom: 12px; }
.row.wrap { flex-wrap: wrap; }
.row > label { flex: 1 1 180px; }
.actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 12px; flex-wrap: wrap; }

/* Sell */
.sell-grid { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(320px, 1fr); gap: 16px; align-items: start; }
.product-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.product-tile {
  text-align: left;
  padding: 12px;
  min-height: 84px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.product-tile .price { font-weight: 600; color: var(--accent); }
.product-tile.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }

/* Photo tiles: the product grid scrolls, and tapping a photo selects the product. */
#productGrid { max-height: calc(100vh - 190px); overflow-y: auto; padding: 2px; }
.product-grid { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); }
.product-tile.photo-tile { padding: 0; min-height: 0; overflow: hidden; text-align: left; }
.product-tile.photo-tile .photo { aspect-ratio: 1 / 1; width: 100%; border-radius: 0; }
.product-tile.photo-tile .tile-text { display: block; padding: 6px 8px 8px; }
.product-tile.photo-tile .name { display: block; font-weight: 600; font-size: 14px; line-height: 1.25; }
.product-tile.photo-tile .price { display: block; font-weight: 600; color: var(--accent); font-size: 14px; margin-top: 2px; }
.product-tile.photo-tile .muted { font-size: 12px; }
.photo {
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #e3e9f4, #c9d5ea);
  color: #3d4a66; font-weight: 700; font-size: 26px; overflow: hidden; border-radius: 8px;
}
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-preview { width: 120px; height: 120px; flex: 0 0 120px; }
.photo-field { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; }
.photo-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.file-btn { display: inline-block; padding: 8px 14px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; color: var(--text); font-size: 14px; }
.pick-top { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 8px; }
.pick-top .pick-head { flex: 1; margin-bottom: 0; }
.pick-photo { width: 72px; height: 72px; flex: 0 0 72px; font-size: 20px; }
.thumb { width: 36px; height: 36px; border-radius: 6px; vertical-align: middle; margin-right: 8px; }
.product-tile .name { font-weight: 500; }

table { width: 100%; border-collapse: collapse; }
.cart th, .list th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px; border-bottom: 1px solid var(--line); }
.cart td, .list td { padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.cart input.qty-input { width: 96px; text-align: right; padding: 4px 6px; }
.cart .unit { color: var(--muted); font-size: 13px; margin-left: 4px; }
.pick { border: 1px solid var(--accent); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; }
.pick-head { margin-bottom: 8px; }
.pick-row { align-items: flex-end; margin-bottom: 8px; }
.pick-row > label { flex: 0 1 160px; }
.pick-unit { padding-bottom: 10px; }
.pick-amount { font-size: 16px; }
.pick-amount b { font-variant-numeric: tabular-nums; }
th.num { text-align: right; }
.badge { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--accent); color: var(--accent-text); vertical-align: middle; margin-left: 6px; }
.words { margin: 6px 0 10px; font-style: italic; }
.button-link { display: inline-block; padding: 6px 12px; border: 1px solid var(--line); border-radius: 8px; color: var(--accent); text-decoration: none; }
.button-link:hover { background: var(--bg); }
.button-link.primary-link { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.list tr.clickable { cursor: pointer; }
.list tr.clickable:hover { background: var(--bg); }

.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 14px 0; }
.fields label:first-child, .fields label:nth-child(2) { }

.totals {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 12px;
  margin: 12px 0 4px;
  font-size: 15px;
}
.totals dt { color: var(--muted); }
.totals dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.totals .grand { font-size: 19px; font-weight: 700; color: var(--text); }

.bill .actions { justify-content: space-between; }
.bill .check { margin-top: 10px; }

/* Summary strip */
.summary { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.stat { flex: 1 1 140px; border: 1px solid var(--line); border-radius: 8px; padding: 10px; }
.stat b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }

/* Forms */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px 16px; }
.form-grid h3 { grid-column: 1 / -1; margin-bottom: 0; border-bottom: 1px solid var(--line); padding-bottom: 6px; }
.form-grid .wide { grid-column: 1 / -1; }
.form-grid .actions { grid-column: 1 / -1; }

/* Invoice detail */
.receipt-preview { font-family: ui-monospace, Consolas, monospace; font-size: 13px; white-space: pre; background: var(--bg); padding: 12px; border-radius: 8px; overflow-x: auto; }

/* Login & toast */
.login { display: flex; justify-content: center; padding-top: 12vh; }
.login-card { width: min(360px, 92vw); display: flex; flex-direction: column; gap: 12px; }
.login-card h2 { margin: 0; }

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  max-width: min(420px, 92vw);
  padding: 12px 16px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2);
  z-index: 20;
}
.toast.bad { background: var(--danger); color: #fff; }

/* Phone and tablet widths */
@media (max-width: 860px) {
  .sell-grid { grid-template-columns: 1fr; }
  .fields { grid-template-columns: 1fr; }
  .topbar { padding: 10px 12px; }
  main { padding: 12px; }
}
