:root {
  --bg: var(--tg-theme-secondary-bg-color, #f4f6f8);
  --surface: var(--tg-theme-bg-color, #ffffff);
  --surface-strong: #ffffff;
  --text: var(--tg-theme-text-color, #18212b);
  --muted: var(--tg-theme-hint-color, #72808f);
  --line: rgba(32, 51, 68, 0.10);
  --accent: var(--tg-theme-button-color, #127a78);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --blue: #2c8ccf;
  --green: #26a665;
  --shadow: 0 10px 30px rgba(28, 43, 57, 0.08);
  --safe-bottom: max(12px, env(safe-area-inset-bottom));
  color-scheme: light;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface-strong: var(--tg-theme-bg-color, #17212b);
    --line: rgba(255, 255, 255, 0.09);
    --shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; }
button { color: inherit; }
button:disabled, textarea:disabled { cursor: not-allowed; opacity: .55; }

.app-shell {
  width: min(100%, 760px);
  min-height: 100vh;
  margin: 0 auto;
  background: var(--bg);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 11px;
  min-height: 68px;
  padding: max(12px, env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(16px);
}

.brand-mark {
  position: relative;
  flex: 0 0 36px;
  width: 36px;
  height: 36px;
}

.brand-mark span {
  position: absolute;
  width: 17px;
  height: 17px;
  border-radius: 50%;
}

.brand-mark span:nth-child(1) { left: 2px; top: 2px; background: #06a9eb; }
.brand-mark span:nth-child(2) { right: 2px; top: 2px; background: #a947b9; }
.brand-mark span:nth-child(3) { left: 9px; bottom: 1px; background: #15b88a; }

.brand-copy { min-width: 0; display: grid; flex: 1; }
.brand-copy strong { font-size: 15px; line-height: 1.25; }
.brand-copy span { color: var(--muted); font-size: 12px; margin-top: 2px; }

.mode-badge {
  padding: 6px 8px;
  border: 1px solid color-mix(in srgb, var(--accent) 28%, transparent);
  border-radius: 999px;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  font-size: 10px;
  font-weight: 700;
  white-space: nowrap;
}

.notice {
  display: flex;
  gap: 10px;
  margin: 12px 16px 0;
  padding: 11px 13px;
  border: 1px solid color-mix(in srgb, var(--blue) 25%, transparent);
  border-radius: 13px;
  background: color-mix(in srgb, var(--blue) 8%, var(--surface));
  color: color-mix(in srgb, var(--text) 82%, var(--blue));
  font-size: 13px;
  line-height: 1.4;
}

.notice-icon {
  display: grid;
  place-items: center;
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  color: white;
  background: var(--blue);
  font-weight: 800;
}

main { padding: 0 16px calc(92px + var(--safe-bottom)); }
.screen { display: none; animation: reveal 180ms ease-out; }
.screen.active { display: block; }

@keyframes reveal {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

.screen-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 92px;
  padding-top: 14px;
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 28px; line-height: 1.15; letter-spacing: -.03em; }
h2 { margin-bottom: 5px; font-size: 18px; line-height: 1.25; }

.icon-button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--surface);
  cursor: pointer;
}
.icon-button:active { transform: scale(.96); }

.hero-card, .metric-card, .safety-card, .chat-card, .item-card, .client-card, .settings-group, .info-card {
  background: var(--surface-strong);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.hero-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 19px;
  border-radius: 20px;
}

.hero-card p { margin: 0; color: var(--muted); font-size: 13px; }
.muted-label { color: var(--muted); font-size: 12px; }

.status-orb {
  display: grid;
  place-items: center;
  flex: 0 0 52px;
  width: 52px;
  height: 52px;
  border-radius: 18px;
  background: color-mix(in srgb, var(--green) 12%, var(--surface));
}
.status-orb span { width: 15px; height: 15px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 7px color-mix(in srgb, var(--green) 14%, transparent); }
.status-orb.loading span { background: var(--muted); animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .35; } }

.metric-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
.metric-card { display: flex; min-height: 154px; flex-direction: column; align-items: flex-start; padding: 16px; border-radius: 18px; }
.metric-card strong { margin-top: 5px; font-size: 23px; line-height: 1.15; }
.metric-icon { display: grid; place-items: center; width: 34px; height: 34px; margin-bottom: 13px; border-radius: 11px; font-weight: 800; }
.metric-icon.blue { color: var(--blue); background: color-mix(in srgb, var(--blue) 12%, transparent); }
.metric-icon.green { color: var(--green); background: color-mix(in srgb, var(--green) 12%, transparent); }
.metric-icon.warning { color: #d9822b; background: color-mix(in srgb, #d9822b 12%, transparent); }
.metric-footnote { margin-top: auto; color: var(--muted); font-size: 11px; }
.text-button { margin-top: auto; padding: 0; border: 0; background: none; color: var(--accent); font-size: 12px; font-weight: 700; cursor: pointer; }

.safety-card { display: flex; gap: 13px; margin-top: 12px; padding: 16px; border-radius: 18px; box-shadow: none; }
.safety-card p, .info-card p { margin: 5px 0 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.shield { display: grid; place-items: center; flex: 0 0 32px; width: 32px; height: 32px; border-radius: 10px; color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); font-weight: 900; }

.list-state { display: grid; gap: 10px; }
.chat-card { width: 100%; padding: 15px; border-radius: 17px; text-align: left; cursor: pointer; }
.chat-card:active { transform: scale(.992); }
.chat-top { display: flex; gap: 12px; align-items: center; }
.avatar { display: grid; place-items: center; flex: 0 0 42px; width: 42px; height: 42px; border-radius: 14px; color: var(--accent); background: color-mix(in srgb, var(--accent) 11%, var(--surface)); font-weight: 800; }
.chat-main { min-width: 0; flex: 1; }
.chat-line { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.chat-line strong, .chat-title, .chat-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-line strong { font-size: 15px; }
.chat-time { flex: 0 0 auto; color: var(--muted); font-size: 11px; }
.chat-title { margin-top: 3px; color: var(--muted); font-size: 12px; }
.chat-preview-row { display: flex; gap: 9px; align-items: center; margin-top: 12px; padding-top: 11px; border-top: 1px solid var(--line); }
.chat-preview { min-width: 0; flex: 1; color: var(--muted); font-size: 13px; }
.unread-count { display: grid; place-items: center; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 999px; color: var(--accent-text); background: var(--accent); font-size: 11px; font-weight: 800; }

.status-filter { display: flex; gap: 8px; margin: -2px 0 14px; }
.filter-chip { min-height: 36px; padding: 0 15px; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); background: var(--surface); cursor: pointer; }
.filter-chip.active { border-color: color-mix(in srgb, var(--accent) 46%, transparent); color: var(--accent-text); background: var(--accent); font-weight: 750; }
.item-card { padding: 16px; border-radius: 18px; }
.client-card { display: grid; gap: 12px; padding: 16px; border-radius: 18px; }
.client-card p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.45; }
.problem-list { display: flex; flex-wrap: wrap; gap: 6px; }
.problem-chip { padding: 5px 8px; border-radius: 999px; color: #b44b18; background: color-mix(in srgb, #d9822b 12%, var(--surface)); font-size: 10px; font-weight: 750; }
.attention-card { margin: 0 0 12px; border-color: color-mix(in srgb, #d9822b 35%, var(--line)); }
.attention-list { display: grid; gap: 7px; margin-top: 12px; }
.attention-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%; padding: 10px 11px; border: 1px solid var(--line); border-radius: 12px; color: var(--text); background: var(--surface); text-align: left; cursor: pointer; }
.attention-item span { color: var(--muted); font-size: 11px; }
.attention-item strong { font-size: 12px; }
.date-filter { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: -2px 0 14px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 14px; color: var(--muted); background: var(--surface-strong); font-size: 12px; }
.date-filter input { min-height: 34px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 9px; color: var(--text); background: var(--surface); font: inherit; }
.analytics-card .item-facts > span { min-width: 76px; }
.analytics-meta { margin-top: 12px; color: var(--muted); font-size: 11px; line-height: 1.5; }
.campaign-summary { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 12px; }
.campaign-summary > .loading-state, .campaign-summary > .error-state { grid-column: 1 / -1; }
.campaign-metric { display: grid; gap: 5px; min-height: 88px; padding: 14px; border: 1px solid var(--line); border-radius: 16px; background: var(--surface-strong); box-shadow: var(--shadow); }
.campaign-metric span { color: var(--muted); font-size: 11px; line-height: 1.3; }
.campaign-metric strong { align-self: end; font-size: 20px; letter-spacing: -.02em; }
.campaign-items { display: grid; gap: 10px; }
.campaign-item .item-alias { overflow-wrap: anywhere; }
.campaign-analysis { border-color: color-mix(in srgb, var(--accent) 28%, var(--line)); }
.campaign-analysis[data-status="ready"] { background: color-mix(in srgb, var(--accent) 5%, var(--surface-strong)); }
.campaign-insights { display: grid; gap: 9px; margin-top: 13px; }
.insight-group { padding: 11px 12px; border-radius: 13px; background: var(--bg); }
.insight-group strong { font-size: 12px; }
.insight-group ul { margin: 7px 0 0; padding-left: 18px; color: var(--muted); font-size: 12px; line-height: 1.45; }
.insight-group.risk { border-left: 3px solid #d9822b; }
.insight-group.positive { border-left: 3px solid var(--green); }
.insight-group.action { border-left: 3px solid var(--blue); }
.section-title { margin: 22px 0 12px; font-size: 17px; }
.client-sheet { width: min(100%, 760px); max-width: none; height: 100%; max-height: none; margin: 0 auto; padding: 0; border: 0; color: var(--text); background: var(--bg); }
.client-sheet::backdrop { background: rgba(17, 28, 39, .38); backdrop-filter: blur(5px); }
.client-detail { display: grid; gap: 12px; padding: 16px 14px 48px; }
.client-detail .info-card { margin: 0; }
.client-detail ol { margin: 10px 0 0; padding-left: 22px; color: var(--muted); font-size: 13px; line-height: 1.5; }
.item-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.item-header > div { min-width: 0; display: grid; gap: 4px; }
.item-header strong { overflow: hidden; font-size: 15px; line-height: 1.35; text-overflow: ellipsis; }
.item-alias { color: var(--muted); font-size: 11px; font-weight: 750; }
.status-pill { flex: 0 0 auto; padding: 5px 8px; border-radius: 999px; color: var(--muted); background: var(--bg); font-size: 10px; font-weight: 750; }
.status-pill.active { color: var(--green); background: color-mix(in srgb, var(--green) 11%, var(--surface)); }
.status-pill.blocked, .status-pill.rejected { color: #d9475b; background: color-mix(in srgb, #d9475b 10%, var(--surface)); }
.item-facts { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.item-facts > span { min-width: 88px; display: grid; gap: 2px; }
.item-facts small { color: var(--muted); font-size: 10px; }
.item-facts b { font-size: 12px; }
.item-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.service-summary { min-width: 0; color: var(--muted); font-size: 11px; line-height: 1.4; }
.text-button.compact { flex: 0 0 auto; margin: 0; }
.promotion-note { margin-bottom: 6px; }
.empty-state, .error-state, .loading-state { padding: 44px 24px; border: 1px dashed var(--line); border-radius: 18px; color: var(--muted); text-align: center; line-height: 1.5; }
.loading-state::before { content: ""; display: block; width: 24px; height: 24px; margin: 0 auto 13px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.load-more, .primary-button { width: 100%; min-height: 48px; border: 0; border-radius: 14px; color: var(--accent-text); background: var(--accent); font-weight: 750; cursor: pointer; }
.load-more { margin-top: 12px; }

.settings-group { overflow: hidden; border-radius: 18px; }
.setting-row { display: flex; justify-content: space-between; gap: 20px; padding: 15px 16px; border-bottom: 1px solid var(--line); }
.setting-row:last-child { border-bottom: 0; }
.setting-row span:first-child { color: var(--muted); font-size: 13px; }
.setting-row strong { text-align: right; font-size: 13px; }
.info-card { margin-top: 12px; padding: 16px; border-radius: 18px; box-shadow: none; }

.bottom-nav {
  position: fixed;
  z-index: 30;
  left: 50%;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  width: min(100%, 760px);
  padding: 8px 8px var(--safe-bottom);
  transform: translateX(-50%);
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(18px);
}

.nav-item { position: relative; display: grid; justify-items: center; gap: 3px; min-height: 52px; padding: 6px 1px; border: 0; color: var(--muted); background: transparent; font-size: 9px; cursor: pointer; }
.nav-item > span:first-child { font-size: 19px; line-height: 1; }
.nav-item.active { color: var(--accent); font-weight: 800; }
.nav-item b { position: absolute; top: 2px; left: calc(50% + 8px); min-width: 16px; height: 16px; padding: 1px 4px; border-radius: 999px; color: #fff; background: #e34b5f; font-size: 9px; }

dialog { color: var(--text); }
.chat-sheet { width: min(100%, 760px); max-width: none; height: 100%; max-height: none; margin: 0 auto; padding: 0; border: 0; background: var(--bg); }
.chat-sheet::backdrop, .unlock-dialog::backdrop { background: rgba(17, 28, 39, .38); backdrop-filter: blur(5px); }
.sheet-header { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 12px; min-height: 70px; padding: max(12px, env(safe-area-inset-top)) 16px 10px; background: var(--surface); border-bottom: 1px solid var(--line); }
.sheet-header div { min-width: 0; display: grid; }
.sheet-header div { flex: 1; }
.sheet-header strong, .sheet-header span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-header span { color: var(--muted); font-size: 12px; }
.sheet-action { flex: 0 0 auto; min-height: 34px; padding: 0 10px; border: 1px solid var(--line); border-radius: 10px; color: var(--accent); background: var(--surface); font-size: 11px; font-weight: 750; cursor: pointer; }
.message-list { display: grid; gap: 9px; padding: 18px 14px 148px; }
.message { width: fit-content; max-width: 84%; padding: 11px 13px 8px; border-radius: 16px 16px 16px 5px; background: var(--surface); border: 1px solid var(--line); box-shadow: 0 4px 14px rgba(27, 45, 61, .05); }
.message.out { justify-self: end; border-radius: 16px 16px 5px 16px; background: color-mix(in srgb, var(--accent) 11%, var(--surface)); }
.message p { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 14px; line-height: 1.42; }
.message-image { display: block; width: min(100%, 560px); max-height: 520px; margin-top: 9px; border-radius: 12px; object-fit: contain; background: var(--bg); }
.message .message-file-note { margin-top: 8px; padding: 9px 10px; border-radius: 10px; color: var(--muted); background: var(--bg); font-size: 12px; }
.message time { display: block; margin-top: 5px; color: var(--muted); font-size: 10px; text-align: right; }
.message-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 7px; padding-top: 6px; border-top: 1px solid var(--line); }
.message-actions button { padding: 0; border: 0; color: var(--accent); background: none; font-size: 10px; cursor: pointer; }
.message-actions .danger-link { color: #d9475b; }
.message-composer { position: fixed; bottom: 0; width: min(100%, 760px); padding: 9px 12px max(9px, env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--line); }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.message-composer textarea { width: 100%; min-height: 44px; max-height: 112px; resize: vertical; padding: 11px 12px; border: 1px solid var(--line); border-radius: 14px; color: var(--text); background: var(--bg); outline: none; }
.message-composer textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 11%, transparent); }
.composer-send { display: grid; place-items: center; flex: 0 0 44px; width: 44px; height: 44px; border: 0; border-radius: 14px; color: var(--accent-text); background: var(--accent); cursor: pointer; }
.message-composer > p { margin: 6px 3px 0; color: var(--muted); font-size: 9px; line-height: 1.3; }
.composer-mode { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 7px; padding: 7px 10px; border-radius: 10px; color: var(--accent); background: color-mix(in srgb, var(--accent) 9%, var(--surface)); font-size: 11px; }
.composer-mode[hidden] { display: none; }
.composer-mode button { padding: 0; border: 0; color: var(--muted); background: none; cursor: pointer; }

.unlock-dialog { width: calc(100% - 32px); max-width: 400px; padding: 0; border: 1px solid var(--line); border-radius: 22px; background: var(--surface); box-shadow: 0 28px 80px rgba(16, 29, 40, .25); }
.unlock-dialog form { padding: 24px; }
.unlock-dialog h2 { text-align: center; }
.unlock-dialog p { color: var(--muted); font-size: 13px; line-height: 1.5; text-align: center; }
.unlock-icon { display: grid; place-items: center; width: 52px; height: 52px; margin: 0 auto 14px; border-radius: 17px; color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); font-size: 26px; }
.unlock-dialog label { display: block; margin: 18px 0 7px; font-size: 12px; font-weight: 700; }
.unlock-dialog input { width: 100%; min-height: 48px; margin-bottom: 10px; padding: 0 13px; border: 1px solid var(--line); border-radius: 12px; color: var(--text); background: var(--bg); outline: none; }
.unlock-dialog input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 12%, transparent); }
.unlock-dialog .form-error { min-height: 20px; margin: 0 0 8px; color: #d9475b; text-align: left; }

.toast { position: fixed; z-index: 100; left: 50%; bottom: calc(84px + var(--safe-bottom)); width: max-content; max-width: calc(100% - 32px); padding: 10px 14px; transform: translateX(-50%); border-radius: 12px; color: #fff; background: #263440; box-shadow: var(--shadow); font-size: 13px; }

@media (min-width: 620px) {
  main { padding-left: 24px; padding-right: 24px; }
  .topbar { padding-left: 24px; padding-right: 24px; }
  .metric-grid { gap: 16px; }
  .chat-sheet { height: min(880px, calc(100% - 32px)); margin: 16px auto; border-radius: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; }
}
