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

:root {
    --bg: #ffffff;
    --bg-light: #f5f5f5;
    --bg-hover: #e8e8e8;
    --text: #333333;
    --text-bright: #1a1a1a;
    --text-dim: #888888;
    --accent: #0078d4;
    --accent-hover: #005a9e;
    --user-bg: #0078d4;
    --assistant-bg: #f0f0f0;
    --border: #e0e0e0;
    --code-bg: #f6f8fa;
    --success: #2ea043;
    --error: #d1242f;
    --warning: #bf8700;
}

html, body {
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 16px;
    overflow: hidden;
    -webkit-text-size-adjust: none;
}

.hidden { display: none !important; }

/* ============================================================
   LOGIN SCREEN
   ============================================================ */

.login-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    height: 100dvh;
    padding: 24px;
    gap: 12px;
}

.login-logo {
    font-size: 48px;
    margin-bottom: 8px;
}

.login-screen h1 {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-bright);
}

.login-subtitle {
    color: var(--text-dim);
    text-align: center;
    margin-bottom: 16px;
}

.login-screen input {
    width: 100%;
    max-width: 320px;
    background: var(--bg-light);
    color: var(--text-bright);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 14px 18px;
    font-size: 16px;
    outline: none;
}

.login-screen input:focus {
    border-color: var(--accent);
}

.login-screen button {
    width: 100%;
    max-width: 320px;
    padding: 14px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
}

.login-screen button:active {
    background: var(--accent-hover);
}

.login-error {
    color: var(--error);
    font-size: 14px;
    text-align: center;
    width: 100%;
    max-width: 320px;
}

/* ============================================================
   APP LAYOUT
   ============================================================ */

.app-screen {
    height: 100%;
    height: 100dvh;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100%;
    height: 100dvh;
}

/* ============================================================
   HEADER
   ============================================================ */

.header {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    background: var(--bg-light);
    border-bottom: 1px solid var(--border);
    gap: 12px;
    flex-shrink: 0;
    min-height: 50px;
}

.menu-btn {
    background: none;
    border: none;
    color: var(--text);
    font-size: 24px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
}

.menu-btn:active {
    background: var(--bg-hover);
}

.title {
    flex: 1;
    font-weight: 600;
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-bright);
}

.status {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

.status.connected { background: var(--success); }
.status.disconnected { background: var(--error); }

/* ============================================================
   CHAT AREA
   ============================================================ */

.chat {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    -webkit-overflow-scrolling: touch;
}

.welcome-msg {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-dim);
}

.welcome-msg strong {
    color: var(--text);
}

/* Message bubbles */
.message {
    max-width: 92%;
    padding: 12px 16px;
    border-radius: 18px;
    line-height: 1.6;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message.user {
    align-self: flex-end;
    background: var(--user-bg);
    color: var(--text-bright);
    border-bottom-right-radius: 4px;
    white-space: pre-wrap;
}

.message.assistant {
    align-self: flex-start;
    background: var(--assistant-bg);
    border-bottom-left-radius: 4px;
    max-width: 98%;
}

/* Markdown inside assistant bubbles */
.message.assistant p {
    margin: 8px 0;
}

.message.assistant p:first-child { margin-top: 0; }
.message.assistant p:last-child { margin-bottom: 0; }

.message.assistant code {
    background: var(--code-bg);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'SF Mono', 'Fira Code', 'Menlo', monospace;
    font-size: 14px;
}

.message.assistant pre {
    background: var(--code-bg);
    padding: 12px;
    border-radius: 10px;
    overflow-x: auto;
    margin: 10px 0;
    -webkit-overflow-scrolling: touch;
}

.message.assistant pre code {
    background: none;
    padding: 0;
    font-size: 13px;
    line-height: 1.5;
}

.message.assistant ul,
.message.assistant ol {
    padding-left: 20px;
    margin: 8px 0;
}

.message.assistant li {
    margin: 4px 0;
}

.message.assistant table {
    border-collapse: collapse;
    margin: 10px 0;
    width: 100%;
    font-size: 14px;
}

.message.assistant th,
.message.assistant td {
    border: 1px solid var(--border);
    padding: 6px 10px;
    text-align: left;
}

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

.message.assistant blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 12px;
    margin: 8px 0;
    color: var(--text-dim);
}

.message.assistant h1,
.message.assistant h2,
.message.assistant h3 {
    margin: 12px 0 6px 0;
    color: var(--text-bright);
}

.message.assistant h1 { font-size: 20px; }
.message.assistant h2 { font-size: 17px; }
.message.assistant h3 { font-size: 15px; }

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

.message.assistant hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 12px 0;
}

.message.assistant strong {
    color: var(--text-bright);
}

/* Tool call pills — expandable/collapsible */
.tool-pill {
    align-self: flex-start;
    background: var(--bg-light);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    max-width: 95%;
    font-size: 13px;
}

.tool-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    cursor: default;
    user-select: none;
    font-family: 'SF Mono', 'Fira Code', monospace;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tool-pill.done .tool-header,
.tool-pill.error .tool-header {
    cursor: pointer;
}

.tool-pill.done .tool-header:hover,
.tool-pill.error .tool-header:hover {
    background: var(--bg-hover);
}

.tool-icon {
    flex-shrink: 0;
    width: 18px;
    text-align: center;
}

.tool-pill.running .tool-icon { color: var(--warning); }
.tool-pill.done .tool-icon { color: var(--success); }
.tool-pill.error .tool-icon { color: var(--error); }

.tool-label {
    font-weight: 600;
    color: var(--text-bright);
    flex-shrink: 0;
}

.tool-input-summary {
    color: var(--text-dim);
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.tool-chevron {
    flex-shrink: 0;
    font-size: 10px;
    color: var(--text-dim);
    transition: transform 0.15s ease;
    display: none;
}

.tool-pill.done .tool-chevron,
.tool-pill.error .tool-chevron {
    display: inline;
}

.tool-pill.expanded .tool-chevron {
    transform: rotate(90deg);
}

.tool-content {
    display: none;
    border-top: 1px solid var(--border);
    padding: 10px 12px;
}

.tool-pill.expanded .tool-content {
    display: block;
}

.tool-detail-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-dim);
    margin-bottom: 4px;
    font-weight: 600;
}

.tool-result-detail {
    margin-top: 10px;
}

.tool-detail-pre {
    background: var(--code-bg);
    color: var(--text);
    padding: 10px;
    border-radius: 6px;
    font-family: 'SF Mono', 'Fira Code', monospace;
    font-size: 12px;
    line-height: 1.5;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 300px;
    overflow-y: auto;
}

/* Typing indicator */
.typing-indicator {
    align-self: flex-start;
    padding: 14px 18px;
    background: var(--assistant-bg);
    border-radius: 18px;
    border-bottom-left-radius: 4px;
    display: flex;
    gap: 5px;
}

.typing-indicator span {
    display: inline-block;
    width: 8px;
    height: 8px;
    background: var(--text-dim);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

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

/* ============================================================
   INPUT AREA
   ============================================================ */

.input-area {
    display: flex;
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: var(--bg-light);
    border-top: 1px solid var(--border);
    gap: 8px;
    align-items: flex-end;
    flex-shrink: 0;
}

.input-area textarea {
    flex: 1;
    background: var(--bg);
    color: var(--text-bright);
    border: 1px solid var(--border);
    border-radius: 22px;
    padding: 10px 18px;
    font-family: inherit;
    font-size: 16px;
    resize: none;
    max-height: 150px;
    min-height: 44px;
    outline: none;
    line-height: 1.4;
}

.input-area textarea:focus {
    border-color: var(--accent);
}

.input-area textarea:disabled {
    opacity: 0.5;
}

.send-btn {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 50%;
    width: 44px;
    height: 44px;
    font-size: 20px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

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

.send-btn:disabled {
    opacity: 0.3;
    cursor: default;
}

/* ============================================================
   SIDEBAR
   ============================================================ */

.sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: 99;
    display: none;
    backdrop-filter: blur(2px);
}

.sidebar-overlay.visible {
    display: block;
}

.sidebar {
    position: fixed;
    top: 0;
    left: -320px;
    width: 300px;
    max-width: 85vw;
    height: 100%;
    background: var(--bg-light);
    z-index: 100;
    transition: left 0.25s ease;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
}

.sidebar.open {
    left: 0;
}

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

.sidebar-header h2 {
    font-size: 18px;
    font-weight: 700;
    color: var(--text-bright);
}

.sidebar-close-btn {
    background: none;
    border: none;
    color: var(--text);
    font-size: 28px;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}

.sidebar-section {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.sidebar-section label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-dim);
    margin-bottom: 8px;
    display: block;
}

.sidebar-section select {
    width: 100%;
    background: var(--bg);
    color: var(--text-bright);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 36px 10px 12px;
    font-size: 14px;
    font-weight: 500;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    cursor: pointer;
}

.sidebar-section select:focus {
    border-color: var(--accent);
}

.select-wrapper {
    position: relative;
}

.select-arrow {
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 10px;
    color: var(--text-dim);
    pointer-events: none;
}

.new-session-btn {
    margin: 12px 16px;
    padding: 12px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    text-align: center;
    flex-shrink: 0;
}

.new-session-btn:active {
    background: var(--accent-hover);
}

.session-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    -webkit-overflow-scrolling: touch;
}

.session-item {
    padding: 12px;
    border-radius: 10px;
    cursor: pointer;
    margin-bottom: 4px;
}

.session-item:active,
.session-item.active {
    background: var(--bg-hover);
}

.session-title {
    font-size: 14px;
    margin-bottom: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-bright);
}

.session-meta {
    font-size: 12px;
    color: var(--text-dim);
}

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.mac-info {
    font-size: 12px;
    color: var(--text-dim);
}

/* ============================================================
   SCROLLBAR (desktop/tablet)
   ============================================================ */

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
