/* =============================
   CSS Custom Properties (Theming)
============================= */
:root {
  --purple-deep: #2f1746;
  --purple-medium: #5a0b73;
  --purple-light: #3a1249;
  --purple-lighter: #4a175c;
  --purple-accent: #6c2a7b;
  --purple-node: #8e44ad;
  --lavender: #e0afff;
  --red-primary: #c0392b;
  --red-hover: #a93226;
  --red-node: #c0392b;
  --orange-node: #e67e22;
  --yellow-link: #ffcf77;
  --yellow-link-hover: #ffc040;

  /* Page-level colors */
  --body-bg: var(--purple-deep);
  --body-text: #ffffff;
  --graph-bg: #f5f2ee;
  --sidebar-bg: var(--purple-deep);
  --sidebar-text: #ffffff;
  --sidebar-border: var(--purple-medium);

  /* Form / input colors */
  --label-color: #ffffff;
  --input-bg: #ffffff;
  --input-text: #333333;
  --input-border: var(--purple-medium);
  --input-shadow: rgba(0, 0, 0, 0.2);

  /* Graph elements */
  --link-color: #a0a0a0;
  --label-fill: var(--purple-medium);
  --node-stroke: #ffffff;

  /* Menus */
  --menu-bg: #f8f8f8;
  --menu-border: #ccc;
  --menu-text: #333;
  --menu-link: #007aff;

  /* Tooltip */
  --tooltip-bg: var(--purple-deep);
  --tooltip-text: #ffffff;

  /* Hint text */
  --hint-color: rgba(0, 0, 0, 0.45);
}

[data-theme="dark"] {
  --graph-bg: #1a1a2e;
  --label-fill: #c8a2e8;
  --link-color: #555577;
  --node-stroke: #2a2a4a;
  --hint-color: rgba(255, 255, 255, 0.35);

  --menu-bg: #2a2a3e;
  --menu-border: #444466;
  --menu-text: #ddd;
  --menu-link: #7eb8ff;

  --tooltip-bg: #1a1a2e;
  --tooltip-text: #ddd;
}

/* =============================
   Global Styles
============================= */
body {
  font-family: "Georgia", serif;
  margin: 0;
  padding: 0;
  background-color: var(--body-bg);
  color: var(--body-text);
  line-height: 1.6;
  display: flex;
  flex-direction: column;
  align-items: center;
}

body, html {
  overflow: visible;
}

/* =============================
   App Page Layout (index.html only)
============================= */
body.app-page {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 6px;
  box-sizing: border-box;
}

#app-layout {
  display: flex;
  flex: 1;
  min-height: 0;
  border: 5px solid var(--sidebar-border);
  border-radius: 6px;
  overflow: hidden;
}

/* --- Graph Area --- */
#graph-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--graph-bg);
  min-width: 0;
}

#graph-container {
  flex: 1;
  min-height: 0;
  position: relative;
}

#graph-svg {
  display: block;
  width: 100%;
  height: 100%;
  background-color: var(--graph-bg);
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

#hint-text {
  position: absolute;
  bottom: 12px;
  left: 0;
  right: 0;
  text-align: center;
  margin: 0;
  font-size: 13px;
  color: var(--hint-color);
  pointer-events: none;
}

/* --- Sidebar --- */
#sidebar {
  width: 340px;
  min-width: 340px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  border-left: 2px solid var(--sidebar-border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  box-sizing: border-box;
}

#sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
  flex-wrap: wrap;
  gap: 6px;
}

#sidebar-header h1 {
  font-size: 1.5rem;
  margin: 0;
}

#sidebar-header-links {
  display: flex;
  align-items: center;
  gap: 10px;
}

#sidebar-header-links a {
  color: var(--yellow-link);
  text-decoration: none;
  font-size: 0.9rem;
  font-family: Arial, sans-serif;
}

#sidebar-header-links a:hover {
  text-decoration: underline;
  color: var(--yellow-link-hover);
}

/* Dark mode toggle */
.dark-toggle {
  background: none;
  border: 1px solid rgba(255,255,255,0.3);
  color: var(--sidebar-text);
  border-radius: 4px;
  padding: 3px 8px;
  cursor: pointer;
  font-size: 14px;
  font-family: Arial, sans-serif;
  line-height: 1;
}

.dark-toggle:hover {
  background: rgba(255,255,255,0.1);
}

/* --- Sidebar fields --- */
.sidebar-field {
  margin-bottom: 14px;
}

.sidebar-field label {
  display: block;
  font-size: 15px;
  font-weight: bold;
  color: var(--label-color);
  margin-bottom: 4px;
  font-family: Arial, sans-serif;
}

.sidebar-field input[type="number"] {
  width: 100%;
  padding: 8px 10px;
  font-size: 16px; /* 16px prevents iOS zoom on focus */
  border: 1px solid var(--input-border);
  border-radius: 5px;
  background-color: var(--input-bg);
  color: var(--input-text);
  box-sizing: border-box;
  box-shadow: 0 2px 4px var(--input-shadow);
}

.sidebar-field select {
  width: 100%;
}

#sidebar #fetch-button {
  width: 100%;
  margin: 6px 0 0 0;
}

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

/* --- Slider fields --- */
.slider-field {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-family: Arial, sans-serif;
  font-size: 15px;
}

.slider-field label {
  flex: 0 0 85px;
  font-size: 15px;
  color: var(--label-color);
}

.slider-field input[type="range"] {
  flex: 1;
  min-width: 0;
}

.freeze-field {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  font-family: Arial, sans-serif;
  font-size: 15px;
}

.freeze-field label {
  font-size: 15px;
  color: var(--label-color);
}

/* --- Mobile header (hidden on desktop) --- */
#mobile-header {
  display: none;
}

/* =============================
   Top Panel Styles
============================= */
#top-panel {
  background-color: var(--purple-medium);
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  text-align: center;
  width: 80%;
  max-width: 1000px;
}

#top-panel h1 {
  font-size: 24px;
  margin: 0 0 10px;
}

#top-panel input,
#top-panel button {
  margin: 5px;
}

/* =============================
   Graph Node Styles
============================= */
.node {
  stroke: var(--node-stroke);
  stroke-width: 1.5px;
  transition: fill 0.3s ease, stroke-width 0.3s ease;
  -webkit-touch-callout: none;
  touch-action: none;
}

.node.work { fill: var(--red-node); }
.node.author { fill: var(--purple-node); }
.node.commentary { fill: var(--orange-node); }

.node:hover {
  stroke-width: 2px;
  stroke: #2c3e50;
}

.node:active {
  fill: #f39c12;
  stroke-width: 2px;
  stroke: #d35400;
}

/* =============================
   Graph Links
============================= */
.link {
  stroke: var(--link-color);
  stroke-opacity: 0.8;
  stroke-width: 1.5px;
  transition: stroke 0.3s ease;
}

.link:hover { stroke: #d63031; }

/* =============================
   Node Labels
============================= */
.label {
  font-size: 15px;
  fill: var(--label-fill);
  font-weight: bold;
  pointer-events: none;
}

/* =============================
   Tooltip Styles
============================= */
.tooltip {
  position: absolute;
  background: var(--tooltip-bg);
  color: var(--tooltip-text);
  padding: 5px 10px;
  border-radius: 5px;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

/* General menu styles */
.nested-menu {
  list-style: none;
  padding: 10px;
  margin: 0;
  background: var(--menu-bg);
  border: 1px solid var(--menu-border);
  border-radius: 5px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  font-family: sans-serif;
  font-size: 16px;
  color: var(--menu-text);
  max-width: 300px;
  position: relative;
  white-space: normal;
  overflow-wrap: break-word;
}

/* Top-level items */
.nested-menu > li {
  position: relative; /* Required for submenu positioning */
  padding: 5px 10px;
  cursor: pointer;
}

/* Submenu styles */
.submenu {
  list-style: none;
  padding: 5px 10px;
  margin: 0;
  background: var(--menu-bg);
  border: 1px solid var(--menu-border);
  border-radius: 5px;
  position: absolute;
  left: 100%; /* Pops out to the right */
  top: 0;
  display: none; /* Hidden by default */
  min-width: 160px; /* Ensure links don't get cut off */
  max-width: 300px; /* Limit width to prevent excessive stretching */
  overflow: visible; /* <-- Let child submenus extend beyond the parent box */
  white-space: normal;
  overflow-wrap: break-word;
  z-index: 1000; /* Ensure it appears on top */
  box-sizing: border-box; /* Include padding in width */
  font-size: 16px;
}

/* Submenus shown via JavaScript .submenu-open class only - no CSS hover */

/* Arrow indicator for items with submenus */
.has-submenu > span::after {
  content: " \25B8"; /* right-pointing triangle */
  font-size: 0.8em;
  opacity: 0.6;
}

/* Flip arrow when submenus open to the left */
.custom-context-menu.flip-submenus .has-submenu > span::after {
  content: " \25C2"; /* left-pointing triangle */
}

/* Sub-submenu styles (to prevent accidental closing) */
.has-submenu {
  position: relative;
}

.has-submenu .submenu {
  top: 0;
  left: 100%; /* Ensures submenu expands to the right */
}

/* Menu items */
.nested-menu a, .nested-menu button, .nested-menu span {
  text-decoration: none;
  color: var(--menu-link);
  background: none;
  border: none;
  padding: 5px 0;
  display: block;
  cursor: pointer;
  font-size: inherit;
}

/* Add consistent simple underline on hover */
.nested-menu a:hover, .nested-menu button:hover {
  text-decoration: underline;
  background: none;
}

/* Disabled buttons */
.nested-menu button:disabled {
  color: #999;
  cursor: not-allowed;
  text-decoration: none;
}

.custom-context-menu {
  position: absolute;
  background: var(--menu-bg);
  padding: 8px;
  border: 1px solid var(--menu-border);
  border-radius: 4px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  display: none;
  max-width: 300px; /* Optional: control overflow */
  overflow-wrap: break-word;
  white-space: normal;
  z-index: 10000; /* Ensure menu appears above everything */
}

/* Touch-friendly submenu toggle (works alongside hover on desktop) */
.has-submenu.submenu-open > .submenu {
  display: block;
}

/* Flip submenus to open left when menu is in right half of viewport */
.custom-context-menu.flip-submenus .submenu {
  left: auto;
  right: 100%;
}

/* =============================
   Buttons
============================= */
button {
  font-family: inherit;
  padding: 10px 20px;
  border: none;
  background: var(--red-primary);
  color: #ffffff;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.3s ease;
}

button:hover { background: var(--red-hover); }

/* =============================
   Select2 Dropdown Styles
============================= */

/* Pre-initialization: hide raw selects entirely */
.select2-initial {
  display: none;
}

/* Loading message shown while controls initialize */
#sidebar-loading-msg {
  display: none;
  text-align: center;
  padding: 2rem 1rem;
  color: var(--text-secondary, #666);
  font-size: 0.95rem;
}

#sidebar.loading #sidebar-loading-msg {
  display: block;
}

/* Hide sidebar content until Select2 is ready, then fade in */
#sidebar.loading #input-controls,
#sidebar.loading #tweak-controls,
#sidebar.loading .sidebar-divider {
  opacity: 0;
}

#sidebar #input-controls,
#sidebar #tweak-controls,
#sidebar .sidebar-divider {
  transition: opacity 0.15s ease;
}

/* =============================
   Select2 Selection Styles (Post-initialization)
============================= */
.select2-container .select2-selection--multiple {
  box-sizing: border-box;
  min-height: 40px;
  height: auto;
  padding: 5px 12px; /* Consistent padding */
  font-size: 16px;
  line-height: 1.2; /* Ensures text alignment matches */
  border: 1px solid var(--input-border);
  border-radius: 5px;
  background-color: var(--input-bg);
  color: var(--input-text);
}

/* Selected Items (Chips) in Dropdown Input */
.select2-container--default .select2-selection--multiple .select2-selection__choice {
  background-color: var(--purple-node) !important; /* Purple background */
  color: #ffffff !important; /* White text */
  border: none;
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 13px;
  margin: 2px 3px 2px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  max-width: calc(100% - 20px); /* Prevent chips from overflowing */
  overflow: hidden;
  text-overflow: ellipsis; /* Add ellipsis for long text */
  white-space: nowrap;
}

/* Remove 'X' Icon Styling */
.select2-container--default .select2-selection__choice__remove {
  color: #ffffff !important; /* White 'X' */
  margin-right: 5px;
  font-size: 12px;
  cursor: pointer;
}

/* =============================
   Select2 Results List (Dropdown Menu)
============================= */
.select2-container .select2-dropdown {
  max-height: 200px; /* Limit dropdown height */
  overflow-y: auto; /* Enable scrolling */
  background-color: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 5px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

/* Dropdown Options */
.select2-results__option {
  background-color: var(--input-bg);
  color: var(--input-text);
  padding: 8px 12px;
  overflow: hidden;
  text-overflow: ellipsis; /* Prevent long text from overflowing */
  white-space: nowrap;
}

.select2-results__option--highlighted {
  background-color: var(--orange-node) !important; /* Orange highlight */
  color: #ffffff !important;
}

/* Search Input in Dropdown */
.select2-search--dropdown .select2-search__field {
  background-color: var(--input-bg);
  color: var(--input-text);
  border: 1px solid var(--input-border);
  border-radius: 5px;
  padding: 5px;
  margin: 5px; /* Add spacing around the search bar */
  font-size: 16px; /* Prevent iOS zoom on focus */
}

/* Inline search inside multi-select */
.select2-container--default .select2-search--inline .select2-search__field {
  font-size: 16px; /* Prevent iOS zoom on focus */
}

/* =============================
   Clear-all button suppression
============================= */
.select2-container .select2-selection__clear {
  display: none !important; /* Completely hides the clear-all button */
}

/* =============================
   Sidebar Select2 width fix
============================= */
#sidebar .select2-container {
  width: 100% !important;
}

/* =============================
   Shared Container Styles
============================= */
.shared-container {
  max-width: 800px;
  margin: 40px auto;
  padding: 36px 40px;
  background-color: var(--purple-light);
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  border-top: 3px solid var(--purple-node);
  box-sizing: border-box;
  width: calc(100% - 32px); /* always leave 16px on each side */
}

.shared-container h1 {
  font-size: 2.2rem;
  margin-bottom: 24px;
  color: var(--lavender);
  text-align: center;
  letter-spacing: 0.02em;
}

.shared-container h2 {
  font-size: 1.4rem;
  color: var(--lavender);
  margin-top: 28px;
  margin-bottom: 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.shared-container p {
  font-size: 1.1rem;
  line-height: 1.7;
  margin-bottom: 18px;
}

.shared-container ul {
  list-style-type: none;
  padding: 0;
  padding-left: 20px;
  margin: 16px 0;
}

.shared-container ul ul {
  margin: 8px 0;
}

.shared-container li {
  margin: 8px 0;
  font-size: 1.05rem;
  line-height: 1.6;
  position: relative;
  padding-left: 18px;
}

.shared-container li::before {
  content: "\25B8";
  position: absolute;
  left: 0;
  color: var(--purple-node);
  font-size: 0.8em;
  top: 2px;
}

.shared-container a {
  color: var(--yellow-link);
  text-decoration: none;
  transition: color 0.2s ease;
}

.shared-container a:hover {
  text-decoration: underline;
  color: var(--yellow-link-hover);
}

.shared-container code {
  font-family: "SF Mono", "Consolas", "Liberation Mono", monospace;
  background: rgba(255, 255, 255, 0.08);
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 0.9em;
  color: var(--lavender);
}

.shared-container .footer {
  margin-top: 36px;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  text-align: center;
  font-size: 1rem;
}

.shared-container .footer p {
  margin-bottom: 8px;
  font-size: 0.95rem;
}

.shared-container .footer a {
  color: var(--lavender);
  padding: 4px 12px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 4px;
  transition: background 0.2s ease, color 0.2s ease;
}

.shared-container .footer a:hover {
  background: rgba(255, 255, 255, 0.08);
  text-decoration: none;
  color: #ffffff;
}

/* Responsive video */
.shared-container video {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  display: block;
  margin: 0 auto;
}

/* =============================
   Specific Overrides
============================= */
#about-container {
  text-align: center;
}

#about-container ul {
  padding-left: 0;
}

#about-container li {
  padding-left: 0;
}

#about-container li::before {
  display: none;
}

#data-notes-container {
  text-align: left; /* Left alignment for Data Notes page */
}

#fancy-table {
  width: 100%;
  max-width: 800px;
  margin: 24px auto;
  border-collapse: collapse;
  background-color: var(--purple-light);
  border: 1px solid var(--purple-medium);
  border-radius: 6px;
  overflow: hidden;
  font-size: 1.05rem;
}

#fancy-table th,
#fancy-table td {
  padding: 12px 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: #ffffff;
}

/* Header row */
#fancy-table th {
  background-color: var(--purple-medium);
  font-weight: bold;
  text-align: center;
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Body rows */
#fancy-table tbody td {
  text-align: center;
}

#fancy-table tbody tr {
  transition: background-color 0.15s ease;
}

#fancy-table tbody tr:nth-child(even) {
  background-color: var(--purple-lighter);
}

#fancy-table tbody tr:hover {
  background-color: var(--purple-accent);
}

/* Footer row using <tfoot> */
#fancy-table tfoot {
  background-color: var(--purple-accent);
  font-weight: bold;
}

#fancy-table tfoot td {
  text-align: center;
}

/* Optional: fallback if using tbody .summary-row instead of <tfoot> */
#fancy-table tbody .summary-row {
  font-weight: bold;
  background-color: var(--purple-accent);
}

/* Horizontally scrollable table wrapper */
.table-scroll-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 24px -8px;
  padding: 0 8px;
}

.table-scroll-wrapper #fancy-table {
  min-width: 500px;
}

/* Mobile responsive: shared container + tables */
@media (max-width: 600px) {
  .shared-container {
    padding: 24px 18px;
  }

  .shared-container h1 {
    font-size: 1.7rem;
  }

  .shared-container h2 {
    font-size: 1.2rem;
  }

  .shared-container p {
    font-size: 1rem;
  }

  .shared-container li {
    font-size: 0.95rem;
  }

  #fancy-table {
    font-size: 0.85rem;
  }

  #fancy-table th,
  #fancy-table td {
    padding: 8px 10px;
  }

  #fancy-table th {
    font-size: 0.75rem;
  }
}

/* =============================
   Mobile Context Menu Improvements
============================= */
@media (max-width: 768px), (hover: none) {
  /* Larger tap targets on touch devices */
  .nested-menu > li {
    padding: 10px 12px;
  }

  .nested-menu a, .nested-menu button, .nested-menu span {
    padding: 8px 0;
    min-height: 44px; /* Apple's recommended minimum tap target */
    display: flex;
    align-items: center;
  }

  .submenu li {
    padding: 6px 10px;
  }

  /* Prevent menu from being too wide on mobile */
  .custom-context-menu {
    max-width: calc(100vw - 20px);
    max-height: 80vh;
    overflow-y: auto;
  }

  /* Accordion-style submenus: expand vertically instead of horizontally */
  .submenu {
    position: relative !important;
    left: auto !important;
    right: auto !important;
    top: auto !important;
    bottom: auto !important;
    max-width: 100%;
    margin-left: 12px;
    margin-top: 4px;
    border: none;
    border-left: 2px solid var(--menu-border);
    box-shadow: none;
    background: transparent;
  }

  /* Visual feedback for active submenu parent */
  .has-submenu.submenu-open > span {
    font-weight: bold;
  }

  /* Rotate arrow to indicate expanded state */
  .has-submenu.submenu-open > span::after {
    content: " \25BE"; /* down-pointing triangle */
  }
}

/* =============================
   Responsive: Mobile (< 768px)
============================= */
@media (max-width: 768px) {
  body.app-page {
    height: 100vh;
    height: 100dvh;
  }

  #mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--sidebar-bg);
    padding: 10px 16px;
    border-bottom: 2px solid var(--sidebar-border);
  }

  #mobile-header h1 {
    font-size: 1.2rem;
    margin: 0;
  }

  #mobile-header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
  }

  #mobile-header-actions a {
    color: var(--yellow-link);
    text-decoration: none;
    font-size: 0.9rem;
    font-family: Arial, sans-serif;
  }

  #mobile-header-actions a:hover {
    text-decoration: underline;
    color: var(--yellow-link-hover);
  }

  #sidebar-toggle {
    background: none;
    border: 1px solid rgba(255,255,255,0.3);
    color: var(--sidebar-text);
    font-size: 20px;
    padding: 4px 10px;
    border-radius: 4px;
    cursor: pointer;
    line-height: 1;
  }

  #sidebar-toggle:hover {
    background: rgba(255,255,255,0.1);
  }

  #app-layout {
    flex-direction: column;
  }

  #sidebar {
    width: 100% !important;
    min-width: 100% !important;
    border-left: none;
    border-bottom: 2px solid var(--sidebar-border);
    max-height: 0;
    overflow: hidden;
    padding: 0 16px;
    transition: max-height 0.3s ease, padding 0.3s ease;
  }

  #sidebar.open {
    max-height: 80vh;
    overflow-y: auto;
    padding: 16px;
  }

  #sidebar-header {
    display: none;
  }

  #graph-area {
    flex: 1;
    min-height: 0;
  }
}

.ext-link-icon {
  width: 0.65em;
  height: 0.65em;
  margin-left: 0.15em;
  vertical-align: baseline;
  position: relative;
  top: -0.1em;
}
