/* Moltonhim - Mobile-First Design System */

/* ===== Scrollbar Styling ===== */
* {
    scrollbar-width: thin;
    scrollbar-color: rgba(108, 92, 231, 0.3) transparent;
}

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

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

*::-webkit-scrollbar-thumb {
    background: rgba(108, 92, 231, 0.3);
    border-radius: 3px;
}

*::-webkit-scrollbar-thumb:hover {
    background: rgba(108, 92, 231, 0.5);
}

/* ===== CSS Variables ===== */
:root {
    /* Colors - Default/Anonymous (neutral purple) */
    --bg-primary: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-tertiary: #16213e;
    --accent: #6c5ce7;
    --accent-hover: #5a4ad1;
    --accent-muted: rgba(108, 92, 231, 0.2);
    --danger: #e94560;
    --success: #4caf50;
    --warning: #ff9800;
    --text-primary: #eee;
    --text-secondary: #aaa;
    --text-muted: #666;
    --border: #333;
    --code-bg: #0d1117;
}

/* Brenton theme - blue */
.theme-brenton {
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --accent-muted: rgba(59, 130, 246, 0.2);
}

/* Gabby theme - pink */
.theme-gabby {
    --accent: #ec4899;
    --accent-hover: #db2777;
    --accent-muted: rgba(236, 72, 153, 0.2);
}

/* Anonymous theme - neutral gray-purple */
.theme-anonymous {
    --accent: #8b5cf6;
    --accent-hover: #7c3aed;
    --accent-muted: rgba(139, 92, 246, 0.2);
}

:root {
    /* Spacing */
    --space-xs: 0.25rem;
    --space-sm: 0.5rem;
    --space-md: 1rem;
    --space-lg: 1.5rem;
    --space-xl: 2rem;

    /* Layout */
    --nav-height: 50px;
    --bottom-nav-height: 60px;
    --sidebar-width: 350px;
    --max-chat-width: 800px;

    /* Radius */
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-full: 9999px;

    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.4);

    /* Touch targets - minimum 44px for accessibility */
    --touch-target: 44px;
}

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

html {
    height: 100%;
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100%;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

/* ===== App Shell ===== */
.app {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

/* ===== Top Navigation ===== */
.top-nav {
    display: flex;
    align-items: center;
    gap: var(--space-lg);
    padding: 0 var(--space-md);
    height: var(--nav-height);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    z-index: 100;
}

.nav-brand {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--accent);
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: var(--space-xs);
}

.instance-badge {
    font-size: 0.7rem;
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    font-weight: 600;
    text-transform: uppercase;
}

.instance-badge.beta {
    background: var(--warning);
    color: #000;
}

.instance-link {
    font-size: 0.8rem;
    color: var(--text-muted);
    padding: var(--space-xs) var(--space-sm);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: all 0.2s;
}

.instance-link:hover {
    color: var(--text-primary);
    border-color: var(--text-muted);
    text-decoration: none;
}

.nav-links {
    display: flex;
    gap: var(--space-xs);
    flex: 1;
}

.nav-links a {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-md);
    color: var(--text-secondary);
    border-radius: var(--radius-sm);
    transition: all 0.2s;
    font-size: 0.9rem;
}

.nav-links a:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.05);
    text-decoration: none;
}

.nav-links a.active {
    color: var(--accent);
    background: var(--accent-muted);
}

.nav-icon {
    font-size: 1.1rem;
}

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

.header-actions > * {
    white-space: nowrap;
}

.user-name {
    color: var(--accent);
    font-weight: 500;
    font-size: 0.9rem;
}

/* ===== Bottom Navigation (Mobile) ===== */
.bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--bottom-nav-height);
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    z-index: 100;
    padding-bottom: env(safe-area-inset-bottom, 0);
}

.bottom-nav-links {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 100%;
}

.bottom-nav-links a {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-width: var(--touch-target);
    min-height: var(--touch-target);
    padding: var(--space-xs);
    color: var(--text-secondary);
    font-size: 0.7rem;
    border-radius: var(--radius-sm);
    transition: color 0.2s;
}

.bottom-nav-links a:hover,
.bottom-nav-links a:active {
    text-decoration: none;
}

.bottom-nav-links a.active {
    color: var(--accent);
}

.bottom-nav-links .nav-icon {
    font-size: 1.4rem;
}

/* ===== Layout System ===== */
.layout {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
}

/* Centered layout (home/chat) */
.layout-centered {
    display: flex;
    justify-content: center;
    padding: var(--space-md);
    padding-bottom: calc(var(--space-md) + var(--bottom-nav-height));
    overflow: hidden;
}

.layout-centered .chat-container {
    width: 100%;
    max-width: var(--max-chat-width);
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Sidebar layout (ops, logs, files) */
.layout-with-sidebar {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.layout-with-sidebar .main-content {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.layout-with-sidebar .chat-sidebar {
    display: none;
}

/* ===== Chat Container ===== */
.chat-container {
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    border-radius: var(--radius-md);
    overflow: hidden;
}

.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-md);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.chat-title {
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
}

/* Hide chat header on centered layout - nav provides context */
.layout-centered .chat-header {
    display: none;
}

/* ===== Messages Area ===== */
.messages {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: var(--space-md);
    display: flex;
    flex-direction: column;
    gap: var(--space-md);
}

/* ===== Message Bubbles ===== */
.message {
    display: flex;
    flex-direction: column;
    max-width: 90%;
    padding: var(--space-md);
    border-radius: var(--radius-lg);
    animation: fadeIn 0.2s ease;
}

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

.message.user {
    align-self: flex-end;
    background: var(--bg-tertiary);
    border-bottom-right-radius: var(--radius-sm);
}

.message.assistant {
    align-self: flex-start;
    background: var(--bg-secondary);
    border-bottom-left-radius: var(--radius-sm);
}

.message.system {
    align-self: center;
    background: var(--bg-tertiary);
    border: 1px solid var(--warning);
    border-left: 3px solid var(--warning);
    border-radius: var(--radius-md);
    max-width: 95%;
    font-size: 0.85rem;
    opacity: 0.9;
}

.message.system .message-header {
    color: var(--warning);
    font-weight: 600;
}

.message.system .message-content {
    max-height: 150px;
    overflow-y: auto;
    color: var(--text-secondary);
}

.message.system .message-content h1,
.message.system .message-content h2 {
    font-size: 1rem;
    margin: 0 0 var(--space-sm);
}

.message.system .message-content ul {
    margin: 0;
    padding-left: var(--space-md);
}

.message.system .message-content li {
    margin-bottom: var(--space-xs);
}

.message-header {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: var(--space-sm);
}

.message-content {
    line-height: 1.6;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message-content p {
    margin-bottom: var(--space-sm);
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content h1,
.message-content h2,
.message-content h3 {
    margin: var(--space-md) 0 var(--space-sm);
}

.message-content h1:first-child,
.message-content h2:first-child,
.message-content h3:first-child {
    margin-top: 0;
}

/* ===== Code Blocks ===== */
.message-content pre {
    background: var(--code-bg);
    padding: var(--space-md);
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: var(--space-sm) 0;
    font-size: 0.85rem;
}

.message-content code {
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
    font-size: 0.875em;
}

.message-content :not(pre) > code {
    background: var(--code-bg);
    padding: 0.125rem 0.375rem;
    border-radius: var(--radius-sm);
}

/* ===== Lists ===== */
.message-content ul,
.message-content ol {
    padding-left: var(--space-lg);
    margin: var(--space-sm) 0;
}

.message-content li {
    margin-bottom: var(--space-xs);
}

/* ===== Tables ===== */
.message-content table {
    border-collapse: collapse;
    width: 100%;
    margin: var(--space-sm) 0;
    font-size: 0.875rem;
    display: block;
    overflow-x: auto;
}

.message-content th,
.message-content td {
    border: 1px solid var(--border);
    padding: var(--space-sm) var(--space-md);
    text-align: left;
    white-space: nowrap;
}

.message-content th {
    background: var(--code-bg);
    font-weight: 600;
}

.message-content tr:nth-child(even) {
    background: rgba(0, 0, 0, 0.1);
}

/* ===== Tool Use Indicator ===== */
.tool-use {
    background: var(--code-bg);
    border-left: 3px solid var(--text-muted);
    padding: var(--space-md);
    margin: var(--space-sm) 0;
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    font-size: 0.875rem;
    transition: border-color 0.2s;
}

.tool-use.running {
    border-left-color: var(--warning);
}

.tool-use.success {
    border-left-color: var(--success);
}

.tool-use.error {
    border-left-color: var(--danger);
}

.tool-header {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    flex-wrap: wrap;
}

.tool-status {
    font-size: 0.9rem;
}

.tool-icon {
    font-size: 1rem;
}

.tool-name {
    color: var(--accent);
    font-weight: 600;
}

.tool-body {
    margin-top: var(--space-sm);
    padding-left: var(--space-lg);
}

.tool-command {
    color: var(--text-muted);
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
    font-size: 0.8rem;
    background: rgba(0, 0, 0, 0.2);
    padding: var(--space-xs) var(--space-sm);
    border-radius: var(--radius-sm);
    display: inline-block;
    word-break: break-all;
}

.tool-detail {
    color: var(--text-muted);
    font-size: 0.85rem;
}

.tool-result-preview {
    margin-top: var(--space-sm);
    padding: var(--space-sm);
    background: rgba(0, 0, 0, 0.2);
    border-radius: var(--radius-sm);
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 100px;
    overflow-y: auto;
}

.tool-result {
    background: var(--code-bg);
    padding: var(--space-md);
    margin: var(--space-sm) 0;
    border-radius: var(--radius-sm);
    font-size: 0.8rem;
    display: flex;
    gap: var(--space-sm);
    align-items: flex-start;
}

.tool-result.success {
    border-left: 3px solid var(--success);
}

.tool-result.error {
    border-left: 3px solid var(--danger);
}

.tool-result-status {
    flex-shrink: 0;
}

.tool-result-content {
    flex: 1;
    max-height: 200px;
    overflow-y: auto;
    color: var(--text-muted);
    font-family: 'SF Mono', Monaco, 'Courier New', monospace;
    white-space: pre-wrap;
    word-break: break-word;
    margin: 0;
    background: transparent;
    padding: 0;
}

/* ===== Thinking Indicator ===== */
.thinking {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    color: var(--text-muted);
    font-size: 0.875rem;
}

.thinking-dots {
    display: flex;
    gap: 4px;
}

.thinking-dots span {
    width: 6px;
    height: 6px;
    background: var(--accent);
    border-radius: var(--radius-full);
    animation: bounce 1.4s infinite ease-in-out both;
}

.thinking-dots span:nth-child(1) { animation-delay: -0.32s; }
.thinking-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* ===== Welcome Message ===== */
.welcome-message {
    background: var(--bg-secondary);
    padding: var(--space-xl);
    border-radius: var(--radius-lg);
    text-align: center;
    max-width: 400px;
    margin: var(--space-xl) auto;
}

.welcome-message h2 {
    margin-bottom: var(--space-md);
    color: var(--accent);
}

.welcome-message ul {
    list-style: none;
    margin: var(--space-md) 0;
    padding: 0;
}

.welcome-message li {
    padding: var(--space-xs) 0;
    color: var(--text-secondary);
}

/* ===== Chat Form ===== */
.chat-form {
    display: flex;
    gap: var(--space-sm);
    padding: var(--space-md);
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.chat-form textarea {
    flex: 1;
    padding: var(--space-md);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 16px; /* Prevents iOS zoom */
    resize: none;
    min-height: var(--touch-target);
    max-height: 150px;
    line-height: 1.4;
}

.chat-form textarea:focus {
    outline: none;
    border-color: var(--accent);
}

.chat-form textarea::placeholder {
    color: var(--text-muted);
}

.chat-form button {
    min-width: var(--touch-target);
    min-height: var(--touch-target);
    padding: var(--space-sm) var(--space-md);
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-form button:hover {
    background: var(--accent-hover);
}

.chat-form button:disabled {
    background: var(--text-muted);
    cursor: not-allowed;
}

.chat-form button:active {
    transform: scale(0.98);
}

.chat-form .btn-icon {
    font-size: 1.1rem;
}

.chat-form .btn-text {
    font-size: 0.9rem;
}

/* ===== Status Indicator ===== */
.status {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
    padding: var(--space-xs) var(--space-md);
}

.status.connected {
    color: var(--success);
}

.status.error {
    color: var(--danger);
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    min-height: var(--touch-target);
    padding: var(--space-sm) var(--space-md);
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.btn-primary {
    background: var(--accent);
    color: white;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.15);
}

.btn-sm {
    min-height: 32px;
    padding: var(--space-xs) var(--space-sm);
    font-size: 0.8rem;
}

/* ===== Page Content ===== */
.main-content {
    padding: var(--space-md);
    padding-bottom: calc(var(--space-md) + var(--bottom-nav-height));
}

.main-content h1 {
    font-size: 1.5rem;
    margin-bottom: var(--space-lg);
}

/* ===== Cards ===== */
.card {
    background: var(--bg-secondary);
    border-radius: var(--radius-md);
    padding: var(--space-md);
    border: 1px solid var(--border);
}

.card h3 {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: var(--space-sm);
    font-weight: 500;
}

.card-value {
    font-size: 1.5rem;
    font-weight: 600;
}

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

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ===== Responsive: Tablet and up ===== */
@media (min-width: 768px) {
    /* Hide bottom nav, show top nav links */
    .bottom-nav {
        display: none !important;
    }

    .nav-links {
        display: flex;
    }

    /* Remove bottom padding on layouts */
    .layout-centered,
    .main-content {
        padding-bottom: var(--space-md);
    }

    /* Message width */
    .message {
        max-width: 80%;
    }

    /* Sidebar layout with actual sidebar */
    .layout-with-sidebar {
        flex-direction: row;
    }

    .layout-with-sidebar .main-content {
        flex: 1;
        min-width: 0;
    }

    .layout-with-sidebar .chat-sidebar {
        display: flex;
        flex-direction: column;
        width: var(--sidebar-width);
        border-left: 1px solid var(--border);
        flex-shrink: 0;
    }

    .layout-with-sidebar .chat-container {
        height: 100%;
    }
}

/* ===== Responsive: Mobile ===== */
@media (max-width: 767px) {
    /* Show bottom nav, hide desktop nav links */
    .bottom-nav {
        display: block;
    }

    .nav-links {
        display: none;
    }

    /* Simplify top nav */
    .top-nav {
        justify-content: space-between;
    }

    /* Chat form adjustments */
    .chat-form {
        padding: var(--space-sm);
    }

    .chat-form textarea {
        padding: var(--space-sm) var(--space-md);
    }

    .chat-form .btn-text {
        display: none; /* Show only icon on mobile */
    }

    /* Welcome message */
    .welcome-message {
        padding: var(--space-lg);
        margin: var(--space-md) auto;
    }

    /* Main content padding */
    .main-content {
        padding: var(--space-sm);
        padding-bottom: calc(var(--space-md) + var(--bottom-nav-height));
    }

    .main-content h1 {
        font-size: 1.25rem;
        margin-bottom: var(--space-md);
    }
}

/* ===== Responsive: Small phones ===== */
@media (max-width: 380px) {
    :root {
        --space-md: 0.75rem;
    }

    .nav-brand {
        font-size: 1rem;
    }

    .message {
        max-width: 95%;
        padding: var(--space-sm);
    }

    .message-content {
        font-size: 0.95rem;
    }

    .tool-use {
        padding: var(--space-sm);
    }

    .tool-body {
        padding-left: var(--space-md);
    }
}

/* ===== Safe areas (notch, home indicator) ===== */
@supports (padding: max(0px)) {
    .top-nav {
        padding-top: max(0px, env(safe-area-inset-top));
    }

    .bottom-nav {
        padding-bottom: max(var(--space-sm), env(safe-area-inset-bottom));
    }

    .chat-form {
        padding-bottom: max(var(--space-md), env(safe-area-inset-bottom));
    }

    .layout-centered .chat-form {
        padding-bottom: var(--space-md);
    }
}

/* ===== Keyboard handling ===== */
@media (max-height: 500px) and (max-width: 767px) {
    /* Landscape mobile or keyboard open */
    .bottom-nav {
        display: none;
    }

    .layout-centered,
    .main-content {
        padding-bottom: var(--space-md);
    }

    .welcome-message {
        display: none;
    }
}
