* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: #f5f6f8;
  color: #333;
}

body {
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  flex-shrink: 0; /* never compress when the canvas makes the page content taller than the viewport */
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  background: #0058ad;
  color: #fff;
  user-select: none;
}

#app-name {
  font-size: 15px;
  font-weight: 600;
  padding-right: 12px;
  border-right: 1px solid rgba(255, 255, 255, 0.35);
}

#sketch-name {
  font-size: 14px;
  padding: 2px 8px;
  border-radius: 3px;
  white-space: nowrap;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Only locally-stored sketches are renamable — .editable is toggled by updateLocalUi() */
#sketch-name.editable { cursor: text; }
#sketch-name.editable:not([contenteditable="true"]):hover { background: rgba(255, 255, 255, 0.15); }

#sketch-name.editable:not([contenteditable="true"])::after {
  content: " ✎";
  font-size: 12px;
  opacity: 0.55;
}

#sketch-name[contenteditable="true"] {
  background: #fff;
  color: darkblue;
  border: 1px solid lightblue;
  outline: none;
  text-overflow: clip;
}

#status {
  flex: 1;
  font-size: 13px;
  color: rgba(255, 255, 255, 0.85);
}

.navbar-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 31px;
  padding: 0 14px;
  font-size: 13px;
  color: #fff;
  background: transparent;
  border: 1px solid #fff;
  border-radius: 4px;
  cursor: pointer;
}

.navbar-btn svg { width: 14px; height: 14px; flex-shrink: 0; }

.navbar-btn:hover { background: rgba(255, 255, 255, 0.15); }
.navbar-btn[hidden] { display: none; }

#editor-controls {
  display: flex;
  align-items: center;
  align-self: center;
  gap: 8px;
  margin-left: auto;
  padding-right: 4px;
}

/* CodeGuppy-style pill play/stop button */
#run {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 52px;
  height: 28px;
  padding: 0;
  color: #43a047;
  background: #fff;
  border: 2px solid #43a047;
  border-radius: 15px;
  cursor: pointer;
}

#run svg { width: 13px; height: 13px; display: block; }
#run .icon-play { margin-left: 2px; } /* optical centering: triangles read left-heavy when geometrically centered */

#run:hover:not(:disabled) { background: #eef7ee; }
#run.stop { color: #e53935; border-color: #e53935; }
#run.stop:hover { background: #fdeeee; }
#run:disabled { color: #bbb; border-color: #bbb; background: #fff; cursor: default; }

/* Red dashed-corner expand buttons */
.expand {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  padding: 0;
  color: #c62828;
  background: none;
  border: none;
  border-radius: 3px;
  cursor: pointer;
}

.expand svg { width: 14px; height: 14px; }
.expand:hover { background: rgba(198, 40, 40, 0.1); }

#output-bar {
  display: flex;
  flex-shrink: 0;
  justify-content: flex-start;
  align-items: center;
  height: 37px; /* matches #tabs */
  padding: 0 6px;
  background: #f3f4f6;
  border-bottom: 1px solid #d0d4d9;
}

/* Expanded-pane states: one pane takes the full width */
body.code-full #right { display: none; }
body.code-full #left { border-right: none; }
body.output-full #left { display: none; }
body.output-full #right { flex: 1 1 auto; } /* fill the full width instead of keeping the 840px basis */

main {
  flex: 1;
  display: flex;
  min-height: 0;
}

#action-bar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 46px;
  padding: 10px 0;
  flex-shrink: 0;
  background: #eef0f3;
  border-right: 1px solid #d0d4d9;
}

#action-bar button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  color: #0058ad;
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
}

#action-bar button svg { width: 20px; height: 20px; }
#action-bar button:hover { background: #dde3ea; }
#action-bar button.active { background: #0058ad; color: #fff; }

#act-delete { margin-top: auto; color: #c62828; }
#act-delete:hover { background: rgba(198, 40, 40, 0.1); }
#act-delete[hidden] { display: none; }

#sidebar {
  display: flex;
  flex-direction: column;
  width: 500px;
  flex-shrink: 0;
  background: #fff;
  border-right: 1px solid #d0d4d9;
}

#sidebar[hidden] { display: none; }

#sidebar-header {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  height: 37px;
  padding: 0 6px 0 12px;
  font-size: 13px;
  font-weight: 600;
  background: #f3f4f6;
  border-bottom: 1px solid #d0d4d9;
}

#sidebar-close {
  margin-left: auto;
  width: 26px;
  height: 26px;
  padding: 0;
  font-size: 16px;
  line-height: 1;
  color: #c62828; /* same red as the .expand buttons */
  background: none;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

#sidebar-close:hover { background: rgba(198, 40, 40, 0.1); }

#sidebar-content {
  flex: 1;
  overflow: auto;
  padding: 10px 12px;
  font-size: 13px;
}

.example-category {
  margin: 16px 0 8px;
  padding-bottom: 4px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #0058ad;
  border-bottom: 1px solid #e3e6ea;
}

.example-category:first-child { margin-top: 2px; }

.example-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  margin-bottom: 6px;
  font-family: inherit;
  background: #f7f8fa;
  border: 1px solid #dfe3e8;
  border-radius: 6px;
  cursor: pointer;
}

.example-item:hover { border-color: #0058ad; background: #eef4fb; }
.example-item .title { display: block; font-size: 13px; font-weight: 600; color: #222; }
.example-item .desc { display: block; font-size: 12px; color: #666; margin-top: 2px; }
.example-item .file { display: block; font-size: 11px; color: #999; margin-top: 2px; }

.md-content { line-height: 1.5; color: #333; }
.md-content h1 { font-size: 17px; margin: 4px 0 10px; }
.md-content h2 { font-size: 14px; margin: 18px 0 6px; border-bottom: 1px solid #eee; padding-bottom: 3px; }
.md-content h3 { font-size: 13px; margin: 14px 0 4px; }
.md-content p { margin: 6px 0; }
.md-content ul, .md-content ol { margin: 6px 0; padding-left: 20px; }
.md-content code {
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
  background: #f0f2f5;
  padding: 1px 4px;
  border-radius: 3px;
}
.md-content pre {
  background: #f6f8fa;
  padding: 10px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 8px 0;
}
.md-content pre code { background: none; padding: 0; }
.md-content table { border-collapse: collapse; margin: 8px 0; }
.md-content th, .md-content td { border: 1px solid #ddd; padding: 4px 8px; font-size: 12px; text-align: left; }
.md-content th { background: #f3f4f6; }

#left {
  flex: 1;
  min-width: 300px; /* keep the editor usable when the sidebar is open — the output pane shrinks instead */
  display: flex;
  flex-direction: column;
  border-right: 1px solid #ddd;
}

body.output-full #left { min-width: 0; }

#tabs {
  display: flex;
  flex-shrink: 0;
  align-items: flex-end;
  height: 37px; /* same fixed height as #output-bar so controls in both bars align */
  background: #f3f4f6;
  border-bottom: 1px solid #d0d4d9;
  padding: 0 6px;
  gap: 4px;
}

#tab-list {
  display: flex;
  gap: 4px;
  margin-bottom: -1px; /* overlap the #tabs bottom border so the active tab merges into the editor */
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
}

.tab {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 96px;
  max-width: 160px;
  padding: 5px 22px 6px 10px;
  font-size: 13px;
  background: whitesmoke;
  color: #555;
  border: 1px solid #d0d4d9;
  border-top: 2px solid #c6cbd1;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.tab.active {
  background: #fff;
  color: #222;
  border-top: 3px solid #0058ac;
  border-bottom-color: #fff;
  padding-top: 4px;
}

.tab .label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 120px;
  padding: 0 2px;
}

.tab .label[contenteditable="true"] {
  background: #fff;
  color: darkblue;
  border: 1px solid lightblue;
  outline: none;
  text-overflow: clip;
}

.tab .gear {
  position: absolute;
  right: 5px;
  font-size: 13px;
  line-height: 1;
  color: #888;
  visibility: hidden;
}

.tab.active:hover .gear { visibility: visible; }
.tab .gear:hover { color: #0058ac; }

#tab-menu {
  position: fixed;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  min-width: 120px;
  padding: 4px;
  background: #fff;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
}

#tab-menu[hidden] { display: none; }

#tab-menu button {
  background: none;
  border: none;
  border-radius: 3px;
  text-align: left;
  padding: 6px 12px;
  font-size: 13px;
  color: #333;
  cursor: pointer;
}

#tab-menu button:hover:not(:disabled) { background: #eef2f7; }
#tab-menu button:disabled { color: #aaa; cursor: default; }

#add-page {
  width: 26px;
  height: 26px;
  align-self: center;
  font-size: 16px;
  line-height: 1;
  color: #555;
  background: transparent;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

#add-page:hover { background: #e2e5e9; color: #0058ac; }

#editor {
  flex: 1;
  min-height: 0;
}

#right {
  flex: 0 1 840px;
  min-width: 340px;
  display: flex;
  flex-direction: column;
  background: #fff;
}

#canvas-host {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 12px;
  background: #eef0f3;
  min-height: 0;
  overflow: auto; /* a canvas taller than the viewport scrolls here instead of squeezing the bars */
}

#canvas-host canvas {
  max-width: 100%;
  height: auto !important;
  background: #fff;
  border: 1px solid #ccc;
}

#canvas-host canvas:focus { outline: none; }

#output {
  flex: 1;
  min-height: 100px;
  margin: 0;
  padding: 10px 14px;
  overflow: auto;
  font-family: Consolas, "Courier New", monospace;
  font-size: 13px;
  color: #333;
  border-top: 1px solid #ddd;
  white-space: pre-wrap;
}

#output .error { color: #c62828; }

/* Confirmation modal */
#confirm-dialog {
  min-width: 320px;
  max-width: 420px;
  padding: 20px 22px 16px;
  border: none;
  border-radius: 8px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
  font-family: inherit;
  color: #333;
}

#confirm-dialog::backdrop { background: rgba(0, 0, 0, 0.3); }

#confirm-message {
  margin: 0 0 18px;
  font-size: 15px;
  line-height: 1.45;
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 0;
}

.dialog-buttons button {
  padding: 7px 18px;
  font-size: 13px;
  font-family: inherit;
  border-radius: 4px;
  cursor: pointer;
  background: #fff;
  color: #333;
  border: 1px solid #bbb;
}

.dialog-buttons button:hover { background: #f0f2f5; }

.dialog-buttons button.danger {
  background: #c62828;
  color: #fff;
  border-color: #c62828;
}

.dialog-buttons button.danger:hover { background: #d9382f; }
