/* ==========================================================================
   QuantaBible component library
   Shared building blocks — feature templates should use these instead of
   re-declaring their own buttons/cards/forms/modals/toasts.
   Requires tokens.css (+ base.css) first.

   Components: cards, buttons, badges/alerts, forms, tabs, modal, toasts,
   skeletons, spinner, empty state, stat card, section header.
   ========================================================================== */

/* ===== CARD SYSTEM ===== */
.card-base { background: var(--bg-card); border: 1px solid var(--border-light); border-radius: var(--radius-lg); padding: 1.25rem; transition: box-shadow 0.2s ease, transform 0.2s ease, border-color 0.2s ease; }
.card-interactive { background: var(--bg-card); border: 1px solid var(--border-light); border-radius: var(--radius-lg); padding: 1.25rem; transition: box-shadow 0.2s ease, transform 0.2s ease, border-color 0.2s ease; cursor: pointer; }
.card-interactive:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); border-color: var(--border-medium); }
.card-content { background: var(--bg-card); border: 1px solid var(--border-light); border-radius: var(--radius-lg); max-width: 42rem; margin: 0 auto; padding: 2rem; }
.card-chat { background: transparent; border: none; padding: 0.75rem 1rem; border-radius: var(--radius-sm); }
.card-chat:hover { background: rgba(0,0,0,0.02); }
[data-theme="dark"] .card-chat:hover { background: rgba(255,255,255,0.04); }
.card-stat { background: var(--bg-card); border: 1px solid var(--border-light); border-radius: var(--radius-lg); text-align: center; padding: 1rem; }
.card-stat .stat-icon { display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; border-radius: var(--radius-md); background: var(--color-accent-light); color: var(--color-accent); margin-bottom: var(--space-2); }
.card-stat .stat-value { font-family: var(--font-heading); font-size: var(--text-2xl); font-weight: 700; color: var(--text-heading); line-height: 1.1; }
.card-stat .stat-label { font-size: var(--text-xs); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); margin-top: var(--space-1); }

/* ===== BUTTON SYSTEM ===== */
.btn-primary { background: var(--brand-olive); color: #fff; border: none; border-radius: 8px; padding: 0.625rem 1.25rem; font-weight: 500; font-size: var(--text-sm); font-family: var(--font-body); transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease; box-shadow: var(--shadow-sm); cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem; }
.btn-primary:hover { background: var(--brand-olive-hover); box-shadow: var(--shadow-md); transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); box-shadow: none; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }
.btn-secondary { background: transparent; color: var(--text-primary); border: 1px solid var(--border-medium); border-radius: 8px; padding: 0.625rem 1.25rem; font-weight: 500; font-size: var(--text-sm); font-family: var(--font-body); transition: background 0.15s ease, border-color 0.15s ease; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem; }
.btn-secondary:hover { background: var(--bg-card-hover); border-color: var(--brand-gold); }
.btn-secondary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-ghost { background: transparent; color: var(--text-secondary); border: none; padding: 0.5rem 0.75rem; border-radius: var(--radius-sm); font-size: var(--text-sm); font-family: var(--font-body); transition: background 0.15s ease, color 0.15s ease; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem; }
.btn-ghost:hover { background: rgba(0,0,0,0.04); color: var(--text-primary); }
[data-theme="dark"] .btn-ghost:hover { background: rgba(255,255,255,0.06); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); border-radius: 8px; padding: 0.625rem 1.25rem; font-weight: 500; font-size: var(--text-sm); font-family: var(--font-body); transition: background 0.15s ease; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem; }
.btn-danger:hover { background: var(--danger-bg); }
/* Size variants — compose with any .btn-* variant */
.btn-sm { padding: 0.375rem 0.75rem; font-size: var(--text-xs); border-radius: var(--radius-sm); }
.btn-lg { padding: 0.875rem 1.75rem; font-size: var(--text-base); border-radius: var(--radius-md); }
.btn-block { display: flex; width: 100%; }
/* Icon-only button (toolbars, modal close, etc.) */
.btn-icon { background: transparent; border: none; color: var(--text-secondary); cursor: pointer; padding: 6px; border-radius: var(--radius-sm); display: inline-flex; align-items: center; justify-content: center; transition: background 0.15s ease, color 0.15s ease; }
.btn-icon:hover { background: var(--bg-sidebar); color: var(--text-primary); }

/* ===== BADGES & ALERTS (semantic, theme-aware) ===== */
.badge { display: inline-flex; align-items: center; gap: 0.35rem; padding: 0.2rem 0.6rem; border-radius: var(--radius-full); font-size: var(--text-xs); font-weight: 600; line-height: 1.4; }
.badge-success { background: var(--success-bg); color: var(--success); }
.badge-warning { background: var(--warning-bg); color: var(--warning); }
.badge-danger  { background: var(--danger-bg);  color: var(--danger); }
.badge-info    { background: var(--info-bg);    color: var(--info); }
.badge-neutral { background: var(--bg-sidebar); color: var(--text-secondary); }
.badge-accent  { background: var(--color-accent-light); color: var(--color-accent); }

.alert { display: flex; align-items: flex-start; gap: 0.6rem; padding: 0.75rem 1rem; border-radius: var(--radius-md); font-size: var(--text-sm); border: 1px solid transparent; }
.alert .lucide { width: 18px; height: 18px; margin-top: 1px; }
.alert-success { background: var(--success-bg); color: var(--success); border-color: var(--success); }
.alert-warning { background: var(--warning-bg); color: var(--warning); border-color: var(--warning); }
.alert-danger  { background: var(--danger-bg);  color: var(--danger);  border-color: var(--danger); }
.alert-info    { background: var(--info-bg);    color: var(--info);    border-color: var(--info); }

/* Status dot (service status, presence) */
.status-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--text-muted); }
.status-dot.is-ok   { background: var(--success); }
.status-dot.is-warn { background: var(--warning); }
.status-dot.is-err  { background: var(--danger); }

/* ===== FORMS ===== */
.field { display: flex; flex-direction: column; gap: 0.35rem; margin-bottom: var(--space-4); }
.field-label { font-size: var(--text-sm); font-weight: 600; color: var(--text-primary); }
.field-hint { font-size: var(--text-xs); color: var(--text-muted); }
.field-error { font-size: var(--text-xs); color: var(--danger); font-weight: 500; }
.input, .select, .textarea {
    width: 100%;
    padding: 0.625rem 0.875rem;
    border: 1px solid var(--border-medium);
    border-radius: 8px;
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: var(--text-sm);
    font-family: var(--font-body);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.input::placeholder, .textarea::placeholder { color: var(--text-muted); }
.input:focus, .select:focus, .textarea:focus {
    outline: none;
    border-color: var(--color-accent);
    box-shadow: var(--focus-ring);
}
.input.has-error, .select.has-error, .textarea.has-error { border-color: var(--danger); }
.input:disabled, .select:disabled, .textarea:disabled { opacity: 0.6; cursor: not-allowed; }
.textarea { min-height: 96px; resize: vertical; line-height: var(--line-height-normal); }
.input[type="checkbox"], .input[type="radio"] { width: auto; accent-color: var(--color-accent); }

/* ===== TABS ===== */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border-light); }
.tabs .tab { padding: 0.625rem 1rem; font-size: var(--text-sm); font-weight: 500; font-family: var(--font-body); color: var(--text-secondary); background: none; border: none; border-bottom: 2px solid transparent; cursor: pointer; text-decoration: none; transition: color 0.15s ease, border-color 0.15s ease; }
.tabs .tab:hover { color: var(--text-primary); }
.tabs .tab.active { color: var(--color-accent); border-bottom-color: var(--color-accent); font-weight: 600; }

/* ===== MODAL ===== */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 9999; justify-content: center; align-items: center; padding: var(--space-4); }
.modal-overlay.open { display: flex; }
.modal { background: var(--bg-card); border: 1px solid var(--border-light); border-radius: var(--radius-xl); box-shadow: var(--shadow-lg); width: 100%; max-width: 480px; max-height: 85vh; overflow-y: auto; position: relative; padding: var(--space-6); }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-4); }
.modal-title { font-family: var(--font-heading); font-size: var(--text-xl); color: var(--text-heading); margin: 0; }
.modal-body { font-size: var(--text-sm); color: var(--text-primary); }
.modal-footer { display: flex; justify-content: flex-end; gap: var(--space-2); margin-top: var(--space-5); }

/* ===== TOASTS ===== */
.toast-container { position: fixed; bottom: var(--space-5); left: 50%; transform: translateX(-50%); z-index: 10000; display: flex; flex-direction: column; align-items: center; gap: var(--space-2); pointer-events: none; width: min(92vw, 420px); }
.toast { display: flex; align-items: center; gap: 0.6rem; background: var(--bg-card); color: var(--text-primary); border: 1px solid var(--border-light); border-radius: var(--radius-md); box-shadow: var(--shadow-lg); padding: 0.7rem 1rem; font-size: var(--text-sm); font-family: var(--font-body); pointer-events: auto; width: 100%; opacity: 0; transform: translateY(8px); transition: opacity 0.2s ease, transform 0.2s ease; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast .lucide { width: 18px; height: 18px; flex-shrink: 0; }
.toast-success { border-left: 3px solid var(--success); }
.toast-success .lucide { color: var(--success); }
.toast-warning { border-left: 3px solid var(--warning); }
.toast-warning .lucide { color: var(--warning); }
.toast-danger { border-left: 3px solid var(--danger); }
.toast-danger .lucide { color: var(--danger); }
.toast-info { border-left: 3px solid var(--info); }
.toast-info .lucide { color: var(--info); }
.toast-accent { border-left: 3px solid var(--color-accent); }
.toast-accent .lucide { color: var(--color-accent); }

/* ===== SKELETONS ===== */
.skeleton { position: relative; overflow: hidden; background: var(--bg-sidebar); border-radius: var(--radius-sm); }
.skeleton::after { content: ''; position: absolute; inset: 0; transform: translateX(-100%); background: linear-gradient(90deg, transparent, rgba(255,255,255,0.35), transparent); animation: skeleton-shimmer 1.4s infinite; }
[data-theme="dark"] .skeleton::after { background: linear-gradient(90deg, transparent, rgba(255,255,255,0.08), transparent); }
@keyframes skeleton-shimmer { 100% { transform: translateX(100%); } }
.skeleton-text { height: 0.9em; margin-bottom: 0.5em; }
.skeleton-circle { border-radius: 50%; }

/* ===== SPINNER ===== */
.spinner { display: inline-block; width: 24px; height: 24px; border: 3px solid var(--border-light); border-top-color: var(--color-accent); border-radius: 50%; animation: qb-spin 0.7s linear infinite; }
.spinner-sm { width: 16px; height: 16px; border-width: 2px; }
@keyframes qb-spin { to { transform: rotate(360deg); } }

/* ===== EMPTY STATE ===== */
.empty-state { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 3rem 1.5rem; text-align: center; }
.empty-state .lucide { width: 48px; height: 48px; color: var(--text-muted); margin-bottom: 1rem; }
.empty-state h3 { font-family: var(--font-heading); font-size: var(--text-xl); color: var(--text-heading); margin-bottom: 0.5rem; }
.empty-state p { color: var(--text-secondary); font-size: var(--text-sm); max-width: 28rem; }

/* ===== SECTION HEADER ===== */
.section-header { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 1rem; padding-bottom: 0.5rem; border-bottom: 1px solid var(--border-light); }
.section-header h2 { font-size: var(--text-lg); font-weight: 600; color: var(--text-heading); margin: 0; text-transform: none; letter-spacing: normal; font-family: var(--font-body); }
.section-header .icon { color: var(--brand-gold); }

/* ===== PAGE HEADER (standard page top: title / subtitle / actions) ===== */
.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--space-4); flex-wrap: wrap; margin-bottom: var(--space-5); }
.page-header .page-title { margin: 0; }
.page-header .page-subtitle { color: var(--text-secondary); font-size: var(--text-sm); margin-top: var(--space-1); }
.page-header .page-actions { display: flex; gap: var(--space-2); flex-shrink: 0; }

/* ===== CHIP (filter pills, tags, preset pickers) ===== */
.chip { display: inline-flex; align-items: center; gap: 0.35rem; padding: 0.35rem 0.85rem; border-radius: var(--radius-full); border: 1px solid var(--border-medium); background: var(--bg-card); color: var(--text-secondary); font-size: var(--text-xs); font-weight: 500; font-family: var(--font-body); cursor: pointer; text-decoration: none; transition: all 0.15s ease; }
.chip:hover { border-color: var(--color-accent); color: var(--color-accent); }
.chip.active, .chip[aria-pressed="true"] { background: var(--color-accent-light); border-color: var(--color-accent); color: var(--color-accent); font-weight: 600; }

/* ===== AVATAR ===== */
.avatar { display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 32px; border-radius: 50%; background: var(--brand-olive-light); color: var(--brand-olive); font-weight: 600; font-size: 13px; text-transform: uppercase; flex-shrink: 0; }
.avatar-lg { width: 48px; height: 48px; font-size: 17px; }

/* ===== CHAT MESSAGES (shared: AI teacher, group chat) ===== */
.chat-msg { display: flex; gap: 0.8em; line-height: 1.65; }
.chat-msg-avatar {
    width: 34px; height: 34px; border-radius: 50%;
    flex-shrink: 0; display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 0.72em; color: #fff; margin-top: 0.15em;
    background: var(--brand-olive);
}
.chat-msg.is-user .chat-msg-avatar { background: var(--color-accent); }
.chat-msg.is-assistant .chat-msg-avatar { background: var(--color-cta); }
.chat-msg-content { flex: 1; min-width: 0; }
.chat-msg-role {
    font-size: 0.73em; font-weight: 700; color: var(--color-text-secondary);
    margin-bottom: 0.2em; font-family: var(--font-ui);
    text-transform: uppercase; letter-spacing: 0.03em;
}
.chat-msg-text {
    font-family: var(--font-reading); font-size: 1em;
    color: var(--color-text-primary); word-wrap: break-word;
}
.chat-msg.is-user .chat-msg-text { white-space: pre-wrap; }
.chat-msg.is-assistant .chat-msg-text {
    background: var(--color-surface); padding: 1em 1.2em;
    border-radius: 14px; border: 1px solid var(--color-border);
    box-shadow: var(--shadow-sm);
}
.chat-msg-text p { margin: 0 0 0.8em 0; }
.chat-msg-text p:last-child { margin-bottom: 0; }

/* Typing / pending indicator */
.chat-typing { display: inline-flex; gap: 4px; padding: 0.4em 0; }
.chat-typing span {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--text-muted); animation: chat-typing-bounce 1.2s infinite;
}
.chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.chat-typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes chat-typing-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
    30% { transform: translateY(-4px); opacity: 1; }
}

/* ===== COMMAND PALETTE (Cmd/Ctrl+K search) ===== */
.cmdk-overlay {
    display: none; position: fixed; inset: 0; z-index: 10001;
    background: rgba(20, 16, 10, 0.45);
    backdrop-filter: blur(3px);
    align-items: flex-start; justify-content: center;
    padding: 12vh var(--space-4) var(--space-4);
}
.cmdk-overlay.open { display: flex; }
.cmdk {
    width: 100%; max-width: 560px;
    background: var(--bg-card); border: 1px solid var(--border-light);
    border-radius: var(--radius-xl); box-shadow: var(--shadow-lg);
    overflow: hidden;
    animation: cmdk-in 0.14s ease;
}
@keyframes cmdk-in {
    from { opacity: 0; transform: translateY(-8px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}
.cmdk-input-wrap {
    display: flex; align-items: center; gap: 0.6rem;
    padding: 0.9rem 1.1rem; border-bottom: 1px solid var(--border-light);
}
.cmdk-input-wrap .lucide { width: 18px; height: 18px; color: var(--text-muted); }
.cmdk-input {
    flex: 1; border: none; outline: none; background: transparent;
    font-family: var(--font-ui); font-size: 1rem; color: var(--text-primary);
}
.cmdk-input::placeholder { color: var(--text-muted); }
.cmdk-kbd {
    font-family: var(--font-ui); font-size: 0.68rem; font-weight: 600;
    color: var(--text-muted); background: var(--bg-sidebar);
    border: 1px solid var(--border-light); border-radius: 5px;
    padding: 2px 6px; line-height: 1.4;
}
.cmdk-results { max-height: 46vh; overflow-y: auto; padding: 0.4rem; }
.cmdk-section {
    font-size: 0.68rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: 0.07em; color: var(--text-muted);
    padding: 0.6rem 0.8rem 0.25rem; font-family: var(--font-ui);
}
.cmdk-item {
    display: flex; align-items: center; gap: 0.7rem;
    padding: 0.6rem 0.8rem; border-radius: var(--radius-md);
    color: var(--text-primary); text-decoration: none;
    font-family: var(--font-ui); font-size: 0.92rem; cursor: pointer;
    border: none; background: none; width: 100%; text-align: left;
}
.cmdk-item .lucide { width: 17px; height: 17px; color: var(--text-muted); flex-shrink: 0; }
.cmdk-item:hover, .cmdk-item.active { background: var(--color-accent-light); }
.cmdk-item.active .lucide, .cmdk-item:hover .lucide { color: var(--color-accent); }
.cmdk-item .cmdk-item-sub { margin-left: auto; font-size: 0.75rem; color: var(--text-muted); }
.cmdk-empty { padding: 2rem 1rem; text-align: center; color: var(--text-muted); font-size: 0.9rem; font-family: var(--font-ui); }
.cmdk-footer {
    display: flex; align-items: center; gap: 1rem;
    padding: 0.55rem 1rem; border-top: 1px solid var(--border-light);
    font-size: 0.7rem; color: var(--text-muted); font-family: var(--font-ui);
}
.cmdk-footer .cmdk-kbd { margin-right: 4px; }
