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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #0a0a0a;
    color: #e0e0e0;
    overflow: hidden;
    height: 100vh;
}

#app {
    display: grid;
    grid-template-columns: 280px 1fr;
    grid-template-rows: 44px 1fr;
    height: 100vh;
}

/* Explicit grid placement — prevents extra DOM children from disrupting layout */
#header         { grid-column: 1 / -1; grid-row: 1; }
#sidebar        { grid-column: 1;      grid-row: 2; }
#graph-container { grid-column: 2;     grid-row: 2; }

/* ── Header ─────────────────────────────────────────────────────────── */

#header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    background: #111;
    border-bottom: 1px solid #222;
    z-index: 10;
}
#header h1 {
    font-size: 15px;
    font-weight: 600;
    color: #fff;
    letter-spacing: 0.5px;
}
.sp-graph-title {
    font-family: 'Bangers', cursive;
    font-size: 18px;
    letter-spacing: 2px;
    color: #f5d442;
    text-shadow: 1px 1px 0 rgba(0,0,0,0.6);
}
.sp-graph-accent {
    color: #ff9e4a;
}
.back-link {
    color: #4a9eff;
    text-decoration: none;
    font-size: 12px;
    margin-right: 12px;
    opacity: 0.8;
    transition: opacity 0.15s;
}
.back-link:hover { opacity: 1; text-decoration: underline; }
#stats-bar {
    font-size: 12px;
    color: #888;
}
#stats-bar span {
    color: #ccc;
    font-weight: 600;
}
.stats-filtered {
    color: #666 !important;
    font-weight: 400 !important;
}

/* ── Sidebar ────────────────────────────────────────────────────────── */

#sidebar {
    background: #111;
    border-right: 1px solid #222;
    overflow-y: auto;
    padding: 10px 12px;
    font-size: 13px;
}
#sidebar::-webkit-scrollbar { width: 4px; }
#sidebar::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; }

.sidebar-section {
    margin-bottom: 14px;
    padding-bottom: 10px;
    border-bottom: 1px solid #1a1a1a;
}
.sidebar-section:last-child { border-bottom: none; }

.sidebar-section h3 {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #666;
    margin-bottom: 6px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.slider-val {
    color: #4a9eff;
    font-size: 11px;
    font-weight: 600;
    font-family: "SF Mono", monospace;
}

.sidebar-section label {
    display: flex;
    align-items: center;
    font-size: 12px;
    padding: 2px 0;
    cursor: pointer;
    color: #bbb;
    gap: 4px;
}
.sidebar-section label:hover { color: #fff; }

.sidebar-section input[type="checkbox"] {
    margin: 0;
    accent-color: #4a9eff;
    flex-shrink: 0;
}

.color-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.filter-count {
    margin-left: auto;
    color: #555;
    font-size: 10px;
    font-family: "SF Mono", monospace;
}

.select-btns {
    display: flex;
    gap: 6px;
    margin-top: 6px;
}
.btn-mini {
    flex: 1;
    padding: 3px 0;
    font-size: 10px;
    background: #1a1a1a;
    border: 1px solid #333;
    border-radius: 3px;
    color: #888;
    cursor: pointer;
}
.btn-mini:hover { color: #fff; border-color: #555; }

.range-hint {
    font-size: 10px;
    color: #555;
    margin-top: 2px;
}

/* Tier selector */
.tier-select {
    width: 100%;
    padding: 6px 8px;
    background: #1a1a1a;
    color: #ccc;
    border: 1px solid #333;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
    outline: none;
}
.tier-select:hover { border-color: #555; }
.tier-select:focus { border-color: #4a9eff; }
.tier-select option { background: #1a1a1a; color: #ccc; }

/* Search */
#search-input {
    width: 100%;
    padding: 6px 10px;
    background: #1a1a1a;
    border: 1px solid #333;
    border-radius: 4px;
    color: #e0e0e0;
    font-size: 12px;
    outline: none;
}
#search-input:focus { border-color: #4a9eff; }
#search-results {
    max-height: 180px;
    overflow-y: auto;
    margin-top: 4px;
}
.search-result {
    padding: 4px 8px;
    font-size: 11px;
    cursor: pointer;
    border-radius: 3px;
    display: flex;
    align-items: center;
    gap: 6px;
}
.search-result:hover { background: #1a1a1a; }
.type-badge {
    display: inline-block;
    font-size: 9px;
    padding: 1px 5px;
    border-radius: 3px;
    flex-shrink: 0;
}

/* Dual range */
.dual-range {
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.dual-range input[type="range"],
input[type="range"] {
    width: 100%;
    accent-color: #4a9eff;
    height: 16px;
}

/* Physics */
.physics-label {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    font-size: 11px;
    color: #999;
    padding: 2px 0;
    gap: 0;
}
.physics-label .slider-val {
    margin-left: auto;
}
.physics-label input[type="range"] {
    width: 100%;
    margin-top: 2px;
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: #999;
    padding: 4px 0;
    cursor: pointer;
}

/* Buttons */
.btn {
    width: 100%;
    padding: 7px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    margin-bottom: 5px;
    background: #4a9eff;
    color: #fff;
}
.btn:hover { background: #3a8eef; }
.btn-secondary {
    background: #222;
    color: #aaa;
    border: 1px solid #333;
}
.btn-secondary:hover { background: #333; color: #fff; }


/* ── Graph ──────────────────────────────────────────────────────────── */

#graph-container {
    position: relative;
    overflow: hidden;
    background: #0a0a0a;
}
#graph-svg {
    width: 100%;
    height: 100%;
}

/* Node styles */
.node { cursor: pointer; transition: opacity 0.3s; }
.node:hover circle { stroke-width: 3px; stroke: #fff; }
.node text {
    font-size: 9px;
    fill: #999;
    pointer-events: none;
    text-anchor: middle;
    dominant-baseline: central;
}

/* Focus / dim states */
.node.dimmed { opacity: 0.08; }
.node.highlighted circle {
    stroke: #fff !important;
    stroke-width: 3px !important;
    filter: drop-shadow(0 0 6px rgba(255,255,255,0.4));
}

/* Search highlight — distinct from focus highlight (pulsing yellow glow) */
.node.search-highlighted circle {
    stroke: #ffcc00 !important;
    stroke-width: 4px !important;
    filter: drop-shadow(0 0 12px rgba(255, 204, 0, 0.7));
    animation: search-pulse 1.5s ease-in-out 3;
}
@keyframes search-pulse {
    0%, 100% { filter: drop-shadow(0 0 8px rgba(255, 204, 0, 0.5)); }
    50% { filter: drop-shadow(0 0 20px rgba(255, 204, 0, 0.9)); }
}

.edge { transition: stroke-opacity 0.3s; cursor: pointer; }
.edge:hover { stroke-opacity: 0.9 !important; stroke-width: 3px !important; }
.edge.dimmed { stroke-opacity: 0.03 !important; }
.edge.highlighted { stroke-opacity: 0.85 !important; }

/* Edge dash patterns */
.edge.CHALLENGES { stroke-dasharray: 6,3; }
.edge.MOCKS { stroke-dasharray: 3,3; }
.edge.INSULTS { stroke-dasharray: 2,2; }
.edge.SAYS { stroke-dasharray: 1,2; }

/* ── Popover ────────────────────────────────────────────────────────── */

.popover {
    position: fixed;
    top: 56px;
    right: 12px;
    width: 380px;
    max-height: calc(100vh - 72px);
    overflow-y: auto;
    background: #151515;
    border: 1px solid #2a2a2a;
    border-radius: 8px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.7);
    z-index: 100;
}
.popover::-webkit-scrollbar { width: 4px; }
.popover::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; }
.popover.hidden { display: none; }

.popover-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    border-bottom: 1px solid #2a2a2a;
    background: #1a1a1a;
    border-radius: 8px 8px 0 0;
    position: sticky;
    top: 0;
    z-index: 1;
}
#popover-title {
    font-weight: 600;
    font-size: 13px;
    color: #fff;
}
#popover-close {
    background: none;
    border: none;
    color: #666;
    font-size: 18px;
    cursor: pointer;
    line-height: 1;
}
#popover-close:hover { color: #fff; }

#popover-body {
    padding: 10px 14px;
    font-size: 12px;
    line-height: 1.5;
}
#popover-body .prop {
    margin-bottom: 6px;
}
#popover-body .prop-key {
    color: #666;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
#popover-body .prop-val {
    color: #ccc;
}
.loading-text { color: #555; }

#popover-body .badge {
    display: inline-block;
    font-size: 10px;
    padding: 1px 6px;
    border-radius: 3px;
    background: #222;
    color: #aaa;
    margin: 1px;
}

/* Edge list in popover */
#popover-body .edge-list {
    margin-top: 10px;
    padding-top: 8px;
    border-top: 1px solid #222;
}
.edge-direction-label {
    font-size: 9px;
    color: #555;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin: 8px 0 4px;
}
#popover-body .edge-item {
    padding: 5px 0;
    font-size: 11px;
    color: #aaa;
    border-bottom: 1px solid #1a1a1a;
}
#popover-body .edge-item:last-child { border-bottom: none; }
#popover-body .edge-item .edge-type {
    font-weight: 600;
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 9px;
    margin-right: 3px;
}
.edge-meta {
    color: #666;
    font-size: 10px;
}
.edge-summary {
    color: #888;
    font-size: 10px;
    margin-top: 2px;
}
.edge-overflow {
    color: #444;
    font-size: 10px;
    padding: 4px 0;
}

#popover-body .quote {
    font-style: italic;
    color: #888;
    border-left: 2px solid #333;
    padding-left: 8px;
    margin: 3px 0;
    font-size: 11px;
}

/* Evidence items */
.evidence-item {
    margin: 4px 0;
    padding: 5px 8px;
    background: #121222;
    border-left: 2px solid #4a9eff;
    border-radius: 0 4px 4px 0;
    font-size: 10px;
}
.evidence-episode {
    color: #4a9eff;
    font-size: 9px;
    font-weight: 600;
    margin-bottom: 1px;
}
.evidence-desc {
    color: #999;
    margin-bottom: 1px;
}
.emotional-tag {
    background: #2a1a1a;
    color: #ff6b6b;
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 8px;
    margin-left: 4px;
    font-weight: 400;
}
.evidence-trigger {
    color: #666;
    font-style: italic;
    font-size: 9px;
    margin-bottom: 1px;
}
.evidence-situation {
    color: #777;
    font-size: 9px;
    margin-bottom: 1px;
}
.evidence-chars {
    margin: 2px 0;
}
.char-badge {
    display: inline-block;
    font-size: 8px;
    padding: 0 4px;
    background: #1a1a2a;
    color: #6b8aff;
    border-radius: 2px;
    margin: 1px;
}

/* Edge groups */
.edge-group {
    margin-bottom: 4px;
    border-bottom: 1px solid #1a1a1a;
}
.edge-group.collapsed .edge-group-body {
    display: none;
}
.edge-group.collapsed .edge-group-chevron {
    transform: rotate(-90deg);
}
.edge-group-header {
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 4px 0;
}
.edge-group-header:hover {
    background: #1a1a1a;
}
.edge-group-count {
    color: #555;
    font-size: 9px;
    margin-left: 6px;
}
.edge-group-chevron {
    color: #444;
    font-size: 8px;
    transition: transform 0.15s;
}
.edge-group-body {
    padding-left: 4px;
}

/* Node portrait */
.node-portrait {
    text-align: center;
    margin-bottom: 10px;
}
.node-portrait img {
    max-width: 120px;
    max-height: 120px;
    border-radius: 8px;
    border: 2px solid #2a2a2a;
    object-fit: cover;
}

/* Weight badge */
.weight-badge {
    background: #4a9eff22;
    color: #4a9eff;
    padding: 1px 5px;
    border-radius: 8px;
    font-size: 9px;
    font-weight: 600;
}

/* ── Type color classes ─────────────────────────────────────────────── */

.type-Character { color: #4a9eff; }
.type-Concept { color: #ff9e4a; }
.type-Episode { color: #888; }
.type-RealWorldEntity { color: #4aff6a; }
.type-Catchphrase { color: #ffff4a; }
.type-RunningThread { color: #9e4aff; }
.type-CreatorVoice { color: #ff4a9e; }

.badge-Character { background: #4a9eff22; color: #4a9eff; }
.badge-Concept { background: #ff9e4a22; color: #ff9e4a; }
.badge-Episode { background: #88888822; color: #888; }
.badge-RealWorldEntity { background: #4aff6a22; color: #4aff6a; }
.badge-Catchphrase { background: #ffff4a22; color: #ffff4a; }
.badge-RunningThread { background: #9e4aff22; color: #9e4aff; }
.badge-CreatorVoice { background: #ff4a9e22; color: #ff4a9e; }

/* ── Loading ────────────────────────────────────────────────────────── */

#loading {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.85);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 200;
    gap: 12px;
}
#loading p { color: #666; font-size: 13px; }
#loading.hidden { display: none; }
.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid #222;
    border-top-color: #4a9eff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Chat Widget ────────────────────────────────────────────────────── */

#chat-widget {
    position: fixed;
    bottom: 16px;
    right: 16px;
    z-index: 150;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* Toggle button */
#chat-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    background: #4a9eff;
    color: #fff;
    border-radius: 24px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    box-shadow: 0 4px 16px rgba(74, 158, 255, 0.3);
    transition: background 0.2s;
}
#chat-toggle:hover { background: #3a8eef; }
.chat-toggle-avatar {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid rgba(255,255,255,0.4);
}

/* Collapsed: only show toggle */
.chat-collapsed #chat-panel { display: none; }

/* Expanded: show panel, hide toggle */
.chat-expanded #chat-toggle { display: none; }
.chat-expanded #chat-panel { display: flex; }

/* Chat panel */
#chat-panel {
    display: none;
    flex-direction: column;
    width: 380px;
    height: 500px;
    background: #141414;
    border: 1px solid #2a2a2a;
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.7);
    overflow: hidden;
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px;
    background: #1a1a1a;
    border-bottom: 1px solid #2a2a2a;
}
.chat-header-avatar {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    object-fit: cover;
}
.chat-persona-label {
    flex: 1;
    font-size: 13px;
    font-weight: 600;
    color: #e0e0e0;
}
.chat-header-btn {
    background: none;
    border: none;
    color: #666;
    font-size: 14px;
    cursor: pointer;
    padding: 4px 6px;
    border-radius: 4px;
}
.chat-header-btn:hover { color: #fff; background: #333; }

/* Messages */
#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
#chat-messages::-webkit-scrollbar { width: 4px; }
#chat-messages::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; }

.chat-msg {
    max-width: 85%;
    font-size: 13px;
    line-height: 1.5;
}
.chat-msg-user {
    align-self: flex-end;
}
.chat-msg-user .chat-msg-content {
    background: #4a9eff;
    color: #fff;
    padding: 8px 12px;
    border-radius: 14px 14px 4px 14px;
}
.chat-msg-assistant {
    align-self: flex-start;
}
.chat-msg-name {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 10px;
    color: #666;
    margin-bottom: 2px;
    padding-left: 4px;
}
.chat-msg-avatar {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    object-fit: cover;
}
.chat-msg-assistant .chat-msg-content {
    background: #222;
    color: #ddd;
    padding: 8px 12px;
    border-radius: 14px 14px 14px 4px;
}
.chat-msg-system {
    text-align: center;
    font-size: 10px;
    color: #555;
    padding: 4px;
}

/* Typing dots — bouncing circles, same as home chat */
.typing-dots {
    display: inline-flex;
    gap: 3px;
}
.typing-dots span {
    width: 8px;
    height: 8px;
    background: #777;
    border-radius: 50%;
    animation: typingBounce 1.2s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* Input area */
.chat-input-area {
    display: flex;
    gap: 6px;
    padding: 8px 10px;
    background: #1a1a1a;
    border-top: 1px solid #2a2a2a;
}
#chat-input {
    flex: 1;
    padding: 8px 12px;
    background: #222;
    border: 1px solid #333;
    border-radius: 20px;
    color: #e0e0e0;
    font-size: 13px;
    outline: none;
}
#chat-input:focus { border-color: #4a9eff; }
#chat-send {
    padding: 8px 16px;
    background: #4a9eff;
    color: #fff;
    border: none;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
}
#chat-send:hover { background: #3a8eef; }

/* Tool trace link */
.tool-trace-link {
    font-size: 10px;
    margin-top: 4px;
    padding: 2px 4px;
}
.tool-trace-link a,
a.tool-trace-link {
    color: #4a9eff;
    text-decoration: none;
    opacity: 0.7;
    transition: opacity 0.15s;
    font-size: 10px;
}
.tool-trace-link a:hover,
a.tool-trace-link:hover {
    opacity: 1;
    text-decoration: underline;
}

/* Tool call blocks (Claude-style, for graph chat widget) */
.tool-call-block {
    margin-bottom: 4px;
    padding: 4px 8px;
    border-radius: 6px;
    background: rgba(74, 158, 255, 0.06);
    font-size: 0.72rem;
    color: #4a9eff;
    opacity: 0.7;
    width: 100%;
}

.tc-label {
    font-style: italic;
}

/* Ask button in popover */
.btn-ask {
    width: 100%;
    padding: 6px 12px;
    margin: 8px 0;
    background: #4a9eff22;
    color: #4a9eff;
    border: 1px solid #4a9eff44;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    text-align: left;
}
.btn-ask:hover { background: #4a9eff33; }

/* ── Sidebar toggle (mobile) ──────────────────────────────────────── */

/* Toggle + backdrop are position:fixed, outside grid flow */
#sidebar-toggle {
    display: none;
    position: fixed;
    top: 50px;
    left: 8px;
    z-index: 50;
    background: #1a1a1a;
    border: 1px solid #333;
    border-radius: 6px;
    color: #aaa;
    padding: 6px 10px;
    font-size: 18px;
    cursor: pointer;
    line-height: 1;
}
#sidebar-toggle:hover { color: #fff; background: #333; }

#sidebar-backdrop {
    display: none;
}

/* ── Graph page footer ───────────────────────────────────────────── */

.graph-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    text-align: center;
    padding: 10px 1rem;
    background: rgba(10, 10, 10, 0.85);
    backdrop-filter: blur(6px);
    color: rgba(255, 255, 255, 0.4);
    font-size: 0.85rem;
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0;
}

.gf-sep {
    margin: 0 0.5rem;
    opacity: 0.3;
}

.gf-credit {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    color: #4a9eff;
    text-decoration: none;
    opacity: 0.75;
    font-style: italic;
    font-size: 0.95rem;
    transition: opacity 0.15s;
}
.gf-credit:hover {
    opacity: 1;
    text-decoration: underline;
}
.gf-linkedin-icon {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    opacity: 0.9;
}

/* ── Responsive: Tablet (< 900px) ────────────────────────────────── */

@media (max-width: 900px) {
    #app {
        grid-template-columns: 240px 1fr;
    }
    #sidebar { font-size: 12px; padding: 8px 10px; }
    .popover { width: 320px; right: 8px; }
    #chat-panel { width: 320px; height: 420px; }
}

/* ── Responsive: Mobile (< 700px) ────────────────────────────────── */

@media (max-width: 700px) {
    #app {
        grid-template-columns: 1fr;
        grid-template-rows: 44px 1fr;
    }
    #header          { grid-column: 1; grid-row: 1; }
    #graph-container { grid-column: 1; grid-row: 2; }

    #sidebar-toggle { display: block; }

    #sidebar {
        position: fixed;
        top: 44px;
        left: 0;
        bottom: 0;
        width: 280px;
        z-index: 40;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: 4px 0 20px rgba(0,0,0,0.5);
    }
    #sidebar.open {
        transform: translateX(0);
    }

    /* Backdrop */
    #sidebar-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        z-index: 35;
        background: rgba(0,0,0,0.5);
    }
    #sidebar-backdrop.visible { display: block; }

    /* Popover — bottom sheet on mobile */
    .popover {
        position: fixed;
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-height: 55vh;
        border-radius: 12px 12px 0 0;
    }

    /* Chat widget — full width on mobile */
    #chat-widget {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
    }

    #chat-toggle {
        border-radius: 0;
        justify-content: center;
        padding: 12px 18px;
    }

    #chat-panel {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-width: none;
        /* Use 100dvh-based calc so panel adapts when virtual keyboard opens */
        height: calc(100dvh - 44px);
        max-height: 55dvh;
        border-radius: 12px 12px 0 0;
        border-bottom: none;
    }

    /* When keyboard is open, panel takes remaining viewport height */
    #chat-panel.keyboard-open {
        max-height: none;
        height: calc(100dvh - 44px);
    }

    /* When chat is expanded, push popover above it */
    .chat-expanded ~ .popover,
    .chat-expanded ~ #popover {
        bottom: 55dvh;
        max-height: 40dvh;
    }

    /* Hide footer behind chat on mobile */
    .graph-footer { padding-bottom: 50px; }

    /* Tool call blocks — tighter on mobile */
    .tool-call-block { font-size: 0.65rem; }

    /* Chat messages — smaller on mobile */
    #chat-messages { padding: 8px; gap: 6px; }
    .chat-msg { font-size: 12px; max-width: 90%; }

    /* Prevent iOS auto-zoom on input focus (requires >= 16px) */
    #chat-input { font-size: 16px; }

    /* Header */
    #header h1 { font-size: 13px; }
    .sp-graph-title { font-size: 14px; letter-spacing: 1px; }
    .back-link { margin-right: 8px; }
    #stats-bar { font-size: 10px; }

    /* Move optimize button above chat toggle */
    .physics-btn {
        font-size: 12px;
        padding: 5px 14px;
    }
}

/* ── Responsive: Small Mobile (< 400px) ──────────────────────────── */

@media (max-width: 400px) {
    #chat-panel { height: 60vh; height: 60dvh; }
    #chat-input { font-size: 16px; padding: 6px 10px; } /* 16px prevents iOS auto-zoom */
    #chat-send { padding: 6px 12px; font-size: 11px; }
    .chat-input-area { padding: 6px 8px; gap: 4px; }
    .tool-call-block { padding: 3px 6px; font-size: 0.6rem; }
    .popover { max-height: 50vh; max-height: 50dvh; }
    .popover-header { font-size: 12px; }
}

/* ── Trait Profile (Character popover) ────────────────────────────── */

.trait-profile {
    margin: 8px 0 12px;
    padding: 10px;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.08);
}

.trait-archetype {
    font-size: 14px;
    font-weight: 600;
    color: #ffcc00;
    margin-bottom: 6px;
    letter-spacing: 0.5px;
}

.trait-persona-desc {
    font-size: 13px;
    color: #e0d8c8;
    margin-bottom: 8px;
    line-height: 1.4;
    font-style: italic;
    border-left: 2px solid #ffcc00;
    padding-left: 8px;
}

.trait-dominant {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-bottom: 8px;
}

.trait-badge {
    font-size: 10px;
    padding: 2px 8px;
    border-radius: 10px;
    background: rgba(255, 204, 0, 0.15);
    color: #ffcc00;
    font-weight: 500;
}

.trait-bars {
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.trait-bar-row {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
}

.trait-bar-label {
    width: 110px;
    flex-shrink: 0;
    color: #aaa;
    text-align: right;
}

.trait-bar-track {
    flex: 1;
    height: 6px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 3px;
    overflow: hidden;
}

.trait-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s ease;
}

.trait-bar-score {
    width: 32px;
    text-align: right;
    color: #888;
    font-size: 10px;
    font-family: monospace;
}

.trait-social {
    margin-top: 6px;
    font-size: 11px;
    color: #aaa;
}

/* ── Optimize Physics Button ──────────────────────────────────────── */

/* ── Physics button — SP construction-paper style ──────────────────── */

.physics-btn {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%) rotate(-0.5deg);
    padding: 6px 18px;
    font-family: 'Bangers', cursive;
    font-size: 15px;
    letter-spacing: 1.5px;
    color: #f5d442;
    text-shadow: 1px 1px 0 rgba(0,0,0,0.5);
    background: #d94444;
    border: 2px solid #2a2520;
    border-radius: 6px;
    box-shadow: 3px 3px 0 #2a2520;
    cursor: pointer;
    z-index: 20;
    animation: physics-pulse 1.8s ease-in-out infinite;
    transition: transform 0.15s, box-shadow 0.15s, background 0.15s;
    opacity: 1;
}
.physics-btn:hover {
    background: #c83b3b;
    transform: translateX(-50%) rotate(0deg) scale(1.04);
    box-shadow: 4px 4px 0 #2a2520;
}
.physics-btn:active {
    transform: translateX(-50%) scale(0.96);
    box-shadow: 2px 2px 0 #2a2520;
}
.physics-btn.hidden {
    display: none;
}
@keyframes physics-pulse {
    0%, 100% { box-shadow: 3px 3px 0 #2a2520, 0 0 0 0 rgba(217, 68, 68, 0); }
    50% { box-shadow: 3px 3px 0 #2a2520, 0 0 14px 4px rgba(217, 68, 68, 0.35); }
}
