/* ==========================================================================
   Fraaash CRM
   Palette and type lifted from shopify-theme/assets/tokens.css so the app
   reads as the same brand as fraaash.com. Nunito for display, Inter for UI.
   ========================================================================== */

:root {
  /* Brand — straight from the theme tokens */
  --green-900: #063f21;
  --green-800: #085129;
  --green-700: #0a6836;   /* primary */
  --green-600: #12814a;
  --green-500: #1d9b5d;
  --green-100: #dff0e5;
  --green-50:  #f0f8f3;

  --acid-500:  #ecf10f;
  --lime-500:  #d7e065;

  --cream-100: #fffdf6;
  --cream-200: #fbf7ea;
  --cream-300: #f4eeda;

  --ink-900: #12170d;
  --ink-700: #3a4232;
  --ink-500: #6b7462;
  --ink-300: #a8b09f;
  --ink-100: #e3e6dd;

  --berry-500: #d94f5c;
  --amber-500: #b8730a;

  /* Semantic */
  --bg: var(--cream-200);
  --surface: var(--cream-100);
  --brand: var(--green-700);
  --brand-ink: var(--green-900);
  --ink: var(--ink-900);
  --muted: var(--ink-500);
  --line: var(--ink-100);
  --logo-tint: none;

  --font-display: "Nunito", "Trebuchet MS", system-ui, sans-serif;
  --font-body: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

  --r-sm: 8px;
  --r-md: 14px;   /* theme button radius */
  --r-lg: 24px;
  --r-pill: 999px;

  --shadow-sm: 0 1px 2px rgba(18,23,13,.06), 0 2px 8px rgba(18,23,13,.04);
  --shadow-lg: 0 12px 28px rgba(18,23,13,.10), 0 32px 64px rgba(18,23,13,.09);
  --shadow-brand: 0 8px 22px rgba(10,104,54,.22);

  --safe-b: env(safe-area-inset-bottom, 0px);
  --tabbar-h: 62px;
  --topbar-h: 60px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e130c;
    --surface: #171d14;
    --ink: #eef1e8;
    --muted: #9aa48f;
    --line: #2a3226;
    --brand: #3fa96e;
    --brand-ink: #8fd9ae;
    --green-100: #16351f;
    --green-50: #12271a;
    /* The logo PNG is solid green; flip it to cream on dark surfaces. */
    --logo-tint: brightness(0) invert(1);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
  margin: 0;
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--ink);
  padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px);
  overscroll-behavior-y: contain;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, .kpi-v, .card-name, .fact b {
  font-family: var(--font-display);
  letter-spacing: -0.015em;
}

/* ---------- top bar ---------- */

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top, 0px);
}
.topbar-inner {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 11px 16px; min-height: var(--topbar-h);
}
.brand { display: flex; align-items: center; gap: 10px; margin: 0; }
.brand img { height: 26px; width: auto; display: block; filter: var(--logo-tint); }
.brand-tag {
  font-family: var(--font-body); font-size: 11px; font-weight: 700;
  letter-spacing: .09em; text-transform: uppercase;
  color: var(--brand); background: var(--green-50);
  border: 1px solid var(--green-100);
  padding: 3px 7px; border-radius: var(--r-sm);
}
.brand-tag.demo { color: var(--green-900); background: var(--acid-500); border-color: var(--acid-500); }
.icon-btn {
  background: var(--green-50); color: var(--brand);
  border: 1px solid var(--green-100);
  width: 38px; height: 38px; border-radius: var(--r-sm);
  font-size: 17px; cursor: pointer; flex: 0 0 auto;
}
.icon-btn:active { background: var(--green-100); }
.icon-btn.spin { animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- layout ---------- */

main { padding: 14px 12px 0; max-width: 820px; margin: 0 auto; }
.loading, .empty {
  text-align: center; color: var(--muted);
  padding: 48px 20px; font-size: 15px;
}
.empty-ico { display: block; font-size: 34px; margin-bottom: 10px; }

/* Error panel — replaces the raw "Failed to fetch" */
.errbox {
  background: var(--surface); border: 1px solid var(--line);
  border-left: 4px solid var(--berry-500);
  border-radius: var(--r-md); padding: 18px; margin-bottom: 12px;
}
.errbox h3 { margin: 0 0 6px; font-size: 16px; color: var(--berry-500); }
.errbox p { margin: 0 0 10px; font-size: 14px; color: var(--muted); }
.errbox code {
  display: block; background: var(--bg); border: 1px solid var(--line);
  border-radius: var(--r-sm); padding: 9px 11px; margin: 8px 0 0;
  font-size: 12.5px; font-family: ui-monospace, "SF Mono", Menlo, monospace;
  color: var(--ink); overflow-x: auto; white-space: pre;
}

/* ---------- segmented control ---------- */

.seg {
  display: flex; gap: 4px; background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--r-md); padding: 4px;
  margin-bottom: 12px; overflow-x: auto; scrollbar-width: none;
}
.seg::-webkit-scrollbar { display: none; }
.seg button {
  flex: 1 0 auto; border: 0; background: transparent; color: var(--muted);
  font-family: var(--font-body); font-size: 14px; font-weight: 600;
  padding: 9px 13px; border-radius: 10px; cursor: pointer; white-space: nowrap;
}
.seg button:hover { color: var(--ink); }
.seg button.is-active { background: var(--brand); color: var(--cream-100); }
.seg .n { opacity: .68; font-weight: 500; }
.seg button.is-active .n { opacity: .85; }

/* ---------- cards ---------- */

.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-md); padding: 15px; margin-bottom: 10px;
  box-shadow: var(--shadow-sm);
}
.card-main { cursor: pointer; min-width: 0; }
.card-name {
  font-weight: 800; font-size: 16.5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.card-sub { color: var(--muted); font-size: 13.5px; margin-top: 1px; }
.card-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }

.pill {
  font-size: 12px; font-weight: 600; padding: 3px 9px;
  border-radius: var(--r-pill); background: var(--bg);
  border: 1px solid var(--line); color: var(--muted);
}
.pill.due { color: var(--amber-500); background: #fdf6e7; border-color: #f0dcae; }
.pill.late { color: var(--berry-500); background: #fdeff0; border-color: #f3ccd0; }
.pill.ok { color: var(--brand); background: var(--green-50); border-color: var(--green-100); }
.pill.loyal {
  color: var(--green-900); background: var(--acid-500);
  border-color: var(--acid-500); font-weight: 700;
}

@media (prefers-color-scheme: dark) {
  .pill.due { background: #2c2410; border-color: #4a3d18; color: #e3b455; }
  .pill.late { background: #2e1618; border-color: #55282c; color: #eb8b93; }
}

/* ---------- WhatsApp action ---------- */

.wa {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--brand); color: var(--cream-100);
  font-family: var(--font-body); font-weight: 700; font-size: 14.5px;
  text-decoration: none; border: 0; border-radius: var(--r-md);
  padding: 12px 15px; min-height: 46px; cursor: pointer;
  box-shadow: var(--shadow-brand);
}
.wa:hover { background: var(--green-600); }
.wa:active { background: var(--green-800); }
.wa svg { width: 17px; height: 17px; flex: 0 0 auto; fill: currentColor; }
.wa-full { width: 100%; margin-top: 12px; }
.wa[aria-disabled="true"] {
  background: var(--bg); color: var(--ink-300);
  border: 1px solid var(--line); box-shadow: none; pointer-events: none;
}

/* ---------- metrics ---------- */

.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 12px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-md); padding: 15px; box-shadow: var(--shadow-sm);
}
.kpi.wide { grid-column: 1 / -1; background: var(--brand); border-color: var(--brand); }
.kpi.wide .kpi-v { color: var(--cream-100); }
.kpi.wide .kpi-l { color: var(--lime-500); }
.kpi.wide .kpi-note { color: rgba(255,253,246,.72); }
.kpi-v { font-size: 30px; font-weight: 900; line-height: 1.05; color: var(--brand); }
.kpi-l { color: var(--muted); font-size: 12.5px; margin-top: 5px; font-weight: 600; }
.kpi-note { color: var(--muted); font-size: 11.5px; margin-top: 7px; line-height: 1.45; }

.bars {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-md); padding: 16px; margin-bottom: 12px;
  box-shadow: var(--shadow-sm);
}
.bars h3 { margin: 0 0 13px; font-size: 15px; font-weight: 800; }
.bar-row { margin-bottom: 11px; }
.bar-head { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 5px; }
.bar-head b { font-variant-numeric: tabular-nums; font-weight: 700; }
.bar-track { height: 8px; background: var(--bg); border-radius: var(--r-pill); overflow: hidden; }
.bar-fill { height: 100%; background: var(--brand); border-radius: var(--r-pill); }
.bar-row.hot .bar-fill { background: var(--acid-500); }

/* ---------- bottom tab bar ---------- */

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.tab {
  flex: 1; border: 0; background: transparent; color: var(--muted);
  height: var(--tabbar-h); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 3px;
  font-family: var(--font-body); font-size: 11px; font-weight: 700;
  cursor: pointer; position: relative;
}
.tab-ico { font-size: 19px; line-height: 1; }
.tab.is-active { color: var(--brand); }
.badge {
  position: absolute; top: 7px; left: 50%; margin-left: 7px;
  background: var(--acid-500); color: var(--green-900);
  font-size: 10.5px; font-weight: 800;
  min-width: 19px; height: 19px; border-radius: var(--r-pill);
  display: grid; place-items: center; padding: 0 5px;
}
/* An author `display` beats the UA rule for [hidden], so restore it explicitly
   — otherwise an empty badge renders as a stray dot. */
.badge[hidden] { display: none; }

/* ---------- sheet / modal ---------- */

.sheet { position: fixed; inset: 0; z-index: 40; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(18,23,13,.5); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0;
  max-height: 92vh; overflow-y: auto;
  background: var(--bg); border-radius: var(--r-lg) var(--r-lg) 0 0;
  padding: 0 16px calc(24px + var(--safe-b));
  animation: rise .22s cubic-bezier(.22,1,.36,1);
  -webkit-overflow-scrolling: touch;
}
@keyframes rise { from { transform: translateY(16px); opacity: .6; } }
.sheet-grab {
  position: sticky; top: 0; background: var(--bg); z-index: 1;
  padding: 11px 0 9px; display: grid; place-items: center; cursor: pointer;
}
.sheet-grab::before { content: ""; width: 42px; height: 4px; border-radius: var(--r-pill); background: var(--line); }
.sheet h2 { margin: 4px 0 2px; font-size: 23px; font-weight: 900; }
.sheet .sub { color: var(--muted); font-size: 14px; margin-bottom: 15px; }

.facts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 14px; }
.fact {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-sm); padding: 11px 12px;
}
.fact b { display: block; font-size: 16px; font-weight: 800; font-variant-numeric: tabular-nums; }
.fact span { color: var(--muted); font-size: 11.5px; }

.block {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-md); padding: 16px; margin-bottom: 12px;
}
.block h3 { margin: 0 0 11px; font-size: 15px; font-weight: 800; }

label.fl { display: block; font-size: 12.5px; font-weight: 700; color: var(--muted); margin: 13px 0 6px; }
input[type="text"], input[type="number"], input[type="date"], textarea, select {
  width: 100%; font-family: var(--font-body); font-size: 16px; color: var(--ink);
  background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 11px 12px;
}
input:focus, textarea:focus {
  outline: 2px solid var(--brand); outline-offset: 1px; border-color: transparent;
}
textarea { min-height: 84px; resize: vertical; line-height: 1.5; }

.stars { display: flex; gap: 8px; }
.star {
  flex: 1; min-height: 50px; border: 1px solid var(--line); background: var(--bg);
  border-radius: 10px; font-family: var(--font-body);
  font-size: 12.5px; font-weight: 700; color: var(--muted);
  cursor: pointer; display: grid; place-items: center; line-height: 1.25; padding: 5px;
}
.star:hover { border-color: var(--brand); color: var(--ink); }
.star.on { background: var(--brand); color: var(--cream-100); border-color: var(--brand); }

.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.switch {
  width: 51px; height: 30px; border-radius: var(--r-pill);
  background: var(--line); border: 0; position: relative; cursor: pointer; flex: 0 0 auto;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 24px; height: 24px; border-radius: var(--r-pill);
  background: var(--cream-100); transition: transform .16s;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.switch.on { background: var(--berry-500); }
.switch.on::after { transform: translateX(21px); }

.btn {
  width: 100%; min-height: 50px; border: 0; border-radius: var(--r-md);
  background: var(--brand); color: var(--cream-100);
  font-family: var(--font-body); font-size: 15.5px; font-weight: 800;
  cursor: pointer; margin-top: 16px; box-shadow: var(--shadow-brand);
}
.btn:hover { background: var(--green-600); }
.btn[disabled] { opacity: .55; box-shadow: none; }

.hist { border-top: 1px solid var(--line); padding: 11px 0; font-size: 13.5px; }
.hist:first-of-type { border-top: 0; }
.hist-top { display: flex; justify-content: space-between; gap: 8px; font-weight: 700; }
.hist-sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.notes-pre {
  white-space: pre-wrap; font-family: var(--font-body);
  font-size: 13.5px; color: var(--muted); margin: 0; line-height: 1.55;
}

/* ---------- toast ---------- */

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); z-index: 60;
  background: var(--green-900); color: var(--cream-100);
  padding: 12px 20px; border-radius: var(--r-pill);
  font-size: 14px; font-weight: 700;
  box-shadow: var(--shadow-lg);
}
.toast.err { background: var(--berry-500); }

/* ==========================================================================
   TABLET — 720px+
   ========================================================================== */

@media (min-width: 720px) {
  main { max-width: 1100px; padding: 18px 20px 0; }

  .grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 12px; align-items: start;
  }
  .card { margin-bottom: 0; }

  .kpis { grid-template-columns: repeat(4, 1fr); }
  .kpi.wide { grid-column: span 2; }

  .panels { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; align-items: start; }
  .bars { margin-bottom: 0; }

  .seg { max-width: 470px; }

  .sheet-panel {
    max-width: 640px; margin: 0 auto;
    border-radius: var(--r-lg); bottom: 24px; left: 16px; right: 16px;
  }
}

/* ==========================================================================
   DESKTOP — 1024px+  · bottom tabs become a sidebar
   ========================================================================== */

@media (min-width: 1024px) {
  :root { --sidebar-w: 236px; }

  body { padding-bottom: 0; }

  .topbar { position: fixed; top: 0; left: 0; right: 0; }
  .topbar-inner { padding: 11px 24px; }
  .brand img { height: 29px; }

  .tabbar {
    top: var(--topbar-h); bottom: 0; left: 0; right: auto;
    width: var(--sidebar-w);
    flex-direction: column; justify-content: flex-start; gap: 4px;
    padding: 16px 12px 0;
    border-top: 0; border-right: 1px solid var(--line);
  }
  .tab {
    flex: 0 0 auto; width: 100%; height: 44px;
    flex-direction: row; justify-content: flex-start; gap: 12px;
    padding: 0 14px; border-radius: 10px; font-size: 14.5px;
  }
  .tab:hover { background: var(--bg); }
  .tab.is-active { background: var(--green-50); }
  .tab-ico { font-size: 18px; }
  .badge { position: static; margin-left: auto; top: auto; left: auto; }

  main {
    margin-left: var(--sidebar-w); max-width: 1400px;
    padding: calc(var(--topbar-h) + 22px) 28px 44px;
  }

  .grid { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 14px; }
  .card { transition: box-shadow .14s, border-color .14s; }
  .card:hover { border-color: var(--green-100); box-shadow: var(--shadow-lg); }
  .card-main:hover .card-name { color: var(--brand); }

  .kpis { grid-template-columns: repeat(5, 1fr); gap: 14px; }
  .kpi.wide { grid-column: span 1; }
  .kpi-v { font-size: 27px; }

  .seg { max-width: 480px; }

  .sheet-panel {
    top: 50%; bottom: auto; left: 50%; right: auto;
    transform: translate(-50%, -50%);
    width: min(680px, calc(100vw - 64px)); max-width: none;
    max-height: 86vh; border-radius: var(--r-lg);
    padding: 0 24px 28px; box-shadow: var(--shadow-lg);
    animation: pop .18s cubic-bezier(.22,1,.36,1);
  }
  @keyframes pop { from { transform: translate(-50%, -48%); opacity: .5; } }
  .sheet-grab { padding: 15px 0 10px; }
  .facts { grid-template-columns: repeat(4, 1fr); }
  .toast { bottom: 28px; left: auto; right: 28px; transform: none; }
}

@media (min-width: 1440px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  .sheet-panel, .icon-btn.spin { animation: none; }
  .card, .switch::after { transition: none; }
}
