/* =================================================================
   Cyber Fidget Studio - Main Layout
   ================================================================= */

/* --- 1. Main Studio Layout Grid --- */
body {
  overflow-y: hidden; /* Prevent the whole page from scrolling */
}

.studio-layout {
  display: grid;
  grid-template-columns: 50px 300px 1fr;
  grid-template-rows: 1fr;
  grid-template-areas:
    "activity sidebar main";

 /* 100vh = 100% of the viewport height
   * - 70px = (approximate height of site-header)
   * - 130px = (approximate height of main.container title + padding)
   * - 50px = (approximate height of site-footer)
   * (You can adjust 250px if it's slightly off)
  */
  height: calc(100vh - 300px);

  min-height: 600px;
  overflow: hidden;
  background-color: var(--pico-card-background-color);

  /* Make it full-width */
  width: 100%;
  border-radius: 0;
  border: 1px solid var(--pico-form-element-border-color);
  border-left: none;
  border-right: none;
}

.studio-activity-bar {
  grid-area: activity;
  background-color: var(--pico-background-color);
  border-right: 1px solid var(--pico-form-element-border-color);
  display: flex;
  flex-direction: column;
  padding: 0.5rem 0;
  gap: 0.5rem;
}

.studio-sidebar {
  grid-area: sidebar;
  background-color: var(--pico-card-background-color);
  border-right: 1px solid var(--pico-form-element-border-color);
  overflow-y: auto;
  resize: horizontal;
  min-width: 250px;
  max-width: 60%;
}

.studio-main {
  grid-area: main;
  overflow: hidden; /* Prevent this area from scrolling */
  position: relative;
  display: flex;
  flex-direction: column;
}

.studio-status-bar {
  /* grid-area: status; (Remove) */
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 1rem;
  background-color: var(--pico-background-color);
  border-top: 1px solid var(--pico-form-element-border-color);
  font-size: 0.9rem;
  height: 30px;
  flex: 0 0 30px; /* Make it a non-growing flex item */
}

/* --- 2. Panel Switching Logic --- */

/* All panels hidden by default */
.sidebar-panel,
.main-panel {
  display: none;
  height: 100%;
  width: 100%;
}
/* Show the active panel */
.sidebar-panel.active,
.main-panel.active {
  display: block;
}
/* Ensure main panels fill the space */
.main-panel {
  overflow-y: auto; /* Allow scrolling *within* the main panel */
}

/* --- 3. Activity Bar Styling --- */

.activity-button {
  background: none;
  border: none;
  color: var(--pico-muted-color);
  padding: 0.75rem;
  cursor: pointer;
  width: 100%;
  aspect-ratio: 1 / 1;
}
.activity-button svg {
  width: 100%;
  height: 100%;
}
.activity-button:hover {
  color: var(--pico-primary);
}
.activity-button.active {
  color: var(--pico-primary);
  border-left: 3px solid var(--pico-primary);
}

/* --- 4. Sidebar Styling --- */

.studio-sidebar section,
.studio-sidebar details {
  padding: 1rem;
  border-bottom: 1px solid var(--pico-form-element-border-color);
}
.studio-sidebar details {
  padding: 0.75rem 1rem;
}
.studio-sidebar details[open] {
  padding-bottom: 1rem;
}
.studio-sidebar summary {
  font-weight: bold;
  font-size: 1.1em;
}
.studio-sidebar details > .box {
  padding: 0;
  margin-top: 1rem;
}
.studio-sidebar h2,
.studio-sidebar h3,
.studio-sidebar h4 {
  margin-top: 0;
  margin-bottom: 0.75rem;
  font-size: 1.1em;
}
.studio-sidebar .form-group {
  margin-bottom: 0.75rem;
}
.studio-sidebar .form-group label {
  font-weight: bold;
  margin-bottom: 0.25rem;
  display: block;
}
.studio-sidebar .button-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}
.studio-sidebar .button-group .button {
  flex-grow: 1;
}
/* New repo select styles */
.studio-sidebar select,
.studio-sidebar input[type="text"] {
  width: 100%;
}

/* =================================================================
   Pixel Sandbox - Migrated Styles
   ================================================================= */
/* --- 1. Pixel Grid & Main Area --- */
:root {
  --pixel-size: 4px; /* Start with a medium size */
  --pixel-grid-lines: #555;
  --pixel-off: #222;
  --pixel-on: #fff;
  --grid-width: calc(128 * var(--pixel-size));
  --grid-height: calc(64 * var(--pixel-size));
}

.pixel-sandbox-main-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  
  /* This is the key: allow this panel to scroll */
  overflow: auto; 
}

.pixel-sandbox-main-content h3 {
  flex-shrink: 0; /* Don't shrink the title */
  margin: 0 0 0.5rem 0;
}

#pixel-grid-container {
  width: var(--grid-width);
  height: var(--grid-height);
  
  border: 2px solid var(--pico-primary);
  overflow: hidden;
  touch-action: none;
  position: relative;
  background-color: var(--pixel-off);
  box-sizing: content-box;
  
  /* Add flex-shrink to prevent it from vanishing */
  flex-shrink: 0; 
}

#pixel-grid {
  display: grid;
  grid-template-columns: repeat(128, 1fr);
  grid-template-rows: repeat(64, 1fr);
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  position: relative;
  z-index: 1;
}

#pixel-grid.tool-move { cursor: move; }
#pixel-grid.tool-select { cursor: crosshair; }
#pixel-grid.show-gridlines .pixel {
  box-shadow: 0 0 0 1px var(--pixel-grid-lines) inset;
}
#pixel-grid .pixel {
  width: 100%;
  height: 100%;
  background-color: transparent;
}
#pixel-grid .pixel.on {
  background-color: var(--pixel-on);
}

/* Outlines */
#layer-outline,
#selection-rect {
  position: absolute;
  box-sizing: border-box;
  pointer-events: none;
  display: none;
  z-index: 2;
  image-rendering: pixelated;
}
#layer-outline {
  box-shadow: 0 0 0 2px var(--pico-primary-focus);
  width: calc(100% + 4px);
  height: calc(100% + 4px);
  top: -2px;
  left: -2px;
}
#selection-rect {
  border: 1px dashed rgba(255, 255, 255, 0.8);
  background-color: rgba(0, 191, 255, 0.1);
}

/* --- 2. Live Preview --- */
#real-preview-container {
  width: 100%;
  max-width: 400px; /* Make preview smaller */
  flex-shrink: 0; /* Don't shrink the preview area */
  
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  border: 1px dashed var(--pico-form-element-border-color);
  box-sizing: border-box;
}

#real-preview-container h3 {
  font-size: 1rem;
  margin: 0;
}
#real-preview-canvas {
  width: 100%;
  max-width: calc(43.2mm * 4); /* Max size */
  height: auto;
  aspect-ratio: 128 / 64;
  border: 1px solid var(--pico-primary);
  background-color: var(--pixel-off);
  image-rendering: pixelated;
}
/* New Preview Controls */
#preview-controls {
  width: 100%;
  max-width: 400px;
  justify-content: center;
}
#preview-controls input[type="range"] {
  flex-grow: 1;
}
#preview-controls span {
  width: 60px;
  text-align: right;
  font-size: 0.9em;
}

/* --- 3. Sidebar Lists (Frames/Layers) --- */
.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}
.list-header h2 { margin: 0; }
.nav-buttons button {
  padding: 2px 6px;
  font-size: 1em;
  line-height: 1;
  min-width: 28px;
}
/* New AI-friendly controls */
#frame-select-dropdown {
    width: 100%;
    margin-bottom: 0.5rem;
}
#btn-duplicate-frame {
    width: 100%;
}

.active-layer-info {
  font-style: italic;
  color: var(--brand-muted);
  margin-bottom: 0.5rem;
  text-align: center;
  background: rgba(255, 255, 255, 0.03);
  padding: 4px;
  border-radius: 4px;
}
.active-layer-info span { font-weight: bold; }

#frame-list,
#layer-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--pico-form-element-border-color);
  border-radius: var(--pico-border-radius);
}
#frame-list li,
#layer-list li {
  padding: 10px;
  background-color: var(--pico-form-element-background-color);
  border-bottom: 1px solid var(--pico-form-element-border-color);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}
#frame-list li:last-child,
#layer-list li:last-child { border-bottom: none; }
#frame-list li:hover,
#layer-list li:hover {
  background-color: var(--pico-form-element-focus-border-color);
}
#frame-list li.active,
#layer-list li.active {
  background-color: var(--pico-primary);
  color: var(--pico-primary-inverse);
  font-weight: bold;
  border-color: var(--pico-primary);
}
#frame-list li button,
#layer-list li button {
  --pico-font-size: 12px;
  --pico-padding: 0.2rem 0.5rem;
  background-color: var(--pico-muted-border-color);
  color: var(--pico-color);
  flex-shrink: 0;
}
#frame-list li button:hover,
#layer-list li button:hover {
  background-color: var(--pico-danger-hover);
  color: var(--pico-danger-inverse);
}
#frame-list li input,
#layer-list li input {
  flex-grow: 1;
  margin: 0 5px;
  padding: 2px 5px;
  font-size: inherit;
  background-color: var(--pico-form-element-background-color);
  color: var(--pico-color);
  border: 1px solid var(--pico-primary);
  border-radius: var(--pico-border-radius);
}
#frame-list li .list-item-content,
#layer-list li .list-item-content {
  display: flex;
  align-items: center;
  flex-grow: 1;
  overflow: hidden;
}
#frame-list li .frame-name,
#layer-list li .layer-name {
  flex-grow: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin: 0 5px;
  cursor: text;
}
#layer-list li .layer-visibility {
  cursor: pointer;
  opacity: 0.7;
  flex-shrink: 0;
}
#layer-list li .layer-visibility.visible { opacity: 1; }
#layer-list li.dragging { opacity: 0.5; background: #555; }

/* --- 4. Sidebar Code Output --- */
#code-output-container { width: 100%; }
#code-output-container .code-tabs {
  display: flex;
  border-bottom: 1px solid var(--pico-form-element-border-color);
}
#code-output-container .code-tab {
  padding: 10px 15px;
  cursor: pointer;
  background-color: var(--pico-form-element-background-color);
  border-radius: var(--pico-border-radius) var(--pico-border-radius) 0 0;
  margin-right: 5px;
  border: 1px solid var(--pico-form-element-border-color);
  border-bottom: none;
}
#code-output-container .code-tab.active {
  background-color: var(--pico-card-background-color);
  border-bottom: 1px solid var(--pico-card-background-color);
  position: relative;
  bottom: -1px;
  color: var(--pico-primary);
}
#code-output-container .code-panel { display: none; position: relative; }
#code-output-container .code-panel.active { display: block; }
#code-output-container textarea {
  background-color: #1e1e1e;
  color: #f0f0f0;
  padding: 15px;
  border-radius: 0 5px 5px 5px;
  border: 1px solid var(--pico-form-element-border-color);
  height: 400px;
  overflow-y: auto;
  white-space: pre;
  word-wrap: normal;
  overflow-x: auto;
  margin-top: -1px;
  font-family: "Courier New", Courier, monospace;
  font-size: 14px;
  width: 100%;
  resize: vertical;
}
#code-output-container .copy-button {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: #555;
  color: white;
  padding: 5px 10px;
  font-size: 12px;
  z-index: 10;
  border: none;
}
#code-output-container .copy-button:hover { background-color: #666; }

/* --- 5. New AI-Friendly Controls --- */
.nudge-controls {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 5px;
  width: 120px;
  margin: 10px 0;
}
.nudge-button {
    --pico-font-size: 1.2rem;
    padding: 0.25rem;
}
#nudge-up { grid-column: 2; grid-row: 1; }
#nudge-left { grid-column: 1; grid-row: 2; }
#nudge-down { grid-column: 2; grid-row: 2; }
#nudge-right { grid-column: 3; grid-row: 2; }


/* =================================================================
   Web IDE - Migrated Styles
   ================================================================= */
.file-tree {
  list-style: none;
  padding-left: 0;
  font-family: monospace;
}
.file-tree li {
  padding: 0.25rem 0.5rem;
  cursor: pointer;
  border-radius: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-tree li:hover { background-color: var(--pico-primary-background); }
.file-tree li.active {
  background-color: var(--pico-primary);
  color: var(--pico-primary-inverse);
  font-weight: bold;
}
.file-tree .icon { margin-right: 0.5rem; }

/* Main IDE Panel */
.main-panel {
  display: none;
  height: 100%; /* Should be handled by flex */
  width: 100%;
  flex: 1 1 auto; /* This makes it fill the space */
  overflow-y: auto; /* The panel itself will scroll, not the page */
}

#ace-editor {
  width: 100%;
  height: 100%;
  min-height: 500px; /* Kept from original */
}

#flash-controls-panel {
    padding: 1.5rem;
}

/* =================================================================
   Responsive & Mobile
   ================================================================= */
@media (max-width: 992px) {
  .studio-layout {
    /* Stack sidebar, main, and activity bar */
    grid-template-columns: 1fr;
    grid-template-rows: 50px 300px 1fr 30px;
    grid-template-areas:
      "activity"
      "sidebar"
      "main"
      "status";
    height: auto; /* Let content define height */
    min-height: 100vh;
  }

  .studio-activity-bar {
    flex-direction: row;
    padding: 0 0.5rem;
    border-right: none;
    border-bottom: 1px solid var(--pico-form-element-border-color);
  }
  .activity-button {
      width: 50px;
      padding: 0.5rem;
  }
  .activity-button.active {
      border-left: none;
      border-bottom: 3px solid var(--pico-primary);
  }

  .studio-sidebar {
    resize: none; /* Disable horizontal resize */
    border-right: none;
    border-bottom: 1px solid var(--pico-form-element-border-color);
    max-width: 100%;
  }

  :root {
    --pixel-size: 3px;
  }
  #real-preview-container {
      resize: none;
  }
}

/* =================================================================
   Expandable File Tree
   ================================================================= */

.file-tree {
  font-family: monospace;
  font-size: 0.9em;
  padding-left: 0;
  list-style-type: none;
}
.file-tree ul {
  padding-left: 1.25rem;
  list-style-type: none;
}
.file-tree li {
  padding: 0;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-tree .tree-entry {
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
}
.file-tree .tree-entry:hover {
  background-color: var(--pico-primary-background);
}
.file-tree .tree-entry.active {
  background-color: var(--pico-primary);
  color: var(--pico-primary-inverse);
  font-weight: bold;
}
.file-tree .icon {
  margin-right: 0.5rem;
  display: inline-block;
  width: 1em;
  text-align: center;
}
.file-tree .folder .folder-icon::before {
  content: '📁'; /* Closed */
}
.file-tree .folder.open > .tree-entry .folder-icon::before {
  content: '📂'; /* Open */
}
.file-tree .file .icon::before {
  content: '📄';
}
/* Hide sub-folders by default */
.file-tree .folder > ul {
  display: none;
}
/* Show sub-folders when .open is added */
.file-tree .folder.open > ul {
  display: block;
}

/* =================================================================
   File Tree Loader
   ================================================================= */
.file-tree-loading-spinner {
    width: 1.5rem;
    height: 1.5rem;
    border-radius: 50%;
    border: 3px solid var(--pico-muted-border-color);
    border-top-color: var(--pico-primary);
    animation: spin 1s ease-in-out infinite;
    margin: 1rem auto;
}
@keyframes spin {
    to { transform: rotate(360deg); }
}

/* =================================================================
   Pixel Grid Responsiveness
   ================================================================= */

/* iPad Mini Portrait (820px wide) & smaller */
@media (max-width: 820px) {
  :root {
    --pixel-size: 3px; /* Make pixels smaller */
  }
}

/* Phones (approx 480px wide) & smaller */
@media (max-width: 480px) {
  :root {
    --pixel-size: 2px; /* Make pixels tiny */
  }
}