/*
 * Telegram Mini App Styles
 * ChatGPT-inspired design with Telegram auto-theme
 */

/* ===== CSS Variables ===== */
:root {
    /* Telegram theme defaults (light) */
    --tg-theme-bg-color: #ffffff;
    --tg-theme-secondary-bg-color: #f7f7f8;
    --tg-theme-text-color: #000000;
    --tg-theme-hint-color: #999999;
    --tg-theme-link-color: #2481cc;
    --tg-theme-button-color: #3390ec;
    --tg-theme-button-text-color: #ffffff;
    --tg-theme-destructive-text-color: #ff3b30;

    /* UI tokens */
    --sidebar-width: 260px;
    --header-height: 56px;
    --border-radius: 12px;
    --border-radius-sm: 8px;
    --transition-fast: 0.15s ease;
    --transition: 0.25s ease;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);

    /* Derived colors */
    --border-color: rgba(0, 0, 0, 0.08);
    --hover-bg: rgba(0, 0, 0, 0.04);
    --active-bg: rgba(0, 0, 0, 0.08);

    /* Safe areas */
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
}

/* Dark theme overrides */
.dark {
    --border-color: rgba(255, 255, 255, 0.1);
    --hover-bg: rgba(255, 255, 255, 0.06);
    --active-bg: rgba(255, 255, 255, 0.1);
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--tg-theme-bg-color);
    color: var(--tg-theme-text-color);
    line-height: 1.5;
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
}

/* ===== App Layout ===== */
#app {
    display: flex;
    height: 100vh;
    height: 100dvh;
}

/* ===== Sidebar ===== */
.sidebar {
    width: var(--sidebar-width);
    background-color: var(--tg-theme-secondary-bg-color);
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
    position: relative;
    z-index: 100;
    transition: transform var(--transition);
}

.sidebar-header {
    height: var(--header-height);
    padding: 0 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.logo-container {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo-icon {
    width: 24px;
    height: 24px;
    color: var(--tg-theme-button-color);
}

.logo-text {
    font-weight: 600;
    font-size: 18px;
}

.sidebar-close {
    display: none;
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: var(--tg-theme-hint-color);
    cursor: pointer;
    border-radius: var(--border-radius-sm);
    padding: 4px;
}

.sidebar-close:hover {
    background: var(--hover-bg);
}

.sidebar-close svg {
    width: 100%;
    height: 100%;
}

.sidebar-nav {
    flex: 1;
    padding: 12px 8px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-radius: var(--border-radius-sm);
    color: var(--tg-theme-text-color);
    text-decoration: none;
    font-size: 15px;
    font-weight: 500;
    transition: background var(--transition-fast);
    margin-bottom: 4px;
}

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

.nav-item:hover {
    background: var(--hover-bg);
}

.nav-item.active {
    background: var(--tg-theme-button-color);
    color: var(--tg-theme-button-text-color);
}

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

.sidebar-footer {
    padding: 16px;
    border-top: 1px solid var(--border-color);
}

.bot-status {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: var(--tg-theme-hint-color);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--tg-theme-hint-color);
    transition: background var(--transition);
}

.bot-status.online .status-dot {
    background: #34c759;
}

.bot-status.offline .status-dot {
    background: #ff3b30;
}

/* Sidebar Overlay (mobile) */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
    opacity: 0;
    transition: opacity var(--transition);
}

.sidebar-overlay.visible {
    opacity: 1;
}

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

.mobile-header {
    display: none;
    height: var(--header-height);
    padding: 0 16px;
    align-items: center;
    background: var(--tg-theme-bg-color);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.menu-toggle {
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    color: var(--tg-theme-text-color);
    cursor: pointer;
    border-radius: var(--border-radius-sm);
    padding: 8px;
    margin-left: -8px;
}

.menu-toggle:hover {
    background: var(--hover-bg);
}

.menu-toggle svg {
    width: 100%;
    height: 100%;
}

.mobile-title {
    flex: 1;
    text-align: center;
    font-size: 17px;
    font-weight: 600;
}

.header-spacer {
    width: 40px;
}

/* ===== Sections ===== */
.section {
    display: none;
    flex: 1;
    overflow: hidden;
}

.section.active {
    display: flex;
}

.section-inner {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}

.section-header h1 {
    font-size: 28px;
    font-weight: 700;
    letter-spacing: -0.5px;
}

/* ===== Stats Grid ===== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--tg-theme-secondary-bg-color);
    border-radius: var(--border-radius);
    padding: 20px;
    text-align: center;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.stat-value {
    display: block;
    font-size: 32px;
    font-weight: 700;
    color: var(--tg-theme-button-color);
    line-height: 1.2;
}

.stat-label {
    display: block;
    font-size: 13px;
    color: var(--tg-theme-hint-color);
    margin-top: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* ===== Cards ===== */
.card {
    background: var(--tg-theme-secondary-bg-color);
    border-radius: var(--border-radius);
    padding: 20px;
    margin-bottom: 16px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}

.card-header h2 {
    font-size: 17px;
    font-weight: 600;
}

/* ===== Chart ===== */
.chart-card {
    margin-bottom: 24px;
}

.chart-container {
    position: relative;
    height: 250px;
}

.period-select {
    padding: 6px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    background: var(--tg-theme-bg-color);
    color: var(--tg-theme-text-color);
    font-size: 14px;
    cursor: pointer;
}

/* ===== Toggle Button ===== */
.toggle-btn {
    width: 52px;
    height: 32px;
    border-radius: 16px;
    border: none;
    background: var(--tg-theme-hint-color);
    cursor: pointer;
    position: relative;
    transition: background var(--transition-fast);
    flex-shrink: 0;
}

.toggle-btn.active {
    background: #34c759;
}

.toggle-slider {
    position: absolute;
    width: 28px;
    height: 28px;
    background: white;
    border-radius: 50%;
    top: 2px;
    left: 2px;
    transition: transform var(--transition-fast);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.toggle-btn.active .toggle-slider {
    transform: translateX(20px);
}

/* ===== Forms ===== */
.form-group {
    margin-bottom: 20px;
}

.form-group:last-child {
    margin-bottom: 0;
}

.form-group label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 8px;
    color: var(--tg-theme-text-color);
}

.form-group input,
.form-group textarea,
.form-group select {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    background: var(--tg-theme-bg-color);
    color: var(--tg-theme-text-color);
    font-size: 16px;
    font-family: inherit;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
    -webkit-appearance: none;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--tg-theme-button-color);
    box-shadow: 0 0 0 3px rgba(36, 129, 204, 0.15);
}

.form-group input::placeholder,
.form-group textarea::placeholder {
    color: var(--tg-theme-hint-color);
}

.form-group textarea {
    resize: vertical;
    min-height: 100px;
    line-height: 1.5;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.hint {
    display: block;
    font-size: 13px;
    color: var(--tg-theme-hint-color);
    margin-top: 6px;
}

/* Checkbox */
.checkbox-item {
    display: flex;
    align-items: center;
    padding: 12px 0;
    cursor: pointer;
    border-bottom: 1px solid var(--border-color);
}

.checkbox-item:last-child {
    border-bottom: none;
}

.checkbox-item input[type="checkbox"] {
    width: 22px;
    height: 22px;
    margin-right: 12px;
    cursor: pointer;
    accent-color: var(--tg-theme-button-color);
}

.checkbox-label {
    font-size: 16px;
}

/* ===== Buttons ===== */
.btn-primary {
    width: 100%;
    padding: 14px 20px;
    border: none;
    border-radius: var(--border-radius-sm);
    background: var(--tg-theme-button-color);
    color: var(--tg-theme-button-text-color);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity var(--transition-fast), transform var(--transition-fast);
    margin-top: 8px;
}

.btn-primary:hover {
    opacity: 0.9;
}

.btn-primary:active {
    transform: scale(0.98);
}

.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-secondary {
    padding: 10px 20px;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    background: transparent;
    color: var(--tg-theme-text-color);
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: background var(--transition-fast);
}

.btn-secondary:hover {
    background: var(--hover-bg);
}

.btn-icon {
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    color: var(--tg-theme-hint-color);
    cursor: pointer;
    border-radius: var(--border-radius-sm);
    padding: 6px;
    transition: background var(--transition-fast), color var(--transition-fast);
}

.btn-icon:hover {
    background: var(--hover-bg);
    color: var(--tg-theme-text-color);
}

.btn-icon svg {
    width: 100%;
    height: 100%;
}

/* ===== Test Result ===== */
.test-result {
    margin-top: 12px;
    padding: 12px;
    border-radius: var(--border-radius-sm);
    font-size: 14px;
    display: none;
}

.test-result.success {
    display: block;
    background: rgba(52, 199, 89, 0.1);
    color: #34c759;
    border: 1px solid rgba(52, 199, 89, 0.3);
}

.test-result.error {
    display: block;
    background: rgba(255, 59, 48, 0.1);
    color: #ff3b30;
    border: 1px solid rgba(255, 59, 48, 0.3);
}

/* ===== Lists ===== */
.list-container {
    max-height: 300px;
    overflow-y: auto;
}

.list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: var(--tg-theme-bg-color);
    border-radius: var(--border-radius-sm);
    margin-bottom: 8px;
    font-size: 14px;
}

.list-item:last-child {
    margin-bottom: 0;
}

.list-item-label {
    color: var(--tg-theme-hint-color);
}

.list-item-value {
    font-weight: 600;
}

.empty-state {
    text-align: center;
    padding: 24px;
    color: var(--tg-theme-hint-color);
    font-size: 14px;
}

/* ===== Logs ===== */
.log-controls {
    display: flex;
    align-items: center;
    gap: 12px;
}

.ws-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--tg-theme-hint-color);
}

.ws-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--tg-theme-hint-color);
    transition: background var(--transition);
}

.ws-status.connected .ws-dot {
    background: #34c759;
}

.logs-container {
    background: var(--tg-theme-secondary-bg-color);
    border-radius: var(--border-radius);
    padding: 16px;
    height: calc(100vh - 200px);
    overflow-y: auto;
    font-family: 'SF Mono', 'Menlo', 'Monaco', 'Consolas', monospace;
    font-size: 13px;
}

.log-entry {
    padding: 10px 12px;
    margin-bottom: 6px;
    border-radius: var(--border-radius-sm);
    background: var(--tg-theme-bg-color);
    display: flex;
    gap: 12px;
    align-items: flex-start;
    border-left: 3px solid var(--tg-theme-hint-color);
}

.log-entry.reply {
    border-left-color: #34c759;
}

.log-entry.error {
    border-left-color: #ff3b30;
}

.log-entry.config {
    border-left-color: #ff9500;
}

.log-entry.system {
    border-left-color: var(--tg-theme-button-color);
}

.log-time {
    color: var(--tg-theme-hint-color);
    white-space: nowrap;
    font-size: 12px;
}

.log-message {
    flex: 1;
    word-break: break-word;
}

/* ===== Loading & Error ===== */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: var(--tg-theme-bg-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    transition: opacity var(--transition);
}

.loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-color);
    border-top-color: var(--tg-theme-button-color);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-overlay p {
    margin-top: 16px;
    color: var(--tg-theme-hint-color);
    font-size: 15px;
}

.error-screen {
    position: fixed;
    inset: 0;
    background: var(--tg-theme-bg-color);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.error-screen.hidden {
    display: none;
}

.error-content {
    text-align: center;
    padding: 24px;
}

.error-icon {
    width: 64px;
    height: 64px;
    color: #ff3b30;
    margin-bottom: 16px;
}

.error-content h2 {
    font-size: 24px;
    margin-bottom: 8px;
}

.error-content p {
    color: var(--tg-theme-hint-color);
    font-size: 15px;
}

/* ===== Utilities ===== */
.hidden {
    display: none !important;
}

/* ===== Mobile Styles ===== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        transform: translateX(-100%);
        z-index: 101;
        width: 280px;
        padding-top: var(--safe-area-top);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-close {
        display: block;
    }

    .sidebar-overlay {
        display: block;
    }

    .mobile-header {
        display: flex;
        padding-top: var(--safe-area-top);
    }

    .main-content {
        width: 100%;
    }

    .section-inner {
        padding: 16px;
        padding-bottom: calc(16px + var(--safe-area-bottom));
    }

    .section-header h1 {
        font-size: 24px;
    }

    .stats-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 12px;
    }

    .stat-card {
        padding: 16px 12px;
    }

    .stat-value {
        font-size: 24px;
    }

    .stat-label {
        font-size: 11px;
    }

    .form-row {
        grid-template-columns: 1fr;
    }

    .chart-container {
        height: 200px;
    }

    .logs-container {
        height: calc(100vh - 180px);
    }
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--tg-theme-hint-color);
}

/* ===== Animation ===== */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.section.active {
    animation: fadeIn 0.2s ease;
}
