/* EnvDraw web UI styles — Phase 4 */

/* ── Design tokens ─────────────────────────────────────────── */
:root {
  --bg:          #f0f0f3;
  --surface:     #ffffff;
  --surface-alt: #fafafa;
  --border:      #d4d4d8;
  --border-light:#e4e4e7;
  --text:        #27272a;
  --text-muted:  #71717a;

  --toolbar-bg:  #ffffff;
  --toolbar-shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);

  --canvas-bg:   #ffffff;
  --canvas-dots: #e4e4e7;

  --trace-bg:    #fafaf9;
  --trace-width: 320px;

  --repl-bg:     #18181b;
  --repl-fg:     #e4e4e7;
  --repl-accent: #60a5fa;
  --repl-dim:    #52525b;

  --accent:      #3b82f6;
  --accent-hover:#2563eb;
  --accent-soft: #eff6ff;

  --success:     #22c55e;
  --warning:     #f59e0b;
  --error:       #ef4444;

  --radius:      6px;
  --radius-sm:   4px;
  --transition:  150ms ease;

  --font-sans:   "Inter", system-ui, -apple-system, sans-serif;
  --font-mono:   "SF Mono", "JetBrains Mono", "Fira Code", "Cascadia Code",
                 "Menlo", "Consolas", monospace;
}

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

html, body {
  height: 100%;
  font-family: var(--font-sans);
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ── App shell ─────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ── Toolbar ───────────────────────────────────────────────── */
header#toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  height: 44px;
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--border-light);
  box-shadow: var(--toolbar-shadow);
  flex-shrink: 0;
  z-index: 10;
}

.toolbar-left, .toolbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.brand {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.02em;
  color: var(--accent);
  user-select: none;
}

.toolbar-sep {
  width: 1px;
  height: 20px;
  background: var(--border-light);
  margin: 0 4px;
}

/* Toolbar buttons */
.tb-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.tb-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-hover);
}

.tb-btn:active {
  transform: scale(0.97);
}

.tb-btn-subtle {
  border-color: transparent;
  background: transparent;
  color: var(--text-muted);
}

.tb-btn-subtle:hover {
  background: var(--surface-alt);
  border-color: var(--border-light);
  color: var(--text);
}

.tb-btn-icon {
  border: none;
  background: none;
  padding: 5px 7px;
  font-size: 16px;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: all var(--transition);
}

.tb-btn-icon:hover {
  background: var(--surface-alt);
  color: var(--text);
}

.tb-icon {
  font-size: 11px;
}

/* Examples dropdown */
.tb-select {
  padding: 5px 24px 5px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%2371717a'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
}

.tb-select:hover {
  background-color: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-hover);
}

.tb-select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

/* Examples flyout menu */
.examples-menu {
  position: relative;
  display: inline-block;
}

.examples-flyout {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 12px rgba(0,0,0,0.12);
  z-index: 100;
  min-width: 160px;
  padding: 4px 0;
}

.examples-flyout .ex-btn {
  display: block;
  width: 100%;
  padding: 6px 14px;
  border: none;
  background: none;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
}

.examples-flyout .ex-btn:hover {
  background: var(--accent-soft);
  color: var(--accent-hover);
}

/* Conditional toolbar item visibility */
.tb-step-btn.hidden,
#btn-randomize.hidden,
.examples-flyout.hidden {
  display: none;
}

/* Toggle switch */
.tb-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}

.tb-toggle input { display: none; }

.toggle-track {
  width: 32px;
  height: 18px;
  border-radius: 9px;
  background: var(--border);
  position: relative;
  transition: background var(--transition);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: white;
  box-shadow: 0 1px 2px rgba(0,0,0,0.2);
  transition: transform var(--transition);
}

.tb-toggle input:checked ~ .toggle-track {
  background: var(--accent);
}

.tb-toggle input:checked ~ .toggle-track .toggle-thumb {
  transform: translateX(14px);
}

.toggle-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}

/* Status indicator */
.status-ready {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--success);
  padding: 3px 8px;
  border-radius: 10px;
  background: rgba(34,197,94,0.08);
}

.status-busy {
  color: var(--warning);
  background: rgba(245,158,11,0.08);
}

.status-error {
  color: var(--error);
  background: rgba(239,68,68,0.08);
}

.status-stepping {
  color: var(--accent);
  background: rgba(59,130,246,0.08);
}

/* Step/Continue button highlight when steps are pending */
.tb-btn.step-highlight {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59,130,246,0.25);
}

.tb-btn.step-highlight:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

/* ── Main area ─────────────────────────────────────────────── */
#main-area {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

/* ── Diagram (SVG) ─────────────────────────────────────────── */
#canvas-container {
  flex: 1;
  overflow: hidden;
  position: relative;
  background: var(--canvas-bg);
  /* Subtle dot grid */
  background-image: radial-gradient(circle, var(--canvas-dots) 0.8px, transparent 0.8px);
  background-size: 24px 24px;
}

#diagram-svg {
  display: block;
  width: 100%;
  height: 100%;
  cursor: default;
}

#diagram-svg.panning {
  cursor: grabbing;
}

#diagram-svg.dragging {
  cursor: move;
}

/* Zoom controls */
#zoom-controls {
  position: absolute;
  bottom: 16px;
  left: 16px;
  display: flex;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  overflow: hidden;
  z-index: 5;
}

.zoom-btn {
  width: 34px;
  height: 30px;
  border: none;
  background: none;
  font-size: 15px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
}

.zoom-btn:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.zoom-btn.zoom-label {
  width: auto;
  padding: 0 6px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  cursor: default;
  border-left: 1px solid var(--border-light);
  border-right: 1px solid var(--border-light);
}

/* ── Force control panel ─────────────────────────────────────── */
.force-panel {
  position: absolute;
  bottom: 52px;
  left: 16px;
  width: 260px;
  max-height: calc(100% - 80px);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  z-index: 10;
  padding: 8px 12px 12px;
  font-size: 11px;
  color: var(--text);
}
.force-panel.hidden { display: none; }
.fp-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  font-size: 12px;
  margin-bottom: 8px;
}
.fp-close {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-muted);
  padding: 0 2px;
}
.fp-close:hover { color: var(--text); }
.fp-section {
  font-weight: 600;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin: 8px 0 4px;
  border-top: 1px solid var(--border-light);
  padding-top: 6px;
}
.fp-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 3px 0;
  cursor: default;
}
.fp-row input[type="range"] {
  flex: 1;
  height: 4px;
  accent-color: var(--accent);
  cursor: pointer;
}
.fp-val {
  min-width: 36px;
  text-align: right;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-muted);
}
.fp-actions {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  border-top: 1px solid var(--border-light);
  padding-top: 8px;
}
.fp-btn {
  flex: 1;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface-alt);
  cursor: pointer;
  font-size: 11px;
  font-weight: 500;
  color: var(--text);
  transition: all var(--transition);
}
.fp-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* Empty state */
#empty-state {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
  opacity: 0.7;
  transition: opacity 0.3s ease;
}

#empty-state.hidden {
  opacity: 0;
  pointer-events: none;
}

.empty-icon {
  font-size: 72px;
  font-weight: 300;
  color: var(--border);
  margin-bottom: 12px;
  font-family: "Helvetica Neue", "Arial", sans-serif;
}

.empty-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.empty-hint {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 16px;
}

/* ── Resize handle ──────────────────────────────────────────── */
#resize-handle {
  width: 5px;
  cursor: col-resize;
  background: transparent;
  position: relative;
  flex-shrink: 0;
  z-index: 6;
  transition: background var(--transition);
}

#resize-handle::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 3px;
  height: 40px;
  border-radius: 2px;
  background: var(--border);
  opacity: 0;
  transition: opacity var(--transition);
}

#resize-handle:hover {
  background: var(--accent-soft);
}

#resize-handle:hover::after {
  opacity: 1;
}

/* Hide handle when trace is collapsed */
#resize-handle.hidden {
  display: none;
}

/* ── Trace panel (right sidebar) ───────────────────────────── */
#trace-panel {
  width: var(--trace-width);
  min-width: 180px;
  max-width: 600px;
  background: var(--trace-bg);
  border-left: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: width 0.2s ease, opacity 0.2s ease;
  overflow: hidden;
}

#trace-panel.collapsed {
  width: 0 !important;
  min-width: 0;
  border-left: none;
  opacity: 0;
  pointer-events: none;
}

.trace-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
}

.trace-title {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.trace-btn {
  width: 22px;
  height: 22px;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  transition: all var(--transition);
}

.trace-btn:hover {
  background: rgba(0,0,0,0.06);
  color: var(--text);
}

#trace-output {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
  font-family: var(--font-mono);
  font-size: 11.5px;
  line-height: 1.5;
}

#trace-output::-webkit-scrollbar {
  width: 6px;
}

#trace-output::-webkit-scrollbar-track {
  background: transparent;
}

#trace-output::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.trace-line {
  padding: 1px 12px;
  white-space: pre-wrap;
  word-break: break-word;
}

.trace-line:hover {
  background: rgba(0,0,0,0.03);
}

.trace-line.eval-line   { color: #1e40af; }
.trace-line.return-line { color: #166534; }
.trace-line.error-line  { color: var(--error); font-weight: 600; }
.trace-line.info-line   { color: #6b21a8; }
.trace-line.input-line  { color: var(--text-muted); font-style: italic; }

/* ── REPL ──────────────────────────────────────────────────── */
#repl-area {
  display: flex;
  flex-direction: column;
  background: var(--repl-bg);
  border-top: 1px solid #333;
  flex-shrink: 0;
  transition: height 0.25s ease;
  overflow: hidden;
}

#repl-area:not(.collapsed) {
  height: 40vh;
  min-height: 200px;
  max-height: 50vh;
}

#repl-area.collapsed {
  height: 42px;
  min-height: 42px;
  border-top: 1px solid #555;
}
#repl-area.collapsed #repl-handle {
  display: none;
}
#repl-area.collapsed #repl-body {
  display: flex;
}
#repl-area.collapsed #repl-log {
  display: none;
}
#repl-area.collapsed #repl-input {
  min-height: 28px;
  max-height: 28px;
  overflow: hidden;
}

#repl-handle {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 12px;
  cursor: pointer;
  user-select: none;
  color: var(--repl-dim);
  font-size: 11px;
  font-weight: 500;
  flex-shrink: 0;
  border-bottom: 1px solid #333;
}
#repl-handle:hover {
  color: var(--repl-fg);
  background: rgba(255,255,255,0.04);
}
.repl-handle-icon {
  font-size: 9px;
  transition: transform 0.25s ease;
}
#repl-area.collapsed .repl-handle-icon {
  transform: rotate(-90deg);
}

#repl-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

#repl-body::-webkit-scrollbar       { width: 6px; }
#repl-body::-webkit-scrollbar-track  { background: transparent; }
#repl-body::-webkit-scrollbar-thumb  { background: #444; border-radius: 3px; }
/* repl-body visibility is controlled by the collapsed rules above */

#repl-log {
  overflow-y: auto;
  flex: 1 1 0;
  min-height: 60px;
  padding: 4px 0;
}

#repl-log::-webkit-scrollbar       { width: 6px; }
#repl-log::-webkit-scrollbar-track  { background: transparent; }
#repl-log::-webkit-scrollbar-thumb  { background: #444; border-radius: 3px; }

.repl-log-block + .repl-log-block {
  border-top: 1px solid #222;
  margin-top: 1px;
  padding-top: 1px;
}

.repl-log-line {
  display: flex;
  align-items: baseline;
  padding: 0 12px;
  line-height: 20px;
  font-family: var(--font-mono);
  font-size: 13px;
  white-space: pre;
}

.repl-log-line .line-num {
  min-width: 28px;
  text-align: right;
  padding-right: 8px;
  color: var(--repl-dim);
  font-size: 11px;
  user-select: none;
  flex-shrink: 0;
}

.repl-log-line .line-input {
  color: var(--repl-fg);
  white-space: pre;
}

.repl-log-line .line-result {
  color: #7ec;
  padding-left: 36px;
  white-space: pre-wrap;
}

.repl-log-line .line-error {
  color: #f66;
  padding-left: 36px;
  white-space: pre-wrap;
}

#repl-input-row {
  display: flex;
  align-items: flex-start;
  padding: 2px 12px 4px;
  flex-shrink: 0;
  border-top: 1px solid #2a2a2a;
}

#input-line-nums {
  display: flex;
  flex-direction: column;
  min-width: 28px;
  text-align: right;
  padding-right: 8px;
  padding-top: 4px;
  color: var(--repl-accent);
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 20px;
  user-select: none;
  flex-shrink: 0;
  overflow: hidden;
}

#input-line-nums span {
  display: block;
  text-align: right;
}

.prompt {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--repl-accent);
  font-weight: 700;
  user-select: none;
  margin-right: 6px;
  flex-shrink: 0;
  padding-top: 2px;
}

#repl-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--repl-fg);
  font-family: var(--font-mono);
  font-size: 13px;
  caret-color: var(--repl-accent);
  line-height: 20px;
  padding: 2px 0;
  resize: none;
  overflow-y: auto;
  min-height: 100px;
  max-height: 200px;
}

#repl-input::placeholder {
  color: var(--repl-dim);
}

#repl-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Keyboard shortcut hints (tooltip) ─────────────────────── */
[title] {
  position: relative;
}

/* ── Responsive ────────────────────────────────────────────── */
@media (max-width: 768px) {
  #trace-panel {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 20;
    box-shadow: -4px 0 16px rgba(0,0,0,0.1);
  }

  #trace-panel.collapsed {
    box-shadow: none;
  }

  #resize-handle {
    display: none;
  }

  #zoom-controls {
    bottom: 8px;
    left: 8px;
  }
}

/* ── Animations ────────────────────────────────────────────── */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.5; }
}

.status-busy {
  animation: pulse 1.5s ease-in-out infinite;
}
