@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&family=VT323&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    cursor: none;
}

:root {
    --bg: #0a0a0a;
    --card: #111111;
    --card2: #0d0d0d;
    --border: #1e1e3a;
    --accent: #00ff88;
    --accent2: #ff00aa;
    --text: #e0e0e0;
    --dim: #555;
    --danger: #ff3344;
    --sidebar-w: 310px;
}

/* ═══════════════════════════════
   CUSTOM CURSOR
   ═══════════════════════════════ */
.cursor {
    width: 20px;
    height: 20px;
    position: fixed;
    top: -50px;
    left: -50px;
    pointer-events: none;
    z-index: 999999;
    transition: transform 0.08s ease;
}
.cursor::before {
    content: '';
    display: block;
    width: 4px;
    height: 4px;
    background: var(--accent);
    box-shadow:
        4px 0 0 var(--accent),
        8px 0 0 var(--accent),
        0 4px 0 var(--accent),
        0 8px 0 var(--accent),
        4px 4px 0 var(--accent2),
        4px 8px 0 var(--accent2),
        8px 4px 0 var(--accent2),
        8px 8px 0 var(--accent),
        12px 4px 0 var(--accent),
        12px 8px 0 var(--accent),
        12px 12px 0 var(--accent),
        8px 12px 0 var(--accent),
        4px 12px 0 var(--accent2);
    image-rendering: pixelated;
}
.cursor.click {
    transform: scale(0.6);
}

.cursor-trail {
    width: 5px;
    height: 5px;
    position: fixed;
    pointer-events: none;
    z-index: 999998;
    background: var(--accent);
    opacity: 0.4;
    transition: opacity 0.4s ease;
}

/* ═══════════════════════════════
   BODY / BACKGROUND
   ═══════════════════════════════ */
body {
    background: var(--bg);
    color: var(--text);
    font-family: 'VT323', monospace;
    min-height: 100vh;
    overflow-x: hidden;
}

.pixel-bg {
    position: fixed;
    inset: 0;
    z-index: 0;
    opacity: 0.03;
    background-image:
        linear-gradient(var(--accent) 1px, transparent 1px),
        linear-gradient(90deg, var(--accent) 1px, transparent 1px);
    background-size: 20px 20px;
}

.scanlines {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 9000;
    background: repeating-linear-gradient(
        0deg,
        rgba(0,0,0,0.05) 0px,
        rgba(0,0,0,0.05) 1px,
        transparent 1px,
        transparent 3px
    );
    opacity: 0.3;
}

.floating-pixel {
    position: fixed;
    width: 4px;
    height: 4px;
    background: var(--accent);
    opacity: 0.15;
    animation: floatPixel linear infinite;
    z-index: 0;
}

@keyframes floatPixel {
    0%   { transform: translateY(100vh) rotate(0deg); opacity: 0; }
    10%  { opacity: 0.15; }
    90%  { opacity: 0.15; }
    100% { transform: translateY(-20px) rotate(360deg); opacity: 0; }
}

/* ═══════════════════════════════
   SIDEBAR OVERLAY
   ═══════════════════════════════ */
.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.65);
    z-index: 10000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}
.sidebar-overlay.active {
    opacity: 1;
    pointer-events: all;
}

/* ═══════════════════════════════
   SIDEBAR
   ═══════════════════════════════ */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-w);
    height: 100%;
    background: var(--card2);
    border-right: 2px solid var(--accent);
    z-index: 10001;
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}
.sidebar.open {
    transform: translateX(0);
}
.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); }

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 16px;
    border-bottom: 2px solid var(--border);
    background: rgba(0,255,136,0.02);
}

.sidebar-title {
    font-family: 'Press Start 2P', monospace;
    font-size: 12px;
    color: var(--accent);
    letter-spacing: 3px;
}

.sidebar-close {
    background: none;
    border: 1px solid var(--border);
    color: var(--dim);
    font-size: 18px;
    width: 34px;
    height: 34px;
    font-family: 'VT323', monospace;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}
.sidebar-close:hover {
    color: var(--danger);
    border-color: var(--danger);
    background: rgba(255,51,68,0.05);
}

.new-chat-btn {
    margin: 16px;
    padding: 14px 16px;
    font-family: 'Press Start 2P', monospace;
    font-size: 9px;
    color: var(--bg);
    background: var(--accent);
    border: none;
    display: flex;
    align-items: center;
    gap: 10px;
    letter-spacing: 1px;
    transition: all 0.2s;
}
.new-chat-btn:hover {
    background: #00cc6e;
    transform: translateY(-2px);
    box-shadow: 0 4px 0 rgba(0,255,136,0.3);
}
.new-chat-btn:active {
    transform: translateY(0);
    box-shadow: none;
}
.btn-icon {
    font-size: 18px;
    font-family: 'VT323', monospace;
    font-weight: bold;
}

.sidebar-section-label {
    padding: 14px 16px 8px;
    font-family: 'Press Start 2P', monospace;
    font-size: 7px;
    color: var(--accent);
    letter-spacing: 2px;
    text-transform: uppercase;
    border-top: 1px dashed var(--border);
}

.chat-history-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px;
    min-height: 80px;
    max-height: 250px;
}

.history-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border: 1px solid transparent;
    transition: all 0.15s;
    margin-bottom: 2px;
}
.history-item:hover {
    background: rgba(0,255,136,0.04);
    border-color: var(--border);
}
.history-item.active {
    background: rgba(0,255,136,0.08);
    border-color: var(--accent);
    border-left: 3px solid var(--accent);
}
.history-item-icon {
    font-size: 14px;
    color: var(--accent);
    flex-shrink: 0;
}
.history-item-content {
    flex: 1;
    min-width: 0;
}
.history-item-title {
    font-size: 15px;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.history-item-date {
    font-size: 12px;
    color: var(--dim);
    margin-top: 2px;
}
.history-item-delete {
    background: none;
    border: none;
    color: var(--dim);
    font-size: 16px;
    padding: 4px 8px;
    opacity: 0;
    transition: all 0.2s;
    font-family: 'VT323', monospace;
}
.history-item:hover .history-item-delete {
    opacity: 1;
}
.history-item-delete:hover {
    color: var(--danger);
}

.history-empty {
    text-align: center;
    padding: 24px 16px;
    color: var(--dim);
    font-size: 14px;
    line-height: 1.6;
}

.sidebar-footer {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px dashed var(--border);
}
.sidebar-footer-btn {
    flex: 1;
    padding: 10px;
    font-family: 'Press Start 2P', monospace;
    font-size: 7px;
    color: var(--dim);
    background: none;
    border: 1px solid var(--border);
    letter-spacing: 1px;
    transition: all 0.2s;
}
.sidebar-footer-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: rgba(0,255,136,0.03);
}
.sidebar-footer-btn.danger:hover {
    color: var(--danger);
    border-color: var(--danger);
    background: rgba(255,51,68,0.03);
}

/* ═══════════════════════════════
   API SETTINGS IN SIDEBAR
   ═══════════════════════════════ */
.api-settings {
    padding: 8px 16px 24px;
}
.api-label {
    display: block;
    font-family: 'Press Start 2P', monospace;
    font-size: 7px;
    color: var(--dim);
    letter-spacing: 1px;
    margin: 14px 0 6px;
}
.api-input,
.api-select,
.api-textarea {
    width: 100%;
    padding: 10px 12px;
    font-family: 'VT323', monospace;
    font-size: 16px;
    color: var(--text);
    background: var(--bg);
    border: 1px solid var(--border);
    outline: none;
    transition: border-color 0.2s;
}
.api-input:focus,
.api-select:focus,
.api-textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px rgba(0,255,136,0.15);
}
.api-select {
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23555'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 30px;
}
.api-select option {
    background: var(--bg);
    color: var(--text);
}
.api-textarea {
    resize: vertical;
    min-height: 60px;
}
.api-key-wrapper {
    position: relative;
}
.api-toggle-eye {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: var(--dim);
    font-size: 20px;
    padding: 4px 6px;
    font-family: 'VT323', monospace;
    transition: color 0.2s;
}
.api-toggle-eye:hover {
    color: var(--accent);
}

.api-save-btn {
    width: 100%;
    margin-top: 16px;
    padding: 14px;
    font-family: 'Press Start 2P', monospace;
    font-size: 9px;
    color: var(--bg);
    background: var(--accent);
    border: none;
    letter-spacing: 1px;
    transition: all 0.2s;
}
.api-save-btn:hover {
    background: #00cc6e;
}

.api-status {
    text-align: center;
    margin-top: 10px;
    font-size: 15px;
    color: var(--accent);
    min-height: 20px;
}

/* ═══════════════════════════════
   MAIN CONTAINER
   ═══════════════════════════════ */
.container {
    position: relative;
    z-index: 1;
    max-width: 800px;
    margin: 0 auto;
    padding: 0 20px 20px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ═══════════════════════════════
   TOP BAR — CLEARLY VISIBLE MENU
   ═══════════════════════════════ */
.topbar {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px 0;
    border-bottom: 2px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--bg);
    z-index: 100;
}

.menu-btn {
    width: 48px;
    height: 48px;
    background: rgba(0,255,136,0.06);
    border: 2px solid var(--accent);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 5px;
    padding: 10px;
    flex-shrink: 0;
    transition: all 0.2s;
    position: relative;
}

.menu-btn::after {
    content: 'MENU';
    position: absolute;
    bottom: -18px;
    left: 50%;
    transform: translateX(-50%);
    font-family: 'Press Start 2P', monospace;
    font-size: 6px;
    color: var(--accent);
    letter-spacing: 1px;
    opacity: 0.6;
    white-space: nowrap;
}

.hamburger-line {
    width: 22px;
    height: 3px;
    background: var(--accent);
    transition: all 0.2s;
    image-rendering: pixelated;
}

.menu-btn:hover {
    background: rgba(0,255,136,0.15);
    box-shadow: 0 0 12px rgba(0,255,136,0.2);
    transform: scale(1.05);
}

.menu-btn:hover .hamburger-line {
    background: #fff;
    box-shadow: 0 0 4px var(--accent);
}

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

.topbar-title {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.logo-icon {
    display: inline-block;
    width: 14px;
    height: 14px;
    background: var(--accent);
    box-shadow:
        4px 0 0 var(--accent2),
        0 4px 0 var(--accent2),
        4px 4px 0 var(--accent);
    image-rendering: pixelated;
}

.logo-text {
    font-family: 'Press Start 2P', monospace;
    font-size: 18px;
    color: var(--accent);
    text-shadow: 2px 2px 0 rgba(0,255,136,0.15);
    letter-spacing: 2px;
}

.logo-text .accent {
    color: var(--accent2);
}

.topbar-status {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
    padding: 6px 12px;
    border: 1px solid var(--border);
    background: rgba(0,255,136,0.02);
}

.status-dot {
    width: 8px;
    height: 8px;
    background: var(--accent);
    animation: blink 1.5s step-start infinite;
}
.status-dot.error {
    background: var(--danger);
}
.status-dot.loading {
    animation: pulse 0.6s ease-in-out infinite;
}

.status-label {
    font-family: 'Press Start 2P', monospace;
    font-size: 8px;
    color: var(--accent);
    letter-spacing: 1px;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0.3; }
}
@keyframes pulse {
    0%, 100% { opacity: 0.2; transform: scale(0.8); }
    50%      { opacity: 1; transform: scale(1.2); }
}

.chat-name-bar {
    text-align: center;
    padding: 10px 0;
    font-size: 15px;
    color: var(--dim);
    letter-spacing: 1px;
    border-bottom: 1px dashed var(--border);
    margin-bottom: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ═══════════════════════════════
   CHAT AREA
   ═══════════════════════════════ */
.chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 12px 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}
.chat-area::-webkit-scrollbar { width: 4px; }
.chat-area::-webkit-scrollbar-thumb { background: var(--border); }

/* ═══════════════════════════════
   MESSAGES
   ═══════════════════════════════ */
.message {
    display: flex;
    gap: 12px;
    animation: msgIn 0.3s cubic-bezier(0.22, 1, 0.36, 1);
    max-width: 85%;
}
.message.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}
.message.error .bubble {
    border-left-color: var(--danger);
    color: var(--danger);
}
@keyframes msgIn {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}

.avatar {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    grid-template-rows: repeat(6, 1fr);
    image-rendering: pixelated;
}
.avatar-pixel {
    width: 100%;
    height: 100%;
}

.bubble {
    padding: 14px 18px;
    font-size: 18px;
    line-height: 1.6;
    border: 1px solid var(--border);
    background: var(--card);
    word-wrap: break-word;
    overflow-wrap: break-word;
}
.message.ai .bubble {
    border-left: 3px solid var(--accent);
}
.message.user .bubble {
    border-right: 3px solid var(--accent2);
    background: rgba(255,0,170,0.03);
}

.bubble .msg-text {
    white-space: pre-wrap;
}
.bubble .time {
    display: block;
    font-size: 12px;
    color: var(--dim);
    margin-top: 8px;
    text-align: right;
}

/* ═══════════════════════════════
   TYPING
   ═══════════════════════════════ */
.typing {
    display: none;
    align-items: center;
    gap: 12px;
    padding: 8px 0;
}
.typing.active { display: flex; }

.typing-dots {
    display: flex;
    gap: 5px;
    padding: 14px 18px;
    background: var(--card);
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
}
.typing-dots span {
    width: 8px;
    height: 8px;
    background: var(--accent);
    animation: typeDot 1.4s step-start infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typeDot {
    0%, 60% { opacity: 0.2; }
    30%     { opacity: 1; }
}

/* ═══════════════════════════════
   WELCOME
   ═══════════════════════════════ */
.welcome {
    text-align: center;
    padding: 50px 20px 30px;
    color: var(--dim);
}
.welcome h2 {
    font-family: 'Press Start 2P', monospace;
    font-size: 14px;
    color: var(--accent);
    margin-bottom: 16px;
    line-height: 2;
}
.welcome p {
    font-size: 18px;
    line-height: 1.7;
}
.welcome .highlight {
    color: var(--accent);
    font-weight: bold;
}
.welcome-arrow {
    margin-top: 20px;
    font-family: 'Press Start 2P', monospace;
    font-size: 9px;
    color: var(--accent);
    animation: arrowPulse 1.5s ease-in-out infinite;
}
@keyframes arrowPulse {
    0%, 100% { opacity: 0.4; transform: translateX(0); }
    50%      { opacity: 1; transform: translateX(-8px); }
}

.pixel-art-display {
    margin: 0 auto 24px;
    width: 80px;
    height: 80px;
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    gap: 1px;
    image-rendering: pixelated;
}
.pixel-art-display .p {
    width: 100%;
    height: 100%;
}

/* ═══════════════════════════════
   QUICK ACTIONS
   ═══════════════════════════════ */
.quick-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    padding: 10px 0;
}
.quick-btn {
    padding: 8px 14px;
    font-family: 'VT323', monospace;
    font-size: 16px;
    color: var(--dim);
    background: transparent;
    border: 1px solid var(--border);
    transition: all 0.2s;
}
.quick-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: rgba(0,255,136,0.04);
}

/* ═══════════════════════════════
   INPUT AREA
   ═══════════════════════════════ */
.input-area {
    display: flex;
    gap: 8px;
    padding: 16px 0 8px;
    border-top: 2px dashed var(--border);
    margin-top: 10px;
}
.input-wrapper {
    flex: 1;
    position: relative;
}
.input-wrapper::before {
    content: '>';
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--accent);
    font-family: 'Press Start 2P', monospace;
    font-size: 10px;
    animation: blink 1s step-start infinite;
}
#chatInput {
    width: 100%;
    padding: 16px 16px 16px 38px;
    font-family: 'VT323', monospace;
    font-size: 18px;
    color: var(--text);
    background: var(--card);
    border: 1px solid var(--border);
    outline: none;
    transition: border-color 0.2s;
}
#chatInput:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent), inset 0 0 20px rgba(0,255,136,0.02);
}
#chatInput::placeholder {
    color: var(--dim);
}
#chatInput:disabled {
    opacity: 0.4;
}

.send-btn {
    padding: 16px 24px;
    font-family: 'Press Start 2P', monospace;
    font-size: 10px;
    color: var(--bg);
    background: var(--accent);
    border: none;
    letter-spacing: 1px;
    transition: all 0.15s;
    position: relative;
    overflow: hidden;
}
.send-btn:hover {
    background: #00cc6e;
    transform: translateY(-2px);
    box-shadow: 0 4px 0 rgba(0,255,136,0.3);
}
.send-btn:active {
    transform: translateY(0);
    box-shadow: none;
}
.send-btn::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.15), transparent);
    transition: left 0.5s;
}
.send-btn:hover::after {
    left: 100%;
}
.send-btn:disabled {
    opacity: 0.3;
    pointer-events: none;
}

/* ═══════════════════════════════
   FOOTER
   ═══════════════════════════════ */
.footer {
    text-align: center;
    padding: 10px 0;
    font-size: 12px;
    color: var(--dim);
    letter-spacing: 2px;
}

/* ═══════════════════════════════
   TOAST
   ═══════════════════════════════ */
.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 999999;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.toast {
    padding: 12px 20px;
    font-family: 'VT323', monospace;
    font-size: 16px;
    border: 1px solid var(--border);
    background: var(--card);
    color: var(--text);
    animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
    max-width: 320px;
}
.toast.success { border-left: 3px solid var(--accent); }
.toast.error   { border-left: 3px solid var(--danger); color: var(--danger); }
.toast.info    { border-left: 3px solid var(--accent2); }

@keyframes toastIn {
    from { opacity: 0; transform: translateX(40px); }
    to   { opacity: 1; transform: translateX(0); }
}
@keyframes toastOut {
    from { opacity: 1; }
    to   { opacity: 0; transform: translateY(-10px); }
}

/* ═══════════════════════════════
   DIALOG
   ═══════════════════════════════ */
.dialog-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.75);
    z-index: 999999;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}
.dialog-overlay.active {
    opacity: 1;
    pointer-events: all;
}
.dialog-box {
    background: var(--card);
    border: 2px solid var(--border);
    padding: 28px;
    max-width: 400px;
    width: 90%;
    animation: msgIn 0.3s ease;
}
.dialog-title {
    font-family: 'Press Start 2P', monospace;
    font-size: 10px;
    color: var(--accent);
    margin-bottom: 16px;
}
.dialog-msg {
    font-size: 18px;
    color: var(--text);
    margin-bottom: 24px;
    line-height: 1.6;
}
.dialog-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}
.dialog-btn {
    padding: 10px 22px;
    font-family: 'Press Start 2P', monospace;
    font-size: 8px;
    border: 1px solid var(--border);
    background: none;
    color: var(--dim);
    letter-spacing: 1px;
    transition: all 0.2s;
}
.dialog-btn.cancel:hover {
    border-color: var(--dim);
    color: var(--text);
}
.dialog-btn.confirm {
    background: var(--danger);
    border-color: var(--danger);
    color: #fff;
}
.dialog-btn.confirm:hover {
    background: #cc2233;
}

/* ═══════════════════════════════
   RESPONSIVE
   ═══════════════════════════════ */
@media (max-width: 600px) {
    :root { --sidebar-w: 85vw; }
    .container { padding: 0 12px 12px; }
    .logo-text { font-size: 14px; }
    .message { max-width: 95%; }
    .send-btn { padding: 16px 14px; font-size: 8px; }
    .menu-btn { width: 42px; height: 42px; }
    .menu-btn::after { font-size: 5px; }
    .topbar-status { padding: 4px 8px; }
    .status-label { font-size: 6px; }
    .quick-btn { font-size: 14px; padding: 6px 10px; }
}