:root {
  --bg-panel: rgba(20, 20, 26, 0.92);
  --bg-card: rgba(255, 255, 255, 0.05);
  --bg-input: rgba(255, 255, 255, 0.07);
  --border: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.18);
  --text: #e8e8ee;
  --text-dim: #9a9aa8;
  --accent: #8b7cf8;
  --accent-hover: #a294ff;
  --danger: #f87171;
  --success: #4ade80;
  --radius: 10px;
}

body,
html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
}

canvas {
  display: block;
  /* the app owns all touch gestures on the canvas (pan, pinch, drag) */
  touch-action: none;
}

button {
  font-family: inherit;
}

/* ───────────────────────── corner toggle buttons ───────────────────────── */

.corner-btn {
  position: fixed;
  top: 16px;
  z-index: 5;
  width: 42px;
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 19px;
  color: var(--text);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  transition: background 0.15s, border-color 0.15s;
}

.corner-btn:hover {
  background: rgba(50, 50, 62, 0.95);
  border-color: var(--border-strong);
}

#menu-toggle {
  right: 16px;
}

#database-menu-toggle {
  left: 16px;
}

/* ───────────────────────── slide-in panels ───────────────────────── */

.panel {
  position: fixed;
  top: 0;
  bottom: 0;
  z-index: 10;
  width: 320px;
  max-width: 88vw;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  transition: transform 0.25s ease;
  overflow: hidden;
}

#transform-gui {
  right: 0;
  border-left: 1px solid var(--border);
  transform: translateX(105%);
}

#database-gui {
  left: 0;
  border-right: 1px solid var(--border);
  transform: translateX(-105%);
}

#transform-gui.open,
#database-gui.open {
  transform: translateX(0);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.panel-header h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.closeToolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  font-size: 20px;
  line-height: 1;
  color: var(--text-dim);
  text-decoration: none;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}

.closeToolbar:hover {
  background: var(--bg-card);
  color: var(--text);
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.panel-body::-webkit-scrollbar {
  width: 8px;
}

.panel-body::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.12);
  border-radius: 4px;
}

.panel-footer {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.panel-footer .btn {
  width: 100%;
}

/* ───────────────────────── shared button styles ───────────────────────── */

.btn {
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.btn:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: var(--border-strong);
}

.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: transparent;
}

.btn-primary:disabled {
  opacity: 0.55;
  cursor: default;
}

body.hide-handles .control-handle {
  display: none;
}

.render-mode-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 2px 12px;
}

/* ───────────────────────── transform cards ───────────────────────── */

#transform-forms {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.transform-form {
  position: relative;
  padding: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.tf-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  padding-right: 26px; /* keep clear of the absolute-positioned remove button */
}

.tf-header input[type="color"] {
  width: 30px;
  height: 30px;
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 50%;
  background: none;
  cursor: pointer;
  flex-shrink: 0;
}

.tf-header input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 2px;
}

.tf-header input[type="color"]::-webkit-color-swatch {
  border: none;
  border-radius: 50%;
}

.tf-alpha {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  font-size: 11px;
  color: var(--text-dim);
}

.tf-alpha input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}

.tf-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tf-row {
  display: grid;
  grid-template-columns: 52px 1fr 1fr 1fr;
  gap: 6px;
  align-items: center;
}

.tf-row.tf-row-4 {
  grid-template-columns: 52px 1fr 1fr 1fr 1fr;
}

.tf-row-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.tf-field {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.tf-field span {
  font-size: 10px;
  color: var(--text-dim);
  padding-left: 2px;
}

input[type="number"],
input[type="text"] {
  width: 100%;
  box-sizing: border-box;
  padding: 6px 7px;
  font-size: 12px;
  font-family: inherit;
  color: var(--text);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  outline: none;
  transition: border-color 0.15s;
}

input[type="number"]:focus,
input[type="text"]:focus {
  border-color: var(--accent);
}

.transform-form .close-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: var(--text-dim);
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.transform-form .close-btn:hover {
  background: rgba(248, 113, 113, 0.15);
  color: var(--danger);
}

/* hide number input spinners */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

input[type="number"] {
  -moz-appearance: textfield;
}

/* ───────────────────────── save / load panel ───────────────────────── */

.db-section {
  margin-bottom: 16px;
}

.db-section h3 {
  margin: 4px 2px 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.save-row {
  display: flex;
  gap: 8px;
}

.save-row input {
  flex: 1;
}

.share-btn {
  width: 100%;
  margin-top: 8px;
}

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

.load-header h3 {
  margin: 0 2px;
}

.sort-toggle {
  display: flex;
  gap: 2px;
  padding: 2px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.sort-toggle button {
  padding: 4px 10px;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-dim);
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.sort-toggle button.active {
  background: rgba(255, 255, 255, 0.12);
  color: var(--text);
}

.list-subheader {
  margin: 14px 2px 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.list-subheader:first-child {
  margin-top: 2px;
}

.fractal-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  margin-bottom: 6px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.fractal-info {
  flex: 1;
  min-width: 0;
}

.fav-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 9px;
  font-size: 13px;
  color: var(--text-dim);
  background: none;
  border: 1px solid transparent;
  border-radius: 999px;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.15s, background 0.15s;
}

.fav-btn span {
  font-size: 11px;
}

.fav-btn:hover {
  background: rgba(248, 113, 113, 0.12);
  color: var(--danger);
}

.fav-btn.favorited {
  color: var(--danger);
}

.del-btn {
  padding: 5px 8px;
  font-size: 12px;
  color: var(--text-dim);
  background: none;
  border: 1px solid transparent;
  border-radius: 999px;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.15s, background 0.15s;
}

.del-btn:hover {
  background: rgba(248, 113, 113, 0.12);
  color: var(--danger);
}

.del-btn.confirm {
  color: #fff;
  background: var(--danger);
  font-weight: 600;
}

.fractal-item:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: var(--border-strong);
}

.fractal-name {
  font-weight: 500;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.fractal-date {
  font-size: 11px;
  color: var(--text-dim);
}

.list-message {
  padding: 18px 8px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
}

.list-message.error {
  color: var(--danger);
}

/* ───────────────────────── transition matrix popup ───────────────────────── */

#transition-matrix-gui {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 20;
  padding: 14px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

#transition-matrix-gui .tm-title {
  margin: 0 28px 10px 0;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#transition-matrix-gui table {
  border-collapse: collapse;
}

#transition-matrix-gui td,
#transition-matrix-gui th {
  padding: 4px;
  text-align: center;
}

#transition-matrix-gui input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.tm-dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.4);
}

.tm-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: var(--text-dim);
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.tm-close:hover {
  background: var(--bg-card);
  color: var(--text);
}

/* ───────────────────────── toasts ───────────────────────── */

#toast-container {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 10px 18px;
  font-size: 13px;
  color: var(--text);
  background: var(--bg-panel);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.5);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s, transform 0.2s;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.toast.success {
  border-color: rgba(74, 222, 128, 0.45);
}

.toast.error {
  border-color: rgba(248, 113, 113, 0.55);
}

/* ───────────────────────── drag handles ───────────────────────── */

.control-handle {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  box-shadow: 0 0 0 1.5px white;
  z-index: 1;
  cursor: grab;
  transition: box-shadow 0.15s;
}

.control-handle.selected {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  animation: handle-pulse 1.6s ease-in-out infinite;
}

@keyframes handle-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 2px white, 0 0 0 5px var(--accent),
      0 0 14px 4px rgba(139, 124, 248, 0.55);
  }
  50% {
    box-shadow: 0 0 0 2px white, 0 0 0 7px var(--accent),
      0 0 24px 8px rgba(139, 124, 248, 0.75);
  }
}

/* ───────────────────────── first-visit tutorial hint ───────────────────────── */

#tutorial-hint {
  position: fixed;
  left: -9999px; /* positioned by JS, anchored to the control point */
  top: -9999px;
  z-index: 90;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px 12px 18px;
  background: var(--bg-panel);
  border: 1px solid rgba(139, 124, 248, 0.5);
  border-radius: 999px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.5);
  transition: opacity 0.35s, left 0.2s ease-out, top 0.2s ease-out;
}

#tutorial-hint.hide {
  opacity: 0;
  pointer-events: none;
}

.tutorial-arrow {
  position: absolute;
  width: 12px;
  height: 12px;
  margin-left: -6px;
  background: rgb(26, 26, 33);
  transform: rotate(45deg);
}

.tutorial-arrow.on-top {
  top: -7px;
  border-top: 1px solid rgba(139, 124, 248, 0.5);
  border-left: 1px solid rgba(139, 124, 248, 0.5);
}

.tutorial-arrow.on-bottom {
  bottom: -7px;
  border-bottom: 1px solid rgba(139, 124, 248, 0.5);
  border-right: 1px solid rgba(139, 124, 248, 0.5);
}

.tutorial-dots {
  display: flex;
  gap: 5px;
}

.tutorial-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
}

.tutorial-dot.done {
  background: var(--accent);
}

.tutorial-text {
  font-size: 13px;
}

.tutorial-skip {
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: var(--text-dim);
  background: none;
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

.tutorial-skip:hover {
  background: var(--bg-card);
  color: var(--text);
}

.control-handle {
  touch-action: none;
}

@media (pointer: coarse) {
  .control-handle {
    width: 30px;
    height: 30px;
  }

  .control-handle.selected {
    width: 40px;
    height: 40px;
  }

  /* 16px+ stops iOS Safari from auto-zooming the page when a field is focused */
  input[type="number"],
  input[type="text"] {
    font-size: 16px;
  }
}
