/* === CSS Custom Properties === */
:root {
  --bg: #1e1e1e;
  --bg-secondary: #252526;
  --text: #ccc;
  --text-bright: #fff;
  --header-bg: #1a1a2e;
  --header-text: #e0e0e0;
  --accent: #007acc;
  --accent-hover: #005f99;
  --border: #3c3c3c;
  --sidebar-bg: #252526;
  --sidebar-hover: #37373d;
  --preview-bg: #ffffff;
  --preview-header: #f0f0f0;
  --graph-bg: #f8f9fa;
  --select-bg: #007acc;
  --error-bg: #ffebee;
  --error-text: #c62828;
  --error-border: #ef9a9a;
}

body.light {
  --bg: #f5f5f5;
  --bg-secondary: #e8e8e8;
  --text: #333;
  --text-bright: #111;
  --header-bg: #ffffff;
  --header-text: #333;
  --accent: #0078d4;
  --accent-hover: #106ebe;
  --border: #d4d4d4;
  --sidebar-bg: #f3f3f3;
  --sidebar-hover: #e0e0e0;
  --preview-bg: #ffffff;
  --preview-header: #e8e8e8;
  --graph-bg: #fafafa;
  --select-bg: #0078d4;
}

/* === Reset === */
* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* === Header === */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--header-bg);
  color: var(--header-text);
  padding: 8px 16px;
  gap: 12px;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
  min-height: 48px;
}

.header .title {
  font-size: 1.1rem;
  font-weight: 700;
  white-space: nowrap;
  letter-spacing: 0.3px;
}

.header .nav {
  display: flex;
  align-items: center;
  gap: 6px;
}

.header .nav label {
  font-size: 0.8rem;
  opacity: 0.7;
  white-space: nowrap;
}

/* === Buttons & Selects === */
button, select {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  background: var(--select-bg);
  color: white;
  cursor: pointer;
  font-size: 0.85rem;
  font-family: inherit;
  transition: background 0.15s;
}

select {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  padding-right: 24px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='white'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
}

button:hover, select:hover {
  background: var(--accent-hover);
}

/* === Main Layout === */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 200px 1fr 320px;
  min-height: 0;
  overflow: hidden;
}

/* === File Navigation Sidebar === */
.file-nav {
  background: var(--sidebar-bg);
  color: var(--text);
  padding: 12px;
  overflow-y: auto;
  border-right: 1px solid var(--border);
}

.file-nav h3 {
  color: var(--text-bright);
  margin-bottom: 10px;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  opacity: 0.8;
}

.file-nav ul { list-style: none; }

.file-nav li {
  padding: 6px 10px;
  cursor: pointer;
  border-radius: 4px;
  margin: 1px 0;
  position: relative;
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.1s;
}

.file-nav li:hover { background: var(--sidebar-hover); color: var(--text-bright); }
.file-nav li.active { background: var(--accent); color: white; }

/* === Context Menu === */
.context-menu {
  display: none;
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  z-index: 100;
  padding: 4px;
}

.context-menu button {
  display: block;
  width: 100%;
  padding: 4px 12px;
  background: transparent;
  color: var(--text);
  font-size: 0.8rem;
  text-align: left;
  border-radius: 2px;
}

.context-menu button:hover {
  background: var(--accent);
  color: white;
}

/* === Editor Section === */
.editor-section {
  background: #1e1e1e;
  position: relative;
  overflow: hidden;
  min-height: 0;
}

#editor-container {
  width: 100%;
  height: 100%;
}

#fallback-textarea {
  display: none;
  width: 100%;
  height: 100%;
  font-family: 'Courier New', Consolas, monospace;
  font-size: 14px;
  line-height: 1.5;
  padding: 12px;
  background: #1e1e1e;
  color: #d4d4d4;
  border: none;
  resize: none;
  outline: none;
  tab-size: 2;
}

/* === Preview Section === */
.preview-section {
  background: var(--preview-bg);
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--border);
  min-height: 0;
  overflow: hidden;
}

.preview-section h3 {
  padding: 8px 12px;
  font-size: 0.85rem;
  background: var(--preview-header);
  border-bottom: 1px solid var(--border);
  color: #555;
  flex-shrink: 0;
}

body.light .preview-section h3 { color: #555; }

#preview-frame {
  flex: 1;
  border: none;
  width: 100%;
  background: white;
}

/* === Error Box === */
.error-box {
  background: var(--error-bg);
  color: var(--error-text);
  padding: 8px 12px;
  font-size: 0.8rem;
  max-height: 100px;
  overflow-y: auto;
  display: none;
  border-top: 1px solid var(--error-border);
  flex-shrink: 0;
}

/* === Dependency Graph === */
.graph-section {
  background: var(--graph-bg);
  padding: 12px;
  border-top: 1px solid var(--border);
}

.graph-section h3 {
  font-size: 0.9rem;
  margin-bottom: 8px;
}

#dependency-graph {
  height: 350px;
  background: white;
  border: 1px solid var(--border);
  border-radius: 4px;
}

/* === Modal Dialog === */
dialog {
  background: white;
  padding: 20px;
  border: none;
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
  width: 480px;
  max-width: 90vw;
}

dialog::backdrop { background: rgba(0, 0, 0, 0.5); }

dialog h3 { margin-bottom: 12px; }

dialog form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

dialog label {
  font-size: 0.85rem;
  font-weight: 600;
}

dialog input, dialog textarea {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-family: monospace;
  font-size: 0.85rem;
}

dialog textarea { min-height: 80px; resize: vertical; }

#routes-list { margin: 12px 0; max-height: 200px; overflow-y: auto; }

.route-item {
  padding: 8px 10px;
  background: #f0f0f0;
  margin: 4px 0;
  border-radius: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.85rem;
}

.route-item button {
  padding: 2px 8px;
  font-size: 0.8rem;
  background: #e53935;
}

.route-item button:hover { background: #c62828; }

#close-modal {
  margin-top: 12px;
  width: 100%;
  background: #666;
}

#close-modal:hover { background: #444; }

/* === Responsive: Tablet === */
@media (max-width: 1000px) {
  .layout {
    grid-template-columns: 180px 1fr;
    grid-template-rows: 1fr auto;
  }
  .preview-section {
    grid-column: 1 / -1;
    height: 40vh;
  }
  .graph-section { display: none; }
}

/* === Responsive: Mobile === */
@media (max-width: 800px) {
  .header {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 8px 12px;
  }
  .header .nav {
    justify-content: space-between;
  }
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
  }
  .file-nav {
    max-height: 120px;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .editor-section { min-height: 200px; }
  #editor-container { display: none; }
  #fallback-textarea { display: block; }
  .preview-section {
    height: 40vh;
    border-left: none;
    border-top: 1px solid var(--border);
  }
  .graph-section { display: none; }
}