/* ============================================================
   BASE & THEME VARIABLES
   ============================================================ */
:root {
  --bg-primary: #0d0d1a;
  --bg-secondary: #1a1a2e;
  --bg-panel: #16213e;
  --bg-card: #1e2a4a;
  --accent-blue: #0f3460;
  --accent-bright: #4a9eff;
  --accent-red: #e94560;
  --accent-green: #00d4aa;
  --accent-orange: #ff8c42;
  --accent-purple: #b44fff;
  --accent-yellow: #ffd700;
  --text-primary: #e8eaf6;
  --text-secondary: #9ba3c7;
  --text-muted: #5c6494;
  --border: #2a3a6e;
  --border-bright: #3d5a9e;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --shadow-glow: 0 0 20px rgba(74,158,255,0.3);
  --radius: 10px;
  --radius-sm: 6px;
  --tape-colors: #4a9eff, #8323a5, #ff8c42, #b44fff, #ffd700;
}
.light-theme {
  /* Backgrounds: Soft oatmeal tones */
  --bg-primary: #fcf9f6; 
  --bg-secondary: #f4ece4; 
  --bg-panel: #ffffff;
  --bg-card: #fbf5f0;

  /* Accents: Professional brand palette */
  --accent-brand: #d58137;  /* Terracotta */
  --accent-blue: #1047a0;   /* Clean UI Blue */
  --accent-bright: #e8a05d;
  --accent-red: #d32f2f;
  --accent-green: #2e7d5e;
  --accent-orange: #e05800;
  --accent-purple: #7b3fa2;
  --accent-yellow: #d4880a;

  /* Text: Deep brown-grays for readability */
  --text-primary: #2c241e; 
  --text-secondary: #63564b; 
  --text-muted: #9e8e82;

  /* Borders & Elevation */
  --border: #e6d8cc; 
  --border-bright: #d5bda8;
  --shadow: 0 4px 20px rgba(120, 80, 50, 0.06); 
  --shadow-glow: 0 0 20px rgba(213, 129, 55, 0.15);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 14px;
  transition: background 0.3s, color 0.3s;
}
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-secondary); }
::-webkit-scrollbar-thumb { background: var(--border-bright); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent-bright); }

/* ============================================================
   LAYOUT
   ============================================================ */
#app {
  display: grid;
  grid-template-rows: 52px 1fr;
  grid-template-columns: 300px 1fr 300px;
  height: 100vh;
  gap: 0;
}
#header {
  grid-column: 1 / -1;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  z-index: 100;
  box-shadow: var(--shadow);
}
#left-panel {
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0;
}
#center-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-primary);
}
#right-panel {
  background: var(--bg-secondary);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* ============================================================
   HEADER
   ============================================================ */
#header h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent-bright);
  letter-spacing: 0.5px;
  white-space: nowrap;
}
#header h1 span { color: var(--text-secondary); font-weight: 400; font-size: 13px; margin-left: 6px; }
.header-spacer { flex: 1; }
.header-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s;
  white-space: nowrap;
}
.header-btn:hover { border-color: var(--accent-bright); color: var(--accent-bright); }
.header-btn.active { background: var(--accent-blue); border-color: var(--accent-bright); color: var(--accent-bright); }

/* ============================================================
   PANELS & SECTIONS
   ============================================================ */
.panel-section {
  border-bottom: 1px solid var(--border);
  padding: 12px;
}
.panel-section h3 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.panel-section h3 svg { opacity: 0.7; }

/* ============================================================
   FORM ELEMENTS
   ============================================================ */
label {
  display: block;
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  font-weight: 500;
}
input[type="text"], input[type="number"], select, textarea {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 6px 9px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}
input[type="text"]:focus, input[type="number"]:focus, select:focus, textarea:focus {
  border-color: var(--accent-bright);
  box-shadow: 0 0 0 2px rgba(74,158,255,0.15);
}
select option { background: var(--bg-panel); }
textarea {
  resize: vertical;
  font-family: 'Courier New', Courier, monospace;
  font-size: 12px;
  line-height: 1.6;
  min-height: 120px;
}
.form-row { margin-bottom: 8px; }
.form-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}
.btn-primary { background: var(--accent-blue); color: #fff; border: 1px solid var(--accent-bright); }
.btn-primary:hover { background: var(--accent-bright); color: #fff; }
.btn-success { background: rgba(0,212,170,0.15); color: var(--accent-green); border: 1px solid var(--accent-green); }
.btn-success:hover { background: var(--accent-green); color: #000; }
.btn-danger { background: rgba(233,69,96,0.15); color: var(--accent-red); border: 1px solid var(--accent-red); }
.btn-danger:hover { background: var(--accent-red); color: #fff; }
.btn-warning { background: rgba(255,140,66,0.15); color: var(--accent-orange); border: 1px solid var(--accent-orange); }
.btn-warning:hover { background: var(--accent-orange); color: #fff; }
.btn-secondary { background: var(--bg-card); color: var(--text-secondary); border: 1px solid var(--border); }
.btn-secondary:hover { border-color: var(--accent-bright); color: var(--text-primary); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-group { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-full { width: 100%; justify-content: center; margin-bottom: 6px; }

/* ============================================================
   STATE BADGE
   ============================================================ */
#state-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#current-state-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-card);
  border: 2px solid var(--accent-bright);
  border-radius: 24px;
  padding: 6px 18px;
  font-size: 18px;
  font-weight: 700;
  color: var(--accent-bright);
  transition: all 0.3s;
  min-width: 120px;
  justify-content: center;
}
#current-state-badge.flash { animation: stateFlash 0.4s ease; }
@keyframes stateFlash {
  0% { transform: scale(1); box-shadow: 0 0 0 rgba(74,158,255,0); }
  50% { transform: scale(1.1); box-shadow: 0 0 30px rgba(74,158,255,0.7); }
  100% { transform: scale(1); box-shadow: 0 0 0 rgba(74,158,255,0); }
}
#current-state-badge.accept { border-color: var(--accent-green); color: var(--accent-green); }
#current-state-badge.reject { border-color: var(--accent-red); color: var(--accent-red); }
#current-state-badge.halt { border-color: var(--accent-yellow); color: var(--accent-yellow); }
#step-counter {
  font-size: 13px;
  color: var(--text-secondary);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 14px;
}
#step-counter span { color: var(--accent-bright); font-weight: 700; font-size: 16px; }
#last-transition {
  font-family: 'Courier New', monospace;
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 10px;
  max-width: 400px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#machine-status {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 20px;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
}
#machine-status.running { color: var(--accent-green); border-color: var(--accent-green); }
#machine-status.paused { color: var(--accent-yellow); border-color: var(--accent-yellow); }
#machine-status.accepted { color: var(--accent-green); border-color: var(--accent-green); background: rgba(0,212,170,0.1); }
#machine-status.rejected { color: var(--accent-red); border-color: var(--accent-red); background: rgba(233,69,96,0.1); }
#machine-status.halted { color: var(--accent-yellow); border-color: var(--accent-yellow); }
#machine-status.error { color: var(--accent-red); border-color: var(--accent-red); }

/* ============================================================
   TAPES
   ============================================================ */
#tapes-container {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.tape-wrapper {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  transition: border-color 0.2s;
}
.tape-wrapper:hover { border-color: var(--border-bright); }
.tape-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.tape-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 2px 8px;
  border-radius: 4px;
}
.tape-head-pos {
  font-size: 11px;
  color: var(--text-muted);
  margin-left: auto;
}
.tape-scroll-container { overflow: hidden; position: relative; }
.tape-track {
  display: flex;
  align-items: center;
  gap: 2px;
  transition: transform 0.2s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  padding: 8px 0 4px;
  will-change: transform;
}
.tape-cell {
  width: 38px;
  height: 38px;
  min-width: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--border);
  border-radius: 6px;
  font-family: 'Courier New', Courier, monospace;
  font-size: 16px;
  font-weight: 700;
  color: var(--text-secondary);
  background: var(--bg-card);
  transition: all 0.2s;
  position: relative;
  cursor: pointer;
  user-select: none;
}
.tape-cell.blank { color: var(--text-muted); opacity: 0.5; }
.tape-cell.active {
  border-width: 2px;
  transform: scale(1.08);
  z-index: 2;
  color: var(--text-primary);
}
.tape-cell.active.t0 { border-color: #4a9eff; box-shadow: 0 0 12px rgba(74,158,255,0.5); background: rgba(74,158,255,0.12); animation: cellPulse0 1.5s infinite; }
.tape-cell.active.t1 { border-color: #00d4aa; box-shadow: 0 0 12px rgba(0,212,170,0.5); background: rgba(0,212,170,0.12); animation: cellPulse1 1.5s infinite; }
.tape-cell.active.t2 { border-color: #ff8c42; box-shadow: 0 0 12px rgba(255,140,66,0.5); background: rgba(255,140,66,0.12); animation: cellPulse2 1.5s infinite; }
.tape-cell.active.t3 { border-color: #b44fff; box-shadow: 0 0 12px rgba(180,79,255,0.5); background: rgba(180,79,255,0.12); animation: cellPulse3 1.5s infinite; }
.tape-cell.active.t4 { border-color: #ffd700; box-shadow: 0 0 12px rgba(255,215,0,0.5); background: rgba(255,215,0,0.12); animation: cellPulse4 1.5s infinite; }
.tape-cell.trail { opacity: 0.6; }
.tape-cell.trail-1 { border-color: rgba(74,158,255,0.3); }
.tape-cell.written { animation: writeFlash 0.4s ease; }
@keyframes writeFlash { 0%,100%{background:var(--bg-card)} 50%{background:rgba(255,215,0,0.25)} }
@keyframes cellPulse0 { 0%,100%{box-shadow:0 0 12px rgba(74,158,255,0.5)} 50%{box-shadow:0 0 22px rgba(74,158,255,0.9)} }
@keyframes cellPulse1 { 0%,100%{box-shadow:0 0 12px rgba(0,212,170,0.5)} 50%{box-shadow:0 0 22px rgba(0,212,170,0.9)} }
@keyframes cellPulse2 { 0%,100%{box-shadow:0 0 12px rgba(255,140,66,0.5)} 50%{box-shadow:0 0 22px rgba(255,140,66,0.9)} }
@keyframes cellPulse3 { 0%,100%{box-shadow:0 0 12px rgba(180,79,255,0.5)} 50%{box-shadow:0 0 22px rgba(180,79,255,0.9)} }
@keyframes cellPulse4 { 0%,100%{box-shadow:0 0 12px rgba(255,215,0,0.5)} 50%{box-shadow:0 0 22px rgba(255,215,0,0.9)} }

.tape-head-indicator {
  width: 38px;
  min-width: 38px;
  display: flex;
  justify-content: center;
  margin-bottom: 2px;
}
.tape-head-arrow {
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 10px solid;
  transition: all 0.2s;
}

/* ============================================================
   CONTROLS
   ============================================================ */
#controls-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}
#speed-control {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-secondary);
  margin-left: auto;
}
#speed-slider {
  -webkit-appearance: none;
  width: 100px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  outline: none;
}
#speed-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent-bright);
  cursor: pointer;
  transition: transform 0.1s;
}
#speed-slider::-webkit-slider-thumb:hover { transform: scale(1.3); }

/* ============================================================
   STATE DIAGRAM
   ============================================================ */
#diagram-container {
  height: 180px;
  min-height: 160px;
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
}
#state-diagram-svg {
  width: 100%;
  height: 100%;
}
@keyframes dashFlow {
  from { stroke-dashoffset: 20; }
  to   { stroke-dashoffset: 0; }
}

/* ============================================================
   LOG
   ============================================================ */
#log-container {
  flex: 1;
  overflow-y: auto;
  font-family: 'Courier New', monospace;
  font-size: 11px;
}
.log-entry {
  display: grid;
  grid-template-columns: 40px 80px 1fr 70px;
  gap: 4px;
  padding: 3px 10px;
  border-bottom: 1px solid rgba(255,255,255,0.03);
  cursor: pointer;
  transition: background 0.1s;
  align-items: center;
}
.log-entry:hover { background: var(--bg-card); }
.log-entry.current { background: rgba(74,158,255,0.08); border-left: 2px solid var(--accent-bright); }
.log-entry.breakpoint { background: rgba(233,69,96,0.08); border-left: 2px solid var(--accent-red); }
.log-step { color: var(--text-muted); font-size: 10px; }
.log-state { color: var(--accent-bright); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-detail { color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-result { color: var(--text-secondary); text-align: right; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bp-marker {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent-red);
  flex-shrink: 0;
}

/* ============================================================
   TOAST
   ============================================================ */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--bg-panel);
  border: 1px solid var(--border-bright);
  border-radius: var(--radius);
  padding: 10px 24px;
  font-size: 13px;
  color: var(--text-primary);
  box-shadow: var(--shadow);
  z-index: 1000;
  transition: transform 0.3s;
  pointer-events: none;
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast.success { border-color: var(--accent-green); }
#toast.error { border-color: var(--accent-red); }

/* ============================================================
   MISC
   ============================================================ */
.tag {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.5px;
}
.tag-blue { background: rgba(74,158,255,0.15); color: var(--accent-bright); }
.tag-green { background: rgba(0,212,170,0.15); color: var(--accent-green); }
.tag-red { background: rgba(233,69,96,0.15); color: var(--accent-red); }
.tag-orange { background: rgba(255,140,66,0.15); color: var(--accent-orange); }
.divider { height: 1px; background: var(--border); margin: 8px 0; }
.tooltip-container { position: relative; }
.tooltip {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-panel);
  border: 1px solid var(--border-bright);
  color: var(--text-primary);
  font-size: 11px;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  white-space: nowrap;
  z-index: 200;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
  box-shadow: var(--shadow);
}
.tooltip-container:hover .tooltip { opacity: 1; }
.example-desc {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.6;
  margin-top: 8px;
  display: none;
}
.example-desc.visible { display: block; }
.breakpoint-list { font-size: 11px; }
.bp-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 0;
  color: var(--text-secondary);
}
.bp-item input[type="text"] { padding: 2px 6px; font-size: 11px; }
.btn-tiny {
  padding: 2px 6px;
  font-size: 10px;
  border-radius: 3px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-muted);
  cursor: pointer;
}
.btn-tiny:hover { border-color: var(--accent-red); color: var(--accent-red); }
#export-import { display: flex; gap: 6px; }
.cell-edit-input {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg-secondary);
  border: 2px solid var(--accent-yellow);
  border-radius: 4px;
  text-align: center;
  font-family: 'Courier New', monospace;
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  outline: none;
  z-index: 10;
}
#guided-walkthrough {
  background: rgba(74,158,255,0.08);
  border: 1px solid rgba(74,158,255,0.3);
  border-radius: var(--radius-sm);
  padding: 10px;
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.6;
  display: none;
  margin-top: 8px;
}
#guided-walkthrough.visible { display: block; }
#guided-step { color: var(--accent-bright); font-weight: 600; }

/* Sound toggle */
#sound-toggle { font-size: 16px; cursor: pointer; background: none; border: none; color: var(--text-muted); transition: color 0.2s; }
#sound-toggle:hover { color: var(--text-primary); }
