:root {
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --primary-light: rgba(99, 102, 241, 0.12);
    --bg: #0a0e1a;
    --bg-card: #141927;
    --bg-elevated: #1a2035;
    --bg-input: #1e2642;
    --text: #f1f5f9;
    --text-muted: #8892a8;
    --text-dim: #5a6478;
    --border: #2a3550;
    --border-light: #354060;
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --accent: #a78bfa;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
    --shadow-sm: 0 2px 12px rgba(0, 0, 0, 0.3);
    --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

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

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

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* Header */
header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    z-index: 100;
    backdrop-filter: blur(12px);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo {
    width: 28px;
    height: 28px;
    border-radius: 6px;
}

header h1 {
    font-size: 1.15rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

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

.search-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.search-icon {
    position: absolute;
    left: 10px;
    color: var(--text-muted);
    pointer-events: none;
}

#search-input {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px 12px 8px 34px;
    color: var(--text);
    font-size: 0.85rem;
    width: 180px;
    transition: all var(--transition);
}

#search-input:focus {
    outline: none;
    border-color: var(--primary);
    width: 220px;
    box-shadow: 0 0 0 3px var(--primary-light);
}

#refresh-btn {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px;
    color: var(--text-muted);
    cursor: pointer;
    transition: all var(--transition);
    display: flex;
    align-items: center;
}

#refresh-btn:hover { border-color: var(--primary); color: var(--primary); }
#refresh-btn.spinning svg { animation: spin 0.8s linear infinite; }

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

.btn-add {
    display: flex;
    align-items: center;
    gap: 6px;
    background: linear-gradient(135deg, var(--primary), #7c3aed);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    padding: 8px 16px;
    font-size: 0.85rem;
    font-weight: 500;
    text-decoration: none;
    transition: all var(--transition);
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
}

.btn-add:hover { transform: translateY(-1px); box-shadow: 0 4px 16px rgba(99, 102, 241, 0.4); }

/* Main Layout */
.main-layout {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Left Panel */
.panel-left {
    width: 240px;
    background: var(--bg-card);
    border-right: 1px solid var(--border);
    padding: 16px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.panel-section { }

.panel-title {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.panel-select {
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px 10px;
    color: var(--text);
    font-size: 0.85rem;
    cursor: pointer;
    appearance: none;
    transition: border-color var(--transition);
}

.panel-select:focus { outline: none; border-color: var(--primary); }

.depth-control {
    margin-top: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.depth-control label {
    font-size: 0.75rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.depth-buttons {
    display: flex;
    gap: 4px;
}

.depth-btn {
    width: 28px;
    height: 28px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-input);
    color: var(--text-muted);
    font-size: 0.8rem;
    cursor: pointer;
    transition: all var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.depth-btn.active, .depth-btn:hover {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
}

.hidden-list {
    max-height: 150px;
    overflow-y: auto;
    margin-bottom: 8px;
}

.hidden-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 5px 8px;
    font-size: 0.8rem;
    border-radius: 4px;
    background: var(--bg-input);
    margin-bottom: 4px;
}

.hidden-item button {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.75rem;
    padding: 2px 6px;
    border-radius: 4px;
    transition: all var(--transition);
}

.hidden-item button:hover { color: var(--primary); background: var(--primary-light); }

.empty-hint {
    font-size: 0.75rem;
    color: var(--text-dim);
    font-style: italic;
}

.btn-small {
    width: 100%;
    padding: 6px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-muted);
    font-size: 0.75rem;
    cursor: pointer;
    transition: all var(--transition);
}

.btn-small:hover { border-color: var(--primary); color: var(--primary); }

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
}

.stat-item {
    text-align: center;
    padding: 8px 4px;
    background: var(--bg-input);
    border-radius: 8px;
}

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

.stat-label {
    font-size: 0.65rem;
    color: var(--text-dim);
    text-transform: uppercase;
}

/* Graph Container */
#graph-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: radial-gradient(ellipse at center, #0f1629 0%, var(--bg) 70%);
}

#graph { width: 100%; height: 100%; }

#loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: var(--text-muted);
}

#loading.hidden { display: none; }

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 12px;
}

.spinner-small {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    vertical-align: middle;
}

/* Right Detail Panel */
.panel-right {
    width: 280px;
    background: var(--bg-card);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.panel-right.hidden { display: none; }

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

.detail-header h3 {
    font-size: 1rem;
    font-weight: 600;
}

#close-detail {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    transition: all var(--transition);
}

#close-detail:hover { background: var(--bg-input); color: var(--text); }

.detail-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.detail-section {
    margin-bottom: 20px;
}

.detail-section h4 {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 8px;
}

#detail-connections .connection-item {
    padding: 8px 10px;
    border-radius: 6px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all var(--transition);
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    gap: 8px;
}

#detail-connections .connection-item:hover {
    background: var(--primary-light);
    color: var(--primary);
}

.connection-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent);
    flex-shrink: 0;
}

.attachment-link {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 6px;
    background: var(--bg-input);
    margin-bottom: 6px;
    font-size: 0.8rem;
    color: var(--text-muted);
    text-decoration: none;
    transition: all var(--transition);
}

.attachment-link:hover { background: var(--primary-light); color: var(--primary); }

/* Context Menu */
.context-menu {
    position: absolute;
    background: var(--bg-elevated);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    padding: 6px;
    box-shadow: var(--shadow);
    z-index: 200;
    min-width: 160px;
}

.context-menu.hidden { display: none; }

.context-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    font-size: 0.83rem;
    border-radius: 6px;
    cursor: pointer;
    transition: all var(--transition);
    color: var(--text);
}

.context-item:hover { background: var(--primary-light); color: var(--primary); }

/* Graph Styles */
.node-label {
    font-size: 10px;
    fill: var(--text);
    pointer-events: none;
    text-anchor: middle;
    dominant-baseline: middle;
    font-weight: 500;
}

.link { stroke: var(--border); stroke-opacity: 0.5; }
.link.highlighted { stroke: var(--primary); stroke-opacity: 1; stroke-width: 2.5px; }
.link.dimmed { stroke-opacity: 0.08; }

.node circle {
    stroke-width: 2px;
    cursor: pointer;
    transition: r 0.3s;
}

.node.dimmed circle { opacity: 0.15; }
.node.dimmed .node-label { opacity: 0.1; }

.node-glow {
    filter: drop-shadow(0 0 6px rgba(99, 102, 241, 0.5));
}

/* ===== Form Page ===== */
.form-page { overflow: auto; }
.form-page header { justify-content: flex-start; }

.back-link {
    display: flex;
    align-items: center;
    color: var(--text-muted);
    text-decoration: none;
    transition: color var(--transition);
    padding: 4px;
}

.back-link:hover { color: var(--primary); }

.form-container {
    max-width: 680px;
    margin: 0 auto;
    padding: 28px 24px;
}

/* AI Section */
.ai-section {
    background: linear-gradient(135deg, rgba(99, 102, 241, 0.08), rgba(167, 139, 250, 0.06));
    border: 1px solid rgba(99, 102, 241, 0.2);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 24px;
}

.ai-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    flex-wrap: wrap;
    gap: 8px;
}

.ai-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--accent);
}

.ai-hint {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.ai-textarea {
    min-height: 100px;
    margin-bottom: 12px;
}

.btn-ai {
    display: flex;
    align-items: center;
    gap: 6px;
    background: linear-gradient(135deg, var(--primary), #7c3aed);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    padding: 10px 20px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
}

.btn-ai:hover { transform: translateY(-1px); box-shadow: 0 4px 16px rgba(99, 102, 241, 0.4); }
.btn-ai:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }

.ai-result {
    margin-top: 16px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 14px;
}

.ai-result.hidden { display: none; }

.ai-result-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.btn-apply {
    background: var(--success);
    color: white;
    border: none;
    border-radius: 6px;
    padding: 6px 14px;
    font-size: 0.8rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
}

.btn-apply:hover { background: #059669; }

.ai-entity-group {
    margin-bottom: 10px;
    padding: 10px;
    background: var(--bg-input);
    border-radius: 6px;
}

.ai-entity-core {
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 6px;
    font-size: 0.9rem;
}

.ai-entity-related {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.ai-tag {
    background: var(--primary-light);
    color: var(--primary);
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 0.78rem;
    cursor: pointer;
    transition: all var(--transition);
}

.ai-tag:hover { background: var(--primary); color: white; }
.ai-tag.removed { opacity: 0.3; text-decoration: line-through; }

/* Divider */
.divider {
    display: flex;
    align-items: center;
    margin: 28px 0;
    color: var(--text-dim);
    font-size: 0.8rem;
}

.divider::before, .divider::after {
    content: '';
    flex: 1;
    border-bottom: 1px solid var(--border);
}

.divider span { padding: 0 16px; }

/* Form Elements */
.form-section { margin-bottom: 24px; }

.form-label { display: block; margin-bottom: 8px; }

.label-text {
    font-size: 0.9rem;
    font-weight: 600;
    display: block;
    margin-bottom: 3px;
}

.label-hint { font-size: 0.75rem; color: var(--text-muted); }

.form-input {
    width: 100%;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 11px 14px;
    color: var(--text);
    font-size: 0.9rem;
    transition: all var(--transition);
}

.form-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.form-textarea {
    min-height: 72px;
    resize: vertical;
    font-family: inherit;
    line-height: 1.5;
}

.form-select {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 11px 10px;
    color: var(--text);
    font-size: 0.85rem;
    min-width: 80px;
    cursor: pointer;
}

.form-select:focus { outline: none; border-color: var(--primary); }

.related-item-row, .attachment-row {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
    align-items: center;
}

.related-item-row .form-input, .attachment-row .form-input { flex: 1; }

.btn-remove-row {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 8px;
    color: var(--text-dim);
    cursor: pointer;
    transition: all var(--transition);
    display: flex;
    flex-shrink: 0;
}

.btn-remove-row:hover { border-color: var(--danger); color: var(--danger); }

.btn-add-row {
    display: flex;
    align-items: center;
    gap: 6px;
    background: none;
    border: 1px dashed var(--border);
    border-radius: var(--radius-sm);
    padding: 9px 16px;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.83rem;
    width: 100%;
    justify-content: center;
    transition: all var(--transition);
}

.btn-add-row:hover { border-color: var(--primary); color: var(--primary); }

.form-actions {
    display: flex;
    gap: 12px;
    margin-top: 28px;
}

.btn-primary {
    flex: 1;
    background: linear-gradient(135deg, var(--primary), #7c3aed);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    padding: 13px 24px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
}

.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 4px 16px rgba(99, 102, 241, 0.4); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }

.btn-secondary {
    background: var(--bg-input);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 13px 24px;
    font-size: 0.88rem;
    cursor: pointer;
    transition: all var(--transition);
}

.btn-secondary:hover { border-color: var(--primary); color: var(--primary); }

.hidden { display: none !important; }

/* Toast */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--success);
    color: white;
    padding: 12px 24px;
    border-radius: var(--radius-sm);
    font-size: 0.88rem;
    font-weight: 500;
    box-shadow: var(--shadow);
    z-index: 1000;
    animation: slideUp 0.3s ease;
}

.toast.error { background: var(--danger); }

@keyframes slideUp {
    from { transform: translateX(-50%) translateY(20px); opacity: 0; }
    to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* Suggestions */
.suggestions {
    position: absolute;
    background: var(--bg-elevated);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    margin-top: 4px;
    max-height: 160px;
    overflow-y: auto;
    width: 100%;
    z-index: 10;
    box-shadow: var(--shadow);
}

.suggestion-item {
    padding: 9px 14px;
    cursor: pointer;
    font-size: 0.85rem;
    transition: background var(--transition);
}

.suggestion-item:hover { background: var(--primary-light); color: var(--primary); }

/* Recent entries */
.recent-section {
    margin-top: 40px;
    padding-top: 20px;
    border-top: 1px solid var(--border);
}

.recent-section h3 {
    font-size: 0.85rem;
    margin-bottom: 14px;
    color: var(--text-muted);
}

.recent-entry {
    background: var(--bg-input);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    margin-bottom: 8px;
    border: 1px solid var(--border);
}

.recent-entry .core {
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 3px;
    font-size: 0.9rem;
}

.recent-entry .related {
    font-size: 0.78rem;
    color: var(--text-muted);
}

/* Mobile */
@media (max-width: 768px) {
    .panel-left { display: none; }
    .panel-right { position: absolute; right: 0; top: 0; bottom: 0; z-index: 50; width: 260px; }
    #search-input { width: 120px; }
    #search-input:focus { width: 150px; }
    .form-container { padding: 16px; }
    .ai-header { flex-direction: column; align-items: flex-start; }
}
