/* GLOBAL & LAYOUT */
:root {
    /* Brand Colors */
    --brand-primary: #3498db;
    --brand-primary-dark: #2980b9;
    --brand-secondary: #9b59b6;
    --brand-secondary-dark: #8e44ad;
    
    /* UI Colors */
    --ui-dark: #2c3e50;
    --ui-dark-light: #34495e;
    --ui-bg-canvas: #f4f7f6;
    --ui-bg-sidebar: #ebedef;
    --ui-bg-card: #ffffff;
    --ui-bg-light: #ecf0f1;
    --ui-bg-hover: #f8f9fa;
    --ui-bg-track: #f1f1f1;
    --ui-border: #d1d4d7;
    --ui-border-light: #e0e4e8;
    --ui-border-dark: #dcdde1;
    --ui-dots: #e0e0e0;
    
    /* State Colors */
    --state-success: #27ae60;
    --state-success-light: #2ecc71;
    --state-danger: #e74c3c;
    --state-danger-light: #fdf2f2;
    --state-warning: #f1c40f;
    --state-warning-dark: #f39c12;
    --state-accent: #e67e22;
    --state-active-light: #e8f4fd;
    
    /* Text Colors */
    --text-main: #2c3e50;
    --text-muted: #7f8c8d;
    --text-light: #bdc3c7;
    --text-on-dark: #ffffff;
    
    /* Others */
    --shadow-soft: rgba(0,0,0,0.1);
    --shadow-medium: rgba(0,0,0,0.15);
    --shadow-hard: rgba(0,0,0,0.2);

    /* Algorithmic State Aliases (Semantic) */
    --alg-highlight: var(--state-warning);
    --alg-searching: var(--state-accent);
    --alg-success: var(--state-success-light);
    --alg-path: var(--brand-primary);
    --alg-error: var(--state-danger);
}

body {
    font-family: 'Segoe UI', sans-serif;
    margin: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    user-select: none;
}

body.modal-open {
    overflow: hidden;
    touch-action: none;
}

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

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

::-webkit-scrollbar-thumb {
    background: var(--text-light);
    border-radius: 10px;
}

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

#toolbar {
    background-color: var(--ui-dark);
    color: var(--text-on-dark);
    padding: 12px;
    display: flex;
    gap: 12px;
    box-shadow: 0 4px 6px var(--shadow-soft);
    z-index: 10;
    align-items: center;
}

.toolbar-group { display: flex; gap: 8px; }

.toolbar-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
}

#logo { width: 40px; height: auto; object-fit: contain; }

#toast-container {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 9999;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.toast {
    background: rgba(44, 62, 80, 0.9);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    color: white;
    padding: 12px 24px;
    border-radius: 50px;
    font-size: 14px;
    font-weight: 500;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.1);
    animation: toast-in 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    align-items: center;
    gap: 10px;
    white-space: nowrap;
}

.toast.toast-out {
    animation: toast-out 0.3s forwards;
}

@keyframes toast-in {
    from { opacity: 0; transform: translateY(20px) scale(0.9); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toast-out {
    from { opacity: 1; transform: translateY(0) scale(1); }
    to { opacity: 0; transform: translateY(-20px) scale(0.9); }
}

main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    width: 100%;
    overflow: hidden;
}

#upper-workspace {
    flex: 1;
    display: flex;
    flex-direction: row;
    min-height: 0;
    width: 100%;
}

#graph-container {
    flex: 1 1 0%;
    min-width: 0;
    width: 0;
    background-color: var(--ui-bg-canvas);
    position: relative;
    background-image: radial-gradient(var(--ui-dots) 1px, transparent 1px);
    background-size: 20px 20px;
    display: flex;
    flex-direction: column;
    align-items: stretch;
}

svg { width: 100%; height: 100%; flex: 1; display: block; touch-action: none; }

/* BUTTONS */
button {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    font-size: 14px;
    transition: all 0.2s ease;
    background-color: var(--ui-bg-light);
    color: var(--ui-dark-light);
}

button:active { transform: scale(0.96); }

button.active {
    background-color: var(--state-success);
    color: var(--text-on-dark);
    box-shadow: inset 0 2px 4px var(--shadow-hard);
}

.btn-primary {
    background-color: var(--brand-primary);
    color: var(--text-on-dark);
    padding: 10px 24px;
    border-radius: 8px;
    font-weight: 700;
    cursor: pointer;
    border: none;
}

.btn-primary:hover {
    background-color: var(--brand-primary-dark);
}

.btn-icon-text { display: flex; align-items: center; gap: 6px; padding: 6px 12px; }
.btn-icon-text .material-symbols-outlined { font-size: 20px; }

#btn-save-menu { background-color: var(--brand-primary); color: var(--text-on-dark); }
#btn-save-menu:hover { background-color: var(--brand-primary-dark); }

#btn-load-menu { background-color: var(--brand-secondary); color: var(--text-on-dark); }
#btn-load-menu:hover { background-color: var(--brand-secondary-dark); }

/* GRAPH ELEMENTS (NODES & EDGES) */
.node {
    fill: var(--brand-primary);
    stroke: var(--brand-primary-dark);
    stroke-width: 2px;
    cursor: grab;
    transition: fill 0.2s, stroke-width 0.2s, stroke 0.2s;
    will-change: transform;
}

.node[data-selected="true"] {
    stroke: var(--state-warning);
    stroke-width: 6px;
    filter: drop-shadow(0 0 8px var(--state-warning));
}

.node:hover, .node.target-hover {
    stroke: var(--state-warning);
    stroke-width: 4px;
    fill: var(--state-warning-dark);
    filter: drop-shadow(0 0 5px rgba(241, 196, 15, 0.6));
}

.node-label {
    fill: var(--text-on-dark);
    font-size: 14px;
    font-weight: bold;
    text-anchor: middle;
    dominant-baseline: central;
    pointer-events: none;
}

.node-alg-label {
    font-size: 12px;
    font-weight: bold;
    fill: var(--alg-searching);
    paint-order: stroke;
    stroke: var(--ui-bg-canvas);
    stroke-width: 3px;
}

/* Node Algorithmic States */
.node[data-alg-state="highlighted"] { fill: var(--alg-highlight); stroke: var(--state-warning-dark); }
.node[data-alg-state="highlighted"] + .node-label + .node-alg-label { fill: var(--alg-highlight); }

.node[data-alg-state="searching"] { fill: var(--alg-searching); stroke: var(--state-warning-dark); }
.node[data-alg-state="searching"] + .node-label + .node-alg-label { fill: var(--state-warning-dark); }

.node[data-alg-state="success"] { fill: var(--alg-success); stroke: var(--state-success); }
.node[data-alg-state="success"] + .node-label + .node-alg-label { fill: var(--state-success); }

.node[data-alg-state="path"] { fill: var(--alg-path); stroke: var(--brand-primary-dark); }
.node[data-alg-state="path"] + .node-label + .node-alg-label { fill: var(--brand-primary); }

.edge {
    stroke: var(--ui-dark-light);
    stroke-width: 2px;
    cursor: pointer;
    transition: stroke 0.2s, stroke-width 0.2s;
    fill: none;
}

.edge-hitarea {
    stroke: transparent;
    stroke-width: 12px;
    cursor: pointer;
    fill: none;
}

.edge:hover, .edge.hover { stroke: var(--brand-secondary); stroke-width: 4px; }

/* Special Edge States */
.edge[data-is-saturated="true"] { stroke: var(--state-danger) !important; stroke-width: 4px; }

/* Edge Algorithmic States */
.edge[data-alg-state="highlighted"] { stroke: var(--alg-highlight) !important; }
.edge[data-alg-state="searching"] { stroke: var(--alg-searching) !important; }
.edge[data-alg-state="success"] { stroke: var(--alg-success) !important; }
.edge[data-alg-state="path"] { stroke: var(--alg-path) !important; stroke-width: 4px; }
.edge[data-alg-state="error"] { stroke: var(--alg-error) !important; }

.edge-label {
    font-size: 15px;
    font-weight: bold;
    fill: var(--ui-dark);
    paint-order: stroke;
    stroke: var(--text-on-dark);
    stroke-width: 3px;
    pointer-events: none;
}

.drag-edge {
    stroke: var(--text-light);
    stroke-width: 2px;
    stroke-dasharray: 6, 4;
    pointer-events: none;
    fill: none;
}

/* DROPDOWNS */
.dropdown { position: relative; display: inline-block; }

.dropdown-content {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    background-color: var(--ui-bg-card);
    min-width: 180px;
    box-shadow: 0px 8px 16px var(--shadow-medium);
    z-index: 100;
    border-radius: 8px;
    padding: 8px 0;
    margin-top: 8px;
    border: 1px solid var(--ui-dots);
    flex-direction: column;
}

.dropdown-content.show { display: flex; }
.toolbar-right .dropdown-content { left: auto; right: 0; }

.btn-dropdown-item {
    background: none !important;
    color: var(--ui-dark-light) !important;
    width: 100%;
    text-align: left;
    padding: 10px 16px !important;
    border-radius: 0 !important;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
    font-weight: 500 !important;
    border: none;
    cursor: pointer;
}

.btn-dropdown-item:hover { background-color: var(--ui-bg-hover) !important; color: var(--brand-primary) !important; }
.dropdown-arrow { font-size: 18px !important; margin-left: 4px; transition: transform 0.2s; }

/* UI PANELS (SIDEBAR & FLOATING) */
#floating-panel {
    position: absolute;
    background: var(--ui-bg-card);
    border-radius: 8px;
    box-shadow: 0 4px 15px var(--shadow-hard);
    padding: 8px 0;
    min-width: 150px;
    z-index: 100;
    display: none;
    border: 1px solid var(--ui-dots);
}

#floating-panel.visible { display: block; }

#floating-panel .panel-title {
    padding: 8px 12px;
    font-weight: 600;
    color: var(--ui-dark);
    border-bottom: 1px solid var(--ui-bg-light);
    font-size: 13px;
}

#floating-panel .panel-row {
    padding: 8px 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

#floating-panel label {
    font-size: 11px;
    text-transform: uppercase;
    color: var(--text-muted);
    font-weight: bold;
}

#floating-panel input {
    padding: 6px;
    border: 1px solid var(--ui-border-dark);
    border-radius: 4px;
    font-size: 14px;
    outline: none;
}

#floating-panel input:focus { border-color: var(--brand-primary); }

#floating-panel .panel-delete {
    margin-top: 8px;
    padding: 10px 12px;
    cursor: pointer;
    font-size: 13px;
    color: var(--state-danger);
    border-top: 1px solid var(--ui-bg-light);
    font-weight: 600;
}

#floating-panel .panel-delete:hover { background-color: var(--state-danger-light); }

#btn-open-sidebar {
    position: absolute;
    top: 20px;
    right: 0;
    background-color: var(--ui-dark);
    color: var(--text-on-dark);
    padding: 10px 16px;
    border-radius: 8px 0 0 8px;
    z-index: 14;
    box-shadow: -2px 2px 8px var(--shadow-hard);
    font-size: 13px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    border: none;
    transition: all 0.2s ease;
}

#btn-open-sidebar:hover {
    background-color: var(--ui-dark-light);
    transform: translateX(-2px);
}

#btn-open-sidebar .material-symbols-outlined {
    font-size: 20px;
}

#log-sidebar {
    width: 300px;
    flex-shrink: 0;
    background-color: var(--ui-bg-sidebar);
    border-left: 1px solid var(--ui-border);
    display: flex;
    flex-direction: column;
    z-index: 15;
}

#log-sidebar.hidden, .hidden { display: none !important; }

.sidebar-header {
    background-color: var(--ui-dark);
    color: var(--text-on-dark);
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

#btn-close-sidebar { background: none; color: var(--text-on-dark); font-size: 24px; padding: 0; }

#log-list { 
    list-style: none; 
    padding: 12px;
    margin: 0; 
    overflow-y: auto; 
    flex-grow: 1; 
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#log-list li { 
    padding: 12px 15px; 
    background-color: var(--ui-bg-card);
    border-radius: 8px;
    box-shadow: 0 2px 5px var(--shadow-soft);
    font-size: 13px; 
    color: var(--ui-dark);
    border: 1px solid var(--ui-border-light);
    line-height: 1.4;
}

#log-list li.current { 
    background-color: var(--state-active-light); 
    border-left: 4px solid var(--brand-primary); 
    font-weight: 600; 
}

/* PLAYER BAR */
#player-bar {
    background-color: var(--ui-dark);
    color: var(--text-on-dark);
    padding: 12px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 20;
    border-top: 1px solid var(--ui-dark-light);
    width: 100%;
    box-sizing: border-box;
}

.player-info {
    flex: 1;
    display: flex;
    justify-content: flex-start;
    font-size: 13px;
    color: var(--text-light);
    font-family: monospace;
}

.player-controls {
    flex: 0 1 auto;
    display: flex;
    gap: 15px;
    align-items: center;
    justify-content: center;
}

.player-stop-container {
    flex: 1;
    display: flex;
    justify-content: flex-end;
}

.btn-icon {
    background: transparent !important;
    color: var(--ui-bg-light) !important;
    padding: 8px;
    border-radius: 50%;
    display: flex;
    width: 40px; height: 40px;
    align-items: center; justify-content: center;
}

.btn-icon:hover { background: rgba(255, 255, 255, 0.1) !important; transform: scale(1.05); }

.circle-btn { width: 48px; height: 48px; background-color: var(--state-success) !important; }
.circle-btn:hover { background-color: var(--state-success-light) !important; }

#player-stop { color: var(--state-danger) !important; }
.divider { width: 1px; height: 24px; background: rgba(255, 255, 255, 0.2); margin: 0 5px; }

/* MODALS */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding-top: 40px;
    z-index: 1000;
    backdrop-filter: blur(3px);
    overflow-y: auto;
}

.modal-content {
    background-color: var(--ui-bg-card);
    width: 90%;
    max-width: 600px;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.modal-header {
    padding: 16px 20px;
    background-color: var(--ui-dark);
    color: var(--text-on-dark);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.btn-close-modal {
    background: none;
    color: var(--text-on-dark);
    font-size: 28px;
    padding: 0;
    line-height: 1;
    border: none;
    cursor: pointer;
}

.modal-body {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.modal-body .input-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.modal-body label {
    font-size: 12px;
    font-weight: bold;
    text-transform: uppercase;
    color: var(--text-muted);
}

.modal-body textarea {
    width: 100%;
    height: 120px;
    padding: 12px;
    border: 1px solid var(--ui-border-dark);
    border-radius: 8px;
    font-family: monospace;
    font-size: 14px;
    resize: vertical;
    box-sizing: border-box;
}

.modal-body textarea[readonly] {
    background-color: var(--ui-bg-track);
    cursor: text;
}

.modal-body textarea:focus {
    outline: none;
    border-color: var(--brand-primary);
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

.modal-status {
    font-size: 13px;
    color: var(--brand-primary);
    font-weight: 600;
}

.modal-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--ui-border-light);
    display: flex;
    justify-content: flex-end;
}

.modal-header h3 {
    margin: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 1.25rem;
}

.modal-content.guide-modal {
    max-width: 600px;
    padding-top: 0; /* Let header handle top padding */
}

.modal-tabs {
    display: flex;
    border-bottom: 1px solid var(--ui-border-light);
    margin: 20px 0; /* Added vertical margin for breathing room */
    gap: 5px;
    padding: 0 5px;
}

.tab-link {
    background: none;
    border: none;
    padding: 10px 24px;
    cursor: pointer;
    color: var(--text-muted);
    font-weight: 600;
    border-radius: 8px 8px 0 0;
    transition: all 0.2s;
    border-bottom: 3px solid transparent;
}

.tab-link:hover {
    background-color: var(--ui-bg-light);
    color: var(--brand-primary);
}

.tab-link.active {
    color: var(--brand-primary) !important;
    border-bottom: 3px solid var(--brand-primary);
    background-color: transparent !important; /* Ensure no background overrides from other classes */
}

.tab-content {
    display: none;
    animation: fadeIn 0.3s;
}

.tab-content.active {
    display: block;
}

.tab-content h4 {
    margin-top: 0;
    color: var(--brand-primary);
}

.tab-content ul, .tab-content ol {
    padding-left: 20px;
    line-height: 1.6;
}

.tab-content li {
    margin-bottom: 10px;
    color: var(--text-main);
}

.guide-image {
    display: block;
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    margin: 20px auto 0;
    border: 1px solid var(--ui-border-light);
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ORIENTATION OVERLAY */
#orientation-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: var(--ui-dark);
    color: var(--text-on-dark);
    z-index: 9999;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    padding: 20px;
}

#orientation-overlay .material-symbols-outlined { font-size: 64px; margin-bottom: 20px; animation: rotateHint 2s infinite; }
@keyframes rotateHint { 0%, 100% { transform: rotate(0deg); } 50% { transform: rotate(90deg); } }

/* MOBILE RESPONSIVE */
@media (max-width: 768px) {
    #toolbar { padding: 8px; overflow-x: auto; white-space: nowrap; }
    .btn-text { display: none !important; }
    .btn-icon-text { padding: 8px !important; min-width: 44px; }
    #logo { width: 30px; }
    #log-sidebar { position: fixed; top: 0; right: 0; bottom: 0; width: 85% !important; z-index: 1000; }
    
    #player-bar { 
        padding: 8px 10px; 
        padding-bottom: calc(8px + env(safe-area-inset-bottom));
    }
    
    .player-info { flex: 0; min-width: 60px; font-size: 11px; }
    .player-controls { flex: 1; gap: 8px; }
    .player-stop-container { flex: 0; }

    .dropdown-content { position: fixed; top: 60px; left: 10px; right: 10px; width: auto; }
}

@media (max-width: 992px) and (orientation: portrait) {
    #orientation-overlay { display: flex; }
}