/* Apple-style system look: SF font stack, grouped surfaces, iOS colours. */

:root {
  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #f2f2f7;
  --fill: rgba(120, 120, 128, 0.12);
  --fill-2: rgba(120, 120, 128, 0.2);
  --text: #000000;
  --text-2: #3c3c43;
  --muted: rgba(60, 60, 67, 0.6);
  --line: rgba(60, 60, 67, 0.12);
  --blue: #007aff;
  --green: #34c759;
  --red: #ff3b30;
  --green-bg: rgba(52, 199, 89, 0.14);
  --red-bg: rgba(255, 59, 48, 0.14);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  --nav: rgba(249, 249, 249, 0.8);
  --photo-bg: #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-2: #2c2c2e;
    --fill: rgba(120, 120, 128, 0.24);
    --fill-2: rgba(120, 120, 128, 0.36);
    --text: #ffffff;
    --text-2: #ebebf5;
    --muted: rgba(235, 235, 245, 0.6);
    --line: rgba(84, 84, 88, 0.6);
    --blue: #0a84ff;
    --green: #30d158;
    --red: #ff453a;
    --green-bg: rgba(48, 209, 88, 0.18);
    --red-bg: rgba(255, 69, 58, 0.18);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    --nav: rgba(29, 29, 31, 0.75);
    --photo-bg: #f5f5f7;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input { font: inherit; color: inherit; }
a { color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.5; cursor: default; transform: none; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--blue) 45%, transparent); outline-offset: 2px; }

.wrap { max-width: 980px; margin: 0 auto; padding: 0 20px; }

/* Nav bar with frosted glass */
.nav {
  position: sticky; top: 0; z-index: 10;
  background: var(--nav);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--line);
}
.nav .wrap { display: flex; align-items: center; justify-content: space-between; height: 50px; }
.brand { font-weight: 600; font-size: 17px; letter-spacing: -0.2px; }
.hint { color: var(--muted); font-size: 13px; }

/* Large title */
.hero { padding: 28px 0 16px; }
.hero h1 { margin: 0; font-size: 34px; font-weight: 700; letter-spacing: -0.5px; line-height: 1.15; }
.subtitle { margin: 6px 0 0; color: var(--muted); font-size: 15px; }

/* Add form */
.add { display: flex; gap: 10px; }
.field {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border-radius: 14px;
  padding: 0 14px; height: 48px;
  box-shadow: var(--shadow);
  transition: box-shadow .15s;
}
.field:focus-within { box-shadow: var(--shadow), 0 0 0 3px color-mix(in srgb, var(--blue) 35%, transparent); }
.field.grow { flex: 1; min-width: 0; }
.field .icon { width: 20px; height: 20px; color: var(--muted); flex: none; }
.field input { flex: 1; min-width: 0; border: 0; background: none; height: 100%; font-size: 16px; }
.field input::placeholder { color: var(--muted); }
.field input[type=number] { width: 120px; }
.field input[type=number]::-webkit-outer-spin-button,
.field input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; }
button.primary {
  background: var(--blue); color: #fff;
  border-radius: 14px; height: 48px; padding: 0 22px;
  font-size: 16px; font-weight: 600;
  box-shadow: var(--shadow);
}

/* Segmented control */
.segmented {
  display: inline-flex; gap: 2px;
  background: var(--fill); border-radius: 10px; padding: 2px;
  margin: 20px 0 16px;
}
.segmented button {
  border-radius: 8px; padding: 6px 16px;
  font-size: 13px; font-weight: 600; color: var(--text-2);
  transition: background .15s, box-shadow .15s;
}
.segmented button[aria-pressed="true"] {
  background: var(--surface); color: var(--text);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 0 0 0.5px rgba(0, 0, 0, 0.04);
}
@media (prefers-color-scheme: dark) {
  .segmented button[aria-pressed="true"] { background: #636366; }
}

/* Empty state */
.empty { text-align: center; padding: 56px 20px; color: var(--muted); }
.empty svg { width: 56px; height: 56px; color: var(--blue); opacity: 0.9; }
.empty h2 { margin: 14px 0 4px; font-size: 20px; font-weight: 600; color: var(--text); }
.empty p { margin: 0 auto; max-width: 340px; font-size: 15px; }

/* Cards */
.products { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
.card {
  background: var(--surface); border-radius: 18px; padding: 16px;
  box-shadow: var(--shadow);
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; min-width: 0;
  animation: rise .25s ease-out both;
}
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.card.hit { box-shadow: var(--shadow), 0 0 0 1.5px var(--green); }

.card-top { display: grid; grid-template-columns: 68px 1fr; gap: 12px; align-items: center; }
.photo {
  width: 68px; height: 68px; border-radius: 14px; overflow: hidden;
  background: var(--photo-bg); border: 1px solid var(--line);
  display: grid; place-items: center;
}
.photo img { width: 100%; height: 100%; object-fit: contain; display: block; }
.info { min-width: 0; }
.title {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  font-weight: 600; font-size: 15px; line-height: 1.3; text-decoration: none; letter-spacing: -0.1px;
}
.title:hover { color: var(--blue); }
.store { color: var(--muted); font-size: 13px; margin-top: 3px; }

.price-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.price { font-size: 28px; font-weight: 700; letter-spacing: -0.6px; font-variant-numeric: tabular-nums; }
.chip {
  font-size: 12px; font-weight: 600; border-radius: 999px; padding: 3px 9px;
  font-variant-numeric: tabular-nums;
}
.chip.down { color: var(--green); background: var(--green-bg); }
.chip.up { color: var(--red); background: var(--red-bg); }
.chip.hit { color: #fff; background: var(--green); }

.chart { width: 100%; height: 72px; display: block; overflow: visible; }
.chart .line { fill: none; stroke: var(--blue); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }
.chart.flat .line { stroke: var(--line); stroke-dasharray: 3 4; }
.chart .stop-a { stop-color: var(--blue); stop-opacity: 0.25; }
.chart .stop-b { stop-color: var(--blue); stop-opacity: 0; }
.chart .target { stroke: var(--green); stroke-width: 1.5; stroke-dasharray: 4 4; vector-effect: non-scaling-stroke; }
.chart .dot { fill: var(--blue); stroke: var(--surface); stroke-width: 2; vector-effect: non-scaling-stroke; }

.stats { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 6px 12px; font-size: 13px; font-variant-numeric: tabular-nums; }
.stats span { display: inline-flex; gap: 5px; min-width: 0; }
.stats .k { color: var(--muted); }
.stats .v { font-weight: 600; }
.err { margin: 0; font-size: 12px; color: var(--red); }

.card-foot { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; padding-top: 10px; border-top: 1px solid var(--line); }
.target { display: flex; align-items: center; gap: 8px; flex: 1 1 100%; font-size: 13px; color: var(--muted); white-space: nowrap; }
.target input {
  width: 72px;
  background: var(--fill); border: 0; border-radius: 9px; padding: 6px 9px; font-size: 13px;
}
.target input::-webkit-outer-spin-button, .target input::-webkit-inner-spin-button { -webkit-appearance: none; }
.pill {
  background: var(--fill); border-radius: 999px; padding: 7px 13px;
  font-size: 13px; font-weight: 600; color: var(--blue); white-space: nowrap;
}
.pill.danger { color: var(--red); }

.footnote { color: var(--muted); font-size: 12px; text-align: center; max-width: 560px; margin: 40px auto 24px; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  background: rgba(30, 30, 32, 0.92); color: #fff;
  border-radius: 999px; padding: 10px 18px; font-size: 14px; font-weight: 500;
  max-width: min(92vw, 520px); text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  transition: opacity .2s, transform .2s;
  z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-kind="error"] { background: var(--red); }
.toast[data-kind="success"] { background: var(--green); color: #fff; }

@media (max-width: 640px) {
  .wrap { padding: 0 16px; }
  .hero { padding: 20px 0 14px; }
  .hero h1 { font-size: 30px; }
  .add { flex-wrap: wrap; }
  .field.grow { flex-basis: 100%; }
  .field:not(.grow) { flex: 1; }
  .field input[type=number] { width: 100%; }
  .segmented { display: flex; }
  .segmented button { flex: 1; padding: 7px 8px; }
  .products { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  .card { animation: none; }
  button:active { transform: none; }
}

/* ---------- nav actions ---------- */
.nav-actions { display: flex; gap: 6px; }
.nav-btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 10px; border-radius: 999px;
  color: var(--blue); font-size: 14px; font-weight: 600;
}
.nav-btn svg { width: 20px; height: 20px; }
.nav-btn:hover { background: var(--fill); }
.nav-btn[aria-pressed="true"] { background: var(--green-bg); color: var(--green); }

/* ---------- toolbar ---------- */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin: 20px 0 16px; }
.toolbar .segmented { margin: 0; }
.tools { display: flex; gap: 8px; align-items: center; }
.field.small { height: 36px; border-radius: 10px; padding: 0 10px; box-shadow: none; background: var(--fill); }
.field.small input { font-size: 14px; width: 150px; }
.field.small .icon { width: 16px; height: 16px; }
.field.small input[type=search]::-webkit-search-cancel-button { -webkit-appearance: none; }
.select {
  height: 36px; border: 0; border-radius: 10px; padding: 0 30px 0 12px;
  background: var(--fill) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3E%3Cpath d='M6 8l4 4 4-4' fill='none' stroke='%23888' stroke-width='1.8' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 8px center / 16px;
  font-size: 14px; font-weight: 500; color: var(--text); -webkit-appearance: none; appearance: none;
}

/* ---------- card extras ---------- */
.chip.stock { color: var(--text-2); background: var(--fill); }
.chip.stock.out { color: var(--red); background: var(--red-bg); }
.chip.low { color: var(--blue); background: color-mix(in srgb, var(--blue) 14%, transparent); }
.card-foot .spacer { flex: 1; }
button.primary.small { height: 40px; padding: 0 16px; font-size: 15px; border-radius: 12px; }

/* ---------- sheets ---------- */
.sheet {
  border: 0; border-radius: 22px; padding: 20px;
  width: min(640px, calc(100vw - 32px));
  max-height: min(86vh, 820px);
  background: var(--surface); color: var(--text);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.25);
  animation: rise .2s ease-out;
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.35); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.sheet-head h2 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -0.3px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.close { width: 30px; height: 30px; border-radius: 50%; background: var(--fill); color: var(--muted); font-size: 13px; font-weight: 700; flex: none; }
.muted { color: var(--muted); font-size: 14px; margin: 6px 0 0; }
.big-chart { margin: 16px 0 10px; }
.big-chart .chart { height: 180px; }
.sheet-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.sheet-actions .pill { text-decoration: none; display: inline-flex; align-items: center; }
.table-wrap { max-height: 280px; overflow: auto; border-radius: 12px; background: var(--surface-2); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--line); }
.table th { position: sticky; top: 0; background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: 12px; }
.table .num { text-align: right; }
.table .down { color: var(--green); }
.table .up { color: var(--red); }
.group { margin-top: 20px; }
.group h3 { margin: 0; font-size: 15px; font-weight: 600; }
.group .pill { margin-top: 10px; }
.code-row { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.code-row code {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis;
  background: var(--fill); border-radius: 10px; padding: 10px 12px; font-size: 13px;
}
.code-row .field { height: 40px; border-radius: 12px; box-shadow: none; background: var(--fill); }
.code-row .field input { font-size: 14px; }

@media (max-width: 640px) {
  .nav-btn span { display: none; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .tools { width: 100%; }
  .field.small { flex: 1; }
  .field.small input { width: 100%; }
  .sheet { width: 100vw; max-width: none; margin: auto 0 0; border-radius: 22px 22px 0 0; max-height: 92vh; padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
}

/* ---------- timeline ---------- */
.range-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 14px; }
.segmented.small { margin: 0; }
.segmented.small button { padding: 4px 10px; font-size: 12px; }
.readout { font-size: 13px; font-variant-numeric: tabular-nums; color: var(--muted); min-height: 1.2em; text-align: right; }
.readout b { color: var(--text); font-weight: 600; }
.big-chart { margin: 8px 0 4px; }
.big-chart .chart { height: 200px; cursor: crosshair; touch-action: none; }
.chart .axis { font-size: 11px; fill: var(--muted); }
.chart .grid { stroke: var(--line); stroke-width: 1; vector-effect: non-scaling-stroke; }
.chart .mark { fill: var(--surface); stroke-width: 2; vector-effect: non-scaling-stroke; }
.chart .mark.lo { stroke: var(--green); }
.chart .mark.hi { stroke: var(--red); }
.chart .label { font-size: 11px; font-weight: 600; }
.chart .label.lo { fill: var(--green); }
.chart .label.hi { fill: var(--red); }
.chart .cursor { stroke: var(--text); stroke-opacity: .35; stroke-width: 1; vector-effect: non-scaling-stroke; }
.lowline { margin: 0 0 12px; font-size: 14px; }
.lowline b { font-weight: 600; }
.lowline .lo { color: var(--green); }
