/* ── Reset & Base ── */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #007aff;
    --primary-dark: #0056b3;
    --success: #34c759;
    --warning: #ff9500;
    --danger: #ff3b30;
    --info: #5ac8fa;
    --bg: #f5f5f7;
    --card-bg: #ffffff;
    --text: #1d1d1f;
    --text-secondary: #86868b;
    --border: #e5e5ea;
    --sidebar-bg: #1c1c1e;
    --sidebar-text: #a1a1a6;
    --sidebar-active: #007aff;
    --header-bg: #ffffff;
    --shadow: 0 2px 10px rgba(0,0,0,0.08);
    --radius: 12px;
    --radius-sm: 8px;
}

html {
    font-size: 16px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
}

a {
    color: var(--primary);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

/* ── App Layout ── */
.app-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* ── Header ── */
.app-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
    background: var(--header-bg);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 8px 16px;
    height: 52px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.menu-toggle {
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-right: 12px;
}

.menu-toggle span {
    display: block;
    width: 20px;
    height: 2.5px;
    background: var(--text);
    border-radius: 2px;
    transition: 0.3s;
}

.app-title {
    font-size: 1.1rem;
    font-weight: 600;
    flex: 1;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-badge {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--border-color);
}

.user-avatar-placeholder {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    font-size: 0.85rem;
    font-weight: 600;
}

.btn-logout {
    font-size: 0.8rem;
    color: var(--danger);
    padding: 4px 10px;
    border: 1px solid var(--danger);
    border-radius: var(--radius-sm);
    transition: 0.2s;
}

.btn-logout:hover {
    background: var(--danger);
    color: #fff;
    text-decoration: none;
}

/* ── Sidebar ── */
.sidebar {
    position: fixed;
    top: 52px;
    left: -260px;
    width: 240px;
    height: calc(100vh - 52px);
    background: var(--sidebar-bg);
    z-index: 200;
    transition: left 0.3s ease;
    overflow-y: auto;
    padding-top: 8px;
}

.sidebar.open {
    left: 0;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 150;
}

.sidebar-overlay.show {
    display: block;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 0 8px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    color: var(--sidebar-text);
    font-size: 0.95rem;
    transition: 0.2s;
}

.nav-item:hover {
    background: rgba(255,255,255,0.08);
    color: #fff;
    text-decoration: none;
}

.nav-item.active {
    background: var(--sidebar-active);
    color: #fff;
}

.sub-nav-item {
    padding-left: 48px !important;
    font-size: 0.85rem;
}

.nav-section-label {
    padding: 12px 16px 4px 16px;
    font-size: 0.72rem;
    color: rgba(255,255,255,0.35);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* 折叠菜单 */
.nav-toggle {
    cursor: pointer;
    user-select: none;
    border-radius: var(--radius-sm);
}
.nav-toggle:hover {
    background: rgba(255,255,255,0.08);
    color: #fff;
    text-decoration: none;
}
.nav-toggle-arrow {
    margin-left: auto;
    font-size: 0.6rem;
    transition: transform 0.25s ease;
    display: inline-block;
    color: rgba(255,255,255,0.35);
}
.nav-toggle.expanded .nav-toggle-arrow {
    transform: rotate(90deg);
}
.nav-submenu {
    overflow: hidden;
}

.nav-icon {
    font-size: 1.2rem;
    width: 24px;
    text-align: center;
}

/* ── Main Content ── */
.main-content {
    margin-top: 52px;
    padding: 16px;
    max-width: 1200px;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
}

/* ── Flash Messages ── */
.flash-container {
    margin-bottom: 16px;
}

.flash-message {
    position: relative;
    padding: 10px 36px 10px 14px;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    margin-bottom: 8px;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

.flash-success { background: #e8f5e9; color: #2e7d32; border-left: 3px solid var(--success); }
.flash-error { background: #fbe9e7; color: #c62828; border-left: 3px solid var(--danger); }
.flash-info { background: #e3f2fd; color: #1565c0; border-left: 3px solid var(--info); }
.flash-warning { background: #fff3e0; color: #e65100; border-left: 3px solid var(--warning); }

.flash-close {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    opacity: 0.6;
}

/* ── Page Header ── */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 12px;
}

.page-header h2 {
    font-size: 1.3rem;
    font-weight: 600;
}

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

/* ── Buttons ── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 18px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: 0.2s;
    text-decoration: none;
    white-space: nowrap;
}

.btn:hover { text-decoration: none; opacity: 0.9; }
.btn:active { transform: scale(0.97); }

.btn-primary { background: var(--primary); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-warning { background: var(--warning); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-info { background: var(--info); color: #fff; }
.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}
.btn-outline:hover { background: var(--bg); }
.btn-sm { padding: 5px 12px; font-size: 0.8rem; }
.btn-xs { padding: 3px 8px; font-size: 0.75rem; }

/* ── Cards ── */
.card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 16px;
    animation: cardFadeIn 0.3s ease;
}

@keyframes cardFadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border);
}

.card-header h3 {
    font-size: 1.05rem;
    font-weight: 600;
}

/* ── Stats Grid ── */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    text-align: center;
}

.stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--primary);
}

.stat-link {
    text-decoration: none;
    color: inherit;
    display: block;
}
.stat-link:hover .stat-value {
    opacity: 0.8;
}

.stat-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* ── Tables ── */
.table-responsive {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
}

th {
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

tr:hover { background: #f8f8fa; }

/* ── Status Badges ── */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 500;
    white-space: nowrap;
}

.badge-lead { background: #e3f2fd; color: #1565c0; }
.badge-following { background: #fff3e0; color: #e65100; }
.badge-deal { background: #e8f5e9; color: #2e7d32; }
.badge-lost { background: #fbe9e7; color: #c62828; }

.badge-pending { background: #fff3e0; color: #e65100; }
.badge-in_progress { background: #e3f2fd; color: #1565c0; }
.badge-resolved { background: #e8f5e9; color: #2e7d32; }
.badge-closed { background: #f5f5f5; color: #666; }

.badge-urgent { background: #fbe9e7; color: #c62828; }
.badge-high { background: #fff3e0; color: #e65100; }
.badge-medium { background: #e3f2fd; color: #1565c0; }
.badge-low { background: #f5f5f5; color: #666; }

/* ── Forms ── */
.form-card {
    max-width: 900px;
}

.form-card .card {
    padding: 0;
    overflow: hidden;
}

.form-card .card > form {
    padding: 24px 28px;
}

/* 表单区域分组 */
.form-section {
    margin-bottom: 24px;
}

.form-section-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--primary);
    padding-bottom: 8px;
    margin-bottom: 16px;
    border-bottom: 2px solid var(--primary);
    display: flex;
    align-items: center;
    gap: 6px;
}

.form-section-title .section-icon {
    font-size: 1rem;
}

/* 自适应网格布局：字段自动排列，不限制每行数量 */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 14px;
}

/* 字段占满整行（备注、大文本等） */
.form-grid-full {
    grid-column: 1 / -1;
}

/* 折叠面板美化 */
.form-details {
    margin-top: 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    transition: box-shadow 0.2s;
}

.form-details:hover {
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.form-details summary {
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text);
    padding: 12px 16px;
    background: #fafafc;
    border-bottom: 1px solid transparent;
    transition: background 0.2s;
    user-select: none;
}

.form-details[open] summary {
    border-bottom-color: var(--border);
    background: #f0f4ff;
    color: var(--primary);
}

.form-details .details-body {
    padding: 16px;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--text);
}

.form-control {
    width: 100%;
    padding: 11px 14px;
    border: 1.5px solid var(--border);
    border-radius: 10px;
    font-size: 0.9rem;
    font-family: inherit;
    background: #fafafc;
    transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}

.form-control:hover {
    border-color: #c7c7cc;
    background: #fff;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(0,122,255,0.12);
    background: #fff;
}

textarea.form-control {
    min-height: 80px;
    resize: vertical;
}

select.form-control {
    appearance: auto;
    cursor: pointer;
}

/* 小型输入框（用于紧凑布局） */
.form-control-sm {
    padding: 7px 10px;
    font-size: 0.82rem;
    border-radius: 8px;
}

.form-actions {
    display: flex;
    gap: 10px;
    margin-top: 28px;
    padding-top: 20px;
    border-top: 1px solid var(--border);
}

.form-hint {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* ── Filters ── */
.filter-bar {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    align-items: center;
}

.filter-bar .form-control {
    width: auto;
    min-width: 120px;
}

.search-form {
    display: flex;
    gap: 8px;
    flex: 1;
    min-width: 200px;
}

.search-form .form-control {
    flex: 1;
}

/* ── Pagination ── */
.pagination {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-top: 20px;
}

.page-link {
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 0.85rem;
    transition: 0.2s;
}

.page-link:hover {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
    text-decoration: none;
}

.page-link.current {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

/* ── Detail View ── */
.detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 12px;
    margin-bottom: 20px;
}

.detail-field {
    padding: 8px 0;
}

.detail-field .field-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 2px;
}

.detail-field .field-value {
    font-size: 0.95rem;
    font-weight: 500;
}

/* ── Knowledge Content ── */
.knowledge-content {
    line-height: 1.8;
    font-size: 0.95rem;
}

.knowledge-content h1, .knowledge-content h2, .knowledge-content h3 {
    margin: 1.2rem 0 0.6rem;
}

.knowledge-content p {
    margin-bottom: 0.8rem;
}

.knowledge-content ul, .knowledge-content ol {
    margin: 0.5rem 0 0.5rem 1.5rem;
}

.knowledge-content pre {
    background: #f5f5f7;
    padding: 12px;
    border-radius: var(--radius-sm);
    overflow-x: auto;
}

.knowledge-content code {
    background: #f0f0f2;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 0.85rem;
}

.knowledge-content pre code {
    background: none;
    padding: 0;
}

/* ── Action Buttons List ── */
.action-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 16px 0;
}

/* ── Empty State ── */
.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
}

.empty-state-icon {
    font-size: 3rem;
    margin-bottom: 12px;
}

.empty-state-text {
    font-size: 0.9rem;
    margin-bottom: 16px;
}

/* ── Timeline ── */
.timeline {
    position: relative;
    padding-left: 24px;
}

.timeline::before {
    content: '';
    position: absolute;
    left: 8px;
    top: 0;
    bottom: 0;
    width: 2px;
    background: var(--border);
}

.timeline-item {
    position: relative;
    padding-bottom: 20px;
}

.timeline-item::before {
    content: '';
    position: absolute;
    left: -20px;
    top: 6px;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--primary);
    border: 2px solid #fff;
    box-shadow: 0 0 0 2px var(--primary);
}

.timeline-item .time {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.timeline-item .title {
    font-weight: 600;
    font-size: 0.9rem;
    margin-bottom: 4px;
}

.timeline-item .desc {
    font-size: 0.85rem;
    color: #555;
}

/* ── Login Page ── */
.login-page {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 20px;
}

.login-card {
    background: var(--card-bg);
    border-radius: var(--radius);
    box-shadow: 0 10px 40px rgba(0,0,0,0.15);
    padding: 40px 30px;
    width: 100%;
    max-width: 380px;
}

.login-card h2 {
    text-align: center;
    margin-bottom: 6px;
    font-size: 1.4rem;
}

.login-subtitle {
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.85rem;
    margin-bottom: 28px;
}

.login-card .btn {
    width: 100%;
    padding: 12px;
    font-size: 1rem;
}

/* ── Responsive ── */
@media (min-width: 768px) {
    .sidebar {
        left: 0;
    }

    .main-content {
        margin-left: 260px;
        padding: 24px 32px;
    }

    .menu-toggle {
        display: none;
    }

    .sidebar-overlay {
        display: none !important;
    }

    .stats-grid {
        grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    }
}

@media (max-width: 767px) {
    .page-header {
        flex-direction: column;
        align-items: stretch;
    }

    .page-actions {
        flex-direction: column;
    }

    .page-actions .btn {
        width: 100%;
        justify-content: center;
    }

    .filter-bar {
        flex-direction: column;
    }

    .filter-bar .form-control {
        width: 100%;
    }

    .search-form {
        width: 100%;
    }

    .detail-grid {
        grid-template-columns: 1fr;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ── 预警通知铃铛 ── */
.alert-bell-wrapper {
    position: relative;
}

.alert-bell {
    background: none;
    border: none;
    cursor: pointer;
    padding: 6px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    transition: background 0.2s, color 0.2s;
    position: relative;
}

.alert-bell:hover {
    background: var(--bg-secondary, #f0f0f0);
    color: var(--text);
}

.alert-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    min-width: 16px;
    height: 16px;
    border-radius: 8px;
    background: #e74c3c;
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    line-height: 16px;
    text-align: center;
    padding: 0 4px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
    pointer-events: none;
}

/* ── 预警下拉面板 ── */
.alert-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    width: 360px;
    max-height: 420px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    z-index: 200;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.alert-dropdown-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 14px;
    font-size: 0.9rem;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
    color: var(--text);
    background: #fafafa;
}

.alert-dropdown-count {
    font-weight: 400;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.alert-dropdown-body {
    flex: 1;
    overflow-y: auto;
    max-height: 350px;
}

.alert-loading,
.alert-empty {
    padding: 30px 14px;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.85rem;
}

.alert-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 14px;
    border-bottom: 1px solid #f0f0f0;
    text-decoration: none;
    color: var(--text);
    transition: background 0.15s;
}

.alert-item:last-child {
    border-bottom: none;
}

.alert-item:hover {
    background: #f8f9fa;
}

.alert-item-danger {
    border-left: 3px solid #e74c3c;
}

.alert-item-warning {
    border-left: 3px solid #f39c12;
}

.alert-item-info {
    border-left: 3px solid #3498db;
}

.alert-item-icon {
    flex-shrink: 0;
    font-size: 1rem;
    line-height: 1.4;
    width: 20px;
    text-align: center;
}

.alert-item-content {
    flex: 1;
    min-width: 0;
}

.alert-item-title {
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.alert-item-desc {
    font-size: 0.78rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.alert-meta {
    margin-left: 6px;
    font-size: 0.75rem;
    color: #888;
}

/* ── 预警区域分栏 ── */
.alert-section:not(:last-child) {
    border-bottom: 1px solid #eee;
}

.alert-section-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px 4px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text);
    background: #fafafa;
    position: sticky;
    top: 0;
    z-index: 1;
}

.alert-section-count {
    font-size: 0.7rem;
    font-weight: 600;
    color: #fff;
    background: var(--primary, #4a7cf7);
    border-radius: 8px;
    min-width: 18px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    padding: 0 5px;
}
