/* ============================================================
   Agent Platform
   ============================================================ */

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f5f5f5;
    color: #333;
    line-height: 1.6;
}

.page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Auth container */
.auth-container {
    background: white;
    border-radius: 12px;
    padding: 48px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.08);
    text-align: center;
    max-width: 400px;
    width: 90%;
}
.auth-container h1 { font-size: 24px; font-weight: 600; margin-bottom: 8px; }
.subtitle { color: #666; margin-bottom: 32px; }
.auth-link { margin-top: 16px; font-size: 14px; color: #666; }
.auth-link a { color: #2563eb; text-decoration: none; }
.auth-link a:hover { text-decoration: underline; }

/* Buttons */
.btn {
    display: inline-block;
    padding: 10px 24px;
    border-radius: 8px;
    border: none;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
}
.btn-primary { background: #2563eb; color: white; }
.btn-primary:hover { background: #1d4ed8; }
.btn-secondary { background: #e5e7eb; color: #333; }
.btn-secondary:hover { background: #d1d5db; }
.btn-small { padding: 6px 16px; font-size: 13px; }
.btn-danger { background: #ef4444; color: white; }
.btn-danger:hover { background: #dc2626; }

/* Spinner */
.spinner {
    width: 32px; height: 32px;
    border: 3px solid #e5e7eb;
    border-top-color: #2563eb;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 16px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Top bar */
.topbar {
    background: white;
    border-bottom: 1px solid #e5e7eb;
    padding: 12px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: fixed;
    top: 0; left: 0; right: 0;
    z-index: 100;
}
.topbar h1 { font-size: 18px; font-weight: 600; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-email { font-size: 14px; color: #666; }

/* Dashboard layout */
#page-dashboard, #page-create-agent, #page-agent-detail, #page-chat {
    display: none;
    flex-direction: column;
    min-height: 100vh;
    align-items: stretch;
    justify-content: flex-start;
}

.dashboard {
    padding: 72px 24px 24px;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
}

.dashboard-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}
.dashboard-header h2 { font-size: 20px; }

/* Agent cards */
.agent-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 16px;
}

.agent-card {
    background: white;
    border-radius: 10px;
    padding: 20px 12px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    cursor: pointer;
    transition: box-shadow 0.2s, transform 0.1s;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    aspect-ratio: 1 / 1;
    text-align: center;
}
.agent-card:hover {
    box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    transform: translateY(-2px);
}

.agent-card-avatar-img {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    object-fit: cover;
    background: #e5e7eb;
    flex-shrink: 0;
}

.agent-card-avatar-placeholder {
    width: 72px;
    height: 72px;
    border-radius: 50%;
    background: #6366f1;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 28px;
    user-select: none;
    flex-shrink: 0;
}

.agent-card-name {
    font-size: 14px;
    font-weight: 600;
    color: #1f2937;
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    word-break: break-word;
}

.badge {
    background: #eff6ff;
    color: #2563eb;
    padding: 2px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
}

/* Session cards */
.session-list { display: flex; flex-direction: column; gap: 8px; }

.session-card {
    background: white;
    border-radius: 8px;
    padding: 16px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    transition: box-shadow 0.2s;
}
.session-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
.session-card h4 { font-size: 14px; margin-bottom: 4px; }
.session-card-body { flex: 1; min-width: 0; }
.session-meta { font-size: 12px; color: #999; }

.session-delete {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    background: transparent;
    color: #9ca3af;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex-shrink: 0;
    opacity: 0;
    transition: opacity 0.15s, background-color 0.15s, color 0.15s;
}
.session-card:hover .session-delete { opacity: 0.7; }
.session-delete:hover {
    background: #fee2e2;
    color: #dc2626;
    opacity: 1 !important;
}

/* Agent info */
.agent-info {
    background: white;
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 24px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}
.agent-info p { color: #666; margin-bottom: 8px; }
.agent-meta { font-size: 13px; color: #999; display: flex; gap: 16px; }

/* Forms */
.form-card {
    background: white;
    border-radius: 12px;
    padding: 32px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}
.form-card h2 { margin-bottom: 24px; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 14px; font-weight: 500; margin-bottom: 4px; }
.form-group input, .form-group textarea, .form-group select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
}
.form-group textarea { resize: vertical; }
.form-actions { display: flex; gap: 12px; justify-content: flex-end; margin-top: 24px; }

/* Chat */
.chat-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 52px);
    margin-top: 52px;
}
.chat-messages {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
}
.chat-input-area {
    display: flex;
    gap: 8px;
    padding: 16px 24px;
    border-top: 1px solid #e5e7eb;
    background: white;
    align-items: flex-end;
}
.chat-input-area textarea {
    flex: 1;
    padding: 10px 12px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    resize: vertical;
    min-height: 44px;
    max-height: 200px;
}

/* Empty state */
.empty-state { color: #999; text-align: center; padding: 32px; }

/* Messages */
.message { margin-bottom: 16px; padding: 12px 16px; border-radius: 10px; }
.msg-human { background: #eff6ff; margin-left: 40px; }
.msg-assistant { background: white; margin-right: 40px; box-shadow: 0 1px 4px rgba(0,0,0,0.06); }
.msg-origin { font-size: 12px; font-weight: 600; color: #666; margin-bottom: 4px; }
.msg-text { white-space: pre-wrap; word-break: break-word; }
.msg-thinking { color: #999; font-style: italic; font-size: 13px; margin-top: 4px; }
.msg-tool-call {
    background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 6px;
    padding: 8px 12px; margin-top: 8px; font-size: 13px;
}
.msg-tool-call pre { margin-top: 4px; font-size: 12px; overflow-x: auto; }
.msg-tool-result {
    background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 6px;
    padding: 8px 12px; margin-top: 4px; font-size: 13px;
}
.msg-tool-result pre { font-size: 12px; overflow-x: auto; }

/* Turn status badge */
.turn-status {
    font-size: 12px; padding: 2px 10px; border-radius: 12px;
    background: #fef3c7; color: #92400e;
}
.turn-status:empty { display: none; }

/* Credentials */
.cred-list { display: flex; flex-direction: column; gap: 10px; }
.cred-card {
    background: white; border-radius: 8px; padding: 16px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    display: flex; align-items: center; gap: 12px;
}
.cred-card-header { display: flex; align-items: center; gap: 8px; min-width: 200px; }
.cred-card-header h4 { font-size: 14px; }
.cred-detail { flex: 1; font-size: 13px; color: #666; font-family: monospace; }
.field-hint { font-size: 12px; color: #999; margin-top: 4px; }
.field-hint a { color: #2563eb; }
.dashboard-actions { display: flex; gap: 8px; }

/* Page credentials layout */
#page-credentials { display: none; flex-direction: column; min-height: 100vh; align-items: stretch; justify-content: flex-start; }

#page-edit-agent { display: none; flex-direction: column; min-height: 100vh; align-items: stretch; justify-content: flex-start; }


/* ============================================================
   Avatars
   ============================================================ */

.avatar-img {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    object-fit: cover;
    display: block;
    background: #e5e7eb;
}

.avatar-img-lg {
    width: 96px;
    height: 96px;
    border-radius: 50%;
    object-fit: cover;
    display: block;
    background: #e5e7eb;
}

.avatar-placeholder {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: #6366f1;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 20px;
    user-select: none;
}

.avatar-placeholder-lg {
    width: 96px;
    height: 96px;
    font-size: 36px;
}

/* Agent detail header */
.agent-detail-header {
    display: flex;
    gap: 24px;
    align-items: flex-start;
    margin-bottom: 16px;
}

.agent-detail-avatar {
    flex-shrink: 0;
}

/* Avatar selector (in forms) */
.avatar-selector {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 16px;
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
}

.avatar-selector-none {
    display: flex;
    gap: 12px;
    align-items: center;
}

.avatar-section h4 {
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #6b7280;
    margin-bottom: 8px;
    font-weight: 600;
}

.avatar-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
    gap: 8px;
}

.avatar-option {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 6px;
    border: 2px solid transparent;
    border-radius: 10px;
    cursor: pointer;
    transition: border-color 0.1s, background-color 0.1s;
}

.avatar-option:hover {
    background: #eef2ff;
}

.avatar-option.selected {
    border-color: #4f46e5;
    background: #eef2ff;
}

.avatar-option .avatar-img,
.avatar-option .avatar-placeholder {
    width: 56px;
    height: 56px;
    font-size: 20px;
}

.avatar-label {
    font-size: 11px;
    color: #6b7280;
}

.avatar-plus {
    background: #e5e7eb;
    color: #6b7280;
    font-size: 28px;
    font-weight: 300;
}

.avatar-upload {
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 6px;
    border: 2px dashed #d1d5db;
    border-radius: 10px;
    transition: border-color 0.1s, background-color 0.1s;
}

.avatar-upload:hover {
    border-color: #4f46e5;
    background: #eef2ff;
}

.avatar-delete {
    position: absolute;
    top: 0;
    right: 0;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: rgba(220, 38, 38, 0.9);
    color: white;
    border: none;
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.avatar-option:hover .avatar-delete {
    display: flex;
}


/* ============================================================
   Message rendering (markdown + tool cards)
   ============================================================ */

/* Markdown content inside assistant messages */
.msg-text-assistant {
    line-height: 1.6;
}
.msg-text-assistant p {
    margin: 0 0 12px;
}
.msg-text-assistant p:last-child {
    margin-bottom: 0;
}
.msg-text-assistant h1,
.msg-text-assistant h2,
.msg-text-assistant h3,
.msg-text-assistant h4 {
    margin: 20px 0 8px;
    font-weight: 600;
    line-height: 1.25;
}
.msg-text-assistant h1 { font-size: 1.5em; }
.msg-text-assistant h2 { font-size: 1.3em; }
.msg-text-assistant h3 { font-size: 1.15em; }
.msg-text-assistant h4 { font-size: 1em; }

.msg-text-assistant ul,
.msg-text-assistant ol {
    margin: 0 0 12px;
    padding-left: 28px;
}
.msg-text-assistant li {
    margin: 2px 0;
}
.msg-text-assistant li > p {
    margin: 0 0 4px;
}

.msg-text-assistant blockquote {
    margin: 0 0 12px;
    padding: 6px 14px;
    border-left: 3px solid #d1d5db;
    color: #4b5563;
    background: #f9fafb;
    border-radius: 0 6px 6px 0;
}

.msg-text-assistant a {
    color: #4f46e5;
    text-decoration: none;
}
.msg-text-assistant a:hover {
    text-decoration: underline;
}

.msg-text-assistant hr {
    border: none;
    border-top: 1px solid #e5e7eb;
    margin: 16px 0;
}

.msg-text-assistant table {
    border-collapse: collapse;
    margin: 0 0 12px;
    font-size: 14px;
}
.msg-text-assistant th,
.msg-text-assistant td {
    padding: 6px 12px;
    border: 1px solid #e5e7eb;
    text-align: left;
}
.msg-text-assistant th {
    background: #f3f4f6;
    font-weight: 600;
}

/* Inline code */
.msg-text-assistant code {
    background: #f3f4f6;
    color: #be185d;
    padding: 1px 6px;
    border-radius: 4px;
    font-family: ui-monospace, 'SF Mono', Monaco, Menlo, Consolas, monospace;
    font-size: 0.9em;
}

/* Code block: highlight.js styles github-dark theme */
.msg-text-assistant pre {
    background: #0d1117;          /* matches github-dark */
    padding: 14px 16px;
    border-radius: 8px;
    margin: 0 0 12px;
    overflow-x: auto;
    font-size: 13px;
    line-height: 1.5;
}
.msg-text-assistant pre code {
    background: transparent;
    color: #e6edf3;               /* matches github-dark foreground */
    padding: 0;
    border-radius: 0;
    font-size: 13px;
}

/* Human messages: keep plain but readable */
.msg-text-human {
    line-height: 1.5;
    word-wrap: break-word;
}

/* Tool call card */
.msg-tool-call,
.msg-tool-result {
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    margin: 8px 0;
    font-size: 13px;
    overflow: hidden;
}
.msg-tool-call summary,
.msg-tool-result summary {
    padding: 8px 12px;
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    gap: 8px;
    color: #374151;
    list-style: none;
}
.msg-tool-call summary::-webkit-details-marker,
.msg-tool-result summary::-webkit-details-marker { display: none; }
.msg-tool-call summary::before,
.msg-tool-result summary::before {
    content: '▸';
    display: inline-block;
    transition: transform 0.15s;
    color: #9ca3af;
    font-size: 10px;
}
.msg-tool-call[open] summary::before,
.msg-tool-result[open] summary::before {
    transform: rotate(90deg);
}

.tool-icon {
    color: #6366f1;
    font-weight: 600;
}
.msg-tool-result .tool-icon {
    color: #059669;
}
.msg-tool-error .tool-icon {
    color: #dc2626;
}

.tool-label {
    font-weight: 500;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-size: 11px;
}

.tool-name {
    background: #eef2ff;
    color: #4338ca;
    padding: 1px 8px;
    border-radius: 4px;
    font-family: ui-monospace, 'SF Mono', Monaco, Menlo, Consolas, monospace;
    font-size: 12px;
    font-weight: 500;
}

.tool-input,
.tool-output {
    margin: 0;
    padding: 10px 14px;
    background: #ffffff;
    border-top: 1px solid #e5e7eb;
    overflow-x: auto;
    font-family: ui-monospace, 'SF Mono', Monaco, Menlo, Consolas, monospace;
    font-size: 12px;
    line-height: 1.5;
    color: #1f2937;
    max-height: 400px;
    overflow-y: auto;
}
.tool-input code,
.tool-output code {
    background: transparent;
    color: inherit;
    padding: 0;
    font-size: 12px;
}

.msg-tool-error {
    background: #fef2f2;
    border-color: #fecaca;
}
.msg-tool-error .tool-output {
    background: #fef2f2;
    color: #991b1b;
}

/* Thinking block: muted styling */
.msg-thinking {
    background: #fafaf9;
    border: 1px solid #e7e5e4;
    border-radius: 8px;
    margin: 8px 0;
    font-size: 13px;
    overflow: hidden;
}
.msg-thinking summary {
    padding: 8px 12px;
    cursor: pointer;
    color: #6b7280;
    font-style: italic;
    font-size: 12px;
    user-select: none;
}
.msg-thinking-body {
    padding: 10px 14px;
    border-top: 1px solid #e7e5e4;
    color: #4b5563;
}


/* ============================================================
   Confirmation modal
   ============================================================ */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(17, 24, 39, 0.5);
    backdrop-filter: blur(2px);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    animation: modal-fade-in 0.15s ease-out;
}

@keyframes modal-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.modal {
    background: white;
    border-radius: 12px;
    padding: 28px;
    max-width: 480px;
    width: calc(100% - 40px);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
    animation: modal-pop-in 0.18s ease-out;
}

@keyframes modal-pop-in {
    from { transform: scale(0.96); opacity: 0; }
    to   { transform: scale(1); opacity: 1; }
}

.modal h3 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 12px;
    color: #1f2937;
}

.modal-body {
    color: #4b5563;
    font-size: 14px;
    line-height: 1.6;
    margin-bottom: 20px;
}

.modal-body p {
    margin: 0 0 10px;
}
.modal-body p:last-child {
    margin-bottom: 0;
}

.modal-body strong {
    color: #111827;
    font-weight: 600;
}

.modal-warning {
    padding: 10px 12px;
    background: #fef3c7;
    border-left: 3px solid #f59e0b;
    border-radius: 4px;
    color: #78350f;
    font-size: 13px;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}
