*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  background: #1e1e2e;
  color: #cdd6f4;
  font-family: system-ui, sans-serif;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  /* Safe-area insets for iPhone notch / home bar */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* Suppress iOS tap highlight */
* {
  -webkit-tap-highlight-color: transparent;
}

/* ── Toolbar ── */

#toolbar {
  height: 48px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  background: #181825;
  border-bottom: 1px solid #313244;
  flex-shrink: 0;
  touch-action: none;
}

#sidebar-toggle {
  background: none;
  border: none;
  color: #a6adc8;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  line-height: 1;
  flex-shrink: 0;
}

#sidebar-toggle:hover,
#sidebar-toggle:active {
  background: #313244;
  color: #cdd6f4;
}

.logo {
  font-family: monospace;
  font-size: 1.1rem;
  font-weight: bold;
  color: #cba6f7;
  letter-spacing: 0.05em;
}

#play-btn {
  margin-left: auto;
  background: #a6e3a1;
  color: #1e1e2e;
  border: none;
  border-radius: 4px;
  padding: 8px 18px;
  font-size: 0.9rem;
  font-weight: bold;
  cursor: pointer;
  transition: background 0.15s;
  /* Larger touch target on mobile */
  min-height: 36px;
}

#play-btn:hover,
#play-btn:active {
  background: #94e2d5;
}

/* ── Main (sidebar + editor) ── */

#main {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

/* ── Sidebar backdrop (mobile overlay) ── */

#sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 49;
  background: rgba(0, 0, 0, 0.5);
}

#sidebar-backdrop.visible {
  display: block;
}

/* ── Sidebar ── */

#sidebar {
  width: 220px;
  flex-shrink: 0;
  background: #181825;
  border-right: 1px solid #313244;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  user-select: none;
  /* Smooth collapse on desktop */
  transition: width 0.2s ease, opacity 0.2s ease;
}

/* Desktop collapsed state */
#sidebar.closed {
  width: 0;
  opacity: 0;
  pointer-events: none;
  border-right: none;
}

/* On mobile the sidebar slides over the editor */
@media (max-width: 639px) {
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    /* Account for toolbar */
    padding-top: calc(48px + env(safe-area-inset-top));
    z-index: 50;
    width: 80vw;
    max-width: 300px;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    /* Reset desktop collapse behaviour */
    opacity: 1;
    pointer-events: auto;
    border-right: 1px solid #313244;
  }

  #sidebar.open {
    transform: translateX(0);
  }

  /* Editor always full width on mobile */
  #editor-pane {
    width: 100%;
  }
}

/* Desktop: sidebar starts open */
@media (min-width: 640px) {
  #sidebar {
    /* open by default, JS adds .closed to collapse */
  }
}

.tree-section {
  padding: 4px 0;
}

.tree-folder-header {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 8px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #6c7086;
  cursor: pointer;
  min-height: 36px;
}

.tree-folder-header:hover {
  color: #a6adc8;
}

.tree-folder-header .arrow {
  font-size: 0.6rem;
  transition: transform 0.15s;
  flex-shrink: 0;
}

.tree-folder-header.collapsed .arrow {
  transform: rotate(-90deg);
}

.tree-folder-header .folder-actions {
  margin-left: auto;
  display: flex;
  gap: 4px;
}

.folder-action-btn {
  background: none;
  border: none;
  color: #6c7086;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 3px;
  min-width: 28px;
  min-height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.folder-action-btn:hover,
.folder-action-btn:active {
  color: #cdd6f4;
  background: #313244;
}

.tree-folder-content {
  overflow: hidden;
}

.tree-folder-content.collapsed {
  display: none;
}

.tree-file {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px 6px 22px;
  font-size: 0.85rem;
  color: #a6adc8;
  cursor: pointer;
  border-radius: 0;
  position: relative;
  min-height: 36px;
}

.tree-file:hover {
  background: #26263a;
  color: #cdd6f4;
}

.tree-file.active {
  background: #313244;
  color: #cdd6f4;
}

.tree-file .file-icon {
  flex-shrink: 0;
  opacity: 0.6;
  font-size: 0.8rem;
}

.tree-file .file-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-file .delete-btn,
.tree-file .download-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  color: #6c7086;
  font-size: 0.9rem;
  cursor: pointer;
  padding: 4px 5px;
  border-radius: 3px;
  line-height: 1;
  min-width: 24px;
  min-height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tree-file .download-btn {
  display: none;
}

.tree-file:hover .download-btn {
  display: flex;
}

.tree-file .delete-btn:hover,
.tree-file .delete-btn:active {
  color: #f38ba8;
}

.tree-file .download-btn:hover,
.tree-file .download-btn:active {
  color: #89b4fa;
}

/* Inline new-file input */

.new-file-row {
  display: flex;
  align-items: center;
  padding: 4px 10px 4px 22px;
}

.new-file-input {
  flex: 1;
  background: #313244;
  border: 1px solid #cba6f7;
  border-radius: 3px;
  color: #cdd6f4;
  font-size: 0.85rem;
  padding: 4px 6px;
  outline: none;
  font-family: monospace;
  /* Prevent iOS zoom on focus */
  font-size: 16px;
}

/* ── Editor pane ── */

#editor-pane {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#ace-editor {
  flex: 1;
  font-size: 14px;
}

/* ── Runner overlay ── */

#runner-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: #000;
}

#runner-overlay.hidden {
  display: none;
}

#runner-frame {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

#close-btn {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  z-index: 101;
  background: rgba(0, 0, 0, 0.65);
  color: #cdd6f4;
  border: 1px solid #555;
  border-radius: 4px;
  padding: 8px 14px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s;
  min-height: 36px;
}

#close-btn:hover,
#close-btn:active {
  background: rgba(80, 80, 80, 0.8);
}

/* ── Docs pane ── */

#docs-pane {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  background: #1e1e2e;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

#docs-inner {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

#docs-pane.hidden {
  display: none;
}

#docs-inner {
  max-width: 680px;
  margin: 0 auto;
  padding: 24px 28px 48px;
}

@media (max-width: 639px) {
  #docs-pane {
    position: fixed;
    inset: 0;
    padding-top: calc(48px + env(safe-area-inset-top));
    z-index: 60;
  }

  #docs-inner {
    padding: 16px 16px 40px;
  }
}

/* ── Docs link area (bottom of sidebar) ── */

#docs-link-area {
  margin-top: auto;
  padding: 10px 8px;
  border-top: 1px solid #313244;
}

#docs-header {
  width: 100%;
  background: none;
  border: 1px solid #313244;
  border-radius: 6px;
  color: #a6adc8;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  cursor: pointer;
  padding: 7px 10px;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 36px;
  transition: background 0.15s, color 0.15s;
}

#docs-header::before {
  content: '?';
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #313244;
  color: #cba6f7;
  font-size: 0.75rem;
  font-weight: bold;
  flex-shrink: 0;
}

#docs-header:hover,
#docs-header.active {
  background: #313244;
  color: #cdd6f4;
  border-color: #45475a;
}

#docs-header.active::before {
  background: #cba6f7;
  color: #1e1e2e;
}

/* ── Docs pane toolbar ── */

#docs-toolbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 44px;
  border-bottom: 1px solid #313244;
  background: #181825;
  flex-shrink: 0;
  font-weight: 600;
  font-size: 0.9rem;
  color: #cdd6f4;
}

@media (max-width: 639px) {
  #docs-toolbar {
    display: flex;
  }
}

#docs-close-btn {
  background: none;
  border: 1px solid #313244;
  border-radius: 4px;
  color: #a6adc8;
  font-size: 0.82rem;
  cursor: pointer;
  padding: 5px 10px;
  min-height: 32px;
}

#docs-close-btn:active {
  background: #313244;
  color: #cdd6f4;
}

#docs-pane .docs-section {
  padding: 20px 0 4px;
  border-top: 1px solid #313244;
}

#docs-pane .docs-section:first-child {
  border-top: none;
  padding-top: 0;
}

#docs-pane .docs-section-title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: #cba6f7;
  margin-bottom: 14px;
}

#docs-pane .docs-entry {
  margin-bottom: 20px;
  padding-left: 0;
  border-left: none;
}

#docs-pane .docs-entry:last-child {
  margin-bottom: 0;
}

#docs-pane .docs-entry > code {
  display: inline-block;
  font-family: monospace;
  font-size: 0.9rem;
  color: #89dceb;
  background: #2a2a3d;
  padding: 3px 7px;
  border-radius: 4px;
  margin-bottom: 6px;
  word-break: break-all;
}

#docs-pane .docs-entry > span {
  display: block;
  font-size: 0.88rem;
  color: #a6adc8;
  line-height: 1.55;
  margin-bottom: 4px;
}

#docs-pane .docs-entry span em {
  font-style: normal;
  color: #cdd6f4;
  font-weight: 500;
}

#docs-pane .docs-entry span code {
  font-family: monospace;
  font-size: 0.85rem;
  color: #89dceb;
  background: #2a2a3d;
  padding: 1px 4px;
  border-radius: 3px;
}

#docs-pane .docs-entry pre {
  margin-top: 8px;
  background: #181825;
  border: 1px solid #313244;
  border-radius: 6px;
  padding: 12px 14px;
  font-family: monospace;
  font-size: 0.85rem;
  color: #cdd6f4;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.55;
  overflow-x: hidden;
}
