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

:root {
    --bg: #fafafa;
    --surface: #ffffff;
    --border: #e5e5e5;
    --text: #1a1a1a;
    --text-secondary: #888;
    --accent: #3b82f6;
    --sidebar-width: 280px;
    --header-height: 56px;

    --field-math: #3b82f6;
    --field-cs: #8b5cf6;
    --field-imaging: #10b981;
    --field-color: #ec4899;
}

body {
    font-family: 'Inter', sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Header */
header {
    height: var(--header-height);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 16px;
    flex-shrink: 0;
    z-index: 100;
}
header .back {
    font-size: 0.85rem;
    color: var(--text-secondary);
    text-decoration: none;
    white-space: nowrap;
}
header .back:hover { color: var(--text); }
header h1 {
    font-family: 'Space Grotesk', sans-serif;
    font-size: 1.15rem;
    font-weight: 600;
    flex: 1;
}
.header-actions { display: flex; gap: 8px; }
.header-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 12px;
    font-size: 0.8rem;
    font-family: 'Inter', sans-serif;
    cursor: pointer;
    color: var(--text);
    transition: background 0.15s;
}
.header-btn:hover { background: #f0f0f0; }
.header-btn span { display: inline; }
.hint-btn { border-color: #f59e0b; color: #b45309; }
.hint-btn:hover { background: #fffbeb; }

/* Workspace */
#workspace {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Sidebar */
#sidebar {
    width: var(--sidebar-width);
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}
.sidebar-header {
    padding: 12px;
    border-bottom: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 8px;
}
#search {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.85rem;
    font-family: 'Inter', sans-serif;
    outline: none;
    background: var(--bg);
}
#search:focus { border-color: var(--accent); }
#counter {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-align: right;
}
#element-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.element-chip {
    padding: 10px 14px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: grab;
    user-select: none;
    transition: transform 0.15s, box-shadow 0.15s;
    display: flex;
    align-items: center;
    gap: 8px;
    -webkit-user-select: none;
    touch-action: none;
}
.element-chip:hover {
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.element-chip:active { cursor: grabbing; }
.element-chip .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}
.element-chip .info-btn {
    margin-left: auto;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-secondary);
    padding: 2px;
    font-size: 1rem;
    line-height: 1;
    opacity: 0;
    transition: opacity 0.15s;
}
.element-chip:hover .info-btn { opacity: 1; }

/* Canvas / Drop Zone */
#canvas-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
}

#drop-zone {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

#drop-zone .hint {
    color: #ccc;
    font-size: 1rem;
    pointer-events: none;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

#combine-zone {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.dropped-element {
    position: absolute;
    padding: 10px 18px;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    pointer-events: auto;
    transition: box-shadow 0.15s;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}
.dropped-element:hover {
    box-shadow: 0 4px 16px rgba(0,0,0,0.1);
}
.dropped-element .dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}
.dropped-element.combining {
    animation: pulse 0.6s ease-in-out;
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); box-shadow: 0 0 20px rgba(59,130,246,0.3); }
    100% { transform: scale(1); }
}

.combine-result {
    position: absolute;
    pointer-events: auto;
    z-index: 20;
    animation: popIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes popIn {
    0% { transform: scale(0); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

/* Info Panel */
#info-panel {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
}
#info-panel.hidden { display: none; }
.info-panel-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.4);
    backdrop-filter: blur(4px);
}
.info-panel-content {
    position: relative;
    background: var(--surface);
    border-radius: 20px;
    padding: 32px;
    max-width: 520px;
    width: calc(100% - 40px);
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.15);
}
.info-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-secondary);
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
}
.info-close:hover { background: #f0f0f0; }
.info-image-wrap {
    width: 100%;
    max-height: 260px;
    border-radius: 12px;
    overflow: hidden;
    margin-bottom: 20px;
    background: #f5f5f5;
    display: flex;
    align-items: center;
    justify-content: center;
}
.info-image {
    width: 100%;
    height: auto;
    max-height: 260px;
    object-fit: contain;
    display: block;
}
#info-title {
    font-family: 'Space Grotesk', sans-serif;
    font-size: 1.6rem;
    font-weight: 700;
    margin-bottom: 8px;
}
.field-tag {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 100px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 16px;
    color: #fff;
}
#info-description {
    color: #555;
    font-size: 0.9rem;
    line-height: 1.7;
    margin-bottom: 16px;
}
.info-recipe {
    padding-top: 16px;
    border-top: 1px solid var(--border);
    font-size: 0.85rem;
    color: var(--text-secondary);
}
.info-recipe .recipe-formula {
    margin-top: 8px;
    font-weight: 500;
    color: var(--text);
}
.info-attribution {
    margin-top: 12px;
    font-size: 0.7rem;
    color: #aaa;
    line-height: 1.4;
}
.info-attribution a {
    color: #999;
    text-decoration: underline;
}

/* Chain Panel */
#chain-panel {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
}
#chain-panel.hidden { display: none; }
.chain-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.4);
    backdrop-filter: blur(4px);
}
.chain-content {
    position: relative;
    background: var(--surface);
    border-radius: 20px;
    padding: 32px;
    max-width: 900px;
    width: calc(100% - 40px);
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.15);
}
.chain-content h2 {
    font-family: 'Space Grotesk', sans-serif;
    font-size: 1.5rem;
    font-weight: 700;
    margin-bottom: 4px;
}
.chain-subtitle {
    color: var(--text-secondary);
    font-size: 0.85rem;
    margin-bottom: 20px;
}
.chain-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-secondary);
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
}
.chain-close:hover { background: #f0f0f0; }

#chain-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 24px;
}
.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
    color: var(--text-secondary);
}
.legend-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

#chain-tree {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.chain-step {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-radius: 10px;
    background: var(--bg);
    font-size: 0.85rem;
    flex-wrap: wrap;
}
.chain-step .chain-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}
.chain-step .chain-a,
.chain-step .chain-b {
    font-weight: 500;
    cursor: pointer;
}
.chain-step .chain-a:hover,
.chain-step .chain-b:hover { text-decoration: underline; }
.chain-step .chain-plus {
    color: var(--text-secondary);
    font-weight: 600;
}
.chain-step .chain-arrow {
    color: var(--text-secondary);
}
.chain-step .chain-result {
    font-weight: 600;
    cursor: pointer;
}
.chain-step .chain-result:hover { text-decoration: underline; }
.chain-step .chain-field {
    margin-left: auto;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #fff;
    padding: 2px 8px;
    border-radius: 100px;
    font-weight: 600;
}
.chain-step.locked {
    opacity: 0.35;
}

/* Sidebar sections */
.sidebar-section { margin-bottom: 4px; }
.sidebar-section-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 8px 4px;
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-secondary);
}
.sidebar-section-header .section-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    flex-shrink: 0;
}
.sidebar-section-header .section-count {
    margin-left: auto;
    opacity: 0.5;
}
.sidebar-section-body {
    display: flex;
    flex-direction: column;
    gap: 3px;
}

/* Assembly */
#assembly {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: linear-gradient(transparent, rgba(255,255,255,0.95) 20%);
    padding: 20px 16px 10px;
    z-index: 5;
    pointer-events: none;
}
.assembly-label {
    font-family: 'Space Grotesk', sans-serif;
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-secondary);
    text-align: center;
    margin-bottom: 6px;
}
.assembly-tracks {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}
.assembly-track {
    display: flex;
    align-items: center;
    gap: 6px;
    width: 100%;
    max-width: 600px;
}
.track-label {
    font-size: 0.55rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    width: 52px;
    flex-shrink: 0;
    text-align: right;
    opacity: 0.7;
}
.track-nodes {
    display: flex;
    align-items: center;
    gap: 3px;
    flex: 1;
    overflow-x: auto;
}
.track-merge {
    color: #ccc;
    font-size: 0.6rem;
    font-weight: 600;
    line-height: 1;
    transition: color 0.4s ease;
    padding-left: 58px;
}
.track-merge.lit { color: var(--text-secondary); }
.track-merge .merge-arrow { display: block; text-align: left; padding-left: 24px; }
.asm-node {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    border-radius: 6px;
    border: 1.5px solid #ddd;
    background: #f5f5f5;
    font-size: 0.6rem;
    font-weight: 500;
    color: #bbb;
    white-space: nowrap;
    transition: all 0.4s ease;
    pointer-events: auto;
    cursor: default;
    flex-shrink: 0;
}
.asm-node .asm-dot {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: #ddd;
    flex-shrink: 0;
    transition: background 0.4s ease, box-shadow 0.4s ease;
}
.asm-node.unlocked {
    border-color: var(--node-color, #888);
    background: #fff;
    color: var(--text);
    cursor: pointer;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}
.asm-node.unlocked .asm-dot {
    background: var(--node-color, #888);
    box-shadow: 0 0 5px var(--node-color, #888);
}
.asm-node.unlocked.highlight {
    animation: asmPop 0.6s cubic-bezier(0.34,1.56,0.64,1);
}
@keyframes asmPop {
    0% { transform: scale(1); }
    40% { transform: scale(1.15); box-shadow: 0 0 10px var(--node-color, #888); }
    100% { transform: scale(1); }
}
.asm-node.goal {
    border-width: 2px;
    font-weight: 600;
    font-size: 0.65rem;
}
.asm-node.goal.unlocked {
    background: linear-gradient(135deg, #fff 0%, #fce7f3 100%);
    border-color: var(--field-color);
    box-shadow: 0 0 10px rgba(236,72,153,0.3);
}
.asm-arrow {
    color: #ccc;
    font-size: 0.6rem;
    font-weight: 600;
    line-height: 1;
    transition: color 0.4s ease;
    flex-shrink: 0;
}
.asm-arrow.lit { color: var(--text-secondary); }

/* Toast */
#toast-container {
    position: fixed;
    top: 72px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 300;
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
    pointer-events: none;
}
.toast {
    background: var(--text);
    color: #fff;
    padding: 10px 20px;
    border-radius: 10px;
    font-size: 0.85rem;
    font-weight: 500;
    animation: toastIn 0.3s ease, toastOut 0.3s ease 2s forwards;
    white-space: nowrap;
}
@keyframes toastIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}
@keyframes toastOut {
    from { opacity: 1; }
    to { opacity: 0; transform: translateY(-10px); }
}

/* Win overlay */
.win-overlay {
    position: fixed;
    inset: 0;
    z-index: 250;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(6px);
    animation: fadeIn 0.4s ease;
}
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}
.win-card {
    background: var(--surface);
    border-radius: 24px;
    padding: 48px;
    text-align: center;
    max-width: 420px;
    animation: popIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.win-card h2 {
    font-family: 'Space Grotesk', sans-serif;
    font-size: 2rem;
    margin-bottom: 8px;
}
.win-card p {
    color: var(--text-secondary);
    margin-bottom: 24px;
    line-height: 1.6;
}
.win-card button {
    background: var(--text);
    color: #fff;
    border: none;
    padding: 12px 28px;
    border-radius: 10px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    font-family: 'Inter', sans-serif;
}

/* Mobile */
@media (max-width: 700px) {
    .header-btn span { display: none; }
    #sidebar { width: 100%; height: 40%; border-right: none; border-bottom: 1px solid var(--border); }
    #workspace { flex-direction: column; }
    #canvas-area { height: 60%; }
    .info-panel-content { padding: 24px; }
    .chain-content { padding: 24px; }
    .element-chip .info-btn { opacity: 1; font-size: 1.1rem; padding: 4px 6px; }
    .chain-step { font-size: 0.78rem; }
    .chain-step .chain-field { font-size: 0.6rem; }
    .dropped-element { font-size: 0.8rem; padding: 8px 14px; }
    #assembly { padding: 12px 6px 6px; }
    .asm-node { padding: 2px 5px; font-size: 0.5rem; }
    .asm-node .asm-dot { width: 4px; height: 4px; }
    .asm-arrow { font-size: 0.5rem; }
    .track-label { font-size: 0.45rem; width: 36px; }
    .track-merge { padding-left: 42px; }
    .assembly-track { gap: 4px; }
}

@media (max-width: 400px) {
    #sidebar { height: 35%; }
    #canvas-area { height: 65%; }
    .element-chip { padding: 8px 10px; font-size: 0.8rem; }
}

/* Scrollbar */
#element-list::-webkit-scrollbar { width: 4px; }
#element-list::-webkit-scrollbar-track { background: transparent; }
#element-list::-webkit-scrollbar-thumb { background: #ddd; border-radius: 2px; }
.chain-content::-webkit-scrollbar { width: 4px; }
.chain-content::-webkit-scrollbar-track { background: transparent; }
.chain-content::-webkit-scrollbar-thumb { background: #ddd; border-radius: 2px; }
