:root {
    --bg: #0f172a;
    --panel-bg: #1e293b;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --accent: #38bdf8;
    --accent-hover: #0ea5e9;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --success: #22c55e;
    --success-hover: #16a34a;
    --border: #334155;
    --radius: 8px;
    --font: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    height: 100%;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    height: 100vh;
}

.panel {
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
}

.connection-panel {
    display: flex;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
}

.connection-panel h1 {
    margin: 0;
    font-size: 1.25rem;
    white-space: nowrap;
}

.status-badge {
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 0.85rem;
    font-weight: 600;
    background: #334155;
    color: var(--muted);
}

.status-badge.connected {
    background: rgba(34, 197, 94, 0.15);
    color: var(--success);
}

.status-badge.error {
    background: rgba(239, 68, 68, 0.15);
    color: var(--danger);
}

.btn {
    border: none;
    padding: 8px 14px;
    border-radius: var(--radius);
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    transition: background 0.15s ease;
}

.btn-primary {
    background: var(--accent);
    color: #0f172a;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-success {
    background: var(--success);
    color: #fff;
}
.btn-success:hover { background: var(--success-hover); }

.btn-danger {
    background: var(--danger);
    color: #fff;
}
.btn-danger:hover { background: var(--danger-hover); }

.btn-secondary {
    background: var(--border);
    color: var(--text);
}
.btn-secondary:hover { background: #475569; }

.main-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 16px;
    flex: 1;
    min-height: 0;
}

.scraper-panel {
    display: flex;
    flex-direction: column;
    gap: 12px;
    overflow: hidden;
}

.scraper-panel h2 {
    margin: 0;
    font-size: 1.1rem;
}

.scrape-form {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.scrape-form input[type="text"],
.scrape-form input[type="number"] {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 8px 10px;
    border-radius: var(--radius);
    font-size: 0.9rem;
}

.scrape-form .row {
    display: flex;
    gap: 12px;
    align-items: center;
    flex-wrap: wrap;
}

.scrape-form label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.9rem;
    color: var(--muted);
}

.output-box {
    background: #0b1220;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.8rem;
    line-height: 1.4;
    white-space: pre-wrap;
    overflow-y: auto;
    flex: 1;
    min-height: 120px;
    max-height: 300px;
    color: #cbd5e1;
}

.download-section {
    padding: 8px 0;
}

.files-section {
    border-top: 1px solid var(--border);
    padding-top: 10px;
}

.files-section h3 {
    margin: 0 0 8px;
    font-size: 1rem;
}

.files-toolbar {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.file-list {
    list-style: none;
    padding: 0;
    margin: 0;
    max-height: 160px;
    overflow-y: auto;
}

.file-list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 8px;
    border-radius: var(--radius);
    font-size: 0.85rem;
}

.file-list li:nth-child(odd) { background: rgba(255,255,255,0.03); }

.file-list li .fname {
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-list li .factions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

.file-list li button {
    padding: 4px 8px;
    font-size: 0.75rem;
}

.chat-panel {
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow: hidden;
}

.chat-panel h2 {
    margin: 0;
    font-size: 1.1rem;
}

.chat-settings {
    display: flex;
    gap: 8px;
}

.chat-settings input {
    flex: 1;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 8px 10px;
    border-radius: var(--radius);
}

.context-indicator {
    font-size: 0.85rem;
    color: var(--muted);
    background: rgba(56, 189, 248, 0.08);
    border: 1px solid rgba(56, 189, 248, 0.2);
    padding: 8px 10px;
    border-radius: var(--radius);
}

.context-indicator.has-context {
    color: var(--accent);
    border-color: rgba(56, 189, 248, 0.4);
}

.chat-history {
    flex: 1;
    background: #0b1220;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.message {
    max-width: 92%;
    padding: 10px 12px;
    border-radius: var(--radius);
    font-size: 0.9rem;
    line-height: 1.4;
    white-space: pre-wrap;
}

.message.user {
    align-self: flex-end;
    background: var(--accent);
    color: #0f172a;
}

.message.ai {
    align-self: flex-start;
    background: #334155;
    color: var(--text);
}

.message.system {
    align-self: center;
    background: transparent;
    color: var(--muted);
    font-size: 0.8rem;
    padding: 4px;
}

.chat-input-row {
    display: flex;
    gap: 8px;
}

.chat-input-row input {
    flex: 1;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 10px;
    border-radius: var(--radius);
    font-size: 0.95rem;
}

/* Generated files */
.generated-files-section {
    border-top: 1px solid var(--border);
    padding-top: 10px;
    margin-top: 10px;
}

.generated-files-section h3 {
    margin: 0 0 8px;
    font-size: 1rem;
}

.file-editor {
    max-height: 160px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.gen-file-item {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px 8px;
    font-size: 0.85rem;
}

.gen-file-item input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text);
    font-size: 0.85rem;
    border-bottom: 1px solid var(--border);
}

.gen-file-item button {
    padding: 4px 8px;
    font-size: 0.75rem;
}

/* Deploy panel */
.deploy-panel {
    display: flex;
    flex-direction: column;
    gap: 12px;
    overflow: hidden;
}

.deploy-panel h2 {
    margin: 0;
    font-size: 1.1rem;
}

.deploy-form {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.deploy-form input,
.deploy-form textarea {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 8px 10px;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-family: inherit;
}

.deploy-form textarea {
    min-height: 60px;
    resize: vertical;
}

.deploy-form .row {
    display: flex;
    gap: 8px;
}

.deploy-target {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.deploy-target label {
    font-size: 0.9rem;
    color: var(--muted);
}

.deploy-target input {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 8px 10px;
    border-radius: var(--radius);
    font-size: 0.9rem;
}

/* Debug Console */
.debug-toggle {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--panel-bg);
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 1.3rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10000;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    transition: transform 0.15s ease, background 0.15s ease;
}

.debug-toggle:hover {
    transform: scale(1.1);
    background: #334155;
}

.debug-console {
    position: fixed;
    bottom: 72px;
    right: 20px;
    width: 520px;
    max-width: calc(100vw - 40px);
    height: 380px;
    max-height: calc(100vh - 100px);
    background: var(--panel-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    z-index: 10000;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 32px rgba(0,0,0,0.5);
    overflow: hidden;
}

.debug-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    background: #0b1220;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 0.9rem;
    user-select: none;
}

.debug-actions {
    display: flex;
    gap: 6px;
}

.debug-body {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.78rem;
    line-height: 1.4;
    color: #cbd5e1;
}

.debug-line {
    padding: 3px 6px;
    border-radius: 4px;
    margin-bottom: 2px;
    word-break: break-word;
}

.debug-line.log { color: #cbd5e1; }
.debug-line.info { color: var(--accent); }
.debug-line.warn { color: #fbbf24; background: rgba(251,191,36,0.06); }
.debug-line.error { color: #f87171; background: rgba(248,113,113,0.08); }
.debug-line.ws-in { color: #a78bfa; }
.debug-line.ws-out { color: #34d399; }
.debug-line.api { color: #f472b6; }

.debug-timestamp {
    opacity: 0.5;
    margin-right: 6px;
    font-size: 0.7rem;
}

@media (max-width: 600px) {
    .debug-console {
        width: calc(100vw - 32px);
        right: 16px;
        bottom: 68px;
    }
}
