:root {
  --bg: #07110f;
  --surface: rgba(14, 29, 25, 0.86);
  --surface-strong: #10231e;
  --line: rgba(169, 211, 191, 0.13);
  --text: #edf8f2;
  --muted: #8da79c;
  --accent: #b7f56c;
  --accent-soft: rgba(183, 245, 108, 0.13);
  --danger: #ff897d;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.28);
  --radius: 22px;
}

* { box-sizing: border-box; }

html { min-width: 320px; min-height: 100%; background: var(--bg); }

body {
  min-height: var(--tg-viewport-height, 100dvh);
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 10% -20%, rgba(75, 142, 112, 0.16), transparent 34rem),
    linear-gradient(145deg, #07110f 0%, #091612 52%, #07100e 100%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input { font: inherit; }
button { color: inherit; }
select { font: inherit; }

.auth-screen { position: relative; z-index: 5; display: grid; min-height: var(--tg-viewport-height, 100dvh); place-items: center; padding: 24px; }
.auth-card { width: min(100%, 420px); padding: 34px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.auth-card .brand-mark { margin-bottom: 24px; }
.auth-card h1 { margin: 0 0 8px; font-size: 36px; letter-spacing: -.05em; }
.auth-copy { margin: 0 0 24px; color: var(--muted); line-height: 1.5; }
.auth-card form { display: grid; gap: 15px; }
.auth-card label { display: grid; gap: 7px; color: var(--muted); font-size: 12px; }
.auth-card input, .auth-card select { width: 100%; padding: 12px 13px; border: 1px solid var(--line); border-radius: 11px; outline: 0; color: var(--text); background: rgba(5, 15, 12, .65); }
.auth-card input:focus, .auth-card select:focus { border-color: rgba(183, 245, 108, .45); }
.text-button { width: 100%; margin-top: 16px; border: 0; color: var(--accent); background: transparent; cursor: pointer; font-size: 12px; }

.ambient {
  position: fixed;
  z-index: 0;
  width: 28rem;
  height: 28rem;
  border-radius: 50%;
  filter: blur(110px);
  pointer-events: none;
  opacity: .13;
}
.ambient-one { top: 20%; left: -18rem; background: var(--accent); }
.ambient-two { right: -15rem; bottom: -10rem; background: #59d8b0; }

.topbar {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 1440px;
  margin: 0 auto;
  padding: calc(28px + env(safe-area-inset-top)) 38px 28px;
}

.brand { display: flex; gap: 13px; align-items: center; color: inherit; text-decoration: none; }
.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 1px solid rgba(183, 245, 108, .45);
  border-radius: 13px;
  color: #0a160f;
  background: var(--accent);
  font-weight: 900;
  box-shadow: 0 0 30px rgba(183, 245, 108, .15);
}
.brand strong, .brand small { display: block; }
.brand strong { letter-spacing: -.02em; }
.brand small { margin-top: 2px; color: var(--muted); font-size: 11px; letter-spacing: .05em; }

.system-state { display: flex; gap: 9px; align-items: center; color: var(--muted); font-size: 13px; }
.status-dot { width: 8px; height: 8px; flex: 0 0 auto; border-radius: 50%; background: var(--danger); box-shadow: 0 0 14px rgba(255, 137, 125, .5); }
.status-dot.online { background: var(--accent); box-shadow: 0 0 14px rgba(183, 245, 108, .55); }
.status-dot.pending { background: #f2c56b; }
.icon-button { width: 34px; height: 34px; margin-left: 8px; border: 1px solid var(--line); border-radius: 50%; background: transparent; cursor: pointer; }
.icon-button:hover { border-color: rgba(183, 245, 108, .45); color: var(--accent); }

.shell { position: relative; z-index: 1; max-width: 1440px; margin: 0 auto; padding: 28px 38px calc(64px + env(safe-area-inset-bottom)); }
.hero { display: flex; align-items: end; justify-content: space-between; gap: 60px; margin: 25px 0 42px; }
.eyebrow { margin: 0 0 10px; color: var(--accent); font-size: 11px; font-weight: 800; letter-spacing: .16em; text-transform: uppercase; }
.hero h1 { margin: 0; font-size: clamp(42px, 5vw, 72px); line-height: .98; letter-spacing: -.065em; font-weight: 750; }
.hero h1 span { color: var(--muted); }
.hero-copy { max-width: 400px; margin: 0 0 4px; color: var(--muted); line-height: 1.6; }

.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 14px; }
.stat-card { min-height: 152px; padding: 24px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); backdrop-filter: blur(18px); }
.stat-card.accent { background: linear-gradient(135deg, rgba(183,245,108,.18), rgba(14,29,25,.86) 68%); }
.stat-card span, .stat-card small { display: block; color: var(--muted); font-size: 12px; }
.stat-card strong { display: block; margin: 10px 0 7px; font-size: 42px; line-height: 1; letter-spacing: -.05em; }

.mini-summary-panel { display: none; }

.workspace { display: grid; grid-template-columns: minmax(300px, 380px) 1fr; gap: 14px; min-height: 650px; }
.panel { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); backdrop-filter: blur(18px); overflow: hidden; }
.chat-panel { padding: 24px 14px 14px; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; padding: 0 10px; }
.panel-heading h2 { margin: 0; font-size: 22px; letter-spacing: -.035em; }
.count-pill { min-width: 32px; padding: 5px 9px; border-radius: 100px; color: var(--accent); background: var(--accent-soft); text-align: center; font-size: 12px; }

.search-box { display: flex; align-items: center; gap: 10px; margin: 22px 6px 12px; padding: 11px 13px; border: 1px solid var(--line); border-radius: 13px; background: rgba(5, 15, 12, .55); color: var(--muted); }
.search-box:focus-within { border-color: rgba(183, 245, 108, .4); }
.search-box input { width: 100%; border: 0; outline: 0; color: var(--text); background: transparent; }
.search-box input::placeholder { color: #647a71; }
.filters { display: flex; gap: 5px; margin: 0 6px 14px; }
.filter { padding: 7px 10px; border: 0; border-radius: 9px; color: var(--muted); background: transparent; font-size: 12px; cursor: pointer; }
.filter.active { color: #0c1712; background: var(--accent); font-weight: 750; }

.chat-list { display: flex; flex-direction: column; gap: 6px; max-height: 520px; overflow-y: auto; }
.chat-item { width: 100%; padding: 14px; border: 1px solid transparent; border-radius: 15px; background: transparent; text-align: left; cursor: pointer; transition: .18s ease; }
.chat-item:hover { background: rgba(255,255,255,.025); border-color: var(--line); }
.chat-item.selected { border-color: rgba(183,245,108,.28); background: var(--accent-soft); }
.chat-item-top { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.chat-item strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.chat-item p { margin: 7px 0 0; color: var(--muted); font-size: 11px; }
.state-label { font-size: 10px; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); }
.state-label.inactive { color: var(--danger); }

.messages-panel { min-width: 0; padding: 28px; }
.empty-state { display: grid; min-height: 590px; place-content: center; justify-items: center; text-align: center; }
.empty-symbol { display: grid; width: 64px; height: 64px; place-items: center; margin-bottom: 18px; border: 1px solid var(--line); border-radius: 20px; color: var(--accent); background: var(--accent-soft); font-size: 26px; }
.empty-state h2 { margin: 0 0 8px; }
.empty-state p { max-width: 330px; margin: 0; color: var(--muted); line-height: 1.5; }
.hidden { display: none !important; }

.detail-heading { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding-bottom: 24px; border-bottom: 1px solid var(--line); }
.detail-title-row { display: flex; align-items: center; gap: 10px; min-width: 0; }
.detail-heading h2 { min-width: 0; margin: 0; font-size: 26px; letter-spacing: -.04em; overflow-wrap: anywhere; }
.mobile-back { display: none; align-items: center; gap: 5px; flex: 0 0 auto; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; color: var(--muted); background: transparent; cursor: pointer; font-size: 12px; }
.mobile-back:hover { border-color: rgba(183,245,108,.45); color: var(--accent); }
.detail-heading p { margin: 7px 0 0 18px; color: var(--muted); font-size: 12px; }
.detail-actions { display: flex; align-items: center; gap: 9px; }
.primary-button, .secondary-button { border: 0; border-radius: 12px; cursor: pointer; font-weight: 750; }
.primary-button { padding: 12px 17px; color: #0b160f; background: var(--accent); }
.primary-button:hover { filter: brightness(1.06); transform: translateY(-1px); }
.primary-button:disabled { opacity: .5; cursor: wait; transform: none; }
.secondary-button { display: block; margin: 18px auto 0; padding: 10px 16px; color: var(--text); background: rgba(255,255,255,.05); }
.system-state .secondary-button { margin: 0 4px; padding: 8px 11px; font-size: 11px; }
.detail-actions .secondary-button { margin: 0; padding: 12px 17px; }

.digest-result { margin: 18px 0; padding: 16px 18px; border: 1px solid rgba(183,245,108,.22); border-radius: 14px; color: #dff8ca; background: var(--accent-soft); line-height: 1.55; font-size: 13px; }
.messages-heading { display: flex; align-items: center; justify-content: space-between; margin: 25px 0 12px; }
.messages-heading h3 { margin: 0; font-size: 14px; }
.messages-heading span { color: var(--muted); font-size: 11px; }
.message-list { display: flex; flex-direction: column; gap: 8px; max-height: 465px; overflow-y: auto; padding-right: 5px; }
.message { padding: 14px 16px; border: 1px solid var(--line); border-radius: 14px; background: rgba(4, 13, 10, .35); }
.message-head { display: flex; justify-content: space-between; gap: 15px; color: var(--muted); font-size: 11px; }
.message-author { color: var(--accent); font-weight: 700; }
.message-body { margin: 8px 0 0; color: #dbe9e2; font-size: 13px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere; }
.content-badge { display: inline-block; margin-top: 8px; padding: 3px 7px; border-radius: 6px; color: var(--muted); background: rgba(255,255,255,.05); font-size: 10px; }
.list-placeholder { padding: 30px 10px; color: var(--muted); text-align: center; font-size: 13px; }

.dialog-open { overflow: hidden; }
.agent-dialog {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(2, 8, 6, .72);
  backdrop-filter: blur(12px);
}
.agent-window {
  display: grid;
  grid-template-rows: auto minmax(260px, 1fr) auto;
  width: min(100%, 760px);
  max-height: min(820px, calc(var(--tg-viewport-height, 100dvh) - 48px));
  border: 1px solid rgba(183,245,108,.2);
  border-radius: 24px;
  overflow: hidden;
  background: #0d1e19;
  box-shadow: 0 36px 110px rgba(0,0,0,.55);
}
.agent-header { display: flex; align-items: start; justify-content: space-between; gap: 20px; padding: 23px 25px 19px; border-bottom: 1px solid var(--line); }
.agent-header h2 { margin: 0; font-size: 24px; letter-spacing: -.035em; }
.agent-header p:last-child { margin: 5px 0 0; color: var(--muted); font-size: 12px; }
.agent-header .icon-button { margin: 0; font-size: 21px; }
.agent-header-actions { display: flex; align-items: center; gap: 8px; }
.agent-clear-button { padding: 8px 11px; border: 1px solid rgba(255,137,125,.3); border-radius: 10px; color: var(--danger); background: transparent; cursor: pointer; font-size: 11px; font-weight: 700; white-space: nowrap; }
.agent-clear-button:hover { border-color: rgba(255,137,125,.65); background: rgba(255,137,125,.08); }
.agent-clear-button:disabled { opacity: .5; cursor: wait; }
.agent-messages { display: flex; flex-direction: column; gap: 12px; min-height: 0; padding: 24px; overflow-y: auto; }
.agent-message { align-self: flex-start; max-width: min(82%, 560px); padding: 13px 15px; border: 1px solid var(--line); border-radius: 6px 16px 16px; background: rgba(255,255,255,.04); }
.agent-message.user { align-self: flex-end; border-color: rgba(183,245,108,.22); border-radius: 16px 6px 16px 16px; background: var(--accent-soft); }
.agent-message-role { color: var(--accent); font-size: 10px; font-weight: 800; letter-spacing: .1em; text-transform: uppercase; }
.agent-message p { margin: 6px 0 0; color: #dbe9e2; font-size: 13px; line-height: 1.55; white-space: pre-wrap; overflow-wrap: anywhere; }
.agent-markdown { margin-top: 6px; color: #dbe9e2; font-size: 13px; line-height: 1.55; overflow-wrap: anywhere; }
.agent-markdown p { margin: 0 0 9px; white-space: normal; }
.agent-markdown p:last-child { margin-bottom: 0; }
.agent-markdown h3, .agent-markdown h4, .agent-markdown h5 { margin: 12px 0 5px; color: var(--text); font-size: 14px; line-height: 1.35; }
.agent-markdown h3:first-child, .agent-markdown h4:first-child, .agent-markdown h5:first-child { margin-top: 0; }
.agent-markdown ul, .agent-markdown ol { margin: 6px 0 9px; padding-left: 21px; }
.agent-markdown li + li { margin-top: 4px; }
.agent-markdown code { padding: 1px 5px; border: 1px solid var(--line); border-radius: 5px; background: rgba(0,0,0,.22); font-size: .92em; }
.agent-message.pending { opacity: .72; }
.agent-message.pending p { animation: agent-thinking 1.2s ease-in-out infinite; }
@keyframes agent-thinking {
  0%, 100% { opacity: .45; }
  50% { opacity: 1; }
}
.agent-form { padding: 18px 24px 22px; border-top: 1px solid var(--line); background: rgba(4,13,10,.32); }
.agent-form label { display: block; margin-bottom: 8px; color: var(--muted); font-size: 11px; }
.agent-composer { display: flex; align-items: end; gap: 10px; }
.agent-composer textarea { width: 100%; min-height: 50px; max-height: 150px; resize: vertical; padding: 13px 14px; border: 1px solid var(--line); border-radius: 13px; outline: 0; color: var(--text); background: rgba(5,15,12,.7); font: inherit; line-height: 1.4; }
.agent-composer textarea:focus { border-color: rgba(183,245,108,.42); }
.agent-form small { display: block; margin-top: 8px; color: #6f887d; font-size: 10px; }

.toast { position: fixed; z-index: 20; right: 24px; bottom: 24px; max-width: 380px; padding: 13px 16px; border: 1px solid var(--line); border-radius: 12px; background: #172b25; box-shadow: var(--shadow); opacity: 0; transform: translateY(12px); pointer-events: none; transition: .2s ease; }
.toast.visible { opacity: 1; transform: translateY(0); }
.toast.error { border-color: rgba(255,137,125,.4); }

::-webkit-scrollbar { width: 7px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { border-radius: 10px; background: rgba(141,167,156,.2); }

body.telegram-mini-app {
  overscroll-behavior-y: none;
}

body.telegram-mini-app .stats-grid:not(.mini-stats-grid) {
  display: none;
}

body.telegram-mini-app .mini-summary-panel {
  display: block;
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}

body.telegram-mini-app .mini-summary-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 72px;
  padding: 8px 18px 0;
  cursor: pointer;
  list-style: none;
}

body.telegram-mini-app .mini-summary-summary::-webkit-details-marker {
  display: none;
}

body.telegram-mini-app .mini-summary-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: 56px;
  text-align: left;
}

body.telegram-mini-app .mini-summary-text {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 2px;
  min-height: 56px;
}

body.telegram-mini-app .mini-summary-header .eyebrow {
  margin: 0;
}

body.telegram-mini-app .mini-summary-header h2 {
  margin: 0;
  font-size: 20px;
  letter-spacing: -.035em;
}

body.telegram-mini-app .mini-summary-chevron {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--muted);
  transition: transform .18s ease;
  flex: 0 0 auto;
  margin-left: auto;
  align-self: center;
}

body.telegram-mini-app details[open] .mini-summary-chevron {
  transform: rotate(180deg);
}

body.telegram-mini-app .mini-stats-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin: 0;
  padding: 0 14px 14px;
}

body.telegram-mini-app .mini-stats-grid .stat-card {
  min-height: 88px;
  padding: 14px 16px;
  border-radius: 14px;
}

body.telegram-mini-app .mini-stats-grid .stat-card strong {
  margin: 6px 0 4px;
  font-size: 28px;
}

body.telegram-mini-app .hero h1 {
  font-size: clamp(22px, 4vw, 34px);
  line-height: 1.06;
  letter-spacing: -.045em;
}

body.telegram-mini-app .hero h1 span {
  display: block;
  margin-top: 2px;
}

body.telegram-mini-app .workspace {
  display: block;
  grid-template-columns: 1fr;
  min-height: 0;
}

body.telegram-mini-app .chat-panel {
  display: block !important;
  order: 0;
  margin-top: 0;
}

body.telegram-mini-app:not(.mini-chat-selected) .messages-panel {
  display: none !important;
}

body.telegram-mini-app.mini-chat-selected .chat-panel {
  display: none !important;
}

body.telegram-mini-app.mini-chat-selected .messages-panel {
  display: block !important;
  margin-top: 14px;
}

body.telegram-mini-app .chat-list {
  max-height: none;
  overflow: visible;
}

@media (max-width: 700px) {
  .topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
    border-bottom: 1px solid var(--line);
    background: rgba(7, 17, 15, .88);
    backdrop-filter: blur(18px);
  }

  .topbar .brand-mark {
    width: 36px;
    height: 36px;
    border-radius: 11px;
  }

  .brand strong {
    font-size: 13px;
  }

  .system-state {
    gap: 5px;
    font-size: 11px;
  }

  #account-label,
  #health-label,
  #process-pending,
  #register-button {
    display: none !important;
  }

  .shell {
    padding: 12px 12px calc(24px + env(safe-area-inset-bottom));
  }

  .hero {
    margin: 2px 0 8px;
    gap: 2px;
  }

  .hero h1 {
    font-size: 20px;
    line-height: 1.05;
    letter-spacing: -.035em;
  }

  .hero-copy {
    display: none;
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 10px;
  }

  .stat-card {
    min-height: 112px;
    padding: 17px;
    border-radius: 18px;
  }

  .stat-card.accent {
    grid-column: 1 / -1;
  }

  .stat-card strong {
    margin: 8px 0 6px;
    font-size: 34px;
  }

  .workspace {
    grid-template-columns: 1fr;
    min-height: 0;
  }

  .panel {
    border-radius: 18px;
  }

  .chat-panel {
    padding: 18px 10px 10px;
  }

  .chat-list {
    max-height: none;
    overflow-y: visible;
  }

  .chat-item {
    min-height: 68px;
    padding: 14px;
  }

  .messages-panel {
    padding: 18px;
    min-height: 0;
  }

  body:not(.mobile-chat-open) .messages-panel {
    display: none;
  }

  body.mobile-chat-open .chat-panel {
    display: none;
  }

  .empty-state {
    min-height: 45vh;
  }

  .detail-heading {
    align-items: stretch;
    flex-direction: column;
    gap: 16px;
    padding-bottom: 18px;
  }

  .detail-title-row {
    align-items: center;
    gap: 8px;
  }

  .mobile-back {
    display: inline-flex;
  }

  .detail-heading h2 {
    font-size: 21px;
  }

  .detail-heading p {
    margin-left: 28px;
    line-height: 1.4;
  }

  .detail-actions {
    width: 100%;
    align-items: stretch;
    flex-direction: column;
  }

  .detail-actions .primary-button,
  .detail-actions .secondary-button {
    width: 100%;
    min-height: 44px;
  }

  .messages-heading {
    gap: 10px;
  }

  .messages-heading h3 {
    font-size: 13px;
  }

  .message-list {
    max-height: calc(var(--tg-viewport-height, 100dvh) * .55);
    min-height: 220px;
    padding-right: 2px;
  }

  .message {
    padding: 13px 14px;
  }

  .message-head {
    gap: 8px;
    flex-wrap: wrap;
  }

  .message-body {
    font-size: 13px;
  }

  .toast {
    right: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom));
    max-width: calc(100vw - 24px);
  }
}

@media (min-width: 701px) and (max-width: 900px) {
  .topbar, .shell { padding-left: 20px; padding-right: 20px; }
  .hero { align-items: start; flex-direction: column; gap: 18px; }
  .stats-grid { grid-template-columns: 1fr; }
  .stat-card { min-height: 125px; }
  .workspace { grid-template-columns: 1fr; }
  .chat-list { max-height: 360px; }
}

@media (max-width: 560px) {
  .brand small, #health-label { display: none; }
  .shell { padding-top: 8px; }
  .hero h1 { font-size: 42px; }
  .messages-panel { padding: 20px; }
  .detail-heading { align-items: start; flex-direction: column; }
  .primary-button { width: 100%; }
  .detail-actions { width: 100%; flex-direction: column; align-items: stretch; }
  .detail-actions .secondary-button { width: 100%; }
  .agent-dialog { padding: 0; }
  .agent-window { width: 100%; max-height: var(--tg-viewport-height, 100dvh); min-height: var(--tg-viewport-height, 100dvh); border: 0; border-radius: 0; }
  .agent-header, .agent-messages, .agent-form { padding-left: 18px; padding-right: 18px; }
  .agent-message { max-width: 92%; }
  .agent-composer { align-items: stretch; flex-direction: column; }
}

.access-window { width: min(560px, calc(100vw - 32px)); min-height: 0; }
.access-users { display: grid; gap: 8px; overflow-y: auto; padding: 20px 24px 28px; }
.access-user { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 13px 15px; border: 1px solid var(--line); border-radius: 12px; background: rgba(5, 15, 12, .55); }
.access-user strong { overflow: hidden; text-overflow: ellipsis; }
.access-user input { width: 18px; height: 18px; accent-color: var(--accent); }
