/* CSS Variables - Dark Theme (Tokyo Night inspired) */
:root {
    --bg-primary: #1a1b26;
    --bg-secondary: #16161e;
    --bg-terminal: #0d1117;
    --bg-elevated: #1f2335;
    --bg-hover: #292e42;

    --accent-primary: #7aa2f7;
    --accent-success: #9ece6a;
    --accent-warning: #e0af68;
    --accent-error: #f7768e;
    --accent-info: #7dcfff;

    --text-primary: #c0caf5;
    --text-secondary: #a9b1d6;
    --text-muted: #565f89;
    --text-inverse: #1a1b26;

    --border-color: #292e42;
    --border-focus: #7aa2f7;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.5);

    --font-mono: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;

    --transition-fast: 0.15s ease;
    --transition-normal: 0.2s ease;
    --transition-slow: 0.3s ease;

    --header-height: 48px;
    --tabs-height: 40px;
    --input-height: 56px;
    --quick-buttons-height: 44px;
}

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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* App Container */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    overflow: hidden;
}

/* Header */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-height);
    padding: 0 12px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.logo {
    font-size: 16px;
    font-weight: 600;
    color: var(--accent-primary);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.connection-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--text-muted);
}

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

.status-dot.online {
    background-color: var(--accent-success);
    box-shadow: 0 0 8px var(--accent-success);
}

.status-dot.offline {
    background-color: var(--accent-error);
}

.status-dot.connecting {
    background-color: var(--accent-warning);
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Icon Buttons */
.btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

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

.btn-icon:active {
    transform: scale(0.95);
}

/* Session Tabs */
.tabs-container {
    display: flex;
    align-items: center;
    height: var(--tabs-height);
    padding: 0 8px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
    overflow: hidden;
}

.tabs {
    display: flex;
    gap: 4px;
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
    flex: 1;
}

.tabs::-webkit-scrollbar {
    display: none;
}

.tab {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: 13px;
    font-family: var(--font-mono);
    cursor: pointer;
    transition: all var(--transition-fast);
    white-space: nowrap;
    flex-shrink: 0;
}

.tab:hover {
    background: var(--bg-hover);
    color: var(--text-secondary);
}

.tab.active {
    background: var(--bg-elevated);
    border-color: var(--border-color);
    color: var(--text-primary);
}

.tab.has-activity {
    color: var(--accent-primary);
}

.tab.has-activity::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent-primary);
    animation: pulse 2s infinite;
}

.tab-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border-radius: 2px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
    transition: all var(--transition-fast);
}

.tab-close:hover {
    background: var(--accent-error);
    color: white;
}

.btn-new-tab {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: 1px dashed var(--border-color);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    transition: all var(--transition-fast);
    flex-shrink: 0;
    margin-left: 4px;
}

.btn-new-tab:hover {
    border-color: var(--accent-primary);
    color: var(--accent-primary);
    background: rgba(122, 162, 247, 0.1);
}

/* Terminal Container */
.terminal-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: var(--bg-terminal);
}

#terminal {
    height: 100%;
    padding: 8px;
}

/* xterm.js customization */
.xterm {
    padding: 0;
}

.xterm-viewport {
    background-color: var(--bg-terminal) !important;
}

.xterm-screen {
    padding: 4px;
}

/* Choice Popup */
.choice-popup {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(22, 22, 30, 0.95);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--border-color);
    padding: 16px;
    transform: translateY(0);
    transition: transform var(--transition-normal);
    z-index: 100;
}

.choice-popup.hidden {
    transform: translateY(100%);
    pointer-events: none;
}

.choice-content {
    max-width: 500px;
    margin: 0 auto;
}

.choice-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.choice-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    background: var(--accent-primary);
    color: var(--text-inverse);
    border-radius: 50%;
    font-size: 18px;
    font-weight: 600;
}

.choice-title {
    font-size: 16px;
    font-weight: 500;
    color: var(--text-primary);
}

.choice-context {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 16px;
    padding: 8px 12px;
    background: var(--bg-terminal);
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    max-height: 100px;
    overflow-y: auto;
}

.choice-buttons {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.choice-btn {
    flex: 1;
    min-width: 100px;
    padding: 12px 20px;
    border: none;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.choice-btn.primary {
    background: var(--accent-primary);
    color: var(--text-inverse);
}

.choice-btn.primary:hover {
    background: #8bb0ff;
}

.choice-btn.secondary {
    background: var(--bg-hover);
    color: var(--text-primary);
}

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

.choice-btn.danger {
    background: var(--accent-error);
    color: white;
}

.choice-btn.danger:hover {
    background: #ff8fa0;
}

.choice-btn .key-hint {
    display: inline-block;
    margin-left: 8px;
    padding: 2px 6px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
    font-size: 11px;
    text-transform: uppercase;
}

/* Input Panel */
.input-panel {
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 6px;
    transition: border-color var(--transition-fast);
}

.input-wrapper:focus-within {
    border-color: var(--border-focus);
}

.input-field {
    flex: 1;
    min-height: 36px;
    max-height: 120px;
    padding: 8px 12px;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 14px;
    line-height: 1.4;
    resize: none;
    outline: none;
}

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

.btn-mic {
    color: var(--text-muted);
}

.btn-mic.recording {
    color: var(--accent-error);
    animation: pulse 1s infinite;
}

.btn-send {
    background: var(--accent-primary);
    color: var(--text-inverse);
}

.btn-send:hover {
    background: #8bb0ff;
}

/* Quick Buttons */
.quick-buttons {
    display: flex;
    justify-content: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.quick-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px 16px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 12px;
    font-family: var(--font-mono);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.quick-btn:hover {
    background: var(--bg-hover);
    border-color: var(--accent-primary);
    color: var(--accent-primary);
}

.quick-btn:active {
    transform: scale(0.95);
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 1;
    transition: opacity var(--transition-normal);
}

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

.modal-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
}

.modal-content {
    position: relative;
    width: 90%;
    max-width: 400px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
}

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

.modal-header h3 {
    font-size: 18px;
    font-weight: 600;
}

.btn-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 24px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
}

.btn-close:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
}

.setting-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid var(--border-color);
}

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

.setting-item label {
    font-size: 14px;
    color: var(--text-primary);
}

.setting-control {
    display: flex;
    align-items: center;
    gap: 8px;
}

.setting-control input[type="range"] {
    width: 100px;
    accent-color: var(--accent-primary);
}

.setting-control input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--accent-primary);
}

#fontSizeValue {
    font-size: 12px;
    color: var(--text-muted);
    min-width: 35px;
}

/* Loading Overlay */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-primary);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    z-index: 2000;
    opacity: 1;
    transition: opacity var(--transition-slow);
}

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

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--bg-elevated);
    border-top-color: var(--accent-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

.loading-text {
    font-size: 14px;
    color: var(--text-muted);
}

/* Toast Notifications */
.toast-container {
    position: fixed;
    bottom: calc(var(--input-height) + var(--quick-buttons-height) + 16px);
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 500;
    pointer-events: none;
}

.toast {
    padding: 12px 20px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: 14px;
    box-shadow: var(--shadow-md);
    animation: slideUp 0.3s ease;
    pointer-events: auto;
}

.toast.success {
    border-color: var(--accent-success);
}

.toast.error {
    border-color: var(--accent-error);
}

.toast.warning {
    border-color: var(--accent-warning);
}

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

/* Utility Classes */
.hidden {
    display: none !important;
}

/* Responsive */
@media (max-width: 480px) {
    .quick-btn span {
        display: none;
    }

    .quick-btn {
        padding: 10px 12px;
    }

    .choice-buttons {
        flex-direction: column;
    }

    .choice-btn {
        width: 100%;
    }
}

/* Telegram WebApp specific */
body.tg-webapp {
    --tg-theme-bg-color: var(--bg-primary);
    --tg-theme-text-color: var(--text-primary);
    --tg-theme-button-color: var(--accent-primary);
    --tg-theme-button-text-color: var(--text-inverse);
}

/* Safe area padding for mobile */
@supports (padding-top: env(safe-area-inset-top)) {
    .header {
        padding-top: env(safe-area-inset-top);
        height: calc(var(--header-height) + env(safe-area-inset-top));
    }

    .quick-buttons {
        padding-bottom: calc(8px + env(safe-area-inset-bottom));
    }
}

/* Scrollbar styling */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
    background: var(--bg-hover);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Selection */
::selection {
    background: var(--accent-primary);
    color: var(--text-inverse);
}

/* Tab Directory */
.tab-dir {
    font-size: 10px;
    color: var(--text-muted);
    opacity: 0.7;
    max-width: 80px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tab.active .tab-dir {
    color: var(--accent-primary);
    opacity: 0.9;
}

/* Folder Picker Modal */
.folder-picker {
    max-height: 70vh;
    display: flex;
    flex-direction: column;
}

.folder-picker .current-path {
    padding: 12px 20px;
    background: var(--bg-secondary);
    font-family: var(--font-mono);
    font-size: 13px;
    color: var(--accent-primary);
    border-bottom: 1px solid var(--border-color);
    word-break: break-all;
}

.folder-picker .folder-list {
    flex: 1;
    overflow-y: auto;
    max-height: 300px;
    padding: 8px 0;
}

.folder-picker .folder-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    cursor: pointer;
    transition: background var(--transition-fast);
}

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

.folder-picker .folder-item.parent {
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
}

.folder-picker .folder-icon {
    font-size: 18px;
    width: 24px;
    text-align: center;
}

.folder-picker .folder-name {
    flex: 1;
    font-size: 14px;
    color: var(--text-primary);
}

.folder-picker .loading,
.folder-picker .empty,
.folder-picker .error {
    padding: 20px;
    text-align: center;
    color: var(--text-muted);
}

.folder-picker .error {
    color: var(--accent-error);
}

.folder-picker .modal-footer {
    display: flex;
    gap: 12px;
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
}

.folder-picker .modal-footer .btn {
    flex: 1;
    padding: 12px 16px;
    border: none;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.folder-picker .modal-footer .btn.primary {
    background: var(--accent-primary);
    color: var(--text-inverse);
}

.folder-picker .modal-footer .btn.primary:hover {
    background: #8bb0ff;
}

.folder-picker .modal-footer .btn.secondary {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.folder-picker .modal-footer .btn.secondary:hover {
    background: var(--border-color);
}

/* Debug Panel */
.debug-toggle {
    position: fixed;
    bottom: 160px;
    right: 12px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--bg-elevated);
    border: 1px solid var(--border-color);
    font-size: 20px;
    cursor: pointer;
    z-index: 900;
    box-shadow: var(--shadow-md);
    transition: all var(--transition-fast);
}

.debug-toggle:hover {
    background: var(--bg-hover);
    transform: scale(1.05);
}

.debug-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 50vh;
    background: var(--bg-secondary);
    border-top: 2px solid var(--accent-primary);
    z-index: 950;
    display: flex;
    flex-direction: column;
    transition: transform var(--transition-normal);
}

.debug-panel.hidden {
    transform: translateY(100%);
}

.debug-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border-color);
    font-weight: 600;
    color: var(--accent-primary);
}

.debug-header > div {
    display: flex;
    gap: 4px;
}

.debug-content {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    font-family: var(--font-mono);
    font-size: 11px;
    line-height: 1.4;
    white-space: pre-wrap;
    word-break: break-all;
}

.debug-content .log-entry {
    padding: 2px 0;
    border-bottom: 1px solid var(--border-color);
}

.debug-content .log-time {
    color: var(--text-muted);
    margin-right: 8px;
}

.debug-content .log-info {
    color: var(--accent-info);
}

.debug-content .log-warn {
    color: var(--accent-warning);
}

.debug-content .log-error {
    color: var(--accent-error);
}

.debug-content .log-debug {
    color: var(--text-secondary);
}
