/* ==========================================================================
   CSS VARIABLES & DESIGN SYSTEM
   ========================================================================== */
:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: #eff6ff;
    --secondary-color: #475569;
    --secondary-hover: #334155;
    --bg-color: #f8fafc;
    --card-bg: #ffffff;
    --text-color: #0f172a;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --border-focus: #3b82f6;
    
    --success-color: #16a34a;
    --success-bg: #f0fdf4;
    --warning-color: #d97706;
    --warning-bg: #fffbeb;
    --error-color: #dc2626;
    --error-bg: #fef2f2;
    
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    
    --font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    --max-width: 900px;
}

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

body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
    padding: 1rem;
    min-height: 100vh;
}

.app-container {
    max-width: var(--max-width);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

/* Header */
.app-header {
    background: var(--card-bg);
    padding: 1.25rem 1.5rem;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 0.85rem;
}

.brand-icon {
    font-size: 2rem;
    background: var(--primary-light);
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
}

.app-header h1 {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--text-color);
}

.subtitle {
    font-size: 0.875rem;
    color: var(--text-muted);
}

/* Status Bar */
.status-bar {
    background: var(--card-bg);
    padding: 0.65rem 1.25rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.875rem;
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 500;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
}

.dot-green { background-color: var(--success-color); box-shadow: 0 0 6px var(--success-color); }
.dot-yellow { background-color: var(--warning-color); box-shadow: 0 0 6px var(--warning-color); }
.dot-red { background-color: var(--error-color); box-shadow: 0 0 6px var(--error-color); }
.dot-gray { background-color: var(--text-muted); }

/* Error Panel */
.error-panel {
    background-color: var(--error-bg);
    border: 1px solid #fca5a5;
    border-radius: var(--radius-md);
    padding: 1rem 1.25rem;
    color: #991b1b;
}

.error-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.35rem;
}

.error-header strong { flex-grow: 1; }

.error-body {
    font-size: 0.9rem;
    margin-bottom: 0.75rem;
    white-space: pre-wrap;
}

.error-actions {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.tech-details {
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid #f87171;
}

.tech-details pre {
    background: #450a0a;
    color: #fecaca;
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    font-size: 0.8rem;
    overflow-x: auto;
    margin-bottom: 0.5rem;
}

/* Cards & Layout */
.card {
    background: var(--card-bg);
    padding: 1.5rem;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}

.card h2 {
    font-size: 1.15rem;
    margin-bottom: 1.25rem;
    color: var(--text-color);
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.5rem;
}

/* Form Controls */
.form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
    margin-bottom: 1rem;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.form-group.full-width {
    grid-column: 1 / -1;
}

label {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-color);
}

.required { color: var(--error-color); }
.optional { font-weight: normal; color: var(--text-muted); font-size: 0.8rem; }

input[type="text"],
input[type="password"],
select,
textarea {
    width: 100%;
    padding: 0.65rem 0.85rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-size: 0.95rem;
    color: var(--text-color);
    background-color: #fff;
    transition: border-color 0.2s, box-shadow 0.2s;
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--border-focus);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

textarea { resize: vertical; }

.textarea-footer {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    margin-top: 0.25rem;
}

.char-counter {
    color: var(--text-muted);
    margin-left: auto;
}

.field-error {
    color: var(--error-color);
    font-size: 0.8rem;
    font-weight: 500;
}

.field-info {
    font-size: 0.8rem;
    margin-top: 0.25rem;
    display: block;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.65rem 1.25rem;
    font-size: 0.95rem;
    font-weight: 600;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s;
    text-decoration: none;
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
}
.btn-primary:hover:not(:disabled) { background-color: var(--primary-hover); }

.btn-secondary {
    background-color: #f1f5f9;
    color: var(--text-color);
    border-color: var(--border-color);
}
.btn-secondary:hover:not(:disabled) { background-color: #e2e8f0; }

.btn-outline {
    background-color: transparent;
    color: var(--primary-color);
    border-color: var(--primary-color);
}
.btn-outline:hover:not(:disabled) { background-color: var(--primary-light); }

.btn-danger-outline {
    background-color: transparent;
    color: var(--error-color);
    border-color: #fca5a5;
}
.btn-danger-outline:hover:not(:disabled) { background-color: var(--error-bg); }

.btn-text {
    background: none;
    border: none;
    color: var(--text-muted);
    text-decoration: underline;
    padding: 0;
    cursor: pointer;
}

.btn-large {
    padding: 0.85rem 1.75rem;
    font-size: 1.05rem;
    width: 100%;
}

.btn-sm {
    padding: 0.35rem 0.75rem;
    font-size: 0.85rem;
}

.btn-icon {
    background: none;
    border: none;
    font-size: 1.25rem;
    cursor: pointer;
    padding: 0.2rem 0.4rem;
    border-radius: var(--radius-sm);
}
.btn-icon:hover { background-color: rgba(0,0,0,0.05); }

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Chat Section */
.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.badge {
    background: var(--primary-light);
    color: var(--primary-color);
    padding: 0.25rem 0.65rem;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
}

.chat-messages {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    max-height: 600px;
    overflow-y: auto;
    padding-right: 0.5rem;
    margin-bottom: 1.25rem;
}

.message-bubble {
    display: flex;
    flex-direction: column;
    max-width: 90%;
    border-radius: var(--radius-md);
    padding: 1rem 1.25rem;
    position: relative;
    line-height: 1.6;
}

.message-bubble.user {
    align-self: flex-end;
    background-color: var(--primary-color);
    color: white;
    border-bottom-right-radius: 2px;
}

.message-bubble.assistant {
    align-self: flex-start;
    background-color: #f1f5f9;
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-bottom-left-radius: 2px;
    width: 100%;
}

.message-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.75rem;
    margin-bottom: 0.5rem;
    opacity: 0.8;
    padding-bottom: 0.35rem;
    border-bottom: 1px solid rgba(0,0,0,0.08);
}

.message-bubble.user .message-header {
    border-bottom: 1px solid rgba(255,255,255,0.2);
}

.message-actions {
    display: flex;
    gap: 0.5rem;
}

.btn-copy {
    background: rgba(0,0,0,0.05);
    border: none;
    font-size: 0.75rem;
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    cursor: pointer;
    color: inherit;
}
.message-bubble.user .btn-copy { background: rgba(255,255,255,0.2); }
.btn-copy:hover { opacity: 0.8; }

/* Markdown Formatting */
.message-content h2, .message-content h3 {
    margin-top: 1rem;
    margin-bottom: 0.5rem;
    color: var(--primary-color);
}

.message-content h2 { font-size: 1.1rem; }
.message-content h3 { font-size: 1rem; }
.message-content p { margin-bottom: 0.65rem; }
.message-content p:last-child { margin-bottom: 0; }

.message-content ul, .message-content ol {
    margin-left: 1.25rem;
    margin-bottom: 0.75rem;
}

.message-content li { margin-bottom: 0.25rem; }

.message-content code {
    background: #e2e8f0;
    padding: 0.1rem 0.3rem;
    border-radius: 3px;
    font-family: monospace;
    font-size: 0.9em;
}

.message-content pre {
    background: #1e293b;
    color: #f8fafc;
    padding: 0.75rem;
    border-radius: var(--radius-sm);
    overflow-x: auto;
    margin: 0.5rem 0;
}

.message-content pre code {
    background: transparent;
    color: inherit;
}

/* Chat Input Area */
.chat-input-area {
    display: flex;
    gap: 0.75rem;
    align-items: flex-end;
}

.chat-input-area textarea {
    flex-grow: 1;
}

/* Loading Box */
.loading-box {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.85rem;
    background-color: var(--primary-light);
    border-radius: var(--radius-md);
    color: var(--primary-color);
    font-weight: 500;
    margin-bottom: 1rem;
}

.spinner {
    width: 20px;
    height: 20px;
    border: 3px solid rgba(37, 99, 235, 0.2);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* Modal */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
}

.modal-content {
    background: var(--card-bg);
    width: 100%;
    max-width: 550px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.modal-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h2 { font-size: 1.15rem; }

.modal-body {
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.modal-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--border-color);
    background: #f8fafc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.modal-footer-right {
    display: flex;
    gap: 0.5rem;
}

.notice-box {
    padding: 0.85rem;
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
}

.notice-info {
    background-color: var(--primary-light);
    border: 1px solid #bfdbfe;
    color: #1e40af;
}

.small-text {
    font-size: 0.78rem;
    margin-top: 0.4rem;
    opacity: 0.85;
}

.password-input-wrapper {
    display: flex;
    gap: 0.35rem;
}

.external-link {
    color: var(--primary-color);
    font-weight: 600;
    text-decoration: none;
    display: inline-block;
    margin-top: 0.35rem;
}

.external-link:hover { text-decoration: underline; }

.app-footer {
    text-align: center;
    font-size: 0.8rem;
    color: var(--text-muted);
    padding: 1rem 0;
}

.hidden { display: none !important; }

@media (max-width: 640px) {
    .form-grid { grid-template-columns: 1fr; }
    .app-header { flex-direction: column; align-items: flex-start; }
    .app-header button { align-self: flex-end; }
    .modal-footer { flex-direction: column-reverse; align-items: stretch; }
    .modal-footer-right { flex-direction: column; }
    .btn-large { font-size: 1rem; }
    .message-bubble { max-width: 95%; }
}