/* ============================================
   Torah Map Explorer - Complete Redesign
   ============================================ */

/* --- Reset & Base --- */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --sidebar-width: 380px;
    --color-primary: #3b82f6;
    --color-primary-dark: #2563eb;
    --color-primary-light: #93c5fd;
    --color-bechina: #06b6d4;
    --color-eitza: #a855f7;
    --color-start: #22c55e;
    --color-end: #ef4444;
    --color-bg: #f8fafc;
    --color-surface: #ffffff;
    --color-text: #0f172a;
    --color-text-secondary: #64748b;
    --color-text-muted: #94a3b8;
    --color-border: #e2e8f0;
    --color-border-light: #f1f5f9;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
    --shadow-lg: 0 12px 32px rgba(0,0,0,0.12);
    --transition: 0.2s ease;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: var(--color-text);
    background: var(--color-bg);
}

/* --- App Layout --- */
.app {
    display: flex;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
}

/* --- Sidebar --- */
.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    z-index: 10;
    transition: margin-left 0.3s ease, opacity 0.3s ease;
    overflow: hidden;
}

.sidebar.collapsed {
    margin-left: calc(var(--sidebar-width) * -1);
}

/* Sidebar Header */
.sidebar-header {
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--color-border);
    background: linear-gradient(135deg, #eff6ff 0%, #f0f9ff 100%);
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-icon {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, var(--color-primary), #8b5cf6);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
}

.brand-icon svg {
    width: 20px;
    height: 20px;
}

.brand-title {
    font-size: 18px;
    font-weight: 700;
    color: var(--color-text);
    line-height: 1.2;
}

.brand-subtitle {
    font-size: 12px;
    color: var(--color-text-secondary);
}

.header-actions {
    display: flex;
    gap: 8px;
}

.icon-btn {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text-secondary);
    transition: all var(--transition);
}

.icon-btn:hover {
    background: var(--color-bg);
    border-color: var(--color-primary-light);
    color: var(--color-primary);
}

.icon-btn svg {
    width: 18px;
    height: 18px;
}

/* Stats Bar */
.stats-bar {
    display: flex;
    padding: 10px 20px;
    gap: 4px;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-bg);
}

.stat {
    flex: 1;
    text-align: center;
    padding: 4px 0;
}

.stat-value {
    display: block;
    font-size: 16px;
    font-weight: 700;
    color: var(--color-primary);
}

.stat-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-muted);
}

/* Sidebar Tabs */
.sidebar-tabs {
    display: flex;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-surface);
}

.sidebar-tab {
    flex: 1;
    padding: 10px 8px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    color: var(--color-text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    position: relative;
    transition: all var(--transition);
}

.sidebar-tab svg {
    width: 16px;
    height: 16px;
}

.sidebar-tab:hover {
    color: var(--color-text-secondary);
    background: var(--color-bg);
}

.sidebar-tab.active {
    color: var(--color-primary);
    font-weight: 600;
}

.sidebar-tab.active::after {
    content: '';
    position: absolute;
    bottom: -1px;
    left: 12px;
    right: 12px;
    height: 2px;
    background: var(--color-primary);
    border-radius: 2px 2px 0 0;
}

/* Tab Content */
.tab-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    display: none;
}

.tab-content.active {
    display: block;
}

/* --- Search Modes --- */
.search-modes {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 16px;
}

.mode-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 10px 4px 8px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-surface);
    cursor: pointer;
    transition: all var(--transition);
    font-size: 10px;
    font-weight: 500;
    color: var(--color-text-secondary);
}

.mode-card svg {
    width: 20px;
    height: 20px;
    stroke-width: 1.8;
}

.mode-card:hover {
    border-color: var(--color-primary-light);
    background: #eff6ff;
    color: var(--color-primary);
}

.mode-card.active {
    border-color: var(--color-primary);
    background: linear-gradient(135deg, #eff6ff, #e0f2fe);
    color: var(--color-primary);
    box-shadow: 0 0 0 1px var(--color-primary);
}

.mode-description {
    font-size: 13px;
    color: var(--color-text-secondary);
    margin-bottom: 16px;
    padding: 8px 12px;
    background: var(--color-bg);
    border-radius: var(--radius-sm);
    border-left: 3px solid var(--color-primary);
}

/* --- Search Form --- */
.search-form {
    margin-bottom: 16px;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.form-group.hidden {
    display: none;
}

.form-group label {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-text-secondary);
}

.form-group input,
.form-group select {
    padding: 10px 14px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-family: inherit;
    background: var(--color-surface);
    color: var(--color-text);
    transition: border-color var(--transition), box-shadow var(--transition);
    width: 100%;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.form-group select {
    appearance: none;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="10" viewBox="0 0 16 10"><path fill="%2364748b" d="M1 1l7 7 7-7"/></svg>');
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}

.input-spacer {
    height: 10px;
}

/* Search Button */
.search-btn {
    width: 100%;
    padding: 12px 20px;
    border: none;
    border-radius: var(--radius-md);
    background: linear-gradient(135deg, var(--color-primary), var(--color-primary-dark));
    color: white;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all var(--transition);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.search-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 20px rgba(59, 130, 246, 0.4);
}

.search-btn:active {
    transform: translateY(0);
}

.search-btn svg {
    width: 18px;
    height: 18px;
}

.search-btn.loading {
    opacity: 0.7;
    pointer-events: none;
}

.search-hint {
    margin-top: 12px;
    font-size: 12px;
    color: var(--color-text-muted);
    text-align: center;
}

/* Results Summary */
.results-summary {
    margin-top: 16px;
    padding: 12px 14px;
    background: linear-gradient(135deg, #f0fdf4, #ecfdf5);
    border: 1px solid #bbf7d0;
    border-radius: var(--radius-md);
}

.results-summary.hidden {
    display: none;
}

.results-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.results-title {
    font-weight: 600;
    font-size: 13px;
    color: #166534;
}

.clear-btn {
    border: none;
    background: none;
    color: var(--color-text-muted);
    font-size: 12px;
    cursor: pointer;
    text-decoration: underline;
    font-family: inherit;
}

.clear-btn:hover {
    color: var(--color-text-secondary);
}

.results-stats {
    font-size: 13px;
    color: #15803d;
    line-height: 1.6;
}

/* --- Details Tab --- */
.details-placeholder {
    text-align: center;
    padding: 40px 20px;
    color: var(--color-text-muted);
}

.placeholder-icon {
    width: 48px;
    height: 48px;
    margin-bottom: 12px;
    opacity: 0.4;
}

.details-placeholder p {
    font-size: 13px;
}

.hidden {
    display: none !important;
}

.detail-title {
    font-size: 18px;
    font-weight: 700;
    color: var(--color-text);
    margin-bottom: 4px;
    word-break: break-word;
}

.detail-subtitle {
    font-size: 13px;
    color: var(--color-text-secondary);
    margin-bottom: 16px;
    word-break: break-word;
}

.detail-stats {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.detail-stat {
    flex: 1;
    text-align: center;
    padding: 10px 8px;
    background: var(--color-bg);
    border-radius: var(--radius-sm);
    border: 1px solid var(--color-border-light);
}

.detail-stat-value {
    display: block;
    font-size: 20px;
    font-weight: 700;
    color: var(--color-primary);
}

.detail-stat-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-text-muted);
}

.detail-section {
    margin-bottom: 16px;
}

.detail-section h4 {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-text-secondary);
    margin-bottom: 8px;
}

.tag-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.tag {
    display: inline-block;
    padding: 4px 10px;
    font-size: 12px;
    font-weight: 500;
    background: #eff6ff;
    color: var(--color-primary);
    border-radius: 20px;
    border: 1px solid #dbeafe;
}

.detail-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 16px;
}

.action-btn {
    padding: 10px 14px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-surface);
    color: var(--color-text);
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all var(--transition);
}

.action-btn:hover {
    border-color: var(--color-primary-light);
    background: #eff6ff;
    color: var(--color-primary);
}

.action-btn svg {
    width: 16px;
    height: 16px;
}

/* Edge Details */
.edge-type-badge {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: 12px;
}

.edge-type-badge.bechina {
    background: #ecfeff;
    color: #0e7490;
    border: 1px solid #a5f3fc;
}

.edge-type-badge.eitza {
    background: #faf5ff;
    color: #7e22ce;
    border: 1px solid #e9d5ff;
}

.edge-fields {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin: 0;
}

.edge-field dt {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-text-muted);
    margin-bottom: 4px;
}

.edge-field dd {
    margin: 0;
    padding: 10px 14px;
    background: var(--color-bg);
    border-radius: var(--radius-sm);
    border: 1px solid var(--color-border-light);
    font-size: 14px;
    line-height: 1.5;
    min-height: 20px;
    word-break: break-word;
}

/* --- History Tab --- */
.history-empty {
    text-align: center;
    padding: 40px 20px;
    color: var(--color-text-muted);
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.history-item {
    padding: 12px 14px;
    background: var(--color-bg);
    border: 1px solid var(--color-border-light);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition);
}

.history-item:hover {
    border-color: var(--color-primary-light);
    background: #eff6ff;
}

.history-mode {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-primary);
    margin-bottom: 4px;
}

.history-query {
    font-size: 13px;
    color: var(--color-text);
    font-weight: 500;
    margin-bottom: 2px;
}

.history-result {
    font-size: 11px;
    color: var(--color-text-muted);
}

/* --- Toggle Sidebar Button --- */
.toggle-sidebar {
    position: fixed;
    top: 50%;
    left: var(--sidebar-width);
    transform: translateY(-50%);
    width: 28px;
    height: 64px;
    border: 1px solid var(--color-border);
    border-left: none;
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    background: var(--color-surface);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 11;
    transition: all 0.3s ease;
    box-shadow: 2px 0 8px rgba(0,0,0,0.06);
}

.toggle-sidebar:hover {
    background: var(--color-bg);
}

.toggle-sidebar svg {
    width: 16px;
    height: 16px;
    color: var(--color-text-muted);
    transition: transform 0.3s ease;
}

.sidebar.collapsed ~ .toggle-sidebar {
    left: 0;
}

.sidebar.collapsed ~ .toggle-sidebar svg {
    transform: rotate(180deg);
}

/* --- Graph Area --- */
.graph-area {
    flex: 1;
    position: relative;
    background: radial-gradient(ellipse at 30% 30%, #f0f9ff 0%, #e8f4f8 30%, #f1f5f9 100%);
    overflow: hidden;
}

#graph-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

/* Graph Controls */
.graph-controls {
    position: absolute;
    top: 16px;
    right: 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    z-index: 5;
    background: var(--color-surface);
    border-radius: var(--radius-md);
    padding: 6px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--color-border);
}

.graph-ctrl-btn {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-text-secondary);
    transition: all var(--transition);
}

.graph-ctrl-btn:hover {
    background: var(--color-bg);
    color: var(--color-primary);
}

.graph-ctrl-btn svg {
    width: 18px;
    height: 18px;
}

.ctrl-divider {
    height: 1px;
    background: var(--color-border);
    margin: 2px 4px;
}

/* Legend */
.graph-legend {
    position: absolute;
    bottom: 16px;
    right: 16px;
    background: rgba(255,255,255,0.95);
    border-radius: var(--radius-md);
    padding: 12px 16px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--color-border);
    z-index: 5;
    display: flex;
    flex-direction: column;
    gap: 6px;
    backdrop-filter: blur(8px);
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: var(--color-text-secondary);
}

.legend-line {
    width: 24px;
    height: 3px;
    border-radius: 2px;
}

.legend-line.bechina-line {
    background: var(--color-bechina);
}

.legend-line.eitza-line {
    background: var(--color-eitza);
}

.legend-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
}

.legend-dot.start-dot {
    background: var(--color-start);
}

.legend-dot.end-dot {
    background: var(--color-end);
}

/* --- Welcome Screen --- */
.welcome-screen {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 4;
    background: radial-gradient(ellipse at 30% 30%, rgba(240,249,255,0.97) 0%, rgba(232,244,248,0.97) 30%, rgba(241,245,249,0.97) 100%);
    transition: opacity 0.4s ease;
}

.welcome-screen.hidden {
    opacity: 0;
    pointer-events: none;
}

.welcome-content {
    text-align: center;
    max-width: 480px;
    padding: 40px;
}

.welcome-icon {
    margin-bottom: 24px;
}

.welcome-icon svg {
    width: 100px;
    height: 100px;
}

.welcome-content h2 {
    font-size: 28px;
    font-weight: 700;
    color: var(--color-text);
    margin-bottom: 12px;
}

.welcome-text {
    font-size: 15px;
    color: var(--color-text-secondary);
    line-height: 1.6;
    margin-bottom: 28px;
}

.welcome-stats {
    display: flex;
    justify-content: center;
    gap: 32px;
    margin-bottom: 28px;
}

.welcome-stat {
    text-align: center;
}

.ws-value {
    display: block;
    font-size: 28px;
    font-weight: 700;
    color: var(--color-primary);
    line-height: 1.2;
}

.ws-label {
    font-size: 12px;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.welcome-cta {
    font-size: 14px;
    color: var(--color-text-muted);
    font-style: italic;
}

/* --- Status Toast --- */
.status-toast {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    padding: 10px 24px;
    background: rgba(15, 23, 42, 0.9);
    color: white;
    border-radius: 24px;
    font-size: 13px;
    font-weight: 500;
    z-index: 20;
    backdrop-filter: blur(8px);
    box-shadow: var(--shadow-lg);
    transition: opacity 0.3s ease, transform 0.3s ease;
    white-space: nowrap;
}

.status-toast.hidden {
    opacity: 0;
    transform: translateX(-50%) translateY(8px);
    pointer-events: none;
}

/* --- jQuery UI Autocomplete Override --- */
.ui-autocomplete {
    font-family: 'Inter', sans-serif !important;
    font-size: 13px !important;
    max-height: 250px;
    overflow-y: auto;
    overflow-x: hidden;
    border-radius: var(--radius-sm) !important;
    border: 1px solid var(--color-border) !important;
    box-shadow: var(--shadow-md) !important;
    z-index: 100 !important;
}

.ui-menu-item-wrapper {
    padding: 8px 14px !important;
}

.ui-state-active {
    background: #eff6ff !important;
    color: var(--color-primary) !important;
    border: none !important;
}

/* --- Scrollbar --- */
.tab-content::-webkit-scrollbar {
    width: 6px;
}

.tab-content::-webkit-scrollbar-track {
    background: transparent;
}

.tab-content::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 3px;
}

.tab-content::-webkit-scrollbar-thumb:hover {
    background: var(--color-text-muted);
}

/* --- Animations --- */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

.results-summary:not(.hidden) {
    animation: fadeIn 0.3s ease;
}

.node-details:not(.hidden),
.edge-details:not(.hidden) {
    animation: fadeIn 0.3s ease;
}

.history-item {
    animation: fadeIn 0.2s ease;
}

/* --- Loading Spinner for search btn --- */
@keyframes spin {
    to { transform: rotate(360deg); }
}

.search-btn.loading svg {
    animation: spin 1s linear infinite;
}

/* --- Responsive --- */
@media (max-width: 768px) {
    :root {
        --sidebar-width: 100%;
    }

    .app {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        min-width: 100%;
        max-height: 50vh;
        border-right: none;
        border-bottom: 1px solid var(--color-border);
    }

    .sidebar.collapsed {
        margin-left: 0;
        max-height: 0;
        min-height: 0;
    }

    .toggle-sidebar {
        top: auto;
        bottom: 50%;
        left: 50%;
        transform: translateX(-50%);
        width: 64px;
        height: 28px;
        border: 1px solid var(--color-border);
        border-top: none;
        border-radius: 0 0 var(--radius-sm) var(--radius-sm);
    }

    .toggle-sidebar svg {
        transform: rotate(-90deg);
    }

    .sidebar.collapsed ~ .toggle-sidebar {
        bottom: auto;
        top: 0;
    }

    .sidebar.collapsed ~ .toggle-sidebar svg {
        transform: rotate(90deg);
    }

    .search-modes {
        grid-template-columns: repeat(4, 1fr);
    }

    .graph-legend {
        flex-direction: row;
        flex-wrap: wrap;
        bottom: 8px;
        right: 8px;
        padding: 8px 12px;
    }

    .welcome-content {
        padding: 24px;
    }

    .welcome-content h2 {
        font-size: 22px;
    }

    .welcome-stats {
        gap: 16px;
    }

    .ws-value {
        font-size: 22px;
    }
}
