@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap');

/* ────────────────────────────────────────────────────────── Reset / Base */
*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    overflow: hidden;
    background: #000;
    font-family: 'Inter', sans-serif;
    color: #fff;
    -webkit-font-smoothing: antialiased;
}

canvas {
    display: block;
}

/* ────────────────────────────────────────────────────────── Top-left title */
#ui {
    position: absolute;
    top: 20px;
    left: 20px;
    pointer-events: none;
    z-index: 10;
}

#ui h1 {
    margin: 0 0 4px 0;
    font-size: 1.4rem;
    font-weight: 600;
    letter-spacing: -0.02em;
    text-shadow: 0 0 20px rgba(99, 102, 241, 0.7);
}

#ui p {
    margin: 0;
    font-size: 0.75rem;
    color: rgba(255, 255, 255, 0.45);
}

/* ────────────────────────────────────────────────────────── Left Toolbar */
#toolbar {
    position: absolute;
    left: 20px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 20;
}

.toolbar-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    width: 64px;
    padding: 12px 8px;
    background: rgba(20, 20, 35, 0.85);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    color: rgba(255, 255, 255, 0.8);
    font-family: inherit;
    font-size: 0.65rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s, transform 0.15s, box-shadow 0.2s;
    letter-spacing: 0.01em;
}

.tb-icon {
    font-size: 1.1rem;
}

.toolbar-btn:hover {
    background: rgba(99, 102, 241, 0.25);
    border-color: rgba(99, 102, 241, 0.5);
    transform: scale(1.06);
    box-shadow: 0 0 16px rgba(99, 102, 241, 0.3);
    color: #fff;
}

.toolbar-btn:active {
    transform: scale(0.98);
}

/* ────────────────────────────────────────────────────────── Right Data Panel */
#data-panel {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 390px;
    max-height: calc(100vh - 40px);
    overflow-y: auto;
    background: rgba(14, 14, 24, 0.92);
    backdrop-filter: blur(16px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.25s ease;
    z-index: 20;
    scrollbar-width: thin;
    scrollbar-color: rgba(99, 102, 241, 0.3) transparent;
}

.hidden#data-panel {
    transform: translateX(120%);
    opacity: 0;
    pointer-events: none;
}

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

.panel-header h2 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
    color: #fff;
}

.divider {
    border: none;
    border-top: 1px solid rgba(255, 255, 255, 0.07);
    margin: 14px 0;
}

/* ── Stats readout ── */
#node-info .stat-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: rgba(255, 255, 255, 0.55);
    padding: 3px 0;
}

#node-info .stat-row span:last-child {
    color: rgba(255, 255, 255, 0.85);
    font-weight: 500;
}

/* ────────────────────────────────────────────────────────── Forms */
.form-group {
    display: flex;
    flex-direction: column;
    gap: 5px;
    margin-bottom: 12px;
}

.form-group label {
    font-size: 0.72rem;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.45);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.form-row {
    display: flex;
    gap: 10px;
}

.form-group.half {
    flex: 1;
}

input[type="text"],
input[type="number"],
textarea,
select {
    width: 100%;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    padding: 8px 10px;
    color: #fff;
    font-family: inherit;
    font-size: 0.85rem;
    outline: none;
    resize: vertical;
    transition: border-color 0.2s, background 0.2s;
}

input[type="text"]:focus,
input[type="number"]:focus,
textarea:focus,
select:focus {
    border-color: rgba(99, 102, 241, 0.6);
    background: rgba(99, 102, 241, 0.08);
}

input[type="file"] {
    background: rgba(255, 255, 255, 0.05);
    border: 1px dashed rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    padding: 8px 10px;
    color: rgba(255, 255, 255, 0.65);
    font-family: inherit;
    font-size: 0.85rem;
    width: 100%;
    cursor: pointer;
}

input[type="color"] {
    width: 100%;
    height: 36px;
    padding: 2px 4px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    cursor: pointer;
}

select option {
    background: #1a1a2e;
}

/* ────────────────────────────────────────────────────────── Custom Data */
.custom-data-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    font-size: 0.72rem;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.45);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.custom-row {
    display: flex;
    gap: 6px;
    margin-bottom: 6px;
    align-items: center;
}

.custom-row input {
    flex: 1;
    margin-bottom: 0;
}

/* ────────────────────────────────────────────────────────── Buttons */
button {
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s ease;
}

.btn-primary {
    width: 100%;
    padding: 10px 16px;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    border: none;
    border-radius: 9px;
    color: #fff;
    font-size: 0.875rem;
    font-weight: 600;
    margin-bottom: 8px;
}

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

.btn-primary:active {
    transform: translateY(0);
}

.btn-secondary {
    width: 100%;
    padding: 10px 16px;
    background: rgba(99, 102, 241, 0.15);
    border: 1px solid rgba(99, 102, 241, 0.3);
    border-radius: 9px;
    color: rgba(255, 255, 255, 0.85);
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 8px;
}

.btn-secondary:hover {
    background: rgba(99, 102, 241, 0.25);
    border-color: rgba(99, 102, 241, 0.5);
}

.btn-danger {
    width: 100%;
    padding: 10px 16px;
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.25);
    border-radius: 9px;
    color: #f87171;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 8px;
}

.btn-danger:hover {
    background: rgba(239, 68, 68, 0.2);
    border-color: rgba(239, 68, 68, 0.4);
    box-shadow: 0 4px 12px rgba(239, 68, 68, 0.2);
}

.btn-ghost {
    padding: 10px 16px;
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 9px;
    color: rgba(255, 255, 255, 0.55);
    font-size: 0.875rem;
    font-weight: 500;
    flex: 1;
}

.btn-ghost:hover {
    background: rgba(255, 255, 255, 0.05);
    color: rgba(255, 255, 255, 0.85);
}

.btn-edge {
    width: 100%;
    padding: 10px 16px;
    background: rgba(6, 182, 212, 0.12);
    border: 1px solid rgba(6, 182, 212, 0.35);
    border-radius: 9px;
    color: #67e8f9;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 8px;
    transition: all 0.2s ease;
}

.btn-edge:hover {
    background: rgba(6, 182, 212, 0.25);
    border-color: rgba(6, 182, 212, 0.6);
    box-shadow: 0 0 12px rgba(6, 182, 212, 0.25);
}

.btn-edge.active {
    background: rgba(6, 182, 212, 0.3);
    border-color: #06b6d4;
    color: #fff;
    box-shadow: 0 0 16px rgba(6, 182, 212, 0.4);
    animation: pulse-edge 1.4s ease-in-out infinite;
}

@keyframes pulse-edge {

    0%,
    100% {
        box-shadow: 0 0 12px rgba(6, 182, 212, 0.35);
    }

    50% {
        box-shadow: 0 0 22px rgba(6, 182, 212, 0.65);
    }
}

/* ── Edge-pending banner ── */
#edge-pending-banner {
    position: fixed;
    bottom: 72px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: rgba(6, 182, 212, 0.18);
    border: 1px solid rgba(6, 182, 212, 0.45);
    border-radius: 10px;
    padding: 10px 22px;
    font-size: 0.85rem;
    font-weight: 500;
    color: #67e8f9;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.25s ease, transform 0.25s ease;
    z-index: 200;
    white-space: nowrap;
}

#edge-pending-banner.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.small-btn {
    padding: 3px 10px;
    background: rgba(99, 102, 241, 0.2);
    border: 1px solid rgba(99, 102, 241, 0.3);
    border-radius: 6px;
    color: rgba(255, 255, 255, 0.75);
    font-size: 0.75rem;
    font-weight: 500;
}

.small-btn:hover {
    background: rgba(99, 102, 241, 0.35);
    color: #fff;
}

.icon-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 7px;
    color: rgba(255, 255, 255, 0.45);
    font-size: 0.8rem;
    padding: 4px 8px;
    line-height: 1;
    flex-shrink: 0;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

.remove-row-btn {
    padding: 5px 7px;
    font-size: 0.7rem;
}

/* ────────────────────────────────────────────────────────── Modals */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(4px);
    z-index: 100;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal-overlay.open {
    display: flex;
}

.modal-card {
    background: rgba(14, 14, 28, 0.97);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 18px;
    padding: 28px;
    width: 100%;
    max-width: 460px;
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.8);
    animation: modal-in 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modal-in {
    from {
        opacity: 0;
        transform: scale(0.94) translateY(12px);
    }

    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.modal-header h3 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
}

.modal-hint {
    font-size: 0.82rem;
    color: rgba(255, 255, 255, 0.45);
    margin: 0 0 16px 0;
}

.modal-hint code {
    background: rgba(99, 102, 241, 0.15);
    padding: 1px 5px;
    border-radius: 4px;
    color: #a5b4fc;
    font-size: 0.8rem;
}

.modal-actions {
    display: flex;
    gap: 10px;
    margin-top: 16px;
}

.modal-actions .btn-primary {
    flex: 1.5;
    margin-bottom: 0;
}

.error-text {
    color: #f87171;
    font-size: 0.82rem;
    min-height: 1.2em;
    margin: 4px 0;
}

/* ────────────────────────────────────────────────────────── Toast */
#toast {
    position: fixed;
    bottom: 28px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: rgba(30, 30, 50, 0.95);
    border: 1px solid rgba(99, 102, 241, 0.35);
    border-radius: 10px;
    padding: 10px 20px;
    font-size: 0.85rem;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.9);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
    z-index: 200;
    white-space: nowrap;
}

#toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ────────────────────────────────────────────────────────── Chat Bar */
#chat-bar {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    height: 140px;
    /* Fixed height is more reliable than calc for layout */
    background: rgba(10, 10, 20, 0.95);
    backdrop-filter: blur(20px);
    border-top: 1px solid rgba(255, 255, 255, 0.12);
    display: flex;
    flex-direction: column;
    z-index: 1000;
    /* Ensure it's on top of everything */
    box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.8);
}

#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 8px 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    scrollbar-width: thin;
    scrollbar-color: rgba(99, 102, 241, 0.3) transparent;
}

.chat-msg {
    font-size: 0.82rem;
    line-height: 1.45;
    padding: 4px 10px;
    border-radius: 8px;
    max-width: 80%;
    word-break: break-word;
}

.chat-msg.user {
    background: rgba(99, 102, 241, 0.18);
    border: 1px solid rgba(99, 102, 241, 0.25);
    color: rgba(255, 255, 255, 0.92);
    align-self: flex-end;
}

.chat-msg.system {
    color: rgba(255, 255, 255, 0.4);
    font-style: italic;
    align-self: flex-start;
    background: none;
    border: none;
    padding-left: 2px;
}

.chat-msg.assistant {
    background: rgba(16, 185, 129, 0.12);
    border: 1px solid rgba(16, 185, 129, 0.2);
    color: rgba(255, 255, 255, 0.88);
    align-self: flex-start;
}

#chat-input-row {
    display: flex;
    gap: 8px;
    padding: 6px 12px 8px;
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    flex-shrink: 0;
}

#chat-input {
    flex: 1;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    padding: 7px 12px;
    color: #fff;
    font-family: inherit;
    font-size: 0.85rem;
    outline: none;
    resize: none;
    transition: border-color 0.2s, background 0.2s;
    margin-bottom: 0;
}

#chat-input:focus {
    border-color: rgba(99, 102, 241, 0.55);
    background: rgba(99, 102, 241, 0.08);
}

#chat-send-btn {
    padding: 7px 18px;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    border: none;
    border-radius: 8px;
    color: #fff;
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.2s ease;
    margin-bottom: 0;
    width: auto;
}

#chat-send-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

/* Push toast & banner up above the chat bar */
#toast {
    bottom: 156px;
    /* 140px + 16px */
}

#edge-pending-banner {
    bottom: 152px;
    /* 140px + 12px */
}

/* ────────────────────────────────────────────────────────── Persistence UI */
.toolbar-sep {
    height: 1px;
    background: rgba(255, 255, 255, 0.1);
    margin: 10px 4px;
}

#saved-list-container {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-height: 300px;
    overflow-y: auto;
    padding-right: 2px;
}

#saved-list-container::-webkit-scrollbar {
    width: 4px;
}

#saved-list-container::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 2px;
}

.saved-list-header {
    font-size: 0.65rem;
    text-transform: uppercase;
    color: rgba(255, 255, 255, 0.4);
    letter-spacing: 0.05em;
    padding-left: 6px;
    margin-bottom: 4px;
}

.saved-item {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 6px;
    padding: 8px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.saved-item:hover {
    background: rgba(99, 102, 241, 0.1);
    border-color: rgba(99, 102, 241, 0.3);
}

.saved-item-name {
    font-size: 0.8rem;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.9);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.saved-item-date {
    font-size: 0.6rem;
    color: rgba(255, 255, 255, 0.3);
}

.saved-item-actions {
    display: flex;
    gap: 10px;
    margin-top: 4px;
    opacity: 0;
    transition: opacity 0.2s;
}

.saved-item:hover .saved-item-actions {
    opacity: 1;
}

.saved-action {
    font-size: 0.7rem;
    color: rgba(255, 255, 255, 0.4);
    text-decoration: underline;
}

.saved-action:hover {
    color: #ef4444;
}

.modal-hint {
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.6);
    line-height: 1.4;
    margin-bottom: 16px;
}

.saved-empty {
    font-size: 0.75rem;
    color: rgba(255, 255, 255, 0.2);
    font-style: italic;
    padding-left: 6px;
}