* {
    box-sizing: border-box;
}

:root {
    --bg-primary: #1a1a1a;
    --bg-secondary: #2d2d2d;
    --bg-tertiary: #3a3a3a;
    --text-primary: #ffffff;
    --text-secondary: #b3b3b3;
    --text-muted: #808080;
    --border-color: #404040;
    --accent-primary: #667eea;
    --accent-secondary: #764ba2;
    --success: #10b981;
    --error: #ef4444;
    --warning: #f59e0b;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    margin: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    color: var(--text-primary);
    background: var(--bg-primary);
    overflow: hidden;
}

.stats-bar {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 12px 0;
    box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}

.stats-container {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 40px;
    padding: 0 20px;
}

.dashboard-link {
    color: white;
    text-decoration: none;
    font-weight: 700;
    background: rgba(255, 255, 255, 0.18);
    border: 1px solid rgba(255, 255, 255, 0.25);
    border-radius: 8px;
    padding: 12px 16px;
}

.stat-item {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, 0.15);
    padding: 8px 16px;
    border-radius: 8px;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    transition: all 0.3s ease;
}

.stat-item:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: translateY(-2px);
}

.stat-icon {
    font-size: 24px;
    opacity: 0.9;
}

.stat-content {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-number {
    font-size: 20px;
    font-weight: 700;
    color: white;
    line-height: 1;
}

.stat-label {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.8);
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.tg-layout {
    display: grid;
    grid-template-columns: 350px 380px 1fr;
    width: 100%;
    flex: 1;
    background: var(--bg-secondary);
    box-shadow: 0 0 20px rgba(0,0,0,0.3);
    min-height: 0; /* ensure grid children can shrink for inner scroll */
}

.tg-layout.analytics-view {
    grid-template-columns: 350px minmax(0, 1fr);
}

.tg-layout.analytics-view .chats-pane {
    display: none;
}

.tg-layout.analytics-view .messages-pane {
    border-right: 0;
}

.tg-pane {
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
    overflow: hidden;
    background: var(--bg-secondary);
    min-height: 0; /* allow inner scroll areas */
}

.pane-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    background: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
    min-height: 48px;
    color: var(--text-primary);
    flex-shrink: 0;
}

.pane-title {
    font-weight: 600;
    font-size: 14px;
    color: var(--text-primary);
    text-shadow: 0 1px 2px rgba(0,0,0,0.3);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    margin-right: 8px;
}

.pane-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.search {
    width: 100%;
    max-width: 250px;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 14px;
    background: var(--bg-tertiary);
    transition: all 0.2s ease;
    color: var(--text-primary);
}

.search:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
    background: var(--bg-primary);
}

.pane-content {
    flex: 1;
    overflow: hidden;
    position: relative;
    display: flex;
    flex-direction: column;
    min-height: 0; /* critical for nested scroll */
}

.list {
    list-style: none;
    padding: 0;
    margin: 0;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    flex: 1;            /* take remaining height within pane-content */
    min-height: 0;      /* allow scroll within flex container */
}

.list-item {
    padding: 12px 20px;
    cursor: pointer;
    border-bottom: 1px solid var(--border-color);
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-secondary);
}

.list-item:hover { 
    background: var(--bg-tertiary); 
    transform: translateX(2px);
    border-left: 3px solid var(--accent-primary);
}

.list-item.selected { 
    background: var(--bg-tertiary); 
    border-left: 3px solid var(--accent-primary);
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.list-item-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-primary);
    font-weight: 600;
    font-size: 16px;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
    transition: all 0.2s ease;
}

.list-item:hover .list-item-avatar {
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.list-item-content {
    flex: 1;
    min-width: 0;
}

.list-item-title {
    font-weight: 600;
    color: var(--text-primary);
    margin: 0 0 4px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.list-item-subtitle {
    font-size: 13px;
    color: var(--text-secondary);
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.empty-state, .loading-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 200px;
    color: var(--text-muted);
}

.empty-icon {
    font-size: 48px;
    margin-bottom: 12px;
    opacity: 0.5;
}

.empty-text, .loading-text {
    font-size: 14px;
    margin: 0;
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #3b82f6;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 12px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.btn-icon {
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.3);
    padding: 8px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 16px;
    transition: all 0.2s ease;
    color: white;
}

.btn-icon:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.1);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: var(--bg-primary);
    max-height: calc(100vh - 120px);
    scroll-behavior: smooth;
}

.msg-row {
    display: flex;
    margin: 12px 0;
    animation: fadeIn 0.3s ease;
}

.msg-row.from-user { justify-content: flex-end; }
.msg-row.from-assistant { justify-content: flex-start; }

.msg-bubble {
    max-width: 70%;
    padding: 12px 16px;
    border-radius: 18px;
    background: var(--bg-tertiary);
    box-shadow: 0 1px 2px rgba(0,0,0,0.3);
    position: relative;
    word-wrap: break-word;
    color: var(--text-primary);
}

.from-user .msg-bubble {
    background: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
    color: var(--text-primary);
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.from-assistant .msg-bubble {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.msg-time {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
    text-align: right;
}

.msg-attachments {
    margin-top: 10px;
    padding: 8px 10px;
    border-radius: 10px;
    background: rgba(0, 0, 0, 0.15);
    border: 1px solid var(--border-color);
}

.from-user .msg-attachments {
    background: rgba(255, 255, 255, 0.2);
    border-color: rgba(255, 255, 255, 0.35);
}

.msg-attachments-title {
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 6px;
    opacity: 0.9;
}

.msg-attachments-list {
    margin: 0;
    padding-left: 18px;
}

.msg-attachments-item {
    font-size: 12px;
    line-height: 1.4;
    word-break: break-all;
}

.from-user .msg-time {
    color: rgba(255,255,255,0.7);
}

.msg-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-top: 6px;
}

.msg-meta-left {
    flex: 0 0 auto;
}

.msg-meta-center {
    flex: 1;
    display: flex;
    justify-content: center;
}

.msg-meta-right {
    flex: 0 0 auto;
}

.reaction-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    font-size: 12px;
    border-radius: 999px;
    background: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.from-user .reaction-badge {
    background: rgba(255,255,255,0.25);
    color: var(--text-primary);
    border-color: rgba(255,255,255,0.35);
}

.reaction-like { background: rgba(16,185,129,0.2); color: var(--success); border-color: rgba(16,185,129,0.4); }
.reaction-dislike { background: rgba(239,68,68,0.2); color: var(--error); border-color: rgba(239,68,68,0.4); }
.from-user .reaction-like { background: rgba(255,255,255,0.28); color: var(--text-primary); }
.from-user .reaction-dislike { background: rgba(255,255,255,0.28); color: var(--text-primary); }

.comment-toggle {
    background: rgba(102, 126, 234, 0.15);
    border: 1px solid rgba(102, 126, 234, 0.3);
    color: var(--accent-primary);
    cursor: pointer;
    font-size: 12px;
    padding: 4px 6px;
    border-radius: 6px;
}

.comment-toggle:hover {
    background: rgba(102, 126, 234, 0.25);
}

.from-user .comment-toggle {
    background: rgba(255,255,255,0.22);
    border-color: rgba(255,255,255,0.35);
    color: var(--text-primary);
}

.msg-comment {
    margin-top: 8px;
    padding: 10px 12px;
    border-left: 3px solid var(--accent-primary);
    background: rgba(102, 126, 234, 0.1);
    border-radius: 8px;
    font-size: 13px;
    color: var(--text-primary);
    display: none;
}

.from-user .msg-comment {
    background: rgba(255,255,255,0.15);
    border-left-color: var(--text-primary);
    color: var(--text-primary);
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.login-container {
    width: 400px;
    margin: 100px auto;
    padding: 40px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}

.login-container h2 {
    text-align: center;
    margin-bottom: 30px;
    color: #2c3e50;
}

.login-container input {
    width: 100%;
    padding: 12px 16px;
    margin: 8px 0;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 16px;
    transition: all 0.2s ease;
}

.login-container input:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.login-container button {
    width: 100%;
    padding: 12px;
    background: #3b82f6;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    margin-top: 16px;
}

.login-container button:hover {
    background: #2563eb;
    transform: translateY(-1px);
}

.error-message {
    background: #fee;
    color: #c53030;
    padding: 12px 16px;
    border-radius: 8px;
    border: 1px solid #feb2b2;
    margin-bottom: 16px;
    font-size: 14px;
    text-align: center;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.logout-btn {
    position: fixed;
    right: 20px;
    top: 20px;
    background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%);
    color: white;
    border: none;
    border-radius: 12px;
    padding: 12px 20px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
    transition: all 0.3s ease;
    z-index: 1000;
    display: flex;
    align-items: center;
    gap: 8px;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.logout-btn:hover {
    background: linear-gradient(135deg, #ff5252 0%, #e53935 100%);
    transform: translateY(-3px);
    box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
}

.logout-icon {
    font-size: 16px;
    opacity: 0.9;
}

.logout-text {
    font-size: 14px;
    font-weight: 600;
}

.view-toggle {
    display: inline-flex;
    align-items: center;
    padding: 3px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.28);
}

.view-toggle[hidden] {
    display: none;
}

.view-toggle-btn {
    border: 0;
    border-radius: 6px;
    padding: 6px 10px;
    background: transparent;
    color: rgba(255, 255, 255, 0.82);
    cursor: pointer;
    font-weight: 600;
    font-size: 13px;
}

.view-toggle-btn.active {
    background: #fff;
    color: #334155;
}

.user-analytics {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 18px;
    background: #f3f6fb;
}

.user-analytics[hidden] {
    display: none;
}

.user-analytics-status {
    color: #64748b;
    font-size: 14px;
    margin-bottom: 12px;
}

.user-analytics-content {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.user-analytics-section {
    background: #fff;
    border: 1px solid #dce3ee;
    border-radius: 8px;
    padding: 14px;
    box-shadow: 0 1px 2px rgba(31, 42, 68, 0.06);
}

.user-analytics-section h2,
.user-analytics-section h3 {
    margin: 0;
    color: #172033;
}

.user-analytics-section h2 {
    font-size: 22px;
    line-height: 1.2;
}

.user-analytics-section h3 {
    font-size: 16px;
    line-height: 1.25;
}

.user-profile-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 20px;
}

.user-profile-head p {
    margin: 6px 0 0;
    color: #64748b;
    font-size: 13px;
}

.user-profile-dates {
    display: flex;
    flex-direction: column;
    gap: 5px;
    min-width: 210px;
    color: #475569;
    font-size: 13px;
    text-align: right;
}

.user-kpi-grid {
    display: grid;
    grid-template-columns: repeat(6, minmax(120px, 1fr));
    gap: 10px;
}

.user-kpi {
    background: #fff;
    border: 1px solid #dce3ee;
    border-radius: 8px;
    padding: 12px;
}

.user-kpi-label {
    min-height: 34px;
    color: #64748b;
    font-size: 12px;
    line-height: 1.25;
}

.user-kpi-value {
    margin-top: 8px;
    color: #172033;
    font-size: 24px;
    line-height: 1;
    font-weight: 750;
}

.user-section-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
}

.user-section-head span {
    color: #64748b;
    font-size: 12px;
}

.user-activity-note {
    margin: -2px 0 10px;
    color: #64748b;
    font-size: 13px;
    line-height: 1.35;
}

.user-activity-wrap {
    display: flex;
    gap: 10px;
    min-width: 0;
}

.user-activity-y {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 34px;
    height: 110px;
    padding: 2px 0 18px;
    color: #64748b;
    font-size: 11px;
    text-align: right;
}

.user-activity-main {
    flex: 1;
    min-width: 0;
}

.user-activity-chart {
    display: flex;
    align-items: flex-end;
    gap: 3px;
    height: 110px;
    overflow-x: auto;
    padding: 4px 0 2px;
    border-bottom: 1px solid #dce3ee;
}

.user-activity-bar {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    flex: 1 1 0;
    min-width: 6px;
    height: 100px;
}

.user-activity-bar span {
    display: block;
    width: min(100%, 10px);
    border-radius: 4px 4px 0 0;
    background: #667eea;
}

.user-activity-bar.empty span {
    background: #dbe4f0;
}

.user-activity-axis {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding-top: 6px;
    color: #64748b;
    font-size: 12px;
}

.user-analytics-columns {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 14px;
}

.user-analytics-columns.two {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.user-dist-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 12px;
}

.user-dist-meta {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    color: #172033;
    font-size: 13px;
}

.user-dist-meta span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-dist-meta b {
    font-size: 13px;
}

.user-dist-track {
    height: 8px;
    margin-top: 5px;
    border-radius: 999px;
    background: #e8eef6;
    overflow: hidden;
}

.user-dist-track span {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: #667eea;
}

.user-dist-sub {
    margin-top: 3px;
    color: #64748b;
    font-size: 12px;
}

.user-facts {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin-top: 12px;
}

.user-facts.wide {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.user-facts div {
    padding: 10px;
    border-radius: 8px;
    background: #f6f8fb;
}

.user-facts span {
    display: block;
    color: #64748b;
    font-size: 12px;
}

.user-facts b {
    display: block;
    margin-top: 5px;
    color: #172033;
    font-size: 18px;
    overflow-wrap: anywhere;
}

.user-profile-text {
    margin: 12px 0 0;
    color: #475569;
    font-size: 13px;
    line-height: 1.45;
}

.user-profile-text b {
    color: #172033;
}

.user-retention-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
}

.user-retention-pill {
    display: inline-flex;
    align-items: center;
    min-height: 28px;
    padding: 5px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
}

.user-retention-pill.ok {
    background: #dcfce7;
    color: #166534;
}

.user-retention-pill.muted {
    background: #eef2f7;
    color: #64748b;
}

.user-retention-note {
    color: #64748b;
    font-size: 12px;
}

.user-mini-table {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 12px;
}

.user-mini-table-row {
    display: grid;
    grid-template-columns: 1.1fr 0.9fr 0.9fr 0.9fr;
    gap: 8px;
    padding: 8px;
    border-radius: 7px;
    background: #f6f8fb;
    color: #334155;
    font-size: 12px;
}

.user-mini-table-row.token-chat {
    grid-template-columns: minmax(0, 1.5fr) 0.8fr 0.8fr 0.8fr 0.8fr;
    text-decoration: none;
}

.user-mini-table-row.token-chat:hover {
    background: #eaf1fb;
}

.user-mini-table-row span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-payment-reason,
.user-empty-line {
    margin: 12px 0 0;
    color: #64748b;
    font-size: 13px;
}

.user-tool-row {
    min-width: 0;
}

.user-timeline-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 12px;
}

.user-timeline-item {
    display: grid;
    grid-template-columns: 12px minmax(0, 1fr);
    gap: 10px;
    padding: 10px 12px;
    border-radius: 8px;
    background: #f6f8fb;
    color: #172033;
    text-decoration: none;
}

.user-timeline-item:hover {
    background: #eaf1fb;
}

.user-timeline-dot {
    width: 8px;
    height: 8px;
    margin-top: 6px;
    border-radius: 999px;
    background: #667eea;
}

.user-timeline-body {
    min-width: 0;
}

.user-timeline-top {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    color: #64748b;
    font-size: 12px;
}

.user-timeline-top b {
    color: #172033;
    font-size: 13px;
}

.user-timeline-title {
    margin-top: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 13px;
    font-weight: 700;
}

.user-timeline-detail {
    margin-top: 4px;
    color: #475569;
    font-size: 12px;
    line-height: 1.35;
}

.user-timeline-status {
    display: inline-flex;
    margin-top: 7px;
    padding: 3px 7px;
    border-radius: 999px;
    background: #e8eef6;
    color: #475569;
    font-size: 11px;
    font-weight: 700;
}

.user-timeline-status.success {
    color: #087047;
    background: #dcf7ea;
}

.user-timeline-status.danger {
    color: #9a2828;
    background: #ffe4e4;
}

.user-timeline-status.warning {
    color: #8a5a00;
    background: #fff2c7;
}

.user-recent-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 12px;
}

.user-recent-message {
    display: block;
    padding: 10px 12px;
    border-radius: 8px;
    background: #f6f8fb;
    text-decoration: none;
    color: #172033;
}

.user-recent-message:hover {
    background: #eaf1fb;
}

.user-recent-message span {
    display: block;
    margin-bottom: 5px;
    color: #64748b;
    font-size: 12px;
}

.user-recent-message b {
    display: block;
    overflow: hidden;
    color: #172033;
    font-size: 13px;
    line-height: 1.35;
    font-weight: 500;
}

@media (max-width: 1300px) {
    .user-kpi-grid {
        grid-template-columns: repeat(3, minmax(120px, 1fr));
    }

    .user-analytics-columns,
    .user-analytics-columns.two {
        grid-template-columns: 1fr;
    }

    .user-facts.wide {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 720px) {
    .user-facts.wide {
        grid-template-columns: 1fr;
    }

    .user-profile-head {
        flex-direction: column;
    }

    .user-profile-dates {
        min-width: 0;
        text-align: left;
    }

    .user-mini-table-row,
    .user-mini-table-row.token-chat {
        grid-template-columns: 1fr;
    }
}
