/* ====== 全局重置与变量 ====== */
:root {
    --sidebar-bg: #1a1a2e;
    --sidebar-text: #e0e0e0;
    --sidebar-hover: #16213e;
    --sidebar-active: #0f3460;
    --accent: #4361ee;
    --accent-hover: #3a56d4;
    --accent-light: rgba(67, 97, 238, 0.15);
    --bg-main: #f0f2f5;
    --bg-white: #ffffff;
    --text-primary: #1a1a2e;
    --text-secondary: #555;
    --text-muted: #888;
    --border: #ddd;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.15);
    --radius: 6px;
    --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Microsoft YaHei", sans-serif;
    --font-mono: "SF Mono", "Cascadia Code", "Fira Code", Consolas, monospace;
}

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

html, body {
    height: 100%;
    overflow: hidden;
    font-family: var(--font-sans);
    font-size: 14px;
    color: var(--text-primary);
    background: var(--bg-main);
}

/* ====== 顶部工具栏 ====== */
.toolbar {
    display: flex;
    align-items: center;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    padding: 0 16px;
    height: 48px;
    gap: 8px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    z-index: 100;
}

.toolbar-title {
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
    margin-right: 16px;
    letter-spacing: 0.5px;
}

.toolbar-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 1;
    overflow-x: auto;
}

.toolbar-actions button {
    background: rgba(255, 255, 255, 0.08);
    color: var(--sidebar-text);
    border: 1px solid rgba(255, 255, 255, 0.12);
    padding: 6px 12px;
    border-radius: var(--radius);
    font-size: 13px;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.2s;
    font-family: var(--font-sans);
}

.toolbar-actions button:hover {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    transform: translateY(-1px);
}

.toolbar-actions button:active {
    transform: translateY(0);
}

.toolbar-divider {
    width: 1px;
    height: 24px;
    background: rgba(255, 255, 255, 0.15);
    margin: 0 4px;
    flex-shrink: 0;
}

/* ====== 主布局 ====== */
.main-container {
    display: flex;
    height: calc(100vh - 48px - 28px);
    overflow: hidden;
}

/* ====== 侧边栏通用 ====== */
.sidebar {
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    overflow-y: auto;
    flex-shrink: 0;
}

.sidebar::-webkit-scrollbar {
    width: 5px;
}

.sidebar::-webkit-scrollbar-track {
    background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
}

.sidebar-title {
    font-size: 14px;
    font-weight: 600;
    padding: 14px 12px 10px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    position: sticky;
    top: 0;
    background: var(--sidebar-bg);
    z-index: 10;
}

/* ====== 左侧边栏 - 元素数据库 ====== */
.sidebar-left {
    width: 240px;
    border-right: 1px solid rgba(255, 255, 255, 0.06);
}

.search-box {
    padding: 10px 12px;
}

.search-box input {
    width: 100%;
    padding: 6px 10px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius);
    color: var(--sidebar-text);
    font-size: 12px;
    outline: none;
    transition: border-color 0.2s;
}

.search-box input:focus {
    border-color: var(--accent);
}

.search-box input::placeholder {
    color: rgba(255, 255, 255, 0.35);
}

.element-list {
    padding: 4px 0;
}

.element-group {
    margin-bottom: 2px;
}

.element-group-header {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    cursor: pointer;
    transition: background 0.15s;
    font-weight: 600;
    font-size: 13px;
    user-select: none;
}

.element-group-header:hover {
    background: var(--sidebar-hover);
}

.element-group-header .element-symbol {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    background: var(--accent);
    border-radius: 4px;
    font-size: 12px;
    font-weight: 700;
    margin-right: 8px;
    flex-shrink: 0;
}

.element-group-header .element-name {
    flex: 1;
}

.element-group-header .expand-icon {
    font-size: 10px;
    transition: transform 0.2s;
    color: rgba(255, 255, 255, 0.4);
}

.element-group.expanded .expand-icon {
    transform: rotate(90deg);
}

.element-orbitals {
    display: none;
    background: rgba(0, 0, 0, 0.15);
}

.element-group.expanded .element-orbitals {
    display: block;
}

.orbital-item {
    display: flex;
    align-items: center;
    padding: 6px 12px 6px 48px;
    cursor: pointer;
    transition: background 0.15s;
    font-size: 12px;
    font-family: var(--font-mono);
}

.orbital-item:hover {
    background: var(--sidebar-active);
}

.orbital-item.active {
    background: var(--accent);
}

.orbital-item .orbital-label {
    flex: 1;
    color: rgba(255, 255, 255, 0.75);
}

.orbital-item .orbital-be {
    color: var(--accent);
    font-weight: 600;
}

.orbital-item.active .orbital-be {
    color: #fff;
}

.orbital-item .orbital-desc {
    color: rgba(255, 255, 255, 0.45);
    font-size: 11px;
    margin-left: 6px;
}

/* ====== 右侧边栏 - 峰拟合参数 ====== */
.sidebar-right {
    width: 280px;
    border-left: 1px solid rgba(255, 255, 255, 0.06);
}

.background-section {
    padding: 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.background-section h4 {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.6);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.background-section select {
    width: 100%;
    padding: 6px 8px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius);
    color: var(--sidebar-text);
    font-size: 12px;
    margin-bottom: 8px;
    outline: none;
    cursor: pointer;
}

.background-section select:focus {
    border-color: var(--accent);
}

.bg-range {
    display: flex;
    align-items: center;
    margin-bottom: 6px;
}

.bg-range label {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.6);
    width: 70px;
    flex-shrink: 0;
}

.bg-range input {
    flex: 1;
    padding: 4px 8px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius);
    color: var(--sidebar-text);
    font-size: 12px;
    font-family: var(--font-mono);
    outline: none;
}

.bg-range input:focus {
    border-color: var(--accent);
}

.small-btn {
    width: 100%;
    padding: 6px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 12px;
    cursor: pointer;
    transition: background 0.2s;
    margin-top: 4px;
    font-family: var(--font-sans);
}

.small-btn:hover {
    background: var(--accent-hover);
}

/* ====== 峰列表 ====== */
.peak-list {
    padding: 8px 0;
    overflow-y: auto;
}

.no-peaks {
    text-align: center;
    color: rgba(255, 255, 255, 0.35);
    font-size: 12px;
    padding: 20px 12px;
}

.peak-card {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: var(--radius);
    margin: 6px 10px;
    padding: 10px;
    transition: border-color 0.2s;
}

.peak-card:hover {
    border-color: rgba(255, 255, 255, 0.2);
}

.peak-card-header {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
}

.peak-color-picker {
    width: 24px;
    height: 24px;
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    cursor: pointer;
    padding: 0;
    background: none;
    -webkit-appearance: none;
    appearance: none;
    overflow: hidden;
}

.peak-color-picker::-webkit-color-swatch-wrapper {
    padding: 0;
}

.peak-color-picker::-webkit-color-swatch {
    border: none;
    border-radius: 2px;
}

.peak-card-header .peak-label {
    flex: 1;
    font-size: 13px;
    font-weight: 600;
    margin-left: 8px;
}

.peak-card-header .peak-label-input {
    flex: 1;
    font-size: 12px;
    margin-left: 8px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 3px;
    color: var(--sidebar-text);
    padding: 2px 6px;
    outline: none;
    font-family: var(--font-sans);
}

.peak-card-header .peak-label-input:focus {
    border-color: var(--accent);
}

.peak-delete-btn {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.4);
    cursor: pointer;
    font-size: 16px;
    padding: 2px 4px;
    transition: color 0.2s;
    line-height: 1;
}

.peak-delete-btn:hover {
    color: #ff4757;
}

.peak-param {
    display: flex;
    align-items: center;
    margin-bottom: 5px;
}

.peak-param label {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.5);
    width: 60px;
    flex-shrink: 0;
}

.peak-param input {
    flex: 1;
    padding: 3px 6px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 3px;
    color: var(--sidebar-text);
    font-size: 12px;
    font-family: var(--font-mono);
    outline: none;
    min-width: 0;
}

.peak-param input:focus {
    border-color: var(--accent);
}

.peak-param .unit {
    font-size: 10px;
    color: rgba(255, 255, 255, 0.35);
    margin-left: 4px;
    width: 24px;
}

/* ====== 固定位置行 ====== */
.peak-fixed-row {
    display: flex;
    align-items: center;
    padding: 4px 10px;
    background: rgba(255, 255, 255, 0.03);
    border-top: 1px solid rgba(255, 255, 255, 0.04);
    border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

.peak-fixed-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 11px;
    color: rgba(255, 255, 255, 0.55);
    cursor: pointer;
    user-select: none;
}

.peak-fixed-checkbox {
    width: 14px;
    height: 14px;
    accent-color: #f4a261;
    cursor: pointer;
}

.peak-fixed-checkbox:checked + span {
    color: #f4a261;
    font-weight: 600;
}

/* ====== 拟合结果 ====== */
.fit-results {
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    padding: 12px;
}

.fit-results h4 {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.6);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

#fit-stats {
    font-family: var(--font-mono);
    font-size: 11px;
    line-height: 1.8;
    color: rgba(255, 255, 255, 0.7);
}

#chi-squared {
    font-family: var(--font-mono);
    font-size: 12px;
    margin-top: 8px;
    padding: 6px 8px;
    background: rgba(67, 97, 238, 0.15);
    border-radius: var(--radius);
    color: var(--accent);
}

/* ====== 图表区域 ====== */
.chart-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg-white);
    padding: 12px;
    gap: 8px;
}

.chart-container {
    flex: 1;
    min-height: 0;
    position: relative;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    background: #fff;
    padding: 8px;
}

.residual-container {
    height: 120px;
    flex-shrink: 0;
    position: relative;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    background: #fff;
    padding: 4px 8px;
}

/* ====== 弹窗 ====== */
.dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
}

.dialog {
    background: var(--bg-white);
    border-radius: 12px;
    padding: 24px;
    width: 380px;
    box-shadow: var(--shadow-lg);
    color: var(--text-primary);
}

.dialog-wide {
    width: 520px;
}

.dialog h3 {
    font-size: 18px;
    margin-bottom: 16px;
    color: var(--text-primary);
}

.dialog-hint {
    font-size: 12px;
    color: var(--text-secondary);
    margin-bottom: 10px;
}

.dialog-row {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
}

.dialog-row label {
    width: 160px;
    font-size: 13px;
    color: var(--text-secondary);
}

.dialog-row input {
    flex: 1;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 14px;
    font-family: var(--font-mono);
    outline: none;
}

.dialog-row input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-light);
}

.dialog-row span {
    font-family: var(--font-mono);
    font-weight: 600;
    color: var(--accent);
}

.dialog textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 13px;
    font-family: var(--font-mono);
    outline: none;
    resize: vertical;
}

.dialog textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-light);
}

.dialog-buttons {
    display: flex;
    gap: 10px;
    margin-top: 18px;
    justify-content: flex-end;
}

.dialog-buttons button {
    padding: 8px 20px;
    border: none;
    border-radius: var(--radius);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
    font-family: var(--font-sans);
}

.dialog-buttons button:first-child {
    background: var(--accent);
    color: #fff;
}

.dialog-buttons button:first-child:hover {
    background: var(--accent-hover);
}

.dialog-buttons button:last-child {
    background: #e0e0e0;
    color: var(--text-primary);
}

.dialog-buttons button:last-child:hover {
    background: #d0d0d0;
}

/* ====== 状态栏 ====== */
.status-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    height: 28px;
    background: var(--sidebar-bg);
    color: rgba(255, 255, 255, 0.5);
    font-size: 11px;
    font-family: var(--font-mono);
}

#status-text {
    color: var(--accent);
}

#data-info {
    color: rgba(255, 255, 255, 0.4);
}

/* ====== 响应式 ====== */
@media (max-width: 1024px) {
    .sidebar-left {
        width: 200px;
    }
    .sidebar-right {
        width: 240px;
    }
}

@media (max-width: 768px) {
    .main-container {
        flex-direction: column;
    }
    .sidebar-left, .sidebar-right {
        width: 100%;
        max-height: 200px;
    }
    .chart-area {
        min-height: 400px;
    }
    .toolbar {
        flex-wrap: wrap;
        height: auto;
        padding: 8px;
    }
    .toolbar-actions {
        flex-wrap: wrap;
    }
}

/* ====== 动画 ====== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

.peak-card {
    animation: fadeIn 0.2s ease-out;
}

/* ====== 选中元素参考线提示 ====== */
.ref-badge {
    display: inline-block;
    background: var(--accent);
    color: #fff;
    padding: 1px 6px;
    border-radius: 3px;
    font-size: 10px;
    margin-left: 4px;
    vertical-align: middle;
}

/* loading indicator */
.fitting-indicator {
    display: none;
    position: fixed;
    top: 60px;
    right: 20px;
    background: var(--accent);
    color: #fff;
    padding: 8px 16px;
    border-radius: var(--radius);
    font-size: 13px;
    z-index: 500;
    box-shadow: var(--shadow-lg);
}

.fitting-indicator.show {
    display: block;
    animation: fadeIn 0.2s;
}

/* peak toggle visibility */
.peak-toggle {
    appearance: none;
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255,255,255,0.3);
    border-radius: 3px;
    cursor: pointer;
    position: relative;
    margin-left: 4px;
}

.peak-toggle:checked {
    background: var(--accent);
    border-color: var(--accent);
}

.peak-toggle:checked::after {
    content: '✓';
    color: #fff;
    font-size: 10px;
    position: absolute;
    top: -1px;
    left: 1px;
}
