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

:root {
    --bg: #2f2f2e;
    --bg-secondary: #262625;
    --bg-tertiary: #1e1e1d;
    --surface: #3b3a39;
    --surface-hover: #474644;
    --border: #4a4846;
    --text: #ececec;
    --text-secondary: #a8a29e;
    --text-muted: #78716c;
    --accent: #da7756;
    --accent-hover: #c4684a;
    --accent-dim: rgba(218, 119, 86, 0.15);
    --danger: #ef4444;
    --success: #a3e635;
    --warning: #f5a623;
    --user-msg-bg: #4a3f38;
    --user-msg-border: rgba(218, 119, 86, 0.3);
    --sidebar-width: 260px;
    --header-height: 50px;
    --input-height: 60px;
    --radius: 12px;
    --radius-sm: 6px;
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --font-mono: "SF Mono", "Cascadia Code", "Fira Code", monospace;
}

html {
    height: 100%;
    overflow: hidden;
}
body {
    height: 100vh;
    height: 100dvh;
    font-family: var(--font);
    font-size: 14px;
    background: var(--bg);
    color: var(--text);
    overflow: hidden;
    position: fixed;
    width: 100%;
}

.hidden { display: none !important; }

/* === Login === */
.login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    height: 100dvh;
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-secondary) 100%);
}
.login-box {
    text-align: center;
    padding: 40px;
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--border);
}
.login-box h1 { margin-bottom: 8px; font-size: 24px; }
.login-box p { color: var(--text-secondary); margin-bottom: 24px; }

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 8px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s, border-color 0.15s;
    font-family: var(--font);
    color: var(--text);
    background: var(--surface);
}
.btn:hover { background: var(--surface-hover); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--surface); }
.btn-block { width: 100%; margin-bottom: 6px; }
.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 18px;
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    transition: color 0.15s, background 0.15s;
}
.icon-btn:hover { color: var(--text); background: var(--surface-hover); }
.login-btn { font-size: 16px; padding: 12px 32px; }

/* === App Layout === */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    padding-top: env(safe-area-inset-top, 0px);
    background: var(--bg-secondary); /* notch area matches header */
}
.header {
    display: flex;
    align-items: center;
    height: var(--header-height);
    padding: 0 12px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    gap: 8px;
    flex-shrink: 0;
    z-index: 20;
}
.header-title {
    font-weight: 600;
    font-size: 15px;
    white-space: nowrap;
}
.header-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
}
.header-select {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 4px 8px;
    font-size: 12px;
    font-family: var(--font);
    cursor: pointer;
}
.session-cost {
    font-size: 12px;
    font-family: var(--font-mono);
    color: var(--success);
    padding: 2px 8px;
    background: var(--accent-dim);
    border-radius: var(--radius-sm);
    white-space: nowrap;
}
.hamburger-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
    padding: 4px 8px;
}

/* === Main Layout === */
.main-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* === Sidebar === */
.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.sidebar-inner {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow-y: auto;
    padding: 12px;
}
.sidebar-section { margin-bottom: 16px; }
.sidebar-section-title {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin-bottom: 6px;
    padding: 0 4px;
}
.sidebar-list { display: flex; flex-direction: column; gap: 2px; }
.sidebar-item {
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    color: var(--text-secondary);
    transition: background 0.15s, color 0.15s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.sidebar-item:hover { background: var(--surface-hover); color: var(--text); }
.sidebar-item.active { background: var(--accent-dim); color: var(--accent); }
.sidebar-item .delete-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 14px;
    padding: 0 4px;
    flex-shrink: 0;
}
.sidebar-item:hover .delete-btn { display: inline; }
.sidebar-item .delete-btn:hover { color: var(--danger); }
.sidebar-actions {
    margin-top: auto;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}
.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 29;
}

/* === Chat Area === */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    scroll-behavior: smooth;
}
.welcome-msg {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    text-align: center;
    gap: 8px;
}
.welcome-msg h2 { font-size: 20px; color: var(--text-secondary); }

/* === Messages === */
.message {
    margin-bottom: 16px;
    max-width: 850px;
}
.message.user-msg {
    margin-left: auto;
    background: var(--user-msg-bg);
    border: 1px solid var(--user-msg-border);
    border-radius: var(--radius) var(--radius) 4px var(--radius);
    padding: 10px 14px;
    max-width: 80%;
}
.message.assistant-msg {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) var(--radius) 4px;
    padding: 14px 16px;
}
.message-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 6px;
}
.message-content {
    line-height: 1.6;
    word-wrap: break-word;
    overflow-wrap: break-word;
}
.message-content p { margin-bottom: 8px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content pre {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 13px;
}
.message-content code {
    font-family: var(--font-mono);
    font-size: 0.9em;
}
.message-content :not(pre) > code {
    background: var(--bg);
    padding: 2px 6px;
    border-radius: 3px;
}
.message-content ul, .message-content ol {
    margin: 8px 0 8px 20px;
}
.message-content li { margin-bottom: 4px; }
.message-content blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 12px;
    color: var(--text-secondary);
    margin: 8px 0;
}
.message-content h1, .message-content h2, .message-content h3,
.message-content h4, .message-content h5, .message-content h6 {
    margin: 12px 0 6px;
}
.message-content table {
    border-collapse: collapse;
    margin: 8px 0;
    width: 100%;
}
.message-content th, .message-content td {
    border: 1px solid var(--border);
    padding: 6px 10px;
    text-align: left;
}
.message-content th { background: var(--bg-secondary); }

/* Tool calls */
.tool-call {
    margin: 8px 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}
.tool-call-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    cursor: pointer;
    font-size: 12px;
    font-family: var(--font-mono);
    color: var(--text-secondary);
    user-select: none;
}
.tool-call-header:hover { background: var(--surface-hover); }
.tool-call-header .arrow { transition: transform 0.15s; }
.tool-call-header .arrow.open { transform: rotate(90deg); }
.tool-call-body {
    display: none;
    padding: 8px 12px;
    background: var(--bg);
    font-size: 12px;
    font-family: var(--font-mono);
    max-height: 300px;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-all;
}
.tool-call-body.open { display: block; }

/* Streaming raw text */
.streaming-raw {
    white-space: pre-wrap;
    word-wrap: break-word;
    font-family: var(--font);
    line-height: 1.6;
}
.streaming-cursor-char {
    animation: blink 0.8s step-end infinite;
    color: var(--accent);
}

/* Streaming cursor on rendered markdown */
.streaming-cursor::after {
    content: "▊";
    animation: blink 0.8s step-end infinite;
    color: var(--accent);
    font-weight: normal;
}
@keyframes blink { 50% { opacity: 0; } }

/* === Input Area === */
.input-area {
    padding: 12px 16px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--border);
    background: var(--bg-secondary);
    flex-shrink: 0;
    position: relative;
}
.input-row {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px 8px;
}
.message-input {
    flex: 1;
    background: none;
    border: none;
    color: var(--text);
    font-family: var(--font);
    font-size: 14px;
    resize: none;
    outline: none;
    max-height: 200px;
    line-height: 1.5;
    padding: 4px 0;
}
.message-input::placeholder { color: var(--text-muted); }
.quick-cmd-btn { font-size: 20px; color: var(--warning); }
.send-btn { font-size: 20px; color: var(--accent); }
.send-btn:disabled { color: var(--text-muted); cursor: default; }

/* Quick commands dropdown */
.quick-cmd-menu {
    position: absolute;
    bottom: calc(100% + 4px);
    left: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px;
    z-index: 30;
    min-width: 200px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}
.quick-cmd-item {
    display: block;
    width: 100%;
    text-align: left;
    padding: 8px 12px;
    border: none;
    background: none;
    color: var(--text);
    font-size: 13px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-family: var(--font);
}
.quick-cmd-item:hover { background: var(--surface-hover); }

/* === Modals === */
.modal {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
}
.modal-content {
    position: relative;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    width: 90%;
    max-width: 480px;
    max-height: 90vh;
    overflow-y: auto;
}
.modal-small { max-width: 320px; }
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 15px; }
.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 22px;
    cursor: pointer;
    line-height: 1;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 16px; }
.modal-body label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 4px;
    margin-top: 12px;
}
.modal-body label:first-child { margin-top: 0; }
.modal-footer {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}
.settings-textarea {
    width: 100%;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px;
    font-family: var(--font-mono);
    font-size: 13px;
    resize: vertical;
}
.settings-input {
    width: 100%;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px;
    font-family: var(--font);
    font-size: 13px;
}
.settings-select { width: 100%; padding: 8px; }

/* Add project button in sidebar title */
.sidebar-add-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-muted);
    font-size: 14px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    padding: 0 6px;
    line-height: 1.4;
    float: right;
    transition: color 0.15s, border-color 0.15s;
}
.sidebar-add-btn:hover { color: var(--accent); border-color: var(--accent); }

/* Modal tabs */
.modal-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 12px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 8px;
}
.modal-tab {
    background: none;
    border: 1px solid transparent;
    color: var(--text-secondary);
    padding: 6px 12px;
    font-size: 13px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    font-family: var(--font);
    transition: color 0.15s, background 0.15s;
}
.modal-tab:hover { color: var(--text); background: var(--surface); }
.modal-tab.active { color: var(--accent); border-color: var(--accent); background: var(--accent-dim); }
.tab-content.hidden { display: none; }
.input-hint {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* === Scrollbar === */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* === Mobile === */
@media (max-width: 768px) {
    .hamburger-btn {
        display: block;
        font-size: 24px;
        padding: 8px 12px;
        min-width: 44px;
        min-height: 44px;
    }
    .header-title { display: none; }
    .header {
        height: auto;
        min-height: var(--header-height);
        padding: 6px 8px;
        flex-wrap: wrap;
        gap: 6px;
    }
    .header-controls {
        gap: 6px;
    }
    .header-select {
        font-size: 13px;
        padding: 8px 10px;
        min-height: 40px;
        border-radius: var(--radius);
    }
    .session-cost {
        font-size: 12px;
        padding: 6px 10px;
        min-height: 40px;
        display: flex;
        align-items: center;
    }
    .icon-btn {
        font-size: 22px;
        padding: 8px 12px;
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        z-index: 30;
        width: 80vw;
        max-width: 320px;
        padding-top: env(safe-area-inset-top, 0px);
        transform: translateX(-100%);
        transition: transform 0.25s ease;
    }
    .sidebar.open {
        transform: translateX(0);
    }
    .sidebar-item {
        padding: 12px 12px;
        font-size: 14px;
        min-height: 44px;
    }
    .sidebar-actions .btn {
        padding: 12px 16px;
        font-size: 14px;
        min-height: 44px;
    }

    .message.user-msg { max-width: 90%; }

    .input-row {
        padding: 8px 10px;
    }
    .message-input {
        font-size: 16px; /* prevents iOS zoom on focus */
    }
    .quick-cmd-btn {
        font-size: 24px;
        min-width: 44px;
        min-height: 44px;
    }
    .send-btn {
        font-size: 24px;
        min-width: 44px;
        min-height: 44px;
    }
    .quick-cmd-item {
        padding: 12px 16px;
        font-size: 15px;
        min-height: 44px;
    }

    /* Modal buttons bigger on mobile */
    .modal-footer .btn {
        padding: 12px 20px;
        font-size: 15px;
        min-height: 44px;
    }
    .modal-tab {
        padding: 10px 14px;
        font-size: 14px;
        min-height: 40px;
    }
}

/* === Loading spinner === */
.loading-dots::after {
    content: "";
    animation: dots 1.5s steps(4, end) infinite;
}
@keyframes dots {
    0% { content: ""; }
    25% { content: "."; }
    50% { content: ".."; }
    75% { content: "..."; }
}
