/* ============================================================================
 * PriceBook — mobile-first styles
 * ==========================================================================*/

:root {
  --green: #16a34a;
  --green-dark: #15803d;
  --green-soft: #dcfce7;
  --ink: #0f172a;
  --ink-soft: #475569;
  --muted: #94a3b8;
  --line: #e2e8f0;
  --bg: #f1f5f9;
  --card: #ffffff;
  --amber: #f59e0b;
  --amber-soft: #fef3c7;
  --red: #dc2626;
  --red-soft: #fee2e2;
  --blue: #2563eb;

  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px rgba(15, 23, 42, .06);
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, .08);
  --tabbar-h: 64px;
  --header-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.5;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

input, select, button, textarea { font-family: inherit; font-size: 16px; }

/* ── Header ──────────────────────────────────────────────────────────────── */
.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: rgba(255, 255, 255, .85);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
  padding-top: var(--safe-top);
}
.app-header-inner {
  max-width: 640px;
  margin: 0 auto;
  height: var(--header-h);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 1.15rem; }
.brand-mark { font-size: 1.3rem; }
.brand-name { letter-spacing: -.02em; }

.sync-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink-soft);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 600;
  cursor: pointer;
}
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.sync-pill.is-synced .sync-dot { background: var(--green); }
.sync-pill.is-offline .sync-dot { background: var(--amber); }
.sync-pill.is-error .sync-dot { background: var(--red); }

/* ── Layout ──────────────────────────────────────────────────────────────── */
.app-main {
  max-width: 640px;
  margin: 0 auto;
  padding: 20px 16px calc(var(--tabbar-h) + var(--safe-bottom) + 24px);
}
.view-title { font-size: 1.5rem; font-weight: 800; letter-spacing: -.02em; }
.view-sub { color: var(--ink-soft); margin-top: 2px; margin-bottom: 18px; }
.section-label { font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 22px 4px 10px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ── Cards & forms ───────────────────────────────────────────────────────── */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 16px;
}
.card-title { font-size: 1.05rem; font-weight: 700; margin-bottom: 4px; }

.field { margin-bottom: 14px; display: flex; flex-direction: column; }
.field:last-child { margin-bottom: 0; }
.field label { font-size: .82rem; font-weight: 600; color: var(--ink-soft); margin-bottom: 6px; }
.field input, .field select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: #fff;
  color: var(--ink);
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus, .field select:focus {
  outline: none;
  border-color: var(--green);
  box-shadow: 0 0 0 3px var(--green-soft);
}
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 0 0 auto; }
.field-row .grow { flex: 1 1 auto; min-width: 0; }
.form .field-row { margin-bottom: 14px; }
.form .field-row .field { margin-bottom: 0; }
#f-unit, #d-unit { min-width: 92px; }
#f-qty, #d-qty { width: 84px; }

.unit-preview {
  background: var(--green-soft);
  color: var(--green-dark);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-weight: 600;
  font-size: .9rem;
  margin-bottom: 14px;
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 12px 18px;
  font-weight: 600;
  cursor: pointer;
  transition: transform .04s, background .15s, border-color .15s, opacity .15s;
}
.btn:active { transform: scale(.98); }
.btn-full { width: 100%; }
.btn-primary { background: var(--green); color: #fff; }
.btn-primary:hover { background: var(--green-dark); }
.btn-outline { background: #fff; border-color: var(--line); color: var(--ink); }
.btn-outline:hover { border-color: var(--green); color: var(--green-dark); }
.btn-ghost { background: transparent; color: var(--ink-soft); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: #fff; border-color: var(--red-soft); color: var(--red); }
.btn-danger:hover { background: var(--red-soft); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-pair { display: flex; gap: 10px; }
.btn-pair .btn { flex: 1; }

/* ── Search ──────────────────────────────────────────────────────────────── */
.search-wrap { position: relative; margin-bottom: 16px; }
.search-icon { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); font-size: .95rem; opacity: .6; }
#searchInput {
  width: 100%;
  padding: 13px 14px 13px 40px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
}
#searchInput:focus { outline: none; border-color: var(--green); box-shadow: 0 0 0 3px var(--green-soft); }

/* ── Items list ──────────────────────────────────────────────────────────── */
.items-list { display: flex; flex-direction: column; gap: 10px; }
.item-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  transition: border-color .15s, transform .04s;
}
.item-card:hover { border-color: var(--green); }
.item-card:active { transform: scale(.995); }
.item-card-main { min-width: 0; }
.item-card-name { font-weight: 700; font-size: 1.02rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-card-meta { color: var(--ink-soft); font-size: .82rem; margin-top: 2px; }
.item-card-price { text-align: right; flex: 0 0 auto; }
.item-card-best { font-weight: 800; color: var(--green-dark); white-space: nowrap; }
.item-card-best-label { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

/* ── Empty state ─────────────────────────────────────────────────────────── */
.empty { text-align: center; padding: 48px 20px; color: var(--ink-soft); }
.empty-emoji { font-size: 2.6rem; margin-bottom: 8px; }

/* ── Detail ──────────────────────────────────────────────────────────────── */
.back-btn { background: none; border: none; color: var(--green-dark); font-weight: 600; cursor: pointer; padding: 4px 0; margin-bottom: 6px; }
.deal-card { border-color: var(--green-soft); }

.verdict {
  margin-top: 14px;
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  border: 1px solid var(--line);
}
.verdict-label { font-weight: 800; font-size: 1.05rem; }
.verdict-note { font-size: .85rem; margin-top: 2px; opacity: .9; }
.verdict-ref { font-size: .82rem; margin-top: 8px; padding-top: 8px; border-top: 1px dashed currentColor; opacity: .85; }
.verdict.best    { background: var(--green-soft); border-color: #86efac; color: var(--green-dark); }
.verdict.good    { background: var(--green-soft); border-color: #bbf7d0; color: var(--green-dark); }
.verdict.average { background: var(--amber-soft); border-color: #fde68a; color: #92400e; }
.verdict.pricey  { background: var(--red-soft); border-color: #fecaca; color: #b91c1c; }
.verdict.none    { background: var(--bg); border-color: var(--line); color: var(--ink-soft); }

.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 12px;
  text-align: center;
}
.stat-label { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.stat-value { font-weight: 800; font-size: 1.05rem; margin-top: 3px; }
.stat-sub { font-size: .72rem; color: var(--ink-soft); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ── History ─────────────────────────────────────────────────────────────── */
.history-list { display: flex; flex-direction: column; gap: 8px; }
.history-row {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.history-main { flex: 1; min-width: 0; }
.history-shop { font-weight: 600; }
.history-sub { font-size: .8rem; color: var(--ink-soft); margin-top: 1px; }
.history-price { text-align: right; }
.history-amount { font-weight: 700; }
.history-unit { font-size: .76rem; color: var(--ink-soft); }
.history-cheapest { box-shadow: inset 3px 0 0 var(--green); }
.del-btn {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 1.1rem; padding: 4px 6px; line-height: 1; border-radius: 8px;
}
.del-btn:hover { color: var(--red); background: var(--red-soft); }

/* ── Settings extras ─────────────────────────────────────────────────────── */
.divider { height: 1px; background: var(--line); margin: 16px 0; }
.auth-block { margin-top: 4px; }
.signed-in-as { margin-bottom: 12px; }
.version-note { text-align: center; margin-top: 8px; }

/* ── Tab bar ─────────────────────────────────────────────────────────────── */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: rgba(255, 255, 255, .92);
  backdrop-filter: saturate(180%) blur(12px);
  border-top: 1px solid var(--line);
  display: flex;
}
.tab {
  flex: 1;
  background: none;
  border: none;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: var(--muted);
  font-weight: 600;
}
.tab-icon { font-size: 1.25rem; line-height: 1; }
.tab-label { font-size: .72rem; }
.tab.active { color: var(--green-dark); }

/* ── Toast ───────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
  transform: translateX(-50%) translateY(8px);
  background: var(--ink);
  color: #fff;
  padding: 11px 18px;
  border-radius: 999px;
  font-size: .9rem;
  font-weight: 500;
  box-shadow: var(--shadow);
  opacity: 0;
  transition: opacity .2s, transform .2s;
  z-index: 50;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.toast-error { background: var(--red); }

/* ── Desktop niceties ────────────────────────────────────────────────────── */
@media (min-width: 720px) {
  .stats-grid { gap: 14px; }
  .app-main { padding-top: 28px; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --ink: #e2e8f0;
    --ink-soft: #94a3b8;
    --muted: #64748b;
    --line: #1e293b;
    --bg: #0b1120;
    --card: #0f172a;
    --green-soft: #064e3b;
    --amber-soft: #422006;
    --red-soft: #450a0a;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
    --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
  }
  .app-header, .tabbar { background: rgba(15, 23, 42, .85); }
  .field input, .field select, #searchInput, .sync-pill { background: #0b1120; }
  .verdict.average { color: #fcd34d; }
  .verdict.pricey { color: #fca5a5; }
}
