/* ============================================================
   ANLGRAM Intelligence — Layout
   ============================================================ */

/* ── App Shell ────────────────────────────────────────────── */
.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
  background: var(--bg-primary);
}

/* ── Sidebar ──────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100vh;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 100;
  overflow: hidden;
}

.sidebar::after {
  content: '';
  position: absolute;
  top: 0; right: 0;
  width: 1px; height: 100%;
  background: linear-gradient(180deg, transparent 0%, var(--ton-blue) 40%, var(--cyan) 60%, transparent 100%);
  opacity: 0.15;
}

/* Sidebar Logo */
.sidebar-logo {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  height: var(--topbar-height);
  text-decoration: none;
}

.sidebar-logo-icon {
  width: 30px; height: 30px;
  background: transparent;
  border-radius: var(--radius-md);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  box-shadow: none;
}
.sidebar-logo-icon img, .nav-logo-icon img {
  width: 100%; height: 100%; object-fit: contain;
  filter: drop-shadow(0 0 8px rgba(0,207,255,0.4));
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), filter 0.3s ease;
}
.sidebar-logo:hover .sidebar-logo-icon img, .nav-logo:hover .nav-logo-icon img {
  transform: scale(1.15) rotate(6deg);
  filter: drop-shadow(0 0 14px rgba(0,207,255,0.8));
}

.sidebar-logo-text {
  font-size: var(--text-base);
  font-weight: 700;
  letter-spacing: -0.01em;
  background: var(--grad-primary);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* Sidebar Nav */
.sidebar-nav {
  flex: 1;
  padding: var(--space-3) var(--space-2);
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
}

.sidebar-section-label {
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: var(--space-3) var(--space-3) var(--space-1);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 9px var(--space-3);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: var(--text-sm);
  font-weight: 500;
  transition: var(--transition-fast);
  position: relative;
  cursor: pointer;
  text-decoration: none;
}

.nav-item:hover {
  background: var(--bg-card);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--ton-blue-dim);
  color: var(--cyan);
  border: 1px solid var(--border-accent);
}

.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 60%;
  background: var(--grad-primary);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.nav-item svg {
  width: 16px; height: 16px;
  flex-shrink: 0;
  opacity: 0.7;
}

.nav-item.active svg, .nav-item:hover svg {
  opacity: 1;
}

.nav-badge {
  margin-left: auto;
  background: var(--red);
  color: white;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: var(--radius-full);
  line-height: 1.4;
}

/* Sidebar Footer */
.sidebar-footer {
  padding: var(--space-3) var(--space-2);
  border-top: 1px solid var(--border-subtle);
}

.sidebar-network-badge {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--green-dim);
  border: 1px solid rgba(34,197,94,0.15);
  border-radius: var(--radius-md);
  font-size: var(--text-xs);
  color: var(--green);
  font-weight: 500;
}

/* ── Main Content Area ────────────────────────────────────── */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ── Top Bar ──────────────────────────────────────────────── */
.topbar {
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  padding: 0 var(--space-6);
  border-bottom: 1px solid var(--border-subtle);
  background: var(--bg-secondary);
  gap: var(--space-4);
  flex-shrink: 0;
  position: relative;
  z-index: 50;
}

.topbar-search {
  flex: 1;
  max-width: 480px;
  position: relative;
}

.topbar-search input {
  width: 100%;
  height: 34px;
  background: var(--bg-input);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  padding: 0 var(--space-3) 0 36px;
  color: var(--text-primary);
  font-size: var(--text-sm);
  transition: var(--transition-fast);
}

.topbar-search input::placeholder { color: var(--text-muted); }

.topbar-search input:focus {
  outline: none;
  border-color: var(--ton-blue);
  box-shadow: 0 0 0 3px var(--ton-blue-dim);
}

.topbar-search-icon {
  position: absolute;
  left: 10px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  width: 15px; height: 15px;
  pointer-events: none;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-left: auto;
}

.topbar-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px; height: 34px;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  transition: var(--transition-fast);
}

.topbar-btn:hover {
  color: var(--text-primary);
  border-color: var(--border-default);
}

.topbar-price {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 0 var(--space-3);
  height: 34px;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
}

.topbar-price-label {
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-family: var(--font-mono);
}

.topbar-price-value {
  color: var(--text-primary);
  font-weight: 600;
  font-family: var(--font-mono);
}

/* ── Page Content ─────────────────────────────────────────── */
.page-content {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-6);
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: var(--space-6);
  gap: var(--space-4);
}

.page-title {
  font-size: var(--text-xl);
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.02em;
}

.page-subtitle {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  margin-top: var(--space-1);
}

/* ── Cards ────────────────────────────────────────────────── */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  transition: var(--transition-fast);
  overflow: hidden;
}

.card:hover {
  border-color: var(--border-default);
}

.card.interactive:hover {
  border-color: var(--border-accent);
  box-shadow: var(--shadow-card), 0 0 0 1px var(--ton-blue-dim) inset;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-subtle);
}

.card-title {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.card-body {
  padding: var(--space-5);
}

/* ── Metric Cards ─────────────────────────────────────────── */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--space-4);
}

.metric-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  transition: var(--transition-fast);
  position: relative;
  overflow: hidden;
}

.metric-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: var(--grad-primary);
  opacity: 0;
  transition: var(--transition-fast);
}

.metric-card:hover {
  border-color: var(--border-accent);
  transform: translateY(-1px);
}

.metric-card:hover::before { opacity: 1; }

.metric-label {
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-bottom: var(--space-2);
}

.metric-value {
  font-size: var(--text-2xl);
  font-weight: 700;
  font-family: var(--font-mono);
  color: var(--text-primary);
  letter-spacing: -0.02em;
  line-height: 1;
  margin-bottom: var(--space-2);
}

.metric-change {
  font-size: var(--text-xs);
  font-family: var(--font-mono);
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 3px;
}

/* ── Data Tables ──────────────────────────────────────────── */
.data-table-wrap {
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.data-table thead th {
  padding: var(--space-2) var(--space-4);
  text-align: left;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  border-bottom: 1px solid var(--border-subtle);
  white-space: nowrap;
}

.data-table tbody tr {
  border-bottom: 1px solid var(--border-subtle);
  transition: var(--transition-fast);
  cursor: pointer;
}

.data-table tbody tr:last-child { border-bottom: none; }

.data-table tbody tr:hover { background: rgba(255,255,255,0.02); }

.data-table tbody td {
  padding: var(--space-3) var(--space-4);
  color: var(--text-primary);
  white-space: nowrap;
}

.td-mono {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  color: var(--text-secondary);
}

.td-entity {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.entity-avatar {
  width: 24px; height: 24px;
  border-radius: var(--radius-sm);
  background: var(--ton-blue-dim);
  border: 1px solid var(--border-accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 700;
  color: var(--cyan);
  flex-shrink: 0;
}

/* ── Buttons ──────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 0 var(--space-4);
  height: 36px;
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 600;
  font-family: var(--font-sans);
  cursor: pointer;
  transition: var(--transition-fast);
  white-space: nowrap;
  border: 1px solid transparent;
}

.btn-primary {
  background: var(--ton-blue);
  color: white;
  border-color: var(--ton-blue);
}
.btn-primary:hover {
  background: #0099e6;
  box-shadow: var(--shadow-glow-blue);
}

.btn-cyan {
  background: var(--cyan);
  color: var(--bg-void);
  border-color: var(--cyan);
}
.btn-cyan:hover {
  background: #00e0ff;
  box-shadow: var(--shadow-glow-cyan);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border-color: var(--border-default);
}
.btn-ghost:hover {
  color: var(--text-primary);
  border-color: var(--border-strong);
  background: var(--bg-card);
}

.btn-sm {
  height: 28px;
  padding: 0 var(--space-3);
  font-size: var(--text-xs);
}

.btn-lg {
  height: 44px;
  padding: 0 var(--space-6);
  font-size: var(--text-md);
}

/* ── Grids ────────────────────────────────────────────────── */
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--space-4); }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--space-4); }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--space-4); }

/* ── Responsive ───────────────────────────────────────────── */
.mobile-bottom-nav {
  display: none;
}

@media (max-width: 1024px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  :root { --sidebar-width: 0px; }
  .sidebar { display: none !important; }
  
  .app-shell {
    height: auto !important;
    min-height: 100vh;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    display: block !important;
  }
  
  .main-content {
    width: 100% !important;
    max-width: 100vw !important;
    overflow-x: hidden !important;
    padding-bottom: 84px !important;
  }
  
  /* Topbar Responsive */
  .topbar {
    padding: 10px 14px;
    height: auto;
    min-height: 56px;
    flex-wrap: wrap;
    gap: 10px;
  }
  .topbar-search {
    width: 100% !important;
    max-width: 100% !important;
    order: 3;
    margin: 4px 0 0 0 !important;
  }
  .topbar-actions {
    margin-left: auto;
  }
  .topbar-btn {
    padding: 6px 12px !important;
    font-size: 12px !important;
    white-space: nowrap;
  }

  /* Grids & Metrics */
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr !important; gap: 14px; }
  .metric-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px; }
  .page-content { padding: 16px !important; }
  
  /* Tables & Horizontal Scroll */
  .table-container, table {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  /* Modals */
  .modal, .modal-glass {
    width: 92% !important;
    max-width: 92% !important;
    max-height: 85vh !important;
    overflow-y: auto !important;
    padding: 20px !important;
    margin: 10px auto !important;
  }

  /* Bottom Navigation Bar on Mobile */
  .mobile-bottom-nav {
    display: flex !important;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: 68px;
    background: rgba(13, 13, 18, 0.94);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 1px solid rgba(0, 240, 255, 0.25);
    box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.8), 0 0 20px rgba(0, 240, 255, 0.1);
    z-index: 99999;
    justify-content: space-around;
    align-items: center;
    padding: 0 4px;
  }
  .mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    color: #64748b;
    text-decoration: none !important;
    font-size: 10px;
    font-weight: 600;
    font-family: var(--font-mono, sans-serif);
    padding: 6px 8px;
    border-radius: 12px;
    transition: all 0.2s ease;
    flex: 1;
    text-align: center;
  }
  .mobile-nav-item svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
    transition: all 0.2s ease;
  }
  .mobile-nav-item.active {
    color: var(--neon-cyan, #00f0ff);
    background: rgba(0, 240, 255, 0.1);
    box-shadow: 0 0 15px rgba(0, 240, 255, 0.15);
  }
  .mobile-nav-item.active svg {
    stroke: var(--neon-cyan, #00f0ff);
    transform: scale(1.1);
  }
}

@media (max-width: 480px) {
  .metric-grid { grid-template-columns: 1fr !important; }
  .topbar-price { display: none !important; }
  .mobile-nav-item { font-size: 9px; padding: 4px 2px; }
  .mobile-nav-item svg { width: 18px; height: 18px; }
}

/* ── Automatic Device Recognition & Adaptation Rules ──────────────────────── */
html.device-pc body {
  /* Enforce clean desktop proportions and prevent oversized text */
  --content-scale: 1;
}
html.device-phone body {
  /* Optimize touch target spacing and prevent bottom navigation clipping */
  --content-scale: 0.95;
  padding-bottom: 84px !important;
}
html.device-phone .sidebar {
  display: none !important;
}
html.device-phone .topbar {
  left: 0 !important;
}
html.device-phone .main-content {
  margin-left: 0 !important;
  width: 100% !important;
}
/* Ensure device pill is responsive on very small screens */
@media (max-width: 420px) {
  #anlgramDevicePill { padding: 3px 6px !important; font-size: 9px !important; }
}

/* ── Telegram Mini App (TMA) Native Adaptations ───────────────────────────── */
body.tma-active {
  padding-top: env(safe-area-inset-top, 0);
  padding-bottom: calc(72px + env(safe-area-inset-bottom, 0)) !important;
  min-height: 100vh;
  min-height: var(--tg-viewport-height, 100vh);
  overscroll-behavior-y: none;
}

body.tma-active .app-header,
body.tma-active .topbar {
  padding-top: calc(env(safe-area-inset-top, 0) + 6px);
}

/* Unified TMA & Mobile Bottom Navigation Bar */
#anlgram-tma-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 64px;
  background: rgba(3, 7, 18, 0.96);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-top: 1px solid rgba(0, 240, 255, 0.22);
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.85), 0 0 16px rgba(0, 240, 255, 0.1);
  z-index: 999999;
  display: none;
  justify-content: space-around;
  align-items: center;
  padding: 0 4px calc(env(safe-area-inset-bottom, 0));
  box-sizing: border-box;
}

@media (max-width: 768px) {
  #anlgram-tma-nav {
    display: flex !important;
  }
}

body.tma-active #anlgram-tma-nav {
  display: flex !important;
}

.tma-nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: #64748b;
  text-decoration: none !important;
  font-size: 10px;
  font-weight: 600;
  font-family: var(--font-mono, 'JetBrains Mono', monospace);
  padding: 6px 10px;
  border-radius: 10px;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  flex: 1;
  text-align: center;
  background: transparent;
  border: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.tma-nav-btn svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  transition: transform 0.2s ease, stroke 0.2s ease;
}

.tma-nav-btn:hover {
  color: #94a3b8;
}

.tma-nav-btn.active {
  color: #00f0ff;
  background: rgba(0, 240, 255, 0.08);
  box-shadow: 0 0 12px rgba(0, 240, 255, 0.15);
}

.tma-nav-btn.active svg {
  stroke: #00f0ff;
  transform: translateY(-1px) scale(1.1);
}

.tma-user-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  background: rgba(0, 136, 204, 0.15);
  border: 1px solid rgba(0, 207, 255, 0.3);
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  color: #00f0ff;
  font-family: var(--font-mono, monospace);
}


