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

body,
html {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: #fff;
    font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
}

canvas {
    display: block;
    cursor: crosshair;
    touch-action: none;
    user-select: none;
}

/* ===== Control Panel ===== */
#control-panel {
    position: absolute;
    top: 12px;
    left: 12px;
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 12px;
    width: 260px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    z-index: 100;
}

#control-panel h3 {
    color: #333;
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 10px;
}

.input-row {
    display: flex;
    gap: 6px;
    margin-bottom: 10px;
}

#function-input {
    flex: 1;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 8px 10px;
    color: #333;
    font-size: 13px;
    font-family: monospace;
    outline: none;
}

#function-input:focus {
    border-color: #2563eb;
}

#function-input::placeholder {
    color: #999;
}

.btn {
    background: #2563eb;
    border: none;
    border-radius: 4px;
    padding: 8px 14px;
    color: #fff;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
}

.btn:hover {
    background: #1d4ed8;
}

.btn-secondary {
    background: #f5f5f5;
    color: #333;
    border: 1px solid #ddd;
}

.btn-secondary:hover {
    background: #eee;
}

#functions-list {
    max-height: 180px;
    overflow-y: auto;
    margin-bottom: 10px;
}

.function-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    background: #f9f9f9;
    border-radius: 4px;
    margin-bottom: 4px;
}

.function-item:hover {
    background: #f0f0f0;
}

.function-color {
    width: 10px;
    height: 10px;
    border-radius: 2px;
    flex-shrink: 0;
}

.function-expr {
    flex: 1;
    color: #333;
    font-size: 12px;
    font-family: monospace;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.function-toggle {
    width: 16px;
    height: 16px;
    background: #fff;
    border: 1.5px solid #ccc;
    border-radius: 3px;
    cursor: pointer;
}

.function-toggle.active {
    background: #2563eb;
    border-color: #2563eb;
}

.function-toggle.active::after {
    content: '✓';
    color: #fff;
    font-size: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.function-remove {
    width: 18px;
    height: 18px;
    background: none;
    border: none;
    color: #999;
    cursor: pointer;
    font-size: 14px;
    border-radius: 3px;
}

.function-remove:hover {
    color: #e53e3e;
    background: #fee;
}

.actions-row {
    display: flex;
    gap: 8px;
}

.actions-row .btn {
    flex: 1;
}

/* ===== Coordinate Display ===== */
#coord-display {
    position: absolute;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 4px 8px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
    z-index: 50;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

#coord-display.visible {
    opacity: 1;
}

#coord-display .coords {
    color: #333;
    font-size: 11px;
    font-family: monospace;
}

#coord-display .fn-label {
    color: #2563eb;
    font-size: 10px;
    margin-top: 2px;
}

/* ===== Dropped Points ===== */
.point-marker {
    position: absolute;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 40;
}

.point-marker .dot {
    width: 10px;
    height: 10px;
    background: #ff6b6b;
    border: 2px solid #fff;
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.point-marker .label {
    position: absolute;
    top: 14px;
    left: 50%;
    transform: translateX(-50%);
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 3px 6px;
    color: #333;
    font-size: 10px;
    font-family: monospace;
    white-space: nowrap;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* ===== Hint ===== */
#hint {
    position: absolute;
    bottom: 12px;
    left: 50%;
    transform: translateX(-50%);
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 16px;
    padding: 6px 12px;
    color: #666;
    font-size: 11px;
    pointer-events: none;
}

/* ===== Floating AI Button ===== */
#ai-button {
    position: absolute;
    bottom: 20px;
    right: 20px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: #fff;
    border: 1px solid #e0e0e0;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    cursor: pointer;
    z-index: 200;
    overflow: hidden;
    transition: transform 0.2s, box-shadow 0.2s;
}

#ai-button:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

#ai-button img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* ===== Chat Panel ===== */
#chat-panel {
    position: absolute;
    bottom: 90px;
    right: 20px;
    width: 450px;
    max-height: 500px;
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    display: none;
    flex-direction: column;
    z-index: 150;
    overflow: hidden;
}

#chat-panel.open {
    display: flex;
}

#chat-header {
    padding: 12px 16px;
    background: #f9f9f9;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    align-items: center;
    gap: 10px;
}

#chat-header img {
    width: 28px;
    height: 28px;
    border-radius: 50%;
}

#chat-header span {
    font-size: 14px;
    font-weight: 600;
    color: #333;
}

#chat-close {
    margin-left: auto;
    background: none;
    border: none;
    font-size: 18px;
    color: #666;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
}

#chat-close:hover {
    background: #eee;
}

#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    max-height: 350px;
    min-height: 150px;
}

.chat-msg {
    margin-bottom: 12px;
    max-width: 90%;
}

.chat-msg.user {
    margin-left: auto;
    text-align: right;
}

.chat-msg .bubble {
    display: inline-block;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.6;
    text-align: left;
}

.chat-msg.user .bubble {
    background: #2563eb;
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chat-msg.ai .bubble {
    background: #f5f5f5;
    color: #333;
    border-bottom-left-radius: 4px;
}

.chat-msg.ai .bubble.typing {
    color: #888;
}

/* Math and Markdown Styling in Chat */
.chat-msg.ai .bubble p {
    margin: 0 0 8px 0;
}

.chat-msg.ai .bubble p:last-child {
    margin-bottom: 0;
}

.chat-msg.ai .bubble ul,
.chat-msg.ai .bubble ol {
    margin: 8px 0;
    padding-left: 20px;
}

.chat-msg.ai .bubble li {
    margin-bottom: 4px;
}

.chat-msg.ai .bubble strong {
    font-weight: 600;
}

.chat-msg.ai .bubble code {
    background: #e8e8e8;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    font-family: 'Monaco', 'Consolas', monospace;
}

.chat-msg.ai .bubble pre {
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
}

.chat-msg.ai .bubble pre code {
    background: none;
    padding: 0;
    color: inherit;
}

/* MathJax styling */
.chat-msg .bubble .MathJax {
    font-size: 105% !important;
}

.chat-msg .bubble mjx-container {
    margin: 4px 0;
}

.chat-msg .bubble mjx-container[display="true"] {
    display: block;
    text-align: center;
    margin: 12px 0;
    overflow-x: auto;
}

#chat-input-row {
    padding: 12px;
    border-top: 1px solid #e0e0e0;
    display: flex;
    gap: 8px;
}

#chat-input {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 8px 14px;
    font-size: 13px;
    outline: none;
}

#chat-input:focus {
    border-color: #2563eb;
}

#chat-send {
    background: #2563eb;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    color: #fff;
    cursor: pointer;
    font-size: 16px;
}

#chat-send:hover {
    background: #1d4ed8;
}

#chat-send:disabled {
    background: #ccc;
    cursor: not-allowed;
}

/* ===== Drawing Toolbar ===== */
#drawing-toolbar {
    position: absolute;
    top: 50%;
    right: 20px;
    transform: translateY(-50%);
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 6px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    z-index: 100;
}

.tool-btn {
    width: 36px;
    height: 36px;
    background: #fff;
    border: 1px solid transparent;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    transition: all 0.15s ease;
}

.tool-btn:hover {
    background: #f5f5f5;
    color: #333;
}

.tool-btn.active {
    background: #2563eb;
    color: #fff;
    border-color: #1d4ed8;
}

.tool-btn.active:hover {
    background: #1d4ed8;
}

.tool-btn svg {
    width: 18px;
    height: 18px;
}

.tool-separator {
    height: 1px;
    background: #e0e0e0;
    margin: 4px 0;
}

/* Responsive adjustments for mobile */
@media (max-width: 768px) {
    #drawing-toolbar {
        top: auto;
        bottom: 80px;
        right: 10px;
        transform: none;
        flex-direction: row;
        padding: 4px;
    }

    .tool-btn {
        width: 32px;
        height: 32px;
    }

    .tool-separator {
        width: 1px;
        height: auto;
        margin: 0 4px;
    }
}