/* ============================================================
   Terraform Module Browser — Styles
   HashiCorp-inspired dark mode, blueprint aesthetic
   ============================================================ */

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

/* --- Design Tokens --- */
:root {
    --primary: #7B42BC;
    --primary-light: #9B6DD7;
    --primary-dark: #5C2D91;
    --secondary: #1C1E26;
    --accent: #00CEC9;
    --accent-light: #33E8E3;
    --bg: #0D1117;
    --bg-elevated: #161B22;
    --bg-surface: #1C2128;
    --bg-hover: #252C35;
    --text-primary: #E6EDF3;
    --text-secondary: #8B949E;
    --text-muted: #484F58;
    --border: #30363D;
    --border-light: #3D444D;
    --danger: #F85149;
    --warning: #D29922;
    --success: #3FB950;
    --info: #58A6FF;
    --aws-orange: #FF9900;
    --gcp-blue: #4285F4;
    --azure-blue: #0078D4;
    --radius: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, .3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, .4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, .5);
    --shadow-glow: 0 0 20px rgba(123, 66, 188, .15);
    --transition: 200ms ease;
    --transition-slow: 400ms ease;
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
}

/* --- Reset --- */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    line-height: 1.6;
}

/* --- Scrollbar --- */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg);
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* --- Header --- */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    background: linear-gradient(135deg, var(--bg-elevated) 0%, var(--secondary) 100%);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
    backdrop-filter: blur(10px);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: 700;
    color: #fff;
    box-shadow: var(--shadow-glow);
}

.header h1 {
    font-size: 1.25rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--text-primary), var(--primary-light));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.header h1 span {
    font-weight: 400;
    opacity: .6;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: .85rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

.btn:hover {
    background: var(--bg-hover);
    border-color: var(--border-light);
    transform: translateY(-1px);
}

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

.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border-color: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: linear-gradient(135deg, var(--primary-light), var(--primary));
    border-color: var(--primary-light);
    box-shadow: var(--shadow-glow);
}

.btn-accent {
    background: linear-gradient(135deg, var(--accent), #00b4b0);
    border-color: var(--accent);
    color: var(--bg);
    font-weight: 600;
}

.btn-accent:hover {
    box-shadow: 0 0 16px rgba(0, 206, 201, .3);
}

.btn-icon {
    padding: 8px;
    min-width: 36px;
    justify-content: center;
}

.btn-sm {
    padding: 4px 10px;
    font-size: .8rem;
}

.btn-group {
    display: flex;
    gap: 0;
}

.btn-group .btn {
    border-radius: 0;
}

.btn-group .btn:first-child {
    border-radius: var(--radius) 0 0 var(--radius);
}

.btn-group .btn:last-child {
    border-radius: 0 var(--radius) var(--radius) 0;
}

.btn-group .btn:not(:last-child) {
    border-right: none;
}

.btn-group .btn.active {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
}

/* --- Main Layout --- */
.main-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0;
    height: calc(100vh - 65px);
}

.main-layout.graph-only {
    grid-template-columns: 1fr;
}

.main-layout.graph-only .editor-panel {
    display: none;
}

/* --- Editor Panel --- */
.editor-panel {
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
    overflow: hidden;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-elevated);
    min-height: 48px;
}

.panel-title {
    font-size: .85rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: .5px;
}

.editor-area {
    flex: 1;
    position: relative;
    overflow: hidden;
}

.editor-textarea {
    width: 100%;
    height: 100%;
    background: var(--bg);
    color: var(--accent-light);
    border: none;
    padding: 16px;
    font-family: var(--font-mono);
    font-size: .875rem;
    line-height: 1.7;
    resize: none;
    outline: none;
    tab-size: 2;
}

.editor-textarea::placeholder {
    color: var(--text-muted);
}

/* --- Graph Panel --- */
.graph-panel {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

.graph-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-elevated);
    flex-wrap: wrap;
}

.search-box {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 4px 10px;
    flex: 1;
    max-width: 260px;
    transition: border-color var(--transition);
}

.search-box:focus-within {
    border-color: var(--primary);
}

.search-box svg {
    flex-shrink: 0;
    color: var(--text-muted);
}

.search-input {
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: .85rem;
    outline: none;
    width: 100%;
}

.search-input::placeholder {
    color: var(--text-muted);
}

.filter-chips {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
}

.chip {
    padding: 3px 10px;
    border-radius: 20px;
    font-size: .75rem;
    font-weight: 500;
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

.chip:hover,
.chip.active {
    border-color: var(--primary);
    color: var(--primary-light);
    background: rgba(123, 66, 188, .1);
}

.chip .chip-count {
    margin-left: 4px;
    opacity: .6;
}

/* --- Graph Canvas --- */
.graph-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background:
        radial-gradient(circle at 50% 50%, rgba(123, 66, 188, .03) 0%, transparent 70%),
        linear-gradient(var(--bg) 0%, var(--bg) 100%);
}

.graph-canvas {
    width: 100%;
    height: 100%;
    cursor: grab;
}

.graph-canvas:active {
    cursor: grabbing;
}

/* Blueprint grid on the SVG */
.graph-grid line {
    stroke: var(--border);
    stroke-width: .5;
    opacity: .2;
}

.graph-grid line.major {
    opacity: .35;
}

/* --- Node styles --- */
.node-group {
    cursor: pointer;
}

.node-rect {
    rx: 6;
    ry: 6;
    stroke-width: 1.5;
    transition: all var(--transition);
    filter: drop-shadow(0 2px 6px rgba(0, 0, 0, .3));
}

.node-group:hover .node-rect {
    stroke-width: 2.5;
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, .5));
}

.node-group.selected .node-rect {
    stroke-width: 3;
    stroke-dasharray: none;
}

.node-icon {
    font-size: 14px;
    fill: var(--text-primary);
    dominant-baseline: central;
    text-anchor: middle;
}

.node-label {
    font-family: var(--font-mono);
    font-size: 11px;
    fill: var(--text-primary);
    font-weight: 500;
}

.node-type-label {
    font-family: var(--font-sans);
    font-size: 9px;
    fill: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: .5px;
}

/* Provider color-coding */
.node-aws .node-rect {
    fill: rgba(255, 153, 0, .08);
    stroke: var(--aws-orange);
}

.node-gcp .node-rect {
    fill: rgba(66, 133, 244, .08);
    stroke: var(--gcp-blue);
}

.node-azure .node-rect {
    fill: rgba(0, 120, 212, .08);
    stroke: var(--azure-blue);
}

.node-variable .node-rect {
    fill: rgba(0, 206, 201, .08);
    stroke: var(--accent);
}

.node-output .node-rect {
    fill: rgba(63, 185, 80, .08);
    stroke: var(--success);
}

.node-module .node-rect {
    fill: rgba(123, 66, 188, .08);
    stroke: var(--primary);
}

.node-data .node-rect {
    fill: rgba(88, 166, 255, .08);
    stroke: var(--info);
}

.node-local .node-rect {
    fill: rgba(210, 153, 34, .08);
    stroke: var(--warning);
}

.node-provider .node-rect {
    fill: rgba(248, 81, 73, .08);
    stroke: var(--danger);
}

.node-default .node-rect {
    fill: rgba(139, 148, 158, .08);
    stroke: var(--text-secondary);
}

/* --- Edge styles --- */
.edge-path {
    fill: none;
    stroke: var(--border-light);
    stroke-width: 1.5;
    opacity: .6;
    transition: all var(--transition);
}

.edge-path.highlighted {
    stroke: var(--accent);
    stroke-width: 2.5;
    opacity: 1;
}

.edge-arrowhead {
    fill: var(--border-light);
}

.edge-arrowhead.highlighted {
    fill: var(--accent);
}

/* Animated draw-in */
@keyframes edgeDraw {
    from {
        stroke-dashoffset: 1000;
    }

    to {
        stroke-dashoffset: 0;
    }
}

.edge-path.animate {
    stroke-dasharray: 1000;
    animation: edgeDraw 1.2s ease forwards;
}

/* --- Detail Panel (right side overlay) --- */
.detail-overlay {
    position: absolute;
    top: 0;
    right: 0;
    width: 360px;
    height: 100%;
    background: var(--bg-elevated);
    border-left: 1px solid var(--border);
    z-index: 50;
    transform: translateX(100%);
    transition: transform var(--transition-slow);
    display: flex;
    flex-direction: column;
    box-shadow: -4px 0 20px rgba(0, 0, 0, .3);
}

.detail-overlay.open {
    transform: translateX(0);
}

.detail-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid var(--border);
}

.detail-title {
    font-family: var(--font-mono);
    font-size: .95rem;
    font-weight: 600;
}

.detail-badge {
    display: inline-flex;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: .7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .5px;
}

.detail-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.detail-section {
    margin-bottom: 20px;
}

.detail-section-title {
    font-size: .75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .5px;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.detail-code {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    font-family: var(--font-mono);
    font-size: .8rem;
    color: var(--accent-light);
    line-height: 1.6;
    overflow-x: auto;
    white-space: pre;
}

.detail-connections {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.connection-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    background: var(--bg);
    border-radius: var(--radius);
    font-size: .8rem;
    cursor: pointer;
    transition: background var(--transition);
}

.connection-item:hover {
    background: var(--bg-hover);
}

.connection-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

/* --- Diff Viewer --- */
.diff-panel {
    display: none;
}

.diff-panel.open {
    display: flex;
    flex-direction: column;
}

.diff-container {
    flex: 1;
    overflow: auto;
    padding: 16px;
}

.diff-line {
    font-family: var(--font-mono);
    font-size: .8rem;
    padding: 2px 8px;
    white-space: pre;
}

.diff-add {
    background: rgba(63, 185, 80, .1);
    color: var(--success);
}

.diff-remove {
    background: rgba(248, 81, 73, .1);
    color: var(--danger);
}

.diff-context {
    color: var(--text-secondary);
}

.diff-header {
    color: var(--info);
    font-weight: 600;
}

/* --- Pattern Alerts --- */
.pattern-alert {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 12px 16px;
    border-radius: var(--radius);
    margin-bottom: 8px;
    font-size: .85rem;
    line-height: 1.5;
}

.pattern-alert.warning {
    background: rgba(210, 153, 34, .08);
    border: 1px solid rgba(210, 153, 34, .3);
    color: var(--warning);
}

.pattern-alert.info {
    background: rgba(88, 166, 255, .08);
    border: 1px solid rgba(88, 166, 255, .3);
    color: var(--info);
}

.pattern-alert.danger {
    background: rgba(248, 81, 73, .08);
    border: 1px solid rgba(248, 81, 73, .3);
    color: var(--danger);
}

.pattern-alert-icon {
    flex-shrink: 0;
    font-size: 1.1rem;
}

/* --- Status Bar --- */
.status-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 16px;
    background: var(--secondary);
    border-top: 1px solid var(--border);
    font-size: .75rem;
    color: var(--text-muted);
}

.status-left,
.status-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.status-item {
    display: flex;
    align-items: center;
    gap: 4px;
}

.status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--success);
}

/* --- Empty State --- */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 16px;
    color: var(--text-muted);
    padding: 40px;
    text-align: center;
}

.empty-state-icon {
    font-size: 4rem;
    opacity: .3;
}

.empty-state h3 {
    font-size: 1.1rem;
    color: var(--text-secondary);
}

.empty-state p {
    max-width: 360px;
    font-size: .9rem;
    line-height: 1.6;
}

/* --- Toast Notification --- */
.toast {
    position: fixed;
    bottom: 60px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 20px;
    font-size: .85rem;
    color: var(--text-primary);
    box-shadow: var(--shadow-lg);
    opacity: 0;
    pointer-events: none;
    transition: all var(--transition);
    z-index: 200;
}

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

/* --- Loading Spinner --- */
.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* --- Modal Overlay --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .6);
    backdrop-filter: blur(4px);
    z-index: 500;
    display: none;
    align-items: center;
    justify-content: center;
}

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

.modal {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    width: 90%;
    max-width: 520px;
    box-shadow: var(--shadow-lg);
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 1.1rem;
}

.modal-body {
    padding: 24px;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 16px 24px;
    border-top: 1px solid var(--border);
}

/* --- Responsive --- */
@media (max-width: 960px) {
    .main-layout {
        grid-template-columns: 1fr;
        grid-template-rows: 40% 60%;
    }

    .editor-panel {
        border-right: none;
        border-bottom: 1px solid var(--border);
    }

    .detail-overlay {
        width: 100%;
    }

    .header {
        flex-wrap: wrap;
        gap: 8px;
    }
}

@media (max-width: 640px) {
    .header h1 {
        font-size: 1rem;
    }

    .graph-toolbar {
        gap: 4px;
    }

    .search-box {
        max-width: 100%;
    }

    .btn span.hide-mobile {
        display: none;
    }

    .filter-chips {
        display: none;
    }
}

/* --- Accessibility --- */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}