/* ============================================================================
   KIBO v2 — Memory Map / Graph (global: nodes injected by cytoscape)
   Extracted from app.css (monolith split). Token-based, see _tokens.css.
   ============================================================================ */

/* ─── Memory Map (Graph.razor) — page scaffold/chips/fields are generic
   (.k-page*, .k-chip, .k-field); only map-specific styles live here. ─── */

/* Heatmap collapse toggle (.k-chip) — only the mobile-only display logic here. */
.mm-heatmap-toggle { display: none; }
.mm-heatmap-chevron { transition: transform .15s; flex: 0 0 auto; }

/* Mobile modal scrim behind the graph settings panel — hidden on desktop. */
.mm-settings-scrim { display: none; }
.mm-status   { margin: .5rem 0; }
.mm-canvas-wrap {
    flex: 1 1 auto;
    position: relative;
    min-height: 0;
    /* Canvas background tracks the app theme — clusters need to pop against
       either the bright page or the dark page without manual tinting. */
    background: var(--bg-surface-2);
    border-radius: 8px;
    overflow: hidden;
    margin-bottom: 1rem;
}
.mm-canvas { position: absolute; inset: 0; }


/* ── Activity heatmap (Memory Map §16) ────────────────────────────────── */
.mm-heatmap {
    display: flex;
    flex-direction: column;
    gap: .45rem;
    margin: .35rem 0 .7rem;
    flex: 0 0 auto;
}
.mm-heatmap-ranges {
    display: flex;
    flex-wrap: wrap;
    gap: .3rem;
    align-items: center;
}
.mm-heatmap-empty {
    font-size: .78rem;
    color: var(--text-secondary, #6b7280);
    padding: .25rem 0;
}
.mm-heatmap-svg {
    display: block;
    max-width: 100%;
    overflow-x: auto;
}
.mm-axis-label {
    font-size: .5em;
}
/* GitHub-style activity scale, built from --human so it auto-adapts to dark
   (no separate dark block needed). l0/empty = neutral line colour. */
.mm-cell {
    cursor: pointer;
    transition: opacity .12s, stroke-width .12s;
    stroke: transparent;
    stroke-width: 0;
    fill: var(--line);
}
.mm-cell.empty      { fill: var(--line); opacity: .55; }
.mm-cell.mm-cell-l0 { fill: var(--line); }
.mm-cell.mm-cell-l1 { fill: color-mix(in srgb, var(--human) 28%, var(--card)); }
.mm-cell.mm-cell-l2 { fill: color-mix(in srgb, var(--human) 52%, var(--card)); }
.mm-cell.mm-cell-l3 { fill: color-mix(in srgb, var(--human) 76%, var(--card)); }
.mm-cell.mm-cell-l4 { fill: var(--human); }
.mm-cell:hover {
    stroke: var(--human);
    stroke-width: 1.5;
}
.mm-cell.active {
    stroke: var(--ink);
    stroke-width: 2;
}

/* Floating popup over the canvas (positioned by memory_map.js near the
   double-clicked node — no sidebar, by design). */
.mm-popup {
    position: absolute;
    z-index: 10;
    width: 280px;
    max-width: 320px;
    background: var(--card);
    color: var(--text-primary, #1f2937);
    border-radius: 8px;
    border: 1px solid var(--line);
    box-shadow: 0 8px 24px rgba(0,0,0,.25);
    padding: .75rem .9rem .9rem;
    font-size: .85rem;
    pointer-events: auto;
}
.mm-popup-close {
    position: absolute;
    top: 4px; right: 6px;
    background: transparent;
    border: 0;
    font-size: 1.2rem;
    line-height: 1;
    color: var(--text-secondary, #9ca3af);
    cursor: pointer;
    padding: 2px 6px;
}
.mm-popup-close:hover { color: var(--text-primary, #1f2937); }
.mm-popup-kind {
    font-size: .7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--text-secondary, #6b7280);
    margin-bottom: .2rem;
}
.mm-popup-title {
    font-weight: 600;
    margin-bottom: .35rem;
    word-break: break-word;
    line-height: 1.25;
}
.mm-popup-meta {
    font-size: .78rem;
    color: var(--text-secondary, #6b7280);
    line-height: 1.45;
    margin-bottom: .55rem;
}
.mm-popup-meta div { margin-bottom: .1rem; }
.mm-popup-body {
    font-size: .82rem;
    color: var(--text-primary, #374151);
    background: var(--paper);
    padding: .45rem .55rem;
    border-radius: 5px;
    margin-bottom: .55rem;
    max-height: 200px;
    overflow-y: auto;
    line-height: 1.4;
}
.mm-popup-actions {
    display: flex;
    gap: .4rem;
    flex-wrap: wrap;
    margin-top: .55rem;
}

/* Section block inside the popup (used for chat memory summaries) */
.mm-popup-section { margin-top: .8rem; }
.mm-popup-section h4 {
    font-size: .75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--text-secondary, #6b7280);
    margin: 0 0 .35rem;
}
.mm-summaries {
    list-style: none;
    padding: 0;
    margin: 0;
    max-height: 280px;
    overflow-y: auto;
}
.mm-summaries li {
    padding: .45rem .55rem;
    border: 1px solid var(--line);
    border-radius: 5px;
    margin-bottom: .35rem;
    background: var(--paper);
}
.mm-summary-header { font-weight: 600; font-size: .8rem; color: var(--text-primary, #1f2937); }
.mm-summary-body   { font-size: .77rem; color: var(--text-secondary, #6b7280); margin-top: .15rem; line-height: 1.4; }
.mm-summary-date   { font-size: .7rem;  color: var(--text-secondary, #9ca3af); margin-top: .15rem; }

/* First-chunk preview block on the document popup — italic, indented, gives
   a glance at what's inside the doc without expanding the chunk list. */
.mm-popup-preview-header {
    font-size: .76rem;
    font-weight: 600;
    color: var(--text-primary, #1f2937);
    margin-top: .4rem;
}
.mm-popup-preview-body {
    font-size: .77rem;
    color: var(--text-secondary, #6b7280);
    font-style: italic;
    line-height: 1.4;
    margin-top: .15rem;
    border-left: 2px solid var(--border, #e5e7eb);
    padding-left: .55rem;
}

/* Inline chunk list inside the document popup, rendered after the user
   clicks "Show chunks". Container is empty until then. */
.mm-popup-chunks { margin-top: .55rem; }
.mm-chunk-list {
    list-style: none;
    padding: 0;
    margin: 0;
    max-height: 240px;
    overflow-y: auto;
}
.mm-chunk-item {
    padding: .4rem .55rem;
    border: 1px solid var(--border, #e5e7eb);
    border-radius: 5px;
    margin-bottom: .3rem;
    background: var(--bg-surface-2, #f9fafb);
}
.mm-chunk-header { font-weight: 600; font-size: .77rem; color: var(--text-primary, #1f2937); }
.mm-chunk-body   { font-size: .74rem; color: var(--text-secondary, #6b7280); margin-top: .15rem; line-height: 1.35; }


/* ── Object-detail modal ──────────────────────────────────────────────────
   Full-screen overlay that opens when the user clicks "Visualizza" on a
   ref node. Bigger than the popup, so each kind (email/event/task/note/
   contact) can render a rich layout (all headers, body, attachments…).
   Backdrop closes on click; ESC closes; visible "×" close button. */
.mm-modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 1000;
    background: rgba(0,0,0,.55);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2vh 2vw;
}
.mm-modal {
    position: relative;
    background: var(--bg-surface, #ffffff);
    color: var(--text-primary, #1f2937);
    border: 1px solid var(--border, #e5e7eb);
    border-radius: 10px;
    box-shadow: 0 16px 48px rgba(0,0,0,.45);
    width: min(720px, 96vw);
    max-height: 92vh;
    overflow-y: auto;
    padding: 1.1rem 1.3rem 1.3rem;
}
.mm-modal-close {
    position: absolute;
    top: 6px;
    right: 10px;
    background: transparent;
    border: 0;
    font-size: 1.6rem;
    line-height: 1;
    color: var(--text-secondary, #6b7280);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
}
.mm-modal-close:hover { background: var(--bg-surface-2, #f3f4f6); color: var(--text-primary, #1f2937); }

.mm-modal-header { margin-bottom: .9rem; }
.mm-modal-kind {
    font-size: .72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-secondary, #6b7280);
    margin-bottom: .2rem;
}
.mm-modal-title {
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--text-primary, #1f2937);
    line-height: 1.3;
    word-wrap: break-word;
}
.mm-modal-meta {
    width: 100%;
    border-collapse: collapse;
    font-size: .85rem;
    margin-bottom: 1rem;
}
.mm-modal-meta th,
.mm-modal-meta td {
    padding: .35rem .55rem;
    text-align: left;
    vertical-align: top;
    border-bottom: 1px solid var(--border, #e5e7eb);
}
.mm-modal-meta th {
    font-weight: 600;
    color: var(--text-secondary, #6b7280);
    width: 110px;
    white-space: nowrap;
}
.mm-modal-meta td { word-break: break-word; }

.mm-modal-section { margin-top: 1rem; }
.mm-modal-section-label {
    font-size: .72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-secondary, #6b7280);
    margin-bottom: .35rem;
}
.mm-modal-body-text {
    margin: 0;
    padding: .7rem .85rem;
    border: 1px solid var(--border, #e5e7eb);
    border-radius: 6px;
    background: var(--bg-surface-2, #f9fafb);
    font-size: .85rem;
    color: var(--text-primary, #1f2937);
    line-height: 1.5;
    white-space: pre-wrap;
    word-wrap: break-word;
    font-family: inherit;
    max-height: 50vh;
    overflow-y: auto;
}
.mm-modal-empty {
    padding: 1rem;
    font-size: .85rem;
    color: var(--text-secondary, #6b7280);
    font-style: italic;
    text-align: center;
}


/* ─── Node labels (rendered by cytoscape-node-html-label) ──────────────────
   Color-only nodes — text appears below the dot, with type-specific tints.
   Hidden via opacity at low zoom to avoid clutter. */
.mm-node-label {
    font-size: 10px;
    line-height: 1.15;
    padding: 1px 5px;
    margin-top: 4px;
    max-width: 160px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
    /* Light theme defaults: dark-grey text on light bg with white halo */
    color: #374151;
    text-shadow: 0 0 3px #ffffff, 0 0 3px #ffffff;
    transition: opacity .25s ease;
}
[data-theme="dark"] .mm-node-label {
    color: #e7eaf2;
    text-shadow: 0 0 3px #0f172a, 0 0 3px #0f172a;
}
.mm-low-zoom .mm-node-label,
.mm-node-hidden { opacity: 0 !important; }
.mm-node-hidden { pointer-events: none; }

/* Slightly emphasize chat & document labels (they're the anchors) */
.mm-node-label-chat,
.mm-node-label-document { font-weight: 600; font-size: 11px; }


/* ─── Settings panel (Memory Map) ─────────────────────────────────────────
   Floating gear button top-right of the canvas; clicking it opens a
   panel anchored to the same corner with three collapsible-style
   sections (filters / appearance / forces). Settings persist via
   GET/PUT /api/auth/ui-preferences/memory_map. */

/* Panel uses the same theme variables as the rest of the app so it
   tracks light/dark automatically. */
.mm-settings-gear {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 12;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: var(--shadow-md);
    transition: background .15s ease, transform .15s ease, color .15s ease;
}
.mm-settings-gear:hover  { background: var(--bg-surface-2); transform: rotate(30deg); }
.mm-settings-gear:active { transform: rotate(45deg); }

.mm-settings-panel {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 11;
    width: 280px;
    max-height: calc(100% - 24px);
    overflow-y: auto;
    background: var(--bg-surface);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow-md);
    padding: 0;
    font-size: .85rem;
}

.mm-settings-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .7rem .85rem .55rem;
    border-bottom: 1px solid var(--border);
}
.mm-settings-head h3 {
    margin: 0;
    font-size: .9rem;
    font-weight: 600;
    color: var(--text-primary);
}
.mm-settings-head-actions { display: flex; gap: .25rem; }

.mm-icon-btn {
    background: transparent;
    border: 0;
    color: var(--text-secondary);
    padding: 4px 6px;
    border-radius: 4px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.mm-icon-btn:hover { background: var(--bg-surface-2); color: var(--text-primary); }

.mm-settings-section {
    padding: .7rem .85rem;
    border-bottom: 1px solid var(--border);
}
.mm-settings-section:last-of-type { border-bottom: 0; }
.mm-settings-section h4 {
    margin: 0 0 .55rem;
    font-size: .72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-secondary);
}

.mm-settings-row    { margin-bottom: .45rem; }
.mm-settings-label  { font-size: .72rem; color: var(--text-secondary); margin-bottom: .25rem; }

.mm-settings-search {
    width: 100%;
    background: var(--bg-surface-2);
    color: var(--text-primary);
    border: 1px solid var(--border-input);
    border-radius: 6px;
    padding: .35rem .55rem;
    font-size: .82rem;
    outline: none;
}
.mm-settings-search::placeholder { color: var(--text-muted); }
.mm-settings-search:focus { border-color: var(--accent); }

.mm-toggle-row {
    display: flex;
    align-items: center;
    gap: .55rem;
    padding: .25rem 0;
}
.mm-toggle-row .mm-toggle-label {
    flex: 1;
    font-size: .82rem;
    color: var(--text-primary);
}
.mm-toggle-row .form-check { padding-left: 0; }
.mm-toggle-row .form-check-input {
    margin-left: 0;
    cursor: pointer;
}
.mm-swatch {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex: 0 0 12px;
    border: 1px solid var(--border);
}

.mm-slider-row {
    margin: .45rem 0 .65rem;
}
.mm-slider-label {
    display: block;
    font-size: .72rem;
    color: var(--text-secondary);
    margin-bottom: .3rem;
}
.mm-slider-row .form-range { width: 100%; }

.mm-radio-row {
    display: flex;
    gap: .9rem;
    margin: .25rem 0 .55rem;
    font-size: .82rem;
    color: var(--text-primary);
}
.mm-radio-row label {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    cursor: pointer;
}
.mm-radio-row input[type=radio] { accent-color: var(--accent); cursor: pointer; }

.mm-rerun-btn {
    width: 100%;
    margin-top: .25rem;
}

.mm-settings-footer {
    padding: .55rem .85rem .7rem;
    border-top: 1px solid var(--border);
}


/* ── Mobile (phone ≤640px) ─────────────────────────────────────────────────
   Toolbar: title/subtitle full width, actions stacked below (right-aligned) so
   the title no longer wraps to many lines. Heatmap: collapsible. Settings
   panel: centered modal (otherwise it doesn't fit the viewport). */
@media (max-width: 640px) {
    .mm-heatmap-toggle { display: inline-flex; }
    .mm-heatmap.collapsed .mm-heatmap-body { display: none; }
    .mm-heatmap.collapsed .mm-heatmap-chevron { transform: rotate(-90deg); }

    .mm-settings-scrim {
        display: block;
        position: fixed;
        inset: 0;
        background: var(--scrim);
        z-index: 1090;
    }
    .mm-settings-panel {
        position: fixed;
        top: 50%;
        left: 50%;
        right: auto;
        transform: translate(-50%, -50%);
        width: min(340px, 92vw);
        max-height: 80dvh;
        z-index: 1100;
    }
}
