/* =========================================
   1. VARIABLES & CORE OVERRIDES
   ========================================= */
:root {
  /* Colors */
  --primary-color: rgb(38, 38, 39);
  --bg-light: rgb(247, 247, 247);
  --bg-card-content: rgb(243, 244, 246); /* gray-100 */
  --bg-active-preset: rgb(209, 213, 219); /* gray-300 */
  --bg-toggle-off: rgb(229, 231, 235); /* gray-200 */
  
  --text-main: rgb(17, 24, 39);
  --text-sub: rgb(107, 114, 128);
  --border-color: rgb(229, 231, 235);
  
  /* Grade & Category Colors */
  --grade1: rgb(252, 228, 4);
  --grade2: rgb(252, 158, 32);
  --grade3: rgb(220, 54, 117);
  --grade4: rgb(106, 0, 168);
  --cat-inf: rgb(255, 149, 43);
  --cat-ind: rgb(240, 62, 62);
}

/* Core Sidebar Override (No !important needed due to load order) */
#right-sidebar:not(.collapsed) { width: 335px; }

/* =========================================
   2. UTILITIES (Replaces Tailwind)
   ========================================= */
.hidden { display: none; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-1 { flex: 1 1 0%; }
.min-h-0 { min-height: 0; }
.relative { position: relative; }
.w-full { width: 100%; }
.h-full { height: 100%; }
.items-center { align-items: center; }
.justify-center { justify-content: center; }

/* Backgrounds */
.bg-background-light { background: #1a1a1a; }

/* Viewer / Iframe */
.viewer-iframe {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
  background: #000;
}

/* Flex Container Resets */
#viewer-container,
#viewer-container > .flex,
#viewer-container main,
#viewer-container main > .flex-1,
#viewer-container main > .flex-1 > .w-full,
#viewer-container main > .flex-1 > .w-full > .w-full {
  min-height: 0;
}

/* =========================================
   3. LAYOUT & SIDEBAR
   ========================================= */
.app-frame {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

#aside {
  font-family: 'Inter', sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.sidebar { padding-top: 1rem; }

.section-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-sub);
  margin-bottom: 0.5rem;
}

/* Presets */
.preset-container {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}

.preset-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  cursor: pointer;
  gap: 0.25rem;
}

.preset-icon-box {
  width: 4rem;
  height: 4rem;
  border-radius: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--bg-light);
  transition: background-color 0.2s;
}

.preset-icon-box.active { background-color: var(--bg-active-preset); }
.preset-icon-box img { width: 2.5rem; height: 2.5rem; }

.preset-label { font-size: 0.75rem; color: var(--text-main); }
.preset-label.active { font-weight: 600; color: var(--primary-color); }

/* Control Cards */
.controls-section { margin-top: 1.5rem; }
.cards-stack { display: flex; flex-direction: column; gap: 1rem; }

.control-card {
  padding: 1rem;
  border-radius: 0.75rem;
  border: 1px solid var(--border-color);
}

.card-header {
  display: flex;
  align-items: center;
  margin-bottom: 0.75rem;
}

.card-title {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-main);
}

.card-content {
  background-color: var(--bg-card-content);
  padding: 0.75rem;
  border-radius: 0.375rem;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.disabled {
  opacity: 0.5;
  pointer-events: none;
}

/* =========================================
   4. FORM ELEMENTS
   ========================================= */
/* Toggle Switch */
.toggle-switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  margin-right: 0.75rem;
}

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

.toggle-track {
  width: 2.25rem;
  height: 1.25rem;
  background-color: var(--bg-toggle-off);
  border-radius: 9999px;
  transition: all 0.15s ease-in-out;
  position: relative;
}

.toggle-track::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  background-color: white;
  border: 1px solid rgb(209, 213, 219);
  border-radius: 50%;
  height: 1rem;
  width: 1rem;
  transition: all 0.15s ease-in-out;
}

input:checked + .toggle-track { background-color: var(--primary-color); }
input:checked + .toggle-track::after {
  transform: translateX(100%);
  border-color: white;
}
input:focus + .toggle-track {
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.5);
  outline: none;
}

/* Pill Buttons */
.pill-group-container { display: flex; align-items: center; }
.pill-group {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.75rem;
  background-color: white;
  padding: 0.125rem;
  border-radius: 0.375rem;
}

.pill-btn {
  padding: 0.25rem 0.5rem;
  border-radius: 0.375rem;
  border: none;
  background: transparent;
  cursor: pointer;
  color: var(--text-sub);
  font-family: inherit;
  font-size: inherit;
}
.pill-btn.active {
  background-color: rgb(75, 85, 99);
  color: white;
}

/* Checkboxes & Rows */
.checkbox-row { display: flex; align-items: center; }
.split-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.custom-checkbox {
  appearance: none;
  -webkit-appearance: none;
  height: 1rem;
  width: 1rem;
  border-radius: 0.25rem;
  border: 1px solid var(--border-color);
  background-color: white;
  color: var(--primary-color);
  cursor: pointer;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  margin-right: 0.5rem;
  flex-shrink: 0;
}

.custom-checkbox:checked {
  border-color: transparent;
  background-color: currentColor;
  background-image: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3cpath d='M12.207 4.793a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0l-2-2a1 1 0 011.414-1.414L6.5 9.086l4.293-4.293a1 1 0 011.414 0z'/%3e%3c/svg%3e");
}

.custom-checkbox:focus {
  outline: 2px solid transparent;
  outline-offset: 2px;
  box-shadow: 0 0 0 2px white, 0 0 0 4px var(--primary-color);
}

.checkbox-label {
  font-size: 0.875rem;
  color: var(--text-main);
  cursor: pointer;
  display: flex;
  align-items: center;
}

.label-text-stack {
  display: flex;
  flex-direction: column;
  line-height: 1.5;
}

.subtext {
  font-size: 0.75rem;
  color: var(--text-sub);
}

.submenu-indent {
  margin-left: 1rem;
  display: flex !important;
  flex-direction: column;
  gap: 0.5rem; /* Gap between submenu items */
}
/* Ensure hidden submenus stay hidden */
.submenu-indent[style*="display: none"] { display: none !important; }

/* Colored Checkbox Variants */
.custom-checkbox.grade-1 { color: var(--grade1); background-color: var(--grade1); }
.custom-checkbox.grade-2 { color: var(--grade2); background-color: var(--grade2); }
.custom-checkbox.grade-3 { color: var(--grade3); background-color: var(--grade3); }
.custom-checkbox.grade-4 { color: var(--grade4); background-color: var(--grade4); }
.custom-checkbox.cat-inf { color: var(--cat-inf); background-color: var(--cat-inf); }
.custom-checkbox.cat-ind { color: var(--cat-ind); background-color: var(--cat-ind); }

.custom-checkbox.grade-1:checked,
.custom-checkbox.grade-2:checked,
.custom-checkbox.grade-3:checked,
.custom-checkbox.grade-4:checked,
.custom-checkbox.cat-inf:checked,
.custom-checkbox.cat-ind:checked {
   background-color: currentColor; 
}

/* Checkbox Focus States */
.custom-checkbox.grade-1:focus { box-shadow: 0 0 0 2px white, 0 0 0 4px var(--grade1); }
.custom-checkbox.grade-2:focus { box-shadow: 0 0 0 2px white, 0 0 0 4px var(--grade2); }
.custom-checkbox.grade-3:focus { box-shadow: 0 0 0 2px white, 0 0 0 4px var(--grade3); }
.custom-checkbox.grade-4:focus { box-shadow: 0 0 0 2px white, 0 0 0 4px var(--grade4); }
.custom-checkbox.cat-inf:focus { box-shadow: 0 0 0 2px white, 0 0 0 4px var(--cat-inf); }
.custom-checkbox.cat-ind:focus { box-shadow: 0 0 0 2px white, 0 0 0 4px var(--cat-ind); }

.active-text { color: var(--text-main); }
.muted-text { color: var(--text-sub); }

/* =========================================
   5. MODAL
   ========================================= */
.af-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,.50);
}

.af-modal {
  background: var(--popup-bg, rgba(255,255,255,.95));
  color: var(--popup-text, #1f2a37);
  border-radius: 12px;
  box-shadow: var(--popup-shadow, 0 20px 25px -5px rgba(0,0,0,.10), 0 8px 10px -6px rgba(0,0,0,.10));
  padding: 24px;
  max-width: fit-content;
  margin: 0 auto;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.af-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  gap: 16px;
}

.af-modal-title {
  font-family: "Space Grotesk", Inter, system-ui, -apple-system, sans-serif;
  font-size: 24px;
  font-weight: 700;
  color: var(--popup-title, #111827);
}

.af-modal-close {
  border: 0;
  background: transparent;
  color: rgba(107,114,128,1);
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.af-modal-close:hover { background: rgba(15,23,42,.06); color: #0f172a; }

.af-modal-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 56px;
}
@media (min-width: 768px){ .af-modal-grid{ grid-template-columns: 1fr 1fr; } }

.af-modal-sectiontitle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 16px;
  color: var(--popup-title, #111827);
}

.af-modal-row {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin: 18px 0;
}
.af-modal-label {
  width: 80px;
  flex: 0 0 80px;
  font-weight: 600;
  color: var(--popup-muted, #6b7280);
}

/* =========================================
   6. LEGENDS
   ========================================= */
.legend-stack {
  position: absolute;
  right: 16px;
  bottom: 16px;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}

.legend-card {
  background: var(--popup-bg, rgba(255,255,255,.95));
  color: var(--popup-text, #111827);
  border-radius: 14px;
  box-shadow: var(--popup-shadow, 0 10px 28px rgba(0,0,0,.18));
  padding: 12px;
  width: auto;
  display: block;
}

.legend-card--tall { min-height: 172px; }
.legend-card--mid { min-height: 128px; }

.legend-row-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 6px;
}

.legend-content { flex: 1 1 auto; }

.legend-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.legend-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .02em;
  line-height: 1.1;
  margin: 0;
  white-space: pre-line;
}

.legend-panel {
  width: 124px;
  background: #f3f4f6;
  border-radius: 10px;
  padding: 10px;
}

.legend-subtitle {
  display: block;
  font-size: 14px;
  line-height: 1.2;
  margin: 0 0 10px 0;
  white-space: nowrap;
}
.legend-subtitle-strong { font-weight: 700; }

.legend-list {
  display: grid;
  gap: 6px;
  font-size: 14px;
  line-height: 1.2;
}

.legend-item { display: flex; align-items: center; }
.legend-item.is-disabled { color: #d1d5db; }

.legend-swatch {
  width: 16px;
  height: 16px;
  margin-right: 8px;
  flex: 0 0 16px;
}
.legend-swatch.circle { border-radius: 9999px; }
.legend-swatch.square { border-radius: 3px; }

.legend-toggle {
  border: 0;
  background: transparent;
  color: rgba(107,114,128,1);
  padding: 2px;
  border-radius: 9999px;
  line-height: 0;
}

.legend-toggle--offset {
  margin-left: 4px;
  margin-right: -6px;
  margin-top: -6px;
}

.legend-toggle:hover {
  color: #111827;
  background: rgba(17,24,39,.06);
}

.legend-toggle-icon {
  font-size: 18px;
  line-height: 18px;
  display: inline-block;
}

/* Legend Colors */
.bg-grade1-normal { background: rgb(252, 228, 4); }
.bg-grade2-normal { background: rgb(252, 158, 32); }
.bg-grade3-normal { background: rgb(220, 54, 117); }
.bg-grade4-normal { background: rgb(106, 0, 168); }
.bg-inf-normal { background: rgb(255, 149, 43); }
.bg-ind-normal { background: rgb(240, 62, 62); }

.bg-grade1-light { background: rgb(254, 249, 206); }
.bg-grade2-light { background: rgb(254, 232, 202); }
.bg-grade3-light { background: rgb(249, 223, 233); }
.bg-grade4-light { background: rgb(242, 221, 255); }
.bg-inf-light { background: rgb(255, 228, 201); }
.bg-ind-light { background: rgb(252, 220, 220); }

/* Dark Mode Support */
.dark .legend-card {
  background: rgba(55, 65, 81, .92);
  color: rgb(249, 250, 251);
}
.dark .legend-panel { background: rgba(31, 41, 55, .50); }
.dark .legend-item.is-disabled { color: rgba(75, 85, 99, 1); }
.dark .legend-toggle:hover { background: rgba(255,255,255,.08); }

/* Collapsed Legend Logic (Merged from "Fix" block) */
.legend-card.is-collapsed {
  display: flex !important;
  justify-content: center !important;
  align-items: center !important;
  padding: 0 !important;
  min-height: 0 !important;
  width: 38px !important;
  height: 38px !important;
  border-radius: 12px;
}

.legend-card.is-collapsed .legend-toggle--offset { margin: 0 !important; }

.legend-card.is-collapsed .legend-toggle {
  width: 100%;
  height: 100%;
  padding: 0;
  border-radius: inherit;
  display: grid;
  place-items: center;
}

.legend-card.is-collapsed .legend-toggle-icon {
  font-size: 20px;
  line-height: 20px;
}

.legend-card.is-collapsed .legend-content,
.legend-card.is-collapsed .legend-header,
.legend-card.is-collapsed .legend-panel {
  display: none !important;
}