:root {
  --font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Inter, Arial, sans-serif;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-soft: #f7f7f7;
  --surface-muted: #f0f0f5;
  --surface-strong: #e9e9ef;
  --text: #1d1d1f;
  --text-soft: #6e6e73;
  --text-muted: #86868b;
  --text-inverse: #ffffff;
  --border: #d2d2d7;
  --border-soft: #efeff4;
  --shadow: 0 4px 18px rgba(0,0,0,.07);
  --shadow-sm: 0 1px 4px rgba(0,0,0,.1);
  --primary: #0071e3;
  --primary-2: #00aaff;
  --success: #34c759;
  --success-soft: #dff6dd;
  --warning: #ffcc00;
  --warning-soft: #fff5b4;
  --danger: #ff3b30;
  --danger-soft: #ffdce0;
  --info: #0366d6;
  --info-soft: #f1f8ff;
  --radius-xs: 8px;
  --radius-sm: 10px;
  --radius-md: 12px;
  --radius-lg: 15px;
  --radius-xl: 20px;
  --radius-2xl: 28px;
  --tg-viewport-height: 100vh;
  --tg-viewport-stable-height: 100vh;
  --tg-safe-top: 0px;
  --tg-safe-bottom: 0px;
  --tg-safe-left: 0px;
  --tg-safe-right: 0px;
  --tg-content-safe-top: 0px;
  --tg-content-safe-bottom: 0px;
  --tg-content-safe-left: 0px;
  --tg-content-safe-right: 0px;
  --safe-top: max(env(safe-area-inset-top), var(--tg-safe-top));
  --safe-bottom: max(env(safe-area-inset-bottom), var(--tg-safe-bottom));
  --safe-left: max(env(safe-area-inset-left), var(--tg-safe-left));
  --safe-right: max(env(safe-area-inset-right), var(--tg-safe-right));
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { min-height: 100%; background: var(--bg); }
body {
  font-family: var(--font-family);
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { border: 0; background: none; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .5; }

.app-shell {
  width: min(720px, 100%);
  min-height: var(--tg-viewport-height);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--surface);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border-soft);
  padding: 14px 16px;
}
.header-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.header-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.header-title { font-size: 21px; font-weight: 800; letter-spacing: -.02em; }
.header-subtitle { color: var(--text-muted); font-size: 13px; margin-top: 2px; }
.header-actions { display: flex; align-items: center; gap: 8px; }
.avatar {
  width: 42px; height: 42px; border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 700; flex-shrink: 0;
}
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--surface-muted); color: var(--primary);
}

.auth-panel {
  margin: 14px 16px 0;
  padding: 14px;
  border-radius: var(--radius-xl);
  background: var(--surface);
  box-shadow: var(--shadow);
  border: 1px solid var(--border-soft);
}
.auth-panel.hidden { display: none; }
.auth-title { font-weight: 700; margin-bottom: 8px; }
.auth-controls { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.auth-extra { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }

.app-body { flex: 1; overflow: auto; padding: 14px 16px 92px; }
.view { display: none; animation: viewIn .18s ease both; }
.view.active { display: flex; }
@keyframes viewIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.card, .section {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
  border: 1px solid var(--border-soft);
  margin-bottom: 14px;
}
.card-body { padding: 16px; }
.card-header, .section-header {
  padding: 14px 16px;
  background: var(--surface-soft);
  border-bottom: 1px solid var(--border-soft);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; font-weight: 800; color: var(--text-muted); }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.stack { display: grid; gap: 10px; }
.grid-2 { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 12px; }

.btn {
  min-height: 42px; padding: 10px 14px; border-radius: var(--radius-sm);
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font-weight: 700; transition: transform .12s ease, opacity .12s ease, background .12s ease;
  background: var(--surface-muted); color: var(--text);
}
.btn:active, .icon-btn:active, .nav-item:active { transform: scale(.97); }
.btn-primary, .btn.primary { background: var(--primary); color: var(--text-inverse); }
.btn-secondary { background: var(--surface-muted); color: var(--text); }
.btn-danger, .btn.danger { background: var(--danger); color: #fff; }
.btn-small { min-height: 34px; padding: 7px 10px; font-size: 13px; }

.input, .form-input, .form-select, select, textarea {
  width: 100%; min-height: 42px; padding: 10px 12px;
  background: var(--surface-muted); border: 1px solid transparent;
  border-radius: var(--radius-sm); outline: none;
}
.input:focus, .form-input:focus, select:focus, textarea:focus { border-color: var(--primary); background: var(--surface); }

.badge {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 700;
  background: var(--surface-muted); color: var(--text-soft);
}
.badge-success { background: var(--success-soft); color: #22863a; }
.badge-warning { background: var(--warning-soft); color: #735600; }
.badge-danger { background: var(--danger-soft); color: #cb2431; }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-primary { background: var(--primary); color: #fff; }
.text-muted { color: var(--text-muted); }
.text-soft { color: var(--text-soft); }
.small { font-size: 12px; }
.output { white-space: pre-wrap; word-break: break-word; font-size: 12px; background: var(--surface-muted); padding: 12px; border-radius: var(--radius-sm); max-height: 220px; overflow: auto; }

.metric-card { background: var(--surface); border-radius: var(--radius-lg); padding: 16px; box-shadow: var(--shadow); border: 1px solid var(--border-soft); }
.metric-label { color: var(--text-muted); font-size: 13px; margin-bottom: 8px; }
.metric-value { font-size: 24px; font-weight: 800; letter-spacing: -.03em; }

.task-board { display: grid; gap: 12px; }
.columns-container { display: grid; gap: 12px; }
.column { background: var(--surface-muted); border-radius: var(--radius-lg); padding: 12px; min-height: 120px; }
.column-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.column-title { font-weight: 800; color: var(--text-soft); }
.task-card { background: var(--surface); border-radius: var(--radius-md); padding: 12px; margin-bottom: 10px; box-shadow: var(--shadow-sm); }
.task-title { font-weight: 700; margin-bottom: 5px; }
.task-description { color: var(--text-muted); font-size: 13px; margin-bottom: 8px; }
.task-meta { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--text-muted); }
.priority-high { color: var(--danger); }
.priority-medium { color: #b88700; }
.priority-low { color: var(--success); }

.unread-badge { background: var(--danger); color: #fff; min-width: 20px; height: 20px; border-radius: 999px; display: inline-flex; align-items: center; justify-content: center; font-size: 12px; padding: 0 6px; }

.chat-layout { display: grid; gap: 12px; }
.messages-container {
  min-height: 360px; max-height: 54vh; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column;
  border-radius: var(--radius-lg);
  background: linear-gradient(180deg, #ececef 0%, #f7f7f9 100%);
  border: 1px solid var(--border-soft);
}
.message { max-width: 78%; padding: 11px 14px; margin-bottom: 10px; border-radius: 18px; box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.message.received { background: var(--surface); align-self: flex-start; border-top-left-radius: 5px; }
.message.sent { background: var(--primary); color: #fff; align-self: flex-end; border-top-right-radius: 5px; }
.message.system { background: var(--surface-muted); color: var(--text-muted); align-self: center; max-width: 90%; font-size: 12px; }
.message-time { font-size: 11px; margin-top: 5px; opacity: .72; }
.input-area { display: flex; align-items: center; gap: 8px; background: var(--surface); padding: 10px; border-radius: var(--radius-lg); box-shadow: var(--shadow); border: 1px solid var(--border-soft); }
.input-container { flex: 1; background: var(--surface-muted); border-radius: 22px; display: flex; align-items: center; padding: 0 12px; min-width: 0; }
.input-container input { flex: 1; border: none; background: transparent; outline: none; min-height: 42px; min-width: 0; }
.send-button { width: 42px; height: 42px; border-radius: 50%; background: var(--primary); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-weight: 800; }

.wallet-main { background: linear-gradient(135deg, var(--primary), var(--primary-2)); padding: 16px; border-radius: var(--radius-2xl); color: #fff; margin-bottom: 14px; }
.balance-card { background: rgba(255,255,255,.2); backdrop-filter: blur(12px); border-radius: var(--radius-xl); padding: 22px; text-align: center; margin-bottom: 14px; border: 1px solid rgba(255,255,255,.24); }
.balance-label { font-size: 14px; opacity: .9; margin-bottom: 8px; }
.balance-amount { font-size: 34px; font-weight: 900; letter-spacing: -.05em; }
.balance-currency { font-size: 16px; opacity: .82; }
.balance-change { font-size: 13px; margin-top: 8px; color: #dff6dd; }
.quick-actions { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 12px; }
.action-card { background: var(--surface); color: var(--text); border-radius: var(--radius-lg); padding: 16px; text-align: center; box-shadow: 0 4px 12px rgba(0,0,0,.08); }
.action-icon, .asset-icon, .setting-icon, .item-icon {
  width: 40px; height: 40px; border-radius: 12px; background: var(--surface-muted); color: var(--primary);
  display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.action-card .action-icon { width: 48px; height: 48px; margin: 0 auto 10px; border-radius: 50%; }
.action-title { font-weight: 800; margin-bottom: 4px; }
.action-desc { color: var(--text-muted); font-size: 12px; }
.asset-value, .transaction-value { text-align: right; font-weight: 800; margin-left: auto; }
.transaction-value.credit { color: var(--success); }
.transaction-value.debit { color: var(--danger); }

.toggle-switch { position: relative; display: inline-block; width: 50px; height: 30px; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; cursor: pointer; background: #e0e0e0; transition: .22s; border-radius: 30px; }
.slider::before { content: ""; position: absolute; height: 22px; width: 22px; left: 4px; bottom: 4px; background: #fff; transition: .22s; border-radius: 50%; box-shadow: 0 1px 3px rgba(0,0,0,.18); }
.toggle-switch input:checked + .slider { background: #4cd964; }
.toggle-switch input:checked + .slider::before { transform: translateX(20px); }

.nav-menu {
  position: fixed; left: 50%; bottom: 0; transform: translateX(-50%);
  width: min(720px, 100%); z-index: 40;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface);
  backdrop-filter: blur(18px);
  padding: 8px 6px calc(8px + var(--safe-bottom));
  border-top: 1px solid var(--border-soft);
}
.nav-item { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--text-muted); font-size: 11px; min-height: 50px; border-radius: 14px; }
.nav-item.active { color: var(--primary); background: var(--info-soft); }
.nav-icon { font-size: 20px; line-height: 1; }

.toast { position: fixed; left: 50%; bottom: calc(78px + var(--safe-bottom)); transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none; z-index: 70; background: rgba(20,20,24,.92); color: #fff; padding: 10px 14px; border-radius: 999px; max-width: min(92vw, 640px); transition: .2s ease; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.empty { text-align: center; color: var(--text-muted); padding: 24px 12px; }
.hidden { display: none !important; }

@media (max-width: 640px) {
  .app-body { padding: 12px 12px 90px; }
  .auth-panel { margin: 12px 12px 0; }
  .grid-2 { grid-template-columns: 1fr; }
  .auth-controls { grid-template-columns: 1fr; }
  .header-title { font-size: 19px; }
  .balance-amount { font-size: 30px; }
  .messages-container { max-height: 48vh; }
}

.service-strip {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 12px 0 14px;
}
.service-pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 5px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  color: var(--text-soft);
}
.service-pill.ok { background: var(--success-soft); color: #1f7a38; }
.service-pill.warn { background: var(--warning-soft); color: #735600; }
.service-pill.fail { background: var(--danger-soft); color: #b42318; }
.service-pill.pending { background: var(--surface-muted); color: var(--text-muted); }
.loading-line::after {
  content: "";
  display: inline-block;
  width: 1em;
  text-align: left;
  animation: dots 1s steps(4, end) infinite;
}
.error-line { color: var(--danger); }
body.is-busy #refreshBtn { animation: spin 0.8s linear infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75%, 100% { content: "..."; } }
@keyframes spin { from { transform: rotate(0); } to { transform: rotate(360deg); } }

.auth-copy {
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.35;
  margin-bottom: 12px;
}
.link-auth-box {
  margin-top: 12px;
  padding: 12px;
  border-radius: var(--radius-lg);
  background: var(--surface-soft);
  border: 1px solid var(--border-soft);
}
.link-auth-label {
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 8px;
}
.link-auth-code-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
}
.link-auth-code {
  display: block;
  min-height: 38px;
  padding: 9px 10px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  color: var(--text);
  font-size: 20px;
  font-weight: 800;
  letter-spacing: .12em;
  text-align: center;
  user-select: all;
}
.link-auth-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}
.btn.disabled,
.btn:disabled {
  opacity: .55;
  pointer-events: none;
}

  /* === CHAT LAYOUT & NAVIGATION === */
  #view-chat { display: flex; flex-direction: column; height: 100%; overflow: hidden; background: var(--bg, #f5f5f7); }
  .chat-layout { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow: hidden; }

  /* Header */
  .chat-header { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--surface, #fff); border-bottom: 1px solid var(--border, #e0e0e0); min-height: 52px; box-sizing: border-box; }
  .chat-title { font-weight: 600; font-size: 17px; color: var(--text, #000); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }

  /* Controls */
  .chat-controls { display: flex; gap: 8px; padding: 6px 16px; background: var(--surface, #fff); border-bottom: 1px solid var(--border, #e0e0e0); box-sizing: border-box; }

  /* Dialogs List */
  .dialogs-list { flex: 0 0 auto; max-height: 38vh; overflow-y: auto; background: var(--surface, #fff); -webkit-overflow-scrolling: touch; }
  .dialog-item { display: flex; align-items: center; gap: 12px; padding: 12px 16px; width: 100%; border: none; background: transparent; cursor: pointer; text-align: left; transition: background 0.12s ease; box-sizing: border-box; }
  .dialog-item:hover { background: var(--hover-bg, #f0f0f5); }
  .dialog-item.active { background: var(--active-bg, #e3f2fd); }
  .dialog-avatar { width: 46px; height: 46px; border-radius: 50%; background: var(--primary, #3390ec); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 600; font-size: 17px; flex-shrink: 0; }
  .dialog-info { flex: 1; min-width: 0; }
  .dialog-name { font-weight: 600; font-size: 15px; color: var(--text, #000); margin-bottom: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .dialog-preview { font-size: 13px; color: var(--text-muted, #888); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .dialog-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; min-width: 36px; }
  .dialog-time { font-size: 11px; color: var(--text-muted, #888); }
  .dialog-badge { background: var(--primary, #3390ec); color: #fff; font-size: 11px; font-weight: 700; padding: 2px 5px; border-radius: 9px; min-width: 16px; text-align: center; display: none; }
  .dialog-badge.show { display: block; }

  /* Messages */
  .messages-container { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 8px; min-height: 0; -webkit-overflow-scrolling: touch; }
  .message { max-width: 82%; padding: 10px 14px; border-radius: 18px; font-size: 15px; line-height: 1.45; word-break: break-word; position: relative; animation: fadeIn 0.2s ease; }
  @keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
  .message.received { align-self: flex-start; background: var(--surface, #fff); color: var(--text, #000); border-bottom-left-radius: 4px; box-shadow: 0 1px 2px rgba(0,0,0,0.06); }
  .message.sent { align-self: flex-end; background: var(--primary, #3390ec); color: var(--text-inverse, #fff); border-bottom-right-radius: 4px; }
  .message.system { align-self: center; background: var(--hover-bg, #f0f0f5); color: var(--text-muted, #888); font-size: 12px; padding: 5px 10px; border-radius: 12px; }
  .message-time { font-size: 11px; opacity: 0.75; margin-top: 4px; text-align: right; }
  .message.sent .message-time { color: rgba(255,255,255,0.75); }

  /* Input Area */
  .input-area { display: flex; align-items: flex-end; gap: 10px; padding: 10px 16px; padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); background: var(--surface, #fff); border-top: 1px solid var(--border, #e0e0e0); box-sizing: border-box; }
  .input-container { flex: 1; min-width: 0; }
  #messageInput { width: 100%; padding: 12px 16px; border-radius: 22px; border: 1px solid var(--border, #ccc); background: var(--bg, #f5f5f7); color: var(--text, #000); font-size: 16px; line-height: 1.4; outline: none; box-sizing: border-box; resize: none; min-height: 44px; max-height: 110px; transition: border-color 0.15s; }
  #messageInput:focus { border-color: var(--primary, #3390ec); background: var(--surface, #fff); }
  .send-button { width: 44px; height: 44px; border-radius: 50%; border: none; background: var(--primary, #3390ec); color: #fff; font-size: 20px; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); flex-shrink: 0; opacity: 0.45; pointer-events: none; transform: scale(0.95); }
  .send-button.enabled { opacity: 1; pointer-events: auto; transform: scale(1); }
  .send-button:active { transform: scale(0.92); }

  /* Utilities */
  .empty { padding: 28px 16px; text-align: center; color: var(--text-muted, #888); font-size: 14px; line-height: 1.5; }
  .loading-line::after { content: "..."; animation: dots 1.2s steps(4, end) infinite; }
  @keyframes dots { 0%, 100% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }

