:root {
  --bg: #f4efe7;
  --surface: #fffaf2;
  --surface-strong: #ffffff;
  --ink: #1f241f;
  --muted: #5b655f;
  --line: #d7cbbd;
  --brand: #16302b;
  --brand-soft: #224740;
  --accent: #c96d2d;
  --accent-soft: #f3d7bf;
  --success: #2f6f4f;
  --danger: #a13f30;
  --shadow: 0 12px 30px rgba(29, 26, 17, 0.08);
  --radius: 22px;
  --font-display: "Georgia", "Times New Roman", serif;
  --font-body: "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at top left, rgba(201, 109, 45, 0.14), transparent 32%),
    linear-gradient(180deg, #f7f2ea 0%, #f1e7d9 100%);
  color: var(--ink);
  font-family: var(--font-body);
}

body { padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); }
body.modal-open { overflow: hidden; }
button,
input,
select { font: inherit; }

.app-shell {
  width: min(100%, 980px);
  margin: 0 auto;
  padding: 20px 16px 32px;
}

.flash-message {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 16px);
  left: 50%;
  z-index: 9999;
  width: min(calc(100vw - 32px), 980px);
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(255, 247, 232, 0.96);
  border: 1px solid var(--line);
  box-shadow: 0 14px 32px rgba(29, 26, 17, 0.18);
  color: var(--brand);
  backdrop-filter: blur(10px);
  transform: translateX(-50%) translateY(0);
  opacity: 1;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.flash-message.error {
  background: #fbe5df;
  color: var(--danger);
}

.flash-message[hidden] {
  display: block !important;
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(-10px);
}

.scanner-modal {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: grid;
  place-items: center;
}

.scanner-modal[hidden] {
  display: none;
}

.scanner-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(17, 24, 22, 0.58);
  backdrop-filter: blur(6px);
}

.scanner-panel {
  position: relative;
  width: min(calc(100vw - 24px), 520px);
  display: grid;
  gap: 12px;
  z-index: 1;
}

.product-edit-modal {
  position: fixed;
  inset: 0;
  z-index: 10010;
  display: grid;
  place-items: center;
  padding: 20px;
}

.product-edit-panel {
  position: relative;
  width: min(calc(100vw - 32px), 960px);
  max-height: calc(100vh - 32px);
  overflow: auto;
  display: grid;
  gap: 12px;
  z-index: 1;
}

.product-edit-backdrop {
  z-index: 0;
}

#scanner-video {
  width: 100%;
  min-height: 260px;
  border-radius: 18px;
  background: #141716;
  object-fit: cover;
}

.topbar,
.hero,
.hero-actions,
.tabbar,
.product-row,
.purchase-row,
.count-toolbar,
.fraction-grid,
.line-item,
.line-main,
.report-grid,
.report-row,
.form-grid,
.inline-metrics {
  display: flex;
  gap: 12px;
}

.topbar,
.hero,
.product-row,
.purchase-row,
.line-item,
.report-row {
  justify-content: space-between;
  align-items: center;
}

.topbar { margin-bottom: 20px; }

.eyebrow,
.hero-kicker,
.section-label,
.metric-label,
.mini-label,
.pill {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.72rem;
}

h1, h2, h3 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
}

h1 { font-size: clamp(2rem, 3vw, 2.5rem); }
h2 { font-size: clamp(1.6rem, 4vw, 2.2rem); }
p { margin: 0; color: var(--muted); line-height: 1.45; }

.layout {
  display: grid;
  gap: 16px;
}

.card,
.view,
.tabbar {
  background: rgba(255, 250, 242, 0.86);
  border: 1px solid rgba(215, 203, 189, 0.9);
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

.card,
.view {
  border-radius: var(--radius);
  padding: 18px;
}

.hero {
  align-items: end;
  background: linear-gradient(135deg, rgba(22, 48, 43, 0.95), rgba(34, 71, 64, 0.88));
  color: #fdf8f1;
  position: relative;
  overflow: hidden;
}

.hero::after {
  content: "";
  position: absolute;
  inset: auto -40px -50px auto;
  width: 180px;
  height: 180px;
  background: radial-gradient(circle, rgba(201, 109, 45, 0.35), transparent 68%);
}

.hero-copy,
.hero-kicker { color: rgba(253, 248, 241, 0.82); }
.hero-actions { flex-wrap: wrap; justify-content: flex-end; }

.primary-button,
.secondary-button,
.ghost-button,
.fraction-button,
.tab,
.quick-button {
  border: 0;
  border-radius: 999px;
  padding: 12px 16px;
  cursor: pointer;
}

.primary-button { background: var(--accent); color: #fffaf4; }
.secondary-button { background: rgba(255, 248, 237, 0.16); border: 1px solid rgba(255, 248, 237, 0.28); color: #fffaf4; }
.ghost-button { background: var(--surface-strong); color: var(--brand); border: 1px solid var(--line); }

.tabbar {
  padding: 8px;
  border-radius: 999px;
  overflow-x: auto;
}

.tab { background: transparent; color: var(--muted); white-space: nowrap; }
.tab.active { background: var(--brand); color: #fffaf4; }

.view { display: none; gap: 16px; position: relative; }
.view.active { display: grid; }

.metric-value {
  display: block;
  margin: 10px 0 4px;
  font-size: 1.7rem;
}

.metric-caption,
.mini-copy { color: var(--muted); font-size: 0.9rem; }

.grid-4,
.grid-2,
.stack,
.list {
  display: grid;
  gap: 12px;
}

.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.section-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.search,
.field input,
.field select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  background: #fffcf7;
}

.product-row,
.purchase-row,
.report-row,
.line-item,
.report-cell {
  padding: 14px;
  border-radius: 18px;
  background: var(--surface-strong);
  border: 1px solid rgba(215, 203, 189, 0.8);
}

.product-meta,
.line-submeta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: #724221;
}

.pill.neutral { background: #efe8dc; color: #5c5044; }
.count-panel { display: grid; gap: 16px; }
.count-layout { align-items: start; }
.count-section { display: grid; gap: 10px; }
.count-section-top {
  position: sticky;
  top: calc(env(safe-area-inset-top) + 16px);
  z-index: 25;
  padding-bottom: 6px;
  background: linear-gradient(180deg, rgba(255, 250, 242, 0.98), rgba(255, 250, 242, 0.92));
}
.count-toolbar-tight { align-items: center; }
.count-nav .pill { min-width: 130px; justify-content: center; }
.count-toolbar,
.fraction-grid,
.form-grid,
.report-grid { flex-wrap: wrap; }
.square-mapping-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: stretch;
  gap: 16px;
}

.square-mapping-meta {
  display: grid;
  gap: 8px;
  min-width: 0;
}

.square-mapping-meta p {
  color: var(--muted);
  line-height: 1.45;
}

.square-mapping-controls {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(2, minmax(220px, 1fr));
  align-items: end;
}

.square-mapping-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  grid-column: 1 / -1;
}

.square-mapping-controls .field {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.square-mapping-controls .field span {
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.square-mapping-tags {
  margin-top: 0;
}

.count-search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
}
.count-search-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.count-panel .line-item {
  position: sticky;
  top: calc(env(safe-area-inset-top) + 152px);
  z-index: 20;
}
.count-focus-card {
  padding: 18px;
  box-shadow: 0 10px 20px rgba(29, 26, 17, 0.08);
}
.fraction-button {
  background: #f7efe4;
  border: 1px solid var(--line);
  min-width: 92px;
  min-height: 58px;
  font-size: 1rem;
  font-weight: 600;
}
.fraction-button.selected { background: var(--brand); color: #fffaf4; }
.quick-button {
  background: #fff;
  border: 1px solid var(--line);
  min-height: 52px;
  padding-inline: 18px;
}
.ghost-button:disabled,
.quick-button:disabled { opacity: 0.45; cursor: not-allowed; }
.quantity-value { font-size: 2.2rem; font-weight: 700; color: var(--brand); }
.report-cell { flex: 1 1 170px; }
.report-cell strong { display: block; margin-top: 8px; font-size: 1.4rem; }
.field { flex: 1 1 180px; display: grid; gap: 8px; }
.field-full { flex-basis: 100%; }
.status-good { color: var(--success); }
.status-bad { color: var(--danger); }
.line-item.is-current { border-color: var(--brand); box-shadow: inset 0 0 0 1px var(--brand); }
.product-row-inactive { opacity: 0.72; }
.auth-card { max-width: 520px; margin: 0 auto; }
.audit-log-row { align-items: flex-start; }

@media (max-width: 760px) {
  .grid-4,
  .grid-2 { grid-template-columns: 1fr; }

  .count-section-top {
    position: static;
    padding-bottom: 0;
    background: transparent;
  }

  .count-search-row {
    grid-template-columns: 1fr;
  }

  .count-panel .line-item {
    position: static;
  }

  .hero,
  .topbar,
  .product-row,
  .purchase-row,
  .report-row,
  .line-main { flex-direction: column; align-items: stretch; }

  .square-mapping-controls {
    min-width: 100%;
    grid-template-columns: 1fr;
  }

  .square-mapping-actions > * {
    width: 100%;
  }

  .product-edit-modal {
    padding: 12px;
  }

  .product-edit-panel {
    width: calc(100vw - 24px);
    max-height: calc(100vh - 24px);
  }

  .hero-actions > button,
  .count-toolbar > * { width: 100%; }
}
