: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);
}
.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-qty {
  font-size: 40px; font-weight: 800;
  letter-spacing: -0.02em; line-height: 1.0;
  color: var(--text);
}
.hero-today .big-rev {
  font-size: 22px; font-weight: 600;
  /* CRITICAL FIX: explicit text color for both themes.
     Was var(--muted) which became invisible on the hero's tinted bg in light mode. */
  color: var(--text);
  opacity: 0.85;
  margin-top: 4px;
}
.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-qty { font-size: 32px; }
  .hero-today .big-rev { font-size: 18px; }
  .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; }
}
