/* Core Variables for Dark Theme */
:root {
    --bg-main: #0f0f0f;
    --bg-input: #212121;
    --bg-modal: #171717;
    --text-primary: #ececec;
    --text-secondary: #a0a0a0;
    --accent-color: #0f0; /* Hacker green accent */
    --hover-color: #2f2f2f;
    --border-color: #333;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body {
    display: flex;
    height: 100vh;
    background-color: var(--bg-main);
    color: var(--text-primary);
    overflow: hidden;
    position: relative;
}

/* Main Chat Area */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    width: 100%;
}

.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 40px 15%;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* ASCII Logo */
.ascii-logo {
    color: var(--accent-color);
    font-family: monospace;
    text-align: center;
    white-space: pre-wrap;
    font-size: 12px;
    line-height: 1.2;
    margin: 20px auto 40px auto;
    user-select: none;
    font-weight: bold;
}

.message {
    display: flex;
    animation: msg-in .18s ease-out;
}

@keyframes msg-in {
    from { opacity: 0; transform: translateY(5px); }
    to   { opacity: 1; transform: none; }
}

.ai-message {
    justify-content: flex-start;
    flex-direction: column;
}

.user-message {
    justify-content: flex-end;
}

.message-content {
    max-width: 80%;
    padding: 15px;
    border-radius: 12px;
    line-height: 1.5;
    word-wrap: break-word;
}

.ai-message .message-content {
    background-color: transparent;
    max-width: 100%;
}

.user-message .message-content {
    background-color: var(--bg-input);
    white-space: pre-wrap;
}

/* ============ Markdown Rendering ============ */
.message-content > *:first-child { margin-top: 0; }
.message-content > *:last-child { margin-bottom: 0; }

.message-content p { margin: 0 0 10px; }

.message-content h1, .message-content h2, .message-content h3,
.message-content h4, .message-content h5, .message-content h6 {
    color: var(--text-primary);
    line-height: 1.3;
    margin: 18px 0 10px;
}
.message-content h1 { font-size: 1.4em; padding-bottom: 6px; border-bottom: 1px solid var(--border-color); }
.message-content h2 { font-size: 1.22em; padding-bottom: 5px; border-bottom: 1px solid var(--border-color); }
.message-content h3 { font-size: 1.1em; }
.message-content h4, .message-content h5, .message-content h6 { font-size: 1em; }

.message-content ul, .message-content ol {
    margin: 0 0 10px;
    padding-left: 24px;
}
.message-content li { margin: 4px 0; }
.message-content li > ul, .message-content li > ol { margin: 4px 0 0; }
.message-content ul ul { list-style-type: circle; }

.message-content a {
    color: var(--accent-color);
    text-decoration: none;
}
.message-content a:hover { text-decoration: underline; }

.message-content img {
    max-width: 100%;
    border-radius: 8px;
    display: block;
    margin: 8px 0;
}

.message-content hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 16px 0;
}

.message-content blockquote {
    border-left: 3px solid var(--accent-color);
    background: rgba(255, 255, 255, 0.03);
    border-radius: 0 8px 8px 0;
    padding: 6px 14px;
    margin: 10px 0;
    color: var(--text-secondary);
}
.message-content blockquote p:last-child { margin-bottom: 0; }

/* Inline code */
.message-content code {
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
    font-size: 0.86em;
    background: #1c1c1c;
    border: 1px solid #2e2e2e;
    border-radius: 5px;
    padding: 2px 6px;
}

/* Code blocks */
.code-block {
    background: #141414;
    border: 1px solid #2a2a2a;
    border-radius: 10px;
    overflow: hidden;
    margin: 12px 0;
}

.code-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 12px;
    background: #1a1a1a;
    border-bottom: 1px solid #262626;
}

.code-lang {
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: 11px;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-secondary);
}

.code-copy {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    border-radius: 6px;
    font-size: 11px;
    padding: 3px 10px;
    cursor: pointer;
    transition: color .15s, border-color .15s;
}
.code-copy:hover { color: var(--text-primary); border-color: var(--text-secondary); }

.message-content pre {
    margin: 0;
    padding: 12px 14px;
    overflow-x: auto;
    background: transparent;
    font-size: 13px;
    line-height: 1.55;
}
.message-content pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: inherit;
}

/* Highlighter tokens (One-Dark-ish palette) */
.tok-com { color: #7f848e; font-style: italic; }
.tok-str { color: #98c379; }
.tok-num { color: #d19a66; }
.tok-kw  { color: #c678dd; }

/* Tables */
.table-wrap {
    overflow-x: auto;
    margin: 12px 0;
    border: 1px solid var(--border-color);
    border-radius: 8px;
}

.message-content table {
    border-collapse: collapse;
    width: 100%;
    font-size: 14px;
}

.message-content th, .message-content td {
    padding: 8px 12px;
    border-bottom: 1px solid #262626;
    text-align: left;
}

.message-content th {
    background: #1a1a1a;
    font-weight: 600;
}

.message-content tbody tr:last-child td { border-bottom: none; }
.message-content tbody tr:nth-child(even) { background: rgba(255, 255, 255, 0.02); }

/* Task list checkboxes */
.task-box {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 1.5px solid var(--text-secondary);
    border-radius: 4px;
    margin-right: 7px;
    vertical-align: -2px;
    position: relative;
}
.task-box.checked {
    background: var(--accent-color);
    border-color: var(--accent-color);
}
.task-box.checked::after {
    content: '\2713';
    position: absolute;
    inset: 0;
    color: var(--bg-main);
    font-size: 11px;
    line-height: 11px;
    text-align: center;
    font-weight: bold;
}

/* Streaming cursor */
.message-content.streaming > :last-child::after {
    content: '';
    display: inline-block;
    width: 8px;
    height: 15px;
    margin-left: 4px;
    vertical-align: -2px;
    background: var(--accent-color);
    animation: cursor-blink 1s steps(2, start) infinite;
}

@keyframes cursor-blink { 50% { opacity: 0; } }

/* Thinking dots */
.thinking {
    display: inline-flex;
    gap: 6px;
    padding: 4px 0 2px;
}
.thinking span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--accent-color);
    opacity: .4;
    animation: thinking-bounce 1.2s ease-in-out infinite;
}
.thinking span:nth-child(2) { animation-delay: .15s; }
.thinking span:nth-child(3) { animation-delay: .3s; }

@keyframes thinking-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .4; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* Message action bar (copy / regenerate) */
.msg-actions {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    opacity: 0;
    transition: opacity .15s;
}
.message:hover .msg-actions,
.msg-actions:focus-within { opacity: 1; }

.msg-btn {
    font-size: 12px;
    color: var(--text-secondary);
    background: transparent;
    border: 1px solid var(--border-color);
    padding: 3px 10px;
    border-radius: 6px;
    cursor: pointer;
    transition: color .15s, border-color .15s;
}
.msg-btn:hover { color: var(--text-primary); border-color: var(--text-secondary); }

.msg-regen { display: none; }
.ai-message:last-child .msg-regen { display: inline-block; }

/* Error + status notes */
.error-box {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    border: 1px solid rgba(255, 107, 107, .35);
    background: rgba(255, 107, 107, .08);
    color: #ff6b6b;
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 14px;
}
.error-box .retry-btn { margin-left: auto; }

.stopped-note {
    color: var(--text-secondary);
    font-size: 12px;
    margin-top: 6px;
}

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

/* Scrollbars (subtle) */
.messages-area::-webkit-scrollbar { width: 10px; }
.messages-area::-webkit-scrollbar-thumb { background: #2a2a2a; border-radius: 5px; }
.messages-area::-webkit-scrollbar-thumb:hover { background: #3a3a3a; }
.message-content pre::-webkit-scrollbar { height: 8px; }
.message-content pre::-webkit-scrollbar-thumb { background: #2e2e2e; border-radius: 4px; }
.table-wrap::-webkit-scrollbar { height: 8px; }
.table-wrap::-webkit-scrollbar-thumb { background: #2e2e2e; border-radius: 4px; }

/* Input Area */
.input-area {
    padding: 20px 15%;
    background-color: var(--bg-main);
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    background-color: var(--bg-input);
    border-radius: 16px;
    padding: 10px 15px;
    border: 1px solid var(--border-color);
    gap: 8px; /* Adds space between all items */
}

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

.hidden-file-input {
    position: absolute;
    width: 0.1px;
    height: 0.1px;
    opacity: 0;
    overflow: hidden;
    z-index: -1;
}

textarea {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    resize: none;
    max-height: 200px;
    padding: 10px;
    font-size: 16px;
    outline: none;
}

.icon-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 10px;
    border-radius: 8px;
    font-size: 16px;
}

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

/* Send / Stop button */
.send-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    flex: 0 0 auto;
    border: none;
    border-radius: 50%;
    background-color: var(--text-primary);
    color: var(--bg-main);
    cursor: pointer;
    margin-bottom: 2px;
    transition: background-color .15s, transform .06s;
}
.send-btn:hover { background-color: #ffffff; }
.send-btn:active { transform: scale(.92); }
.send-btn.stop {
    background: transparent;
    border: 1.5px solid #ff6b6b;
    color: #ff6b6b;
}
.send-btn.stop:hover { background: rgba(255, 107, 107, .12); }

/* In-line Control Buttons (Info/Restart) */
.input-divider {
    width: 1px;
    height: 30px;
    background-color: var(--border-color);
    margin: 0 5px 8px 5px;
}

.control-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    margin-bottom: 6px;
    transition: all 0.2s;
    white-space: nowrap;
}

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

.file-preview {
    margin-top: 10px;
    padding: 8px 15px;
    background-color: var(--bg-input);
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
}

/* Modal Styling */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.85);
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.modal {
    background-color: var(--bg-modal);
    padding: 30px;
    border-radius: 12px;
    width: 480px;
    max-width: 90%;
    border: 1px solid var(--border-color);
    position: relative;
    max-height: 90vh;
    overflow-y: auto;
}

/* Improved Info Modal Presentation */
.info-section {
    margin-bottom: 15px;
}

.info-section h3 {
    font-size: 15px;
    color: var(--text-primary);
    margin-bottom: 5px;
}

.info-section p {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.5;
}

.info-modal-content a {
    color: var(--accent-color);
    text-decoration: none;
}

.info-modal-content a:hover {
    text-decoration: underline;
}

.close-info-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 16px;
}

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

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    color: var(--text-secondary);
}

.form-group input[type="text"],
.form-group input[type="password"] {
    width: 100%;
    padding: 12px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    background-color: var(--bg-main);
    color: var(--text-primary);
    outline: none;
}

.form-group input:focus {
    border-color: var(--text-secondary);
}

/* Fixed Radio Alignment */
.radio-group {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    margin-bottom: 25px;
}

.radio-group label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--text-primary);
    cursor: pointer;
    margin-bottom: 0;
    line-height: 1;
}

.radio-group input[type="radio"] {
    margin: 0; /* Reset margins that disrupt vertical alignment */
    cursor: pointer;
}

.save-btn {
    width: 100%;
    padding: 12px;
    background-color: var(--text-primary);
    color: var(--bg-main);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: bold;
    font-size: 16px;
    margin-top: 10px;
}

.save-btn:hover {
    background-color: var(--text-secondary);
}

/* Accessibility */
button:focus-visible, a:focus-visible {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
    border-radius: 4px;
}

/* Responsive Design */
@media (max-width: 768px) {
    .messages-area, .input-area {
        padding-left: 15px;
        padding-right: 15px;
    }

    .input-wrapper {
        flex-wrap: wrap; /* Allows controls to wrap on tiny screens */
    }

    .message-content { max-width: 92%; }
    .ai-message .message-content { max-width: 100%; }
    .message-content pre { font-size: 12px; }

    .ascii-logo {
    text-align: center;
    font-family: monospace;
    white-space: pre;
    font-size: 12px;
    line-height: 1.2;
    margin: 40px auto;

    color: #00eaff;

    text-shadow:
        0 0 5px #00eaff,
        0 0 10px #00eaff,
        0 0 20px #00eaff,
        0 0 40px #0088ff;

    user-select: none;
    font-weight: bold;
    }
}

/* Touch devices: keep message actions visible (no hover) */
@media (hover: none) {
    .msg-actions { opacity: 1; }
}

/* Respect reduced-motion preferences */
@media (prefers-reduced-motion: reduce) {
    .message, .thinking span,
    .message-content.streaming > :last-child::after {
        animation: none !important;
    }
    * { transition: none !important; }
}
