:root {
  /* Telegram theme variables with LIGHT-friendly defaults.
     Telegram always overrides these at runtime with the user's current theme. */
  --bg:           var(--tg-theme-bg-color, #ffffff);
  --bg2:          var(--tg-theme-secondary-bg-color, #f1f1f1);
  /* bg3 = slightly darker than bg2, used for tertiary chips/inputs */
  --bg3:          color-mix(in srgb, var(--bg2) 85%, #000 15%);
  --text:         var(--tg-theme-text-color, #111111);
  --muted:        var(--tg-theme-hint-color, #707579);
  --accent:       var(--tg-theme-button-color, #3390ec);
  --accent-text:  var(--tg-theme-button-text-color, #ffffff);
  --link:         var(--tg-theme-link-color, #3390ec);
  --danger:       var(--tg-theme-destructive-text-color, #e53935);

  /* Subtle horizontal separator/border that works for both themes. */
  --line:         color-mix(in srgb, var(--text) 10%, transparent);

  /* Urgency colors — fixed, readable on both themes. */
  --green:  #22c55e;
  --orange: #f59e0b;
  --red:    #ef4444;
  --yellow: #eab308;

  --radius-card: 16px;
  --radius-btn:  12px;
  --radius-chip: 999px;

  --pad: 16px;
  --gap: 12px;

  --tab-h: 64px;
  --top-h: 64px;

  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top:    env(safe-area-inset-top, 0px);
}

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

html, body {
  margin: 0; padding: 0;
  overflow-x: hidden; max-width: 100vw;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.4;
  min-height: 100vh;
  overscroll-behavior-y: contain;
}

a { color: var(--link); text-decoration: none; }

button {
  font: inherit; color: inherit;
  background: transparent; border: 0;
  cursor: pointer;
}

/* ---------- Top bar ---------- */
#top-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
  padding: calc(var(--safe-top) + 10px) var(--pad) 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--line);
}
#top-title { display: flex; flex-direction: column; flex: 1; min-width: 0; }
#screen-title { font-size: 20px; font-weight: 700; line-height: 1.15; color: var(--text); }
#updated-at { font-size: 12px; color: var(--muted); margin-top: 2px; }
#updated-at.stale { color: var(--orange); font-weight: 600; }

.icon-btn {
  flex-shrink: 0;
  width: 38px; height: 38px;
  border-radius: 10px;
  background: var(--bg2);
  color: var(--text);
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { background: var(--bg3); }

#ip-selector-wrap { flex-shrink: 0; }
#ip-selector {
  background: var(--bg2);
  color: var(--text);
  border: 1px solid transparent;
  border-radius: var(--radius-btn);
  padding: 10px 12px;
  font-size: 14px;
  max-width: 180px;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='%23707579' d='M6 8L2 4h8z'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
}
body.theme-dark #ip-selector {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='%238ea0b3' d='M6 8L2 4h8z'/></svg>");
}
#ip-selector[hidden] { display: none; }

/* ---------- Offline banner ---------- */
#offline-banner {
  position: sticky;
  top: 0;
  z-index: 25;
  background: var(--red);
  color: #fff;
  text-align: center;
  font-weight: 600;
  font-size: 13px;
  padding: 8px var(--pad);
}
#offline-banner[hidden] { display: none; }

/* ---------- PTR indicator ---------- */
#ptr-indicator {
  height: 0;
  overflow: hidden;
  text-align: center;
  padding: 0;
  transition: height 0.2s;
  color: var(--muted);
}
#ptr-indicator.visible { height: 28px; padding-top: 6px; }

/* ---------- Content ---------- */
#content {
  padding: var(--pad) var(--pad) calc(var(--tab-h) + var(--safe-bottom) + var(--pad));
  min-height: calc(100vh - var(--top-h));
}
.screen { display: none; }
.screen.active { display: block; }

/* ---------- Cards ---------- */
.card {
  background: var(--bg2);
  color: var(--text);
  max-width: 100%;
  overflow: hidden;
  border-radius: var(--radius-card);
  padding: 16px;
  margin-bottom: var(--gap);
}
.card-title {
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 10px;
  font-weight: 600;
}

.metric {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}
.metric-sub { margin-top: 4px; font-size: 14px; color: var(--muted); }
.row { display: flex; justify-content: space-between; align-items: center; gap: 10px; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.big { font-size: 16px; font-weight: 600; }

/* ---------- Colors ---------- */
.c-green  { color: var(--green); }
.c-orange { color: var(--orange); }
.c-red    { color: var(--red); }
.c-yellow { color: var(--yellow); }
.bg-green  { background: rgba(34,197,94,0.15); color: var(--green); }
.bg-orange { background: rgba(245,158,11,0.15); color: var(--orange); }
.bg-red    { background: rgba(239,68,68,0.18); color: var(--red); }
.bg-yellow { background: rgba(234,179,8,0.15); color: var(--yellow); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: var(--radius-chip);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.6;
}

/* ---------- Alert banner (home) ---------- */
.alert-banner {
  border-radius: var(--radius-card);
  padding: 16px;
  margin-bottom: var(--gap);
  color: var(--text);
}
.alert-banner.level-red    { background: linear-gradient(135deg, rgba(239,68,68,0.22), rgba(239,68,68,0.10)); border: 1px solid rgba(239,68,68,0.4); }
.alert-banner.level-orange { background: linear-gradient(135deg, rgba(245,158,11,0.22), rgba(245,158,11,0.10)); border: 1px solid rgba(245,158,11,0.4); }
.alert-banner .big-num { font-size: 36px; font-weight: 800; letter-spacing: -0.02em; line-height: 1.05; color: var(--text); }
.alert-banner .breakdown { margin-top: 6px; font-size: 13px; color: var(--text); opacity: 0.9; }
.alert-banner .btn { margin-top: 12px; width: 100%; }

/* ---------- Hero "Today" card ---------- */
.hero-today {
  background: linear-gradient(135deg, var(--bg2), var(--bg3));
  border-radius: var(--radius-card);
  padding: 20px;
  margin-bottom: var(--gap);
  text-align: center;
  color: var(--text);
  position: relative;
  overflow: hidden;
  transition: background 0.4s ease;
}

/* Trend gradients — work in both light and dark themes via rgba overlays */
.hero-today.trend-down-strong {
  background:
    linear-gradient(135deg, rgba(239, 68, 68, 0.18), rgba(239, 68, 68, 0.04)),
    linear-gradient(135deg, var(--bg2), var(--bg3));
}
.hero-today.trend-down {
  background:
    linear-gradient(135deg, rgba(239, 68, 68, 0.10), rgba(239, 68, 68, 0.02)),
    linear-gradient(135deg, var(--bg2), var(--bg3));
}
.hero-today.trend-up {
  background:
    linear-gradient(135deg, rgba(34, 197, 94, 0.10), rgba(34, 197, 94, 0.02)),
    linear-gradient(135deg, var(--bg2), var(--bg3));
}
.hero-today.trend-up-strong {
  background:
    linear-gradient(135deg, rgba(34, 197, 94, 0.18), rgba(34, 197, 94, 0.04)),
    linear-gradient(135deg, var(--bg2), var(--bg3));
}

/* Top accent stripe — subtle 3px colored line at the very top of card */
.hero-today::before {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: transparent;
  transition: background 0.4s ease;
}
.hero-today.trend-down-strong::before { background: var(--red); }
.hero-today.trend-down::before        { background: rgba(239, 68, 68, 0.6); }
.hero-today.trend-up::before          { background: rgba(34, 197, 94, 0.6); }
.hero-today.trend-up-strong::before   { background: var(--green); }
.hero-today .hdr { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600; }
.hero-today .sep { height: 1px; background: var(--line); margin: 14px 0; }
.hero-today .big-rev {
  font-size: 40px; font-weight: 800;
  letter-spacing: -0.02em; line-height: 1.0;
  color: var(--text);
}
.hero-today .big-qty {
  font-size: 18px; font-weight: 600;
  color: var(--text);
  opacity: 0.7;
  margin-top: 6px;
}
.hero-today .delta-line { font-size: 14px; font-weight: 600; margin-top: 10px; color: var(--text); }
.hero-today .forecast-line { font-size: 13px; color: var(--muted); margin-top: 4px; }

/* ---------- Top today (list) ---------- */
.top-list { padding: 0; }
.top-item {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  min-width: 0;
}
.top-item:last-child { border-bottom: 0; }
.top-item .rank {
  flex: 0 0 auto;
  font-weight: 700; color: var(--muted); font-size: 13px;
  padding-top: 2px; min-width: 20px;
}
.top-item > div:nth-child(2) { flex: 1 1 auto; min-width: 0; }
.top-item .name {
  font-weight: 600; font-size: 14px;
  line-height: 1.3;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}
.top-item .tag { color: var(--muted); font-size: 11px; margin-top: 3px; }
.top-item .qty {
  flex: 0 0 auto;
  font-weight: 700; font-size: 15px; white-space: nowrap;
  color: var(--text);
  align-self: center;
}

/* ---------- By-IP table (home) ---------- */
.ip-table { width: 100%; }
.ip-table-row {
  display: grid;
  grid-template-columns: 1.3fr 0.9fr 1fr 0.9fr;
  gap: 6px;
  align-items: center;
  padding: 10px 4px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
}
.ip-table-row:last-child { border-bottom: 0; }
.ip-table-row .name { font-weight: 600; overflow: hidden; min-width: 0; word-break: break-word; line-height: 1.25; color: var(--text); }
.ip-table-row .qty { font-weight: 700; text-align: right; color: var(--text); }
.ip-table-row .rev { color: var(--muted); text-align: right; }
.ip-table-row .delta { text-align: right; font-weight: 600; }
.ip-table-head {
  display: grid;
  grid-template-columns: 1.3fr 0.9fr 1fr 0.9fr;
  gap: 6px;
  padding: 0 4px 6px;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}
.ip-table-head .r { text-align: right; }

/* ---------- Buttons / chips ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
  border-radius: var(--radius-btn);
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  font-size: 14px;
}
.btn-secondary {
  background: var(--bg3);
  color: var(--text);
}
.btn-block { width: 100%; }
.btn-red { background: var(--red); color: #fff; }
.btn-orange { background: var(--orange); color: #111; }

.chip-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
}
.chip {
  padding: 8px 12px;
  border-radius: var(--radius-chip);
  background: var(--bg2);
  color: var(--text);
  font-size: 13px;
  white-space: nowrap;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.chip.active { background: var(--accent); color: var(--accent-text); }
.chip .chip-count {
  background: color-mix(in srgb, var(--text) 20%, transparent);
  color: inherit;
  border-radius: var(--radius-chip);
  padding: 0 6px;
  font-size: 11px;
  font-weight: 700;
}
.chip.chip-red { background: rgba(239,68,68,0.18); color: var(--red); }
.chip.chip-red.active { background: var(--red); color: #fff; }

/* ---------- Search ---------- */
.search-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-btn);
  background: var(--bg2);
  color: var(--text);
  border: 1px solid transparent;
  font-size: 14px;
  margin-bottom: 10px;
}
.search-input::placeholder { color: var(--muted); }
.search-input:focus { outline: none; border-color: var(--accent); }

/* ---------- Search overlay ---------- */
#search-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 50;
  display: flex; align-items: flex-start; justify-content: center;
}
#search-overlay[hidden] { display: none; }
#search-panel {
  background: var(--bg);
  color: var(--text);
  width: 100%;
  max-width: 720px;
  max-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
}
#search-header {
  display: flex; gap: 8px;
  padding: 10px var(--pad);
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
#search-input {
  flex: 1;
  padding: 12px 14px;
  border-radius: var(--radius-btn);
  background: var(--bg2);
  color: var(--text);
  border: 1px solid transparent;
  font-size: 15px;
}
#search-input:focus { outline: none; border-color: var(--accent); }
#search-results {
  flex: 1;
  overflow-y: auto;
  padding: var(--pad);
  padding-bottom: calc(var(--safe-bottom) + var(--pad));
}
.search-result {
  background: var(--bg2);
  color: var(--text);
  border-radius: var(--radius-card);
  padding: 12px 14px;
  margin-bottom: 8px;
  cursor: pointer;
  display: grid;
  grid-template-columns: 10px 1fr auto;
  gap: 4px 10px;
  align-items: center;
}
.search-result .dot {
  width: 10px; height: 10px; border-radius: 50%;
  grid-row: 1 / span 2;
}
.search-result .title { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.search-result .meta { font-size: 12px; color: var(--muted); }
.search-result .right { text-align: right; grid-row: 1 / span 2; }
.search-result .right .days { font-weight: 700; font-size: 15px; }

/* ---------- Stock item ---------- */
.stock-item {
  background: var(--bg2);
  color: var(--text);
  border-radius: var(--radius-card);
  padding: 14px;
  margin-bottom: 10px;
  cursor: pointer;
  max-width: 100%;
}
.stock-item .title-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 6px;
  min-width: 0;
}
.stock-item .urgency-dot {
  flex: 0 0 auto;
  width: 10px; height: 10px;
  border-radius: 50%;
  margin-top: 6px;
}
.stock-item .title {
  flex: 1 1 auto;
  min-width: 0;
  font-weight: 600;
  font-size: 14px;
  line-height: 1.3;
  word-break: break-word;
  color: var(--text);
}
.stock-item .days-badge {
  flex: 0 0 auto;
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
  align-self: flex-start;
  margin-top: 2px;
}
.stock-item .meta-row {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
  padding-left: 18px;
  word-break: break-word;
}

/* ---------- Alert item ---------- */
.alert-group { margin-bottom: 16px; }
.alert-group-title {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 13px; font-weight: 600; color: var(--muted);
  padding: 6px 4px 8px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  cursor: pointer;
  user-select: none;
}
.alert-group-title .caret { transition: transform 0.15s; display: inline-block; margin-right: 4px; }
.alert-group.collapsed .caret { transform: rotate(-90deg); }
.alert-group.collapsed .alert-group-body { display: none; }
.alert-item {
  background: var(--bg2);
  color: var(--text);
  border-radius: var(--radius-card);
  padding: 14px;
  margin-bottom: 8px;
  cursor: pointer;
  border-left: 3px solid var(--orange);
}
.alert-item.kind-fbo_low   { border-left-color: var(--red); }
.alert-item.kind-fbs_low   { border-left-color: var(--orange); }
.alert-item.kind-sales_drop   { border-left-color: var(--red); }
.alert-item.kind-sales_spike  { border-left-color: var(--green); }
.alert-item .head {
  display: flex; justify-content: space-between; gap: 8px;
  font-weight: 600; margin-bottom: 4px;
  color: var(--text);
}
.alert-item .sub { font-size: 13px; color: var(--muted); }
.alert-item .meta-row { margin-top: 6px; font-size: 12px; color: var(--muted); }

/* ---------- Forecast item ---------- */
.forecast-section {
  margin-bottom: 16px;
}
.forecast-section-title {
  font-size: 13px; font-weight: 700;
  padding: 6px 4px 10px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  display: flex; align-items: center; justify-content: space-between;
  cursor: pointer;
}
.forecast-section-title .caret { transition: transform 0.15s; display: inline-block; }
.forecast-section.collapsed .caret { transform: rotate(-90deg); }
.forecast-section.collapsed .forecast-section-body { display: none; }
.forecast-item {
  background: var(--bg2);
  color: var(--text);
  border-radius: var(--radius-card);
  padding: 14px;
  margin-bottom: 8px;
  cursor: pointer;
  border-left: 3px solid var(--green);
}
.forecast-item.u-urgent { border-left-color: var(--red); background: rgba(239,68,68,0.08); }
.forecast-item.u-warning { border-left-color: var(--orange); }
.forecast-item.u-ok { border-left-color: var(--green); }
.forecast-item .head {
  display: flex; justify-content: space-between; gap: 8px; align-items: baseline;
  font-weight: 600; margin-bottom: 4px;
  color: var(--text);
}
.forecast-item .brand { color: var(--muted); font-size: 12px; font-weight: 500; }
.forecast-item .sub { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.forecast-item .action { font-size: 13px; font-weight: 600; color: var(--text); }

/* ---------- Budget screen ---------- */
.balance-card {
  border-radius: var(--radius-card);
  padding: 20px;
  margin-bottom: var(--gap);
  text-align: center;
  color: var(--text);
}
.balance-card.lvl-red    { background: linear-gradient(135deg, rgba(239,68,68,0.22), rgba(239,68,68,0.08)); border: 1px solid rgba(239,68,68,0.4); }
.balance-card.lvl-orange { background: linear-gradient(135deg, rgba(245,158,11,0.22), rgba(245,158,11,0.08)); border: 1px solid rgba(245,158,11,0.4); }
.balance-card.lvl-green  { background: linear-gradient(135deg, rgba(34,197,94,0.18), var(--bg2)); border: 1px solid rgba(34,197,94,0.25); }
.balance-card .ip-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600; }
.balance-card .balance { font-size: 40px; font-weight: 800; letter-spacing: -0.02em; margin-top: 8px; line-height: 1.0; color: var(--text); }
.balance-card .status { font-weight: 700; font-size: 15px; margin-top: 8px; }
.balance-card .bonus { font-size: 13px; color: var(--muted); margin-top: 6px; }

.campaign-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
  align-items: center;
  font-size: 14px;
  color: var(--text);
}
.campaign-row:last-child { border-bottom: 0; }
.campaign-row .id { font-weight: 600; }
.campaign-row .budget { text-align: right; font-weight: 700; }
.campaign-row .warn { color: var(--red); font-size: 12px; margin-left: 4px; }

/* ---------- Tab bar ---------- */
#tab-bar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tab-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--bg2);
  border-top: 1px solid var(--line);
  z-index: 30;
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 500;
  position: relative;
}
.tab.active { color: var(--accent); }
.tab-icon { font-size: 20px; line-height: 1; }
.tab-badge {
  position: absolute;
  top: 6px;
  right: calc(50% - 22px);
  background: var(--red);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  border-radius: 999px;
  padding: 1px 6px;
  min-width: 18px;
  text-align: center;
  line-height: 1.4;
}

/* ---------- States ---------- */
.loader {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 12px;
  color: var(--muted);
}
.loader::before {
  content: "";
  width: 18px; height: 18px;
  border: 2px solid var(--muted);
  border-top-color: transparent;
  border-radius: 50%;
  margin-right: 10px;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Skeletons ---------- */
@keyframes sk-pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 0.7; }
}
.sk {
  background: var(--bg3);
  border-radius: 8px;
  animation: sk-pulse 1.1s ease-in-out infinite;
}
.sk-line { height: 12px; margin: 6px 0; }
.sk-card {
  background: var(--bg2);
  border-radius: var(--radius-card);
  padding: 16px;
  margin-bottom: var(--gap);
}

.empty {
  text-align: center;
  padding: 32px 16px;
  color: var(--muted);
}
.empty.ok {
  background: rgba(34,197,94,0.1);
  border-radius: var(--radius-card);
  color: var(--green);
  font-weight: 600;
}
.empty-emoji { font-size: 36px; margin-bottom: 10px; }

.error-card {
  background: rgba(239,68,68,0.1);
  border: 1px solid rgba(239,68,68,0.25);
  border-radius: var(--radius-card);
  padding: 16px;
  text-align: center;
  color: var(--text);
}
.error-card .btn { margin-top: 12px; }

.info-card {
  background: rgba(245,158,11,0.10);
  border: 1px solid rgba(245,158,11,0.30);
  border-radius: var(--radius-card);
  padding: 12px 14px;
  color: var(--orange);
  font-size: 13px;
  margin-bottom: var(--gap);
}

/* ---------- Stocks summary header ---------- */
.stocks-summary {
  background: var(--bg2);
  color: var(--text);
  border-radius: var(--radius-card);
  padding: 14px 16px;
  margin-bottom: var(--gap);
}
.stocks-summary .title { font-size: 14px; font-weight: 700; margin-bottom: 4px; color: var(--text); }
.stocks-summary .meta { font-size: 12px; color: var(--muted); }
.stocks-summary .crit { font-size: 13px; font-weight: 600; margin-top: 8px; }

/* ---------- Utility ---------- */
.stack > * + * { margin-top: var(--gap); }
.hrule { height: 1px; background: var(--line); margin: 8px 0; }
.trend-wrap { height: 160px; position: relative; }
.week-total { margin-top: 6px; font-size: 12px; color: var(--muted); text-align: right; }

/* ---------- Responsive / narrow screens ---------- */
@media (max-width: 360px) {
  .hero-today .big-rev { font-size: 32px; }
  .hero-today .big-qty { font-size: 16px; }
  .ip-table-row { grid-template-columns: 1.2fr 0.8fr 1fr; }
  .ip-table-row .delta { display: none; }
  .ip-table-head { grid-template-columns: 1.2fr 0.8fr 1fr; }
  .ip-table-head .r.delta { display: none; }
}

/* ---------- Desktop tweaks ---------- */
@media (min-width: 720px) {
  body { max-width: 720px; margin: 0 auto; }
  #top-bar { border-radius: 0; }
}

/* ---------- Brands tab ---------- */
.brand-sort {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin: 4px 0 10px;
}
.brand-sort button {
  background: var(--bg2); color: var(--text);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
.brand-sort button.active {
  background: var(--accent); color: #fff; border-color: var(--accent);
}
.brand-list { padding: 0; }
.brand-row {
  padding: 10px 4px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.brand-row:last-child { border-bottom: 0; }
.brand-row-head {
  display: flex; justify-content: space-between; align-items: center;
  font-weight: 600;
}
.brand-row-stats {
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px;
  margin-top: 4px; font-size: 13px;
}
.brand-row-meta { margin-top: 4px; }
.brand-alert {
  background: rgba(239,68,68,0.12); color: #ef4444;
  padding: 2px 8px; border-radius: 12px; font-size: 12px;
}
.brand-back {
  background: transparent; border: 0; color: var(--accent);
  font-size: 14px; cursor: pointer; margin: 6px 0 8px; padding: 4px 0;
}
.brand-sku-list { padding: 0; }
.brand-sku-item {
  padding: 10px 4px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.brand-sku-item:last-child { border-bottom: 0; }
.brand-sku-item .head {
  display: flex; justify-content: space-between; align-items: center;
}
.brand-sku-item .meta-row { font-size: 12px; margin-top: 2px; }
.brand-sku-item.urgency-red { border-left: 3px solid #ef4444; padding-left: 8px; }
.brand-sku-item.urgency-orange { border-left: 3px solid #f59e0b; padding-left: 8px; }
.brand-sku-item.urgency-yellow { border-left: 3px solid #eab308; padding-left: 8px; }
.brand-sku-item.urgency-green { border-left: 3px solid #10b981; padding-left: 8px; }

/* Compact tab labels for 6 tabs */
#tab-bar .tab-label { font-size: 11px; }
@media (max-width: 360px) {
  #tab-bar .tab-label { font-size: 10px; }
  #tab-bar .tab-icon { font-size: 18px; }
}

/* ========== Feature add-ons (v17) ========== */

/* Alert resolve button — subtle, bottom-right of card */
.alert-item { position: relative; }
.alert-resolve-btn {
  position: absolute;
  right: 8px;
  bottom: 8px;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.alert-resolve-btn:hover, .alert-resolve-btn:active {
  background: var(--bg2);
  color: var(--accent);
}
.alert-item.alert-resolving .alert-resolve-btn {
  color: #10b981;
  border-color: #10b981;
  background: rgba(16, 185, 129, 0.08);
}

/* Period tab chips */
.chip-row.period-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}
.chip.period-chip {
  padding: 6px 12px;
  font-size: 13px;
  border-radius: 999px;
  background: var(--bg2);
  border: 1px solid var(--line);
  cursor: pointer;
}
.chip.period-chip.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.period-custom-form .period-date-row {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
}
.period-custom-form input[type="date"] {
  flex: 1 1 130px;
  min-width: 0;
  font: inherit;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--bg2);
  color: inherit;
  border: 1px solid var(--line);
}
.period-custom-form .btn-primary {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 6px;
  padding: 6px 12px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.period-hero .big-rev { font-size: 36px; font-weight: 800; letter-spacing: -0.02em; line-height: 1.0; color: var(--text); }
.period-hero .big-qty { font-size: 16px; color: var(--text); opacity: 0.7; margin-top: 6px; font-weight: 600; }

.ip-table-row.period-row,
.ip-table-head.period-row {
  display: grid;
  grid-template-columns: 1.4fr 0.9fr 1.1fr 0.6fr;
  gap: 8px;
  padding: 8px 4px;
  align-items: baseline;
}
.ip-table-head.period-row { font-weight: 600; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); }
.period-sort { cursor: pointer; }
.period-sort:hover { color: var(--accent); }
.ip-table-row.period-row { border-bottom: 1px solid var(--line); }
.ip-table-row.period-row .qty,
.ip-table-row.period-row .rev,
.ip-table-row.period-row .share { text-align: right; }


/* IP name tag inside stock card title (visible only in 'all IPs' view) */
.stock-item .ip-tag {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 7px;
  font-size: 10px;
  font-weight: 600;
  color: var(--muted);
  background: color-mix(in srgb, var(--bg2), var(--text) 5%);
  border-radius: 999px;
  vertical-align: middle;
}


/* ------ Period: trend gradient + delta line + colored share bars ------ */
.period-hero {
  position: relative;
  overflow: hidden;
  transition: background 0.4s ease;
}
.period-hero.trend-down-strong {
  background:
    linear-gradient(135deg, rgba(239, 68, 68, 0.18), rgba(239, 68, 68, 0.04)),
    var(--bg2);
}
.period-hero.trend-down {
  background:
    linear-gradient(135deg, rgba(239, 68, 68, 0.10), rgba(239, 68, 68, 0.02)),
    var(--bg2);
}
.period-hero.trend-up {
  background:
    linear-gradient(135deg, rgba(34, 197, 94, 0.10), rgba(34, 197, 94, 0.02)),
    var(--bg2);
}
.period-hero.trend-up-strong {
  background:
    linear-gradient(135deg, rgba(34, 197, 94, 0.18), rgba(34, 197, 94, 0.04)),
    var(--bg2);
}
.period-hero::before {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: transparent;
  transition: background 0.4s ease;
}
.period-hero.trend-down-strong::before { background: var(--red); }
.period-hero.trend-down::before        { background: rgba(239, 68, 68, 0.6); }
.period-hero.trend-up::before          { background: rgba(34, 197, 94, 0.6); }
.period-hero.trend-up-strong::before   { background: var(--green); }

.period-delta-line {
  margin-top: 12px;
  padding: 10px 12px;
  background: color-mix(in srgb, var(--bg2), var(--text) 5%);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.period-delta-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  text-align: left;
}
.period-delta-row .muted { font-weight: 600; min-width: 80px; }
.delta-badge {
  font-weight: 700;
  font-size: 13px;
  padding: 1px 7px;
  border-radius: 6px;
  display: inline-block;
}
.delta-badge.up   { color: #15803d; background: rgba(34, 197, 94, 0.12); }
.delta-badge.down { color: #b91c1c; background: rgba(239, 68, 68, 0.12); }

/* Per-IP share bar — subtle horizontal gradient inside the row */
.period-row-bar {
  position: relative;
  isolation: isolate;
}
.period-row-bar::before {
  content: "";
  position: absolute;
  top: 4px; bottom: 4px; left: 0;
  width: var(--share, 0%);
  background: linear-gradient(90deg, rgba(51, 144, 236, 0.10), rgba(51, 144, 236, 0.02));
  border-radius: 6px;
  z-index: -1;
  transition: width 0.4s ease;
}
.period-row-bar .share {
  font-weight: 700;
  color: var(--text);
}


/* v23 additions */
.avg-check-line {
  margin-top: 10px;
  font-size: 14px;
  color: var(--fg, #1c1c1c);
  line-height: 1.4;
}
.avg-check-line b { font-weight: 700; }
.stockout-card { padding: 12px 14px; }
.stockout-card .stockout-amount {
  font-size: 18px;
  font-weight: 700;
  color: #b3261e;
  margin-top: 2px;
}
.penalties-card { padding: 12px 14px; }
.penalties-card .penalties-amount {
  font-size: 18px;
  font-weight: 700;
  color: #b3261e;
  margin-top: 2px;
}


/* Penalties screen */
.penalties-card { cursor: pointer; }
.penalties-card:active { transform: scale(0.99); }
.penalties-screen .pen-hero {
  padding: 16px;
  border-radius: 14px;
  margin-bottom: 14px;
  background: linear-gradient(135deg, rgba(255,67,67,0.12), rgba(255,67,67,0.02));
  border: 1px solid rgba(255,67,67,0.18);
}
.penalties-screen .pen-hero-title {
  font-size: 13px; opacity: 0.7; margin-bottom: 4px;
}
.penalties-screen .pen-hero-amount {
  font-size: 28px; font-weight: 700; color: var(--tg-theme-text-color);
}
.penalties-screen .pen-hero-sub {
  font-size: 13px; opacity: 0.7; margin-top: 4px;
}
.penalties-screen .pen-byip {
  margin-bottom: 14px;
}
.penalties-screen .pen-byip-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 12px; border-radius: 10px; background: var(--bg2);
  margin-bottom: 6px;
}
.penalties-screen .pen-byip-name { font-weight: 600; }
.penalties-screen .pen-byip-amount { color: var(--red); font-weight: 600; }
.penalties-screen .pen-byip-count { font-size: 12px; opacity: 0.6; margin-left: 6px; }
.penalties-screen .pen-list-title {
  font-size: 14px; font-weight: 600; margin: 16px 0 8px;
}
.penalties-screen .pen-row {
  padding: 10px 12px; border-radius: 10px; background: var(--bg2);
  margin-bottom: 6px;
}
.penalties-screen .pen-row-top {
  display: flex; justify-content: space-between; gap: 8px;
}
.penalties-screen .pen-row-date {
  font-size: 12px; opacity: 0.6;
}
.penalties-screen .pen-row-amount {
  color: var(--red); font-weight: 700;
}
.penalties-screen .pen-row-reason {
  font-size: 13px; margin: 4px 0 2px;
}
.penalties-screen .pen-row-meta {
  font-size: 12px; opacity: 0.65;
}
.penalties-screen .pen-empty {
  text-align: center; padding: 40px 20px; opacity: 0.6;
}

/* ===================== Sales tab ===================== */
.period-chips {
  display: flex; gap: 8px; flex-wrap: wrap;
}
.period-chips button {
  background: var(--card-bg, #2c2c2c); color: inherit;
  border: 1px solid transparent; border-radius: 999px;
  padding: 6px 14px; font-size: 13px; cursor: pointer;
}
.period-chips button.active {
  background: var(--accent, #3390ec); color: #fff;
}

.warn-card { border-left: 3px solid #f59e0b; }

.funnel { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.funnel-row {
  position: relative; padding: 12px 14px; border-radius: 10px;
  background: linear-gradient(90deg, var(--accent, #3390ec) var(--w, 100%), rgba(127,127,127,0.12) var(--w, 100%));
  color: #fff; display: flex; align-items: center; gap: 10px;
  font-size: 14px; font-weight: 500;
  transition: background 0.3s;
}
.funnel-row.r2 { background: linear-gradient(90deg, #4f9eed var(--w, 100%), rgba(127,127,127,0.12) var(--w, 100%)); }
.funnel-row.r3 { background: linear-gradient(90deg, #6caefa var(--w, 100%), rgba(127,127,127,0.12) var(--w, 100%)); }
.funnel-row.r4 { background: linear-gradient(90deg, #22c55e var(--w, 100%), rgba(127,127,127,0.12) var(--w, 100%)); }
.funnel-label { flex: 1; text-shadow: 0 1px 1px rgba(0,0,0,0.25); }
.funnel-val { font-weight: 700; text-shadow: 0 1px 1px rgba(0,0,0,0.25); }
.funnel-cr {
  margin-left: 8px; font-size: 12px; padding: 2px 8px;
  background: rgba(0,0,0,0.25); border-radius: 999px;
}

.cr-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px;
  margin-top: 10px;
}
.cr-cell {
  padding: 14px 10px; border-radius: 10px;
  background: rgba(127,127,127,0.08); text-align: center;
}
.cr-num { font-size: 22px; font-weight: 700; color: var(--accent, #3390ec); }

.cr-chip {
  display: inline-block; padding: 1px 7px; border-radius: 999px;
  background: rgba(51,144,236,0.15); color: var(--accent, #3390ec);
  font-size: 12px; font-weight: 600;
}

.sales-ip-head, .sales-ip-row {
  display: grid;
  grid-template-columns: minmax(60px, 1.4fr) 1.1fr 0.9fr 0.9fr 0.7fr;
  gap: 6px; padding: 8px 4px; align-items: center;
}
.sales-ip-head { font-size: 11px; opacity: 0.6; text-transform: uppercase; letter-spacing: 0.4px; }
.sales-ip-head .r, .sales-ip-row .r { text-align: right; }
.sales-ip-row { border-top: 1px solid rgba(127,127,127,0.12); cursor: pointer; }
.sales-ip-row:active { background: rgba(127,127,127,0.08); }
.sales-ip-name { font-weight: 600; font-size: 14px; }

.top-list { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.top-row {
  display: grid; grid-template-columns: 36px 1fr auto; gap: 10px;
  align-items: center; padding: 10px 4px;
  border-top: 1px solid rgba(127,127,127,0.10);
  cursor: pointer;
}
.top-row:first-child { border-top: none; }
.top-row:active { background: rgba(127,127,127,0.06); }
.top-rank { font-weight: 700; font-size: 14px; opacity: 0.7; text-align: center; }
.top-star { color: #f59e0b; margin-right: 2px; }
.top-title { font-size: 14px; }
.top-meta { margin-top: 2px; }
.top-cr { margin-top: 4px; display: flex; gap: 6px; }
.top-rev { white-space: nowrap; font-size: 14px; }

/* Ad balance card on Home */
.ad-balance-card { padding: 12px 14px; }
.ad-bal-list { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.ad-bal-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0; border-top: 1px solid rgba(127,127,127,0.10);
}
.ad-bal-row:first-child { border-top: none; }
.ad-bal-name { font-size: 14px; font-weight: 500; }
.ad-bal-amount { text-align: right; display: flex; flex-direction: column; gap: 2px; }
.ad-bal-row.low .ad-bal-amount b { color: #ef4444; }
