*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg: #000000;
    --bg-elevated: #0d0d0d;
    --bg-card: #0f0f0f;
    --bg-hover: #1a1a1a;
    --bg-active: #262626;
    --surface: #141414;
    --border: #2a2a2a;
    --border-subtle: #1e1e1e;
    --border-hover: #444444;
    --fg: #ffffff;
    --fg-secondary: #b0b0b0;
    --fg-muted: #777777;
    --fg-faint: #444444;
    --sidebar-w: 280px;
    --header-h: 54px;
    --accent: #e0e0e0;
}

html, body {
    height: 100%;
    font-family: "Inter", -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
    background: var(--bg);
    color: var(--fg);
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}

/* Noise texture overlay */
body::before {
    content: "";
    position: fixed;
    inset: 0;
    opacity: 0.03;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
    background-size: 200px;
    pointer-events: none;
    z-index: 9999;
}

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #222; border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: #444; }

/* ===== Overlay ===== */
#sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    z-index: 90;
    opacity: 0;
    transition: opacity 300ms ease;
}
#sidebar-overlay.visible { display: block; opacity: 1; }

/* ===== Sidebar ===== */
#sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    width: var(--sidebar-w);
    background: var(--bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 100;
    transition: transform 400ms cubic-bezier(0.16, 1, 0.3, 1);
}
#sidebar.hidden { transform: translateX(calc(-1 * var(--sidebar-w))); }

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 18px;
    border-bottom: 1px solid var(--border);
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
}

.sidebar-brand img {
    width: 28px;
    height: 28px;
    border-radius: 4px;
}

.sidebar-brand h1 {
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.3em;
    text-transform: uppercase;
    color: var(--fg);
}

#new-chat-btn {
    width: 30px;
    height: 30px;
    border: 1px solid var(--border-hover);
    border-radius: 8px;
    background: none;
    color: var(--fg-secondary);
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 200ms ease;
}
#new-chat-btn:hover {
    color: var(--fg);
    border-color: var(--fg-muted);
    background: var(--bg-hover);
    transform: scale(1.05);
}

#conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 6px 8px;
}

.conv-item {
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 2px;
    font-size: 13px;
    color: var(--fg-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: all 200ms ease;
}
.conv-item:hover {
    color: var(--fg);
    background: var(--bg-hover);
}
.conv-item.active {
    color: var(--fg);
    background: var(--bg-active);
    font-weight: 500;
}
.conv-item.active::before {
    content: "";
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--fg);
    margin-right: 8px;
    vertical-align: middle;
    box-shadow: 0 0 8px rgba(255, 255, 255, 0.3);
}

/* ===== Main ===== */
#chat-area {
    margin-left: var(--sidebar-w);
    height: 100vh;
    display: flex;
    flex-direction: column;
    transition: margin-left 400ms cubic-bezier(0.16, 1, 0.3, 1);
}
#chat-area.full-width { margin-left: 0; }

/* ===== Header ===== */
#chat-header {
    height: var(--header-h);
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 24px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-elevated);
    flex-shrink: 0;
    position: relative;
}

/* Glow line at bottom of header */
#chat-header::after {
    content: "";
    position: absolute;
    bottom: -1px;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.12) 15%, rgba(255, 255, 255, 0.12) 85%, transparent);
}

.header-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    padding-right: 16px;
    border-right: 1px solid var(--border);
}

.header-logo img {
    width: 24px;
    height: 24px;
    border-radius: 3px;
}

.header-logo span {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.25em;
    text-transform: uppercase;
    color: var(--fg);
}

#toggle-sidebar {
    background: var(--bg-hover);
    border: 1px solid var(--border);
    color: var(--fg-secondary);
    font-size: 16px;
    cursor: pointer;
    padding: 6px 8px;
    border-radius: 8px;
    transition: all 200ms ease;
}
#toggle-sidebar:hover {
    color: var(--fg);
    background: var(--bg-active);
    border-color: var(--border-hover);
}

#chat-title {
    flex: 1;
    font-size: 14px;
    font-weight: 500;
    color: var(--fg);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#chat-actions {
    display: flex;
    align-items: center;
    gap: 5px;
    flex-shrink: 0;
}

#chat-actions button {
    background: var(--bg-hover);
    border: 1px solid var(--border);
    color: var(--fg-secondary);
    padding: 6px 10px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 12px;
    transition: all 200ms ease;
}
#chat-actions button:hover:not(:disabled) {
    color: var(--fg);
    border-color: var(--border-hover);
    background: var(--bg-active);
}
#chat-actions button:disabled { opacity: 0.3; cursor: default; }

#model-input {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--fg);
    padding: 6px 10px;
    border-radius: 8px;
    font-size: 12px;
    font-family: inherit;
    width: 140px;
    transition: border-color 200ms, box-shadow 200ms;
}
#model-input:focus {
    border-color: var(--fg-muted);
    outline: none;
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.06);
}
#model-input::placeholder { color: var(--fg-muted); }

/* ===== Messages ===== */
#messages {
    flex: 1;
    overflow-y: auto;
    padding: 32px 24px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.message { animation: msgIn 350ms cubic-bezier(0.16, 1, 0.3, 1); }

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

/* User messages */
.message.user {
    align-self: flex-end;
    max-width: 70%;
    padding: 12px 18px;
    background: var(--fg);
    color: var(--bg);
    font-size: 14px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
    border-radius: 18px 18px 4px 18px;
    font-weight: 450;
    box-shadow: 0 2px 12px rgba(255, 255, 255, 0.06);
}

/* Assistant messages */
.message.assistant {
    align-self: flex-start;
    width: 100%;
    max-width: 100%;
    position: relative;
    padding-left: 18px;
}

.message.assistant::before {
    content: "";
    position: absolute;
    left: 0;
    top: 4px;
    bottom: 4px;
    width: 1px;
    background: linear-gradient(180deg, transparent, var(--border-hover) 15%, var(--border-hover) 85%, transparent);
}

.message .markdown-body {
    font-size: 14.5px;
    line-height: 1.75;
    color: var(--fg);
}

.markdown-body p { margin-bottom: 14px; }
.markdown-body p:last-child { margin-bottom: 0; }
.markdown-body ul, .markdown-body ol { margin: 8px 0; padding-left: 20px; }
.markdown-body li { margin-bottom: 4px; }
.markdown-body li::marker { color: var(--fg-muted); }
.markdown-body h1, .markdown-body h2, .markdown-body h3 {
    margin: 24px 0 12px;
    font-weight: 600;
    color: var(--fg);
    letter-spacing: -0.02em;
}
.markdown-body h1 { font-size: 1.5em; }
.markdown-body h2 { font-size: 1.25em; }
.markdown-body h3 { font-size: 1.1em; }

.markdown-body pre {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px 20px;
    overflow-x: auto;
    margin: 12px 0;
    font-size: 13px;
}

.markdown-body code {
    font-family: "JetBrains Mono", "SF Mono", "Fira Code", monospace;
    font-size: 0.87em;
}

.markdown-body :not(pre) > code {
    background: var(--bg-hover);
    padding: 2px 7px;
    border-radius: 5px;
    color: var(--fg);
    border: 1px solid var(--border);
}

.markdown-body blockquote {
    border-left: 2px solid var(--fg-muted);
    margin: 12px 0;
    padding: 4px 0 4px 16px;
    color: var(--fg-secondary);
}

.markdown-body table { border-collapse: collapse; margin: 12px 0; width: 100%; }
.markdown-body th, .markdown-body td {
    border: 1px solid var(--border);
    padding: 8px 14px;
    text-align: left;
    font-size: 13.5px;
}
.markdown-body th {
    font-weight: 600;
    color: var(--fg-secondary);
    background: var(--bg-elevated);
}

/* ===== Tool Use Blocks ===== */
.tool-block {
    border: 1px solid var(--border);
    border-radius: 10px;
    margin: 12px 0;
    overflow: hidden;
    background: var(--bg-elevated);
}

.tool-block-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    cursor: pointer;
    font-size: 12.5px;
    font-weight: 500;
    user-select: none;
    color: var(--fg-secondary);
    transition: all 200ms ease;
}
.tool-block-header:hover { color: var(--fg); background: var(--bg-hover); }

.tool-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px; height: 20px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 700;
    color: var(--bg);
    background: var(--fg-muted);
    flex-shrink: 0;
}

.tool-icon.bash { background: #666; }
.tool-icon.read { background: #777; }
.tool-icon.write { background: #888; }
.tool-icon.edit { background: #999; }
.tool-icon.search { background: #aaa; }
.tool-icon.web { background: #bbb; }
.tool-icon.default { background: var(--fg-muted); }

.tool-name {
    font-family: "JetBrains Mono", "SF Mono", monospace;
    font-size: 12px;
    color: inherit;
}

.tool-status {
    margin-left: auto;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 500;
}
.tool-status.running { color: var(--fg-secondary); }
.tool-status.done { color: var(--fg-muted); }
.tool-status.error { color: #888; }

.tool-block-body {
    padding: 0;
    max-height: 0;
    overflow: hidden;
    transition: max-height 400ms cubic-bezier(0.16, 1, 0.3, 1), padding 300ms ease;
}
.tool-block.open .tool-block-body {
    max-height: 2000px;
    padding: 8px 14px 14px;
}

.tool-section-label {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--fg-muted);
    margin: 8px 0 5px;
}
.tool-section-label:first-child { margin-top: 0; }

.tool-content {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px 14px;
    font-family: "JetBrains Mono", "SF Mono", monospace;
    font-size: 12px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
    overflow-x: auto;
    max-height: 400px;
    overflow-y: auto;
    color: var(--fg-secondary);
}
.tool-content.error-content {
    border-color: #333;
    color: #888;
}

/* ===== Thinking Blocks ===== */
.thinking-block {
    border-left: 2px solid var(--border-hover);
    margin: 12px 0;
    overflow: hidden;
    padding-left: 14px;
}

.thinking-block-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 0;
    cursor: pointer;
    font-size: 12px;
    color: var(--fg-muted);
    user-select: none;
    transition: color 200ms ease;
}
.thinking-block-header:hover { color: var(--fg-secondary); }

.thinking-block-body {
    padding: 0;
    max-height: 0;
    overflow: hidden;
    transition: max-height 400ms cubic-bezier(0.16, 1, 0.3, 1), padding 300ms ease;
}
.thinking-block.open .thinking-block-body {
    max-height: 2000px;
    padding: 8px 0;
}

.thinking-content {
    font-size: 13px;
    line-height: 1.6;
    color: var(--fg-muted);
    white-space: pre-wrap;
    font-style: italic;
}

/* ===== Result Footer ===== */
.result-footer {
    margin-top: 12px;
    font-size: 11px;
    color: var(--fg-muted);
    padding-top: 10px;
    border-top: 1px solid var(--border);
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.03em;
}

/* ===== Streaming ===== */
.streaming-cursor::after {
    content: "\2588";
    animation: blink 1s step-end infinite;
    color: var(--fg);
    margin-left: 2px;
}
@keyframes blink { 50% { opacity: 0; } }

/* ===== Loading ===== */
.loading-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 14px 0;
}
.loading-indicator .dot {
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--fg);
    animation: loadPulse 1.4s ease-in-out infinite;
}
.loading-indicator .dot:nth-child(1) { animation-delay: 0s; }
.loading-indicator .dot:nth-child(2) { animation-delay: 0.2s; }
.loading-indicator .dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes loadPulse {
    0%, 80%, 100% { opacity: 0.1; }
    40% { opacity: 0.8; }
}

/* ===== Input Area ===== */
#input-area {
    padding: 16px 24px 24px;
    background: var(--bg);
    display: flex;
    align-items: flex-end;
    gap: 10px;
    flex-shrink: 0;
    position: relative;
}

/* Glow line at top of input area */
#input-area::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.08) 15%, rgba(255, 255, 255, 0.08) 85%, transparent);
}

#message-input {
    flex: 1;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--fg);
    padding: 14px 18px;
    border-radius: 16px;
    font-size: 14.5px;
    font-family: inherit;
    resize: none;
    max-height: 160px;
    line-height: 1.5;
    outline: none;
    transition: border-color 200ms, box-shadow 200ms;
}
#message-input:focus {
    border-color: var(--fg-muted);
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.03);
}
#message-input::placeholder { color: var(--fg-muted); }

#send-btn {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    border: none;
    background: var(--fg);
    color: var(--bg);
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 200ms cubic-bezier(0.16, 1, 0.3, 1);
    font-weight: 600;
}
#send-btn:hover:not(:disabled) {
    transform: scale(1.08);
    box-shadow: 0 0 24px rgba(255, 255, 255, 0.1);
}
#send-btn:active:not(:disabled) {
    transform: scale(0.95);
}
#send-btn:disabled {
    opacity: 0.12;
    cursor: default;
    transform: none;
    box-shadow: none;
}

/* ===== Welcome ===== */
.welcome-screen {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;
    padding: 40px 20px;
    text-align: center;
    animation: welcomeFade 800ms ease;
}

@keyframes welcomeFade {
    from { opacity: 0; }
    to { opacity: 1; }
}

.welcome-screen .welcome-logo {
    width: 64px;
    height: 64px;
    border-radius: 16px;
    opacity: 0.7;
    animation: logoFloat 4s ease-in-out infinite;
}

@keyframes logoFloat {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-6px); }
}

.welcome-screen h2 {
    font-size: 42px;
    font-weight: 200;
    letter-spacing: 0.3em;
    text-transform: uppercase;
    color: var(--fg);
}

.welcome-screen .welcome-line {
    width: 40px;
    height: 1px;
    background: var(--fg-muted);
}

.welcome-screen p {
    font-size: 13px;
    line-height: 1.7;
    color: var(--fg-muted);
    max-width: 320px;
    letter-spacing: 0.02em;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    :root { --sidebar-w: 100vw; }

    #sidebar { transform: translateX(-100%); }
    #sidebar.show { transform: translateX(0); }
    #chat-area { margin-left: 0; }

    #chat-header { padding: 0 14px; gap: 8px; }
    .header-logo { display: none; }
    #model-input { width: 90px; }
    #messages { padding: 20px 12px; gap: 16px; }
    .message.user { max-width: 88%; }
    .message.assistant { padding-left: 14px; }
    .message.assistant::before { left: 0; }
    #input-area { padding: 12px 12px 18px; gap: 8px; }
    #message-input { padding: 12px 14px; font-size: 14px; border-radius: 14px; }
    #send-btn { width: 42px; height: 42px; }

    .welcome-screen h2 { font-size: 28px; }
    .welcome-screen .welcome-logo { width: 48px; height: 48px; }
}

@media (max-width: 480px) {
    #chat-actions button { padding: 4px 6px; font-size: 11px; }
    #model-input { width: 70px; font-size: 11px; }
    .message.user { max-width: 92%; }
}

@supports (padding-top: env(safe-area-inset-top)) {
    #chat-header {
        padding-top: env(safe-area-inset-top);
        height: calc(var(--header-h) + env(safe-area-inset-top));
    }
    #input-area { padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
}
