:root {
    --bg: #1a1d23;
    --bg-elevated: #21252e;
    --panel: #262b35;
    --panel-hover: #2e3440;
    --border: #353b48;
    --border-strong: #475168;
    --text: #e8eaed;
    --text-soft: #c5c8cf;
    --muted: #8b909a;
    --accent: #6b8aff;
    --accent-soft: #4a5d99;
    --user: #3a4a73;
    --user-text: #e8efff;
    --ai: #2a2f3a;
    --error: #e07a7a;
    --error-bg: rgba(224, 122, 122, 0.12);
    --ok: #6cc28a;
    --warn: #e8b75e;
    --core: #d4a253;
    --tool: #7eb6d4;
    --tool-bg: rgba(126, 182, 212, 0.10);
}

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

html, body, #root { height: 100%; width: 100%; }

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    touch-action: manipulation;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, "Segoe UI", "Heebo", "Arial Hebrew", "Rubik", sans-serif;
    font-size: 16px;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    line-height: 1.5;
}

#root { display: flex; flex-direction: column; }

header {
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
    padding: 12px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.title {
    font-size: 18px;
    font-weight: 600;
    letter-spacing: 0.3px;
    color: var(--text);
}

.status {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 13px;
    color: var(--muted);
}

.dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--warn);
    transition: background 0.3s, box-shadow 0.3s;
    flex-shrink: 0;
}

.dot.ok {
    background: var(--ok);
    box-shadow: 0 0 0 2px rgba(108, 194, 138, 0.18);
}

.dot.err {
    background: var(--error);
    box-shadow: 0 0 0 2px rgba(224, 122, 122, 0.18);
}

.mem-count {
    background: var(--panel);
    border: 1px solid var(--border);
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 11px;
    color: var(--text-soft);
    white-space: nowrap;
}

#model-select {
    background: var(--panel);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 5px 10px;
    border-radius: 8px;
    font-size: 12px;
    max-width: 180px;
    font-family: inherit;
}

.engine-bar {
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    padding: 6px 14px;
    font-size: 12px;
    color: var(--muted);
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background 0.3s, color 0.3s;
}

.engine-bar.tool-active {
    background: var(--tool-bg);
    color: var(--tool);
    border-bottom-color: rgba(126, 182, 212, 0.3);
}

.engine-bar.claude-active {
    background: rgba(212, 162, 83, 0.12);
    color: var(--core);
    border-bottom-color: rgba(212, 162, 83, 0.3);
}

.engine-icon { font-size: 14px; }

.engine-text { font-weight: 500; }

#tabs {
    display: flex;
    gap: 4px;
    padding: 8px 12px;
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
    align-items: center;
}

.tab {
    background: transparent;
    color: var(--muted);
    border: 1px solid transparent;
    padding: 7px 16px;
    border-radius: 9px;
    cursor: pointer;
    font-size: 14px;
    font-family: inherit;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.tab:hover { color: var(--text); background: var(--panel); }

.tab.active {
    background: var(--panel-hover);
    color: var(--text);
    border-color: var(--border-strong);
}

.tab-action {
    margin-inline-start: auto;
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
    padding: 5px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 12px;
    font-family: inherit;
    transition: color 0.15s, border-color 0.15s;
}

.tab-action:hover { color: var(--text); border-color: var(--accent); }

.tab-pane {
    display: none;
    flex: 1;
    flex-direction: column;
    overflow: hidden;
}

.tab-pane.active { display: flex; }

main#messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--bg);
}

main::-webkit-scrollbar, .scrollable::-webkit-scrollbar { width: 6px; }
main::-webkit-scrollbar-thumb, .scrollable::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}
main::-webkit-scrollbar-thumb:hover { background: var(--border-strong); }

.msg {
    max-width: 85%;
    padding: 11px 15px;
    border-radius: 14px;
    line-height: 1.55;
    word-wrap: break-word;
    white-space: pre-wrap;
    box-shadow: 0 1px 2px rgba(0,0,0,0.15);
}

.msg.user {
    align-self: flex-end;
    background: var(--user);
    color: var(--user-text);
    border-bottom-left-radius: 4px;
}

.msg.ai {
    align-self: flex-start;
    background: var(--ai);
    border: 1px solid var(--border);
    border-bottom-right-radius: 4px;
}

.msg.error {
    align-self: stretch;
    background: var(--error-bg);
    border: 1px solid var(--error);
    color: #ffc6c6;
    font-size: 13px;
    text-align: center;
}

.msg.system {
    align-self: stretch;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
    text-align: center;
    padding: 4px;
    box-shadow: none;
}

.tool-event {
    align-self: stretch;
    background: var(--tool-bg);
    border: 1px solid rgba(126, 182, 212, 0.3);
    border-radius: 10px;
    padding: 8px 12px;
    font-size: 13px;
    color: var(--tool);
    display: flex;
    flex-direction: column;
    gap: 4px;
    box-shadow: none;
}

.tool-event-head {
    display: flex;
    align-items: center;
    gap: 8px;
}

.tool-event-spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid var(--tool);
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.tool-event.done .tool-event-spinner {
    display: none;
}

.tool-event.done::before {
    content: "✓ ";
    color: var(--ok);
}

.tool-event-name {
    font-weight: 600;
    font-family: "Consolas", "Menlo", monospace;
    color: var(--text);
    font-size: 12px;
}

.tool-event-args {
    color: var(--muted);
    font-size: 11px;
    font-family: "Consolas", "Menlo", monospace;
    direction: ltr;
    text-align: left;
    word-break: break-all;
}

.tool-event-result {
    color: var(--text-soft);
    font-size: 12px;
    border-top: 1px dashed rgba(126, 182, 212, 0.25);
    padding-top: 6px;
    margin-top: 4px;
    max-height: 120px;
    overflow: auto;
    white-space: pre-wrap;
}

.msg-stats {
    display: block;
    color: var(--muted);
    font-size: 11px;
    margin-top: 6px;
    direction: ltr;
    text-align: left;
}

.cursor::after {
    content: "▍";
    animation: blink 1s infinite;
    opacity: 0.5;
    margin-inline-start: 2px;
}

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

.thinking-pulse {
    color: var(--accent) !important;
    font-size: 13px !important;
    animation: pulse 1.4s ease-in-out infinite;
    display: flex !important;
    justify-content: center;
    gap: 4px;
}

@keyframes pulse {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 1; }
}

.thinking-card {
    align-self: stretch;
    background: var(--tool-bg);
    border: 1px solid rgba(126, 182, 212, 0.25);
    border-radius: 10px;
    padding: 10px 14px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.thinking-head {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--tool);
    font-size: 14px;
}

.thinking-spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid var(--tool);
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    flex-shrink: 0;
}

.thinking-label {
    flex: 1;
    color: var(--text);
    font-weight: 500;
}

.thinking-time {
    font-size: 12px;
    color: var(--muted);
    font-family: "Consolas", "Menlo", monospace;
    direction: ltr;
    flex-shrink: 0;
}

.thinking-detail {
    font-size: 11px;
    color: var(--text-soft);
    line-height: 1.5;
    word-break: break-word;
}

.thinking-detail:empty {
    display: none;
}

#composer {
    background: var(--bg-elevated);
    border-top: 1px solid var(--border);
    padding: 12px 14px;
    display: flex;
    gap: 10px;
    align-items: flex-end;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
}

#input {
    flex: 1;
    background: var(--panel);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 11px 14px;
    font-family: inherit;
    font-size: 16px;
    resize: none;
    max-height: 160px;
    outline: none;
    line-height: 1.4;
    transition: border-color 0.15s;
}

#input:focus { border-color: var(--accent); }
#input::placeholder { color: var(--muted); }

#send-btn {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 12px;
    padding: 11px 20px;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    flex-shrink: 0;
    font-family: inherit;
    transition: filter 0.15s, transform 0.05s;
}

#send-btn:disabled { opacity: 0.5; cursor: not-allowed; }
#send-btn:hover:not(:disabled) { filter: brightness(1.1); }
#send-btn:active:not(:disabled) { transform: scale(0.98); }

.memory-toolbar {
    display: flex;
    gap: 10px;
    padding: 12px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-elevated);
}

.memory-toolbar .primary {
    background: var(--accent);
    color: white;
    border: none;
    padding: 9px 16px;
    border-radius: 9px;
    cursor: pointer;
    font-family: inherit;
    font-size: 14px;
    font-weight: 500;
}

.memory-toolbar .primary:hover { filter: brightness(1.1); }

#mem-search {
    flex: 1;
    background: var(--panel);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 9px 14px;
    border-radius: 9px;
    font-family: inherit;
    font-size: 14px;
    outline: none;
}

#mem-search:focus { border-color: var(--accent); }

#memory-list {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 7px;
}

.mem-item {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 11px 14px;
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s;
}

.mem-item:hover {
    border-color: var(--accent);
    background: var(--panel-hover);
}

.mem-item.is-core {
    border-inline-start: 3px solid var(--core);
}

.mem-item-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}

.mem-item-name {
    font-weight: 600;
    font-size: 14px;
    color: var(--text);
}

.mem-item-type {
    font-size: 11px;
    color: var(--muted);
    background: var(--bg);
    padding: 2px 8px;
    border-radius: 8px;
    font-family: "Consolas", "Menlo", monospace;
}

.mem-item-desc {
    font-size: 12px;
    color: var(--text-soft);
    margin-top: 5px;
    line-height: 1.4;
}

.overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 12, 16, 0.78);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 16px;
    backdrop-filter: blur(2px);
}

.overlay.hidden { display: none; }

.overlay-card {
    background: var(--bg-elevated);
    border: 1px solid var(--border-strong);
    border-radius: 14px;
    padding: 18px;
    width: 100%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}

.overlay-card h3 {
    font-size: 16px;
    margin-bottom: 4px;
    color: var(--text);
}

.overlay-card label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 13px;
    color: var(--text-soft);
}

.overlay-card label.checkbox-row {
    flex-direction: row;
    align-items: center;
    gap: 10px;
}

.overlay-card input[type="text"],
.overlay-card select,
.overlay-card textarea {
    background: var(--panel);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 9px 11px;
    font-family: inherit;
    font-size: 14px;
    outline: none;
}

.overlay-card textarea {
    resize: vertical;
    min-height: 140px;
    font-family: "Consolas", "Menlo", monospace;
    font-size: 13px;
    line-height: 1.5;
}

.overlay-card input:focus, .overlay-card textarea:focus, .overlay-card select:focus {
    border-color: var(--accent);
}

.overlay-actions {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.overlay-actions button {
    border: none;
    padding: 9px 16px;
    border-radius: 8px;
    cursor: pointer;
    font-family: inherit;
    font-size: 14px;
    flex: 1;
    transition: filter 0.15s;
}

#mem-save {
    background: var(--accent);
    color: white;
}

#mem-save:hover { filter: brightness(1.1); }

#mem-cancel.secondary {
    background: var(--panel);
    color: var(--text);
    border: 1px solid var(--border);
    flex: 0 0 auto;
}

#mem-delete.danger {
    background: var(--error);
    color: white;
    flex: 0 0 auto;
}

.hidden { display: none !important; }
